Release oam-ves-adopter Contatiner
[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 / SnmpCommandResponder.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.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;
32
33 class SnmpCommandResponder implements CommandResponder {
34
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;
39
40     public SnmpCommandResponder(final TimeZoneOffsetService timeZoneOffsetService, final SnmpMapper mapper,
41             final VesEventNotifier vesEventNotifier) {
42         this.timeZoneOffsetService = timeZoneOffsetService;
43         this.mapper = mapper;
44         this.vesEventNotifier = vesEventNotifier;
45     }
46
47     /**
48      * This method will be called whenever a pdu is received on the given port
49      * specified in the listen() method.
50      */
51     @Override
52     public synchronized void processPdu(final CommandResponderEvent cmdRespEvent) {
53         LOG.info("Received PDU");
54         final var pdu = cmdRespEvent.getPDU();
55         if (pdu == null) {
56             LOG.warn("Ignoring PDU.");
57             return;
58         }
59
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);
65
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();
70     }
71 }