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