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