blob: 37adf6b12fd7d2e79f5761a308606e1490cc5eae [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * Copyright 2011, Big Switch Networks, Inc.
3 * Originally created by David Erickson, Stanford University
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 **/
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080017
Jonathan Hart284e70f2014-07-05 12:32:51 -070018package net.onrc.onos.core.linkdiscovery;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
Jonathan Hart2fa28062013-11-25 20:16:28 -080020import static org.easymock.EasyMock.createNiceMock;
21import static org.easymock.EasyMock.expect;
22import static org.easymock.EasyMock.replay;
23import static org.easymock.EasyMock.verify;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024
srikanth9f383342014-06-12 11:49:00 -070025import java.io.IOException;
Jonathan Hart2fa28062013-11-25 20:16:28 -080026import java.util.Collections;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080027import java.util.HashMap;
28import java.util.Map;
29
srikanth9f383342014-06-12 11:49:00 -070030import net.floodlightcontroller.core.FloodlightContext;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080031import net.floodlightcontroller.core.IFloodlightProviderService;
Jonathan Hart0f383542014-07-09 11:38:03 -070032import net.floodlightcontroller.core.IListener.Command;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080033import net.floodlightcontroller.core.IOFSwitch;
34import net.floodlightcontroller.core.module.FloodlightModuleContext;
35import net.floodlightcontroller.core.test.MockThreadPoolService;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080036import net.floodlightcontroller.restserver.IRestApiService;
37import net.floodlightcontroller.restserver.RestApiServer;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080038import net.floodlightcontroller.test.FloodlightTestCase;
39import net.floodlightcontroller.threadpool.IThreadPoolService;
Jonathan Hart0f383542014-07-09 11:38:03 -070040import net.onrc.onos.core.packet.Ethernet;
41import net.onrc.onos.core.packet.OnosLldp;
Jonathan Hartcd1ab172014-07-03 14:59:48 -070042import net.onrc.onos.core.registry.IControllerRegistryService;
Jonathan Hart2fa28062013-11-25 20:16:28 -080043
srikanth9f383342014-06-12 11:49:00 -070044import org.easymock.EasyMock;
Jonathan Hart2fa28062013-11-25 20:16:28 -080045import org.junit.Before;
46import org.junit.Test;
srikanth9f383342014-06-12 11:49:00 -070047import org.openflow.protocol.OFMessage;
Jonathan Hart0f383542014-07-09 11:38:03 -070048import org.openflow.protocol.OFPacketIn;
srikanth9f383342014-06-12 11:49:00 -070049import org.openflow.protocol.OFPhysicalPort;
Jonathan Hartcd1ab172014-07-03 14:59:48 -070050import org.openflow.protocol.OFPortStatus;
51import org.openflow.protocol.OFPortStatus.OFPortReason;
Jonathan Hart2fa28062013-11-25 20:16:28 -080052import org.slf4j.Logger;
53import org.slf4j.LoggerFactory;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054
Yuta HIGUCHIaa132f52014-06-26 10:18:39 -070055// CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080056/**
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080057 * @author David Erickson (daviderickson@cs.stanford.edu)
58 */
59public class LinkDiscoveryManagerTest extends FloodlightTestCase {
60
61 private TestLinkDiscoveryManager ldm;
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070062 protected static final Logger log = LoggerFactory.getLogger(LinkDiscoveryManagerTest.class);
Ray Milkey269ffb92014-04-03 14:43:30 -070063
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080064 public class TestLinkDiscoveryManager extends LinkDiscoveryManager {
65 public boolean isSendLLDPsCalled = false;
66 public boolean isClearLinksCalled = false;
67
68 @Override
69 protected void discoverOnAllPorts() {
70 isSendLLDPsCalled = true;
71 super.discoverOnAllPorts();
72 }
73
74 public void reset() {
75 isSendLLDPsCalled = false;
76 isClearLinksCalled = false;
77 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080078 }
Ray Milkey269ffb92014-04-03 14:43:30 -070079
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080080 public LinkDiscoveryManager getTopology() {
81 return ldm;
82 }
83
84 public IOFSwitch createMockSwitch(Long id) {
85 IOFSwitch mockSwitch = createNiceMock(IOFSwitch.class);
86 expect(mockSwitch.getId()).andReturn(id).anyTimes();
Jonathan Hart0f383542014-07-09 11:38:03 -070087 expect(mockSwitch.portEnabled(EasyMock.anyShort())).andReturn(true).anyTimes();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080088 return mockSwitch;
89 }
90
Yuta HIGUCHI44a0b352014-05-14 21:32:48 -070091 @Override
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080092 @Before
93 public void setUp() throws Exception {
94 super.setUp();
95 FloodlightModuleContext cntx = new FloodlightModuleContext();
96 ldm = new TestLinkDiscoveryManager();
Jonathan Hart284e70f2014-07-05 12:32:51 -070097 //ldm.linkDiscoveryAware = new ArrayList<ILinkDiscoveryListener>();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080098 MockThreadPoolService tp = new MockThreadPoolService();
99 RestApiServer restApi = new RestApiServer();
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700100 IControllerRegistryService registry =
101 EasyMock.createMock(IControllerRegistryService.class);
102 expect(registry.hasControl(EasyMock.anyLong())).andReturn(true).anyTimes();
103 replay(registry);
104 cntx.addService(IControllerRegistryService.class, registry);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800105 cntx.addService(IRestApiService.class, restApi);
106 cntx.addService(IThreadPoolService.class, tp);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800107 cntx.addService(ILinkDiscoveryService.class, ldm);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800108 cntx.addService(IFloodlightProviderService.class, getMockFloodlightProvider());
109 restApi.init(cntx);
110 tp.init(cntx);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800111 ldm.init(cntx);
112 restApi.startUp(cntx);
113 tp.startUp(cntx);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800114 ldm.startUp(cntx);
115
116 IOFSwitch sw1 = createMockSwitch(1L);
117 IOFSwitch sw2 = createMockSwitch(2L);
118 Map<Long, IOFSwitch> switches = new HashMap<Long, IOFSwitch>();
119 switches.put(1L, sw1);
120 switches.put(2L, sw2);
121 getMockFloodlightProvider().setSwitches(switches);
122 replay(sw1, sw2);
123 }
124
125 @Test
126 public void testAddOrUpdateLink() throws Exception {
127 LinkDiscoveryManager topology = getTopology();
128
129 Link lt = new Link(1L, 2, 2L, 1);
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700130 long firstSeenTime = System.currentTimeMillis();
131 LinkInfo info = new LinkInfo(firstSeenTime,
Jonathan Hartba354e02014-06-30 19:18:16 -0700132 System.currentTimeMillis(), 0, 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800133 topology.addOrUpdateLink(lt, info);
134
135
136 NodePortTuple srcNpt = new NodePortTuple(1L, 2);
137 NodePortTuple dstNpt = new NodePortTuple(2L, 1);
138
139 // check invariants hold
140 assertNotNull(topology.switchLinks.get(lt.getSrc()));
141 assertTrue(topology.switchLinks.get(lt.getSrc()).contains(lt));
142 assertNotNull(topology.portLinks.get(srcNpt));
143 assertTrue(topology.portLinks.get(srcNpt).contains(lt));
144 assertNotNull(topology.portLinks.get(dstNpt));
145 assertTrue(topology.portLinks.get(dstNpt).contains(lt));
146 assertTrue(topology.links.containsKey(lt));
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700147
148 LinkInfo infoToVerify = topology.links.get(lt);
149 assertEquals(firstSeenTime, infoToVerify.getFirstSeenTime());
150 assertEquals(0, infoToVerify.getSrcPortState());
151 assertEquals(0, infoToVerify.getDstPortState());
152
153 // Arbitrary new port states to verify that the port state is updated
154 final int newSrcPortState = 1;
155 final int newDstPortState = 2;
156
157 // Update the last received probe timestamp and the port states
158 LinkInfo infoWithStateChange = new LinkInfo(System.currentTimeMillis(),
159 System.currentTimeMillis(), newSrcPortState, newDstPortState);
160
161 topology.addOrUpdateLink(lt, infoWithStateChange);
162
163 assertNotNull(topology.links.get(lt));
164 infoToVerify = topology.links.get(lt);
165 // First seen time should be the original time, not the second update time
166 assertEquals(firstSeenTime, infoToVerify.getFirstSeenTime());
167 // Both port states should have been updated
168 assertEquals(newSrcPortState, infoToVerify.getSrcPortState());
169 assertEquals(newDstPortState, infoToVerify.getDstPortState());
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800170 }
171
172 @Test
173 public void testDeleteLink() throws Exception {
174 LinkDiscoveryManager topology = getTopology();
175
176 Link lt = new Link(1L, 2, 2L, 1);
177 LinkInfo info = new LinkInfo(System.currentTimeMillis(),
Jonathan Hartba354e02014-06-30 19:18:16 -0700178 System.currentTimeMillis(), 0, 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800179 topology.addOrUpdateLink(lt, info);
Jonathan Hart284e70f2014-07-05 12:32:51 -0700180 topology.deleteLinks(Collections.singletonList(lt));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800181
182 // check invariants hold
183 assertNull(topology.switchLinks.get(lt.getSrc()));
184 assertNull(topology.switchLinks.get(lt.getDst()));
185 assertNull(topology.portLinks.get(lt.getSrc()));
186 assertNull(topology.portLinks.get(lt.getDst()));
187 assertTrue(topology.links.isEmpty());
188 }
189
190 @Test
191 public void testAddOrUpdateLinkToSelf() throws Exception {
192 LinkDiscoveryManager topology = getTopology();
193
194 Link lt = new Link(1L, 2, 2L, 3);
195 NodePortTuple srcNpt = new NodePortTuple(1L, 2);
196 NodePortTuple dstNpt = new NodePortTuple(2L, 3);
197
198 LinkInfo info = new LinkInfo(System.currentTimeMillis(),
Jonathan Hartba354e02014-06-30 19:18:16 -0700199 System.currentTimeMillis(), 0, 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800200 topology.addOrUpdateLink(lt, info);
201
202 // check invariants hold
203 assertNotNull(topology.switchLinks.get(lt.getSrc()));
204 assertTrue(topology.switchLinks.get(lt.getSrc()).contains(lt));
205 assertNotNull(topology.portLinks.get(srcNpt));
206 assertTrue(topology.portLinks.get(srcNpt).contains(lt));
207 assertNotNull(topology.portLinks.get(dstNpt));
208 assertTrue(topology.portLinks.get(dstNpt).contains(lt));
209 assertTrue(topology.links.containsKey(lt));
210 }
211
212 @Test
213 public void testDeleteLinkToSelf() throws Exception {
214 LinkDiscoveryManager topology = getTopology();
215
216 Link lt = new Link(1L, 2, 1L, 3);
217 NodePortTuple srcNpt = new NodePortTuple(1L, 2);
218 NodePortTuple dstNpt = new NodePortTuple(2L, 3);
219
220 LinkInfo info = new LinkInfo(System.currentTimeMillis(),
Jonathan Hartba354e02014-06-30 19:18:16 -0700221 System.currentTimeMillis(), 0, 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800222 topology.addOrUpdateLink(lt, info);
Jonathan Hart284e70f2014-07-05 12:32:51 -0700223 topology.deleteLinks(Collections.singletonList(lt));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800224
225 // check invariants hold
226 assertNull(topology.switchLinks.get(lt.getSrc()));
227 assertNull(topology.switchLinks.get(lt.getDst()));
228 assertNull(topology.portLinks.get(srcNpt));
229 assertNull(topology.portLinks.get(dstNpt));
230 assertTrue(topology.links.isEmpty());
231 }
232
233 @Test
234 public void testRemovedSwitch() {
235 LinkDiscoveryManager topology = getTopology();
236
237 Link lt = new Link(1L, 2, 2L, 1);
238 NodePortTuple srcNpt = new NodePortTuple(1L, 2);
239 NodePortTuple dstNpt = new NodePortTuple(2L, 1);
240 LinkInfo info = new LinkInfo(System.currentTimeMillis(),
Jonathan Hartba354e02014-06-30 19:18:16 -0700241 System.currentTimeMillis(), 0, 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800242 topology.addOrUpdateLink(lt, info);
243
244 IOFSwitch sw1 = getMockFloodlightProvider().getSwitches().get(1L);
245 IOFSwitch sw2 = getMockFloodlightProvider().getSwitches().get(2L);
246 // Mock up our expected behavior
247 topology.removedSwitch(sw1);
248 verify(sw1, sw2);
249
250 // check invariants hold
251 assertNull(topology.switchLinks.get(lt.getSrc()));
252 assertNull(topology.switchLinks.get(lt.getDst()));
253 assertNull(topology.portLinks.get(srcNpt));
254 assertNull(topology.portLinks.get(dstNpt));
255 assertTrue(topology.links.isEmpty());
256 }
257
258 @Test
259 public void testRemovedSwitchSelf() {
260 LinkDiscoveryManager topology = getTopology();
261 IOFSwitch sw1 = createMockSwitch(1L);
262 replay(sw1);
263 Link lt = new Link(1L, 2, 1L, 3);
264 LinkInfo info = new LinkInfo(System.currentTimeMillis(),
Jonathan Hartba354e02014-06-30 19:18:16 -0700265 System.currentTimeMillis(), 0, 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800266 topology.addOrUpdateLink(lt, info);
267
268 // Mock up our expected behavior
269 topology.removedSwitch(sw1);
270
271 verify(sw1);
272 // check invariants hold
273 assertNull(topology.switchLinks.get(lt.getSrc()));
274 assertNull(topology.portLinks.get(lt.getSrc()));
275 assertNull(topology.portLinks.get(lt.getDst()));
276 assertTrue(topology.links.isEmpty());
277 }
278
279 @Test
280 public void testAddUpdateLinks() throws Exception {
281 LinkDiscoveryManager topology = getTopology();
282
283 Link lt = new Link(1L, 1, 2L, 1);
284 NodePortTuple srcNpt = new NodePortTuple(1L, 1);
285 NodePortTuple dstNpt = new NodePortTuple(2L, 1);
Ray Milkey269ffb92014-04-03 14:43:30 -0700286
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800287 LinkInfo info;
288
289 info = new LinkInfo(System.currentTimeMillis() - 40000,
Jonathan Hartba354e02014-06-30 19:18:16 -0700290 System.currentTimeMillis() - 40000, 0, 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800291 topology.addOrUpdateLink(lt, info);
292
293 // check invariants hold
294 assertNotNull(topology.switchLinks.get(lt.getSrc()));
295 assertTrue(topology.switchLinks.get(lt.getSrc()).contains(lt));
296 assertNotNull(topology.portLinks.get(srcNpt));
297 assertTrue(topology.portLinks.get(srcNpt).contains(lt));
298 assertNotNull(topology.portLinks.get(dstNpt));
299 assertTrue(topology.portLinks.get(dstNpt).contains(lt));
300 assertTrue(topology.links.containsKey(lt));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800301
Jonathan Hart284e70f2014-07-05 12:32:51 -0700302 topology.timeOutLinks();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800303
Jonathan Hartba354e02014-06-30 19:18:16 -0700304 // Add a link info based on info that would be obtained from unicast LLDP
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800305 // Setting the unicast LLDP reception time to be 40 seconds old, so we can use
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700306 // this to test timeout after this test.
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800307 info = new LinkInfo(System.currentTimeMillis() - 40000,
Jonathan Hartba354e02014-06-30 19:18:16 -0700308 System.currentTimeMillis() - 40000, 0, 0);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800309 topology.addOrUpdateLink(lt, info);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800310
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700311 // Expect to timeout the unicast Valid Time, so the link should disappear
Jonathan Hart284e70f2014-07-05 12:32:51 -0700312 topology.timeOutLinks();
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800313 assertTrue(topology.links.get(lt) == null);
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800314 }
srikanth9f383342014-06-12 11:49:00 -0700315
316 /**
317 * This test case verifies that LinkDiscoveryManager.sendDiscoveryMessage()
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700318 * performs "write" operation on the specified IOFSwitch object
319 * with a LLDP packet.
320 *
srikanth9f383342014-06-12 11:49:00 -0700321 * @throws IOException
322 */
323 @Test
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700324 public void testSendDiscoveryMessage() throws IOException {
Jonathan Hart299d1132014-06-27 09:25:28 -0700325 byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1};
326
srikanth9f383342014-06-12 11:49:00 -0700327 LinkDiscoveryManager topology = getTopology();
328
329 // Mock up our expected behavior
330 IOFSwitch swTest = createMockSwitch(3L);
331 getMockFloodlightProvider().getSwitches().put(3L, swTest);
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700332
srikanth9f383342014-06-12 11:49:00 -0700333 short portNum = 1;
334 OFPhysicalPort ofpPort = new OFPhysicalPort();
335 ofpPort.setPortNumber(portNum);
Jonathan Hart299d1132014-06-27 09:25:28 -0700336 ofpPort.setHardwareAddress(macAddress);
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700337
338 /* sendDiscoverMessage() should perform the following actions on
srikanth9f383342014-06-12 11:49:00 -0700339 * IOFSwitch object
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700340 * - getPort() with argument as "1"
341 * - write() with OFPacketOut
342 * - flush()
srikanth9f383342014-06-12 11:49:00 -0700343 */
344 expect(swTest.getPort(portNum)).andReturn(ofpPort).atLeastOnce();
345 swTest.write(EasyMock.anyObject(OFMessage.class), EasyMock.anyObject(FloodlightContext.class));
346 EasyMock.expectLastCall().times(1);
347 swTest.flush();
348 EasyMock.expectLastCall().once();
349 replay(swTest);
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700350
Jonathan Hartba354e02014-06-30 19:18:16 -0700351 topology.sendDiscoveryMessage(3L, portNum, false);
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -0700352
srikanth9f383342014-06-12 11:49:00 -0700353 verify(swTest);
354 }
Jonathan Hartcd1ab172014-07-03 14:59:48 -0700355
356 @Test
357 public void testHandlePortStatusForNewPort() throws IOException {
358 byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1};
359
360 LinkDiscoveryManager linkDiscovery = getTopology();
361
362 long dpid = 3L;
363 IOFSwitch sw = createMockSwitch(dpid);
364 getMockFloodlightProvider().getSwitches().put(dpid, sw);
365
366 short portNum = 1;
367 OFPhysicalPort ofpPort = new OFPhysicalPort();
368 ofpPort.setPortNumber(portNum);
369 ofpPort.setHardwareAddress(macAddress);
370
371 OFPortStatus portStatus = new OFPortStatus();
372 portStatus.setDesc(ofpPort);
373 portStatus.setReason((byte) OFPortReason.OFPPR_ADD.ordinal());
374
375 expect(sw.getPort(portNum)).andReturn(ofpPort).anyTimes();
376 sw.write(EasyMock.anyObject(OFMessage.class),
377 EasyMock.anyObject(FloodlightContext.class));
378 sw.flush();
379
380 replay(sw);
381
382 linkDiscovery.handlePortStatus(sw, portStatus);
383
384 verify(sw);
385 }
386
387 @Test
388 public void testHandlePortStatusForExistingPort() {
389 byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1};
390
391 LinkDiscoveryManager linkDiscovery = getTopology();
392
393 // Add a link that we can update later during the test
394 Link lt = new Link(1L, 1, 2L, 1);
395 LinkInfo info = new LinkInfo(System.currentTimeMillis(),
396 System.currentTimeMillis(), 0, 0);
397 linkDiscovery.addOrUpdateLink(lt, info);
398
399 short portNum = 1;
400 // src port
401 int srcPortState = 2;
402 OFPhysicalPort srcPort = new OFPhysicalPort();
403 srcPort.setPortNumber(portNum);
404 srcPort.setHardwareAddress(macAddress);
405 srcPort.setState(srcPortState);
406
407 // dst port
408 int dstPortState = 4;
409 OFPhysicalPort dstPort = new OFPhysicalPort();
410 dstPort.setPortNumber(portNum);
411 dstPort.setHardwareAddress(macAddress);
412 dstPort.setState(dstPortState);
413
414 OFPortStatus srcPortStatus = new OFPortStatus();
415 srcPortStatus.setDesc(srcPort);
416 srcPortStatus.setReason((byte) OFPortReason.OFPPR_MODIFY.ordinal());
417
418 OFPortStatus dstPortStatus = new OFPortStatus();
419 dstPortStatus.setDesc(dstPort);
420 dstPortStatus.setReason((byte) OFPortReason.OFPPR_MODIFY.ordinal());
421
422 linkDiscovery.handlePortStatus(
423 getMockFloodlightProvider().getSwitches().get(1L), srcPortStatus);
424
425
426 LinkInfo newInfo = linkDiscovery.links.get(lt);
427 assertEquals(srcPortState, newInfo.getSrcPortState());
428 assertEquals(0, newInfo.getDstPortState());
429
430
431 linkDiscovery.handlePortStatus(
432 getMockFloodlightProvider().getSwitches().get(2L), dstPortStatus);
433
434 newInfo = linkDiscovery.links.get(lt);
435 assertEquals(srcPortState, newInfo.getSrcPortState());
436 assertEquals(dstPortState, newInfo.getDstPortState());
437 }
Jonathan Hart0f383542014-07-09 11:38:03 -0700438
439 @Test
440 public void testHandlePortStatusForDeletePort() {
441 byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1};
442
443 LinkDiscoveryManager linkDiscovery = getTopology();
444
445 // Add a link that we can delete later during the test
446 Link lt = new Link(1L, 1, 2L, 2);
447 LinkInfo info = new LinkInfo(System.currentTimeMillis(),
448 System.currentTimeMillis(), 0, 0);
449 linkDiscovery.addOrUpdateLink(lt, info);
450
451 short portNum = 1;
452 int srcPortState = 1;
453 OFPhysicalPort srcPort = new OFPhysicalPort();
454 srcPort.setPortNumber(portNum);
455 srcPort.setHardwareAddress(macAddress);
456 srcPort.setState(srcPortState);
457
458 OFPortStatus srcPortStatus = new OFPortStatus();
459 srcPortStatus.setDesc(srcPort);
460 srcPortStatus.setReason((byte) OFPortReason.OFPPR_DELETE.ordinal());
461
462 assertNotNull(linkDiscovery.getLinks().get(lt));
463
464 // Send a delete port status for the source port, which should result
465 // in the link being deleted
466 linkDiscovery.handlePortStatus(
467 getMockFloodlightProvider().getSwitches().get(1L), srcPortStatus);
468
469 assertNull(linkDiscovery.getLinks().get(lt));
470 }
471
472 @Test
473 public void testReceive() {
474 byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1};
475
476 OnosLldp lldpPacket = new OnosLldp();
477 lldpPacket.setPort((short) 1);
478 lldpPacket.setSwitch(1L);
479 lldpPacket.setReverse(false);
480
481 Ethernet ethPacket = new Ethernet();
482 ethPacket.setEtherType(Ethernet.TYPE_LLDP);
483 ethPacket.setSourceMACAddress(macAddress);
484 ethPacket.setDestinationMACAddress(
485 LinkDiscoveryManager.LLDP_STANDARD_DST_MAC_STRING);
486 ethPacket.setPayload(lldpPacket);
487 ethPacket.setPad(true);
488
489 OFPacketIn pi = new OFPacketIn();
490 pi.setInPort((short) 2);
491 pi.setPacketData(ethPacket.serialize());
492
493 LinkDiscoveryManager linkDiscovery = getTopology();
494
495 Link expectedLink = new Link(1L, 1, 2L, 2);
496
497 assertNull(linkDiscovery.links.get(expectedLink));
498
499 // Sending in the LLDP packet should cause the link to be created
500 Command command = linkDiscovery.handleLldp(lldpPacket, 2L, pi);
501
502 assertEquals(Command.STOP, command);
503 assertNotNull(linkDiscovery.links.get(expectedLink));
504 }
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800505}