Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.provider.lldp.impl; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 17 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 18 | import static org.easymock.EasyMock.createMock; |
| 19 | import static org.easymock.EasyMock.expect; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 20 | import static org.easymock.EasyMock.replay; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertFalse; |
| 23 | import static org.junit.Assert.assertNotNull; |
| 24 | import static org.junit.Assert.assertNull; |
| 25 | import static org.junit.Assert.assertTrue; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 26 | |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 27 | import java.nio.ByteBuffer; |
| 28 | import java.util.Collections; |
| 29 | import java.util.HashMap; |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
Yuta HIGUCHI | 376d750 | 2014-10-19 15:57:52 -0700 | [diff] [blame] | 33 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 34 | import org.junit.After; |
| 35 | import org.junit.Before; |
| 36 | import org.junit.Test; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 37 | import org.onlab.packet.ChassisId; |
| 38 | import org.onlab.packet.Ethernet; |
| 39 | import org.onlab.packet.ONOSLLDP; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 40 | import org.onosproject.cfg.ComponentConfigAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.cluster.NodeId; |
| 42 | import org.onosproject.cluster.RoleInfo; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 43 | import org.onosproject.core.ApplicationId; |
| 44 | import org.onosproject.core.CoreService; |
| 45 | import org.onosproject.core.DefaultApplicationId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 46 | import org.onosproject.mastership.MastershipListener; |
| 47 | import org.onosproject.mastership.MastershipService; |
| 48 | import org.onosproject.net.ConnectPoint; |
| 49 | import org.onosproject.net.DefaultDevice; |
| 50 | import org.onosproject.net.DefaultPort; |
| 51 | import org.onosproject.net.Device; |
| 52 | import org.onosproject.net.DeviceId; |
| 53 | import org.onosproject.net.MastershipRole; |
| 54 | import org.onosproject.net.Port; |
| 55 | import org.onosproject.net.PortNumber; |
| 56 | import org.onosproject.net.device.DeviceEvent; |
| 57 | import org.onosproject.net.device.DeviceListener; |
| 58 | import org.onosproject.net.device.DeviceServiceAdapter; |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 59 | import org.onosproject.net.flow.FlowRule; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 60 | import org.onosproject.net.flow.TrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 61 | import org.onosproject.net.flow.TrafficTreatment; |
| 62 | import org.onosproject.net.link.LinkDescription; |
| 63 | import org.onosproject.net.link.LinkProvider; |
| 64 | import org.onosproject.net.link.LinkProviderRegistry; |
| 65 | import org.onosproject.net.link.LinkProviderService; |
| 66 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 67 | import org.onosproject.net.packet.InboundPacket; |
| 68 | import org.onosproject.net.packet.OutboundPacket; |
| 69 | import org.onosproject.net.packet.PacketContext; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 70 | import org.onosproject.net.packet.PacketPriority; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 71 | import org.onosproject.net.packet.PacketProcessor; |
| 72 | import org.onosproject.net.packet.PacketService; |
| 73 | import org.onosproject.net.provider.AbstractProviderService; |
| 74 | import org.onosproject.net.provider.ProviderId; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 75 | |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 76 | import com.google.common.collect.ArrayListMultimap; |
| 77 | import com.google.common.collect.Lists; |
| 78 | import com.google.common.collect.Maps; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 79 | |
| 80 | public class LLDPLinkProviderTest { |
| 81 | |
| 82 | private static final DeviceId DID1 = DeviceId.deviceId("of:0000000000000001"); |
| 83 | private static final DeviceId DID2 = DeviceId.deviceId("of:0000000000000002"); |
| 84 | |
| 85 | private static Port pd1; |
| 86 | private static Port pd2; |
| 87 | private static Port pd3; |
| 88 | private static Port pd4; |
| 89 | |
| 90 | private final LLDPLinkProvider provider = new LLDPLinkProvider(); |
| 91 | private final TestLinkRegistry linkService = new TestLinkRegistry(); |
| 92 | private final TestPacketService packetService = new TestPacketService(); |
| 93 | private final TestDeviceService deviceService = new TestDeviceService(); |
| 94 | private final TestMasterShipService masterService = new TestMasterShipService(); |
| 95 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 96 | private CoreService coreService; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 97 | private TestLinkProviderService providerService; |
| 98 | |
| 99 | private PacketProcessor testProcessor; |
| 100 | private DeviceListener deviceListener; |
| 101 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 102 | private ApplicationId appId = |
| 103 | new DefaultApplicationId(100, "org.onosproject.provider.lldp"); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 104 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 105 | @Before |
| 106 | public void setUp() { |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 107 | coreService = createMock(CoreService.class); |
| 108 | expect(coreService.registerApplication(appId.name())) |
| 109 | .andReturn(appId).anyTimes(); |
| 110 | replay(coreService); |
| 111 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 112 | provider.cfgService = new ComponentConfigAdapter(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 113 | provider.coreService = coreService; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 114 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 115 | provider.deviceService = deviceService; |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 116 | provider.packetService = packetService; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 117 | provider.providerRegistry = linkService; |
| 118 | provider.masterService = masterService; |
| 119 | |
| 120 | |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 121 | provider.activate(null); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Test |
| 125 | public void basics() { |
| 126 | assertNotNull("registration expected", providerService); |
| 127 | assertEquals("incorrect provider", provider, providerService.provider()); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void switchAdd() { |
| 132 | DeviceEvent de = deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1); |
| 133 | deviceListener.event(de); |
| 134 | |
| 135 | assertFalse("Device not added", provider.discoverers.isEmpty()); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void switchRemove() { |
| 140 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 141 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_REMOVED, DID1)); |
| 142 | |
| 143 | assertTrue("Discoverer is not gone", provider.discoverers.get(DID1).isStopped()); |
| 144 | assertTrue("Device is not gone.", vanishedDpid(DID1)); |
| 145 | } |
| 146 | |
| 147 | @Test |
| 148 | public void portUp() { |
| 149 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 150 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 3, true))); |
| 151 | |
| 152 | assertTrue("Port not added to discoverer", |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 153 | provider.discoverers.get(DID1).containsPort(3L)); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void portDown() { |
| 158 | |
| 159 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 160 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 1, false))); |
| 161 | |
| 162 | |
| 163 | |
| 164 | assertFalse("Port added to discoverer", |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 165 | provider.discoverers.get(DID1).containsPort(1L)); |
| 166 | assertTrue("Port is not gone.", vanishedPort(1L)); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | @Test |
| 170 | public void portUnknown() { |
| 171 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 172 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID2, port(DID2, 1, false))); |
| 173 | |
| 174 | |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 175 | assertNull("DeviceId exists", |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 176 | provider.discoverers.get(DID2)); |
| 177 | } |
| 178 | |
| 179 | @Test |
| 180 | public void unknownPktCtx() { |
| 181 | |
| 182 | PacketContext pktCtx = new TestPacketContext(deviceService.getDevice(DID2)); |
| 183 | |
| 184 | testProcessor.process(pktCtx); |
| 185 | assertFalse("Context should still be free", pktCtx.isHandled()); |
| 186 | } |
| 187 | |
| 188 | @Test |
| 189 | public void knownPktCtx() { |
| 190 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 191 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID2)); |
| 192 | PacketContext pktCtx = new TestPacketContext(deviceService.getDevice(DID2)); |
| 193 | |
| 194 | |
| 195 | testProcessor.process(pktCtx); |
| 196 | |
| 197 | assertTrue("Link not detected", detectedLink(DID1, DID2)); |
| 198 | |
| 199 | } |
| 200 | |
| 201 | |
| 202 | @After |
| 203 | public void tearDown() { |
| 204 | provider.deactivate(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 205 | provider.coreService = null; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 206 | provider.providerRegistry = null; |
| 207 | provider.deviceService = null; |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 208 | provider.packetService = null; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | private DeviceEvent deviceEvent(DeviceEvent.Type type, DeviceId did) { |
| 212 | return new DeviceEvent(type, deviceService.getDevice(did)); |
| 213 | |
| 214 | } |
| 215 | |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 216 | @SuppressWarnings(value = { "unused" }) |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 217 | private DeviceEvent portEvent(DeviceEvent.Type type, DeviceId did, PortNumber port) { |
| 218 | return new DeviceEvent(type, deviceService.getDevice(did), |
| 219 | deviceService.getPort(did, port)); |
| 220 | } |
| 221 | |
| 222 | private DeviceEvent portEvent(DeviceEvent.Type type, DeviceId did, Port port) { |
| 223 | return new DeviceEvent(type, deviceService.getDevice(did), port); |
| 224 | } |
| 225 | |
| 226 | private Port port(DeviceId did, long port, boolean enabled) { |
| 227 | return new DefaultPort(deviceService.getDevice(did), |
| 228 | PortNumber.portNumber(port), enabled); |
| 229 | } |
| 230 | |
| 231 | |
| 232 | private boolean vanishedDpid(DeviceId... dids) { |
| 233 | for (int i = 0; i < dids.length; i++) { |
| 234 | if (!providerService.vanishedDpid.contains(dids[i])) { |
| 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | private boolean vanishedPort(Long... ports) { |
| 242 | for (int i = 0; i < ports.length; i++) { |
| 243 | if (!providerService.vanishedPort.contains(ports[i])) { |
| 244 | return false; |
| 245 | } |
| 246 | } |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | private boolean detectedLink(DeviceId src, DeviceId dst) { |
| 251 | for (DeviceId key : providerService.discoveredLinks.keySet()) { |
| 252 | if (key.equals(src)) { |
| 253 | return providerService.discoveredLinks.get(src).equals(dst); |
| 254 | } |
| 255 | } |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | |
| 260 | private class TestLinkRegistry implements LinkProviderRegistry { |
| 261 | |
| 262 | @Override |
| 263 | public LinkProviderService register(LinkProvider provider) { |
| 264 | providerService = new TestLinkProviderService(provider); |
| 265 | return providerService; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public void unregister(LinkProvider provider) { |
| 270 | } |
| 271 | |
| 272 | @Override |
| 273 | public Set<ProviderId> getProviders() { |
| 274 | return null; |
| 275 | } |
| 276 | |
| 277 | } |
| 278 | |
| 279 | private class TestLinkProviderService |
| 280 | extends AbstractProviderService<LinkProvider> |
| 281 | implements LinkProviderService { |
| 282 | |
| 283 | List<DeviceId> vanishedDpid = Lists.newLinkedList(); |
| 284 | List<Long> vanishedPort = Lists.newLinkedList(); |
| 285 | Map<DeviceId, DeviceId> discoveredLinks = Maps.newHashMap(); |
| 286 | |
| 287 | protected TestLinkProviderService(LinkProvider provider) { |
| 288 | super(provider); |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void linkDetected(LinkDescription linkDescription) { |
| 293 | DeviceId sDid = linkDescription.src().deviceId(); |
| 294 | DeviceId dDid = linkDescription.dst().deviceId(); |
| 295 | discoveredLinks.put(sDid, dDid); |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | public void linkVanished(LinkDescription linkDescription) { |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void linksVanished(ConnectPoint connectPoint) { |
| 304 | vanishedPort.add(connectPoint.port().toLong()); |
| 305 | |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void linksVanished(DeviceId deviceId) { |
| 310 | vanishedDpid.add(deviceId); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | } |
| 315 | |
| 316 | |
| 317 | |
| 318 | private class TestPacketContext implements PacketContext { |
| 319 | |
| 320 | protected Device device; |
| 321 | protected boolean blocked = false; |
| 322 | |
| 323 | public TestPacketContext(Device dev) { |
| 324 | device = dev; |
| 325 | } |
| 326 | |
| 327 | @Override |
| 328 | public long time() { |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | @Override |
| 333 | public InboundPacket inPacket() { |
| 334 | ONOSLLDP lldp = new ONOSLLDP(); |
| 335 | lldp.setChassisId(device.chassisId()); |
| 336 | lldp.setPortId((int) pd1.number().toLong()); |
| 337 | lldp.setDevice(deviceService.getDevice(DID1).id().toString()); |
| 338 | |
| 339 | |
| 340 | Ethernet ethPacket = new Ethernet(); |
| 341 | ethPacket.setEtherType(Ethernet.TYPE_LLDP); |
| 342 | ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA); |
| 343 | ethPacket.setPayload(lldp); |
| 344 | ethPacket.setPad(true); |
| 345 | |
| 346 | |
| 347 | |
| 348 | ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11"); |
| 349 | |
| 350 | ConnectPoint cp = new ConnectPoint(device.id(), pd3.number()); |
| 351 | |
| 352 | return new DefaultInboundPacket(cp, ethPacket, |
| 353 | ByteBuffer.wrap(ethPacket.serialize())); |
| 354 | |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public OutboundPacket outPacket() { |
| 359 | return null; |
| 360 | } |
| 361 | |
| 362 | @Override |
| 363 | public TrafficTreatment.Builder treatmentBuilder() { |
| 364 | return null; |
| 365 | } |
| 366 | |
| 367 | @Override |
| 368 | public void send() { |
| 369 | |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public boolean block() { |
| 374 | blocked = true; |
| 375 | return blocked; |
| 376 | } |
| 377 | |
| 378 | @Override |
| 379 | public boolean isHandled() { |
| 380 | return blocked; |
| 381 | } |
| 382 | |
| 383 | } |
| 384 | |
| 385 | private class TestPacketService implements PacketService { |
| 386 | |
| 387 | @Override |
| 388 | public void addProcessor(PacketProcessor processor, int priority) { |
| 389 | testProcessor = processor; |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public void removeProcessor(PacketProcessor processor) { |
| 394 | |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public void emit(OutboundPacket packet) { |
| 399 | |
| 400 | } |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 401 | |
| 402 | @Override |
| 403 | public void requestPackets(TrafficSelector selector, |
| 404 | PacketPriority priority, ApplicationId appId) { |
| 405 | } |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 406 | |
| 407 | @Override |
| 408 | public void requestPackets(TrafficSelector selector, |
| 409 | PacketPriority priority, ApplicationId appId, |
| 410 | FlowRule.Type tableType) { |
| 411 | } |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 412 | } |
| 413 | |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 414 | private class TestDeviceService extends DeviceServiceAdapter { |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 415 | |
| 416 | private Map<DeviceId, Device> devices = new HashMap<>(); |
| 417 | private final ArrayListMultimap<DeviceId, Port> ports = |
| 418 | ArrayListMultimap.create(); |
| 419 | |
| 420 | public TestDeviceService() { |
| 421 | Device d1 = new DefaultDevice(ProviderId.NONE, DID1, Device.Type.SWITCH, |
| 422 | "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId()); |
| 423 | Device d2 = new DefaultDevice(ProviderId.NONE, DID2, Device.Type.SWITCH, |
| 424 | "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId()); |
| 425 | devices.put(DID1, d1); |
| 426 | devices.put(DID2, d2); |
| 427 | |
| 428 | pd1 = new DefaultPort(d1, PortNumber.portNumber(1), true); |
| 429 | pd2 = new DefaultPort(d1, PortNumber.portNumber(2), true); |
| 430 | pd3 = new DefaultPort(d2, PortNumber.portNumber(1), true); |
| 431 | pd4 = new DefaultPort(d2, PortNumber.portNumber(2), true); |
| 432 | |
| 433 | ports.putAll(DID1, Lists.newArrayList(pd1, pd2)); |
| 434 | ports.putAll(DID2, Lists.newArrayList(pd3, pd4)); |
| 435 | |
| 436 | |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public int getDeviceCount() { |
| 441 | return devices.values().size(); |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public Iterable<Device> getDevices() { |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 446 | return Collections.emptyList(); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @Override |
| 450 | public Device getDevice(DeviceId deviceId) { |
| 451 | return devices.get(deviceId); |
| 452 | } |
| 453 | |
| 454 | @Override |
| 455 | public MastershipRole getRole(DeviceId deviceId) { |
| 456 | return MastershipRole.MASTER; |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | public List<Port> getPorts(DeviceId deviceId) { |
| 461 | return ports.get(deviceId); |
| 462 | } |
| 463 | |
| 464 | @Override |
| 465 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 466 | for (Port p : ports.get(deviceId)) { |
| 467 | if (p.number().equals(portNumber)) { |
| 468 | return p; |
| 469 | } |
| 470 | } |
| 471 | return null; |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public boolean isAvailable(DeviceId deviceId) { |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public void addListener(DeviceListener listener) { |
| 481 | deviceListener = listener; |
| 482 | |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public void removeListener(DeviceListener listener) { |
| 487 | |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | private final class TestMasterShipService implements MastershipService { |
| 492 | |
| 493 | @Override |
| 494 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 495 | return MastershipRole.MASTER; |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public MastershipRole requestRoleFor(DeviceId deviceId) { |
| 500 | return null; |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void relinquishMastership(DeviceId deviceId) { |
| 505 | |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public NodeId getMasterFor(DeviceId deviceId) { |
| 510 | return null; |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public Set<DeviceId> getDevicesOf(NodeId nodeId) { |
| 515 | return null; |
| 516 | } |
| 517 | |
| 518 | @Override |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 519 | public void addListener(MastershipListener listener) { |
| 520 | |
| 521 | } |
| 522 | |
| 523 | @Override |
| 524 | public void removeListener(MastershipListener listener) { |
| 525 | |
| 526 | } |
Yuta HIGUCHI | 376d750 | 2014-10-19 15:57:52 -0700 | [diff] [blame] | 527 | |
| 528 | @Override |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 529 | public RoleInfo getNodesFor(DeviceId deviceId) { |
| 530 | return new RoleInfo(new NodeId("foo"), Collections.<NodeId>emptyList()); |
Yuta HIGUCHI | 376d750 | 2014-10-19 15:57:52 -0700 | [diff] [blame] | 531 | } |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | |
| 535 | } |