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