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 java.time.LocalDateTime;
23 import java.util.Optional;
24 import org.o.ran.oam.nf.oam.adopter.api.VesEventNotifier;
25 import org.o.ran.oam.nf.oam.adopter.snmp.manager.api.TimeZoneOffsetService;
26 import org.o.ran.oam.nf.oam.adopter.snmp.manager.mapper.SnmpMapper;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.snmp4j.CommandResponder;
30 import org.snmp4j.CommandResponderEvent;
31 import org.snmp4j.smi.UdpAddress;
33 class SnmpCommandResponder implements CommandResponder {
35 private static final Logger LOG = LoggerFactory.getLogger(SnmpCommandResponder.class);
36 private final TimeZoneOffsetService timeZoneOffsetService;
37 private final SnmpMapper mapper;
38 private final VesEventNotifier vesEventNotifier;
40 public SnmpCommandResponder(final TimeZoneOffsetService timeZoneOffsetService, final SnmpMapper mapper,
41 final VesEventNotifier vesEventNotifier) {
42 this.timeZoneOffsetService = timeZoneOffsetService;
44 this.vesEventNotifier = vesEventNotifier;
48 * This method will be called whenever a pdu is received on the given port
49 * specified in the listen() method.
52 public synchronized void processPdu(final CommandResponderEvent cmdRespEvent) {
53 LOG.info("Received PDU");
54 final var pdu = cmdRespEvent.getPDU();
56 LOG.warn("Ignoring PDU.");
60 final UdpAddress address = (UdpAddress) cmdRespEvent.getPeerAddress();
61 final var optZoneId = timeZoneOffsetService.getTimeZone(address.getInetAddress().getHostAddress());
62 final String timeZone = Optional.ofNullable(optZoneId)
63 .map(zoneId -> "UTC" + LocalDateTime.now().atZone(zoneId).getOffset()
64 .toString()).orElse(null);
66 mapper.toEvent(address, timeZone, pdu).flatMapCompletable(vesEventNotifier::notifyEvents)
67 .doOnSubscribe(result -> LOG.debug("SNMP Trap processing started"))
68 .doOnComplete(() -> LOG.debug("SNMP Trap processed successfully"))
69 .doOnError(error -> LOG.error("Failed to process SNMP Trap", error)).subscribe();