blob: d0a2a0ed30a5fca6609755285253ae6e271ca131 [file] [log] [blame]
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07001/*
Brian O'Connor180c1092017-08-03 22:46:14 -07002 * Copyright 2017-present Open Networking Foundation
David K. Bainbridgeeda2b052017-07-12 09:41:04 -07003 *
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 */
Amit Ghoshc29c7a92017-08-01 09:59:13 +010016package org.opencord.sadis.impl;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070017
18import static org.junit.Assert.assertEquals;
Amit Ghoshc29c7a92017-08-01 09:59:13 +010019import static org.junit.Assert.assertTrue;
Deepa vaddireddyad295192017-08-10 13:14:02 +053020import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.assertNull;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070022
23import java.io.InputStream;
24import java.time.Duration;
25import java.util.ArrayList;
26import java.util.HashMap;
27import java.util.List;
28import java.util.Map;
29
30import org.junit.After;
31import org.junit.Before;
32import org.junit.Test;
Amit Ghosh38b232a2017-07-23 15:11:56 +010033import org.onlab.packet.Ip4Address;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070034import org.onlab.packet.MacAddress;
35import org.onlab.packet.VlanId;
36import org.onosproject.core.ApplicationId;
37import org.onosproject.core.CoreServiceAdapter;
38import org.onosproject.net.config.Config;
39import org.onosproject.net.config.ConfigApplyDelegate;
40import org.onosproject.net.config.NetworkConfigRegistryAdapter;
Deepa vaddireddy386f38b2017-08-02 06:24:01 +000041import org.onosproject.codec.impl.CodecManager;
Deepa vaddireddyad295192017-08-10 13:14:02 +053042import org.onosproject.net.config.NetworkConfigEvent;
43import org.onosproject.net.config.NetworkConfigListener;
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070044
Amit Ghoshc29c7a92017-08-01 09:59:13 +010045import org.opencord.sadis.SubscriberAndDeviceInformation;
46
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070047import com.fasterxml.jackson.databind.JsonNode;
48import com.fasterxml.jackson.databind.ObjectMapper;
49
50/**
51 * Set of tests of the SADIS ONOS application component.
52 */
53public class SadisManagerTest {
54
55 private SadisManager sadis;
Deepa vaddireddyad295192017-08-10 13:14:02 +053056 private ObjectMapper mapper;
57 private ApplicationId subject;
58 private ConfigApplyDelegate delegate;
59 private SadisConfig config;
60 private NetworkConfigEvent event;
61 private static NetworkConfigListener configListener;
62
63 SubscriberAndDeviceInformationBuilder entry1 = SubscriberAndDeviceInformationBuilder.build("1", (short) 2,
64 (short) 2, "1/1/2", (short) 125, (short) 3, "aa:bb:cc:dd:ee:ff", "XXX-NASID", "10.10.10.10",
65 "circuit123", "remote123");
66 SubscriberAndDeviceInformationBuilder entry2 = SubscriberAndDeviceInformationBuilder.build("2", (short) 4,
67 (short) 4, "1/1/2", (short) 129, (short) 4, "aa:bb:cc:dd:ee:ff", "YYY-NASID", "1.1.1.1",
68 "circuit234", "remote234");
69 SubscriberAndDeviceInformationBuilder entry3 = SubscriberAndDeviceInformationBuilder.build("3", (short) 7,
70 (short) 8, "1/1/2", (short) 130, (short) 7, "ff:aa:dd:cc:bb:ee", "MNO-NASID", "30.30.30.30",
71 "circuit567", "remote567");
72 SubscriberAndDeviceInformationBuilder entry4 = SubscriberAndDeviceInformationBuilder.build("4", (short) 2,
73 (short) 1, "1/1/2", (short) 132, (short) 1, "ff:cc:dd:aa:ee:bb", "PQR-NASID", "15.15.15.15",
74 "circuit678", "remote678");
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070075
76 @Before
77 public void setUp() throws Exception {
Deepa vaddireddyad295192017-08-10 13:14:02 +053078 sadis = new SadisManager();
79 sadis.coreService = new MockCoreService();
80 delegate = new MockConfigDelegate();
81 mapper = new ObjectMapper();
82 config = new SadisConfig();
83 subject = sadis.coreService.registerApplication("org.opencord.sadis");
84 config.init(subject, "sadis-local-mode-test", node("/LocalConfig.json"), mapper, delegate);
85 sadis.cfgService = new MockNetworkConfigRegistry(config);
86 event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, subject, config.getClass());
87 sadis.codecService = new CodecManager();
88 sadis.activate();
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070089 }
90
91 @After
92 public void tearDown() {
93 this.sadis.deactivate();
94 }
95
96 @Test
97 public void testConfiguration() {
98 SadisConfig config = sadis.cfgService.getConfig(null, SadisConfig.class);
99 assertEquals(true, config.getCacheEnabled());
100 assertEquals(50, config.getCacheMaxSize());
101 assertEquals(Duration.parse("PT1m"), config.getCacheTtl());
102 List<SubscriberAndDeviceInformation> entries = config.getEntries();
Amit Ghosh38b232a2017-07-23 15:11:56 +0100103 assertEquals(3, entries.size());
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100104 assertTrue(SubscriberAndDeviceInformationBuilder.build("1", (short) 2, (short) 2, "1/1/2", (short) 125,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700105 (short) 3, "aa:bb:cc:dd:ee:ff", "XXX-NASID", "10.10.10.10", "circuit123", "remote123")
106 .checkEquals(entries.get(0)));
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100107 assertTrue(SubscriberAndDeviceInformationBuilder.build("2", (short) 4, (short) 4, "1/1/2", (short) 129,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700108 (short) 4, "aa:bb:cc:dd:ee:ff", "YYY-NASID", "1.1.1.1", "circuit234", "remote234")
109 .checkEquals(entries.get(1)));
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100110 assertTrue(SubscriberAndDeviceInformationBuilder.build("cc:dd:ee:ff:aa:bb", (short) -1, (short) -1, null,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700111 (short) -1, (short) -1, "cc:dd:ee:ff:aa:bb", "CCC-NASID", "12.12.12.12", "circuit345", "remote345")
112 .checkEquals(entries.get(2)));
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700113
114 }
115
Deepa vaddireddyad295192017-08-10 13:14:02 +0530116 @Test
117 public void testLocalMode() throws Exception {
118 SubscriberAndDeviceInformation entry = sadis.get("3");
119 assertNull(entry);
120
121 entry = sadis.get("1");
122 assertNotNull(entry);
123 assertTrue(entry1.checkEquals(entry));
124
125 entry = sadis.get("2");
126 assertNotNull(entry);
127 assertTrue(entry2.checkEquals(entry));
128
129 sadis.invalidateId("1");
130 entry = sadis.getfromCache("1");
131 assertNull(entry);
132 entry = sadis.get("1");
133 assertNotNull(entry);
134 assertTrue(entry1.checkEquals(entry));
135
136 sadis.invalidateAll();
137 entry = sadis.getfromCache("2");
138 assertNull(entry);
139 entry = sadis.get("2");
140 assertNotNull(entry);
141 assertTrue(entry2.checkEquals(entry));
142 }
143
144 @Test
145 public void testRemoteMode() throws Exception {
146 config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate);
147 configListener.event(event);
148
149 SubscriberAndDeviceInformation entry = sadis.get("3");
150 assertNotNull(entry);
151 assertTrue(entry3.checkEquals(entry));
152
153 entry = sadis.get("4");
154 assertNotNull(entry);
155 assertTrue(entry4.checkEquals(entry));
156
157 sadis.invalidateId("3");
158 entry = sadis.getfromCache("3");
159 assertNull(entry);
160 entry = sadis.get("3");
161 assertNotNull(entry);
162 assertTrue(entry3.checkEquals(entry));
163
164 sadis.invalidateAll();
165 entry = sadis.getfromCache("4");
166 assertNull(entry);
167 entry = sadis.get("4");
168 assertNotNull(entry);
169 assertTrue(entry4.checkEquals(entry));
170
171 entry = sadis.get("8");
172 assertNull(entry);
173 }
174
175 @Test
176 public void testModeSwitch() throws Exception {
177 config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate);
178 configListener.event(event);
179 SubscriberAndDeviceInformation entry = sadis.get("3");
180 assertNotNull(entry);
181 entry = sadis.get("1");
182 assertNull(entry);
183
184 config.init(subject, "sadis-local-mode-test", node("/LocalConfig.json"), mapper, delegate);
185 configListener.event(event);
186 entry = sadis.get("1");
187 assertNotNull(entry);
188 entry = sadis.get("3");
189 assertNull(entry);
190 }
191
192 private JsonNode node(String jsonFile) throws Exception {
193 final InputStream jsonStream = SadisManagerTest.class.getResourceAsStream(jsonFile);
194 final JsonNode testConfig = mapper.readTree(jsonStream);
195 return testConfig;
196 }
197
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700198 // Mocks live here
199
200 private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
201
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100202 public static SubscriberAndDeviceInformationBuilder build(String id, short cTag, short sTag, String nasPortId,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700203 short port, short slot, String mac, String nasId, String ipAddress, String circuitId, String remoteId) {
204
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100205 SubscriberAndDeviceInformationBuilder info = new SubscriberAndDeviceInformationBuilder();
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700206 info.setId(id);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100207 if (cTag != -1) {
208 info.setCTag(VlanId.vlanId(cTag));
209 }
210 if (sTag != -1) {
211 info.setSTag(VlanId.vlanId(sTag));
212 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700213 info.setNasPortId(nasPortId);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100214 if (port != -1) {
215 info.setPort(port);
216 }
217 if (slot != -1) {
218 info.setSlot(slot);
219 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700220 info.setHardwareIdentifier(MacAddress.valueOf(mac));
Amit Ghosh38b232a2017-07-23 15:11:56 +0100221 info.setIPAddress(Ip4Address.valueOf(ipAddress));
222 info.setNasId(nasId);
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700223 info.setCircuitId(circuitId);
224 info.setRemoteId(remoteId);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700225 return info;
226 }
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100227
228 public boolean checkEquals(SubscriberAndDeviceInformation other) {
229 if (other == null) {
230 return false;
231 }
232 if (this.cTag() == null) {
233 if (other.cTag() != null) {
234 return false;
235 }
236 } else if (!this.cTag().equals(other.cTag())) {
237 return false;
238 }
239 if (this.hardwareIdentifier() == null) {
240 if (other.hardwareIdentifier() != null) {
241 return false;
242 }
243 } else if (!this.hardwareIdentifier().equals(other.hardwareIdentifier())) {
244 return false;
245 }
246 if (this.id() == null) {
247 if (other.id() != null) {
248 return false;
249 }
250 } else if (!this.id().equals(other.id())) {
251 return false;
252 }
253 if (this.nasPortId() == null) {
254 if (other.nasPortId() != null) {
255 return false;
256 }
257 } else if (!this.nasPortId().equals(other.nasPortId())) {
258 return false;
259 }
260 if (this.nasId() == null) {
261 if (other.nasId() != null) {
262 return false;
263 }
264 } else if (!this.nasId().equals(other.nasId())) {
265 return false;
266 }
267 if (this.ipAddress() == null) {
268 if (other.ipAddress() != null) {
269 return false;
270 }
271 } else if (!this.ipAddress().equals(other.ipAddress())) {
272 return false;
273 }
274 if (this.port() != other.port()) {
275 return false;
276 }
277 if (this.sTag() == null) {
278 if (other.sTag() != null) {
279 return false;
280 }
281 } else if (!this.sTag().equals(other.sTag())) {
282 return false;
283 }
284 if (this.slot() != other.slot()) {
285 return false;
286 }
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700287 if (this.circuitId() == null) {
288 if (other.circuitId() != null) {
289 return false;
290 }
291 } else if (!this.circuitId().equals(other.circuitId())) {
292 return false;
293 }
294 if (this.remoteId() == null) {
295 if (other.remoteId() != null) {
296 return false;
297 }
298 } else if (!this.remoteId().equals(other.remoteId())) {
299 return false;
300 }
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100301 return true;
302 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700303 }
304
305 /**
306 * Mocks an ONOS configuration delegate to allow JSON based configuration to
307 * be tested.
308 */
309 private static final class MockConfigDelegate implements ConfigApplyDelegate {
310 @Override
311 public void onApply(@SuppressWarnings("rawtypes") Config config) {
312 config.apply();
313 }
314 }
315
316 /**
317 * Mocks an instance of {@link ApplicationId} so that the application
318 * component under test can query and use its application ID.
319 */
320 private static final class MockApplicationId implements ApplicationId {
321
322 private final short id;
323 private final String name;
324
325 public MockApplicationId(short id, String name) {
326 this.id = id;
327 this.name = name;
328 }
329
330 @Override
331 public short id() {
332 return id;
333 }
334
335 @Override
336 public String name() {
337 return name;
338 }
339 }
340
341 /**
342 * Mocks the core services of ONOS so that the application under test can
343 * register and query application IDs.
344 */
345 private static final class MockCoreService extends CoreServiceAdapter {
346
347 private List<ApplicationId> idList = new ArrayList<ApplicationId>();
348 private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
349
350 /*
351 * (non-Javadoc)
352 *
353 * @see
354 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
355 */
356 @Override
357 public ApplicationId getAppId(Short id) {
358 if (id >= idList.size()) {
359 return null;
360 }
361 return idList.get(id);
362 }
363
364 /*
365 * (non-Javadoc)
366 *
367 * @see
368 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
369 */
370 @Override
371 public ApplicationId getAppId(String name) {
372 return idMap.get(name);
373 }
374
375 /*
376 * (non-Javadoc)
377 *
378 * @see
379 * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
380 * .String)
381 */
382 @Override
383 public ApplicationId registerApplication(String name) {
384 ApplicationId appId = idMap.get(name);
385 if (appId == null) {
386 appId = new MockApplicationId((short) idList.size(), name);
387 idList.add(appId);
388 idMap.put(name, appId);
389 }
390 return appId;
391 }
392
393 }
394
395 /**
396 * Mocks the ONOS network configuration registry so that the application
397 * under test can access a JSON defined configuration.
398 */
399 static final class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
400 private final SadisConfig config;
401
402 public MockNetworkConfigRegistry(final SadisConfig config) {
403 this.config = config;
404 }
405
406 @SuppressWarnings("unchecked")
407 @Override
408 public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
409 return (C) this.config;
410 }
Deepa vaddireddyad295192017-08-10 13:14:02 +0530411
412 @Override
413 public void addListener(NetworkConfigListener listener) {
414 configListener = listener;
415 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700416 }
417}