Fix Sonar complains
[oam/nf-oam-adopter.git] / ves-nf-oam-adopter / ves-nf-oam-adopter-snmp-manager / src / test / java / org / o / ran / oam / nf / oam / adopter / snmp / manager / SnmpTestUtil.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 lombok.experimental.UtilityClass;
24 import org.snmp4j.CommunityTarget;
25 import org.snmp4j.PDU;
26 import org.snmp4j.Snmp;
27 import org.snmp4j.mp.SnmpConstants;
28 import org.snmp4j.smi.OID;
29 import org.snmp4j.smi.OctetString;
30 import org.snmp4j.smi.TimeTicks;
31 import org.snmp4j.smi.UdpAddress;
32 import org.snmp4j.smi.Variable;
33 import org.snmp4j.smi.VariableBinding;
34 import org.snmp4j.transport.DefaultUdpTransportMapping;
35
36 @UtilityClass
37 public class SnmpTestUtil {
38     public static final OID PORT_DOWN_OID = new OID(new int[] {1, 3, 6, 1, 4, 1, 1007, 0, 0, 1, 0, 1});
39     public static final OID BOX_NAME = new OID(new int[] {1, 3, 6, 1, 4, 1, 1007, 0, 0, 1, 0, 2});
40     public static final OID NOTIFICATION_DESCRIPTION = new OID(new int[] {1, 3, 6, 1, 4, 1, 1007, 0, 0, 1, 0, 3});
41     public static final OID START_EPOCH = new OID(new int[] {1, 3, 6, 1, 4, 1, 1007, 0, 0, 1, 0, 4});
42     public static final OID LAST_EPOCH = new OID(new int[] {1, 3, 6, 1, 4, 1, 1007, 0, 0, 1, 0, 5});
43     public static final OID BOX_SERIAL = new OID(new int[] {1, 3, 6, 1, 4, 1, 1007, 0, 0, 1, 0, 6});
44     public static final OID NOTIFICATION_INTERFACE = new OID(new int[] {1, 3, 6, 1, 4, 1, 1007, 0, 0, 1, 0, 7});
45
46     private static void sndTrap(final PDU trap, final String host, final String port) throws IOException {
47         final UdpAddress targetaddress = new UdpAddress(host + "/" + port);
48         final CommunityTarget<UdpAddress> target = new CommunityTarget<>();
49         target.setCommunity(new OctetString("public"));
50         target.setVersion(SnmpConstants.version2c);
51         target.setAddress(targetaddress);
52
53         final Snmp snmp = new Snmp(new DefaultUdpTransportMapping());
54         snmp.send(trap, target, null, null);
55         snmp.close();
56     }
57
58     /**
59      * Mock Default trap.
60      */
61     public static void sendDefaultTrapV2(final String host, final String port) throws IOException {
62         final PDU trap = new PDU();
63         trap.setType(PDU.TRAP);
64
65         final OID oid = new OID("1.2.3.4.5");
66         trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, oid));
67         trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
68         trap.add(new VariableBinding(SnmpConstants.sysDescr, new OctetString("System Description")));
69
70         // Add Payload
71         final Variable var = new OctetString("some string");
72         trap.add(new VariableBinding(oid, var));
73
74         sndTrap(trap, host, port);
75     }
76
77     /**
78      * Mock trap.
79      */
80     public static void sendPortDownTrapV2(final String host, final String port) throws IOException {
81         final PDU trap = new PDU();
82         trap.setType(PDU.TRAP);
83
84         trap.add(new VariableBinding(SnmpConstants.snmpTrapOID, PORT_DOWN_OID));
85         trap.add(new VariableBinding(SnmpConstants.sysUpTime, new TimeTicks(5000)));
86
87         // Add Payload
88         trap.add(new VariableBinding(BOX_NAME, new OctetString("OAM-BOX")));
89         trap.add(new VariableBinding(BOX_SERIAL, new OctetString("10283")));
90         trap.add(new VariableBinding(NOTIFICATION_DESCRIPTION, new OctetString("Port DOWN")));
91         trap.add(new VariableBinding(START_EPOCH, new OctetString("1613592976108380")));
92         trap.add(new VariableBinding(LAST_EPOCH, new OctetString("1613592976108880")));
93         trap.add(new VariableBinding(NOTIFICATION_INTERFACE, new OctetString("A0")));
94         sndTrap(trap, host, port);
95     }
96 }