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 | |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 20 | import static org.easymock.EasyMock.createNiceMock; |
| 21 | import static org.easymock.EasyMock.expect; |
| 22 | import static org.easymock.EasyMock.replay; |
| 23 | import static org.easymock.EasyMock.verify; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 24 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 25 | import java.io.IOException; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 26 | import java.util.Collections; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 27 | import java.util.HashMap; |
| 28 | import java.util.Map; |
| 29 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 30 | import net.floodlightcontroller.core.FloodlightContext; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 31 | import net.floodlightcontroller.core.IFloodlightProviderService; |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 32 | import net.floodlightcontroller.core.IListener.Command; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 33 | import net.floodlightcontroller.core.IOFSwitch; |
| 34 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 35 | import net.floodlightcontroller.core.test.MockThreadPoolService; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 36 | import net.floodlightcontroller.restserver.IRestApiService; |
| 37 | import net.floodlightcontroller.restserver.RestApiServer; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 38 | import net.floodlightcontroller.test.FloodlightTestCase; |
| 39 | import net.floodlightcontroller.threadpool.IThreadPoolService; |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.packet.Ethernet; |
| 41 | import net.onrc.onos.core.packet.OnosLldp; |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 42 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 43 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 44 | import org.easymock.EasyMock; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 47 | import org.openflow.protocol.OFMessage; |
Jonathan Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 48 | import org.openflow.protocol.OFPacketIn; |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 49 | import org.openflow.protocol.OFPhysicalPort; |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 50 | import org.openflow.protocol.OFPortStatus; |
| 51 | import org.openflow.protocol.OFPortStatus.OFPortReason; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 52 | import org.slf4j.Logger; |
| 53 | import org.slf4j.LoggerFactory; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 54 | |
Yuta HIGUCHI | aa132f5 | 2014-06-26 10:18:39 -0700 | [diff] [blame] | 55 | // CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 56 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 57 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 58 | */ |
| 59 | public class LinkDiscoveryManagerTest extends FloodlightTestCase { |
| 60 | |
| 61 | private TestLinkDiscoveryManager ldm; |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 62 | protected static final Logger log = LoggerFactory.getLogger(LinkDiscoveryManagerTest.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 63 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 64 | 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 78 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 79 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 80 | 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 Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 87 | expect(mockSwitch.portEnabled(EasyMock.anyShort())).andReturn(true).anyTimes(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 88 | return mockSwitch; |
| 89 | } |
| 90 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 91 | @Override |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 92 | @Before |
| 93 | public void setUp() throws Exception { |
| 94 | super.setUp(); |
| 95 | FloodlightModuleContext cntx = new FloodlightModuleContext(); |
| 96 | ldm = new TestLinkDiscoveryManager(); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 97 | //ldm.linkDiscoveryAware = new ArrayList<ILinkDiscoveryListener>(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 98 | MockThreadPoolService tp = new MockThreadPoolService(); |
| 99 | RestApiServer restApi = new RestApiServer(); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 100 | 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 105 | cntx.addService(IRestApiService.class, restApi); |
| 106 | cntx.addService(IThreadPoolService.class, tp); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 107 | cntx.addService(ILinkDiscoveryService.class, ldm); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 108 | cntx.addService(IFloodlightProviderService.class, getMockFloodlightProvider()); |
| 109 | restApi.init(cntx); |
| 110 | tp.init(cntx); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 111 | ldm.init(cntx); |
| 112 | restApi.startUp(cntx); |
| 113 | tp.startUp(cntx); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 114 | 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 Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 130 | long firstSeenTime = System.currentTimeMillis(); |
| 131 | LinkInfo info = new LinkInfo(firstSeenTime, |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 132 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 133 | 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 Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 147 | |
| 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 170 | } |
| 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 Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 178 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 179 | topology.addOrUpdateLink(lt, info); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 180 | topology.deleteLinks(Collections.singletonList(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 181 | |
| 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 Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 199 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 200 | 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 Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 221 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 222 | topology.addOrUpdateLink(lt, info); |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 223 | topology.deleteLinks(Collections.singletonList(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 224 | |
| 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 Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 241 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 242 | 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 Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 265 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 266 | 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 286 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 287 | LinkInfo info; |
| 288 | |
| 289 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 290 | System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 291 | 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 301 | |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 302 | topology.timeOutLinks(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 303 | |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 304 | // Add a link info based on info that would be obtained from unicast LLDP |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 305 | // Setting the unicast LLDP reception time to be 40 seconds old, so we can use |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 306 | // this to test timeout after this test. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 307 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 308 | System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 309 | topology.addOrUpdateLink(lt, info); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 310 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 311 | // Expect to timeout the unicast Valid Time, so the link should disappear |
Jonathan Hart | 284e70f | 2014-07-05 12:32:51 -0700 | [diff] [blame] | 312 | topology.timeOutLinks(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 313 | assertTrue(topology.links.get(lt) == null); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 314 | } |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * This test case verifies that LinkDiscoveryManager.sendDiscoveryMessage() |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 318 | * performs "write" operation on the specified IOFSwitch object |
| 319 | * with a LLDP packet. |
| 320 | * |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 321 | * @throws IOException |
| 322 | */ |
| 323 | @Test |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 324 | public void testSendDiscoveryMessage() throws IOException { |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 325 | byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1}; |
| 326 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 327 | LinkDiscoveryManager topology = getTopology(); |
| 328 | |
| 329 | // Mock up our expected behavior |
| 330 | IOFSwitch swTest = createMockSwitch(3L); |
| 331 | getMockFloodlightProvider().getSwitches().put(3L, swTest); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 332 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 333 | short portNum = 1; |
| 334 | OFPhysicalPort ofpPort = new OFPhysicalPort(); |
| 335 | ofpPort.setPortNumber(portNum); |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 336 | ofpPort.setHardwareAddress(macAddress); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 337 | |
| 338 | /* sendDiscoverMessage() should perform the following actions on |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 339 | * IOFSwitch object |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 340 | * - getPort() with argument as "1" |
| 341 | * - write() with OFPacketOut |
| 342 | * - flush() |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 343 | */ |
| 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 HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 350 | |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 351 | topology.sendDiscoveryMessage(3L, portNum, false); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 352 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 353 | verify(swTest); |
| 354 | } |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame] | 355 | |
| 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 Hart | 0f38354 | 2014-07-09 11:38:03 -0700 | [diff] [blame] | 438 | |
| 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 Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 505 | } |