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