Committed by
Gerrit Code Review
EdgeManager fixes
- Edge point update should be triggered based on TopologyEvent. {Device, Link}Event can be triggered before TopologyEvent, which will result in use of the outdated Topology to determine if the port is an edge port leading to incorrect edge port set. (ONOS-4896) - Ports on Edge Link should not be considered part of infrastructure, should be candidate for Edge point. Change-Id: I7d69cc242ba7849996c1105ccd1956975db63480 (cherry picked from commit b440ef46)
Showing
3 changed files
with
112 additions
and
73 deletions
... | @@ -37,6 +37,7 @@ import org.onosproject.net.DefaultPath; | ... | @@ -37,6 +37,7 @@ import org.onosproject.net.DefaultPath; |
37 | import org.onosproject.net.DeviceId; | 37 | import org.onosproject.net.DeviceId; |
38 | import org.onosproject.net.DisjointPath; | 38 | import org.onosproject.net.DisjointPath; |
39 | import org.onosproject.net.Link; | 39 | import org.onosproject.net.Link; |
40 | +import org.onosproject.net.Link.Type; | ||
40 | import org.onosproject.net.Path; | 41 | import org.onosproject.net.Path; |
41 | import org.onosproject.net.provider.ProviderId; | 42 | import org.onosproject.net.provider.ProviderId; |
42 | import org.onosproject.net.topology.ClusterId; | 43 | import org.onosproject.net.topology.ClusterId; |
... | @@ -442,14 +443,17 @@ public class DefaultTopology extends AbstractModel implements Topology { | ... | @@ -442,14 +443,17 @@ public class DefaultTopology extends AbstractModel implements Topology { |
442 | riskProfile2.put(new TopologyEdge() { | 443 | riskProfile2.put(new TopologyEdge() { |
443 | Link cur = l; | 444 | Link cur = l; |
444 | 445 | ||
446 | + @Override | ||
445 | public Link link() { | 447 | public Link link() { |
446 | return cur; | 448 | return cur; |
447 | } | 449 | } |
448 | 450 | ||
451 | + @Override | ||
449 | public TopologyVertex src() { | 452 | public TopologyVertex src() { |
450 | return () -> src; | 453 | return () -> src; |
451 | } | 454 | } |
452 | 455 | ||
456 | + @Override | ||
453 | public TopologyVertex dst() { | 457 | public TopologyVertex dst() { |
454 | return () -> dst; | 458 | return () -> dst; |
455 | } | 459 | } |
... | @@ -568,6 +572,12 @@ public class DefaultTopology extends AbstractModel implements Topology { | ... | @@ -568,6 +572,12 @@ public class DefaultTopology extends AbstractModel implements Topology { |
568 | private ImmutableSet<ConnectPoint> findInfrastructurePoints() { | 572 | private ImmutableSet<ConnectPoint> findInfrastructurePoints() { |
569 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); | 573 | ImmutableSet.Builder<ConnectPoint> builder = ImmutableSet.builder(); |
570 | for (TopologyEdge edge : graph.getEdges()) { | 574 | for (TopologyEdge edge : graph.getEdges()) { |
575 | + if (edge.link().type() == Type.EDGE) { | ||
576 | + // exclude EDGE link from infrastructure link | ||
577 | + // - Device <-> Host | ||
578 | + // - Device <-> remote domain Device | ||
579 | + continue; | ||
580 | + } | ||
571 | builder.add(edge.link().src()); | 581 | builder.add(edge.link().src()); |
572 | builder.add(edge.link().dst()); | 582 | builder.add(edge.link().dst()); |
573 | } | 583 | } | ... | ... |
... | @@ -29,8 +29,8 @@ import org.apache.felix.scr.annotations.Service; | ... | @@ -29,8 +29,8 @@ import org.apache.felix.scr.annotations.Service; |
29 | import org.onosproject.event.AbstractListenerManager; | 29 | import org.onosproject.event.AbstractListenerManager; |
30 | import org.onosproject.net.ConnectPoint; | 30 | import org.onosproject.net.ConnectPoint; |
31 | import org.onosproject.net.DeviceId; | 31 | import org.onosproject.net.DeviceId; |
32 | +import org.onosproject.net.Link.Type; | ||
32 | import org.onosproject.net.device.DeviceEvent; | 33 | import org.onosproject.net.device.DeviceEvent; |
33 | -import org.onosproject.net.device.DeviceListener; | ||
34 | import org.onosproject.net.device.DeviceService; | 34 | import org.onosproject.net.device.DeviceService; |
35 | import org.onosproject.net.edge.EdgePortEvent; | 35 | import org.onosproject.net.edge.EdgePortEvent; |
36 | import org.onosproject.net.edge.EdgePortListener; | 36 | import org.onosproject.net.edge.EdgePortListener; |
... | @@ -38,12 +38,12 @@ import org.onosproject.net.edge.EdgePortService; | ... | @@ -38,12 +38,12 @@ import org.onosproject.net.edge.EdgePortService; |
38 | import org.onosproject.net.flow.DefaultTrafficTreatment; | 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
39 | import org.onosproject.net.flow.TrafficTreatment; | 39 | import org.onosproject.net.flow.TrafficTreatment; |
40 | import org.onosproject.net.link.LinkEvent; | 40 | import org.onosproject.net.link.LinkEvent; |
41 | -import org.onosproject.net.link.LinkListener; | ||
42 | -import org.onosproject.net.link.LinkService; | ||
43 | import org.onosproject.net.packet.DefaultOutboundPacket; | 41 | import org.onosproject.net.packet.DefaultOutboundPacket; |
44 | import org.onosproject.net.packet.OutboundPacket; | 42 | import org.onosproject.net.packet.OutboundPacket; |
45 | import org.onosproject.net.packet.PacketService; | 43 | import org.onosproject.net.packet.PacketService; |
46 | import org.onosproject.net.topology.Topology; | 44 | import org.onosproject.net.topology.Topology; |
45 | +import org.onosproject.net.topology.TopologyEvent; | ||
46 | +import org.onosproject.net.topology.TopologyListener; | ||
47 | import org.onosproject.net.topology.TopologyService; | 47 | import org.onosproject.net.topology.TopologyService; |
48 | import org.slf4j.Logger; | 48 | import org.slf4j.Logger; |
49 | 49 | ||
... | @@ -72,11 +72,12 @@ public class EdgeManager | ... | @@ -72,11 +72,12 @@ public class EdgeManager |
72 | 72 | ||
73 | private Topology topology; | 73 | private Topology topology; |
74 | 74 | ||
75 | + /** | ||
76 | + * Set of edge ConnectPoints per Device. | ||
77 | + */ | ||
75 | private final Map<DeviceId, Set<ConnectPoint>> connectionPoints = Maps.newConcurrentMap(); | 78 | private final Map<DeviceId, Set<ConnectPoint>> connectionPoints = Maps.newConcurrentMap(); |
76 | 79 | ||
77 | - private final LinkListener linkListener = new InnerLinkListener(); | 80 | + private final TopologyListener topologyListener = new InnerTopologyListener(); |
78 | - | ||
79 | - private final DeviceListener deviceListener = new InnerDeviceListener(); | ||
80 | 81 | ||
81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
82 | protected PacketService packetService; | 83 | protected PacketService packetService; |
... | @@ -87,23 +88,18 @@ public class EdgeManager | ... | @@ -87,23 +88,18 @@ public class EdgeManager |
87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
88 | protected TopologyService topologyService; | 89 | protected TopologyService topologyService; |
89 | 90 | ||
90 | - @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | ||
91 | - protected LinkService linkService; | ||
92 | - | ||
93 | @Activate | 91 | @Activate |
94 | public void activate() { | 92 | public void activate() { |
95 | eventDispatcher.addSink(EdgePortEvent.class, listenerRegistry); | 93 | eventDispatcher.addSink(EdgePortEvent.class, listenerRegistry); |
96 | - deviceService.addListener(deviceListener); | 94 | + topologyService.addListener(topologyListener); |
97 | - linkService.addListener(linkListener); | ||
98 | loadAllEdgePorts(); | 95 | loadAllEdgePorts(); |
99 | log.info("Started"); | 96 | log.info("Started"); |
100 | } | 97 | } |
101 | 98 | ||
102 | @Deactivate | 99 | @Deactivate |
103 | public void deactivate() { | 100 | public void deactivate() { |
101 | + topologyService.removeListener(topologyListener); | ||
104 | eventDispatcher.removeSink(EdgePortEvent.class); | 102 | eventDispatcher.removeSink(EdgePortEvent.class); |
105 | - deviceService.removeListener(deviceListener); | ||
106 | - linkService.removeListener(linkListener); | ||
107 | log.info("Stopped"); | 103 | log.info("Stopped"); |
108 | } | 104 | } |
109 | 105 | ||
... | @@ -153,21 +149,18 @@ public class EdgeManager | ... | @@ -153,21 +149,18 @@ public class EdgeManager |
153 | return new DefaultOutboundPacket(point.deviceId(), builder.build(), data); | 149 | return new DefaultOutboundPacket(point.deviceId(), builder.build(), data); |
154 | } | 150 | } |
155 | 151 | ||
156 | - private class InnerLinkListener implements LinkListener { | 152 | + private class InnerTopologyListener implements TopologyListener { |
157 | 153 | ||
158 | @Override | 154 | @Override |
159 | - public void event(LinkEvent event) { | 155 | + public void event(TopologyEvent event) { |
160 | - topology = topologyService.currentTopology(); | 156 | + topology = event.subject(); |
161 | - processLinkEvent(event); | 157 | + event.reasons().forEach(reason -> { |
162 | - } | 158 | + if (reason instanceof DeviceEvent) { |
159 | + processDeviceEvent((DeviceEvent) reason); | ||
160 | + } else if (reason instanceof LinkEvent) { | ||
161 | + processLinkEvent((LinkEvent) reason); | ||
163 | } | 162 | } |
164 | - | 163 | + }); |
165 | - private class InnerDeviceListener implements DeviceListener { | ||
166 | - | ||
167 | - @Override | ||
168 | - public void event(DeviceEvent event) { | ||
169 | - topology = topologyService.currentTopology(); | ||
170 | - processDeviceEvent(event); | ||
171 | } | 164 | } |
172 | } | 165 | } |
173 | 166 | ||
... | @@ -180,12 +173,21 @@ public class EdgeManager | ... | @@ -180,12 +173,21 @@ public class EdgeManager |
180 | 173 | ||
181 | // Processes a link event by adding or removing its end-points in our cache. | 174 | // Processes a link event by adding or removing its end-points in our cache. |
182 | private void processLinkEvent(LinkEvent event) { | 175 | private void processLinkEvent(LinkEvent event) { |
183 | - if (event.type() == LinkEvent.Type.LINK_ADDED) { | 176 | + // negative Link event can result in increase of edge ports |
184 | - removeEdgePort(event.subject().src()); | 177 | + boolean addEdgePort = event.type() == LinkEvent.Type.LINK_REMOVED; |
185 | - removeEdgePort(event.subject().dst()); | 178 | + |
186 | - } else if (event.type() == LinkEvent.Type.LINK_REMOVED) { | 179 | + // but if the Link is an Edge type, |
180 | + // it will be the opposite | ||
181 | + if (event.subject().type() == Type.EDGE) { | ||
182 | + addEdgePort = !addEdgePort; | ||
183 | + } | ||
184 | + | ||
185 | + if (addEdgePort) { | ||
187 | addEdgePort(event.subject().src()); | 186 | addEdgePort(event.subject().src()); |
188 | addEdgePort(event.subject().dst()); | 187 | addEdgePort(event.subject().dst()); |
188 | + } else { | ||
189 | + removeEdgePort(event.subject().src()); | ||
190 | + removeEdgePort(event.subject().dst()); | ||
189 | } | 191 | } |
190 | } | 192 | } |
191 | 193 | ||
... | @@ -195,6 +197,8 @@ public class EdgeManager | ... | @@ -195,6 +197,8 @@ public class EdgeManager |
195 | DeviceEvent.Type type = event.type(); | 197 | DeviceEvent.Type type = event.type(); |
196 | DeviceId id = event.subject().id(); | 198 | DeviceId id = event.subject().id(); |
197 | 199 | ||
200 | + // FIXME there's still chance that Topology and Device Service | ||
201 | + // view is out-of-sync | ||
198 | if (type == DEVICE_ADDED || | 202 | if (type == DEVICE_ADDED || |
199 | type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) { | 203 | type == DEVICE_AVAILABILITY_CHANGED && deviceService.isAvailable(id)) { |
200 | // When device is added or becomes available, add all its ports | 204 | // When device is added or becomes available, add all its ports |
... | @@ -202,10 +206,11 @@ public class EdgeManager | ... | @@ -202,10 +206,11 @@ public class EdgeManager |
202 | .forEach(p -> addEdgePort(new ConnectPoint(id, p.number()))); | 206 | .forEach(p -> addEdgePort(new ConnectPoint(id, p.number()))); |
203 | } else if (type == DEVICE_REMOVED || | 207 | } else if (type == DEVICE_REMOVED || |
204 | type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) { | 208 | type == DEVICE_AVAILABILITY_CHANGED && !deviceService.isAvailable(id)) { |
205 | - // When device is removed or becomes unavailable, remove all its ports | 209 | + // When device is removed or becomes unavailable, remove all its ports. |
206 | - deviceService.getPorts(event.subject().id()) | 210 | + // Note: cannot rely on Device subsystem, ports may be gone. |
207 | - .forEach(p -> removeEdgePort(new ConnectPoint(id, p.number()))); | 211 | + Optional.ofNullable(connectionPoints.remove(id)) |
208 | - connectionPoints.remove(id); | 212 | + .orElse(ImmutableSet.of()) |
213 | + .forEach(point -> post(new EdgePortEvent(EDGE_PORT_REMOVED, point))); | ||
209 | 214 | ||
210 | } else if (type == DeviceEvent.Type.PORT_ADDED || | 215 | } else if (type == DeviceEvent.Type.PORT_ADDED || |
211 | type == PORT_UPDATED && event.port().isEnabled()) { | 216 | type == PORT_UPDATED && event.port().isEnabled()) { |
... | @@ -216,14 +221,16 @@ public class EdgeManager | ... | @@ -216,14 +221,16 @@ public class EdgeManager |
216 | } | 221 | } |
217 | } | 222 | } |
218 | 223 | ||
224 | + private boolean isEdgePort(ConnectPoint point) { | ||
225 | + return !topologyService.isInfrastructure(topology, point) && | ||
226 | + !point.port().isLogical(); | ||
227 | + } | ||
228 | + | ||
219 | // Adds the specified connection point to the edge points if needed. | 229 | // Adds the specified connection point to the edge points if needed. |
220 | private void addEdgePort(ConnectPoint point) { | 230 | private void addEdgePort(ConnectPoint point) { |
221 | - if (!topologyService.isInfrastructure(topology, point) && !point.port().isLogical()) { | 231 | + if (isEdgePort(point)) { |
222 | - Set<ConnectPoint> set = connectionPoints.get(point.deviceId()); | 232 | + Set<ConnectPoint> set = connectionPoints.computeIfAbsent(point.deviceId(), |
223 | - if (set == null) { | 233 | + (k) -> Sets.newConcurrentHashSet()); |
224 | - set = Sets.newConcurrentHashSet(); | ||
225 | - connectionPoints.put(point.deviceId(), set); | ||
226 | - } | ||
227 | if (set.add(point)) { | 234 | if (set.add(point)) { |
228 | post(new EdgePortEvent(EDGE_PORT_ADDED, point)); | 235 | post(new EdgePortEvent(EDGE_PORT_ADDED, point)); |
229 | } | 236 | } |
... | @@ -232,7 +239,7 @@ public class EdgeManager | ... | @@ -232,7 +239,7 @@ public class EdgeManager |
232 | 239 | ||
233 | // Removes the specified connection point from the edge points. | 240 | // Removes the specified connection point from the edge points. |
234 | private void removeEdgePort(ConnectPoint point) { | 241 | private void removeEdgePort(ConnectPoint point) { |
235 | - if (!point.port().isLogical()) { | 242 | + if (isEdgePort(point)) { |
236 | Set<ConnectPoint> set = connectionPoints.get(point.deviceId()); | 243 | Set<ConnectPoint> set = connectionPoints.get(point.deviceId()); |
237 | if (set == null) { | 244 | if (set == null) { |
238 | return; | 245 | return; |
... | @@ -241,7 +248,13 @@ public class EdgeManager | ... | @@ -241,7 +248,13 @@ public class EdgeManager |
241 | post(new EdgePortEvent(EDGE_PORT_REMOVED, point)); | 248 | post(new EdgePortEvent(EDGE_PORT_REMOVED, point)); |
242 | } | 249 | } |
243 | if (set.isEmpty()) { | 250 | if (set.isEmpty()) { |
244 | - connectionPoints.remove(point.deviceId()); | 251 | + connectionPoints.computeIfPresent(point.deviceId(), (k, v) -> { |
252 | + if (v.isEmpty()) { | ||
253 | + return v; | ||
254 | + } else { | ||
255 | + return v; | ||
256 | + } | ||
257 | + }); | ||
245 | } | 258 | } |
246 | } | 259 | } |
247 | } | 260 | } | ... | ... |
... | @@ -15,13 +15,16 @@ | ... | @@ -15,13 +15,16 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.net.edgeservice.impl; | 16 | package org.onosproject.net.edgeservice.impl; |
17 | 17 | ||
18 | +import com.google.common.collect.ImmutableList; | ||
18 | import com.google.common.collect.Lists; | 19 | import com.google.common.collect.Lists; |
19 | import com.google.common.collect.Maps; | 20 | import com.google.common.collect.Maps; |
20 | import com.google.common.collect.Sets; | 21 | import com.google.common.collect.Sets; |
22 | + | ||
21 | import org.junit.After; | 23 | import org.junit.After; |
22 | import org.junit.Before; | 24 | import org.junit.Before; |
23 | import org.junit.Test; | 25 | import org.junit.Test; |
24 | import org.onosproject.common.event.impl.TestEventDispatcher; | 26 | import org.onosproject.common.event.impl.TestEventDispatcher; |
27 | +import org.onosproject.event.Event; | ||
25 | import org.onosproject.net.ConnectPoint; | 28 | import org.onosproject.net.ConnectPoint; |
26 | import org.onosproject.net.DefaultPort; | 29 | import org.onosproject.net.DefaultPort; |
27 | import org.onosproject.net.Device; | 30 | import org.onosproject.net.Device; |
... | @@ -36,11 +39,11 @@ import org.onosproject.net.edge.EdgePortEvent; | ... | @@ -36,11 +39,11 @@ import org.onosproject.net.edge.EdgePortEvent; |
36 | import org.onosproject.net.edge.EdgePortListener; | 39 | import org.onosproject.net.edge.EdgePortListener; |
37 | import org.onosproject.net.flow.TrafficTreatment; | 40 | import org.onosproject.net.flow.TrafficTreatment; |
38 | import org.onosproject.net.link.LinkEvent; | 41 | import org.onosproject.net.link.LinkEvent; |
39 | -import org.onosproject.net.link.LinkListener; | ||
40 | -import org.onosproject.net.link.LinkServiceAdapter; | ||
41 | import org.onosproject.net.packet.OutboundPacket; | 42 | import org.onosproject.net.packet.OutboundPacket; |
42 | import org.onosproject.net.packet.PacketServiceAdapter; | 43 | import org.onosproject.net.packet.PacketServiceAdapter; |
43 | import org.onosproject.net.topology.Topology; | 44 | import org.onosproject.net.topology.Topology; |
45 | +import org.onosproject.net.topology.TopologyEvent; | ||
46 | +import org.onosproject.net.topology.TopologyEvent.Type; | ||
44 | import org.onosproject.net.topology.TopologyListener; | 47 | import org.onosproject.net.topology.TopologyListener; |
45 | import org.onosproject.net.topology.TopologyServiceAdapter; | 48 | import org.onosproject.net.topology.TopologyServiceAdapter; |
46 | 49 | ||
... | @@ -52,6 +55,7 @@ import java.util.Map; | ... | @@ -52,6 +55,7 @@ import java.util.Map; |
52 | import java.util.Optional; | 55 | import java.util.Optional; |
53 | import java.util.Set; | 56 | import java.util.Set; |
54 | 57 | ||
58 | +import static org.hamcrest.Matchers.*; | ||
55 | import static org.junit.Assert.*; | 59 | import static org.junit.Assert.*; |
56 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; | 60 | import static org.onosproject.net.NetTestTools.injectEventDispatcher; |
57 | import static org.onosproject.net.device.DeviceEvent.Type.*; | 61 | import static org.onosproject.net.device.DeviceEvent.Type.*; |
... | @@ -74,7 +78,6 @@ public class EdgeManagerTest { | ... | @@ -74,7 +78,6 @@ public class EdgeManagerTest { |
74 | private final Map<DeviceId, Device> devices = Maps.newConcurrentMap(); | 78 | private final Map<DeviceId, Device> devices = Maps.newConcurrentMap(); |
75 | private Set<OutboundPacket> packets = Sets.newConcurrentHashSet(); | 79 | private Set<OutboundPacket> packets = Sets.newConcurrentHashSet(); |
76 | private final EdgePortListener testListener = new TestListener(events); | 80 | private final EdgePortListener testListener = new TestListener(events); |
77 | - private TestLinkManager testLinkManager; | ||
78 | private TestDeviceManager testDeviceManager; | 81 | private TestDeviceManager testDeviceManager; |
79 | private TestTopologyManager testTopologyManager; | 82 | private TestTopologyManager testTopologyManager; |
80 | 83 | ||
... | @@ -87,8 +90,6 @@ public class EdgeManagerTest { | ... | @@ -87,8 +90,6 @@ public class EdgeManagerTest { |
87 | testDeviceManager = new TestDeviceManager(devices); | 90 | testDeviceManager = new TestDeviceManager(devices); |
88 | mgr.deviceService = testDeviceManager; | 91 | mgr.deviceService = testDeviceManager; |
89 | mgr.packetService = new TestPacketManager(); | 92 | mgr.packetService = new TestPacketManager(); |
90 | - testLinkManager = new TestLinkManager(); | ||
91 | - mgr.linkService = testLinkManager; | ||
92 | mgr.activate(); | 93 | mgr.activate(); |
93 | mgr.addListener(testListener); | 94 | mgr.addListener(testListener); |
94 | 95 | ||
... | @@ -126,7 +127,7 @@ public class EdgeManagerTest { | ... | @@ -126,7 +127,7 @@ public class EdgeManagerTest { |
126 | ConnectPoint testPoint, referencePoint; | 127 | ConnectPoint testPoint, referencePoint; |
127 | 128 | ||
128 | //Testing link removal | 129 | //Testing link removal |
129 | - testLinkManager.listener.event(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); | 130 | + postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); |
130 | 131 | ||
131 | assertTrue("The list contained an unexpected number of events", events.size() == 2); | 132 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
132 | assertTrue("The first element is of the wrong type.", | 133 | assertTrue("The first element is of the wrong type.", |
... | @@ -147,12 +148,12 @@ public class EdgeManagerTest { | ... | @@ -147,12 +148,12 @@ public class EdgeManagerTest { |
147 | testPoint.deviceId().equals(referencePoint.deviceId())); | 148 | testPoint.deviceId().equals(referencePoint.deviceId())); |
148 | 149 | ||
149 | //Rebroadcast event to ensure it results in no additional events | 150 | //Rebroadcast event to ensure it results in no additional events |
150 | - testLinkManager.listener.event(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); | 151 | + postTopologyEvent(new LinkEvent(LINK_REMOVED, NetTestTools.link("a", 1, "b", 2))); |
151 | assertTrue("The list contained an unexpected number of events", events.size() == 2); | 152 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
152 | 153 | ||
153 | //Testing link adding when links to remove exist | 154 | //Testing link adding when links to remove exist |
154 | events.clear(); | 155 | events.clear(); |
155 | - testLinkManager.listener.event(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); | 156 | + postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
156 | 157 | ||
157 | assertTrue("The list contained an unexpected number of events", events.size() == 2); | 158 | assertTrue("The list contained an unexpected number of events", events.size() == 2); |
158 | assertTrue("The first element is of the wrong type.", | 159 | assertTrue("The first element is of the wrong type.", |
... | @@ -176,10 +177,11 @@ public class EdgeManagerTest { | ... | @@ -176,10 +177,11 @@ public class EdgeManagerTest { |
176 | 177 | ||
177 | //Apparent duplicate of previous method tests removal when the elements have already been removed | 178 | //Apparent duplicate of previous method tests removal when the elements have already been removed |
178 | events.clear(); | 179 | events.clear(); |
179 | - testLinkManager.listener.event(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); | 180 | + postTopologyEvent(new LinkEvent(LINK_ADDED, NetTestTools.link("a", 1, "b", 2))); |
180 | assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0); | 181 | assertTrue("The list should contain no events, the removed elements don't exist.", events.size() == 0); |
181 | } | 182 | } |
182 | 183 | ||
184 | + | ||
183 | @Test | 185 | @Test |
184 | public void testDeviceUpdates() { | 186 | public void testDeviceUpdates() { |
185 | //Setup | 187 | //Setup |
... | @@ -195,7 +197,7 @@ public class EdgeManagerTest { | ... | @@ -195,7 +197,7 @@ public class EdgeManagerTest { |
195 | referenceDevice = NetTestTools.device("1"); | 197 | referenceDevice = NetTestTools.device("1"); |
196 | event = new DeviceEvent(DEVICE_ADDED, referenceDevice, | 198 | event = new DeviceEvent(DEVICE_ADDED, referenceDevice, |
197 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)); | 199 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true)); |
198 | - testDeviceManager.listener.event(event); | 200 | + postTopologyEvent(event); |
199 | 201 | ||
200 | //Check that ports were populated correctly | 202 | //Check that ports were populated correctly |
201 | assertTrue("Unexpected number of new ports added", | 203 | assertTrue("Unexpected number of new ports added", |
... | @@ -216,7 +218,7 @@ public class EdgeManagerTest { | ... | @@ -216,7 +218,7 @@ public class EdgeManagerTest { |
216 | events.clear(); | 218 | events.clear(); |
217 | 219 | ||
218 | //Repost the event to test repeated posts | 220 | //Repost the event to test repeated posts |
219 | - testDeviceManager.listener.event(event); | 221 | + postTopologyEvent(event); |
220 | assertEquals("The redundant notification should not have created additional notifications.", | 222 | assertEquals("The redundant notification should not have created additional notifications.", |
221 | 0, events.size()); | 223 | 0, events.size()); |
222 | //Calculate the size of the returned iterable of edge points. | 224 | //Calculate the size of the returned iterable of edge points. |
... | @@ -231,21 +233,21 @@ public class EdgeManagerTest { | ... | @@ -231,21 +233,21 @@ public class EdgeManagerTest { |
231 | events.clear(); | 233 | events.clear(); |
232 | event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice, | 234 | event = (new DeviceEvent(DEVICE_REMOVED, referenceDevice, |
233 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true))); | 235 | new DefaultPort(referenceDevice, PortNumber.portNumber(1), true))); |
234 | - testDeviceManager.listener.event(event); | 236 | + postTopologyEvent(event); |
235 | 237 | ||
236 | assertEquals("There should be five new events from removal of edge points", | 238 | assertEquals("There should be five new events from removal of edge points", |
237 | totalPorts - numInfraPorts, events.size()); | 239 | totalPorts - numInfraPorts, events.size()); |
238 | for (int index = 0; index < events.size(); index++) { | 240 | for (int index = 0; index < events.size(); index++) { |
239 | //Assert that the correct port numbers were removed in the correct order | 241 | //Assert that the correct port numbers were removed in the correct order |
240 | - assertEquals("Port removed had unexpected port number.", | 242 | + assertThat("Port removed had unexpected port number.", |
241 | - events.get(index).subject().port(), | 243 | + events.get(index).subject().port().toLong(), |
242 | - (NetTestTools.connectPoint("a", index + numInfraPorts + 1).port())); | 244 | + is(greaterThanOrEqualTo((long) numInfraPorts))); |
243 | //Assert that the events are of the correct type | 245 | //Assert that the events are of the correct type |
244 | assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED); | 246 | assertEquals("Unexpected type of event", events.get(index).type(), EDGE_PORT_REMOVED); |
245 | } | 247 | } |
246 | events.clear(); | 248 | events.clear(); |
247 | //Rebroadcast event to check that it triggers no new behavior | 249 | //Rebroadcast event to check that it triggers no new behavior |
248 | - testDeviceManager.listener.event(event); | 250 | + postTopologyEvent(event); |
249 | assertEquals("Rebroadcast of removal event should not produce additional events", | 251 | assertEquals("Rebroadcast of removal event should not produce additional events", |
250 | 0, events.size()); | 252 | 0, events.size()); |
251 | 253 | ||
... | @@ -256,14 +258,14 @@ public class EdgeManagerTest { | ... | @@ -256,14 +258,14 @@ public class EdgeManagerTest { |
256 | devices.put(referenceDevice.id(), referenceDevice); | 258 | devices.put(referenceDevice.id(), referenceDevice); |
257 | 259 | ||
258 | event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice); | 260 | event = new DeviceEvent(DEVICE_AVAILABILITY_CHANGED, referenceDevice); |
259 | - testDeviceManager.listener.event(event); | 261 | + postTopologyEvent(event); |
260 | //An earlier setup set half of the reference device ports to infrastructure | 262 | //An earlier setup set half of the reference device ports to infrastructure |
261 | assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size()); | 263 | assertEquals("An unexpected number of events were generated.", totalPorts - numInfraPorts, events.size()); |
262 | for (int i = 0; i < 5; i++) { | 264 | for (int i = 0; i < 5; i++) { |
263 | assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED); | 265 | assertEquals("The event was not of the right type", events.get(i).type(), EDGE_PORT_ADDED); |
264 | } | 266 | } |
265 | events.clear(); | 267 | events.clear(); |
266 | - testDeviceManager.listener.event(event); | 268 | + postTopologyEvent(event); |
267 | assertEquals("No events should have been generated for a set of existing ports.", 0, events.size()); | 269 | assertEquals("No events should have been generated for a set of existing ports.", 0, events.size()); |
268 | 270 | ||
269 | //Test removal when state changes when the device becomes unavailable | 271 | //Test removal when state changes when the device becomes unavailable |
... | @@ -275,18 +277,20 @@ public class EdgeManagerTest { | ... | @@ -275,18 +277,20 @@ public class EdgeManagerTest { |
275 | no events will be generated since no ports will be provided in getPorts() to EdgeManager. | 277 | no events will be generated since no ports will be provided in getPorts() to EdgeManager. |
276 | */ | 278 | */ |
277 | alwaysReturnPorts = true; | 279 | alwaysReturnPorts = true; |
278 | - testDeviceManager.listener.event(event); | 280 | + postTopologyEvent(event); |
279 | alwaysReturnPorts = false; | 281 | alwaysReturnPorts = false; |
280 | assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size()); | 282 | assertEquals("An unexpected number of events were created.", totalPorts - numInfraPorts, events.size()); |
281 | for (int i = 0; i < 5; i++) { | 283 | for (int i = 0; i < 5; i++) { |
282 | EdgePortEvent edgeEvent = events.get(i); | 284 | EdgePortEvent edgeEvent = events.get(i); |
283 | assertEquals("The event is of an unexpected type.", | 285 | assertEquals("The event is of an unexpected type.", |
284 | EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type()); | 286 | EdgePortEvent.Type.EDGE_PORT_REMOVED, edgeEvent.type()); |
285 | - assertEquals("The event pertains to an unexpected port", PortNumber.portNumber(i + numInfraPorts + 1), | 287 | + assertThat("The event pertains to an unexpected port", |
286 | - edgeEvent.subject().port()); | 288 | + edgeEvent.subject().port().toLong(), |
289 | + is(greaterThanOrEqualTo((long) numInfraPorts))); | ||
287 | } | 290 | } |
288 | } | 291 | } |
289 | 292 | ||
293 | + | ||
290 | @Test | 294 | @Test |
291 | public void testInternalCache() { | 295 | public void testInternalCache() { |
292 | int numDevices = 10; | 296 | int numDevices = 10; |
... | @@ -298,7 +302,7 @@ public class EdgeManagerTest { | ... | @@ -298,7 +302,7 @@ public class EdgeManagerTest { |
298 | for (int i = 0; i < numDevices; i++) { | 302 | for (int i = 0; i < numDevices; i++) { |
299 | Device newDevice = NetTestTools.device(Integer.toString(i)); | 303 | Device newDevice = NetTestTools.device(Integer.toString(i)); |
300 | devices.put(newDevice.id(), newDevice); | 304 | devices.put(newDevice.id(), newDevice); |
301 | - testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, newDevice)); | 305 | + postTopologyEvent(new DeviceEvent(DEVICE_ADDED, newDevice)); |
302 | } | 306 | } |
303 | //Check all ports have correct designations | 307 | //Check all ports have correct designations |
304 | ConnectPoint testPoint; | 308 | ConnectPoint testPoint; |
... | @@ -342,7 +346,7 @@ public class EdgeManagerTest { | ... | @@ -342,7 +346,7 @@ public class EdgeManagerTest { |
342 | } | 346 | } |
343 | for (int i = 0; i < numDevices; i++) { | 347 | for (int i = 0; i < numDevices; i++) { |
344 | referenceDevice = NetTestTools.device(Integer.toString(i)); | 348 | referenceDevice = NetTestTools.device(Integer.toString(i)); |
345 | - testDeviceManager.listener.event(new DeviceEvent(DEVICE_ADDED, referenceDevice, | 349 | + postTopologyEvent(new DeviceEvent(DEVICE_ADDED, referenceDevice, |
346 | new DefaultPort(referenceDevice, | 350 | new DefaultPort(referenceDevice, |
347 | PortNumber.portNumber(1), | 351 | PortNumber.portNumber(1), |
348 | true))); | 352 | true))); |
... | @@ -373,6 +377,27 @@ public class EdgeManagerTest { | ... | @@ -373,6 +377,27 @@ public class EdgeManagerTest { |
373 | 377 | ||
374 | 378 | ||
375 | /** | 379 | /** |
380 | + * Creates TopologyEvent triggered by {@code event}. | ||
381 | + * | ||
382 | + * @param event reason of the TopologyEvent | ||
383 | + * @return TopologyEvent | ||
384 | + */ | ||
385 | + private TopologyEvent topologyEventOf(Event event) { | ||
386 | + return new TopologyEvent(Type.TOPOLOGY_CHANGED, null, ImmutableList.of(event)); | ||
387 | + } | ||
388 | + | ||
389 | + | ||
390 | + /** | ||
391 | + * Post Event dispatched from TopologyManager. | ||
392 | + * | ||
393 | + * @param event Event | ||
394 | + */ | ||
395 | + private void postTopologyEvent(Event event) { | ||
396 | + testTopologyManager.listener.event(topologyEventOf(event)); | ||
397 | + } | ||
398 | + | ||
399 | + | ||
400 | + /** | ||
376 | * @param numDevices the number of devices to populate. | 401 | * @param numDevices the number of devices to populate. |
377 | * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0 | 402 | * @param numInfraPorts the number of ports to be set as infrastructure on each device, numbered base 0, ports 0 |
378 | * through numInfraPorts - 1 | 403 | * through numInfraPorts - 1 |
... | @@ -493,15 +518,6 @@ public class EdgeManagerTest { | ... | @@ -493,15 +518,6 @@ public class EdgeManagerTest { |
493 | } | 518 | } |
494 | } | 519 | } |
495 | 520 | ||
496 | - private class TestLinkManager extends LinkServiceAdapter { | ||
497 | - private LinkListener listener; | ||
498 | - | ||
499 | - @Override | ||
500 | - public void addListener(LinkListener listener) { | ||
501 | - this.listener = listener; | ||
502 | - } | ||
503 | - } | ||
504 | - | ||
505 | private class TestListener implements EdgePortListener { | 521 | private class TestListener implements EdgePortListener { |
506 | private List<EdgePortEvent> events; | 522 | private List<EdgePortEvent> events; |
507 | 523 | ... | ... |
-
Please register or login to post a comment