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 | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.linkdiscovery.internal; |
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; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 26 | import java.util.ArrayList; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 27 | import java.util.Collections; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 28 | import java.util.HashMap; |
| 29 | import java.util.Map; |
| 30 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 31 | import net.floodlightcontroller.core.FloodlightContext; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 32 | import net.floodlightcontroller.core.IFloodlightProviderService; |
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 | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 40 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryListener; |
| 41 | import net.onrc.onos.core.linkdiscovery.ILinkDiscoveryService; |
| 42 | import net.onrc.onos.core.linkdiscovery.Link; |
| 43 | import net.onrc.onos.core.linkdiscovery.LinkInfo; |
| 44 | import net.onrc.onos.core.linkdiscovery.NodePortTuple; |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame^] | 45 | import net.onrc.onos.core.registry.IControllerRegistryService; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 46 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 47 | import org.easymock.EasyMock; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 50 | import org.openflow.protocol.OFMessage; |
| 51 | import org.openflow.protocol.OFPhysicalPort; |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame^] | 52 | import org.openflow.protocol.OFPortStatus; |
| 53 | import org.openflow.protocol.OFPortStatus.OFPortReason; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 56 | |
Yuta HIGUCHI | aa132f5 | 2014-06-26 10:18:39 -0700 | [diff] [blame] | 57 | // CHECKSTYLE IGNORE WriteTag FOR NEXT 2 LINES |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 58 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 59 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 60 | */ |
| 61 | public class LinkDiscoveryManagerTest extends FloodlightTestCase { |
| 62 | |
| 63 | private TestLinkDiscoveryManager ldm; |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 64 | protected static final Logger log = LoggerFactory.getLogger(LinkDiscoveryManagerTest.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 65 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 66 | public class TestLinkDiscoveryManager extends LinkDiscoveryManager { |
| 67 | public boolean isSendLLDPsCalled = false; |
| 68 | public boolean isClearLinksCalled = false; |
| 69 | |
| 70 | @Override |
| 71 | protected void discoverOnAllPorts() { |
| 72 | isSendLLDPsCalled = true; |
| 73 | super.discoverOnAllPorts(); |
| 74 | } |
| 75 | |
| 76 | public void reset() { |
| 77 | isSendLLDPsCalled = false; |
| 78 | isClearLinksCalled = false; |
| 79 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 80 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 81 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 82 | public LinkDiscoveryManager getTopology() { |
| 83 | return ldm; |
| 84 | } |
| 85 | |
| 86 | public IOFSwitch createMockSwitch(Long id) { |
| 87 | IOFSwitch mockSwitch = createNiceMock(IOFSwitch.class); |
| 88 | expect(mockSwitch.getId()).andReturn(id).anyTimes(); |
| 89 | return mockSwitch; |
| 90 | } |
| 91 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 92 | @Override |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 93 | @Before |
| 94 | public void setUp() throws Exception { |
| 95 | super.setUp(); |
| 96 | FloodlightModuleContext cntx = new FloodlightModuleContext(); |
| 97 | ldm = new TestLinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 98 | ldm.linkDiscoveryAware = new ArrayList<ILinkDiscoveryListener>(); |
| 99 | MockThreadPoolService tp = new MockThreadPoolService(); |
| 100 | RestApiServer restApi = new RestApiServer(); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame^] | 101 | IControllerRegistryService registry = |
| 102 | EasyMock.createMock(IControllerRegistryService.class); |
| 103 | expect(registry.hasControl(EasyMock.anyLong())).andReturn(true).anyTimes(); |
| 104 | replay(registry); |
| 105 | cntx.addService(IControllerRegistryService.class, registry); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 106 | cntx.addService(IRestApiService.class, restApi); |
| 107 | cntx.addService(IThreadPoolService.class, tp); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 108 | cntx.addService(ILinkDiscoveryService.class, ldm); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 109 | cntx.addService(IFloodlightProviderService.class, getMockFloodlightProvider()); |
| 110 | restApi.init(cntx); |
| 111 | tp.init(cntx); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 112 | ldm.init(cntx); |
| 113 | restApi.startUp(cntx); |
| 114 | tp.startUp(cntx); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 115 | ldm.startUp(cntx); |
| 116 | |
| 117 | IOFSwitch sw1 = createMockSwitch(1L); |
| 118 | IOFSwitch sw2 = createMockSwitch(2L); |
| 119 | Map<Long, IOFSwitch> switches = new HashMap<Long, IOFSwitch>(); |
| 120 | switches.put(1L, sw1); |
| 121 | switches.put(2L, sw2); |
| 122 | getMockFloodlightProvider().setSwitches(switches); |
| 123 | replay(sw1, sw2); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testAddOrUpdateLink() throws Exception { |
| 128 | LinkDiscoveryManager topology = getTopology(); |
| 129 | |
| 130 | Link lt = new Link(1L, 2, 2L, 1); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame^] | 131 | long firstSeenTime = System.currentTimeMillis(); |
| 132 | LinkInfo info = new LinkInfo(firstSeenTime, |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 133 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 134 | topology.addOrUpdateLink(lt, info); |
| 135 | |
| 136 | |
| 137 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 138 | NodePortTuple dstNpt = new NodePortTuple(2L, 1); |
| 139 | |
| 140 | // check invariants hold |
| 141 | assertNotNull(topology.switchLinks.get(lt.getSrc())); |
| 142 | assertTrue(topology.switchLinks.get(lt.getSrc()).contains(lt)); |
| 143 | assertNotNull(topology.portLinks.get(srcNpt)); |
| 144 | assertTrue(topology.portLinks.get(srcNpt).contains(lt)); |
| 145 | assertNotNull(topology.portLinks.get(dstNpt)); |
| 146 | assertTrue(topology.portLinks.get(dstNpt).contains(lt)); |
| 147 | assertTrue(topology.links.containsKey(lt)); |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame^] | 148 | |
| 149 | LinkInfo infoToVerify = topology.links.get(lt); |
| 150 | assertEquals(firstSeenTime, infoToVerify.getFirstSeenTime()); |
| 151 | assertEquals(0, infoToVerify.getSrcPortState()); |
| 152 | assertEquals(0, infoToVerify.getDstPortState()); |
| 153 | |
| 154 | // Arbitrary new port states to verify that the port state is updated |
| 155 | final int newSrcPortState = 1; |
| 156 | final int newDstPortState = 2; |
| 157 | |
| 158 | // Update the last received probe timestamp and the port states |
| 159 | LinkInfo infoWithStateChange = new LinkInfo(System.currentTimeMillis(), |
| 160 | System.currentTimeMillis(), newSrcPortState, newDstPortState); |
| 161 | |
| 162 | topology.addOrUpdateLink(lt, infoWithStateChange); |
| 163 | |
| 164 | assertNotNull(topology.links.get(lt)); |
| 165 | infoToVerify = topology.links.get(lt); |
| 166 | // First seen time should be the original time, not the second update time |
| 167 | assertEquals(firstSeenTime, infoToVerify.getFirstSeenTime()); |
| 168 | // Both port states should have been updated |
| 169 | assertEquals(newSrcPortState, infoToVerify.getSrcPortState()); |
| 170 | assertEquals(newDstPortState, infoToVerify.getDstPortState()); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void testDeleteLink() throws Exception { |
| 175 | LinkDiscoveryManager topology = getTopology(); |
| 176 | |
| 177 | Link lt = new Link(1L, 2, 2L, 1); |
| 178 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 179 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 180 | topology.addOrUpdateLink(lt, info); |
| 181 | topology.deleteLinks(Collections.singletonList(lt), "Test"); |
| 182 | |
| 183 | // check invariants hold |
| 184 | assertNull(topology.switchLinks.get(lt.getSrc())); |
| 185 | assertNull(topology.switchLinks.get(lt.getDst())); |
| 186 | assertNull(topology.portLinks.get(lt.getSrc())); |
| 187 | assertNull(topology.portLinks.get(lt.getDst())); |
| 188 | assertTrue(topology.links.isEmpty()); |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void testAddOrUpdateLinkToSelf() throws Exception { |
| 193 | LinkDiscoveryManager topology = getTopology(); |
| 194 | |
| 195 | Link lt = new Link(1L, 2, 2L, 3); |
| 196 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 197 | NodePortTuple dstNpt = new NodePortTuple(2L, 3); |
| 198 | |
| 199 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 200 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 201 | topology.addOrUpdateLink(lt, info); |
| 202 | |
| 203 | // check invariants hold |
| 204 | assertNotNull(topology.switchLinks.get(lt.getSrc())); |
| 205 | assertTrue(topology.switchLinks.get(lt.getSrc()).contains(lt)); |
| 206 | assertNotNull(topology.portLinks.get(srcNpt)); |
| 207 | assertTrue(topology.portLinks.get(srcNpt).contains(lt)); |
| 208 | assertNotNull(topology.portLinks.get(dstNpt)); |
| 209 | assertTrue(topology.portLinks.get(dstNpt).contains(lt)); |
| 210 | assertTrue(topology.links.containsKey(lt)); |
| 211 | } |
| 212 | |
| 213 | @Test |
| 214 | public void testDeleteLinkToSelf() throws Exception { |
| 215 | LinkDiscoveryManager topology = getTopology(); |
| 216 | |
| 217 | Link lt = new Link(1L, 2, 1L, 3); |
| 218 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 219 | NodePortTuple dstNpt = new NodePortTuple(2L, 3); |
| 220 | |
| 221 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 222 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 223 | topology.addOrUpdateLink(lt, info); |
| 224 | topology.deleteLinks(Collections.singletonList(lt), "Test to self"); |
| 225 | |
| 226 | // check invariants hold |
| 227 | assertNull(topology.switchLinks.get(lt.getSrc())); |
| 228 | assertNull(topology.switchLinks.get(lt.getDst())); |
| 229 | assertNull(topology.portLinks.get(srcNpt)); |
| 230 | assertNull(topology.portLinks.get(dstNpt)); |
| 231 | assertTrue(topology.links.isEmpty()); |
| 232 | } |
| 233 | |
| 234 | @Test |
| 235 | public void testRemovedSwitch() { |
| 236 | LinkDiscoveryManager topology = getTopology(); |
| 237 | |
| 238 | Link lt = new Link(1L, 2, 2L, 1); |
| 239 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 240 | NodePortTuple dstNpt = new NodePortTuple(2L, 1); |
| 241 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 242 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 243 | topology.addOrUpdateLink(lt, info); |
| 244 | |
| 245 | IOFSwitch sw1 = getMockFloodlightProvider().getSwitches().get(1L); |
| 246 | IOFSwitch sw2 = getMockFloodlightProvider().getSwitches().get(2L); |
| 247 | // Mock up our expected behavior |
| 248 | topology.removedSwitch(sw1); |
| 249 | verify(sw1, sw2); |
| 250 | |
| 251 | // check invariants hold |
| 252 | assertNull(topology.switchLinks.get(lt.getSrc())); |
| 253 | assertNull(topology.switchLinks.get(lt.getDst())); |
| 254 | assertNull(topology.portLinks.get(srcNpt)); |
| 255 | assertNull(topology.portLinks.get(dstNpt)); |
| 256 | assertTrue(topology.links.isEmpty()); |
| 257 | } |
| 258 | |
| 259 | @Test |
| 260 | public void testRemovedSwitchSelf() { |
| 261 | LinkDiscoveryManager topology = getTopology(); |
| 262 | IOFSwitch sw1 = createMockSwitch(1L); |
| 263 | replay(sw1); |
| 264 | Link lt = new Link(1L, 2, 1L, 3); |
| 265 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 266 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 267 | topology.addOrUpdateLink(lt, info); |
| 268 | |
| 269 | // Mock up our expected behavior |
| 270 | topology.removedSwitch(sw1); |
| 271 | |
| 272 | verify(sw1); |
| 273 | // check invariants hold |
| 274 | assertNull(topology.switchLinks.get(lt.getSrc())); |
| 275 | assertNull(topology.portLinks.get(lt.getSrc())); |
| 276 | assertNull(topology.portLinks.get(lt.getDst())); |
| 277 | assertTrue(topology.links.isEmpty()); |
| 278 | } |
| 279 | |
| 280 | @Test |
| 281 | public void testAddUpdateLinks() throws Exception { |
| 282 | LinkDiscoveryManager topology = getTopology(); |
| 283 | |
| 284 | Link lt = new Link(1L, 1, 2L, 1); |
| 285 | NodePortTuple srcNpt = new NodePortTuple(1L, 1); |
| 286 | NodePortTuple dstNpt = new NodePortTuple(2L, 1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 287 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 288 | LinkInfo info; |
| 289 | |
| 290 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 291 | System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 292 | topology.addOrUpdateLink(lt, info); |
| 293 | |
| 294 | // check invariants hold |
| 295 | assertNotNull(topology.switchLinks.get(lt.getSrc())); |
| 296 | assertTrue(topology.switchLinks.get(lt.getSrc()).contains(lt)); |
| 297 | assertNotNull(topology.portLinks.get(srcNpt)); |
| 298 | assertTrue(topology.portLinks.get(srcNpt).contains(lt)); |
| 299 | assertNotNull(topology.portLinks.get(dstNpt)); |
| 300 | assertTrue(topology.portLinks.get(dstNpt).contains(lt)); |
| 301 | assertTrue(topology.links.containsKey(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 302 | |
| 303 | topology.timeoutLinks(); |
| 304 | |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 305 | // 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] | 306 | // 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^] | 307 | // this to test timeout after this test. |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 308 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 309 | System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 310 | topology.addOrUpdateLink(lt, info); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 311 | |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame^] | 312 | // Expect to timeout the unicast Valid Time, so the link should disappear |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 313 | topology.timeoutLinks(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 314 | assertTrue(topology.links.get(lt) == null); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 315 | } |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 316 | |
| 317 | /** |
| 318 | * This test case verifies that LinkDiscoveryManager.sendDiscoveryMessage() |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 319 | * performs "write" operation on the specified IOFSwitch object |
| 320 | * with a LLDP packet. |
| 321 | * |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 322 | * @throws IOException |
| 323 | */ |
| 324 | @Test |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 325 | public void testSendDiscoveryMessage() throws IOException { |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 326 | byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1}; |
| 327 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 328 | LinkDiscoveryManager topology = getTopology(); |
| 329 | |
| 330 | // Mock up our expected behavior |
| 331 | IOFSwitch swTest = createMockSwitch(3L); |
| 332 | getMockFloodlightProvider().getSwitches().put(3L, swTest); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 333 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 334 | short portNum = 1; |
| 335 | OFPhysicalPort ofpPort = new OFPhysicalPort(); |
| 336 | ofpPort.setPortNumber(portNum); |
Jonathan Hart | 299d113 | 2014-06-27 09:25:28 -0700 | [diff] [blame] | 337 | ofpPort.setHardwareAddress(macAddress); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 338 | |
| 339 | /* sendDiscoverMessage() should perform the following actions on |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 340 | * IOFSwitch object |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 341 | * - getPort() with argument as "1" |
| 342 | * - write() with OFPacketOut |
| 343 | * - flush() |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 344 | */ |
| 345 | expect(swTest.getPort(portNum)).andReturn(ofpPort).atLeastOnce(); |
| 346 | swTest.write(EasyMock.anyObject(OFMessage.class), EasyMock.anyObject(FloodlightContext.class)); |
| 347 | EasyMock.expectLastCall().times(1); |
| 348 | swTest.flush(); |
| 349 | EasyMock.expectLastCall().once(); |
| 350 | replay(swTest); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 351 | |
Jonathan Hart | ba354e0 | 2014-06-30 19:18:16 -0700 | [diff] [blame] | 352 | topology.sendDiscoveryMessage(3L, portNum, false); |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 353 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 354 | verify(swTest); |
| 355 | } |
Jonathan Hart | cd1ab17 | 2014-07-03 14:59:48 -0700 | [diff] [blame^] | 356 | |
| 357 | @Test |
| 358 | public void testHandlePortStatusForNewPort() throws IOException { |
| 359 | byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1}; |
| 360 | |
| 361 | LinkDiscoveryManager linkDiscovery = getTopology(); |
| 362 | |
| 363 | long dpid = 3L; |
| 364 | IOFSwitch sw = createMockSwitch(dpid); |
| 365 | getMockFloodlightProvider().getSwitches().put(dpid, sw); |
| 366 | |
| 367 | short portNum = 1; |
| 368 | OFPhysicalPort ofpPort = new OFPhysicalPort(); |
| 369 | ofpPort.setPortNumber(portNum); |
| 370 | ofpPort.setHardwareAddress(macAddress); |
| 371 | |
| 372 | OFPortStatus portStatus = new OFPortStatus(); |
| 373 | portStatus.setDesc(ofpPort); |
| 374 | portStatus.setReason((byte) OFPortReason.OFPPR_ADD.ordinal()); |
| 375 | |
| 376 | expect(sw.getPort(portNum)).andReturn(ofpPort).anyTimes(); |
| 377 | sw.write(EasyMock.anyObject(OFMessage.class), |
| 378 | EasyMock.anyObject(FloodlightContext.class)); |
| 379 | sw.flush(); |
| 380 | |
| 381 | replay(sw); |
| 382 | |
| 383 | linkDiscovery.handlePortStatus(sw, portStatus); |
| 384 | |
| 385 | verify(sw); |
| 386 | } |
| 387 | |
| 388 | @Test |
| 389 | public void testHandlePortStatusForExistingPort() { |
| 390 | byte[] macAddress = new byte[] {0x0, 0x0, 0x0, 0x0, 0x0, 0x1}; |
| 391 | |
| 392 | LinkDiscoveryManager linkDiscovery = getTopology(); |
| 393 | |
| 394 | // Add a link that we can update later during the test |
| 395 | Link lt = new Link(1L, 1, 2L, 1); |
| 396 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
| 397 | System.currentTimeMillis(), 0, 0); |
| 398 | linkDiscovery.addOrUpdateLink(lt, info); |
| 399 | |
| 400 | short portNum = 1; |
| 401 | // src port |
| 402 | int srcPortState = 2; |
| 403 | OFPhysicalPort srcPort = new OFPhysicalPort(); |
| 404 | srcPort.setPortNumber(portNum); |
| 405 | srcPort.setHardwareAddress(macAddress); |
| 406 | srcPort.setState(srcPortState); |
| 407 | |
| 408 | // dst port |
| 409 | int dstPortState = 4; |
| 410 | OFPhysicalPort dstPort = new OFPhysicalPort(); |
| 411 | dstPort.setPortNumber(portNum); |
| 412 | dstPort.setHardwareAddress(macAddress); |
| 413 | dstPort.setState(dstPortState); |
| 414 | |
| 415 | OFPortStatus srcPortStatus = new OFPortStatus(); |
| 416 | srcPortStatus.setDesc(srcPort); |
| 417 | srcPortStatus.setReason((byte) OFPortReason.OFPPR_MODIFY.ordinal()); |
| 418 | |
| 419 | OFPortStatus dstPortStatus = new OFPortStatus(); |
| 420 | dstPortStatus.setDesc(dstPort); |
| 421 | dstPortStatus.setReason((byte) OFPortReason.OFPPR_MODIFY.ordinal()); |
| 422 | |
| 423 | linkDiscovery.handlePortStatus( |
| 424 | getMockFloodlightProvider().getSwitches().get(1L), srcPortStatus); |
| 425 | |
| 426 | |
| 427 | LinkInfo newInfo = linkDiscovery.links.get(lt); |
| 428 | assertEquals(srcPortState, newInfo.getSrcPortState()); |
| 429 | assertEquals(0, newInfo.getDstPortState()); |
| 430 | |
| 431 | |
| 432 | linkDiscovery.handlePortStatus( |
| 433 | getMockFloodlightProvider().getSwitches().get(2L), dstPortStatus); |
| 434 | |
| 435 | newInfo = linkDiscovery.links.get(lt); |
| 436 | assertEquals(srcPortState, newInfo.getSrcPortState()); |
| 437 | assertEquals(dstPortState, newInfo.getDstPortState()); |
| 438 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 439 | } |