blob: 5ea4dc47d45f801914a03826b0161dc5cd0544fb [file] [log] [blame]
senthil9d33aa22021-10-05 09:59:32 +05301/*
2 * Copyright 2021-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.snmp.ctl;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.net.DeviceId;
25import org.onosproject.net.config.Config;
26import org.onosproject.net.config.ConfigApplyDelegate;
27import org.onosproject.snmp.SnmpDeviceConfig;
28import org.onosproject.snmp.SnmpException;
29import org.snmp4j.TransportMapping;
30import org.snmp4j.security.AuthSHA;
31import org.snmp4j.security.PrivAES128;
32import org.snmp4j.security.SecurityLevel;
33import org.snmp4j.smi.Address;
34import org.snmp4j.smi.GenericAddress;
35import org.snmp4j.transport.DefaultUdpTransportMapping;
36
37import java.io.IOException;
38import java.io.InputStream;
39
40import static org.junit.Assert.assertEquals;
41
42/**
43 * Test class for V3SnmpConfiguration.
44 */
45public class V3SnmpConfigurationTest {
46
47 private final SnmpDeviceConfig config = new SnmpDeviceConfig();
48 private final InputStream jsonStream = DefaultSnmpv3DeviceTest.class
49 .getResourceAsStream("/device.json");
50 private final ObjectMapper mapper = new ObjectMapper();
51
52 private static final String KEY = "snmp";
53 private final String snmpHost = "1.1.1.1";
54 private final int snmpPort = 1;
55 private final String username = "test";
56 private final String community = "test";
57 private final DeviceId deviceId = DeviceId.deviceId("snmp:1.1.1.1:1");
58 private final String defaultProtocol = "udp";
59 private final String securityName = "sdnonos";
60 private final String authProtocol = "SHA";
61 private final String authPassword = "sdn@1234";
62 private final String privPassword = "sdn@1234";
63 private final String contextName = "sdn-context";
64
65 private static final String SLASH = "/";
66
67 private DefaultSnmpv3Device defaultSnmpv3Device;
68 private V3SnmpConfiguration v3SnmpConfiguration;
69
70 @Before
71 public void setUp() throws Exception {
72 JsonNode jsonNode = mapper.readTree(jsonStream);
73 ConfigApplyDelegate delegate = new MockDelegate();
74 config.init(deviceId, KEY, jsonNode, mapper, delegate);
75 defaultSnmpv3Device = new DefaultSnmpv3Device(config);
76
77 v3SnmpConfiguration = V3SnmpConfiguration.builder()
78 .setAddress(defaultSnmpv3Device.getSnmpHost())
79 .setSecurityName(defaultSnmpv3Device.getSecurityName())
80 .setSecurityLevel(defaultSnmpv3Device.getSecurityLevel())
81 .setAuthenticationProtocol(defaultSnmpv3Device.getAuthProtocol())
82 .setAuthenticationPassword(defaultSnmpv3Device.getAuthPassword())
83 .setPrivacyProtocol(defaultSnmpv3Device.getPrivProtocol())
84 .setPrivacyPassword(defaultSnmpv3Device.getPrivPassword())
85 .setContextName(defaultSnmpv3Device.getContextName())
86 .build();
87
88 v3SnmpConfiguration.setPort(defaultSnmpv3Device.getSnmpPort());
89 }
90
91 /**
92 * Test snmp create target exception case.
93 *
94 * @throws IOException
95 */
96 @Test(expected = SnmpException.class)
97 public void testCreateTargetException() throws IOException {
98 Address targetAddress = GenericAddress.parse(
99 defaultSnmpv3Device.getProtocol() +
100 ":" + defaultSnmpv3Device.getSnmpHost() +
101 "/" + defaultSnmpv3Device.getSnmpPort());
102
103 TransportMapping transport = new DefaultUdpTransportMapping();
104 v3SnmpConfiguration.createSnmpSession(transport);
105 v3SnmpConfiguration.createTarget(targetAddress);
106
107 }
108
109 /**
110 * Test fetching security level.
111 */
112 @Test
113 public void testGetSecurityLevel() {
114 assertEquals(SecurityLevel.AUTH_PRIV, v3SnmpConfiguration.getSecurityLevel().getSnmpValue());
115 }
116
117 /**
118 * Test fetching snmp host address.
119 */
120 @Test
121 public void testGetAddress() {
122 assertEquals(snmpHost, v3SnmpConfiguration.getAddress().toString());
123 }
124
125 /**
126 * Test fetching security name.
127 */
128 @Test
129 public void testGetSecurityName() {
130 assertEquals(securityName, v3SnmpConfiguration.getSecurityName());
131 }
132
133 /**
134 * Test fetching authentication password.
135 */
136 @Test
137 public void testGetAuthenticationPassword() {
138 assertEquals(authPassword, v3SnmpConfiguration.getAuthenticationPassword());
139 }
140
141 /**
142 * Test fetching authentication protocol.
143 */
144 @Test
145 public void testGetAuthenticationProtocol() {
146 assertEquals(AuthSHA.ID, v3SnmpConfiguration.getAuthenticationProtocol());
147 }
148
149 /**
150 * Test fetching privacy password.
151 */
152 @Test
153 public void testGetPrivacyPassword() {
154 assertEquals(privPassword, v3SnmpConfiguration.getPrivacyPassword());
155 }
156
157 /**
158 * Test fetching privacy protocol.
159 */
160 @Test
161 public void testGetPrivacyProtocol() {
162 assertEquals(PrivAES128.ID, v3SnmpConfiguration.getPrivacyProtocol());
163 }
164
165 /**
166 * Test fetching context name.
167 */
168 @Test
169 public void testGetContextName() {
170 assertEquals(contextName, v3SnmpConfiguration.getContextName());
171 }
172
173 private class MockDelegate implements ConfigApplyDelegate {
174 @Override
175 public void onApply(Config config) {
176
177 }
178 }
179
180}