Fix Sonar complains
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-snmp-manager / src / main / java / org / o / ran / oam / nf / oam / adopter / snmp / manager / SnmpTrapListener.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  O-RAN-SC
4  *  ================================================================================
5  *  Copyright © 2021 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.o.ran.oam.nf.oam.adopter.snmp.manager;
21
22 import java.io.IOException;
23 import org.o.ran.oam.nf.oam.adopter.api.VesEventNotifier;
24 import org.o.ran.oam.nf.oam.adopter.snmp.manager.api.TimeZoneOffsetService;
25 import org.o.ran.oam.nf.oam.adopter.snmp.manager.mapper.SnmpMapper;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.snmp4j.MessageDispatcher;
29 import org.snmp4j.MessageDispatcherImpl;
30 import org.snmp4j.Snmp;
31 import org.snmp4j.mp.MPv2c;
32 import org.snmp4j.smi.UdpAddress;
33 import org.snmp4j.transport.DefaultUdpTransportMapping;
34 import org.snmp4j.util.MultiThreadedMessageDispatcher;
35 import org.snmp4j.util.ThreadPool;
36
37 final class SnmpTrapListener implements Runnable {
38
39     private static final Logger LOG = LoggerFactory.getLogger(SnmpTrapListener.class);
40     private static final int THREADS_SIZE = 2;
41     private final String hostPortAddress;
42     private final SnmpCommandResponder commandResponder;
43
44     public SnmpTrapListener(final String host, final Integer port, final SnmpMapper mapper,
45             final VesEventNotifier vesEventNotifier, final TimeZoneOffsetService timeZoneOffsetService) {
46         LOG.info("SnmpTrapListener listening on {}:{}", host, port);
47         this.hostPortAddress = host + "/" + port;
48         this.commandResponder = new SnmpCommandResponder(timeZoneOffsetService, mapper, vesEventNotifier);
49     }
50
51     @Override
52     public synchronized void run() {
53         try (final var snmpTarget = new DefaultUdpTransportMapping(
54                 new UdpAddress(hostPortAddress))) {
55             final var threadPool = ThreadPool.create("SNMP_V2_Listener", THREADS_SIZE);
56             final var dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
57             dispatcher.addMessageProcessingModel(new MPv2c());
58             listenSnmp(dispatcher, snmpTarget);
59         } catch (final IOException e) {
60             LOG.error("Error occurred while listening to SNMP messages: {}", e.getMessage());
61         }
62     }
63
64     private synchronized void listenSnmp(final MessageDispatcher dispatcher,
65             final DefaultUdpTransportMapping snmpTarget) {
66         do {
67             try (final var snmp = new Snmp(dispatcher, snmpTarget)) {
68                 snmp.addCommandResponder(commandResponder);
69                 snmpTarget.listen();
70                 LOG.debug("Listening on {}", snmpTarget);
71                 wait();
72             } catch (final InterruptedException | IOException ex) {
73                 Thread.currentThread().interrupt();
74             }
75         } while (snmpTarget.isListening());
76     }
77 }