Manikandan K
Committed by Gerrit Code Review

ONOS-4107: ISIS Provider - Junit

Change-Id: Ieeb157e9be6f1ca54c9129713b7eaef26341244e
...@@ -21,20 +21,19 @@ import org.onlab.packet.Ip4Address; ...@@ -21,20 +21,19 @@ import org.onlab.packet.Ip4Address;
21 import org.onlab.util.Bandwidth; 21 import org.onlab.util.Bandwidth;
22 import org.onosproject.isis.controller.topology.IsisLinkTed; 22 import org.onosproject.isis.controller.topology.IsisLinkTed;
23 23
24 +import java.util.ArrayList;
24 import java.util.List; 25 import java.util.List;
25 26
26 /** 27 /**
27 * Representation of an ISIS device information. 28 * Representation of an ISIS device information.
28 */ 29 */
29 public class DefaultIsisLinkTed implements IsisLinkTed { 30 public class DefaultIsisLinkTed implements IsisLinkTed {
30 -
31 -
32 private int administrativeGroup; 31 private int administrativeGroup;
33 private Ip4Address ipv4InterfaceAddress; 32 private Ip4Address ipv4InterfaceAddress;
34 private Ip4Address ipv4NeighborAddress; 33 private Ip4Address ipv4NeighborAddress;
35 private Bandwidth maximumLinkBandwidth; 34 private Bandwidth maximumLinkBandwidth;
36 private Bandwidth maximumReservableLinkBandwidth; 35 private Bandwidth maximumReservableLinkBandwidth;
37 - private List<Bandwidth> unreservedBandwidth; 36 + private List<Bandwidth> unreservedBandwidth = new ArrayList<>();
38 private long teDefaultMetric; 37 private long teDefaultMetric;
39 38
40 @Override 39 @Override
...@@ -134,13 +133,13 @@ public class DefaultIsisLinkTed implements IsisLinkTed { ...@@ -134,13 +133,13 @@ public class DefaultIsisLinkTed implements IsisLinkTed {
134 Objects.equal(ipv4NeighborAddress, that.ipv4NeighborAddress) && 133 Objects.equal(ipv4NeighborAddress, that.ipv4NeighborAddress) &&
135 Objects.equal(maximumLinkBandwidth, that.maximumLinkBandwidth) && 134 Objects.equal(maximumLinkBandwidth, that.maximumLinkBandwidth) &&
136 Objects.equal(maximumReservableLinkBandwidth, 135 Objects.equal(maximumReservableLinkBandwidth,
137 - that.maximumReservableLinkBandwidth) && 136 + that.maximumReservableLinkBandwidth) &&
138 Objects.equal(teDefaultMetric, that.teDefaultMetric); 137 Objects.equal(teDefaultMetric, that.teDefaultMetric);
139 } 138 }
140 139
141 @Override 140 @Override
142 public int hashCode() { 141 public int hashCode() {
143 return Objects.hashCode(administrativeGroup, ipv4InterfaceAddress, 142 return Objects.hashCode(administrativeGroup, ipv4InterfaceAddress,
144 - ipv4NeighborAddress, maximumLinkBandwidth, teDefaultMetric); 143 + ipv4NeighborAddress, maximumLinkBandwidth, teDefaultMetric);
145 } 144 }
146 } 145 }
...\ No newline at end of file ...\ No newline at end of file
......
1 COMPILE_DEPS = [ 1 COMPILE_DEPS = [
2 '//lib:CORE_DEPS', 2 '//lib:CORE_DEPS',
3 '//protocols/isis/api:onos-protocols-isis-api', 3 '//protocols/isis/api:onos-protocols-isis-api',
4 + '//protocols/isis/ctl:onos-protocols-isis-ctl',
5 +]
6 +
7 +TEST_DEPS = [
8 + '//lib:TEST_ADAPTERS',
4 ] 9 ]
5 10
6 osgi_jar_with_tests ( 11 osgi_jar_with_tests (
7 deps = COMPILE_DEPS, 12 deps = COMPILE_DEPS,
13 + test_deps = TEST_DEPS,
8 ) 14 )
9 -
......
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 + * the License. You may obtain a copy of the License at
6 + *
7 + * http://www.apache.org/licenses/LICENSE-2.0
8 + *
9 + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 + * specific language governing permissions and limitations under the License.
12 + */
13 +
14 +package org.onosproject.provider.isis.topology.impl;
15 +
16 +import com.fasterxml.jackson.databind.JsonNode;
17 +import com.fasterxml.jackson.databind.ObjectMapper;
18 +import com.fasterxml.jackson.databind.node.JsonNodeFactory;
19 +import com.fasterxml.jackson.databind.node.ObjectNode;
20 +import org.junit.After;
21 +import org.junit.Before;
22 +import org.junit.Test;
23 +import org.onlab.packet.Ip4Address;
24 +import org.onlab.util.Bandwidth;
25 +import org.onosproject.isis.controller.IsisController;
26 +import org.onosproject.isis.controller.IsisProcess;
27 +import org.onosproject.isis.controller.impl.topology.DefaultIsisLink;
28 +import org.onosproject.isis.controller.impl.topology.DefaultIsisLinkTed;
29 +import org.onosproject.isis.controller.impl.topology.DefaultIsisRouter;
30 +import org.onosproject.isis.controller.topology.IsisLink;
31 +import org.onosproject.isis.controller.topology.IsisLinkListener;
32 +import org.onosproject.isis.controller.topology.IsisLinkTed;
33 +import org.onosproject.isis.controller.topology.IsisRouter;
34 +import org.onosproject.isis.controller.topology.IsisRouterListener;
35 +import org.onosproject.net.ConnectPoint;
36 +import org.onosproject.net.Device;
37 +import org.onosproject.net.DeviceId;
38 +import org.onosproject.net.Link;
39 +import org.onosproject.net.MastershipRole;
40 +import org.onosproject.net.PortNumber;
41 +import org.onosproject.net.config.Config;
42 +import org.onosproject.net.config.ConfigApplyDelegate;
43 +import org.onosproject.net.config.ConfigFactory;
44 +import org.onosproject.net.config.NetworkConfigRegistryAdapter;
45 +import org.onosproject.net.config.basics.BandwidthCapacity;
46 +import org.onosproject.net.device.DeviceDescription;
47 +import org.onosproject.net.device.DeviceListener;
48 +import org.onosproject.net.device.DeviceProvider;
49 +import org.onosproject.net.device.DeviceProviderRegistry;
50 +import org.onosproject.net.device.DeviceProviderService;
51 +import org.onosproject.net.device.DeviceServiceAdapter;
52 +import org.onosproject.net.device.PortDescription;
53 +import org.onosproject.net.device.PortStatistics;
54 +import org.onosproject.net.link.LinkDescription;
55 +import org.onosproject.net.link.LinkListener;
56 +import org.onosproject.net.link.LinkProvider;
57 +import org.onosproject.net.link.LinkProviderRegistry;
58 +import org.onosproject.net.link.LinkProviderService;
59 +import org.onosproject.net.link.LinkServiceAdapter;
60 +import org.onosproject.net.provider.ProviderId;
61 +
62 +import java.util.ArrayList;
63 +import java.util.Collection;
64 +import java.util.Collections;
65 +import java.util.HashMap;
66 +import java.util.HashSet;
67 +import java.util.List;
68 +import java.util.Map;
69 +import java.util.Set;
70 +import java.util.concurrent.CopyOnWriteArraySet;
71 +
72 +import static org.junit.Assert.assertNotNull;
73 +import static org.junit.Assert.assertTrue;
74 +
75 +/**
76 + * Test cases for ISIS topology provider.
77 + */
78 +public class IsisTopologyProviderTest {
79 +
80 + private final IsisTopologyProvider provider = new IsisTopologyProvider();
81 + private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry();
82 + private final TestLinkRegistry linkRegistry = new TestLinkRegistry();
83 + private final TestController controller = new TestController();
84 + private MockNetConfigRegistryAdapter networkConfigService = new MockNetConfigRegistryAdapter();
85 +
86 + @Before
87 + public void setUp() throws Exception {
88 + provider.deviceProviderRegistry = nodeRegistry;
89 + provider.linkProviderRegistry = linkRegistry;
90 + provider.networkConfigService = networkConfigService;
91 + provider.controller = controller;
92 + provider.activate();
93 + assertNotNull("provider should be registered", nodeRegistry.provider);
94 + assertNotNull("listener should be registered", controller.nodeListener);
95 + }
96 +
97 + @After
98 + public void tearDown() throws Exception {
99 + provider.deactivate();
100 + provider.controller = null;
101 + provider.deviceProviderRegistry = null;
102 + provider.networkConfigService = null;
103 + }
104 +
105 + @Test
106 + public void triggerProbe() {
107 + DeviceId deviceId = DeviceId.deviceId("2929.2929.2929.00-00");
108 + provider.triggerProbe(deviceId);
109 + }
110 +
111 + @Test
112 + public void roleChanged() {
113 + DeviceId deviceId = DeviceId.deviceId("1111.1111.1111.00-00");
114 + provider.roleChanged(deviceId, MastershipRole.MASTER);
115 + }
116 +
117 + @Test
118 + public void changePortState() {
119 + DeviceId deviceId = DeviceId.deviceId("2222.2222.2222.00-82");
120 + provider.changePortState(deviceId, PortNumber.portNumber(168430087), false);
121 + }
122 +
123 + @Test
124 + public void isReachable() {
125 + DeviceId deviceId = DeviceId.deviceId("1010.1010.1111.00-22");
126 + provider.isReachable(deviceId);
127 + }
128 +
129 +
130 + /* Validate node is added to the device validating URI and should get updated properly */
131 + @Test
132 + public void isisTopologyProviderTestAddDevice1() {
133 + int deviceAddCount = 0;
134 + IsisRouter isisRouter = new DefaultIsisRouter();
135 + isisRouter.setSystemId("2929.2929.2929.00");
136 + isisRouter.setNeighborRouterId(Ip4Address.valueOf("10.10.10.1"));
137 + isisRouter.setInterfaceId(Ip4Address.valueOf("10.10.10.2"));
138 + isisRouter.setDis(false);
139 +
140 + for (IsisRouterListener l : controller.nodeListener) {
141 + l.routerAdded(isisRouter);
142 + deviceAddCount = nodeRegistry.connected.size();
143 + assertTrue(deviceAddCount == 1);
144 + l.routerRemoved(isisRouter);
145 + deviceAddCount = nodeRegistry.connected.size();
146 + assertTrue(deviceAddCount == 0);
147 + }
148 + }
149 +
150 + @Test
151 + public void isisTopologyProviderTestAddDevice2() {
152 + int deviceAddCount = 0;
153 + IsisRouter isisRouter = new DefaultIsisRouter();
154 + isisRouter.setSystemId("7777.7777.7777.00");
155 + isisRouter.setNeighborRouterId(Ip4Address.valueOf("10.10.10.1"));
156 + isisRouter.setInterfaceId(Ip4Address.valueOf("10.10.10.7"));
157 + isisRouter.setDis(false);
158 + IsisRouter isisRouter1 = new DefaultIsisRouter();
159 + isisRouter1.setSystemId("1111.1111.1111.00");
160 + isisRouter1.setNeighborRouterId(Ip4Address.valueOf("10.10.10.7"));
161 + isisRouter1.setInterfaceId(Ip4Address.valueOf("10.10.10.1"));
162 + isisRouter1.setDis(true);
163 + for (IsisRouterListener l : controller.nodeListener) {
164 + l.routerAdded(isisRouter);
165 + deviceAddCount = nodeRegistry.connected.size();
166 + assertTrue(deviceAddCount == 1);
167 + l.routerAdded(isisRouter1);
168 + deviceAddCount = nodeRegistry.connected.size();
169 + assertTrue(deviceAddCount == 2);
170 + l.routerRemoved(isisRouter);
171 + deviceAddCount = nodeRegistry.connected.size();
172 + assertTrue(deviceAddCount == 1);
173 + }
174 + }
175 +
176 + @Test
177 + public void isisTopologyProviderTestAddLink() {
178 + int deviceAddCount = 0;
179 + IsisRouter isisRouter = new DefaultIsisRouter();
180 + isisRouter.setSystemId("7777.7777.7777.00");
181 + isisRouter.setNeighborRouterId(Ip4Address.valueOf("10.10.10.1"));
182 + isisRouter.setInterfaceId(Ip4Address.valueOf("10.10.10.7"));
183 + isisRouter.setDis(false);
184 + IsisRouter isisRouter1 = new DefaultIsisRouter();
185 + isisRouter1.setSystemId("1111.1111.1111.00");
186 + isisRouter1.setNeighborRouterId(Ip4Address.valueOf("10.10.10.7"));
187 + isisRouter1.setInterfaceId(Ip4Address.valueOf("10.10.10.1"));
188 + isisRouter1.setDis(true);
189 + IsisLink isisLink = new DefaultIsisLink();
190 + isisLink.setRemoteSystemId("7777.7777.7777.00");
191 + isisLink.setLocalSystemId("1111.1111.1111.00");
192 + isisLink.setInterfaceIp(Ip4Address.valueOf("10.10.10.1"));
193 + isisLink.setNeighborIp(Ip4Address.valueOf("10.10.10.7"));
194 + IsisLinkTed isisLinkTed = new DefaultIsisLinkTed();
195 + isisLinkTed.setTeDefaultMetric(10);
196 + isisLinkTed.setAdministrativeGroup(5);
197 + isisLinkTed.setIpv4InterfaceAddress(Ip4Address.valueOf("10.10.10.1"));
198 + isisLinkTed.setIpv4NeighborAddress(Ip4Address.valueOf("10.10.10.7"));
199 + isisLinkTed.setMaximumLinkBandwidth(Bandwidth.bps(0));
200 + isisLinkTed.setMaximumReservableLinkBandwidth(Bandwidth.bps(1.0));
201 + List<Bandwidth> unresList = new ArrayList<>();
202 + unresList.add(Bandwidth.bps(0.0));
203 + unresList.add(Bandwidth.bps(1.0));
204 + unresList.add(Bandwidth.bps(2.0));
205 + unresList.add(Bandwidth.bps(3.0));
206 + isisLinkTed.setUnreservedBandwidth(unresList);
207 + isisLink.setLinkTed(isisLinkTed);
208 + for (IsisRouterListener l : controller.nodeListener) {
209 + l.routerAdded(isisRouter);
210 + deviceAddCount = nodeRegistry.connected.size();
211 + assertTrue(deviceAddCount == 1);
212 + l.routerAdded(isisRouter1);
213 + deviceAddCount = nodeRegistry.connected.size();
214 + assertTrue(deviceAddCount == 2);
215 + }
216 + for (IsisLinkListener l : controller.linkListener) {
217 + l.addLink(isisLink);
218 + l.deleteLink(isisLink);
219 +
220 + }
221 + }
222 +
223 +
224 + /* Class implement device test registry */
225 + private class TestDeviceRegistry implements DeviceProviderRegistry {
226 + DeviceProvider provider;
227 +
228 + Set<DeviceId> connected = new HashSet<>();
229 +
230 + @Override
231 + public DeviceProviderService register(DeviceProvider provider) {
232 + this.provider = provider;
233 + return new TestProviderService();
234 + }
235 +
236 + @Override
237 + public void unregister(DeviceProvider provider) {
238 + }
239 +
240 + @Override
241 + public Set<ProviderId> getProviders() {
242 + return null;
243 + }
244 +
245 + private class TestProviderService implements DeviceProviderService {
246 +
247 + @Override
248 + public DeviceProvider provider() {
249 + return null;
250 + }
251 +
252 + @Override
253 + public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
254 +
255 + connected.add(deviceId);
256 +
257 + }
258 +
259 +
260 + @Override
261 + public void deviceDisconnected(DeviceId deviceId) {
262 +
263 + connected.remove(deviceId);
264 + }
265 +
266 +
267 + @Override
268 + public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
269 + // TODO Auto-generated method stub
270 +
271 + }
272 +
273 + @Override
274 + public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
275 + // TODO Auto-generated method stub
276 +
277 + }
278 +
279 + @Override
280 + public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) {
281 + // TODO Auto-generated method stub
282 +
283 + }
284 +
285 +
286 + @Override
287 + public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
288 + // TODO Auto-generated method stub
289 +
290 + }
291 + }
292 +
293 +
294 + }
295 +
296 + private class TestDeviceService extends DeviceServiceAdapter {
297 + private DeviceListener listener;
298 +
299 + @Override
300 + public void addListener(DeviceListener listener) {
301 + this.listener = listener;
302 + }
303 +
304 + @Override
305 + public Iterable<Device> getDevices() {
306 + return Collections.emptyList();
307 + }
308 + }
309 +
310 + private class TestLinkService extends LinkServiceAdapter {
311 + private LinkListener listener;
312 +
313 + @Override
314 + public void addListener(LinkListener listener) {
315 + this.listener = listener;
316 + }
317 +
318 + @Override
319 + public Iterable<Link> getLinks() {
320 + return Collections.emptyList();
321 + }
322 + }
323 +
324 + /* Class implement device test registry */
325 + private class TestLinkRegistry implements LinkProviderRegistry {
326 + LinkProvider provider;
327 +
328 + Set<DeviceId> connected = new HashSet<>();
329 +
330 + @Override
331 + public LinkProviderService register(LinkProvider provider) {
332 + this.provider = provider;
333 + return new TestLinkProviderService();
334 + }
335 +
336 + @Override
337 + public void unregister(LinkProvider provider) {
338 +
339 + }
340 +
341 + @Override
342 + public Set<ProviderId> getProviders() {
343 + return null;
344 + }
345 +
346 + private class TestLinkProviderService implements LinkProviderService {
347 +
348 + @Override
349 + public void linkDetected(LinkDescription linkDescription) {
350 +
351 + }
352 +
353 + @Override
354 + public void linkVanished(LinkDescription linkDescription) {
355 +
356 + }
357 +
358 + @Override
359 + public void linksVanished(ConnectPoint connectPoint) {
360 +
361 + }
362 +
363 + @Override
364 + public void linksVanished(DeviceId deviceId) {
365 +
366 + }
367 +
368 + @Override
369 + public LinkProvider provider() {
370 + return null;
371 + }
372 + }
373 + }
374 +
375 + /* class implement test controller */
376 + private class TestController implements IsisController {
377 + protected Set<IsisRouterListener> nodeListener = new CopyOnWriteArraySet<>();
378 + protected Set<IsisLinkListener> linkListener = new CopyOnWriteArraySet<>();
379 +
380 + @Override
381 + public void addRouterListener(IsisRouterListener nodeListener) {
382 + this.nodeListener.add(nodeListener);
383 + }
384 +
385 + @Override
386 + public void removeRouterListener(IsisRouterListener nodeListener) {
387 + this.nodeListener.remove(nodeListener);
388 + }
389 +
390 + @Override
391 + public void addLinkListener(IsisLinkListener listener) {
392 + this.linkListener.add(listener);
393 + }
394 +
395 + @Override
396 + public void removeLinkListener(IsisLinkListener listener) {
397 + this.linkListener.remove(listener);
398 + }
399 +
400 + @Override
401 + public void updateConfig(JsonNode processesNode) {
402 +
403 + }
404 +
405 + @Override
406 + public List<IsisProcess> allConfiguredProcesses() {
407 + return null;
408 + }
409 +
410 + @Override
411 + public Set<IsisRouterListener> listener() {
412 + return null;
413 + }
414 +
415 + @Override
416 + public Set<IsisLinkListener> linkListener() {
417 + return null;
418 + }
419 +
420 + }
421 +
422 + /* Mock test for device service */
423 + private class MockNetConfigRegistryAdapter extends NetworkConfigRegistryAdapter {
424 + private ConfigFactory cfgFactory;
425 + private Map<ConnectPoint, BandwidthCapacity> classConfig = new HashMap<>();
426 +
427 + @Override
428 + public void registerConfigFactory(ConfigFactory configFactory) {
429 + cfgFactory = configFactory;
430 + }
431 +
432 + @Override
433 + public void unregisterConfigFactory(ConfigFactory configFactory) {
434 + cfgFactory = null;
435 + }
436 +
437 + @Override
438 + public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
439 + if (configClass == BandwidthCapacity.class) {
440 + BandwidthCapacity devCap = new BandwidthCapacity();
441 + classConfig.put((ConnectPoint) subject, devCap);
442 +
443 + JsonNode node = new ObjectNode(new MockJsonNode());
444 + ObjectMapper mapper = new ObjectMapper();
445 + ConfigApplyDelegate delegate = new InternalApplyDelegate();
446 + devCap.init((ConnectPoint) subject, null, node, mapper, delegate);
447 + return (C) devCap;
448 + }
449 +
450 + return null;
451 + }
452 +
453 + @Override
454 + public <S, C extends Config<S>> void removeConfig(S subject, Class<C> configClass) {
455 + classConfig.remove(subject);
456 + }
457 +
458 + @Override
459 + public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
460 + if (configClass == BandwidthCapacity.class) {
461 + return (C) classConfig.get(subject);
462 + }
463 + return null;
464 + }
465 +
466 + private class MockJsonNode extends JsonNodeFactory {
467 + }
468 +
469 + // Auxiliary delegate to receive notifications about changes applied to
470 + // the network configuration - by the apps.
471 + private class InternalApplyDelegate implements ConfigApplyDelegate {
472 + @Override
473 + public void onApply(Config config) {
474 + }
475 + }
476 + }
477 +}
...\ No newline at end of file ...\ No newline at end of file