2 * ============LICENSE_START=======================================================
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
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=========================================================
20 package org.o.ran.oam.nf.oam.adopter.snmp.manager;
22 import com.google.gson.Gson;
23 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24 import java.io.IOException;
25 import java.time.LocalDateTime;
26 import java.util.Optional;
27 import org.o.ran.oam.nf.oam.adopter.api.VesEventNotifier;
28 import org.o.ran.oam.nf.oam.adopter.snmp.manager.api.TimeZoneOffsetService;
29 import org.o.ran.oam.nf.oam.adopter.snmp.manager.mapper.SnmpMapper;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.snmp4j.CommandResponder;
33 import org.snmp4j.CommandResponderEvent;
34 import org.snmp4j.MessageDispatcher;
35 import org.snmp4j.MessageDispatcherImpl;
36 import org.snmp4j.Snmp;
37 import org.snmp4j.mp.MPv2c;
38 import org.snmp4j.smi.UdpAddress;
39 import org.snmp4j.transport.DefaultUdpTransportMapping;
40 import org.snmp4j.util.MultiThreadedMessageDispatcher;
41 import org.snmp4j.util.ThreadPool;
43 final class SnmpTrapListener implements CommandResponder, Runnable {
45 private static final Logger LOG = LoggerFactory.getLogger(SnmpTrapListener.class);
46 private static final int THREADS_SIZE = 2;
47 private final String hostPortAddress;
48 private final SnmpMapper mapper;
49 private final VesEventNotifier vesEventNotifier;
50 private final Gson gson = new Gson();
51 private final TimeZoneOffsetService timeZoneOffsetService;
53 public SnmpTrapListener(final String host, final Integer port, final SnmpMapper mapper,
54 final VesEventNotifier vesEventNotifier, final TimeZoneOffsetService timeZoneOffsetService) {
55 LOG.info("SnmpTrapListener listening on {}:{}", host, port);
56 this.hostPortAddress = host + "/" + port;
58 this.vesEventNotifier = vesEventNotifier;
59 this.timeZoneOffsetService = timeZoneOffsetService;
63 public synchronized void run() {
64 try (final var snmpTarget = new DefaultUdpTransportMapping(
65 new UdpAddress(hostPortAddress))) {
66 final var threadPool = ThreadPool.create("SNMP_V2_Listener", THREADS_SIZE);
67 final var dispatcher = new MultiThreadedMessageDispatcher(threadPool, new MessageDispatcherImpl());
68 dispatcher.addMessageProcessingModel(new MPv2c());
69 listenSnmp(dispatcher, snmpTarget);
70 } catch (final IOException e) {
71 LOG.error("Error occurred while listening to SNMP messages: {}", e.getMessage());
75 @SuppressFBWarnings("WA_NOT_IN_LOOP")
76 private synchronized void listenSnmp(final MessageDispatcher dispatcher,
77 final DefaultUdpTransportMapping snmpTarget) {
78 try (final var snmp = new Snmp(dispatcher, snmpTarget)) {
79 snmp.addCommandResponder(this);
81 LOG.debug("Listening on {}", snmpTarget);
83 } catch (final InterruptedException | IOException ex) {
84 Thread.currentThread().interrupt();
89 * This method will be called whenever a pdu is received on the given port
90 * specified in the listen() method.
93 public synchronized void processPdu(final CommandResponderEvent cmdRespEvent) {
94 LOG.info("Received PDU");
95 final var pdu = cmdRespEvent.getPDU();
97 LOG.warn("Ignoring PDU.");
101 final UdpAddress address = (UdpAddress) cmdRespEvent.getPeerAddress();
102 final var optZoneId = timeZoneOffsetService.getTimeZone(address.getInetAddress().getHostAddress());
103 final String timeZone = Optional.ofNullable(optZoneId)
104 .map(zoneId -> "UTC" + LocalDateTime.now().atZone(zoneId).getOffset().toString()).orElse(null);
106 mapper.toEvent(address, timeZone, pdu)
107 .flatMapCompletable(vesEventNotifier::notifyEvents)
108 .doOnSubscribe(result -> LOG.debug("SNMP Trap processing started"))
109 .doOnComplete(() -> LOG.debug("SNMP Trap processed successfully"))
110 .doOnError(error -> LOG.error("Failed to process SNMP Trap", error))