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