blob: 693b30e4714b09a2a031bfa075cac5b4bb0df477 [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);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -070099 assertEquals(50, config.getCacheMaxSize());
100 assertEquals(Duration.parse("PT1m"), config.getCacheTtl());
101 List<SubscriberAndDeviceInformation> entries = config.getEntries();
Amit Ghosh38b232a2017-07-23 15:11:56 +0100102 assertEquals(3, entries.size());
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100103 assertTrue(SubscriberAndDeviceInformationBuilder.build("1", (short) 2, (short) 2, "1/1/2", (short) 125,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700104 (short) 3, "aa:bb:cc:dd:ee:ff", "XXX-NASID", "10.10.10.10", "circuit123", "remote123")
105 .checkEquals(entries.get(0)));
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100106 assertTrue(SubscriberAndDeviceInformationBuilder.build("2", (short) 4, (short) 4, "1/1/2", (short) 129,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700107 (short) 4, "aa:bb:cc:dd:ee:ff", "YYY-NASID", "1.1.1.1", "circuit234", "remote234")
108 .checkEquals(entries.get(1)));
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100109 assertTrue(SubscriberAndDeviceInformationBuilder.build("cc:dd:ee:ff:aa:bb", (short) -1, (short) -1, null,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700110 (short) -1, (short) -1, "cc:dd:ee:ff:aa:bb", "CCC-NASID", "12.12.12.12", "circuit345", "remote345")
111 .checkEquals(entries.get(2)));
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700112
113 }
114
Deepa vaddireddyad295192017-08-10 13:14:02 +0530115 @Test
116 public void testLocalMode() throws Exception {
117 SubscriberAndDeviceInformation entry = sadis.get("3");
118 assertNull(entry);
119
120 entry = sadis.get("1");
121 assertNotNull(entry);
122 assertTrue(entry1.checkEquals(entry));
123
124 entry = sadis.get("2");
125 assertNotNull(entry);
126 assertTrue(entry2.checkEquals(entry));
127
128 sadis.invalidateId("1");
129 entry = sadis.getfromCache("1");
130 assertNull(entry);
131 entry = sadis.get("1");
132 assertNotNull(entry);
133 assertTrue(entry1.checkEquals(entry));
134
135 sadis.invalidateAll();
136 entry = sadis.getfromCache("2");
137 assertNull(entry);
138 entry = sadis.get("2");
139 assertNotNull(entry);
140 assertTrue(entry2.checkEquals(entry));
141 }
142
143 @Test
144 public void testRemoteMode() throws Exception {
145 config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate);
146 configListener.event(event);
147
148 SubscriberAndDeviceInformation entry = sadis.get("3");
149 assertNotNull(entry);
150 assertTrue(entry3.checkEquals(entry));
151
152 entry = sadis.get("4");
153 assertNotNull(entry);
154 assertTrue(entry4.checkEquals(entry));
155
156 sadis.invalidateId("3");
157 entry = sadis.getfromCache("3");
158 assertNull(entry);
159 entry = sadis.get("3");
160 assertNotNull(entry);
161 assertTrue(entry3.checkEquals(entry));
162
163 sadis.invalidateAll();
164 entry = sadis.getfromCache("4");
165 assertNull(entry);
166 entry = sadis.get("4");
167 assertNotNull(entry);
168 assertTrue(entry4.checkEquals(entry));
169
170 entry = sadis.get("8");
171 assertNull(entry);
172 }
173
174 @Test
175 public void testModeSwitch() throws Exception {
176 config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate);
177 configListener.event(event);
178 SubscriberAndDeviceInformation entry = sadis.get("3");
179 assertNotNull(entry);
180 entry = sadis.get("1");
181 assertNull(entry);
182
183 config.init(subject, "sadis-local-mode-test", node("/LocalConfig.json"), mapper, delegate);
184 configListener.event(event);
185 entry = sadis.get("1");
186 assertNotNull(entry);
187 entry = sadis.get("3");
188 assertNull(entry);
189 }
190
191 private JsonNode node(String jsonFile) throws Exception {
192 final InputStream jsonStream = SadisManagerTest.class.getResourceAsStream(jsonFile);
193 final JsonNode testConfig = mapper.readTree(jsonStream);
194 return testConfig;
195 }
196
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700197 // Mocks live here
198
199 private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
200
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100201 public static SubscriberAndDeviceInformationBuilder build(String id, short cTag, short sTag, String nasPortId,
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700202 short port, short slot, String mac, String nasId, String ipAddress, String circuitId, String remoteId) {
203
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100204 SubscriberAndDeviceInformationBuilder info = new SubscriberAndDeviceInformationBuilder();
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700205 info.setId(id);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100206 if (cTag != -1) {
207 info.setCTag(VlanId.vlanId(cTag));
208 }
209 if (sTag != -1) {
210 info.setSTag(VlanId.vlanId(sTag));
211 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700212 info.setNasPortId(nasPortId);
Amit Ghosh38b232a2017-07-23 15:11:56 +0100213 if (port != -1) {
214 info.setPort(port);
215 }
216 if (slot != -1) {
217 info.setSlot(slot);
218 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700219 info.setHardwareIdentifier(MacAddress.valueOf(mac));
Amit Ghosh38b232a2017-07-23 15:11:56 +0100220 info.setIPAddress(Ip4Address.valueOf(ipAddress));
221 info.setNasId(nasId);
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700222 info.setCircuitId(circuitId);
223 info.setRemoteId(remoteId);
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700224 return info;
225 }
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100226
227 public boolean checkEquals(SubscriberAndDeviceInformation other) {
228 if (other == null) {
229 return false;
230 }
231 if (this.cTag() == null) {
232 if (other.cTag() != null) {
233 return false;
234 }
235 } else if (!this.cTag().equals(other.cTag())) {
236 return false;
237 }
238 if (this.hardwareIdentifier() == null) {
239 if (other.hardwareIdentifier() != null) {
240 return false;
241 }
242 } else if (!this.hardwareIdentifier().equals(other.hardwareIdentifier())) {
243 return false;
244 }
245 if (this.id() == null) {
246 if (other.id() != null) {
247 return false;
248 }
249 } else if (!this.id().equals(other.id())) {
250 return false;
251 }
252 if (this.nasPortId() == null) {
253 if (other.nasPortId() != null) {
254 return false;
255 }
256 } else if (!this.nasPortId().equals(other.nasPortId())) {
257 return false;
258 }
259 if (this.nasId() == null) {
260 if (other.nasId() != null) {
261 return false;
262 }
263 } else if (!this.nasId().equals(other.nasId())) {
264 return false;
265 }
266 if (this.ipAddress() == null) {
267 if (other.ipAddress() != null) {
268 return false;
269 }
270 } else if (!this.ipAddress().equals(other.ipAddress())) {
271 return false;
272 }
273 if (this.port() != other.port()) {
274 return false;
275 }
276 if (this.sTag() == null) {
277 if (other.sTag() != null) {
278 return false;
279 }
280 } else if (!this.sTag().equals(other.sTag())) {
281 return false;
282 }
283 if (this.slot() != other.slot()) {
284 return false;
285 }
David K. Bainbridge8bf98e02017-08-07 10:41:56 -0700286 if (this.circuitId() == null) {
287 if (other.circuitId() != null) {
288 return false;
289 }
290 } else if (!this.circuitId().equals(other.circuitId())) {
291 return false;
292 }
293 if (this.remoteId() == null) {
294 if (other.remoteId() != null) {
295 return false;
296 }
297 } else if (!this.remoteId().equals(other.remoteId())) {
298 return false;
299 }
Amit Ghoshc29c7a92017-08-01 09:59:13 +0100300 return true;
301 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700302 }
303
304 /**
305 * Mocks an ONOS configuration delegate to allow JSON based configuration to
306 * be tested.
307 */
308 private static final class MockConfigDelegate implements ConfigApplyDelegate {
309 @Override
310 public void onApply(@SuppressWarnings("rawtypes") Config config) {
311 config.apply();
312 }
313 }
314
315 /**
316 * Mocks an instance of {@link ApplicationId} so that the application
317 * component under test can query and use its application ID.
318 */
319 private static final class MockApplicationId implements ApplicationId {
320
321 private final short id;
322 private final String name;
323
324 public MockApplicationId(short id, String name) {
325 this.id = id;
326 this.name = name;
327 }
328
329 @Override
330 public short id() {
331 return id;
332 }
333
334 @Override
335 public String name() {
336 return name;
337 }
338 }
339
340 /**
341 * Mocks the core services of ONOS so that the application under test can
342 * register and query application IDs.
343 */
344 private static final class MockCoreService extends CoreServiceAdapter {
345
346 private List<ApplicationId> idList = new ArrayList<ApplicationId>();
347 private Map<String, ApplicationId> idMap = new HashMap<String, ApplicationId>();
348
349 /*
350 * (non-Javadoc)
351 *
352 * @see
353 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short)
354 */
355 @Override
356 public ApplicationId getAppId(Short id) {
357 if (id >= idList.size()) {
358 return null;
359 }
360 return idList.get(id);
361 }
362
363 /*
364 * (non-Javadoc)
365 *
366 * @see
367 * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String)
368 */
369 @Override
370 public ApplicationId getAppId(String name) {
371 return idMap.get(name);
372 }
373
374 /*
375 * (non-Javadoc)
376 *
377 * @see
378 * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang
379 * .String)
380 */
381 @Override
382 public ApplicationId registerApplication(String name) {
383 ApplicationId appId = idMap.get(name);
384 if (appId == null) {
385 appId = new MockApplicationId((short) idList.size(), name);
386 idList.add(appId);
387 idMap.put(name, appId);
388 }
389 return appId;
390 }
391
392 }
393
394 /**
395 * Mocks the ONOS network configuration registry so that the application
396 * under test can access a JSON defined configuration.
397 */
398 static final class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
399 private final SadisConfig config;
400
401 public MockNetworkConfigRegistry(final SadisConfig config) {
402 this.config = config;
403 }
404
405 @SuppressWarnings("unchecked")
406 @Override
407 public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
408 return (C) this.config;
409 }
Deepa vaddireddyad295192017-08-10 13:14:02 +0530410
411 @Override
412 public void addListener(NetworkConfigListener listener) {
413 configListener = listener;
414 }
David K. Bainbridgeeda2b052017-07-12 09:41:04 -0700415 }
416}