Showing
4 changed files
with
167 additions
and
5 deletions
| ... | @@ -48,7 +48,7 @@ public class SimpleDeviceManager | ... | @@ -48,7 +48,7 @@ public class SimpleDeviceManager |
| 48 | 48 | ||
| 49 | private final Logger log = getLogger(getClass()); | 49 | private final Logger log = getLogger(getClass()); |
| 50 | 50 | ||
| 51 | - private final AbstractListenerRegistry<DeviceEvent, DeviceListener> | 51 | + protected final AbstractListenerRegistry<DeviceEvent, DeviceListener> |
| 52 | listenerRegistry = new AbstractListenerRegistry<>(); | 52 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 53 | 53 | ||
| 54 | private final SimpleDeviceStore store = new SimpleDeviceStore(); | 54 | private final SimpleDeviceStore store = new SimpleDeviceStore(); | ... | ... |
| ... | @@ -45,7 +45,7 @@ public class SimpleLinkManager | ... | @@ -45,7 +45,7 @@ public class SimpleLinkManager |
| 45 | 45 | ||
| 46 | private final Logger log = getLogger(getClass()); | 46 | private final Logger log = getLogger(getClass()); |
| 47 | 47 | ||
| 48 | - private final AbstractListenerRegistry<LinkEvent, LinkListener> | 48 | + protected final AbstractListenerRegistry<LinkEvent, LinkListener> |
| 49 | listenerRegistry = new AbstractListenerRegistry<>(); | 49 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 50 | 50 | ||
| 51 | private final SimpleLinkStore store = new SimpleLinkStore(); | 51 | private final SimpleLinkStore store = new SimpleLinkStore(); | ... | ... |
core/trivial/src/test/java/org/onlab/onos/net/trivial/topology/impl/DefaultTopologyProviderTest.java
0 → 100644
| 1 | +package org.onlab.onos.net.trivial.topology.impl; | ||
| 2 | + | ||
| 3 | +import org.junit.After; | ||
| 4 | +import org.junit.Before; | ||
| 5 | +import org.junit.Test; | ||
| 6 | +import org.onlab.onos.event.Event; | ||
| 7 | +import org.onlab.onos.event.impl.TestEventDispatcher; | ||
| 8 | +import org.onlab.onos.net.Device; | ||
| 9 | +import org.onlab.onos.net.Link; | ||
| 10 | +import org.onlab.onos.net.device.DeviceEvent; | ||
| 11 | +import org.onlab.onos.net.link.LinkEvent; | ||
| 12 | +import org.onlab.onos.net.provider.AbstractProviderService; | ||
| 13 | +import org.onlab.onos.net.provider.ProviderId; | ||
| 14 | +import org.onlab.onos.net.topology.GraphDescription; | ||
| 15 | +import org.onlab.onos.net.topology.TopologyProvider; | ||
| 16 | +import org.onlab.onos.net.topology.TopologyProviderRegistry; | ||
| 17 | +import org.onlab.onos.net.topology.TopologyProviderService; | ||
| 18 | +import org.onlab.onos.net.trivial.device.impl.SimpleDeviceManager; | ||
| 19 | +import org.onlab.onos.net.trivial.link.impl.SimpleLinkManager; | ||
| 20 | + | ||
| 21 | +import java.util.List; | ||
| 22 | +import java.util.Set; | ||
| 23 | + | ||
| 24 | +import static com.google.common.collect.ImmutableSet.of; | ||
| 25 | +import static org.junit.Assert.assertEquals; | ||
| 26 | +import static org.junit.Assert.assertNotNull; | ||
| 27 | +import static org.onlab.junit.TestTools.assertAfter; | ||
| 28 | +import static org.onlab.onos.net.device.DeviceEvent.Type.DEVICE_ADDED; | ||
| 29 | +import static org.onlab.onos.net.link.LinkEvent.Type.LINK_ADDED; | ||
| 30 | +import static org.onlab.onos.net.trivial.topology.impl.SimpleTopologyManagerTest.device; | ||
| 31 | +import static org.onlab.onos.net.trivial.topology.impl.SimpleTopologyManagerTest.link; | ||
| 32 | + | ||
| 33 | +/** | ||
| 34 | + * Test of the default topology provider implementation. | ||
| 35 | + */ | ||
| 36 | +public class DefaultTopologyProviderTest { | ||
| 37 | + | ||
| 38 | + private DefaultTopologyProvider provider = new DefaultTopologyProvider(); | ||
| 39 | + private TestTopoRegistry topologyService = new TestTopoRegistry(); | ||
| 40 | + private TestDeviceService deviceService = new TestDeviceService(); | ||
| 41 | + private TestLinkService linkService = new TestLinkService(); | ||
| 42 | + private TestTopoProviderService providerService; | ||
| 43 | + | ||
| 44 | + @Before | ||
| 45 | + public void setUp() { | ||
| 46 | + provider.deviceService = deviceService; | ||
| 47 | + provider.linkService = linkService; | ||
| 48 | + provider.providerRegistry = topologyService; | ||
| 49 | + provider.activate(); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + @After | ||
| 53 | + public void tearDown() { | ||
| 54 | + provider.deactivate(); | ||
| 55 | + provider.providerRegistry = null; | ||
| 56 | + provider.deviceService = null; | ||
| 57 | + provider.linkService = null; | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + private void validateSubmission() { | ||
| 61 | + assertNotNull("registration expected", providerService); | ||
| 62 | + assertEquals("incorrect providert", provider, providerService.provider()); | ||
| 63 | + assertNotNull("topo change should be submitted", providerService.graphDesc); | ||
| 64 | + assertEquals("incorrect vertex count", 6, providerService.graphDesc.vertexes().size()); | ||
| 65 | + assertEquals("incorrect edge count", 10, providerService.graphDesc.edges().size()); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + @Test | ||
| 69 | + public void basics() { | ||
| 70 | + assertAfter(100, new Runnable() { | ||
| 71 | + @Override | ||
| 72 | + public void run() { | ||
| 73 | + validateSubmission(); | ||
| 74 | + } | ||
| 75 | + }); | ||
| 76 | + } | ||
| 77 | + | ||
| 78 | + @Test | ||
| 79 | + public void eventDriven() { | ||
| 80 | + assertAfter(100, new Runnable() { | ||
| 81 | + @Override | ||
| 82 | + public void run() { | ||
| 83 | + validateSubmission(); | ||
| 84 | + deviceService.post(new DeviceEvent(DEVICE_ADDED, device("z"), null)); | ||
| 85 | + linkService.post(new LinkEvent(LINK_ADDED, link("z", 1, "a", 4))); | ||
| 86 | + validateSubmission(); | ||
| 87 | + } | ||
| 88 | + }); | ||
| 89 | + } | ||
| 90 | + | ||
| 91 | + | ||
| 92 | + private class TestTopoRegistry implements TopologyProviderRegistry { | ||
| 93 | + | ||
| 94 | + @Override | ||
| 95 | + public TopologyProviderService register(TopologyProvider provider) { | ||
| 96 | + providerService = new TestTopoProviderService(provider); | ||
| 97 | + return providerService; | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + @Override | ||
| 101 | + public void unregister(TopologyProvider provider) { | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + @Override | ||
| 105 | + public Set<ProviderId> getProviders() { | ||
| 106 | + return null; | ||
| 107 | + } | ||
| 108 | + } | ||
| 109 | + | ||
| 110 | + private class TestTopoProviderService | ||
| 111 | + extends AbstractProviderService<TopologyProvider> | ||
| 112 | + implements TopologyProviderService { | ||
| 113 | + GraphDescription graphDesc; | ||
| 114 | + | ||
| 115 | + protected TestTopoProviderService(TopologyProvider provider) { | ||
| 116 | + super(provider); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + @Override | ||
| 120 | + public void topologyChanged(GraphDescription graphDescription, List<Event> reasons) { | ||
| 121 | + graphDesc = graphDescription; | ||
| 122 | + } | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + private class TestDeviceService extends SimpleDeviceManager { | ||
| 126 | + TestDeviceService() { | ||
| 127 | + eventDispatcher = new TestEventDispatcher(); | ||
| 128 | + eventDispatcher.addSink(DeviceEvent.class, listenerRegistry); | ||
| 129 | + } | ||
| 130 | + | ||
| 131 | + @Override | ||
| 132 | + public Iterable<Device> getDevices() { | ||
| 133 | + return of(device("a"), device("b"), | ||
| 134 | + device("c"), device("d"), | ||
| 135 | + device("e"), device("f")); | ||
| 136 | + } | ||
| 137 | + | ||
| 138 | + void post(DeviceEvent event) { | ||
| 139 | + eventDispatcher.post(event); | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + private class TestLinkService extends SimpleLinkManager { | ||
| 144 | + TestLinkService() { | ||
| 145 | + eventDispatcher = new TestEventDispatcher(); | ||
| 146 | + eventDispatcher.addSink(LinkEvent.class, listenerRegistry); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + @Override | ||
| 150 | + public Iterable<Link> getLinks() { | ||
| 151 | + return of(link("a", 1, "b", 1), link("b", 1, "a", 1), | ||
| 152 | + link("b", 2, "c", 1), link("c", 1, "b", 2), | ||
| 153 | + link("c", 2, "d", 1), link("d", 1, "c", 2), | ||
| 154 | + link("d", 2, "a", 2), link("a", 2, "d", 2), | ||
| 155 | + link("e", 1, "f", 1), link("f", 1, "e", 1)); | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + void post(LinkEvent event) { | ||
| 159 | + eventDispatcher.post(event); | ||
| 160 | + } | ||
| 161 | + } | ||
| 162 | +} | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -182,20 +182,20 @@ public class SimpleTopologyManagerTest { | ... | @@ -182,20 +182,20 @@ public class SimpleTopologyManagerTest { |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | // Short-hand for creating a link. | 184 | // Short-hand for creating a link. |
| 185 | - private Link link(String src, int sp, String dst, int dp) { | 185 | + static Link link(String src, int sp, String dst, int dp) { |
| 186 | return new DefaultLink(PID, new ConnectPoint(did(src), portNumber(sp)), | 186 | return new DefaultLink(PID, new ConnectPoint(did(src), portNumber(sp)), |
| 187 | new ConnectPoint(did(dst), portNumber(dp)), | 187 | new ConnectPoint(did(dst), portNumber(dp)), |
| 188 | Link.Type.DIRECT); | 188 | Link.Type.DIRECT); |
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | // Crates a new device with the specified id | 191 | // Crates a new device with the specified id |
| 192 | - private Device device(String id) { | 192 | + static Device device(String id) { |
| 193 | return new DefaultDevice(PID, did(id), Device.Type.SWITCH, | 193 | return new DefaultDevice(PID, did(id), Device.Type.SWITCH, |
| 194 | "mfg", "1.0", "1.1", "1234"); | 194 | "mfg", "1.0", "1.1", "1234"); |
| 195 | } | 195 | } |
| 196 | 196 | ||
| 197 | // Short-hand for producing a device id from a string | 197 | // Short-hand for producing a device id from a string |
| 198 | - private DeviceId did(String id) { | 198 | + static DeviceId did(String id) { |
| 199 | return deviceId("of:" + id); | 199 | return deviceId("of:" + id); |
| 200 | } | 200 | } |
| 201 | 201 | ... | ... |
-
Please register or login to post a comment