Added a couple more link discovery tests to cover the recent changes
Change-Id: I71bbcb9d4570366272ce77eb6a292019f8ad7865
diff --git a/src/main/java/net/onrc/onos/core/linkdiscovery/LinkDiscoveryManager.java b/src/main/java/net/onrc/onos/core/linkdiscovery/LinkDiscoveryManager.java
index db35809..c02aff0 100644
--- a/src/main/java/net/onrc/onos/core/linkdiscovery/LinkDiscoveryManager.java
+++ b/src/main/java/net/onrc/onos/core/linkdiscovery/LinkDiscoveryManager.java
@@ -113,7 +113,7 @@
private IControllerRegistryService registryService;
// LLDP fields
- private static final byte[] LLDP_STANDARD_DST_MAC_STRING =
+ static final byte[] LLDP_STANDARD_DST_MAC_STRING =
HexString.fromHexString("01:80:c2:00:00:0e");
private static final long LINK_LOCAL_MASK = 0xfffffffffff0L;
private static final long LINK_LOCAL_VALUE = 0x0180c2000000L;
@@ -418,7 +418,7 @@
return Command.CONTINUE;
}
- private Command handleLldp(LLDP lldp, long sw, OFPacketIn pi) {
+ protected Command handleLldp(LLDP lldp, long sw, OFPacketIn pi) {
// If LLDP is suppressed on this port, ignore received packet as well
IOFSwitch iofSwitch = floodlightProvider.getSwitches().get(sw);
if (iofSwitch == null) {
diff --git a/src/test/java/net/onrc/onos/core/linkdiscovery/LinkDiscoveryManagerTest.java b/src/test/java/net/onrc/onos/core/linkdiscovery/LinkDiscoveryManagerTest.java
index d6c0043..37adf6b 100644
--- a/src/test/java/net/onrc/onos/core/linkdiscovery/LinkDiscoveryManagerTest.java
+++ b/src/test/java/net/onrc/onos/core/linkdiscovery/LinkDiscoveryManagerTest.java
@@ -29,6 +29,7 @@
import net.floodlightcontroller.core.FloodlightContext;
import net.floodlightcontroller.core.IFloodlightProviderService;
+import net.floodlightcontroller.core.IListener.Command;
import net.floodlightcontroller.core.IOFSwitch;
import net.floodlightcontroller.core.module.FloodlightModuleContext;
import net.floodlightcontroller.core.test.MockThreadPoolService;
@@ -36,12 +37,15 @@
import net.floodlightcontroller.restserver.RestApiServer;
import net.floodlightcontroller.test.FloodlightTestCase;
import net.floodlightcontroller.threadpool.IThreadPoolService;
+import net.onrc.onos.core.packet.Ethernet;
+import net.onrc.onos.core.packet.OnosLldp;
import net.onrc.onos.core.registry.IControllerRegistryService;
import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
import org.openflow.protocol.OFMessage;
+import org.openflow.protocol.OFPacketIn;
import org.openflow.protocol.OFPhysicalPort;
import org.openflow.protocol.OFPortStatus;
import org.openflow.protocol.OFPortStatus.OFPortReason;
@@ -80,6 +84,7 @@
public IOFSwitch createMockSwitch(Long id) {
IOFSwitch mockSwitch = createNiceMock(IOFSwitch.class);
expect(mockSwitch.getId()).andReturn(id).anyTimes();
+ expect(mockSwitch.portEnabled(EasyMock.anyShort())).andReturn(true).anyTimes();
return mockSwitch;
}
@@ -430,4 +435,71 @@
assertEquals(srcPortState, newInfo.getSrcPortState());
assertEquals(dstPortState, newInfo.getDstPortState());
}
+
+ @Test
+ public void testHandlePortStatusForDeletePort() {
+ byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1};
+
+ LinkDiscoveryManager linkDiscovery = getTopology();
+
+ // Add a link that we can delete later during the test
+ Link lt = new Link(1L, 1, 2L, 2);
+ LinkInfo info = new LinkInfo(System.currentTimeMillis(),
+ System.currentTimeMillis(), 0, 0);
+ linkDiscovery.addOrUpdateLink(lt, info);
+
+ short portNum = 1;
+ int srcPortState = 1;
+ OFPhysicalPort srcPort = new OFPhysicalPort();
+ srcPort.setPortNumber(portNum);
+ srcPort.setHardwareAddress(macAddress);
+ srcPort.setState(srcPortState);
+
+ OFPortStatus srcPortStatus = new OFPortStatus();
+ srcPortStatus.setDesc(srcPort);
+ srcPortStatus.setReason((byte) OFPortReason.OFPPR_DELETE.ordinal());
+
+ assertNotNull(linkDiscovery.getLinks().get(lt));
+
+ // Send a delete port status for the source port, which should result
+ // in the link being deleted
+ linkDiscovery.handlePortStatus(
+ getMockFloodlightProvider().getSwitches().get(1L), srcPortStatus);
+
+ assertNull(linkDiscovery.getLinks().get(lt));
+ }
+
+ @Test
+ public void testReceive() {
+ byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1};
+
+ OnosLldp lldpPacket = new OnosLldp();
+ lldpPacket.setPort((short) 1);
+ lldpPacket.setSwitch(1L);
+ lldpPacket.setReverse(false);
+
+ Ethernet ethPacket = new Ethernet();
+ ethPacket.setEtherType(Ethernet.TYPE_LLDP);
+ ethPacket.setSourceMACAddress(macAddress);
+ ethPacket.setDestinationMACAddress(
+ LinkDiscoveryManager.LLDP_STANDARD_DST_MAC_STRING);
+ ethPacket.setPayload(lldpPacket);
+ ethPacket.setPad(true);
+
+ OFPacketIn pi = new OFPacketIn();
+ pi.setInPort((short) 2);
+ pi.setPacketData(ethPacket.serialize());
+
+ LinkDiscoveryManager linkDiscovery = getTopology();
+
+ Link expectedLink = new Link(1L, 1, 2L, 2);
+
+ assertNull(linkDiscovery.links.get(expectedLink));
+
+ // Sending in the LLDP packet should cause the link to be created
+ Command command = linkDiscovery.handleLldp(lldpPacket, 2L, pi);
+
+ assertEquals(Command.STOP, command);
+ assertNotNull(linkDiscovery.links.get(expectedLink));
+ }
}