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