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