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