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 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ArrayListMultimap; |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 21 | import com.google.common.collect.ImmutableMap; |
| 22 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 23 | import com.google.common.collect.Lists; |
| 24 | import com.google.common.collect.Maps; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
Jonathan Hart | 3cfce8e | 2015-01-14 16:43:27 -0800 | [diff] [blame] | 28 | import org.onlab.packet.ChassisId; |
| 29 | import org.onlab.packet.Ethernet; |
| 30 | import org.onlab.packet.ONOSLLDP; |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 31 | import org.onosproject.cfg.ComponentConfigAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.cluster.NodeId; |
| 33 | import org.onosproject.cluster.RoleInfo; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 34 | import org.onosproject.core.ApplicationId; |
| 35 | import org.onosproject.core.CoreService; |
| 36 | import org.onosproject.core.DefaultApplicationId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.mastership.MastershipListener; |
| 38 | import org.onosproject.mastership.MastershipService; |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 39 | import org.onosproject.net.Annotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.ConnectPoint; |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 41 | import org.onosproject.net.DefaultAnnotations; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 42 | import org.onosproject.net.DefaultDevice; |
| 43 | import org.onosproject.net.DefaultPort; |
| 44 | import org.onosproject.net.Device; |
| 45 | import org.onosproject.net.DeviceId; |
| 46 | import org.onosproject.net.MastershipRole; |
| 47 | import org.onosproject.net.Port; |
| 48 | import org.onosproject.net.PortNumber; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 49 | import org.onosproject.net.config.Config; |
| 50 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 51 | import org.onosproject.net.config.NetworkConfigEvent; |
| 52 | import org.onosproject.net.config.NetworkConfigListener; |
| 53 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 54 | import org.onosproject.net.device.DeviceEvent; |
| 55 | import org.onosproject.net.device.DeviceListener; |
| 56 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 57 | import org.onosproject.net.flow.TrafficTreatment; |
| 58 | import org.onosproject.net.link.LinkDescription; |
| 59 | import org.onosproject.net.link.LinkProvider; |
| 60 | import org.onosproject.net.link.LinkProviderRegistry; |
| 61 | import org.onosproject.net.link.LinkProviderService; |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 62 | import org.onosproject.net.link.LinkServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | import org.onosproject.net.packet.DefaultInboundPacket; |
| 64 | import org.onosproject.net.packet.InboundPacket; |
| 65 | import org.onosproject.net.packet.OutboundPacket; |
| 66 | import org.onosproject.net.packet.PacketContext; |
| 67 | import org.onosproject.net.packet.PacketProcessor; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 68 | import org.onosproject.net.packet.PacketServiceAdapter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 69 | import org.onosproject.net.provider.AbstractProviderService; |
| 70 | import org.onosproject.net.provider.ProviderId; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 71 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 72 | import java.nio.ByteBuffer; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 73 | import java.util.HashMap; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 74 | import java.util.HashSet; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 75 | import java.util.List; |
| 76 | import java.util.Map; |
| 77 | import java.util.Set; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 78 | import java.util.Collections; |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 79 | import java.util.concurrent.CompletableFuture; |
| 80 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 81 | import static org.easymock.EasyMock.createMock; |
| 82 | import static org.easymock.EasyMock.expect; |
| 83 | import static org.easymock.EasyMock.replay; |
| 84 | |
| 85 | import static org.junit.Assert.assertNull; |
| 86 | import static org.junit.Assert.assertNotNull; |
| 87 | import static org.junit.Assert.assertEquals; |
| 88 | import static org.junit.Assert.assertTrue; |
| 89 | import static org.onosproject.provider.lldp.impl.LldpLinkProvider.DEFAULT_RULES; |
| 90 | import static org.junit.Assert.assertFalse; |
| 91 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 92 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 93 | public class LldpLinkProviderTest { |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 94 | |
| 95 | private static final DeviceId DID1 = DeviceId.deviceId("of:0000000000000001"); |
| 96 | private static final DeviceId DID2 = DeviceId.deviceId("of:0000000000000002"); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 97 | private static final DeviceId DID3 = DeviceId.deviceId("of:0000000000000003"); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 98 | |
| 99 | private static Port pd1; |
| 100 | private static Port pd2; |
| 101 | private static Port pd3; |
| 102 | private static Port pd4; |
| 103 | |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 104 | private final LldpLinkProvider provider = new LldpLinkProvider(); |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 105 | private final TestLinkRegistry linkRegistry = new TestLinkRegistry(); |
| 106 | private final TestLinkService linkService = new TestLinkService(); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 107 | private final TestPacketService packetService = new TestPacketService(); |
| 108 | private final TestDeviceService deviceService = new TestDeviceService(); |
| 109 | private final TestMasterShipService masterService = new TestMasterShipService(); |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 110 | private final TestNetworkConfigRegistry configRegistry = new TestNetworkConfigRegistry(); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 111 | |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 112 | private CoreService coreService; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 113 | private TestLinkProviderService providerService; |
| 114 | |
| 115 | private PacketProcessor testProcessor; |
| 116 | private DeviceListener deviceListener; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 117 | private NetworkConfigListener configListener; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 118 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 119 | private ApplicationId appId = |
| 120 | new DefaultApplicationId(100, "org.onosproject.provider.lldp"); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 121 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 122 | private TestSuppressionConfig cfg; |
| 123 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 124 | @Before |
| 125 | public void setUp() { |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 126 | cfg = new TestSuppressionConfig(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 127 | coreService = createMock(CoreService.class); |
| 128 | expect(coreService.registerApplication(appId.name())) |
| 129 | .andReturn(appId).anyTimes(); |
| 130 | replay(coreService); |
| 131 | |
Thomas Vachuska | 6519e6f | 2015-03-11 02:29:31 -0700 | [diff] [blame] | 132 | provider.cfgService = new ComponentConfigAdapter(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 133 | provider.coreService = coreService; |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 134 | provider.cfgRegistry = configRegistry; |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 135 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 136 | provider.deviceService = deviceService; |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 137 | provider.linkService = linkService; |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 138 | provider.packetService = packetService; |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 139 | provider.providerRegistry = linkRegistry; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 140 | provider.masterService = masterService; |
| 141 | |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 142 | provider.activate(null); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Test |
| 146 | public void basics() { |
| 147 | assertNotNull("registration expected", providerService); |
| 148 | assertEquals("incorrect provider", provider, providerService.provider()); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void switchAdd() { |
| 153 | DeviceEvent de = deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1); |
| 154 | deviceListener.event(de); |
| 155 | |
| 156 | assertFalse("Device not added", provider.discoverers.isEmpty()); |
| 157 | } |
| 158 | |
| 159 | @Test |
| 160 | public void switchRemove() { |
| 161 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 162 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_REMOVED, DID1)); |
| 163 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 164 | final LinkDiscovery linkDiscovery = provider.discoverers.get(DID1); |
| 165 | if (linkDiscovery != null) { |
| 166 | // If LinkDiscovery helper is there after DEVICE_REMOVED, |
| 167 | // it should be stopped |
| 168 | assertTrue("Discoverer is not stopped", linkDiscovery.isStopped()); |
| 169 | } |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 170 | assertTrue("Device is not gone.", vanishedDpid(DID1)); |
| 171 | } |
| 172 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Checks that links on a reconfigured switch are properly removed. |
| 175 | */ |
| 176 | @Test |
| 177 | public void switchSuppressed() { |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 178 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 179 | // add device to stub DeviceService |
| 180 | deviceService.putDevice(device(DID3)); |
| 181 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID3)); |
| 182 | |
| 183 | assertFalse("Device not added", provider.discoverers.isEmpty()); |
| 184 | |
| 185 | // update device in stub DeviceService with suppression config |
| 186 | deviceService.putDevice(device(DID3, DefaultAnnotations.builder() |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 187 | .set(LldpLinkProvider.NO_LLDP, "true") |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 188 | .build())); |
| 189 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_UPDATED, DID3)); |
| 190 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 191 | // discovery on device is expected to be gone or stopped |
| 192 | LinkDiscovery linkDiscovery = provider.discoverers.get(DID3); |
| 193 | if (linkDiscovery != null) { |
| 194 | assertTrue("Discovery expected to be stopped", linkDiscovery.isStopped()); |
| 195 | } |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 196 | } |
| 197 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 198 | @Test |
| 199 | public void portUp() { |
| 200 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 201 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 3, true))); |
| 202 | |
| 203 | assertTrue("Port not added to discoverer", |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 204 | provider.discoverers.get(DID1).containsPort(3L)); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Test |
| 208 | public void portDown() { |
| 209 | |
| 210 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 211 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 1, false))); |
| 212 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 213 | assertFalse("Port added to discoverer", |
Sho SHIMIZU | c14b2a2 | 2015-05-05 18:15:13 -0700 | [diff] [blame] | 214 | provider.discoverers.get(DID1).containsPort(1L)); |
| 215 | assertTrue("Port is not gone.", vanishedPort(1L)); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | @Test |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 219 | public void portRemoved() { |
| 220 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 221 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 3, true))); |
| 222 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_REMOVED, DID1, port(DID1, 3, true))); |
| 223 | |
| 224 | assertTrue("Port is not gone.", vanishedPort(3L)); |
| 225 | assertFalse("Port was not removed from discoverer", |
| 226 | provider.discoverers.get(DID1).containsPort(3L)); |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Checks that discovery on reconfigured switch are properly restarted. |
| 231 | */ |
| 232 | @Test |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 233 | public void portSuppressedByDeviceAnnotationConfig() { |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 234 | |
| 235 | /// When Device is configured with suppression:ON, Port also is same |
| 236 | |
| 237 | // add device in stub DeviceService with suppression configured |
| 238 | deviceService.putDevice(device(DID3, DefaultAnnotations.builder() |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 239 | .set(LldpLinkProvider.NO_LLDP, "true") |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 240 | .build())); |
| 241 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID3)); |
| 242 | |
| 243 | // non-suppressed port added to suppressed device |
| 244 | final long portno3 = 3L; |
| 245 | deviceService.putPorts(DID3, port(DID3, portno3, true)); |
| 246 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID3, port(DID3, portno3, true))); |
| 247 | |
| 248 | // discovery on device is expected to be stopped |
| 249 | LinkDiscovery linkDiscovery = provider.discoverers.get(DID3); |
| 250 | if (linkDiscovery != null) { |
| 251 | assertTrue("Discovery expected to be stopped", linkDiscovery.isStopped()); |
| 252 | } |
| 253 | |
| 254 | /// When Device is reconfigured without suppression:OFF, |
| 255 | /// Port should be included for discovery |
| 256 | |
| 257 | // update device in stub DeviceService without suppression configured |
| 258 | deviceService.putDevice(device(DID3)); |
| 259 | // update the Port in stub DeviceService. (Port has reference to Device) |
| 260 | deviceService.putPorts(DID3, port(DID3, portno3, true)); |
| 261 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_UPDATED, DID3)); |
| 262 | |
| 263 | // discovery should come back on |
| 264 | assertFalse("Discoverer is expected to start", provider.discoverers.get(DID3).isStopped()); |
| 265 | assertTrue("Discoverer should contain the port there", provider.discoverers.get(DID3).containsPort(portno3)); |
| 266 | } |
| 267 | |
| 268 | /** |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 269 | * Checks that discovery on reconfigured switch are properly restarted. |
| 270 | */ |
| 271 | @Test |
| 272 | public void portSuppressedByDeviceIdConfig() { |
| 273 | |
| 274 | /// When Device is configured without suppression:OFF, |
| 275 | /// Port should be included for discovery |
| 276 | |
| 277 | // add device in stub DeviceService without suppression configured |
| 278 | deviceService.putDevice(device(DID3)); |
| 279 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID3)); |
| 280 | |
| 281 | // non-suppressed port added to suppressed device |
| 282 | final long portno3 = 3L; |
| 283 | deviceService.putPorts(DID3, port(DID3, portno3, true)); |
| 284 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID3, port(DID3, portno3, true))); |
| 285 | |
| 286 | // discovery should succeed |
| 287 | assertFalse("Discoverer is expected to start", provider.discoverers.get(DID3).isStopped()); |
| 288 | assertTrue("Discoverer should contain the port there", provider.discoverers.get(DID3).containsPort(portno3)); |
| 289 | |
| 290 | // add suppression rule for "deviceId: "of:0000000000000003"" |
| 291 | provider.updateRules(new SuppressionRules(ImmutableSet.of(DID3), |
| 292 | ImmutableSet.of(), |
| 293 | ImmutableMap.of())); |
| 294 | |
| 295 | /// When Device is reconfigured with suppression:ON, Port also is same |
| 296 | |
| 297 | // update device in stub DeviceService with suppression configured |
| 298 | deviceService.putDevice(device(DID3)); |
| 299 | // update the Port in stub DeviceService. (Port has reference to Device) |
| 300 | deviceService.putPorts(DID3, port(DID3, portno3, true)); |
| 301 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_UPDATED, DID3)); |
| 302 | |
| 303 | // discovery on device is expected to be stopped |
| 304 | LinkDiscovery linkDiscovery = provider.discoverers.get(DID3); |
| 305 | if (linkDiscovery != null) { |
| 306 | assertTrue("Discovery expected to be stopped", linkDiscovery.isStopped()); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Checks that discovery on reconfigured switch are properly restarted. |
| 312 | */ |
| 313 | @Test |
| 314 | public void portSuppressedByDeviceTypeConfig() { |
| 315 | |
| 316 | /// When Device is configured without suppression:OFF, |
| 317 | /// Port should be included for discovery |
| 318 | |
| 319 | // add device in stub DeviceService without suppression configured |
| 320 | deviceService.putDevice(device(DID1, Device.Type.SWITCH)); |
| 321 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 322 | |
| 323 | // non-suppressed port added to suppressed device |
| 324 | final long portno3 = 3L; |
| 325 | deviceService.putPorts(DID1, port(DID1, portno3, true)); |
| 326 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, portno3, true))); |
| 327 | |
| 328 | // add device in stub DeviceService with suppression configured |
| 329 | deviceService.putDevice(device(DID2, Device.Type.ROADM)); |
| 330 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID2)); |
| 331 | |
| 332 | // non-suppressed port added to suppressed device |
| 333 | final long portno4 = 4L; |
| 334 | deviceService.putPorts(DID2, port(DID2, portno4, true)); |
| 335 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID2, port(DID2, portno4, true))); |
| 336 | |
| 337 | // discovery should succeed for this device |
| 338 | assertFalse("Discoverer is expected to start", provider.discoverers.get(DID1).isStopped()); |
| 339 | assertTrue("Discoverer should contain the port there", provider.discoverers.get(DID1).containsPort(portno3)); |
| 340 | |
| 341 | // discovery on device is expected to be stopped for this device |
| 342 | LinkDiscovery linkDiscovery = provider.discoverers.get(DID2); |
| 343 | if (linkDiscovery != null) { |
| 344 | assertTrue("Discovery expected to be stopped", linkDiscovery.isStopped()); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /** |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 349 | * Checks that discovery on reconfigured port are properly restarted. |
| 350 | */ |
| 351 | @Test |
| 352 | public void portSuppressedByPortConfig() { |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 353 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 354 | // add device in stub DeviceService without suppression configured |
| 355 | deviceService.putDevice(device(DID3)); |
| 356 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID3)); |
| 357 | |
| 358 | // suppressed port added to non-suppressed device |
| 359 | final long portno3 = 3L; |
| 360 | final Port port3 = port(DID3, portno3, true, |
| 361 | DefaultAnnotations.builder() |
Jonathan Hart | b35540a | 2015-11-17 09:30:56 -0800 | [diff] [blame] | 362 | .set(LldpLinkProvider.NO_LLDP, "true") |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 363 | .build()); |
| 364 | deviceService.putPorts(DID3, port3); |
| 365 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID3, port3)); |
| 366 | |
| 367 | // discovery helper should be there turned on |
| 368 | assertFalse("Discoverer is expected to start", provider.discoverers.get(DID3).isStopped()); |
| 369 | assertFalse("Discoverer should not contain the port there", |
| 370 | provider.discoverers.get(DID3).containsPort(portno3)); |
| 371 | } |
| 372 | |
| 373 | @Test |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 374 | public void portUnknown() { |
| 375 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 376 | // Note: DID3 hasn't been added to TestDeviceService, but only port is added |
| 377 | deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID3, port(DID3, 1, false))); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 378 | |
| 379 | |
alshabib | dfc7afb | 2014-10-21 20:13:27 -0700 | [diff] [blame] | 380 | assertNull("DeviceId exists", |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 381 | provider.discoverers.get(DID3)); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | @Test |
| 385 | public void unknownPktCtx() { |
| 386 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 387 | // Note: DID3 hasn't been added to TestDeviceService |
| 388 | PacketContext pktCtx = new TestPacketContext(device(DID3)); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 389 | |
| 390 | testProcessor.process(pktCtx); |
| 391 | assertFalse("Context should still be free", pktCtx.isHandled()); |
| 392 | } |
| 393 | |
| 394 | @Test |
| 395 | public void knownPktCtx() { |
| 396 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1)); |
| 397 | deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID2)); |
| 398 | PacketContext pktCtx = new TestPacketContext(deviceService.getDevice(DID2)); |
| 399 | |
| 400 | |
| 401 | testProcessor.process(pktCtx); |
| 402 | |
| 403 | assertTrue("Link not detected", detectedLink(DID1, DID2)); |
| 404 | |
| 405 | } |
| 406 | |
| 407 | |
| 408 | @After |
| 409 | public void tearDown() { |
| 410 | provider.deactivate(); |
Pavlin Radoslavov | d36a74b | 2015-01-09 11:59:07 -0800 | [diff] [blame] | 411 | provider.coreService = null; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 412 | provider.providerRegistry = null; |
| 413 | provider.deviceService = null; |
Marc De Leenheer | 8b3e80b | 2015-03-06 14:27:03 -0800 | [diff] [blame] | 414 | provider.packetService = null; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | private DeviceEvent deviceEvent(DeviceEvent.Type type, DeviceId did) { |
| 418 | return new DeviceEvent(type, deviceService.getDevice(did)); |
| 419 | |
| 420 | } |
| 421 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 422 | private DefaultDevice device(DeviceId did) { |
| 423 | return new DefaultDevice(ProviderId.NONE, did, Device.Type.SWITCH, |
| 424 | "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId()); |
| 425 | } |
| 426 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 427 | private DefaultDevice device(DeviceId did, Device.Type type) { |
| 428 | return new DefaultDevice(ProviderId.NONE, did, type, |
| 429 | "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId()); |
| 430 | } |
| 431 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 432 | private DefaultDevice device(DeviceId did, Annotations annotations) { |
| 433 | return new DefaultDevice(ProviderId.NONE, did, Device.Type.SWITCH, |
| 434 | "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId(), annotations); |
| 435 | } |
| 436 | |
Saurav Das | c313c40 | 2015-02-27 10:09:47 -0800 | [diff] [blame] | 437 | @SuppressWarnings(value = { "unused" }) |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 438 | private DeviceEvent portEvent(DeviceEvent.Type type, DeviceId did, PortNumber port) { |
| 439 | return new DeviceEvent(type, deviceService.getDevice(did), |
| 440 | deviceService.getPort(did, port)); |
| 441 | } |
| 442 | |
| 443 | private DeviceEvent portEvent(DeviceEvent.Type type, DeviceId did, Port port) { |
| 444 | return new DeviceEvent(type, deviceService.getDevice(did), port); |
| 445 | } |
| 446 | |
| 447 | private Port port(DeviceId did, long port, boolean enabled) { |
| 448 | return new DefaultPort(deviceService.getDevice(did), |
| 449 | PortNumber.portNumber(port), enabled); |
| 450 | } |
| 451 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 452 | private Port port(DeviceId did, long port, boolean enabled, Annotations annotations) { |
| 453 | return new DefaultPort(deviceService.getDevice(did), |
| 454 | PortNumber.portNumber(port), enabled, annotations); |
| 455 | } |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 456 | |
| 457 | private boolean vanishedDpid(DeviceId... dids) { |
| 458 | for (int i = 0; i < dids.length; i++) { |
| 459 | if (!providerService.vanishedDpid.contains(dids[i])) { |
| 460 | return false; |
| 461 | } |
| 462 | } |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | private boolean vanishedPort(Long... ports) { |
| 467 | for (int i = 0; i < ports.length; i++) { |
| 468 | if (!providerService.vanishedPort.contains(ports[i])) { |
| 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | return true; |
| 473 | } |
| 474 | |
| 475 | private boolean detectedLink(DeviceId src, DeviceId dst) { |
| 476 | for (DeviceId key : providerService.discoveredLinks.keySet()) { |
| 477 | if (key.equals(src)) { |
| 478 | return providerService.discoveredLinks.get(src).equals(dst); |
| 479 | } |
| 480 | } |
| 481 | return false; |
| 482 | } |
| 483 | |
| 484 | |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 485 | @Test |
| 486 | public void addDeviceIdRule() { |
| 487 | DeviceId deviceId1 = DeviceId.deviceId("of:0000000000000001"); |
| 488 | DeviceId deviceId2 = DeviceId.deviceId("of:0000000000000002"); |
| 489 | Set<DeviceId> deviceIds = new HashSet<>(); |
| 490 | |
| 491 | deviceIds.add(deviceId1); |
| 492 | cfg.deviceIds(deviceIds); |
| 493 | |
| 494 | configEvent(NetworkConfigEvent.Type.CONFIG_ADDED); |
| 495 | |
| 496 | assertTrue(provider.rules().getSuppressedDevice().contains(deviceId1)); |
| 497 | assertFalse(provider.rules().getSuppressedDevice().contains(deviceId2)); |
| 498 | } |
| 499 | |
| 500 | @Test |
| 501 | public void updateDeviceIdRule() { |
| 502 | DeviceId deviceId1 = DeviceId.deviceId("of:0000000000000001"); |
| 503 | DeviceId deviceId2 = DeviceId.deviceId("of:0000000000000002"); |
| 504 | Set<DeviceId> deviceIds = new HashSet<>(); |
| 505 | |
| 506 | deviceIds.add(deviceId1); |
| 507 | cfg.deviceIds(deviceIds); |
| 508 | |
| 509 | configEvent(NetworkConfigEvent.Type.CONFIG_ADDED); |
| 510 | |
| 511 | deviceIds.add(deviceId2); |
| 512 | cfg.deviceIds(deviceIds); |
| 513 | |
| 514 | configEvent(NetworkConfigEvent.Type.CONFIG_UPDATED); |
| 515 | |
| 516 | assertTrue(provider.rules().getSuppressedDevice().contains(deviceId1)); |
| 517 | assertTrue(provider.rules().getSuppressedDevice().contains(deviceId2)); |
| 518 | } |
| 519 | |
| 520 | @Test |
| 521 | public void addDeviceTypeRule() { |
| 522 | Device.Type deviceType1 = Device.Type.ROADM; |
| 523 | Device.Type deviceType2 = Device.Type.SWITCH; |
| 524 | |
| 525 | Set<Device.Type> deviceTypes = new HashSet<>(); |
| 526 | deviceTypes.add(deviceType1); |
| 527 | |
| 528 | cfg.deviceTypes(deviceTypes); |
| 529 | |
| 530 | configEvent(NetworkConfigEvent.Type.CONFIG_ADDED); |
| 531 | |
| 532 | assertTrue(provider.rules().getSuppressedDeviceType().contains(deviceType1)); |
| 533 | assertFalse(provider.rules().getSuppressedDeviceType().contains(deviceType2)); |
| 534 | } |
| 535 | |
| 536 | @Test |
| 537 | public void updateDeviceTypeRule() { |
| 538 | Device.Type deviceType1 = Device.Type.ROADM; |
| 539 | Device.Type deviceType2 = Device.Type.SWITCH; |
| 540 | Set<Device.Type> deviceTypes = new HashSet<>(); |
| 541 | |
| 542 | deviceTypes.add(deviceType1); |
| 543 | cfg.deviceTypes(deviceTypes); |
| 544 | |
| 545 | configEvent(NetworkConfigEvent.Type.CONFIG_ADDED); |
| 546 | |
| 547 | deviceTypes.add(deviceType2); |
| 548 | cfg.deviceTypes(deviceTypes); |
| 549 | |
| 550 | configEvent(NetworkConfigEvent.Type.CONFIG_UPDATED); |
| 551 | |
| 552 | assertTrue(provider.rules().getSuppressedDeviceType().contains(deviceType1)); |
| 553 | assertTrue(provider.rules().getSuppressedDeviceType().contains(deviceType2)); |
| 554 | } |
| 555 | |
| 556 | @Test |
| 557 | public void addAnnotationRule() { |
| 558 | final String key1 = "key1", key2 = "key2"; |
| 559 | final String value1 = "value1"; |
| 560 | |
| 561 | Map<String, String> annotation = new HashMap<>(); |
| 562 | annotation.put(key1, value1); |
| 563 | |
| 564 | cfg.annotation(annotation); |
| 565 | |
| 566 | configEvent(NetworkConfigEvent.Type.CONFIG_ADDED); |
| 567 | |
| 568 | assertTrue(provider.rules().getSuppressedAnnotation().containsKey(key1)); |
| 569 | assertEquals(value1, provider.rules().getSuppressedAnnotation().get(key1)); |
| 570 | assertFalse(provider.rules().getSuppressedAnnotation().containsKey(key2)); |
| 571 | } |
| 572 | |
| 573 | @Test |
| 574 | public void updateAnnotationRule() { |
| 575 | final String key1 = "key1", key2 = "key2"; |
| 576 | final String value1 = "value1", value2 = "value2"; |
| 577 | Map<String, String> annotation = new HashMap<>(); |
| 578 | |
| 579 | annotation.put(key1, value1); |
| 580 | cfg.annotation(annotation); |
| 581 | |
| 582 | configEvent(NetworkConfigEvent.Type.CONFIG_ADDED); |
| 583 | |
| 584 | assertTrue(provider.rules().getSuppressedAnnotation().containsKey(key1)); |
| 585 | assertEquals(value1, provider.rules().getSuppressedAnnotation().get(key1)); |
| 586 | assertFalse(provider.rules().getSuppressedAnnotation().containsKey(key2)); |
| 587 | |
| 588 | annotation.put(key2, value2); |
| 589 | cfg.annotation(annotation); |
| 590 | |
| 591 | configEvent(NetworkConfigEvent.Type.CONFIG_UPDATED); |
| 592 | |
| 593 | assertTrue(provider.rules().getSuppressedAnnotation().containsKey(key1)); |
| 594 | assertEquals(value1, provider.rules().getSuppressedAnnotation().get(key1)); |
| 595 | assertTrue(provider.rules().getSuppressedAnnotation().containsKey(key2)); |
| 596 | assertEquals(value2, provider.rules().getSuppressedAnnotation().get(key2)); |
| 597 | } |
| 598 | |
| 599 | private void configEvent(NetworkConfigEvent.Type evType) { |
| 600 | configListener.event(new NetworkConfigEvent(evType, |
| 601 | appId, |
| 602 | SuppressionConfig.class)); |
| 603 | } |
| 604 | |
| 605 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 606 | private class TestLinkRegistry implements LinkProviderRegistry { |
| 607 | |
| 608 | @Override |
| 609 | public LinkProviderService register(LinkProvider provider) { |
| 610 | providerService = new TestLinkProviderService(provider); |
| 611 | return providerService; |
| 612 | } |
| 613 | |
| 614 | @Override |
| 615 | public void unregister(LinkProvider provider) { |
| 616 | } |
| 617 | |
| 618 | @Override |
| 619 | public Set<ProviderId> getProviders() { |
| 620 | return null; |
| 621 | } |
| 622 | |
| 623 | } |
| 624 | |
| 625 | private class TestLinkProviderService |
| 626 | extends AbstractProviderService<LinkProvider> |
| 627 | implements LinkProviderService { |
| 628 | |
| 629 | List<DeviceId> vanishedDpid = Lists.newLinkedList(); |
| 630 | List<Long> vanishedPort = Lists.newLinkedList(); |
| 631 | Map<DeviceId, DeviceId> discoveredLinks = Maps.newHashMap(); |
| 632 | |
| 633 | protected TestLinkProviderService(LinkProvider provider) { |
| 634 | super(provider); |
| 635 | } |
| 636 | |
| 637 | @Override |
| 638 | public void linkDetected(LinkDescription linkDescription) { |
| 639 | DeviceId sDid = linkDescription.src().deviceId(); |
| 640 | DeviceId dDid = linkDescription.dst().deviceId(); |
| 641 | discoveredLinks.put(sDid, dDid); |
| 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public void linkVanished(LinkDescription linkDescription) { |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | @Override |
| 649 | public void linksVanished(ConnectPoint connectPoint) { |
| 650 | vanishedPort.add(connectPoint.port().toLong()); |
| 651 | |
| 652 | } |
| 653 | |
| 654 | @Override |
| 655 | public void linksVanished(DeviceId deviceId) { |
| 656 | vanishedDpid.add(deviceId); |
| 657 | } |
| 658 | |
| 659 | |
| 660 | } |
| 661 | |
| 662 | |
| 663 | |
| 664 | private class TestPacketContext implements PacketContext { |
| 665 | |
| 666 | protected Device device; |
| 667 | protected boolean blocked = false; |
| 668 | |
| 669 | public TestPacketContext(Device dev) { |
| 670 | device = dev; |
| 671 | } |
| 672 | |
| 673 | @Override |
| 674 | public long time() { |
| 675 | return 0; |
| 676 | } |
| 677 | |
| 678 | @Override |
| 679 | public InboundPacket inPacket() { |
| 680 | ONOSLLDP lldp = new ONOSLLDP(); |
| 681 | lldp.setChassisId(device.chassisId()); |
| 682 | lldp.setPortId((int) pd1.number().toLong()); |
| 683 | lldp.setDevice(deviceService.getDevice(DID1).id().toString()); |
| 684 | |
| 685 | |
| 686 | Ethernet ethPacket = new Ethernet(); |
| 687 | ethPacket.setEtherType(Ethernet.TYPE_LLDP); |
| 688 | ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA); |
| 689 | ethPacket.setPayload(lldp); |
| 690 | ethPacket.setPad(true); |
| 691 | |
| 692 | |
| 693 | |
| 694 | ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11"); |
| 695 | |
| 696 | ConnectPoint cp = new ConnectPoint(device.id(), pd3.number()); |
| 697 | |
| 698 | return new DefaultInboundPacket(cp, ethPacket, |
| 699 | ByteBuffer.wrap(ethPacket.serialize())); |
| 700 | |
| 701 | } |
| 702 | |
| 703 | @Override |
| 704 | public OutboundPacket outPacket() { |
| 705 | return null; |
| 706 | } |
| 707 | |
| 708 | @Override |
| 709 | public TrafficTreatment.Builder treatmentBuilder() { |
| 710 | return null; |
| 711 | } |
| 712 | |
| 713 | @Override |
| 714 | public void send() { |
| 715 | |
| 716 | } |
| 717 | |
| 718 | @Override |
| 719 | public boolean block() { |
| 720 | blocked = true; |
| 721 | return blocked; |
| 722 | } |
| 723 | |
| 724 | @Override |
| 725 | public boolean isHandled() { |
| 726 | return blocked; |
| 727 | } |
| 728 | |
| 729 | } |
| 730 | |
Thomas Vachuska | 27bee09 | 2015-06-23 19:03:10 -0700 | [diff] [blame] | 731 | private class TestPacketService extends PacketServiceAdapter { |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 732 | @Override |
| 733 | public void addProcessor(PacketProcessor processor, int priority) { |
| 734 | testProcessor = processor; |
| 735 | } |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 736 | } |
| 737 | |
Yuta HIGUCHI | f1f2ac0 | 2014-11-26 14:02:22 -0800 | [diff] [blame] | 738 | private class TestDeviceService extends DeviceServiceAdapter { |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 739 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 740 | private final Map<DeviceId, Device> devices = new HashMap<>(); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 741 | private final ArrayListMultimap<DeviceId, Port> ports = |
| 742 | ArrayListMultimap.create(); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 743 | public TestDeviceService() { |
| 744 | Device d1 = new DefaultDevice(ProviderId.NONE, DID1, Device.Type.SWITCH, |
| 745 | "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId()); |
| 746 | Device d2 = new DefaultDevice(ProviderId.NONE, DID2, Device.Type.SWITCH, |
| 747 | "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId()); |
| 748 | devices.put(DID1, d1); |
| 749 | devices.put(DID2, d2); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 750 | pd1 = new DefaultPort(d1, PortNumber.portNumber(1), true); |
| 751 | pd2 = new DefaultPort(d1, PortNumber.portNumber(2), true); |
| 752 | pd3 = new DefaultPort(d2, PortNumber.portNumber(1), true); |
| 753 | pd4 = new DefaultPort(d2, PortNumber.portNumber(2), true); |
| 754 | |
| 755 | ports.putAll(DID1, Lists.newArrayList(pd1, pd2)); |
| 756 | ports.putAll(DID2, Lists.newArrayList(pd3, pd4)); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 757 | } |
| 758 | |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 759 | private void putDevice(Device device) { |
| 760 | DeviceId deviceId = device.id(); |
| 761 | devices.put(deviceId, device); |
| 762 | } |
| 763 | |
| 764 | private void putPorts(DeviceId did, Port...ports) { |
| 765 | this.ports.putAll(did, Lists.newArrayList(ports)); |
| 766 | } |
| 767 | |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 768 | @Override |
| 769 | public int getDeviceCount() { |
| 770 | return devices.values().size(); |
| 771 | } |
| 772 | |
| 773 | @Override |
| 774 | public Iterable<Device> getDevices() { |
HIGUCHI Yuta | 9a9edf8 | 2015-10-21 11:23:20 -0700 | [diff] [blame] | 775 | return ImmutableList.copyOf(devices.values()); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | @Override |
| 779 | public Device getDevice(DeviceId deviceId) { |
| 780 | return devices.get(deviceId); |
| 781 | } |
| 782 | |
| 783 | @Override |
| 784 | public MastershipRole getRole(DeviceId deviceId) { |
| 785 | return MastershipRole.MASTER; |
| 786 | } |
| 787 | |
| 788 | @Override |
| 789 | public List<Port> getPorts(DeviceId deviceId) { |
| 790 | return ports.get(deviceId); |
| 791 | } |
| 792 | |
| 793 | @Override |
| 794 | public Port getPort(DeviceId deviceId, PortNumber portNumber) { |
| 795 | for (Port p : ports.get(deviceId)) { |
| 796 | if (p.number().equals(portNumber)) { |
| 797 | return p; |
| 798 | } |
| 799 | } |
| 800 | return null; |
| 801 | } |
| 802 | |
| 803 | @Override |
| 804 | public boolean isAvailable(DeviceId deviceId) { |
| 805 | return true; |
| 806 | } |
| 807 | |
| 808 | @Override |
| 809 | public void addListener(DeviceListener listener) { |
| 810 | deviceListener = listener; |
| 811 | |
| 812 | } |
| 813 | |
| 814 | @Override |
| 815 | public void removeListener(DeviceListener listener) { |
| 816 | |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | private final class TestMasterShipService implements MastershipService { |
| 821 | |
| 822 | @Override |
| 823 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 824 | return MastershipRole.MASTER; |
| 825 | } |
| 826 | |
| 827 | @Override |
Madan Jampani | de003d9 | 2015-05-11 17:14:20 -0700 | [diff] [blame] | 828 | public CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId) { |
| 829 | return CompletableFuture.completedFuture(null); |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | @Override |
Madan Jampani | c6e574f | 2015-05-29 13:41:52 -0700 | [diff] [blame] | 833 | public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) { |
| 834 | return null; |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | @Override |
| 838 | public NodeId getMasterFor(DeviceId deviceId) { |
| 839 | return null; |
| 840 | } |
| 841 | |
| 842 | @Override |
| 843 | public Set<DeviceId> getDevicesOf(NodeId nodeId) { |
| 844 | return null; |
| 845 | } |
| 846 | |
| 847 | @Override |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 848 | public void addListener(MastershipListener listener) { |
| 849 | |
| 850 | } |
| 851 | |
| 852 | @Override |
| 853 | public void removeListener(MastershipListener listener) { |
| 854 | |
| 855 | } |
Yuta HIGUCHI | 376d750 | 2014-10-19 15:57:52 -0700 | [diff] [blame] | 856 | |
| 857 | @Override |
Ayaka Koshibe | abedb09 | 2014-10-20 17:01:31 -0700 | [diff] [blame] | 858 | public RoleInfo getNodesFor(DeviceId deviceId) { |
| 859 | return new RoleInfo(new NodeId("foo"), Collections.<NodeId>emptyList()); |
Yuta HIGUCHI | 376d750 | 2014-10-19 15:57:52 -0700 | [diff] [blame] | 860 | } |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | |
Thomas Vachuska | e4ebac9 | 2015-09-10 11:39:05 -0700 | [diff] [blame] | 864 | private class TestLinkService extends LinkServiceAdapter { |
| 865 | } |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame^] | 866 | |
| 867 | private final class TestNetworkConfigRegistry |
| 868 | extends NetworkConfigRegistryAdapter { |
| 869 | @Override |
| 870 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 871 | ConfigApplyDelegate delegate = config -> { }; |
| 872 | ObjectMapper mapper = new ObjectMapper(); |
| 873 | return (C) cfg; |
| 874 | } |
| 875 | |
| 876 | @Override |
| 877 | public void addListener(NetworkConfigListener listener) { |
| 878 | configListener = listener; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | private final class TestSuppressionConfig extends SuppressionConfig { |
| 883 | private Set<DeviceId> deviceIds = new HashSet<>(DEFAULT_RULES.getSuppressedDevice()); |
| 884 | private Set<Device.Type> deviceTypes = new HashSet<>(DEFAULT_RULES.getSuppressedDeviceType()); |
| 885 | private Map<String, String> annotation = new HashMap<>(DEFAULT_RULES.getSuppressedAnnotation()); |
| 886 | |
| 887 | @Override |
| 888 | public Set<DeviceId> deviceIds() { |
| 889 | return ImmutableSet.copyOf(deviceIds); |
| 890 | } |
| 891 | |
| 892 | @Override |
| 893 | public SuppressionConfig deviceIds(Set<DeviceId> deviceIds) { |
| 894 | this.deviceIds = ImmutableSet.copyOf(deviceIds); |
| 895 | return this; |
| 896 | } |
| 897 | |
| 898 | @Override |
| 899 | public Set<Device.Type> deviceTypes() { |
| 900 | return ImmutableSet.copyOf(deviceTypes); |
| 901 | } |
| 902 | |
| 903 | @Override |
| 904 | public SuppressionConfig deviceTypes(Set<Device.Type> deviceTypes) { |
| 905 | this.deviceTypes = ImmutableSet.copyOf(deviceTypes); |
| 906 | return this; |
| 907 | } |
| 908 | |
| 909 | @Override |
| 910 | public Map<String, String> annotation() { |
| 911 | return ImmutableMap.copyOf(annotation); |
| 912 | } |
| 913 | |
| 914 | @Override |
| 915 | public SuppressionConfig annotation(Map<String, String> annotation) { |
| 916 | this.annotation = ImmutableMap.copyOf(annotation); |
| 917 | return this; |
| 918 | } |
| 919 | } |
alshabib | 0ed6a20 | 2014-10-19 12:42:57 -0700 | [diff] [blame] | 920 | } |