blob: ecf60c1df481002bf7281bae051aa4b220c10d4b [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.provider.lldp.impl;
alshabib0ed6a202014-10-19 12:42:57 -070017
Naoki Shiota399a0b32015-11-15 20:36:13 -060018import com.fasterxml.jackson.databind.ObjectMapper;
Thomas Vachuska27bee092015-06-23 19:03:10 -070019import com.google.common.collect.ArrayListMultimap;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070020import com.google.common.collect.ImmutableList;
Naoki Shiota399a0b32015-11-15 20:36:13 -060021import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.ImmutableSet;
Thomas Vachuska27bee092015-06-23 19:03:10 -070023import com.google.common.collect.Lists;
24import com.google.common.collect.Maps;
alshabib0ed6a202014-10-19 12:42:57 -070025import org.junit.After;
26import org.junit.Before;
27import org.junit.Test;
Jonathan Hart3cfce8e2015-01-14 16:43:27 -080028import org.onlab.packet.ChassisId;
29import org.onlab.packet.Ethernet;
30import org.onlab.packet.ONOSLLDP;
Thomas Vachuska6519e6f2015-03-11 02:29:31 -070031import org.onosproject.cfg.ComponentConfigAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.cluster.NodeId;
33import org.onosproject.cluster.RoleInfo;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -080034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
36import org.onosproject.core.DefaultApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080037import org.onosproject.mastership.MastershipListener;
38import org.onosproject.mastership.MastershipService;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070039import org.onosproject.net.Annotations;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.ConnectPoint;
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070041import org.onosproject.net.DefaultAnnotations;
Brian O'Connorabafb502014-12-02 22:26:20 -080042import org.onosproject.net.DefaultDevice;
43import org.onosproject.net.DefaultPort;
44import org.onosproject.net.Device;
45import org.onosproject.net.DeviceId;
46import org.onosproject.net.MastershipRole;
47import org.onosproject.net.Port;
48import org.onosproject.net.PortNumber;
Naoki Shiota399a0b32015-11-15 20:36:13 -060049import org.onosproject.net.config.Config;
50import org.onosproject.net.config.ConfigApplyDelegate;
51import org.onosproject.net.config.NetworkConfigEvent;
52import org.onosproject.net.config.NetworkConfigListener;
53import org.onosproject.net.config.NetworkConfigRegistryAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080054import org.onosproject.net.device.DeviceEvent;
55import org.onosproject.net.device.DeviceListener;
56import org.onosproject.net.device.DeviceServiceAdapter;
57import org.onosproject.net.flow.TrafficTreatment;
58import org.onosproject.net.link.LinkDescription;
59import org.onosproject.net.link.LinkProvider;
60import org.onosproject.net.link.LinkProviderRegistry;
61import org.onosproject.net.link.LinkProviderService;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -070062import org.onosproject.net.link.LinkServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080063import org.onosproject.net.packet.DefaultInboundPacket;
64import org.onosproject.net.packet.InboundPacket;
65import org.onosproject.net.packet.OutboundPacket;
66import org.onosproject.net.packet.PacketContext;
67import org.onosproject.net.packet.PacketProcessor;
Thomas Vachuska27bee092015-06-23 19:03:10 -070068import org.onosproject.net.packet.PacketServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080069import org.onosproject.net.provider.AbstractProviderService;
70import org.onosproject.net.provider.ProviderId;
alshabib0ed6a202014-10-19 12:42:57 -070071
Thomas Vachuska27bee092015-06-23 19:03:10 -070072import java.nio.ByteBuffer;
Thomas Vachuska27bee092015-06-23 19:03:10 -070073import java.util.HashMap;
Naoki Shiota399a0b32015-11-15 20:36:13 -060074import java.util.HashSet;
Thomas Vachuska27bee092015-06-23 19:03:10 -070075import java.util.List;
76import java.util.Map;
77import java.util.Set;
Naoki Shiota399a0b32015-11-15 20:36:13 -060078import java.util.Collections;
Thomas Vachuska27bee092015-06-23 19:03:10 -070079import java.util.concurrent.CompletableFuture;
80
Naoki Shiota399a0b32015-11-15 20:36:13 -060081import static org.easymock.EasyMock.createMock;
82import static org.easymock.EasyMock.expect;
83import static org.easymock.EasyMock.replay;
84
85import static org.junit.Assert.assertNull;
86import static org.junit.Assert.assertNotNull;
87import static org.junit.Assert.assertEquals;
88import static org.junit.Assert.assertTrue;
89import static org.onosproject.provider.lldp.impl.LldpLinkProvider.DEFAULT_RULES;
90import static org.junit.Assert.assertFalse;
91
alshabib0ed6a202014-10-19 12:42:57 -070092
Jonathan Hartb35540a2015-11-17 09:30:56 -080093public class LldpLinkProviderTest {
alshabib0ed6a202014-10-19 12:42:57 -070094
95 private static final DeviceId DID1 = DeviceId.deviceId("of:0000000000000001");
96 private static final DeviceId DID2 = DeviceId.deviceId("of:0000000000000002");
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -070097 private static final DeviceId DID3 = DeviceId.deviceId("of:0000000000000003");
alshabib0ed6a202014-10-19 12:42:57 -070098
99 private static Port pd1;
100 private static Port pd2;
101 private static Port pd3;
102 private static Port pd4;
103
Jonathan Hartb35540a2015-11-17 09:30:56 -0800104 private final LldpLinkProvider provider = new LldpLinkProvider();
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700105 private final TestLinkRegistry linkRegistry = new TestLinkRegistry();
106 private final TestLinkService linkService = new TestLinkService();
alshabib0ed6a202014-10-19 12:42:57 -0700107 private final TestPacketService packetService = new TestPacketService();
108 private final TestDeviceService deviceService = new TestDeviceService();
109 private final TestMasterShipService masterService = new TestMasterShipService();
Naoki Shiota399a0b32015-11-15 20:36:13 -0600110 private final TestNetworkConfigRegistry configRegistry = new TestNetworkConfigRegistry();
alshabib0ed6a202014-10-19 12:42:57 -0700111
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800112 private CoreService coreService;
alshabib0ed6a202014-10-19 12:42:57 -0700113 private TestLinkProviderService providerService;
114
115 private PacketProcessor testProcessor;
116 private DeviceListener deviceListener;
Naoki Shiota399a0b32015-11-15 20:36:13 -0600117 private NetworkConfigListener configListener;
alshabib0ed6a202014-10-19 12:42:57 -0700118
Thomas Vachuska02aeb032015-01-06 22:36:30 -0800119 private ApplicationId appId =
120 new DefaultApplicationId(100, "org.onosproject.provider.lldp");
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800121
Naoki Shiota399a0b32015-11-15 20:36:13 -0600122 private TestSuppressionConfig cfg;
123
alshabib0ed6a202014-10-19 12:42:57 -0700124 @Before
125 public void setUp() {
Naoki Shiota399a0b32015-11-15 20:36:13 -0600126 cfg = new TestSuppressionConfig();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800127 coreService = createMock(CoreService.class);
128 expect(coreService.registerApplication(appId.name()))
129 .andReturn(appId).anyTimes();
130 replay(coreService);
131
Thomas Vachuska6519e6f2015-03-11 02:29:31 -0700132 provider.cfgService = new ComponentConfigAdapter();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800133 provider.coreService = coreService;
Naoki Shiota399a0b32015-11-15 20:36:13 -0600134 provider.cfgRegistry = configRegistry;
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800135
alshabib0ed6a202014-10-19 12:42:57 -0700136 provider.deviceService = deviceService;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700137 provider.linkService = linkService;
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800138 provider.packetService = packetService;
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700139 provider.providerRegistry = linkRegistry;
alshabib0ed6a202014-10-19 12:42:57 -0700140 provider.masterService = masterService;
141
Saurav Dasc313c402015-02-27 10:09:47 -0800142 provider.activate(null);
alshabib0ed6a202014-10-19 12:42:57 -0700143 }
144
145 @Test
146 public void basics() {
147 assertNotNull("registration expected", providerService);
148 assertEquals("incorrect provider", provider, providerService.provider());
149 }
150
151 @Test
152 public void switchAdd() {
153 DeviceEvent de = deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1);
154 deviceListener.event(de);
155
156 assertFalse("Device not added", provider.discoverers.isEmpty());
157 }
158
159 @Test
160 public void switchRemove() {
161 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1));
162 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_REMOVED, DID1));
163
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700164 final LinkDiscovery linkDiscovery = provider.discoverers.get(DID1);
165 if (linkDiscovery != null) {
166 // If LinkDiscovery helper is there after DEVICE_REMOVED,
167 // it should be stopped
168 assertTrue("Discoverer is not stopped", linkDiscovery.isStopped());
169 }
alshabib0ed6a202014-10-19 12:42:57 -0700170 assertTrue("Device is not gone.", vanishedDpid(DID1));
171 }
172
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700173 /**
174 * Checks that links on a reconfigured switch are properly removed.
175 */
176 @Test
177 public void switchSuppressed() {
Naoki Shiota399a0b32015-11-15 20:36:13 -0600178
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700179 // add device to stub DeviceService
180 deviceService.putDevice(device(DID3));
181 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID3));
182
183 assertFalse("Device not added", provider.discoverers.isEmpty());
184
185 // update device in stub DeviceService with suppression config
186 deviceService.putDevice(device(DID3, DefaultAnnotations.builder()
Jonathan Hartb35540a2015-11-17 09:30:56 -0800187 .set(LldpLinkProvider.NO_LLDP, "true")
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700188 .build()));
189 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_UPDATED, DID3));
190
Naoki Shiota399a0b32015-11-15 20:36:13 -0600191 // discovery on device is expected to be gone or stopped
192 LinkDiscovery linkDiscovery = provider.discoverers.get(DID3);
193 if (linkDiscovery != null) {
194 assertTrue("Discovery expected to be stopped", linkDiscovery.isStopped());
195 }
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700196 }
197
alshabib0ed6a202014-10-19 12:42:57 -0700198 @Test
199 public void portUp() {
200 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1));
201 deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 3, true)));
202
203 assertTrue("Port not added to discoverer",
Sho SHIMIZUc14b2a22015-05-05 18:15:13 -0700204 provider.discoverers.get(DID1).containsPort(3L));
alshabib0ed6a202014-10-19 12:42:57 -0700205 }
206
207 @Test
208 public void portDown() {
209
210 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1));
211 deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 1, false)));
212
alshabib0ed6a202014-10-19 12:42:57 -0700213 assertFalse("Port added to discoverer",
Sho SHIMIZUc14b2a22015-05-05 18:15:13 -0700214 provider.discoverers.get(DID1).containsPort(1L));
215 assertTrue("Port is not gone.", vanishedPort(1L));
alshabib0ed6a202014-10-19 12:42:57 -0700216 }
217
218 @Test
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700219 public void portRemoved() {
220 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1));
221 deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, 3, true)));
222 deviceListener.event(portEvent(DeviceEvent.Type.PORT_REMOVED, DID1, port(DID1, 3, true)));
223
224 assertTrue("Port is not gone.", vanishedPort(3L));
225 assertFalse("Port was not removed from discoverer",
226 provider.discoverers.get(DID1).containsPort(3L));
227 }
228
229 /**
230 * Checks that discovery on reconfigured switch are properly restarted.
231 */
232 @Test
Naoki Shiota399a0b32015-11-15 20:36:13 -0600233 public void portSuppressedByDeviceAnnotationConfig() {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700234
235 /// When Device is configured with suppression:ON, Port also is same
236
237 // add device in stub DeviceService with suppression configured
238 deviceService.putDevice(device(DID3, DefaultAnnotations.builder()
Jonathan Hartb35540a2015-11-17 09:30:56 -0800239 .set(LldpLinkProvider.NO_LLDP, "true")
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700240 .build()));
241 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID3));
242
243 // non-suppressed port added to suppressed device
244 final long portno3 = 3L;
245 deviceService.putPorts(DID3, port(DID3, portno3, true));
246 deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID3, port(DID3, portno3, true)));
247
248 // discovery on device is expected to be stopped
249 LinkDiscovery linkDiscovery = provider.discoverers.get(DID3);
250 if (linkDiscovery != null) {
251 assertTrue("Discovery expected to be stopped", linkDiscovery.isStopped());
252 }
253
254 /// When Device is reconfigured without suppression:OFF,
255 /// Port should be included for discovery
256
257 // update device in stub DeviceService without suppression configured
258 deviceService.putDevice(device(DID3));
259 // update the Port in stub DeviceService. (Port has reference to Device)
260 deviceService.putPorts(DID3, port(DID3, portno3, true));
261 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_UPDATED, DID3));
262
263 // discovery should come back on
264 assertFalse("Discoverer is expected to start", provider.discoverers.get(DID3).isStopped());
265 assertTrue("Discoverer should contain the port there", provider.discoverers.get(DID3).containsPort(portno3));
266 }
267
268 /**
Naoki Shiota399a0b32015-11-15 20:36:13 -0600269 * Checks that discovery on reconfigured switch are properly restarted.
270 */
271 @Test
272 public void portSuppressedByDeviceIdConfig() {
273
274 /// When Device is configured without suppression:OFF,
275 /// Port should be included for discovery
276
277 // add device in stub DeviceService without suppression configured
278 deviceService.putDevice(device(DID3));
279 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID3));
280
281 // non-suppressed port added to suppressed device
282 final long portno3 = 3L;
283 deviceService.putPorts(DID3, port(DID3, portno3, true));
284 deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID3, port(DID3, portno3, true)));
285
286 // discovery should succeed
287 assertFalse("Discoverer is expected to start", provider.discoverers.get(DID3).isStopped());
288 assertTrue("Discoverer should contain the port there", provider.discoverers.get(DID3).containsPort(portno3));
289
290 // add suppression rule for "deviceId: "of:0000000000000003""
291 provider.updateRules(new SuppressionRules(ImmutableSet.of(DID3),
292 ImmutableSet.of(),
293 ImmutableMap.of()));
294
295 /// When Device is reconfigured with suppression:ON, Port also is same
296
297 // update device in stub DeviceService with suppression configured
298 deviceService.putDevice(device(DID3));
299 // update the Port in stub DeviceService. (Port has reference to Device)
300 deviceService.putPorts(DID3, port(DID3, portno3, true));
301 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_UPDATED, DID3));
302
303 // discovery on device is expected to be stopped
304 LinkDiscovery linkDiscovery = provider.discoverers.get(DID3);
305 if (linkDiscovery != null) {
306 assertTrue("Discovery expected to be stopped", linkDiscovery.isStopped());
307 }
308 }
309
310 /**
311 * Checks that discovery on reconfigured switch are properly restarted.
312 */
313 @Test
314 public void portSuppressedByDeviceTypeConfig() {
315
316 /// When Device is configured without suppression:OFF,
317 /// Port should be included for discovery
318
319 // add device in stub DeviceService without suppression configured
320 deviceService.putDevice(device(DID1, Device.Type.SWITCH));
321 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1));
322
323 // non-suppressed port added to suppressed device
324 final long portno3 = 3L;
325 deviceService.putPorts(DID1, port(DID1, portno3, true));
326 deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID1, port(DID1, portno3, true)));
327
328 // add device in stub DeviceService with suppression configured
329 deviceService.putDevice(device(DID2, Device.Type.ROADM));
330 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID2));
331
332 // non-suppressed port added to suppressed device
333 final long portno4 = 4L;
334 deviceService.putPorts(DID2, port(DID2, portno4, true));
335 deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID2, port(DID2, portno4, true)));
336
337 // discovery should succeed for this device
338 assertFalse("Discoverer is expected to start", provider.discoverers.get(DID1).isStopped());
339 assertTrue("Discoverer should contain the port there", provider.discoverers.get(DID1).containsPort(portno3));
340
341 // discovery on device is expected to be stopped for this device
342 LinkDiscovery linkDiscovery = provider.discoverers.get(DID2);
343 if (linkDiscovery != null) {
344 assertTrue("Discovery expected to be stopped", linkDiscovery.isStopped());
345 }
346 }
347
348 /**
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700349 * Checks that discovery on reconfigured port are properly restarted.
350 */
351 @Test
352 public void portSuppressedByPortConfig() {
Naoki Shiota399a0b32015-11-15 20:36:13 -0600353
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700354 // add device in stub DeviceService without suppression configured
355 deviceService.putDevice(device(DID3));
356 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID3));
357
358 // suppressed port added to non-suppressed device
359 final long portno3 = 3L;
360 final Port port3 = port(DID3, portno3, true,
361 DefaultAnnotations.builder()
Jonathan Hartb35540a2015-11-17 09:30:56 -0800362 .set(LldpLinkProvider.NO_LLDP, "true")
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700363 .build());
364 deviceService.putPorts(DID3, port3);
365 deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID3, port3));
366
367 // discovery helper should be there turned on
368 assertFalse("Discoverer is expected to start", provider.discoverers.get(DID3).isStopped());
369 assertFalse("Discoverer should not contain the port there",
370 provider.discoverers.get(DID3).containsPort(portno3));
371 }
372
373 @Test
alshabib0ed6a202014-10-19 12:42:57 -0700374 public void portUnknown() {
375 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1));
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700376 // Note: DID3 hasn't been added to TestDeviceService, but only port is added
377 deviceListener.event(portEvent(DeviceEvent.Type.PORT_ADDED, DID3, port(DID3, 1, false)));
alshabib0ed6a202014-10-19 12:42:57 -0700378
379
alshabibdfc7afb2014-10-21 20:13:27 -0700380 assertNull("DeviceId exists",
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700381 provider.discoverers.get(DID3));
alshabib0ed6a202014-10-19 12:42:57 -0700382 }
383
384 @Test
385 public void unknownPktCtx() {
386
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700387 // Note: DID3 hasn't been added to TestDeviceService
388 PacketContext pktCtx = new TestPacketContext(device(DID3));
alshabib0ed6a202014-10-19 12:42:57 -0700389
390 testProcessor.process(pktCtx);
391 assertFalse("Context should still be free", pktCtx.isHandled());
392 }
393
394 @Test
395 public void knownPktCtx() {
396 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID1));
397 deviceListener.event(deviceEvent(DeviceEvent.Type.DEVICE_ADDED, DID2));
398 PacketContext pktCtx = new TestPacketContext(deviceService.getDevice(DID2));
399
400
401 testProcessor.process(pktCtx);
402
403 assertTrue("Link not detected", detectedLink(DID1, DID2));
404
405 }
406
407
408 @After
409 public void tearDown() {
410 provider.deactivate();
Pavlin Radoslavovd36a74b2015-01-09 11:59:07 -0800411 provider.coreService = null;
alshabib0ed6a202014-10-19 12:42:57 -0700412 provider.providerRegistry = null;
413 provider.deviceService = null;
Marc De Leenheer8b3e80b2015-03-06 14:27:03 -0800414 provider.packetService = null;
alshabib0ed6a202014-10-19 12:42:57 -0700415 }
416
417 private DeviceEvent deviceEvent(DeviceEvent.Type type, DeviceId did) {
418 return new DeviceEvent(type, deviceService.getDevice(did));
419
420 }
421
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700422 private DefaultDevice device(DeviceId did) {
423 return new DefaultDevice(ProviderId.NONE, did, Device.Type.SWITCH,
424 "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId());
425 }
426
Naoki Shiota399a0b32015-11-15 20:36:13 -0600427 private DefaultDevice device(DeviceId did, Device.Type type) {
428 return new DefaultDevice(ProviderId.NONE, did, type,
429 "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId());
430 }
431
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700432 private DefaultDevice device(DeviceId did, Annotations annotations) {
433 return new DefaultDevice(ProviderId.NONE, did, Device.Type.SWITCH,
434 "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId(), annotations);
435 }
436
Saurav Dasc313c402015-02-27 10:09:47 -0800437 @SuppressWarnings(value = { "unused" })
alshabib0ed6a202014-10-19 12:42:57 -0700438 private DeviceEvent portEvent(DeviceEvent.Type type, DeviceId did, PortNumber port) {
439 return new DeviceEvent(type, deviceService.getDevice(did),
440 deviceService.getPort(did, port));
441 }
442
443 private DeviceEvent portEvent(DeviceEvent.Type type, DeviceId did, Port port) {
444 return new DeviceEvent(type, deviceService.getDevice(did), port);
445 }
446
447 private Port port(DeviceId did, long port, boolean enabled) {
448 return new DefaultPort(deviceService.getDevice(did),
449 PortNumber.portNumber(port), enabled);
450 }
451
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700452 private Port port(DeviceId did, long port, boolean enabled, Annotations annotations) {
453 return new DefaultPort(deviceService.getDevice(did),
454 PortNumber.portNumber(port), enabled, annotations);
455 }
alshabib0ed6a202014-10-19 12:42:57 -0700456
457 private boolean vanishedDpid(DeviceId... dids) {
458 for (int i = 0; i < dids.length; i++) {
459 if (!providerService.vanishedDpid.contains(dids[i])) {
460 return false;
461 }
462 }
463 return true;
464 }
465
466 private boolean vanishedPort(Long... ports) {
467 for (int i = 0; i < ports.length; i++) {
468 if (!providerService.vanishedPort.contains(ports[i])) {
469 return false;
470 }
471 }
472 return true;
473 }
474
475 private boolean detectedLink(DeviceId src, DeviceId dst) {
476 for (DeviceId key : providerService.discoveredLinks.keySet()) {
477 if (key.equals(src)) {
478 return providerService.discoveredLinks.get(src).equals(dst);
479 }
480 }
481 return false;
482 }
483
484
Naoki Shiota399a0b32015-11-15 20:36:13 -0600485 @Test
486 public void addDeviceIdRule() {
487 DeviceId deviceId1 = DeviceId.deviceId("of:0000000000000001");
488 DeviceId deviceId2 = DeviceId.deviceId("of:0000000000000002");
489 Set<DeviceId> deviceIds = new HashSet<>();
490
491 deviceIds.add(deviceId1);
492 cfg.deviceIds(deviceIds);
493
494 configEvent(NetworkConfigEvent.Type.CONFIG_ADDED);
495
496 assertTrue(provider.rules().getSuppressedDevice().contains(deviceId1));
497 assertFalse(provider.rules().getSuppressedDevice().contains(deviceId2));
498 }
499
500 @Test
501 public void updateDeviceIdRule() {
502 DeviceId deviceId1 = DeviceId.deviceId("of:0000000000000001");
503 DeviceId deviceId2 = DeviceId.deviceId("of:0000000000000002");
504 Set<DeviceId> deviceIds = new HashSet<>();
505
506 deviceIds.add(deviceId1);
507 cfg.deviceIds(deviceIds);
508
509 configEvent(NetworkConfigEvent.Type.CONFIG_ADDED);
510
511 deviceIds.add(deviceId2);
512 cfg.deviceIds(deviceIds);
513
514 configEvent(NetworkConfigEvent.Type.CONFIG_UPDATED);
515
516 assertTrue(provider.rules().getSuppressedDevice().contains(deviceId1));
517 assertTrue(provider.rules().getSuppressedDevice().contains(deviceId2));
518 }
519
520 @Test
521 public void addDeviceTypeRule() {
522 Device.Type deviceType1 = Device.Type.ROADM;
523 Device.Type deviceType2 = Device.Type.SWITCH;
524
525 Set<Device.Type> deviceTypes = new HashSet<>();
526 deviceTypes.add(deviceType1);
527
528 cfg.deviceTypes(deviceTypes);
529
530 configEvent(NetworkConfigEvent.Type.CONFIG_ADDED);
531
532 assertTrue(provider.rules().getSuppressedDeviceType().contains(deviceType1));
533 assertFalse(provider.rules().getSuppressedDeviceType().contains(deviceType2));
534 }
535
536 @Test
537 public void updateDeviceTypeRule() {
538 Device.Type deviceType1 = Device.Type.ROADM;
539 Device.Type deviceType2 = Device.Type.SWITCH;
540 Set<Device.Type> deviceTypes = new HashSet<>();
541
542 deviceTypes.add(deviceType1);
543 cfg.deviceTypes(deviceTypes);
544
545 configEvent(NetworkConfigEvent.Type.CONFIG_ADDED);
546
547 deviceTypes.add(deviceType2);
548 cfg.deviceTypes(deviceTypes);
549
550 configEvent(NetworkConfigEvent.Type.CONFIG_UPDATED);
551
552 assertTrue(provider.rules().getSuppressedDeviceType().contains(deviceType1));
553 assertTrue(provider.rules().getSuppressedDeviceType().contains(deviceType2));
554 }
555
556 @Test
557 public void addAnnotationRule() {
558 final String key1 = "key1", key2 = "key2";
559 final String value1 = "value1";
560
561 Map<String, String> annotation = new HashMap<>();
562 annotation.put(key1, value1);
563
564 cfg.annotation(annotation);
565
566 configEvent(NetworkConfigEvent.Type.CONFIG_ADDED);
567
568 assertTrue(provider.rules().getSuppressedAnnotation().containsKey(key1));
569 assertEquals(value1, provider.rules().getSuppressedAnnotation().get(key1));
570 assertFalse(provider.rules().getSuppressedAnnotation().containsKey(key2));
571 }
572
573 @Test
574 public void updateAnnotationRule() {
575 final String key1 = "key1", key2 = "key2";
576 final String value1 = "value1", value2 = "value2";
577 Map<String, String> annotation = new HashMap<>();
578
579 annotation.put(key1, value1);
580 cfg.annotation(annotation);
581
582 configEvent(NetworkConfigEvent.Type.CONFIG_ADDED);
583
584 assertTrue(provider.rules().getSuppressedAnnotation().containsKey(key1));
585 assertEquals(value1, provider.rules().getSuppressedAnnotation().get(key1));
586 assertFalse(provider.rules().getSuppressedAnnotation().containsKey(key2));
587
588 annotation.put(key2, value2);
589 cfg.annotation(annotation);
590
591 configEvent(NetworkConfigEvent.Type.CONFIG_UPDATED);
592
593 assertTrue(provider.rules().getSuppressedAnnotation().containsKey(key1));
594 assertEquals(value1, provider.rules().getSuppressedAnnotation().get(key1));
595 assertTrue(provider.rules().getSuppressedAnnotation().containsKey(key2));
596 assertEquals(value2, provider.rules().getSuppressedAnnotation().get(key2));
597 }
598
599 private void configEvent(NetworkConfigEvent.Type evType) {
600 configListener.event(new NetworkConfigEvent(evType,
601 appId,
602 SuppressionConfig.class));
603 }
604
605
alshabib0ed6a202014-10-19 12:42:57 -0700606 private class TestLinkRegistry implements LinkProviderRegistry {
607
608 @Override
609 public LinkProviderService register(LinkProvider provider) {
610 providerService = new TestLinkProviderService(provider);
611 return providerService;
612 }
613
614 @Override
615 public void unregister(LinkProvider provider) {
616 }
617
618 @Override
619 public Set<ProviderId> getProviders() {
620 return null;
621 }
622
623 }
624
625 private class TestLinkProviderService
626 extends AbstractProviderService<LinkProvider>
627 implements LinkProviderService {
628
629 List<DeviceId> vanishedDpid = Lists.newLinkedList();
630 List<Long> vanishedPort = Lists.newLinkedList();
631 Map<DeviceId, DeviceId> discoveredLinks = Maps.newHashMap();
632
633 protected TestLinkProviderService(LinkProvider provider) {
634 super(provider);
635 }
636
637 @Override
638 public void linkDetected(LinkDescription linkDescription) {
639 DeviceId sDid = linkDescription.src().deviceId();
640 DeviceId dDid = linkDescription.dst().deviceId();
641 discoveredLinks.put(sDid, dDid);
642 }
643
644 @Override
645 public void linkVanished(LinkDescription linkDescription) {
alshabib0ed6a202014-10-19 12:42:57 -0700646 }
647
648 @Override
649 public void linksVanished(ConnectPoint connectPoint) {
650 vanishedPort.add(connectPoint.port().toLong());
651
652 }
653
654 @Override
655 public void linksVanished(DeviceId deviceId) {
656 vanishedDpid.add(deviceId);
657 }
658
659
660 }
661
662
663
664 private class TestPacketContext implements PacketContext {
665
666 protected Device device;
667 protected boolean blocked = false;
668
669 public TestPacketContext(Device dev) {
670 device = dev;
671 }
672
673 @Override
674 public long time() {
675 return 0;
676 }
677
678 @Override
679 public InboundPacket inPacket() {
680 ONOSLLDP lldp = new ONOSLLDP();
681 lldp.setChassisId(device.chassisId());
682 lldp.setPortId((int) pd1.number().toLong());
683 lldp.setDevice(deviceService.getDevice(DID1).id().toString());
684
685
686 Ethernet ethPacket = new Ethernet();
687 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
688 ethPacket.setDestinationMACAddress(ONOSLLDP.LLDP_NICIRA);
689 ethPacket.setPayload(lldp);
690 ethPacket.setPad(true);
691
692
693
694 ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");
695
696 ConnectPoint cp = new ConnectPoint(device.id(), pd3.number());
697
698 return new DefaultInboundPacket(cp, ethPacket,
699 ByteBuffer.wrap(ethPacket.serialize()));
700
701 }
702
703 @Override
704 public OutboundPacket outPacket() {
705 return null;
706 }
707
708 @Override
709 public TrafficTreatment.Builder treatmentBuilder() {
710 return null;
711 }
712
713 @Override
714 public void send() {
715
716 }
717
718 @Override
719 public boolean block() {
720 blocked = true;
721 return blocked;
722 }
723
724 @Override
725 public boolean isHandled() {
726 return blocked;
727 }
728
729 }
730
Thomas Vachuska27bee092015-06-23 19:03:10 -0700731 private class TestPacketService extends PacketServiceAdapter {
alshabib0ed6a202014-10-19 12:42:57 -0700732 @Override
733 public void addProcessor(PacketProcessor processor, int priority) {
734 testProcessor = processor;
735 }
alshabib0ed6a202014-10-19 12:42:57 -0700736 }
737
Yuta HIGUCHIf1f2ac02014-11-26 14:02:22 -0800738 private class TestDeviceService extends DeviceServiceAdapter {
alshabib0ed6a202014-10-19 12:42:57 -0700739
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700740 private final Map<DeviceId, Device> devices = new HashMap<>();
alshabib0ed6a202014-10-19 12:42:57 -0700741 private final ArrayListMultimap<DeviceId, Port> ports =
742 ArrayListMultimap.create();
alshabib0ed6a202014-10-19 12:42:57 -0700743 public TestDeviceService() {
744 Device d1 = new DefaultDevice(ProviderId.NONE, DID1, Device.Type.SWITCH,
745 "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId());
746 Device d2 = new DefaultDevice(ProviderId.NONE, DID2, Device.Type.SWITCH,
747 "TESTMF", "TESTHW", "TESTSW", "TESTSN", new ChassisId());
748 devices.put(DID1, d1);
749 devices.put(DID2, d2);
alshabib0ed6a202014-10-19 12:42:57 -0700750 pd1 = new DefaultPort(d1, PortNumber.portNumber(1), true);
751 pd2 = new DefaultPort(d1, PortNumber.portNumber(2), true);
752 pd3 = new DefaultPort(d2, PortNumber.portNumber(1), true);
753 pd4 = new DefaultPort(d2, PortNumber.portNumber(2), true);
754
755 ports.putAll(DID1, Lists.newArrayList(pd1, pd2));
756 ports.putAll(DID2, Lists.newArrayList(pd3, pd4));
alshabib0ed6a202014-10-19 12:42:57 -0700757 }
758
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700759 private void putDevice(Device device) {
760 DeviceId deviceId = device.id();
761 devices.put(deviceId, device);
762 }
763
764 private void putPorts(DeviceId did, Port...ports) {
765 this.ports.putAll(did, Lists.newArrayList(ports));
766 }
767
alshabib0ed6a202014-10-19 12:42:57 -0700768 @Override
769 public int getDeviceCount() {
770 return devices.values().size();
771 }
772
773 @Override
774 public Iterable<Device> getDevices() {
HIGUCHI Yuta9a9edf82015-10-21 11:23:20 -0700775 return ImmutableList.copyOf(devices.values());
alshabib0ed6a202014-10-19 12:42:57 -0700776 }
777
778 @Override
779 public Device getDevice(DeviceId deviceId) {
780 return devices.get(deviceId);
781 }
782
783 @Override
784 public MastershipRole getRole(DeviceId deviceId) {
785 return MastershipRole.MASTER;
786 }
787
788 @Override
789 public List<Port> getPorts(DeviceId deviceId) {
790 return ports.get(deviceId);
791 }
792
793 @Override
794 public Port getPort(DeviceId deviceId, PortNumber portNumber) {
795 for (Port p : ports.get(deviceId)) {
796 if (p.number().equals(portNumber)) {
797 return p;
798 }
799 }
800 return null;
801 }
802
803 @Override
804 public boolean isAvailable(DeviceId deviceId) {
805 return true;
806 }
807
808 @Override
809 public void addListener(DeviceListener listener) {
810 deviceListener = listener;
811
812 }
813
814 @Override
815 public void removeListener(DeviceListener listener) {
816
817 }
818 }
819
820 private final class TestMasterShipService implements MastershipService {
821
822 @Override
823 public MastershipRole getLocalRole(DeviceId deviceId) {
824 return MastershipRole.MASTER;
825 }
826
827 @Override
Madan Jampanide003d92015-05-11 17:14:20 -0700828 public CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId) {
829 return CompletableFuture.completedFuture(null);
alshabib0ed6a202014-10-19 12:42:57 -0700830 }
831
832 @Override
Madan Jampanic6e574f2015-05-29 13:41:52 -0700833 public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) {
834 return null;
alshabib0ed6a202014-10-19 12:42:57 -0700835 }
836
837 @Override
838 public NodeId getMasterFor(DeviceId deviceId) {
839 return null;
840 }
841
842 @Override
843 public Set<DeviceId> getDevicesOf(NodeId nodeId) {
844 return null;
845 }
846
847 @Override
alshabib0ed6a202014-10-19 12:42:57 -0700848 public void addListener(MastershipListener listener) {
849
850 }
851
852 @Override
853 public void removeListener(MastershipListener listener) {
854
855 }
Yuta HIGUCHI376d7502014-10-19 15:57:52 -0700856
857 @Override
Ayaka Koshibeabedb092014-10-20 17:01:31 -0700858 public RoleInfo getNodesFor(DeviceId deviceId) {
859 return new RoleInfo(new NodeId("foo"), Collections.<NodeId>emptyList());
Yuta HIGUCHI376d7502014-10-19 15:57:52 -0700860 }
alshabib0ed6a202014-10-19 12:42:57 -0700861 }
862
863
Thomas Vachuskae4ebac92015-09-10 11:39:05 -0700864 private class TestLinkService extends LinkServiceAdapter {
865 }
Naoki Shiota399a0b32015-11-15 20:36:13 -0600866
867 private final class TestNetworkConfigRegistry
868 extends NetworkConfigRegistryAdapter {
869 @Override
870 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
871 ConfigApplyDelegate delegate = config -> { };
872 ObjectMapper mapper = new ObjectMapper();
873 return (C) cfg;
874 }
875
876 @Override
877 public void addListener(NetworkConfigListener listener) {
878 configListener = listener;
879 }
880 }
881
882 private final class TestSuppressionConfig extends SuppressionConfig {
883 private Set<DeviceId> deviceIds = new HashSet<>(DEFAULT_RULES.getSuppressedDevice());
884 private Set<Device.Type> deviceTypes = new HashSet<>(DEFAULT_RULES.getSuppressedDeviceType());
885 private Map<String, String> annotation = new HashMap<>(DEFAULT_RULES.getSuppressedAnnotation());
886
887 @Override
888 public Set<DeviceId> deviceIds() {
889 return ImmutableSet.copyOf(deviceIds);
890 }
891
892 @Override
893 public SuppressionConfig deviceIds(Set<DeviceId> deviceIds) {
894 this.deviceIds = ImmutableSet.copyOf(deviceIds);
895 return this;
896 }
897
898 @Override
899 public Set<Device.Type> deviceTypes() {
900 return ImmutableSet.copyOf(deviceTypes);
901 }
902
903 @Override
904 public SuppressionConfig deviceTypes(Set<Device.Type> deviceTypes) {
905 this.deviceTypes = ImmutableSet.copyOf(deviceTypes);
906 return this;
907 }
908
909 @Override
910 public Map<String, String> annotation() {
911 return ImmutableMap.copyOf(annotation);
912 }
913
914 @Override
915 public SuppressionConfig annotation(Map<String, String> annotation) {
916 this.annotation = ImmutableMap.copyOf(annotation);
917 return this;
918 }
919 }
alshabib0ed6a202014-10-19 12:42:57 -0700920}