blob: c1b35c341599b0970257b0fc3dbbdefe98c6621d [file] [log] [blame]
Ray Milkey2d572dd2017-04-14 10:01:24 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkey2d572dd2017-04-14 10:01:24 -07003 *
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
7 *
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.
15 */
ADARA Networks1fb1eb12016-09-01 12:04:07 -070016package org.onosproject.rabbitmq.listener;
17
18import static com.google.common.collect.ImmutableSet.of;
19import static org.easymock.EasyMock.createMock;
20import static org.easymock.EasyMock.expect;
21import static org.easymock.EasyMock.replay;
22import static org.junit.Assert.assertEquals;
23import static org.junit.Assert.assertFalse;
24import static org.onosproject.net.DeviceId.deviceId;
25import static org.onosproject.net.NetTestTools.device;
26import static org.onosproject.net.NetTestTools.link;
27import static org.onosproject.net.PortNumber.portNumber;
28import static org.onosproject.net.topology.TopologyEvent.Type.TOPOLOGY_CHANGED;
29import static org.onosproject.net.device.DeviceEvent.Type.*;
30import static org.onosproject.net.link.LinkEvent.Type.*;
31import static org.onosproject.net.Device.Type.*;
32
33import java.nio.ByteBuffer;
34import java.util.ArrayList;
35import java.util.HashMap;
36import java.util.List;
37import java.util.Map;
38import java.util.Set;
39
40import org.easymock.EasyMockRunner;
41import org.easymock.Mock;
42import org.junit.After;
43import org.junit.Before;
44import org.junit.Test;
45import org.junit.runner.RunWith;
46import org.onlab.packet.ChassisId;
47import org.onlab.packet.Ethernet;
Ray Milkey780bb9b2017-12-18 16:14:52 -080048import org.onlab.packet.MacAddress;
ADARA Networks1fb1eb12016-09-01 12:04:07 -070049import org.onlab.packet.ONOSLLDP;
50import org.onosproject.core.ApplicationId;
51import org.onosproject.core.CoreService;
52import org.onosproject.core.DefaultApplicationId;
53import org.onosproject.event.AbstractEventTest;
54import org.onosproject.event.Event;
55import org.onosproject.net.ConnectPoint;
56import org.onosproject.net.DefaultDevice;
57import org.onosproject.net.DefaultLink;
58import org.onosproject.net.DefaultPort;
59import org.onosproject.net.Device;
60import org.onosproject.net.DeviceId;
61import org.onosproject.net.Link;
62import org.onosproject.net.link.LinkEvent;
63import org.onosproject.net.MastershipRole;
64import org.onosproject.net.Port;
65import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceServiceAdapter;
68import org.onosproject.net.flow.TrafficTreatment;
69import org.onosproject.net.link.LinkListener;
70import org.onosproject.net.link.LinkService;
71import org.onosproject.net.packet.DefaultInboundPacket;
72import org.onosproject.net.packet.InboundPacket;
73import org.onosproject.net.packet.OutboundPacket;
74import org.onosproject.net.packet.PacketContext;
75import org.onosproject.net.packet.PacketProcessor;
76import org.onosproject.net.packet.PacketServiceAdapter;
77import org.onosproject.net.provider.AbstractProvider;
78import org.onosproject.net.provider.ProviderId;
79import org.onosproject.net.topology.DefaultGraphDescription;
80import org.onosproject.net.topology.GraphDescription;
81import org.onosproject.net.topology.Topology;
82import org.onosproject.net.topology.TopologyEvent;
83import org.onosproject.net.topology.TopologyListener;
84import org.onosproject.net.topology.TopologyProvider;
85import org.onosproject.net.topology.TopologyProviderRegistry;
86import org.onosproject.net.topology.TopologyProviderService;
87import org.onosproject.net.topology.TopologyService;
88import org.onosproject.rabbitmq.api.MQService;
89import org.onosproject.rabbitmq.api.Manageable;
90import org.osgi.service.component.ComponentContext;
91
92import com.google.common.collect.ArrayListMultimap;
93import com.google.common.collect.ImmutableList;
94import com.google.common.collect.Lists;
95import com.google.common.util.concurrent.MoreExecutors;
96/**
97 * Junit tests for packet in, device, topology and link events.
98 */
99@RunWith(EasyMockRunner.class)
100public class MQEventHandlerTest extends AbstractEventTest {
101
102 private final MQEventHandler mqEventHandler = new MQEventHandler();
103 private static final DeviceId DID1 = deviceId("of:0000000000000001");
104 private static final DeviceId DID2 = deviceId("of:0000000000000002");
105 private static final DeviceId DID3 = deviceId("of:0000000000000003");
106
107 private ApplicationId appId = new DefaultApplicationId(100,
108 "org.onosproject.rabbitmq");
109 private final TestPacketService packetService = new TestPacketService();
110 private final TestDeviceService deviceService = new TestDeviceService();
111 private PacketProcessor testProcessor;
112 private DeviceListener deviceListener;
113 private static Port pd1;
114 private static Port pd2;
115 private static Port pd3;
116 private static Port pd4;
117 private CoreService coreService;
118 @Mock
119 ComponentContext context;
120 @Mock
121 private Manageable manageSender;
122
123 private static final ProviderId PID = new ProviderId("of", "foo");
124 private TestLinkListener testLinkListener = new TestLinkListener();
125 @Mock
126 private LinkService linkService;
127 @Mock
128 Topology topology;
129 @Mock
130 protected TopologyService service;
131 protected TopologyProviderRegistry registry;
132 @Mock
133 protected TopologyProviderService providerService;
134 protected TestProvider provider;
135 protected TestTopologyListener listener = new TestTopologyListener();
136 @Mock
137 MQService mqService;
138
139 @Before
140 public void setUp() {
141 coreService = createMock(CoreService.class);
142 expect(coreService.registerApplication(appId.name()))
143 .andReturn(appId).anyTimes();
144 replay(coreService);
145 mqEventHandler.deviceService = deviceService;
146 mqEventHandler.packetService = packetService;
147 mqEventHandler.eventExecutor = MoreExecutors.newDirectExecutorService();
148 linkService.addListener(testLinkListener);
149 mqEventHandler.linkService = linkService;
150 mqEventHandler.topologyService = service;
151 mqEventHandler.activate(context);
152 }
153
154 @After
155 public void tearDown() {
156 mqEventHandler.deactivate();
157 mqEventHandler.deviceService = null;
158 mqEventHandler.packetService = null;
159 }
160
161 private DeviceEvent deviceEvent(DeviceEvent.Type type, DeviceId did) {
162 return new DeviceEvent(type, deviceService.getDevice(did));
163
164 }
165
166 private Port port(DeviceId did, long port, boolean enabled) {
167 return new DefaultPort(deviceService.getDevice(did),
168 portNumber(port), enabled);
169 }
170
171 private DeviceEvent portEvent(DeviceEvent.Type type, DeviceId did,
172 Port port) {
173 return new DeviceEvent(type, deviceService.getDevice(did), port);
174 }
175
176 @Test
177 public void switchAdd() {
178 DeviceEvent de = deviceEvent(DEVICE_ADDED, DID1);
179 deviceListener.event(de);
180
181 }
182
183 @Test
184 public void switchRemove() {
185 deviceListener.event(deviceEvent(DEVICE_ADDED, DID1));
186 deviceListener.event(deviceEvent(DEVICE_REMOVED, DID1));
187 }
188
189 @Test
190 public void switchUpdate() {
191 deviceListener.event(deviceEvent(DEVICE_UPDATED, DID1));
192 deviceListener.event(deviceEvent(DEVICE_REMOVED, DID1));
193 }
194
195 @Test
196 public void switchSuspend() {
197 deviceListener.event(deviceEvent(DEVICE_SUSPENDED, DID1));
198 deviceListener.event(deviceEvent(DEVICE_REMOVED, DID1));
199 }
200
201 @Test
202 public void portUp() {
203 deviceListener.event(deviceEvent(DEVICE_ADDED, DID1));
204 deviceListener.event(portEvent(PORT_ADDED, DID1, port(DID1, 3, true)));
205 }
206
207 @Test
208 public void portDown() {
209 deviceListener.event(deviceEvent(DEVICE_ADDED, DID1));
210 deviceListener.event(portEvent(PORT_ADDED, DID1, port(DID1, 1, false)));
211 }
212
213 @Test
214 public void portRemoved() {
215 deviceListener.event(deviceEvent(DEVICE_ADDED, DID1));
216 deviceListener.event(portEvent(PORT_ADDED, DID1, port(DID1, 3, true)));
217 deviceListener.event(portEvent(PORT_REMOVED, DID1,
218 port(DID1, 3, true)));
219 }
220
221 @Test
222 public void unknownPktCtx() {
223 // Note: DID3 hasn't been added to TestDeviceService
224 PacketContext pktCtx = new TestPacketContext(device1(DID3));
225 testProcessor.process(pktCtx);
226 assertFalse("Context should still be free", pktCtx.isHandled());
227 }
228
229 private DefaultDevice device1(DeviceId did) {
230 return new DefaultDevice(ProviderId.NONE, did, SWITCH,
231 "TESTMF", "TESTHW", "TESTSW", "TESTSN",
232 new ChassisId());
233 }
234
235 @Test
236 public void knownPktCtx() {
237 deviceListener.event(deviceEvent(DEVICE_ADDED, DID1));
238 deviceListener.event(deviceEvent(DEVICE_ADDED, DID2));
239 PacketContext pktCtx = new TestPacketContext(
240 deviceService.getDevice(DID2));
241 /*
242 * EasyMock.expectLastCall(); EasyMock.replay(manageSender);
243 */
244 testProcessor.process(pktCtx);
245 }
246
247 private class TestDeviceService extends DeviceServiceAdapter {
248
249 private final Map<DeviceId, Device> devices = new HashMap<>();
250 private final ArrayListMultimap<DeviceId, Port> ports =
251 ArrayListMultimap.create();
252
253 public TestDeviceService() {
254 Device d1 = new DefaultDevice(ProviderId.NONE, DID1,
255 SWITCH, "TESTMF", "TESTHW",
256 "TESTSW", "TESTSN", new ChassisId());
257 Device d2 = new DefaultDevice(ProviderId.NONE, DID2, SWITCH,
258 "TESTMF", "TESTHW", "TESTSW",
259 "TESTSN", new ChassisId());
260 devices.put(DID1, d1);
261 devices.put(DID2, d2);
262 pd1 = new DefaultPort(d1, portNumber(1), true);
263 pd2 = new DefaultPort(d1, portNumber(2), true);
264 pd3 = new DefaultPort(d2, portNumber(1), true);
265 pd4 = new DefaultPort(d2, portNumber(2), true);
266 ports.putAll(DID1, Lists.newArrayList(pd1, pd2));
267 ports.putAll(DID2, Lists.newArrayList(pd3, pd4));
268 }
269
270 @Override
271 public int getDeviceCount() {
272 return devices.values().size();
273 }
274
275 @Override
276 public Iterable<Device> getDevices() {
277 return ImmutableList.copyOf(devices.values());
278 }
279
280 @Override
281 public Device getDevice(DeviceId deviceId) {
282 return devices.get(deviceId);
283 }
284
285 @Override
286 public MastershipRole getRole(DeviceId deviceId) {
287 return MastershipRole.MASTER;
288 }
289
290 @Override
291 public boolean isAvailable(DeviceId deviceId) {
292 return true;
293 }
294
295 @Override
296 public void addListener(DeviceListener listener) {
297 deviceListener = listener;
298
299 }
300
301 @Override
302 public void removeListener(DeviceListener listener) {
303
304 }
305 }
306
307 private class TestPacketService extends PacketServiceAdapter {
308 @Override
309 public void addProcessor(PacketProcessor processor, int priority) {
310 testProcessor = processor;
311 }
312 }
313
314 private class TestPacketContext implements PacketContext {
315
316 protected Device device;
317 protected boolean blocked = false;
318
319 public TestPacketContext(Device dev) {
320 device = dev;
321 }
322
323 @Override
324 public long time() {
325 return 0;
326 }
327
328 @Override
329 public InboundPacket inPacket() {
Samuel Jero31e16f52018-09-21 10:34:28 -0400330 ONOSLLDP lldp = ONOSLLDP.onosSecureLLDP(deviceService.getDevice(DID1)
ADARA Networks1fb1eb12016-09-01 12:04:07 -0700331 .id().toString(),
Samuel Jero31e16f52018-09-21 10:34:28 -0400332 device.chassisId(),
333 (int) pd1.number().toLong(), "", "test");
ADARA Networks1fb1eb12016-09-01 12:04:07 -0700334
335 Ethernet ethPacket = new Ethernet();
336 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
Ray Milkey780bb9b2017-12-18 16:14:52 -0800337 ethPacket.setDestinationMACAddress(MacAddress.ONOS_LLDP);
ADARA Networks1fb1eb12016-09-01 12:04:07 -0700338 ethPacket.setPayload(lldp);
339 ethPacket.setPad(true);
340
341 ethPacket.setSourceMACAddress("DE:AD:BE:EF:BA:11");
342
343 ConnectPoint cp = new ConnectPoint(device.id(), pd3.number());
344
345 return new DefaultInboundPacket(cp, ethPacket,
346 ByteBuffer.wrap(ethPacket
347 .serialize()));
348
349 }
350
351 @Override
352 public OutboundPacket outPacket() {
353 return null;
354 }
355
356 @Override
357 public TrafficTreatment.Builder treatmentBuilder() {
358 return null;
359 }
360
361 @Override
362 public void send() {
363
364 }
365
366 @Override
367 public boolean block() {
368 blocked = true;
369 return blocked;
370 }
371
372 @Override
373 public boolean isHandled() {
374 return blocked;
375 }
376 }
377
378 private void submitTopologyGraph() {
379 Set<Device> devices = of(device("a"), device("b"), device("c"),
380 device("d"), device("e"), device("f"));
381 Set<Link> links = of(link("a", 1, "b", 1), link("b", 1, "a", 1),
382 link("b", 2, "c", 1), link("c", 1, "b", 2),
383 link("c", 2, "d", 1), link("d", 1, "c", 2),
384 link("d", 2, "a", 2), link("a", 2, "d", 2),
385 link("e", 1, "f", 1), link("f", 1, "e", 1));
386 GraphDescription data = new DefaultGraphDescription(4321L,
387 System.currentTimeMillis(),
388 devices, links);
389 providerService.topologyChanged(data, null);
390 }
391
392 protected void validateEvents(Enum... types) {
393 int i = 0;
394 for (Event event : listener.events) {
395 assertEquals("incorrect event type", types[i], event.type());
396 i++;
397 }
398 listener.events.clear();
399 }
400
401 @Test
402 public void testCreateTopology() {
403 submitTopologyGraph();
404 validateEvents(TOPOLOGY_CHANGED);
405 }
406
407 private class TestProvider extends AbstractProvider
408 implements TopologyProvider {
409 public TestProvider() {
410 super(PID);
411 }
412
413 @Override
414 public void triggerRecompute() {
415 }
416 }
417
418 private class TestTopologyListener implements TopologyListener {
419 final List<TopologyEvent> events = new ArrayList<>();
420
421 @Override
422 public void event(TopologyEvent event) {
423 mqService.publish(event);
424 }
425 }
426
427 private Link createLink() {
428 return DefaultLink.builder().providerId(new ProviderId("of", "foo"))
429 .src(new ConnectPoint(deviceId("of:foo"), portNumber(1)))
430 .dst(new ConnectPoint(deviceId("of:bar"), portNumber(2)))
431 .type(Link.Type.INDIRECT).build();
432 }
433
434 @Test
435 public void testAddLink() throws Exception {
436 Link link = createLink();
437 LinkEvent event = new LinkEvent(LINK_ADDED, link, 123L);
438 validateEvent(event, LINK_ADDED, link, 123L);
439 }
440
441 @Test
442 public void testUpdateLink() throws Exception {
443 Link link = createLink();
444 LinkEvent event = new LinkEvent(LINK_UPDATED, link, 123L);
445 validateEvent(event, LINK_UPDATED, link, 123L);
446 }
447
448 @Test
449 public void testRemoveLink() throws Exception {
450 Link link = createLink();
451 LinkEvent event = new LinkEvent(LINK_ADDED, link, 123L);
452 validateEvent(event, LINK_ADDED, link, 123L);
453 LinkEvent event1 = new LinkEvent(LINK_REMOVED, link, 123L);
454 validateEvent(event1, LINK_REMOVED, link, 123L);
455 }
456
457 private class TestLinkListener implements LinkListener {
458
459 @Override
460 public void event(LinkEvent event) {
461 mqService.publish(event);
462 }
463
464 }
465
466}