Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.linkdiscovery; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 20 | import static org.easymock.EasyMock.anyLong; |
| 21 | import static org.easymock.EasyMock.anyObject; |
| 22 | import static org.easymock.EasyMock.anyShort; |
| 23 | import static org.easymock.EasyMock.createMock; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 24 | import static org.easymock.EasyMock.createNiceMock; |
| 25 | import static org.easymock.EasyMock.expect; |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 26 | import static org.easymock.EasyMock.expectLastCall; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 27 | import static org.easymock.EasyMock.replay; |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 28 | import static org.easymock.EasyMock.reportMatcher; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 29 | import static org.easymock.EasyMock.verify; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 31 | import java.io.IOException; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 32 | import java.util.Collections; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 33 | import java.util.HashMap; |
| 34 | import java.util.Map; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 35 | import java.util.Set; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 36 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 37 | import net.floodlightcontroller.core.FloodlightContext; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 38 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 39 | import net.floodlightcontroller.core.IListener.Command; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 40 | import net.floodlightcontroller.core.IOFSwitch; |
| 41 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 42 | import net.floodlightcontroller.core.test.MockThreadPoolService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 43 | import net.floodlightcontroller.restserver.IRestApiService; |
| 44 | import net.floodlightcontroller.restserver.RestApiServer; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 45 | import net.floodlightcontroller.test.FloodlightTestCase; |
| 46 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 47 | import net.onrc.onos.core.packet.Ethernet; |
| 48 | import net.onrc.onos.core.packet.OnosLldp; |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 49 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 50 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 51 | import org.easymock.IArgumentMatcher; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 52 | import org.junit.Before; |
| 53 | import org.junit.Test; |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 54 | import org.projectfloodlight.openflow.protocol.OFFactories; |
| 55 | import org.projectfloodlight.openflow.protocol.OFFactory; |
| 56 | import org.projectfloodlight.openflow.protocol.OFPacketIn; |
| 57 | import org.projectfloodlight.openflow.protocol.OFPacketOut; |
| 58 | import org.projectfloodlight.openflow.protocol.OFPortConfig; |
| 59 | import org.projectfloodlight.openflow.protocol.OFPortDesc; |
| 60 | import org.projectfloodlight.openflow.protocol.OFPortReason; |
| 61 | import org.projectfloodlight.openflow.protocol.OFPortState; |
| 62 | import org.projectfloodlight.openflow.protocol.OFPortStatus; |
| 63 | import org.projectfloodlight.openflow.protocol.OFVersion; |
| 64 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 65 | import org.projectfloodlight.openflow.types.MacAddress; |
| 66 | import org.projectfloodlight.openflow.types.OFPort; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 67 | |
Yuta HIGUCHI | aa132f5 | 2014-06-26 10:18:39 -0700 | [diff] [blame] | 68 | // CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 69 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 70 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 71 | */ |
| 72 | public class LinkDiscoveryManagerTest extends FloodlightTestCase { |
| 73 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 74 | private LinkDiscoveryManager ldm; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 76 | private static final Set<OFPortState> EMPTY_PORT_STATE = |
| 77 | Collections.<OFPortState>emptySet(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 78 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 79 | // Arbitrary MAC address that we can feed in to our mock objects. This |
| 80 | // value is never actually checked during the tests so it doesn't matter if |
| 81 | // all ports have the same MAC address. |
| 82 | private static final byte[] DEFAULT_MAC_ADDRESS = |
| 83 | new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1}; |
| 84 | |
| 85 | private OFFactory factory10 = OFFactories.getFactory(OFVersion.OF_10); |
| 86 | |
| 87 | /** |
| 88 | * EasyMock matcher to verify the value of the output port of a packet out. |
| 89 | * This is used to verify that the packet out messages generated by |
| 90 | * LinkDiscoveryManager contain the correct output port. |
| 91 | * |
| 92 | */ |
| 93 | private static final class PacketOutPortMatcher implements IArgumentMatcher { |
| 94 | private final int portNumber; |
| 95 | |
| 96 | public PacketOutPortMatcher(int portNumber) { |
| 97 | this.portNumber = portNumber; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 100 | @Override |
| 101 | public void appendTo(StringBuffer strBuffer) { |
| 102 | strBuffer.append("PacketOutPortMatcher failed to verify output port"); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public boolean matches(Object object) { |
| 107 | if (!(object instanceof OFPacketOut)) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | OFPacketOut po = (OFPacketOut) object; |
| 112 | |
| 113 | if (po.getActions().size() != 1 |
| 114 | || !(po.getActions().get(0) instanceof OFActionOutput)) { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | OFActionOutput action = (OFActionOutput) po.getActions().get(0); |
| 119 | |
| 120 | return action.getPort().getPortNumber() == portNumber; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 121 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 122 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 124 | /** |
| 125 | * Matcher method to match a given output port against a packet out message |
| 126 | * passed as an argument to a mock switch. |
| 127 | * |
| 128 | * @param outPort the output port to check in the packet out |
| 129 | * @return anything of type OFPacketOut |
| 130 | */ |
| 131 | private static OFPacketOut matchOutPort(int outPort) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 132 | reportMatcher(new PacketOutPortMatcher(outPort)); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 133 | return null; |
| 134 | } |
| 135 | |
| 136 | private LinkDiscoveryManager getLinkDiscoveryManager() { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 137 | return ldm; |
| 138 | } |
| 139 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 140 | private IOFSwitch createMockSwitch(Long id) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 141 | IOFSwitch mockSwitch = createNiceMock(IOFSwitch.class); |
| 142 | expect(mockSwitch.getId()).andReturn(id).anyTimes(); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 143 | expect(mockSwitch.portEnabled(anyShort())).andReturn(true).anyTimes(); |
Jonathan Hart | a213bce | 2014-08-11 15:44:07 -0700 | [diff] [blame] | 144 | expect(mockSwitch.getFactory()).andReturn(factory10).anyTimes(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 145 | return mockSwitch; |
| 146 | } |
| 147 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 148 | private OFPortDesc createMockPort(short portNumber) { |
| 149 | return createMockPortWithState(portNumber, |
| 150 | Collections.<OFPortState>emptySet()); |
| 151 | } |
| 152 | |
| 153 | private OFPortDesc createMockPortWithState(short portNumber, |
| 154 | Set<OFPortState> state) { |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 155 | OFPort ofPort = createMock(OFPort.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 156 | expect(ofPort.getShortPortNumber()).andReturn(portNumber).anyTimes(); |
| 157 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 158 | OFPortDesc ofPortDesc = createMock(OFPortDesc.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 159 | expect(ofPortDesc.getPortNo()).andReturn(ofPort).anyTimes(); |
| 160 | expect(ofPortDesc.getHwAddr()).andReturn( |
| 161 | MacAddress.of(DEFAULT_MAC_ADDRESS)).anyTimes(); |
| 162 | expect(ofPortDesc.getConfig()). |
| 163 | andReturn(Collections.<OFPortConfig>emptySet()).anyTimes(); |
| 164 | expect(ofPortDesc.getState()).andReturn(state).anyTimes(); |
| 165 | |
| 166 | replay(ofPort); |
| 167 | replay(ofPortDesc); |
| 168 | |
| 169 | return ofPortDesc; |
| 170 | } |
| 171 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 172 | @Override |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 173 | @Before |
| 174 | public void setUp() throws Exception { |
| 175 | super.setUp(); |
| 176 | FloodlightModuleContext cntx = new FloodlightModuleContext(); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 177 | ldm = new LinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 178 | MockThreadPoolService tp = new MockThreadPoolService(); |
| 179 | RestApiServer restApi = new RestApiServer(); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 180 | IControllerRegistryService registry = |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 181 | createMock(IControllerRegistryService.class); |
| 182 | expect(registry.hasControl(anyLong())).andReturn(true).anyTimes(); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 183 | replay(registry); |
| 184 | cntx.addService(IControllerRegistryService.class, registry); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 185 | cntx.addService(IRestApiService.class, restApi); |
| 186 | cntx.addService(IThreadPoolService.class, tp); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 187 | cntx.addService(ILinkDiscoveryService.class, ldm); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 188 | cntx.addService(IFloodlightProviderService.class, getMockFloodlightProvider()); |
| 189 | restApi.init(cntx); |
| 190 | tp.init(cntx); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 191 | ldm.init(cntx); |
| 192 | restApi.startUp(cntx); |
| 193 | tp.startUp(cntx); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 194 | ldm.startUp(cntx); |
| 195 | |
| 196 | IOFSwitch sw1 = createMockSwitch(1L); |
| 197 | IOFSwitch sw2 = createMockSwitch(2L); |
| 198 | Map<Long, IOFSwitch> switches = new HashMap<Long, IOFSwitch>(); |
| 199 | switches.put(1L, sw1); |
| 200 | switches.put(2L, sw2); |
| 201 | getMockFloodlightProvider().setSwitches(switches); |
| 202 | replay(sw1, sw2); |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void testAddOrUpdateLink() throws Exception { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 207 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 208 | |
| 209 | Link lt = new Link(1L, 2, 2L, 1); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 210 | long firstSeenTime = System.currentTimeMillis(); |
| 211 | LinkInfo info = new LinkInfo(firstSeenTime, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 212 | System.currentTimeMillis(), EMPTY_PORT_STATE, |
| 213 | EMPTY_PORT_STATE); |
| 214 | linkDiscovery.addOrUpdateLink(lt, info); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 215 | |
| 216 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 217 | NodePortTuple dstNpt = new NodePortTuple(2L, 1); |
| 218 | |
| 219 | // check invariants hold |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 220 | assertNotNull(linkDiscovery.switchLinks.get(lt.getSrc())); |
| 221 | assertTrue(linkDiscovery.switchLinks.get(lt.getSrc()).contains(lt)); |
| 222 | assertNotNull(linkDiscovery.portLinks.get(srcNpt)); |
| 223 | assertTrue(linkDiscovery.portLinks.get(srcNpt).contains(lt)); |
| 224 | assertNotNull(linkDiscovery.portLinks.get(dstNpt)); |
| 225 | assertTrue(linkDiscovery.portLinks.get(dstNpt).contains(lt)); |
| 226 | assertTrue(linkDiscovery.links.containsKey(lt)); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 227 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 228 | LinkInfo infoToVerify = linkDiscovery.links.get(lt); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 229 | assertEquals(firstSeenTime, infoToVerify.getFirstSeenTime()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 230 | assertEquals(EMPTY_PORT_STATE, infoToVerify.getSrcPortState()); |
| 231 | assertEquals(EMPTY_PORT_STATE, infoToVerify.getDstPortState()); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 232 | |
| 233 | // Arbitrary new port states to verify that the port state is updated |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 234 | final Set<OFPortState> newSrcPortState = |
| 235 | Collections.singleton(OFPortState.STP_BLOCK); |
| 236 | final Set<OFPortState> newDstPortState = |
| 237 | Collections.singleton(OFPortState.LINK_DOWN); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 238 | |
| 239 | // Update the last received probe timestamp and the port states |
| 240 | LinkInfo infoWithStateChange = new LinkInfo(System.currentTimeMillis(), |
| 241 | System.currentTimeMillis(), newSrcPortState, newDstPortState); |
| 242 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 243 | linkDiscovery.addOrUpdateLink(lt, infoWithStateChange); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 244 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 245 | assertNotNull(linkDiscovery.links.get(lt)); |
| 246 | infoToVerify = linkDiscovery.links.get(lt); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 247 | // First seen time should be the original time, not the second update time |
| 248 | assertEquals(firstSeenTime, infoToVerify.getFirstSeenTime()); |
| 249 | // Both port states should have been updated |
| 250 | assertEquals(newSrcPortState, infoToVerify.getSrcPortState()); |
| 251 | assertEquals(newDstPortState, infoToVerify.getDstPortState()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Test |
| 255 | public void testDeleteLink() throws Exception { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 256 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 257 | |
| 258 | Link lt = new Link(1L, 2, 2L, 1); |
| 259 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 260 | System.currentTimeMillis(), EMPTY_PORT_STATE, EMPTY_PORT_STATE); |
| 261 | linkDiscovery.addOrUpdateLink(lt, info); |
| 262 | linkDiscovery.deleteLinks(Collections.singletonList(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 263 | |
| 264 | // check invariants hold |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 265 | assertNull(linkDiscovery.switchLinks.get(lt.getSrc())); |
| 266 | assertNull(linkDiscovery.switchLinks.get(lt.getDst())); |
| 267 | assertNull(linkDiscovery.portLinks.get(lt.getSrc())); |
| 268 | assertNull(linkDiscovery.portLinks.get(lt.getDst())); |
| 269 | assertTrue(linkDiscovery.links.isEmpty()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Test |
| 273 | public void testAddOrUpdateLinkToSelf() throws Exception { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 274 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 275 | |
| 276 | Link lt = new Link(1L, 2, 2L, 3); |
| 277 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 278 | NodePortTuple dstNpt = new NodePortTuple(2L, 3); |
| 279 | |
| 280 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 281 | System.currentTimeMillis(), EMPTY_PORT_STATE, EMPTY_PORT_STATE); |
| 282 | linkDiscovery.addOrUpdateLink(lt, info); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 283 | |
| 284 | // check invariants hold |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 285 | assertNotNull(linkDiscovery.switchLinks.get(lt.getSrc())); |
| 286 | assertTrue(linkDiscovery.switchLinks.get(lt.getSrc()).contains(lt)); |
| 287 | assertNotNull(linkDiscovery.portLinks.get(srcNpt)); |
| 288 | assertTrue(linkDiscovery.portLinks.get(srcNpt).contains(lt)); |
| 289 | assertNotNull(linkDiscovery.portLinks.get(dstNpt)); |
| 290 | assertTrue(linkDiscovery.portLinks.get(dstNpt).contains(lt)); |
| 291 | assertTrue(linkDiscovery.links.containsKey(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Test |
| 295 | public void testDeleteLinkToSelf() throws Exception { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 296 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 297 | |
| 298 | Link lt = new Link(1L, 2, 1L, 3); |
| 299 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 300 | NodePortTuple dstNpt = new NodePortTuple(2L, 3); |
| 301 | |
| 302 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 303 | System.currentTimeMillis(), EMPTY_PORT_STATE, EMPTY_PORT_STATE); |
| 304 | linkDiscovery.addOrUpdateLink(lt, info); |
| 305 | linkDiscovery.deleteLinks(Collections.singletonList(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 306 | |
| 307 | // check invariants hold |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 308 | assertNull(linkDiscovery.switchLinks.get(lt.getSrc())); |
| 309 | assertNull(linkDiscovery.switchLinks.get(lt.getDst())); |
| 310 | assertNull(linkDiscovery.portLinks.get(srcNpt)); |
| 311 | assertNull(linkDiscovery.portLinks.get(dstNpt)); |
| 312 | assertTrue(linkDiscovery.links.isEmpty()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | @Test |
| 316 | public void testRemovedSwitch() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 317 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 318 | |
| 319 | Link lt = new Link(1L, 2, 2L, 1); |
| 320 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 321 | NodePortTuple dstNpt = new NodePortTuple(2L, 1); |
| 322 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 323 | System.currentTimeMillis(), EMPTY_PORT_STATE, EMPTY_PORT_STATE); |
| 324 | linkDiscovery.addOrUpdateLink(lt, info); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 325 | |
| 326 | IOFSwitch sw1 = getMockFloodlightProvider().getSwitches().get(1L); |
| 327 | IOFSwitch sw2 = getMockFloodlightProvider().getSwitches().get(2L); |
| 328 | // Mock up our expected behavior |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 329 | linkDiscovery.switchDisconnected(sw1.getId()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 330 | verify(sw1, sw2); |
| 331 | |
| 332 | // check invariants hold |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 333 | assertNull(linkDiscovery.switchLinks.get(lt.getSrc())); |
| 334 | assertNull(linkDiscovery.switchLinks.get(lt.getDst())); |
| 335 | assertNull(linkDiscovery.portLinks.get(srcNpt)); |
| 336 | assertNull(linkDiscovery.portLinks.get(dstNpt)); |
| 337 | assertTrue(linkDiscovery.links.isEmpty()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | @Test |
| 341 | public void testRemovedSwitchSelf() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 342 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 343 | IOFSwitch sw1 = createMockSwitch(1L); |
| 344 | replay(sw1); |
| 345 | Link lt = new Link(1L, 2, 1L, 3); |
| 346 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 347 | System.currentTimeMillis(), EMPTY_PORT_STATE, EMPTY_PORT_STATE); |
| 348 | linkDiscovery.addOrUpdateLink(lt, info); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 349 | |
| 350 | // Mock up our expected behavior |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 351 | linkDiscovery.switchDisconnected(sw1.getId()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 352 | |
| 353 | verify(sw1); |
| 354 | // check invariants hold |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 355 | assertNull(linkDiscovery.switchLinks.get(lt.getSrc())); |
| 356 | assertNull(linkDiscovery.portLinks.get(lt.getSrc())); |
| 357 | assertNull(linkDiscovery.portLinks.get(lt.getDst())); |
| 358 | assertTrue(linkDiscovery.links.isEmpty()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | @Test |
| 362 | public void testAddUpdateLinks() throws Exception { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 363 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 364 | |
| 365 | Link lt = new Link(1L, 1, 2L, 1); |
| 366 | NodePortTuple srcNpt = new NodePortTuple(1L, 1); |
| 367 | NodePortTuple dstNpt = new NodePortTuple(2L, 1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 368 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 369 | LinkInfo info; |
| 370 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 371 | // Setting the last LLDP reception time to be 40 seconds old, so we |
| 372 | // can use this to test that an old link times out correctly |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 373 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 374 | System.currentTimeMillis() - 40000, |
| 375 | EMPTY_PORT_STATE, EMPTY_PORT_STATE); |
| 376 | linkDiscovery.addOrUpdateLink(lt, info); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 377 | |
| 378 | // check invariants hold |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 379 | assertNotNull(linkDiscovery.switchLinks.get(lt.getSrc())); |
| 380 | assertTrue(linkDiscovery.switchLinks.get(lt.getSrc()).contains(lt)); |
| 381 | assertNotNull(linkDiscovery.portLinks.get(srcNpt)); |
| 382 | assertTrue(linkDiscovery.portLinks.get(srcNpt).contains(lt)); |
| 383 | assertNotNull(linkDiscovery.portLinks.get(dstNpt)); |
| 384 | assertTrue(linkDiscovery.portLinks.get(dstNpt).contains(lt)); |
| 385 | assertTrue(linkDiscovery.links.containsKey(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 386 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 387 | linkDiscovery.timeOutLinks(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 388 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 389 | // Setting the last LLDP reception time to be 40 seconds old, so we |
| 390 | // can use this to test that an old link times out correctly |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 391 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 392 | System.currentTimeMillis() - 40000, |
| 393 | EMPTY_PORT_STATE, EMPTY_PORT_STATE); |
| 394 | linkDiscovery.addOrUpdateLink(lt, info); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 395 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 396 | // Expect to timeout the unicast Valid Time, so the link should disappear |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 397 | linkDiscovery.timeOutLinks(); |
| 398 | assertTrue(linkDiscovery.links.get(lt) == null); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 399 | } |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * This test case verifies that LinkDiscoveryManager.sendDiscoveryMessage() |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 403 | * performs "write" operation on the specified IOFSwitch object |
| 404 | * with a LLDP packet. |
| 405 | * |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 406 | * @throws IOException |
| 407 | */ |
| 408 | @Test |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 409 | public void testSendDiscoveryMessage() throws IOException { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 410 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 411 | |
| 412 | // Mock up our expected behavior |
| 413 | IOFSwitch swTest = createMockSwitch(3L); |
| 414 | getMockFloodlightProvider().getSwitches().put(3L, swTest); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 415 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 416 | short portNum = 1; |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 417 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 418 | OFPortDesc ofPortDesc = createMockPort(portNum); |
| 419 | |
| 420 | expect(swTest.getPort(portNum)).andReturn(ofPortDesc).atLeastOnce(); |
| 421 | swTest.write(matchOutPort(portNum), |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 422 | anyObject(FloodlightContext.class)); |
| 423 | expectLastCall().times(1); |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 424 | swTest.flush(); |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 425 | expectLastCall().once(); |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 426 | replay(swTest); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 427 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 428 | linkDiscovery.sendDiscoveryMessage(3L, portNum, false); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 429 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 430 | verify(swTest); |
| 431 | } |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 432 | |
| 433 | @Test |
| 434 | public void testHandlePortStatusForNewPort() throws IOException { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 435 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 436 | |
| 437 | long dpid = 3L; |
| 438 | IOFSwitch sw = createMockSwitch(dpid); |
| 439 | getMockFloodlightProvider().getSwitches().put(dpid, sw); |
| 440 | |
| 441 | short portNum = 1; |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 442 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 443 | OFPortDesc ofPortDesc = createMockPort(portNum); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 444 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 445 | OFPortStatus portStatus = factory10.buildPortStatus() |
| 446 | .setDesc(ofPortDesc) |
| 447 | .setReason(OFPortReason.ADD) |
| 448 | .build(); |
| 449 | |
| 450 | expect(sw.getPort(portNum)).andReturn(ofPortDesc).once(); |
| 451 | |
| 452 | sw.write(matchOutPort(portNum), |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 453 | anyObject(FloodlightContext.class)); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 454 | sw.flush(); |
| 455 | |
| 456 | replay(sw); |
| 457 | |
| 458 | linkDiscovery.handlePortStatus(sw, portStatus); |
| 459 | |
| 460 | verify(sw); |
| 461 | } |
| 462 | |
| 463 | @Test |
| 464 | public void testHandlePortStatusForExistingPort() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 465 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 466 | |
| 467 | // Add a link that we can update later during the test |
| 468 | Link lt = new Link(1L, 1, 2L, 1); |
| 469 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 470 | System.currentTimeMillis(), EMPTY_PORT_STATE, EMPTY_PORT_STATE); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 471 | linkDiscovery.addOrUpdateLink(lt, info); |
| 472 | |
| 473 | short portNum = 1; |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 474 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 475 | // Arbitrary states to test state changes |
| 476 | Set<OFPortState> srcPortState = |
| 477 | Collections.singleton(OFPortState.STP_FORWARD); |
| 478 | Set<OFPortState> dstPortState = |
| 479 | Collections.singleton(OFPortState.STP_LISTEN); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 480 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 481 | OFPortDesc srcPortDesc = createMockPortWithState(portNum, srcPortState); |
| 482 | OFPortDesc dstPortDesc = createMockPortWithState(portNum, dstPortState); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 483 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 484 | OFPortStatus srcPortStatus = factory10.buildPortStatus() |
| 485 | .setDesc(srcPortDesc) |
| 486 | .setReason(OFPortReason.MODIFY) |
| 487 | .build(); |
| 488 | |
| 489 | OFPortStatus dstPortStatus = factory10.buildPortStatus() |
| 490 | .setDesc(dstPortDesc) |
| 491 | .setReason(OFPortReason.MODIFY) |
| 492 | .build(); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 493 | |
| 494 | linkDiscovery.handlePortStatus( |
| 495 | getMockFloodlightProvider().getSwitches().get(1L), srcPortStatus); |
| 496 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 497 | LinkInfo newInfo = linkDiscovery.links.get(lt); |
| 498 | assertEquals(srcPortState, newInfo.getSrcPortState()); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 499 | assertEquals(EMPTY_PORT_STATE, newInfo.getDstPortState()); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 500 | |
| 501 | linkDiscovery.handlePortStatus( |
| 502 | getMockFloodlightProvider().getSwitches().get(2L), dstPortStatus); |
| 503 | |
| 504 | newInfo = linkDiscovery.links.get(lt); |
| 505 | assertEquals(srcPortState, newInfo.getSrcPortState()); |
| 506 | assertEquals(dstPortState, newInfo.getDstPortState()); |
| 507 | } |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 508 | |
| 509 | @Test |
| 510 | public void testHandlePortStatusForDeletePort() { |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 511 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 512 | |
| 513 | // Add a link that we can delete later during the test |
| 514 | Link lt = new Link(1L, 1, 2L, 2); |
| 515 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 516 | System.currentTimeMillis(), EMPTY_PORT_STATE, EMPTY_PORT_STATE); |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 517 | linkDiscovery.addOrUpdateLink(lt, info); |
| 518 | |
| 519 | short portNum = 1; |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 520 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 521 | OFPortDesc srcPortDesc = createMockPort(portNum); |
| 522 | OFPortStatus srcPortStatus = factory10.buildPortStatus() |
| 523 | .setDesc(srcPortDesc) |
| 524 | .setReason(OFPortReason.DELETE) |
| 525 | .build(); |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 526 | |
| 527 | assertNotNull(linkDiscovery.getLinks().get(lt)); |
| 528 | |
| 529 | // Send a delete port status for the source port, which should result |
| 530 | // in the link being deleted |
| 531 | linkDiscovery.handlePortStatus( |
| 532 | getMockFloodlightProvider().getSwitches().get(1L), srcPortStatus); |
| 533 | |
| 534 | assertNull(linkDiscovery.getLinks().get(lt)); |
| 535 | } |
| 536 | |
| 537 | @Test |
| 538 | public void testReceive() { |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 539 | OnosLldp lldpPacket = new OnosLldp(); |
| 540 | lldpPacket.setPort((short) 1); |
| 541 | lldpPacket.setSwitch(1L); |
| 542 | lldpPacket.setReverse(false); |
| 543 | |
| 544 | Ethernet ethPacket = new Ethernet(); |
| 545 | ethPacket.setEtherType(Ethernet.TYPE_LLDP); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 546 | ethPacket.setSourceMACAddress(DEFAULT_MAC_ADDRESS); |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 547 | ethPacket.setDestinationMACAddress( |
| 548 | LinkDiscoveryManager.LLDP_STANDARD_DST_MAC_STRING); |
| 549 | ethPacket.setPayload(lldpPacket); |
| 550 | ethPacket.setPad(true); |
| 551 | |
Sho SHIMIZU | 107344e | 2014-08-13 16:11:53 -0700 | [diff] [blame^] | 552 | OFPacketIn pi = createMock(OFPacketIn.class); |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 553 | expect(pi.getData()).andReturn(ethPacket.serialize()).anyTimes(); |
| 554 | replay(pi); |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 555 | |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 556 | LinkDiscoveryManager linkDiscovery = getLinkDiscoveryManager(); |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 557 | |
| 558 | Link expectedLink = new Link(1L, 1, 2L, 2); |
| 559 | |
| 560 | assertNull(linkDiscovery.links.get(expectedLink)); |
| 561 | |
| 562 | // Sending in the LLDP packet should cause the link to be created |
Brian O'Connor | c67f9fa | 2014-08-07 18:17:46 -0700 | [diff] [blame] | 563 | Command command = linkDiscovery.handleLldp(lldpPacket, 2L, pi, (short) 2); |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 564 | |
| 565 | assertEquals(Command.STOP, command); |
| 566 | assertNotNull(linkDiscovery.links.get(expectedLink)); |
| 567 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 568 | } |