Ray Milkey
Committed by Gerrit Code Review

Unit tests for NetworkCongifLinksProvider

Also fixed a few bugs in the provider that were turned up by the new tests.

Change-Id: Icafc945251b4d35ac3f285302af150c0a4d646d6
...@@ -17,6 +17,8 @@ package org.onosproject.net.config; ...@@ -17,6 +17,8 @@ package org.onosproject.net.config;
17 17
18 import java.util.Set; 18 import java.util.Set;
19 19
20 +import com.google.common.collect.ImmutableSet;
21 +
20 /** 22 /**
21 * Test adapter for network configuration service registry. 23 * Test adapter for network configuration service registry.
22 */ 24 */
...@@ -29,11 +31,11 @@ public class NetworkConfigRegistryAdapter extends NetworkConfigServiceAdapter im ...@@ -29,11 +31,11 @@ public class NetworkConfigRegistryAdapter extends NetworkConfigServiceAdapter im
29 } 31 }
30 32
31 public Set<ConfigFactory> getConfigFactories() { 33 public Set<ConfigFactory> getConfigFactories() {
32 - return null; 34 + return ImmutableSet.of();
33 } 35 }
34 36
35 public <S, C extends Config<S>> Set<ConfigFactory<S, C>> getConfigFactories(Class<S> subjectClass) { 37 public <S, C extends Config<S>> Set<ConfigFactory<S, C>> getConfigFactories(Class<S> subjectClass) {
36 - return null; 38 + return ImmutableSet.of();
37 } 39 }
38 40
39 public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) { 41 public <S, C extends Config<S>> ConfigFactory<S, C> getConfigFactory(Class<C> configClass) {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
16 package org.onosproject.net.config; 16 package org.onosproject.net.config;
17 17
18 import com.fasterxml.jackson.databind.JsonNode; 18 import com.fasterxml.jackson.databind.JsonNode;
19 +import com.google.common.collect.ImmutableSet;
19 20
20 import java.util.Set; 21 import java.util.Set;
21 22
...@@ -25,7 +26,7 @@ import java.util.Set; ...@@ -25,7 +26,7 @@ import java.util.Set;
25 public class NetworkConfigServiceAdapter implements NetworkConfigService { 26 public class NetworkConfigServiceAdapter implements NetworkConfigService {
26 @Override 27 @Override
27 public Set<Class> getSubjectClasses() { 28 public Set<Class> getSubjectClasses() {
28 - return null; 29 + return ImmutableSet.of();
29 } 30 }
30 31
31 @Override 32 @Override
...@@ -45,17 +46,17 @@ public class NetworkConfigServiceAdapter implements NetworkConfigService { ...@@ -45,17 +46,17 @@ public class NetworkConfigServiceAdapter implements NetworkConfigService {
45 46
46 @Override 47 @Override
47 public <S> Set<S> getSubjects(Class<S> subjectClass) { 48 public <S> Set<S> getSubjects(Class<S> subjectClass) {
48 - return null; 49 + return ImmutableSet.of();
49 } 50 }
50 51
51 @Override 52 @Override
52 public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) { 53 public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) {
53 - return null; 54 + return ImmutableSet.of();
54 } 55 }
55 56
56 @Override 57 @Override
57 public <S> Set<? extends Config<S>> getConfigs(S subject) { 58 public <S> Set<? extends Config<S>> getConfigs(S subject) {
58 - return null; 59 + return ImmutableSet.of();
59 } 60 }
60 61
61 @Override 62 @Override
......
...@@ -16,10 +16,15 @@ ...@@ -16,10 +16,15 @@
16 package org.onosproject.net.link; 16 package org.onosproject.net.link;
17 17
18 import org.junit.Test; 18 import org.junit.Test;
19 +import org.onosproject.net.ConnectPoint;
19 import org.onosproject.net.DefaultAnnotations; 20 import org.onosproject.net.DefaultAnnotations;
20 import org.onosproject.net.DeviceId; 21 import org.onosproject.net.DeviceId;
22 +import org.onosproject.net.Link;
23 +import org.onosproject.net.NetTestTools;
21 import org.onosproject.net.PortNumber; 24 import org.onosproject.net.PortNumber;
22 25
26 +import com.google.common.testing.EqualsTester;
27 +
23 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue; 29 import static org.junit.Assert.assertTrue;
25 import static org.onosproject.net.DefaultLinkTest.cp; 30 import static org.onosproject.net.DefaultLinkTest.cp;
...@@ -44,7 +49,48 @@ public class DefaultLinkDescriptionTest { ...@@ -44,7 +49,48 @@ public class DefaultLinkDescriptionTest {
44 assertEquals("incorrect src", cp(DID1, P1), desc.src()); 49 assertEquals("incorrect src", cp(DID1, P1), desc.src());
45 assertEquals("incorrect dst", cp(DID2, P1), desc.dst()); 50 assertEquals("incorrect dst", cp(DID2, P1), desc.dst());
46 assertEquals("incorrect type", DIRECT, desc.type()); 51 assertEquals("incorrect type", DIRECT, desc.type());
47 - assertTrue("incorrect annotatios", desc.toString().contains("Key=Value")); 52 + assertTrue("incorrect annotations", desc.toString().contains("Key=Value"));
48 } 53 }
49 54
55 + /**
56 + * Tests the equals(), hashCode() and toString() methods.
57 + */
58 + @Test
59 + public void testEquals() {
60 + ConnectPoint connectPoint1 = NetTestTools.connectPoint("sw1", 1);
61 + ConnectPoint connectPoint2 = NetTestTools.connectPoint("sw2", 2);
62 + ConnectPoint connectPoint3 = NetTestTools.connectPoint("sw3", 3);
63 +
64 + DefaultLinkDescription link1 =
65 + new DefaultLinkDescription(connectPoint1, connectPoint2,
66 + Link.Type.DIRECT);
67 + DefaultLinkDescription sameAsLink1 =
68 + new DefaultLinkDescription(connectPoint1, connectPoint2,
69 + Link.Type.DIRECT);
70 + DefaultLinkDescription link2 =
71 + new DefaultLinkDescription(connectPoint1, connectPoint2,
72 + Link.Type.INDIRECT);
73 + DefaultLinkDescription link3 =
74 + new DefaultLinkDescription(connectPoint1, connectPoint3,
75 + Link.Type.DIRECT);
76 + DefaultLinkDescription link4 =
77 + new DefaultLinkDescription(connectPoint2, connectPoint3,
78 + Link.Type.DIRECT);
79 + DefaultLinkDescription link5 =
80 + new DefaultLinkDescription(connectPoint1, connectPoint2,
81 + Link.Type.DIRECT, false);
82 + DefaultLinkDescription link6 =
83 + new DefaultLinkDescription(connectPoint2, connectPoint3,
84 + Link.Type.DIRECT, DA);
85 +
86 + new EqualsTester()
87 + .addEqualityGroup(link1, sameAsLink1)
88 + .addEqualityGroup(link2)
89 + .addEqualityGroup(link3)
90 + .addEqualityGroup(link4)
91 + .addEqualityGroup(link5)
92 + .addEqualityGroup(link6)
93 + .testEquals();
94 +
95 + }
50 } 96 }
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 +package org.onosproject.net.link;
17 +
18 +import java.util.Set;
19 +
20 +import org.onosproject.net.provider.ProviderId;
21 +
22 +import com.google.common.collect.ImmutableSet;
23 +
24 +/**
25 + * Testing adapter for the LinkProviderRegistry API.
26 + */
27 +public class LinkProviderRegistryAdapter implements LinkProviderRegistry {
28 +
29 + LinkProviderServiceAdapter providerService = null;
30 +
31 + @Override
32 + public LinkProviderService register(LinkProvider provider) {
33 + providerService = new LinkProviderServiceAdapter(provider);
34 + return providerService;
35 + }
36 +
37 + @Override
38 + public void unregister(LinkProvider provider) {
39 + }
40 +
41 + @Override
42 + public Set<ProviderId> getProviders() {
43 + return ImmutableSet.of(providerService.provider().id());
44 + }
45 +
46 + public LinkProviderServiceAdapter registeredProvider() {
47 + return providerService;
48 + }
49 +}
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 +package org.onosproject.net.link;
17 +
18 +import java.util.HashMap;
19 +import java.util.List;
20 +import java.util.Map;
21 +
22 +import org.onosproject.net.ConnectPoint;
23 +import org.onosproject.net.DeviceId;
24 +import org.onosproject.net.LinkKey;
25 +import org.onosproject.net.provider.AbstractProviderService;
26 +
27 +import com.google.common.collect.Lists;
28 +import com.google.common.collect.Maps;
29 +
30 +public class LinkProviderServiceAdapter
31 + extends AbstractProviderService<LinkProvider>
32 + implements LinkProviderService {
33 +
34 + List<DeviceId> vanishedDpid = Lists.newLinkedList();
35 + List<Long> vanishedPort = Lists.newLinkedList();
36 + Map<DeviceId, DeviceId> discoveredLinks = Maps.newHashMap();
37 + Map<LinkKey, LinkDescription> discoveredLinkDescriptions = new HashMap<>();
38 +
39 + protected LinkProviderServiceAdapter(LinkProvider provider) {
40 + super(provider);
41 + }
42 +
43 + @Override
44 + public void linkDetected(LinkDescription linkDescription) {
45 + LinkKey key = LinkKey.linkKey(linkDescription.src(), linkDescription.dst());
46 + discoveredLinkDescriptions.put(key, linkDescription);
47 + DeviceId sDid = linkDescription.src().deviceId();
48 + DeviceId dDid = linkDescription.dst().deviceId();
49 + discoveredLinks.put(sDid, dDid);
50 + }
51 +
52 + @Override
53 + public void linkVanished(LinkDescription linkDescription) {
54 + LinkKey key = LinkKey.linkKey(linkDescription.src(), linkDescription.dst());
55 + discoveredLinkDescriptions.remove(key);
56 + }
57 +
58 + @Override
59 + public void linksVanished(ConnectPoint connectPoint) {
60 + vanishedPort.add(connectPoint.port().toLong());
61 +
62 + }
63 +
64 + @Override
65 + public void linksVanished(DeviceId deviceId) {
66 + vanishedDpid.add(deviceId);
67 + }
68 +
69 + public List<DeviceId> vanishedDpid() {
70 + return vanishedDpid;
71 + }
72 +
73 + public List<Long> vanishedPort() {
74 + return vanishedPort;
75 + }
76 +
77 + public Map<DeviceId, DeviceId> discoveredLinks() {
78 + return discoveredLinks;
79 + }
80 +
81 + public Map<LinkKey, LinkDescription> discoveredLinkDescriptions() {
82 + return discoveredLinkDescriptions;
83 + }
84 +}
...@@ -15,13 +15,14 @@ ...@@ -15,13 +15,14 @@
15 */ 15 */
16 package org.onosproject.provider.lldp.impl; 16 package org.onosproject.provider.lldp.impl;
17 17
18 -import com.google.common.collect.ArrayListMultimap; 18 +import java.nio.ByteBuffer;
19 -import com.google.common.collect.ImmutableList; 19 +import java.util.Collections;
20 -import com.google.common.collect.ImmutableMap; 20 +import java.util.HashMap;
21 -import com.google.common.collect.ImmutableSet; 21 +import java.util.HashSet;
22 -import com.google.common.collect.Lists; 22 +import java.util.List;
23 -import com.google.common.collect.Maps; 23 +import java.util.Map;
24 -import com.google.common.collect.Sets; 24 +import java.util.Set;
25 +import java.util.concurrent.CompletableFuture;
25 26
26 import org.junit.After; 27 import org.junit.After;
27 import org.junit.Before; 28 import org.junit.Before;
...@@ -65,10 +66,8 @@ import org.onosproject.net.device.DeviceEvent; ...@@ -65,10 +66,8 @@ import org.onosproject.net.device.DeviceEvent;
65 import org.onosproject.net.device.DeviceListener; 66 import org.onosproject.net.device.DeviceListener;
66 import org.onosproject.net.device.DeviceServiceAdapter; 67 import org.onosproject.net.device.DeviceServiceAdapter;
67 import org.onosproject.net.flow.TrafficTreatment; 68 import org.onosproject.net.flow.TrafficTreatment;
68 -import org.onosproject.net.link.LinkDescription; 69 +import org.onosproject.net.link.LinkProviderRegistryAdapter;
69 -import org.onosproject.net.link.LinkProvider; 70 +import org.onosproject.net.link.LinkProviderServiceAdapter;
70 -import org.onosproject.net.link.LinkProviderRegistry;
71 -import org.onosproject.net.link.LinkProviderService;
72 import org.onosproject.net.link.LinkServiceAdapter; 71 import org.onosproject.net.link.LinkServiceAdapter;
73 import org.onosproject.net.packet.DefaultInboundPacket; 72 import org.onosproject.net.packet.DefaultInboundPacket;
74 import org.onosproject.net.packet.InboundPacket; 73 import org.onosproject.net.packet.InboundPacket;
...@@ -76,27 +75,24 @@ import org.onosproject.net.packet.OutboundPacket; ...@@ -76,27 +75,24 @@ import org.onosproject.net.packet.OutboundPacket;
76 import org.onosproject.net.packet.PacketContext; 75 import org.onosproject.net.packet.PacketContext;
77 import org.onosproject.net.packet.PacketProcessor; 76 import org.onosproject.net.packet.PacketProcessor;
78 import org.onosproject.net.packet.PacketServiceAdapter; 77 import org.onosproject.net.packet.PacketServiceAdapter;
79 -import org.onosproject.net.provider.AbstractProviderService;
80 import org.onosproject.net.provider.ProviderId; 78 import org.onosproject.net.provider.ProviderId;
81 79
82 -import java.nio.ByteBuffer; 80 +import com.google.common.collect.ArrayListMultimap;
83 -import java.util.HashMap; 81 +import com.google.common.collect.ImmutableList;
84 -import java.util.HashSet; 82 +import com.google.common.collect.ImmutableMap;
85 -import java.util.List; 83 +import com.google.common.collect.ImmutableSet;
86 -import java.util.Map; 84 +import com.google.common.collect.Lists;
87 -import java.util.Set; 85 +import com.google.common.collect.Sets;
88 -import java.util.Collections;
89 -import java.util.concurrent.CompletableFuture;
90 86
91 import static org.easymock.EasyMock.createMock; 87 import static org.easymock.EasyMock.createMock;
92 import static org.easymock.EasyMock.expect; 88 import static org.easymock.EasyMock.expect;
93 import static org.easymock.EasyMock.replay; 89 import static org.easymock.EasyMock.replay;
94 -import static org.junit.Assert.assertNull;
95 -import static org.junit.Assert.assertNotNull;
96 import static org.junit.Assert.assertEquals; 90 import static org.junit.Assert.assertEquals;
91 +import static org.junit.Assert.assertFalse;
92 +import static org.junit.Assert.assertNotNull;
93 +import static org.junit.Assert.assertNull;
97 import static org.junit.Assert.assertTrue; 94 import static org.junit.Assert.assertTrue;
98 import static org.onosproject.provider.lldp.impl.LldpLinkProvider.DEFAULT_RULES; 95 import static org.onosproject.provider.lldp.impl.LldpLinkProvider.DEFAULT_RULES;
99 -import static org.junit.Assert.assertFalse;
100 96
101 97
102 public class LldpLinkProviderTest { 98 public class LldpLinkProviderTest {
...@@ -111,7 +107,7 @@ public class LldpLinkProviderTest { ...@@ -111,7 +107,7 @@ public class LldpLinkProviderTest {
111 private static Port pd4; 107 private static Port pd4;
112 108
113 private final LldpLinkProvider provider = new LldpLinkProvider(); 109 private final LldpLinkProvider provider = new LldpLinkProvider();
114 - private final TestLinkRegistry linkRegistry = new TestLinkRegistry(); 110 + private final LinkProviderRegistryAdapter linkRegistry = new LinkProviderRegistryAdapter();
115 private final TestLinkService linkService = new TestLinkService(); 111 private final TestLinkService linkService = new TestLinkService();
116 private final TestPacketService packetService = new TestPacketService(); 112 private final TestPacketService packetService = new TestPacketService();
117 private final TestDeviceService deviceService = new TestDeviceService(); 113 private final TestDeviceService deviceService = new TestDeviceService();
...@@ -119,7 +115,7 @@ public class LldpLinkProviderTest { ...@@ -119,7 +115,7 @@ public class LldpLinkProviderTest {
119 private final TestNetworkConfigRegistry configRegistry = new TestNetworkConfigRegistry(); 115 private final TestNetworkConfigRegistry configRegistry = new TestNetworkConfigRegistry();
120 116
121 private CoreService coreService; 117 private CoreService coreService;
122 - private TestLinkProviderService providerService; 118 + private LinkProviderServiceAdapter providerService;
123 119
124 private PacketProcessor testProcessor; 120 private PacketProcessor testProcessor;
125 private DeviceListener deviceListener; 121 private DeviceListener deviceListener;
...@@ -154,7 +150,10 @@ public class LldpLinkProviderTest { ...@@ -154,7 +150,10 @@ public class LldpLinkProviderTest {
154 provider.providerRegistry = linkRegistry; 150 provider.providerRegistry = linkRegistry;
155 provider.masterService = masterService; 151 provider.masterService = masterService;
156 provider.clusterMetadataService = new TestMetadataService(); 152 provider.clusterMetadataService = new TestMetadataService();
153 +
157 provider.activate(null); 154 provider.activate(null);
155 +
156 + providerService = linkRegistry.registeredProvider();
158 } 157 }
159 158
160 @Test 159 @Test
...@@ -521,7 +520,7 @@ public class LldpLinkProviderTest { ...@@ -521,7 +520,7 @@ public class LldpLinkProviderTest {
521 520
522 private boolean vanishedDpid(DeviceId... dids) { 521 private boolean vanishedDpid(DeviceId... dids) {
523 for (int i = 0; i < dids.length; i++) { 522 for (int i = 0; i < dids.length; i++) {
524 - if (!providerService.vanishedDpid.contains(dids[i])) { 523 + if (!providerService.vanishedDpid().contains(dids[i])) {
525 return false; 524 return false;
526 } 525 }
527 } 526 }
...@@ -530,7 +529,7 @@ public class LldpLinkProviderTest { ...@@ -530,7 +529,7 @@ public class LldpLinkProviderTest {
530 529
531 private boolean vanishedPort(Long... ports) { 530 private boolean vanishedPort(Long... ports) {
532 for (int i = 0; i < ports.length; i++) { 531 for (int i = 0; i < ports.length; i++) {
533 - if (!providerService.vanishedPort.contains(ports[i])) { 532 + if (!providerService.vanishedPort().contains(ports[i])) {
534 return false; 533 return false;
535 } 534 }
536 } 535 }
...@@ -538,9 +537,9 @@ public class LldpLinkProviderTest { ...@@ -538,9 +537,9 @@ public class LldpLinkProviderTest {
538 } 537 }
539 538
540 private boolean detectedLink(DeviceId src, DeviceId dst) { 539 private boolean detectedLink(DeviceId src, DeviceId dst) {
541 - for (DeviceId key : providerService.discoveredLinks.keySet()) { 540 + for (DeviceId key : providerService.discoveredLinks().keySet()) {
542 if (key.equals(src)) { 541 if (key.equals(src)) {
543 - return providerService.discoveredLinks.get(src).equals(dst); 542 + return providerService.discoveredLinks().get(src).equals(dst);
544 } 543 }
545 } 544 }
546 return false; 545 return false;
...@@ -631,65 +630,6 @@ public class LldpLinkProviderTest { ...@@ -631,65 +630,6 @@ public class LldpLinkProviderTest {
631 SuppressionConfig.class)); 630 SuppressionConfig.class));
632 } 631 }
633 632
634 -
635 - private class TestLinkRegistry implements LinkProviderRegistry {
636 -
637 - @Override
638 - public LinkProviderService register(LinkProvider provider) {
639 - providerService = new TestLinkProviderService(provider);
640 - return providerService;
641 - }
642 -
643 - @Override
644 - public void unregister(LinkProvider provider) {
645 - }
646 -
647 - @Override
648 - public Set<ProviderId> getProviders() {
649 - return null;
650 - }
651 -
652 - }
653 -
654 - private class TestLinkProviderService
655 - extends AbstractProviderService<LinkProvider>
656 - implements LinkProviderService {
657 -
658 - List<DeviceId> vanishedDpid = Lists.newLinkedList();
659 - List<Long> vanishedPort = Lists.newLinkedList();
660 - Map<DeviceId, DeviceId> discoveredLinks = Maps.newHashMap();
661 -
662 - protected TestLinkProviderService(LinkProvider provider) {
663 - super(provider);
664 - }
665 -
666 - @Override
667 - public void linkDetected(LinkDescription linkDescription) {
668 - DeviceId sDid = linkDescription.src().deviceId();
669 - DeviceId dDid = linkDescription.dst().deviceId();
670 - discoveredLinks.put(sDid, dDid);
671 - }
672 -
673 - @Override
674 - public void linkVanished(LinkDescription linkDescription) {
675 - }
676 -
677 - @Override
678 - public void linksVanished(ConnectPoint connectPoint) {
679 - vanishedPort.add(connectPoint.port().toLong());
680 -
681 - }
682 -
683 - @Override
684 - public void linksVanished(DeviceId deviceId) {
685 - vanishedDpid.add(deviceId);
686 - }
687 -
688 -
689 - }
690 -
691 -
692 -
693 private class TestPacketContext implements PacketContext { 633 private class TestPacketContext implements PacketContext {
694 634
695 protected Device device; 635 protected Device device;
......
...@@ -56,6 +56,15 @@ ...@@ -56,6 +56,15 @@
56 <artifactId>easymock</artifactId> 56 <artifactId>easymock</artifactId>
57 <scope>test</scope> 57 <scope>test</scope>
58 </dependency> 58 </dependency>
59 +
60 + <dependency>
61 + <groupId>org.onosproject</groupId>
62 + <artifactId>onos-api</artifactId>
63 + <version>${project.version}</version>
64 + <classifier>tests</classifier>
65 + <scope>test</scope>
66 + </dependency>
67 +
59 </dependencies> 68 </dependencies>
60 69
61 </project> 70 </project>
......
...@@ -117,7 +117,7 @@ public class NetworkConfigLinksProvider ...@@ -117,7 +117,7 @@ public class NetworkConfigLinksProvider
117 private final InternalDeviceListener deviceListener = new InternalDeviceListener(); 117 private final InternalDeviceListener deviceListener = new InternalDeviceListener();
118 private final InternalConfigListener cfgListener = new InternalConfigListener(); 118 private final InternalConfigListener cfgListener = new InternalConfigListener();
119 119
120 - private Set<LinkKey> configuredLinks = new HashSet<>(); 120 + protected Set<LinkKey> configuredLinks = new HashSet<>();
121 121
122 public NetworkConfigLinksProvider() { 122 public NetworkConfigLinksProvider() {
123 super(new ProviderId("lldp", PROVIDER_NAME)); 123 super(new ProviderId("lldp", PROVIDER_NAME));
...@@ -259,6 +259,35 @@ public class NetworkConfigLinksProvider ...@@ -259,6 +259,35 @@ public class NetworkConfigLinksProvider
259 } 259 }
260 260
261 /** 261 /**
262 + * Removes after stopping discovery helper for specified device.
263 + * @param deviceId device to remove
264 + */
265 + private void removeDevice(final DeviceId deviceId) {
266 + discoverers.computeIfPresent(deviceId, (did, ld) -> {
267 + ld.stop();
268 + return null;
269 + });
270 +
271 + }
272 +
273 + /**
274 + * Removes a port from the specified discovery helper.
275 + * @param port the port
276 + */
277 + private void removePort(Port port) {
278 + if (port.element() instanceof Device) {
279 + Device d = (Device) port.element();
280 + LinkDiscovery ld = discoverers.get(d.id());
281 + if (ld != null) {
282 + ld.removePort(port.number());
283 + }
284 + } else {
285 + log.warn("Attempted to remove non-Device port", port);
286 + }
287 + }
288 +
289 +
290 + /**
262 * Processes incoming packets. 291 * Processes incoming packets.
263 */ 292 */
264 private class InternalPacketProcessor implements PacketProcessor { 293 private class InternalPacketProcessor implements PacketProcessor {
...@@ -355,21 +384,21 @@ public class NetworkConfigLinksProvider ...@@ -355,21 +384,21 @@ public class NetworkConfigLinksProvider
355 updateDevice(device).ifPresent(ld -> updatePort(ld, port)); 384 updateDevice(device).ifPresent(ld -> updatePort(ld, port));
356 } else { 385 } else {
357 log.debug("Port down {}", port); 386 log.debug("Port down {}", port);
358 - //removePort(port); 387 + removePort(port);
359 providerService.linksVanished(new ConnectPoint(port.element().id(), 388 providerService.linksVanished(new ConnectPoint(port.element().id(),
360 port.number())); 389 port.number()));
361 } 390 }
362 break; 391 break;
363 case PORT_REMOVED: 392 case PORT_REMOVED:
364 log.debug("Port removed {}", port); 393 log.debug("Port removed {}", port);
365 - //removePort(port); 394 + removePort(port);
366 providerService.linksVanished(new ConnectPoint(port.element().id(), 395 providerService.linksVanished(new ConnectPoint(port.element().id(),
367 port.number())); 396 port.number()));
368 break; 397 break;
369 case DEVICE_REMOVED: 398 case DEVICE_REMOVED:
370 case DEVICE_SUSPENDED: 399 case DEVICE_SUSPENDED:
371 log.debug("Device removed {}", deviceId); 400 log.debug("Device removed {}", deviceId);
372 - //removeDevice(deviceId); 401 + removeDevice(deviceId);
373 providerService.linksVanished(deviceId); 402 providerService.linksVanished(deviceId);
374 break; 403 break;
375 case DEVICE_AVAILABILITY_CHANGED: 404 case DEVICE_AVAILABILITY_CHANGED:
...@@ -378,7 +407,7 @@ public class NetworkConfigLinksProvider ...@@ -378,7 +407,7 @@ public class NetworkConfigLinksProvider
378 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId)); 407 updateDevice(device).ifPresent(ld -> updatePorts(ld, deviceId));
379 } else { 408 } else {
380 log.debug("Device down {}", deviceId); 409 log.debug("Device down {}", deviceId);
381 - //removeDevice(deviceId); 410 + removeDevice(deviceId);
382 providerService.linksVanished(deviceId); 411 providerService.linksVanished(deviceId);
383 } 412 }
384 break; 413 break;
......
1 +/*
2 + * Copyright 2016 Open Networking Laboratory
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 +package org.onosproject.provider.netcfglinks;
17 +
18 +import java.nio.ByteBuffer;
19 +import java.util.List;
20 +
21 +import org.junit.After;
22 +import org.junit.Before;
23 +import org.junit.Test;
24 +import org.onlab.packet.ChassisId;
25 +import org.onlab.packet.Ethernet;
26 +import org.onlab.packet.ONOSLLDP;
27 +import org.onosproject.core.CoreServiceAdapter;
28 +import org.onosproject.mastership.MastershipServiceAdapter;
29 +import org.onosproject.net.ConnectPoint;
30 +import org.onosproject.net.DefaultPort;
31 +import org.onosproject.net.Device;
32 +import org.onosproject.net.DeviceId;
33 +import org.onosproject.net.LinkKey;
34 +import org.onosproject.net.NetTestTools;
35 +import org.onosproject.net.Port;
36 +import org.onosproject.net.PortNumber;
37 +import org.onosproject.net.config.NetworkConfigEvent;
38 +import org.onosproject.net.config.NetworkConfigListener;
39 +import org.onosproject.net.config.NetworkConfigRegistryAdapter;
40 +import org.onosproject.net.config.basics.BasicLinkConfig;
41 +import org.onosproject.net.device.DeviceEvent;
42 +import org.onosproject.net.device.DeviceListener;
43 +import org.onosproject.net.device.DeviceServiceAdapter;
44 +import org.onosproject.net.flow.TrafficTreatment;
45 +import org.onosproject.net.link.LinkDescription;
46 +import org.onosproject.net.link.LinkProviderRegistryAdapter;
47 +import org.onosproject.net.link.LinkProviderServiceAdapter;
48 +import org.onosproject.net.packet.DefaultInboundPacket;
49 +import org.onosproject.net.packet.InboundPacket;
50 +import org.onosproject.net.packet.OutboundPacket;
51 +import org.onosproject.net.packet.PacketContext;
52 +import org.onosproject.net.packet.PacketProcessor;
53 +import org.onosproject.net.packet.PacketServiceAdapter;
54 +
55 +import com.google.common.collect.ImmutableList;
56 +
57 +import static org.hamcrest.MatcherAssert.assertThat;
58 +import static org.hamcrest.Matchers.empty;
59 +import static org.hamcrest.Matchers.hasSize;
60 +import static org.hamcrest.Matchers.is;
61 +import static org.hamcrest.Matchers.notNullValue;
62 +
63 +/**
64 + * Unit tests for the network config links provider.
65 + */
66 +public class NetworkConfigLinksProviderTest {
67 +
68 + private NetworkConfigLinksProvider provider;
69 +
70 + private PacketProcessor testProcessor;
71 + private LinkProviderServiceAdapter providerService;
72 + private NetworkConfigListener configListener;
73 + private final TestNetworkConfigRegistry configRegistry =
74 + new TestNetworkConfigRegistry();
75 +
76 + static Device dev1 = NetTestTools.device("sw1");
77 + static Device dev2 = NetTestTools.device("sw2");
78 + static Device dev3 = NetTestTools.device("sw3");
79 + static PortNumber portNumber1 = PortNumber.portNumber(1);
80 + static PortNumber portNumber2 = PortNumber.portNumber(2);
81 + static PortNumber portNumber3 = PortNumber.portNumber(3);
82 + static ConnectPoint src = new ConnectPoint(dev1.id(), portNumber2);
83 + static ConnectPoint dst = new ConnectPoint(dev2.id(), portNumber2);
84 +
85 + static DeviceListener deviceListener;
86 +
87 + /**
88 + * Test device manager. Returns a known set of devices and ports.
89 + */
90 + static class TestDeviceManager extends DeviceServiceAdapter {
91 +
92 + @Override
93 + public Iterable<Device> getAvailableDevices() {
94 + return ImmutableList.of(dev1, dev2);
95 + }
96 +
97 + @Override
98 + public List<Port> getPorts(DeviceId deviceId) {
99 + return ImmutableList.of(new DefaultPort(dev1, portNumber1, true),
100 + new DefaultPort(dev2, portNumber2, true));
101 + }
102 +
103 + @Override
104 + public void addListener(DeviceListener listener) {
105 + deviceListener = listener;
106 + }
107 + }
108 +
109 + /**
110 + * Test mastership service. All devices owned by the local node for testing.
111 + */
112 + static class TestMastershipService extends MastershipServiceAdapter {
113 + @Override
114 + public boolean isLocalMaster(DeviceId deviceId) {
115 + return true;
116 + }
117 + }
118 +
119 + /**
120 + * Test packet context for generation of LLDP packets.
121 + */
122 + private class TestPacketContext implements PacketContext {
123 +
124 + protected ConnectPoint src;
125 + protected ConnectPoint dst;
126 + protected boolean blocked = false;
127 +
128 + public TestPacketContext(ConnectPoint src, ConnectPoint dst) {
129 + this.src = src;
130 + this.dst = dst;
131 + }
132 +
133 + @Override
134 + public long time() {
135 + return 0;
136 + }
137 +
138 + @Override
139 + public InboundPacket inPacket() {
140 + ONOSLLDP lldp = ONOSLLDP.onosLLDP(src.deviceId().toString(),
141 + new ChassisId(),
142 + (int) src.port().toLong());
143 +
144 + Ethernet ethPacket = new Ethernet();
145 + ethPacket.setEtherType(Ethernet.TYPE_LLDP);
146 + ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
147 + ethPacket.setPayload(lldp);
148 + ethPacket.setPad(true);
149 +
150 + ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");
151 +
152 + return new DefaultInboundPacket(dst, ethPacket,
153 + ByteBuffer.wrap(ethPacket.serialize()));
154 +
155 + }
156 +
157 + @Override
158 + public OutboundPacket outPacket() {
159 + return null;
160 + }
161 +
162 + @Override
163 + public TrafficTreatment.Builder treatmentBuilder() {
164 + return null;
165 + }
166 +
167 + @Override
168 + public void send() {
169 +
170 + }
171 +
172 + @Override
173 + public boolean block() {
174 + blocked = true;
175 + return true;
176 + }
177 +
178 + @Override
179 + public boolean isHandled() {
180 + return blocked;
181 + }
182 +
183 + }
184 +
185 + /**
186 + * Test packet service for capturing the packet processor from the service
187 + * under test.
188 + */
189 + private class TestPacketService extends PacketServiceAdapter {
190 + @Override
191 + public void addProcessor(PacketProcessor processor, int priority) {
192 + testProcessor = processor;
193 + }
194 + }
195 +
196 + /**
197 + * Test network config registry. Captures the network config listener from
198 + * the service under test.
199 + */
200 + private final class TestNetworkConfigRegistry
201 + extends NetworkConfigRegistryAdapter {
202 +
203 +
204 + @Override
205 + public void addListener(NetworkConfigListener listener) {
206 + configListener = listener;
207 + }
208 + }
209 +
210 + /**
211 + * Sets up a network config links provider under test and the services
212 + * required to run it.
213 + */
214 + @Before
215 + public void setUp() {
216 + provider = new NetworkConfigLinksProvider();
217 +
218 + provider.coreService = new CoreServiceAdapter();
219 + provider.packetService = new PacketServiceAdapter();
220 + LinkProviderRegistryAdapter linkRegistry =
221 + new LinkProviderRegistryAdapter();
222 + provider.providerRegistry = linkRegistry;
223 + provider.deviceService = new TestDeviceManager();
224 + provider.masterService = new TestMastershipService();
225 + provider.packetService = new TestPacketService();
226 + provider.netCfgService = configRegistry;
227 +
228 + provider.activate();
229 +
230 + providerService = linkRegistry.registeredProvider();
231 + }
232 +
233 + /**
234 + * Tears down the provider under test.
235 + */
236 + @After
237 + public void tearDown() {
238 + provider.deactivate();
239 + }
240 +
241 + /**
242 + * Tests that a network config links provider object can be created.
243 + * The actual creation is done in the setUp() method.
244 + */
245 + @Test
246 + public void testCreation() {
247 + assertThat(provider, notNullValue());
248 + assertThat(provider.configuredLinks, empty());
249 + }
250 +
251 + /**
252 + * Tests loading of devices from the device manager.
253 + */
254 + @Test
255 + public void testDeviceLoad() {
256 + assertThat(provider, notNullValue());
257 + assertThat(provider.discoverers.entrySet(), hasSize(2));
258 + }
259 +
260 + /**
261 + * Tests discovery of a link that is not expected in the configuration.
262 + */
263 + @Test
264 + public void testNotConfiguredLink() {
265 + PacketContext pktCtx = new TestPacketContext(src, dst);
266 +
267 + testProcessor.process(pktCtx);
268 +
269 + assertThat(providerService.discoveredLinks().entrySet(), hasSize(1));
270 + DeviceId destination = providerService.discoveredLinks().get(dev1.id());
271 + assertThat(destination, notNullValue());
272 + LinkKey key = LinkKey.linkKey(src, dst);
273 + LinkDescription linkDescription = providerService
274 + .discoveredLinkDescriptions().get(key);
275 + assertThat(linkDescription, notNullValue());
276 + assertThat(linkDescription.isExpected(), is(false));
277 + }
278 +
279 + /**
280 + * Tests discovery of an expected link.
281 + */
282 + @Test
283 + public void testConfiguredLink() {
284 + LinkKey key = LinkKey.linkKey(src, dst);
285 + configListener.event(new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED,
286 + key,
287 + BasicLinkConfig.class));
288 +
289 + PacketContext pktCtx = new TestPacketContext(src, dst);
290 +
291 + testProcessor.process(pktCtx);
292 +
293 + assertThat(providerService.discoveredLinks().entrySet(), hasSize(1));
294 + DeviceId destination = providerService.discoveredLinks().get(dev1.id());
295 + assertThat(destination, notNullValue());
296 + LinkDescription linkDescription = providerService
297 + .discoveredLinkDescriptions().get(key);
298 + assertThat(linkDescription, notNullValue());
299 + assertThat(linkDescription.isExpected(), is(true));
300 + }
301 +
302 + /**
303 + * Tests removal of a link from the configuration.
304 + */
305 + @Test
306 + public void testRemoveLink() {
307 + LinkKey key = LinkKey.linkKey(src, dst);
308 + configListener.event(new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED,
309 + key,
310 + BasicLinkConfig.class));
311 +
312 + assertThat(provider.configuredLinks, hasSize(1));
313 +
314 + configListener.event(new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_REMOVED,
315 + key,
316 + BasicLinkConfig.class));
317 + assertThat(provider.configuredLinks, hasSize(0));
318 + }
319 +
320 + /**
321 + * Tests adding a new device via an event.
322 + */
323 + @Test
324 + public void testAddDevice() {
325 + deviceListener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, dev3));
326 + assertThat(provider.discoverers.entrySet(), hasSize(3));
327 + }
328 +
329 + /**
330 + * Tests adding a new port via an event.
331 + */
332 + @Test
333 + public void testAddPort() {
334 + deviceListener.event(new DeviceEvent(DeviceEvent.Type.PORT_ADDED, dev3,
335 + new DefaultPort(dev3, portNumber3, true)));
336 + assertThat(provider.discoverers.entrySet(), hasSize(3));
337 + }
338 +
339 + /**
340 + * Tests removing a device via an event.
341 + */
342 + @Test
343 + public void testRemoveDevice() {
344 + assertThat(provider.discoverers.entrySet(), hasSize(2));
345 + deviceListener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, dev3));
346 + assertThat(provider.discoverers.entrySet(), hasSize(3));
347 + deviceListener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_REMOVED, dev3));
348 + assertThat(provider.discoverers.entrySet(), hasSize(2));
349 + }
350 +
351 + /**
352 + * Tests removing a port via an event.
353 + */
354 + @Test
355 + public void testRemovePort() {
356 + assertThat(provider.discoverers.entrySet(), hasSize(2));
357 + deviceListener.event(new DeviceEvent(DeviceEvent.Type.PORT_ADDED, dev3,
358 + new DefaultPort(dev3, portNumber3, true)));
359 + assertThat(provider.discoverers.entrySet(), hasSize(3));
360 + deviceListener.event(new DeviceEvent(DeviceEvent.Type.PORT_REMOVED, dev3,
361 + new DefaultPort(dev3, portNumber3, true)));
362 + assertThat(provider.discoverers.entrySet(), hasSize(3));
363 + }
364 +
365 + /**
366 + * Tests changing device availability via an event.
367 + */
368 + @Test
369 + public void testDeviceAvailabilityChanged() {
370 + assertThat(providerService.vanishedDpid(), hasSize(0));
371 +
372 + deviceListener.event(
373 + new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, dev3));
374 + assertThat(providerService.vanishedDpid(), hasSize(0));
375 +
376 + deviceListener.event(
377 + new DeviceEvent(DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED, dev3));
378 + assertThat(providerService.vanishedDpid(), hasSize(1));
379 + }
380 +}