tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.trivial.impl; |
| 2 | |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 3 | import com.google.common.collect.ImmutableSet; |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 4 | import org.junit.After; |
| 5 | import org.junit.Before; |
| 6 | import org.junit.Test; |
| 7 | import org.onlab.onos.event.Event; |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.ConnectPoint; |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.Device; |
| 10 | import org.onlab.onos.net.DeviceId; |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.Link; |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 12 | import org.onlab.onos.net.MastershipRole; |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 13 | import org.onlab.onos.net.PortNumber; |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 14 | import org.onlab.onos.net.link.DefaultLinkDescription; |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 15 | import org.onlab.onos.net.link.LinkAdminService; |
| 16 | import org.onlab.onos.net.link.LinkEvent; |
| 17 | import org.onlab.onos.net.link.LinkListener; |
| 18 | import org.onlab.onos.net.link.LinkProvider; |
| 19 | import org.onlab.onos.net.link.LinkProviderRegistry; |
| 20 | import org.onlab.onos.net.link.LinkProviderService; |
| 21 | import org.onlab.onos.net.link.LinkService; |
| 22 | import org.onlab.onos.net.provider.AbstractProvider; |
| 23 | import org.onlab.onos.net.provider.ProviderId; |
| 24 | |
| 25 | import java.util.ArrayList; |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 26 | import java.util.Iterator; |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 27 | import java.util.List; |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 28 | import java.util.Set; |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 29 | |
| 30 | import static org.junit.Assert.*; |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 31 | import static org.onlab.onos.net.DeviceId.deviceId; |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 32 | import static org.onlab.onos.net.Link.Type.DIRECT; |
| 33 | import static org.onlab.onos.net.Link.Type.INDIRECT; |
| 34 | import static org.onlab.onos.net.link.LinkEvent.Type.*; |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * Test codifying the link service & link provider service contracts. |
| 38 | */ |
| 39 | public class SimpleLinkManagerTest { |
| 40 | |
| 41 | private static final ProviderId PID = new ProviderId("foo"); |
| 42 | private static final DeviceId DID1 = deviceId("of:foo"); |
| 43 | private static final DeviceId DID2 = deviceId("of:bar"); |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 44 | private static final DeviceId DID3 = deviceId("of:goo"); |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 45 | |
| 46 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 47 | private static final PortNumber P2 = PortNumber.portNumber(2); |
| 48 | private static final PortNumber P3 = PortNumber.portNumber(3); |
| 49 | |
| 50 | |
| 51 | private SimpleLinkManager mgr; |
| 52 | |
| 53 | protected LinkService service; |
| 54 | protected LinkAdminService admin; |
| 55 | protected LinkProviderRegistry registry; |
| 56 | protected LinkProviderService providerService; |
| 57 | protected TestProvider provider; |
| 58 | protected TestListener listener = new TestListener(); |
| 59 | |
| 60 | @Before |
| 61 | public void setUp() { |
| 62 | mgr = new SimpleLinkManager(); |
| 63 | service = mgr; |
| 64 | admin = mgr; |
| 65 | registry = mgr; |
| 66 | mgr.eventDispatcher = new TestEventDispatcher(); |
| 67 | mgr.activate(); |
| 68 | |
| 69 | service.addListener(listener); |
| 70 | |
| 71 | provider = new TestProvider(); |
| 72 | providerService = registry.register(provider); |
| 73 | assertTrue("provider should be registered", |
| 74 | registry.getProviders().contains(provider.id())); |
| 75 | } |
| 76 | |
| 77 | @After |
| 78 | public void tearDown() { |
| 79 | registry.unregister(provider); |
| 80 | assertFalse("provider should not be registered", |
| 81 | registry.getProviders().contains(provider.id())); |
| 82 | service.removeListener(listener); |
| 83 | mgr.deactivate(); |
| 84 | } |
| 85 | |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 86 | @Test |
| 87 | public void createLink() { |
tom | d176fc4 | 2014-09-08 00:12:30 -0700 | [diff] [blame] | 88 | addLink(DID1, P1, DID2, P2, DIRECT); |
| 89 | addLink(DID2, P2, DID1, P1, DIRECT); |
| 90 | assertEquals("incorrect link count", 2, service.getLinkCount()); |
| 91 | |
| 92 | Iterator<Link> it = service.getLinks().iterator(); |
| 93 | it.next(); |
| 94 | it.next(); |
| 95 | assertFalse("incorrect link count", it.hasNext()); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void updateLink() { |
| 100 | addLink(DID1, P1, DID2, P2, DIRECT); |
| 101 | addLink(DID2, P2, DID1, P1, INDIRECT); |
| 102 | assertEquals("incorrect link count", 2, service.getLinkCount()); |
| 103 | |
| 104 | providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT)); |
| 105 | validateEvents(LINK_UPDATED); |
| 106 | assertEquals("incorrect link count", 2, service.getLinkCount()); |
| 107 | |
| 108 | providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), INDIRECT)); |
| 109 | providerService.linkDetected(new DefaultLinkDescription(cp(DID2, P2), cp(DID1, P1), DIRECT)); |
| 110 | assertEquals("no events expected", 0, listener.events.size()); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void removeLink() { |
| 115 | addLink(DID1, P1, DID2, P2, DIRECT); |
| 116 | addLink(DID2, P2, DID1, P1, DIRECT); |
| 117 | assertEquals("incorrect link count", 2, service.getLinkCount()); |
| 118 | |
| 119 | providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT)); |
| 120 | validateEvents(LINK_REMOVED); |
| 121 | assertEquals("incorrect link count", 1, service.getLinkCount()); |
| 122 | assertNull("link should not be found", service.getLink(cp(DID1, P1), cp(DID2, P2))); |
| 123 | assertNotNull("link should be found", service.getLink(cp(DID2, P2), cp(DID1, P1))); |
| 124 | |
| 125 | providerService.linkVanished(new DefaultLinkDescription(cp(DID1, P1), cp(DID2, P2), DIRECT)); |
| 126 | assertEquals("no events expected", 0, listener.events.size()); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void removeLinksByConnectionPoint() { |
| 131 | Link l1 = addLink(DID1, P1, DID2, P2, DIRECT); |
| 132 | Link l2 = addLink(DID2, P2, DID1, P1, DIRECT); |
| 133 | addLink(DID3, P3, DID2, P1, DIRECT); |
| 134 | addLink(DID2, P1, DID3, P3, DIRECT); |
| 135 | assertEquals("incorrect link count", 4, service.getLinkCount()); |
| 136 | |
| 137 | providerService.linksVanished(cp(DID1, P1)); |
| 138 | assertEquals("incorrect link count", 2, service.getLinkCount()); |
| 139 | assertNull("link should be gone", service.getLink(l1.src(), l1.dst())); |
| 140 | assertNull("link should be gone", service.getLink(l2.src(), l2.dst())); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void removeLinksByDevice() { |
| 145 | addLink(DID1, P1, DID2, P2, DIRECT); |
| 146 | addLink(DID2, P2, DID1, P1, DIRECT); |
| 147 | addLink(DID3, P3, DID2, P1, DIRECT); |
| 148 | addLink(DID2, P1, DID3, P3, DIRECT); |
| 149 | Link l5 = addLink(DID3, P1, DID1, P2, DIRECT); |
| 150 | Link l6 = addLink(DID1, P2, DID3, P1, DIRECT); |
| 151 | assertEquals("incorrect link count", 6, service.getLinkCount()); |
| 152 | |
| 153 | providerService.linksVanished(DID2); |
| 154 | assertEquals("incorrect link count", 2, service.getLinkCount()); |
| 155 | assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst())); |
| 156 | assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst())); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void removeLinksAsAdminByConnectionPoint() { |
| 161 | Link l1 = addLink(DID1, P1, DID2, P2, DIRECT); |
| 162 | Link l2 = addLink(DID2, P2, DID1, P1, DIRECT); |
| 163 | addLink(DID3, P3, DID2, P1, DIRECT); |
| 164 | addLink(DID2, P1, DID3, P3, DIRECT); |
| 165 | assertEquals("incorrect link count", 4, service.getLinkCount()); |
| 166 | |
| 167 | admin.removeLinks(cp(DID1, P1)); |
| 168 | assertEquals("incorrect link count", 2, service.getLinkCount()); |
| 169 | assertNull("link should be gone", service.getLink(l1.src(), l1.dst())); |
| 170 | assertNull("link should be gone", service.getLink(l2.src(), l2.dst())); |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void removeLinksAsAdminByDevice() { |
| 175 | addLink(DID1, P1, DID2, P2, DIRECT); |
| 176 | addLink(DID2, P2, DID1, P1, DIRECT); |
| 177 | addLink(DID3, P3, DID2, P1, DIRECT); |
| 178 | addLink(DID2, P1, DID3, P3, DIRECT); |
| 179 | Link l5 = addLink(DID3, P1, DID1, P2, DIRECT); |
| 180 | Link l6 = addLink(DID1, P2, DID3, P1, DIRECT); |
| 181 | assertEquals("incorrect link count", 6, service.getLinkCount()); |
| 182 | |
| 183 | admin.removeLinks(DID2); |
| 184 | assertEquals("incorrect link count", 2, service.getLinkCount()); |
| 185 | assertNotNull("link should not be gone", service.getLink(l5.src(), l5.dst())); |
| 186 | assertNotNull("link should not be gone", service.getLink(l6.src(), l6.dst())); |
| 187 | } |
| 188 | |
| 189 | @Test |
| 190 | public void getLinks() { |
| 191 | Link l1 = addLink(DID1, P1, DID2, P2, DIRECT); |
| 192 | Link l2 = addLink(DID2, P2, DID1, P1, DIRECT); |
| 193 | Link l3 = addLink(DID3, P3, DID2, P1, DIRECT); |
| 194 | Link l4 = addLink(DID2, P1, DID3, P3, DIRECT); |
| 195 | assertEquals("incorrect link count", 4, service.getLinkCount()); |
| 196 | |
| 197 | Set<Link> links = service.getLinks(cp(DID1, P1)); |
| 198 | assertEquals("incorrect links", ImmutableSet.of(l1, l2), links); |
| 199 | links = service.getEgressLinks(cp(DID1, P1)); |
| 200 | assertEquals("incorrect links", ImmutableSet.of(l1), links); |
| 201 | links = service.getIngressLinks(cp(DID1, P1)); |
| 202 | assertEquals("incorrect links", ImmutableSet.of(l2), links); |
| 203 | |
| 204 | links = service.getDeviceLinks(DID2); |
| 205 | assertEquals("incorrect links", ImmutableSet.of(l1, l2, l3, l4), links); |
| 206 | links = service.getDeviceLinks(DID3); |
| 207 | assertEquals("incorrect links", ImmutableSet.of(l3, l4), links); |
| 208 | |
| 209 | links = service.getDeviceEgressLinks(DID2); |
| 210 | assertEquals("incorrect links", ImmutableSet.of(l2, l4), links); |
| 211 | links = service.getDeviceIngressLinks(DID2); |
| 212 | assertEquals("incorrect links", ImmutableSet.of(l1, l3), links); |
| 213 | } |
| 214 | |
| 215 | |
| 216 | private Link addLink(DeviceId sd, PortNumber sp, DeviceId dd, PortNumber dp, |
| 217 | Link.Type type) { |
| 218 | providerService.linkDetected(new DefaultLinkDescription(cp(sd, sp), cp(dd, dp), type)); |
| 219 | Link link = listener.events.get(0).subject(); |
| 220 | validateEvents(LINK_ADDED); |
| 221 | return link; |
| 222 | } |
| 223 | |
| 224 | private ConnectPoint cp(DeviceId id, PortNumber portNumber) { |
| 225 | return new ConnectPoint(id, portNumber); |
tom | bb58c20 | 2014-09-07 22:51:50 -0700 | [diff] [blame] | 226 | } |
| 227 | |
tom | 0d39526 | 2014-09-07 16:53:40 -0700 | [diff] [blame] | 228 | protected void validateEvents(Enum... types) { |
| 229 | int i = 0; |
| 230 | assertEquals("wrong events received", types.length, listener.events.size()); |
| 231 | for (Event event : listener.events) { |
| 232 | assertEquals("incorrect event type", types[i], event.type()); |
| 233 | i++; |
| 234 | } |
| 235 | listener.events.clear(); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | private class TestProvider extends AbstractProvider implements LinkProvider { |
| 240 | private Device deviceReceived; |
| 241 | private MastershipRole roleReceived; |
| 242 | |
| 243 | public TestProvider() { |
| 244 | super(PID); |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | private static class TestListener implements LinkListener { |
| 249 | final List<LinkEvent> events = new ArrayList<>(); |
| 250 | |
| 251 | @Override |
| 252 | public void event(LinkEvent event) { |
| 253 | events.add(event); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | } |