Fix Sonar complains
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-mock / src / main / java / org / o / ran / oam / nf / oam / adopter / mock / app / SnmpNotifier.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.mock.app;
21
22 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
23 import java.io.IOException;
24 import java.util.HashMap;
25 import org.o.ran.oam.nf.oam.adopter.mock.app.properties.SnmpProperties;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.snmp4j.CommunityTarget;
29 import org.snmp4j.PDU;
30 import org.snmp4j.Snmp;
31 import org.snmp4j.mp.SnmpConstants;
32 import org.snmp4j.smi.OID;
33 import org.snmp4j.smi.OctetString;
34 import org.snmp4j.smi.UdpAddress;
35 import org.snmp4j.smi.VariableBinding;
36 import org.snmp4j.transport.DefaultUdpTransportMapping;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.scheduling.annotation.EnableScheduling;
39 import org.springframework.scheduling.annotation.Scheduled;
40 import org.springframework.stereotype.Service;
41
42 @Service
43 @EnableScheduling
44 public class SnmpNotifier {
45
46     private static final Logger LOG = LoggerFactory.getLogger(SnmpNotifier.class);
47
48     private final HashMap<String, String> alarmTrap;
49     private final HashMap<String, String> clearTrap;
50     private final CommunityTarget<UdpAddress> target;
51
52     /**
53      * Default constructor.
54      */
55     @Autowired
56     public SnmpNotifier(final SnmpProperties snmpProperties) {
57         this.alarmTrap = snmpProperties.getAlarmTrap();
58         this.clearTrap = snmpProperties.getClearTrap();
59         this.target = new CommunityTarget<>();
60         target.setCommunity(new OctetString("public"));
61         target.setVersion(SnmpConstants.version2c);
62         target.setAddress(new UdpAddress(snmpProperties.getDestiny()));
63     }
64
65     @Scheduled(fixedDelayString = "${scheduler.fixedDelay}")
66     @SuppressFBWarnings("UPM_UNCALLED_PRIVATE_METHOD")
67     private void sendAlarmTrap() throws IOException {
68         sendTrap(target, alarmTrap, "alarm");
69     }
70
71     @Scheduled(fixedDelayString = "${scheduler.fixedDelay}", initialDelayString = "${scheduler.initialDelay}")
72     @SuppressFBWarnings("UPM_UNCALLED_PRIVATE_METHOD")
73     private void sendClearTrap() throws IOException {
74         sendTrap(target, clearTrap, "clear");
75     }
76
77     private static void sendTrap(final CommunityTarget<UdpAddress> target, final HashMap<String, String> trap,
78             final String trapType) throws IOException {
79         final PDU pdu = new PDU();
80         pdu.setType(PDU.TRAP);
81         trap.forEach((key, value) -> {
82             try {
83                 pdu.add(new VariableBinding(new OID(key), new OctetString(value)));
84             } catch (final Exception e) {
85                 LOG.error("Failed to parse oid / value : {}, {}", key, value, e);
86             }
87         });
88
89         try (final Snmp snmp = new Snmp(new DefaultUdpTransportMapping())) {
90             snmp.send(pdu, target, null, null);
91             LOG.info("Trap {} sent successfully.", trapType);
92         }
93     }
94 }