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 | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 45 | |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 46 | import org.easymock.EasyMock; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 47 | import org.junit.Before; |
| 48 | import org.junit.Test; |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 49 | import org.openflow.protocol.OFMessage; |
| 50 | import org.openflow.protocol.OFPhysicalPort; |
Jonathan Hart | 2fa2806 | 2013-11-25 20:16:28 -0800 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | import org.slf4j.LoggerFactory; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 55 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 56 | */ |
| 57 | public class LinkDiscoveryManagerTest extends FloodlightTestCase { |
| 58 | |
| 59 | private TestLinkDiscoveryManager ldm; |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 60 | protected static final Logger log = LoggerFactory.getLogger(LinkDiscoveryManagerTest.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 62 | public class TestLinkDiscoveryManager extends LinkDiscoveryManager { |
| 63 | public boolean isSendLLDPsCalled = false; |
| 64 | public boolean isClearLinksCalled = false; |
| 65 | |
| 66 | @Override |
| 67 | protected void discoverOnAllPorts() { |
| 68 | isSendLLDPsCalled = true; |
| 69 | super.discoverOnAllPorts(); |
| 70 | } |
| 71 | |
| 72 | public void reset() { |
| 73 | isSendLLDPsCalled = false; |
| 74 | isClearLinksCalled = false; |
| 75 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 76 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 78 | public LinkDiscoveryManager getTopology() { |
| 79 | return ldm; |
| 80 | } |
| 81 | |
| 82 | public IOFSwitch createMockSwitch(Long id) { |
| 83 | IOFSwitch mockSwitch = createNiceMock(IOFSwitch.class); |
| 84 | expect(mockSwitch.getId()).andReturn(id).anyTimes(); |
| 85 | return mockSwitch; |
| 86 | } |
| 87 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 88 | @Override |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 89 | @Before |
| 90 | public void setUp() throws Exception { |
| 91 | super.setUp(); |
| 92 | FloodlightModuleContext cntx = new FloodlightModuleContext(); |
| 93 | ldm = new TestLinkDiscoveryManager(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 94 | ldm.linkDiscoveryAware = new ArrayList<ILinkDiscoveryListener>(); |
| 95 | MockThreadPoolService tp = new MockThreadPoolService(); |
| 96 | RestApiServer restApi = new RestApiServer(); |
| 97 | cntx.addService(IRestApiService.class, restApi); |
| 98 | cntx.addService(IThreadPoolService.class, tp); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 99 | cntx.addService(ILinkDiscoveryService.class, ldm); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 100 | cntx.addService(IFloodlightProviderService.class, getMockFloodlightProvider()); |
| 101 | restApi.init(cntx); |
| 102 | tp.init(cntx); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 103 | ldm.init(cntx); |
| 104 | restApi.startUp(cntx); |
| 105 | tp.startUp(cntx); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 106 | ldm.startUp(cntx); |
| 107 | |
| 108 | IOFSwitch sw1 = createMockSwitch(1L); |
| 109 | IOFSwitch sw2 = createMockSwitch(2L); |
| 110 | Map<Long, IOFSwitch> switches = new HashMap<Long, IOFSwitch>(); |
| 111 | switches.put(1L, sw1); |
| 112 | switches.put(2L, sw2); |
| 113 | getMockFloodlightProvider().setSwitches(switches); |
| 114 | replay(sw1, sw2); |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | public void testAddOrUpdateLink() throws Exception { |
| 119 | LinkDiscoveryManager topology = getTopology(); |
| 120 | |
| 121 | Link lt = new Link(1L, 2, 2L, 1); |
| 122 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | System.currentTimeMillis(), null, |
| 124 | 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 125 | topology.addOrUpdateLink(lt, info); |
| 126 | |
| 127 | |
| 128 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 129 | NodePortTuple dstNpt = new NodePortTuple(2L, 1); |
| 130 | |
| 131 | // check invariants hold |
| 132 | assertNotNull(topology.switchLinks.get(lt.getSrc())); |
| 133 | assertTrue(topology.switchLinks.get(lt.getSrc()).contains(lt)); |
| 134 | assertNotNull(topology.portLinks.get(srcNpt)); |
| 135 | assertTrue(topology.portLinks.get(srcNpt).contains(lt)); |
| 136 | assertNotNull(topology.portLinks.get(dstNpt)); |
| 137 | assertTrue(topology.portLinks.get(dstNpt).contains(lt)); |
| 138 | assertTrue(topology.links.containsKey(lt)); |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void testDeleteLink() throws Exception { |
| 143 | LinkDiscoveryManager topology = getTopology(); |
| 144 | |
| 145 | Link lt = new Link(1L, 2, 2L, 1); |
| 146 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 147 | System.currentTimeMillis(), null, |
| 148 | 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 149 | topology.addOrUpdateLink(lt, info); |
| 150 | topology.deleteLinks(Collections.singletonList(lt), "Test"); |
| 151 | |
| 152 | // check invariants hold |
| 153 | assertNull(topology.switchLinks.get(lt.getSrc())); |
| 154 | assertNull(topology.switchLinks.get(lt.getDst())); |
| 155 | assertNull(topology.portLinks.get(lt.getSrc())); |
| 156 | assertNull(topology.portLinks.get(lt.getDst())); |
| 157 | assertTrue(topology.links.isEmpty()); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void testAddOrUpdateLinkToSelf() throws Exception { |
| 162 | LinkDiscoveryManager topology = getTopology(); |
| 163 | |
| 164 | Link lt = new Link(1L, 2, 2L, 3); |
| 165 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 166 | NodePortTuple dstNpt = new NodePortTuple(2L, 3); |
| 167 | |
| 168 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 169 | System.currentTimeMillis(), null, |
| 170 | 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 171 | topology.addOrUpdateLink(lt, info); |
| 172 | |
| 173 | // check invariants hold |
| 174 | assertNotNull(topology.switchLinks.get(lt.getSrc())); |
| 175 | assertTrue(topology.switchLinks.get(lt.getSrc()).contains(lt)); |
| 176 | assertNotNull(topology.portLinks.get(srcNpt)); |
| 177 | assertTrue(topology.portLinks.get(srcNpt).contains(lt)); |
| 178 | assertNotNull(topology.portLinks.get(dstNpt)); |
| 179 | assertTrue(topology.portLinks.get(dstNpt).contains(lt)); |
| 180 | assertTrue(topology.links.containsKey(lt)); |
| 181 | } |
| 182 | |
| 183 | @Test |
| 184 | public void testDeleteLinkToSelf() throws Exception { |
| 185 | LinkDiscoveryManager topology = getTopology(); |
| 186 | |
| 187 | Link lt = new Link(1L, 2, 1L, 3); |
| 188 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 189 | NodePortTuple dstNpt = new NodePortTuple(2L, 3); |
| 190 | |
| 191 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 192 | System.currentTimeMillis(), null, |
| 193 | 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 194 | topology.addOrUpdateLink(lt, info); |
| 195 | topology.deleteLinks(Collections.singletonList(lt), "Test to self"); |
| 196 | |
| 197 | // check invariants hold |
| 198 | assertNull(topology.switchLinks.get(lt.getSrc())); |
| 199 | assertNull(topology.switchLinks.get(lt.getDst())); |
| 200 | assertNull(topology.portLinks.get(srcNpt)); |
| 201 | assertNull(topology.portLinks.get(dstNpt)); |
| 202 | assertTrue(topology.links.isEmpty()); |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void testRemovedSwitch() { |
| 207 | LinkDiscoveryManager topology = getTopology(); |
| 208 | |
| 209 | Link lt = new Link(1L, 2, 2L, 1); |
| 210 | NodePortTuple srcNpt = new NodePortTuple(1L, 2); |
| 211 | NodePortTuple dstNpt = new NodePortTuple(2L, 1); |
| 212 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 213 | System.currentTimeMillis(), null, |
| 214 | 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 215 | topology.addOrUpdateLink(lt, info); |
| 216 | |
| 217 | IOFSwitch sw1 = getMockFloodlightProvider().getSwitches().get(1L); |
| 218 | IOFSwitch sw2 = getMockFloodlightProvider().getSwitches().get(2L); |
| 219 | // Mock up our expected behavior |
| 220 | topology.removedSwitch(sw1); |
| 221 | verify(sw1, sw2); |
| 222 | |
| 223 | // check invariants hold |
| 224 | assertNull(topology.switchLinks.get(lt.getSrc())); |
| 225 | assertNull(topology.switchLinks.get(lt.getDst())); |
| 226 | assertNull(topology.portLinks.get(srcNpt)); |
| 227 | assertNull(topology.portLinks.get(dstNpt)); |
| 228 | assertTrue(topology.links.isEmpty()); |
| 229 | } |
| 230 | |
| 231 | @Test |
| 232 | public void testRemovedSwitchSelf() { |
| 233 | LinkDiscoveryManager topology = getTopology(); |
| 234 | IOFSwitch sw1 = createMockSwitch(1L); |
| 235 | replay(sw1); |
| 236 | Link lt = new Link(1L, 2, 1L, 3); |
| 237 | LinkInfo info = new LinkInfo(System.currentTimeMillis(), |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 238 | System.currentTimeMillis(), null, |
| 239 | 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 240 | topology.addOrUpdateLink(lt, info); |
| 241 | |
| 242 | // Mock up our expected behavior |
| 243 | topology.removedSwitch(sw1); |
| 244 | |
| 245 | verify(sw1); |
| 246 | // check invariants hold |
| 247 | assertNull(topology.switchLinks.get(lt.getSrc())); |
| 248 | assertNull(topology.portLinks.get(lt.getSrc())); |
| 249 | assertNull(topology.portLinks.get(lt.getDst())); |
| 250 | assertTrue(topology.links.isEmpty()); |
| 251 | } |
| 252 | |
| 253 | @Test |
| 254 | public void testAddUpdateLinks() throws Exception { |
| 255 | LinkDiscoveryManager topology = getTopology(); |
| 256 | |
| 257 | Link lt = new Link(1L, 1, 2L, 1); |
| 258 | NodePortTuple srcNpt = new NodePortTuple(1L, 1); |
| 259 | NodePortTuple dstNpt = new NodePortTuple(2L, 1); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 260 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 261 | LinkInfo info; |
| 262 | |
| 263 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 264 | System.currentTimeMillis() - 40000, null, |
| 265 | 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 266 | topology.addOrUpdateLink(lt, info); |
| 267 | |
| 268 | // check invariants hold |
| 269 | assertNotNull(topology.switchLinks.get(lt.getSrc())); |
| 270 | assertTrue(topology.switchLinks.get(lt.getSrc()).contains(lt)); |
| 271 | assertNotNull(topology.portLinks.get(srcNpt)); |
| 272 | assertTrue(topology.portLinks.get(srcNpt).contains(lt)); |
| 273 | assertNotNull(topology.portLinks.get(dstNpt)); |
| 274 | assertTrue(topology.portLinks.get(dstNpt).contains(lt)); |
| 275 | assertTrue(topology.links.containsKey(lt)); |
| 276 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt) == null || |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 277 | !topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 278 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt) == null || |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 279 | !topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 280 | |
| 281 | topology.timeoutLinks(); |
| 282 | |
| 283 | |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 284 | info = new LinkInfo(System.currentTimeMillis(), /* firstseen */ |
| 285 | null, /* unicast */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 286 | System.currentTimeMillis(), 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 287 | topology.addOrUpdateLink(lt, info); |
| 288 | assertTrue(topology.links.get(lt).getUnicastValidTime() == null); |
| 289 | assertTrue(topology.links.get(lt).getMulticastValidTime() != null); |
| 290 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
| 291 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
| 292 | |
| 293 | |
| 294 | // Add a link info based on info that woudld be obtained from unicast LLDP |
| 295 | // Setting the unicast LLDP reception time to be 40 seconds old, so we can use |
| 296 | // this to test timeout after this test. Although the info is initialized |
| 297 | // with LT_OPENFLOW_LINK, the link property should be changed to LT_NON_OPENFLOW |
| 298 | // by the addOrUpdateLink method. |
| 299 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 300 | System.currentTimeMillis() - 40000, null, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 301 | topology.addOrUpdateLink(lt, info); |
| 302 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt) == null || |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 303 | !topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 304 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt) == null || |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 305 | !topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 306 | |
| 307 | // Expect to timeout the unicast Valid Time, but not the multicast Valid time |
| 308 | // So the link type should go back to non-openflow link. |
| 309 | topology.timeoutLinks(); |
| 310 | assertTrue(topology.links.get(lt).getUnicastValidTime() == null); |
| 311 | assertTrue(topology.links.get(lt).getMulticastValidTime() != null); |
| 312 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
| 313 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
| 314 | |
| 315 | // Set the multicastValidTime to be old and see if that also times out. |
| 316 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 317 | null, System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 318 | topology.addOrUpdateLink(lt, info); |
| 319 | topology.timeoutLinks(); |
| 320 | assertTrue(topology.links.get(lt) == null); |
| 321 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt) == null || |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 322 | !topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 323 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt) == null || |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 324 | !topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 325 | |
| 326 | |
| 327 | // Test again only with multicast LLDP |
| 328 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 329 | null, System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 330 | topology.addOrUpdateLink(lt, info); |
| 331 | assertTrue(topology.links.get(lt).getUnicastValidTime() == null); |
| 332 | assertTrue(topology.links.get(lt).getMulticastValidTime() != null); |
| 333 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
| 334 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
| 335 | |
| 336 | // Call timeout and check if link is no longer present. |
| 337 | topology.timeoutLinks(); |
| 338 | assertTrue(topology.links.get(lt) == null); |
| 339 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt) == null || |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 340 | !topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 341 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt) == null || |
Yuta HIGUCHI | 44a0b35 | 2014-05-14 21:32:48 -0700 | [diff] [blame] | 342 | !topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 343 | |
| 344 | // Start clean and see if loops are also added. |
| 345 | lt = new Link(1L, 1, 1L, 2); |
| 346 | srcNpt = new NodePortTuple(1L, 1); |
| 347 | dstNpt = new NodePortTuple(1L, 2); |
| 348 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 349 | null, System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 350 | topology.addOrUpdateLink(lt, info); |
| 351 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
| 352 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
| 353 | |
| 354 | |
| 355 | // Start clean and see if loops are also added. |
| 356 | lt = new Link(1L, 1, 1L, 3); |
| 357 | srcNpt = new NodePortTuple(1L, 1); |
| 358 | dstNpt = new NodePortTuple(1L, 3); |
| 359 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 360 | null, System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 361 | topology.addOrUpdateLink(lt, info); |
| 362 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
| 363 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
| 364 | |
| 365 | |
| 366 | // Start clean and see if loops are also added. |
| 367 | lt = new Link(1L, 4, 1L, 5); |
| 368 | srcNpt = new NodePortTuple(1L, 4); |
| 369 | dstNpt = new NodePortTuple(1L, 5); |
| 370 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 371 | null, System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 372 | topology.addOrUpdateLink(lt, info); |
| 373 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
| 374 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
| 375 | |
| 376 | |
| 377 | // Start clean and see if loops are also added. |
| 378 | lt = new Link(1L, 3, 1L, 5); |
| 379 | srcNpt = new NodePortTuple(1L, 3); |
| 380 | dstNpt = new NodePortTuple(1L, 5); |
| 381 | info = new LinkInfo(System.currentTimeMillis() - 40000, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 382 | null, System.currentTimeMillis() - 40000, 0, 0); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 383 | topology.addOrUpdateLink(lt, info); |
| 384 | assertTrue(topology.portBroadcastDomainLinks.get(srcNpt).contains(lt)); |
| 385 | assertTrue(topology.portBroadcastDomainLinks.get(dstNpt).contains(lt)); |
| 386 | } |
srikanth | 9f38334 | 2014-06-12 11:49:00 -0700 | [diff] [blame] | 387 | |
| 388 | /** |
| 389 | * This test case verifies that LinkDiscoveryManager.sendDiscoveryMessage() |
| 390 | * performs "write" operation on the specified IOFSwitch object |
| 391 | * with a LLDP packet |
| 392 | * |
| 393 | * @throws IOException |
| 394 | */ |
| 395 | @Test |
| 396 | public void testSendDiscoveryMessage() throws IOException{ |
| 397 | LinkDiscoveryManager topology = getTopology(); |
| 398 | |
| 399 | // Mock up our expected behavior |
| 400 | IOFSwitch swTest = createMockSwitch(3L); |
| 401 | getMockFloodlightProvider().getSwitches().put(3L, swTest); |
| 402 | |
| 403 | short portNum = 1; |
| 404 | OFPhysicalPort ofpPort = new OFPhysicalPort(); |
| 405 | ofpPort.setPortNumber(portNum); |
| 406 | |
| 407 | /* sendDiscoverMessage() should perform the following actions on |
| 408 | * IOFSwitch object |
| 409 | * - getPort() with argument as "1" |
| 410 | * - write() with OFPacketOut |
| 411 | * - flush() |
| 412 | */ |
| 413 | expect(swTest.getPort(portNum)).andReturn(ofpPort).atLeastOnce(); |
| 414 | swTest.write(EasyMock.anyObject(OFMessage.class), EasyMock.anyObject(FloodlightContext.class)); |
| 415 | EasyMock.expectLastCall().times(1); |
| 416 | swTest.flush(); |
| 417 | EasyMock.expectLastCall().once(); |
| 418 | replay(swTest); |
| 419 | |
| 420 | topology.sendDiscoveryMessage(3L, portNum, true, false); |
| 421 | |
| 422 | verify(swTest); |
| 423 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 424 | } |