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