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