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