sunish vk | 7bdf4d4 | 2016-06-24 12:29:43 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | * the License. You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | * specific language governing permissions and limitations under the License. |
| 12 | */ |
| 13 | |
| 14 | package org.onosproject.provider.isis.topology.impl; |
| 15 | |
| 16 | import com.fasterxml.jackson.databind.JsonNode; |
| 17 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.Ip4Address; |
| 24 | import org.onlab.util.Bandwidth; |
| 25 | import org.onosproject.isis.controller.IsisController; |
| 26 | import org.onosproject.isis.controller.IsisProcess; |
| 27 | import org.onosproject.isis.controller.impl.topology.DefaultIsisLink; |
| 28 | import org.onosproject.isis.controller.impl.topology.DefaultIsisLinkTed; |
| 29 | import org.onosproject.isis.controller.impl.topology.DefaultIsisRouter; |
| 30 | import org.onosproject.isis.controller.topology.IsisLink; |
| 31 | import org.onosproject.isis.controller.topology.IsisLinkListener; |
| 32 | import org.onosproject.isis.controller.topology.IsisLinkTed; |
| 33 | import org.onosproject.isis.controller.topology.IsisRouter; |
| 34 | import org.onosproject.isis.controller.topology.IsisRouterListener; |
| 35 | import org.onosproject.net.ConnectPoint; |
| 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Link; |
| 39 | import org.onosproject.net.MastershipRole; |
| 40 | import org.onosproject.net.PortNumber; |
| 41 | import org.onosproject.net.config.Config; |
| 42 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 43 | import org.onosproject.net.config.ConfigFactory; |
| 44 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 45 | import org.onosproject.net.config.basics.BandwidthCapacity; |
| 46 | import org.onosproject.net.device.DeviceDescription; |
| 47 | import org.onosproject.net.device.DeviceListener; |
| 48 | import org.onosproject.net.device.DeviceProvider; |
| 49 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 50 | import org.onosproject.net.device.DeviceProviderService; |
| 51 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 52 | import org.onosproject.net.device.PortDescription; |
| 53 | import org.onosproject.net.device.PortStatistics; |
| 54 | import org.onosproject.net.link.LinkDescription; |
| 55 | import org.onosproject.net.link.LinkListener; |
| 56 | import org.onosproject.net.link.LinkProvider; |
| 57 | import org.onosproject.net.link.LinkProviderRegistry; |
| 58 | import org.onosproject.net.link.LinkProviderService; |
| 59 | import org.onosproject.net.link.LinkServiceAdapter; |
| 60 | import org.onosproject.net.provider.ProviderId; |
| 61 | |
| 62 | import java.util.ArrayList; |
| 63 | import java.util.Collection; |
| 64 | import java.util.Collections; |
| 65 | import java.util.HashMap; |
| 66 | import java.util.HashSet; |
| 67 | import java.util.List; |
| 68 | import java.util.Map; |
| 69 | import java.util.Set; |
| 70 | import java.util.concurrent.CopyOnWriteArraySet; |
| 71 | |
| 72 | import static org.junit.Assert.assertNotNull; |
| 73 | import static org.junit.Assert.assertTrue; |
| 74 | |
| 75 | /** |
| 76 | * Test cases for ISIS topology provider. |
| 77 | */ |
| 78 | public class IsisTopologyProviderTest { |
| 79 | |
| 80 | private final IsisTopologyProvider provider = new IsisTopologyProvider(); |
| 81 | private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry(); |
| 82 | private final TestLinkRegistry linkRegistry = new TestLinkRegistry(); |
| 83 | private final TestController controller = new TestController(); |
| 84 | private final TestLinkService linkService = new TestLinkService(); |
| 85 | private MockNetConfigRegistryAdapter networkConfigService = new MockNetConfigRegistryAdapter(); |
| 86 | |
| 87 | @Before |
| 88 | public void setUp() throws Exception { |
| 89 | provider.deviceProviderRegistry = nodeRegistry; |
| 90 | provider.linkProviderRegistry = linkRegistry; |
| 91 | provider.networkConfigService = networkConfigService; |
| 92 | provider.controller = controller; |
| 93 | provider.linkService = linkService; |
| 94 | provider.activate(); |
| 95 | assertNotNull("provider should be registered", nodeRegistry.provider); |
| 96 | assertNotNull("listener should be registered", controller.nodeListener); |
| 97 | } |
| 98 | |
| 99 | @After |
| 100 | public void tearDown() throws Exception { |
| 101 | provider.deactivate(); |
| 102 | provider.controller = null; |
| 103 | provider.deviceProviderRegistry = null; |
| 104 | provider.networkConfigService = null; |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void triggerProbe() { |
| 109 | DeviceId deviceId = DeviceId.deviceId("2929.2929.2929.00-00"); |
| 110 | provider.triggerProbe(deviceId); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void roleChanged() { |
| 115 | DeviceId deviceId = DeviceId.deviceId("1111.1111.1111.00-00"); |
| 116 | provider.roleChanged(deviceId, MastershipRole.MASTER); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void changePortState() { |
| 121 | DeviceId deviceId = DeviceId.deviceId("2222.2222.2222.00-82"); |
| 122 | provider.changePortState(deviceId, PortNumber.portNumber(168430087), false); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void isReachable() { |
| 127 | DeviceId deviceId = DeviceId.deviceId("1010.1010.1111.00-22"); |
| 128 | provider.isReachable(deviceId); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /* Validate node is added to the device validating URI and should get updated properly */ |
| 133 | @Test |
| 134 | public void isisTopologyProviderTestAddDevice1() { |
| 135 | int deviceAddCount = 0; |
| 136 | IsisRouter isisRouter = new DefaultIsisRouter(); |
| 137 | isisRouter.setSystemId("2929.2929.2929.00"); |
| 138 | isisRouter.setNeighborRouterId(Ip4Address.valueOf("10.10.10.1")); |
| 139 | isisRouter.setInterfaceId(Ip4Address.valueOf("10.10.10.2")); |
| 140 | isisRouter.setDis(false); |
| 141 | |
| 142 | for (IsisRouterListener l : controller.nodeListener) { |
| 143 | l.routerAdded(isisRouter); |
| 144 | deviceAddCount = nodeRegistry.connected.size(); |
| 145 | assertTrue(deviceAddCount == 1); |
| 146 | l.routerRemoved(isisRouter); |
| 147 | deviceAddCount = nodeRegistry.connected.size(); |
| 148 | assertTrue(deviceAddCount == 0); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | @Test |
| 153 | public void isisTopologyProviderTestAddDevice2() { |
| 154 | int deviceAddCount = 0; |
| 155 | IsisRouter isisRouter = new DefaultIsisRouter(); |
| 156 | isisRouter.setSystemId("7777.7777.7777.00"); |
| 157 | isisRouter.setNeighborRouterId(Ip4Address.valueOf("10.10.10.1")); |
| 158 | isisRouter.setInterfaceId(Ip4Address.valueOf("10.10.10.7")); |
| 159 | isisRouter.setDis(false); |
| 160 | IsisRouter isisRouter1 = new DefaultIsisRouter(); |
| 161 | isisRouter1.setSystemId("1111.1111.1111.00"); |
| 162 | isisRouter1.setNeighborRouterId(Ip4Address.valueOf("10.10.10.7")); |
| 163 | isisRouter1.setInterfaceId(Ip4Address.valueOf("10.10.10.1")); |
| 164 | isisRouter1.setDis(true); |
| 165 | for (IsisRouterListener l : controller.nodeListener) { |
| 166 | l.routerAdded(isisRouter); |
| 167 | deviceAddCount = nodeRegistry.connected.size(); |
| 168 | assertTrue(deviceAddCount == 1); |
| 169 | l.routerAdded(isisRouter1); |
| 170 | deviceAddCount = nodeRegistry.connected.size(); |
| 171 | assertTrue(deviceAddCount == 2); |
| 172 | l.routerRemoved(isisRouter); |
| 173 | deviceAddCount = nodeRegistry.connected.size(); |
| 174 | assertTrue(deviceAddCount == 1); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | @Test |
| 179 | public void isisTopologyProviderTestAddLink() { |
| 180 | int deviceAddCount = 0; |
| 181 | IsisRouter isisRouter = new DefaultIsisRouter(); |
| 182 | isisRouter.setSystemId("7777.7777.7777.00"); |
| 183 | isisRouter.setNeighborRouterId(Ip4Address.valueOf("10.10.10.1")); |
| 184 | isisRouter.setInterfaceId(Ip4Address.valueOf("10.10.10.7")); |
| 185 | isisRouter.setDis(false); |
| 186 | IsisRouter isisRouter1 = new DefaultIsisRouter(); |
| 187 | isisRouter1.setSystemId("1111.1111.1111.00"); |
| 188 | isisRouter1.setNeighborRouterId(Ip4Address.valueOf("10.10.10.7")); |
| 189 | isisRouter1.setInterfaceId(Ip4Address.valueOf("10.10.10.1")); |
| 190 | isisRouter1.setDis(true); |
| 191 | IsisLink isisLink = new DefaultIsisLink(); |
| 192 | isisLink.setRemoteSystemId("7777.7777.7777.00"); |
| 193 | isisLink.setLocalSystemId("1111.1111.1111.00"); |
| 194 | isisLink.setInterfaceIp(Ip4Address.valueOf("10.10.10.1")); |
| 195 | isisLink.setNeighborIp(Ip4Address.valueOf("10.10.10.7")); |
| 196 | IsisLinkTed isisLinkTed = new DefaultIsisLinkTed(); |
| 197 | isisLinkTed.setTeDefaultMetric(10); |
| 198 | isisLinkTed.setAdministrativeGroup(5); |
| 199 | isisLinkTed.setIpv4InterfaceAddress(Ip4Address.valueOf("10.10.10.1")); |
| 200 | isisLinkTed.setIpv4NeighborAddress(Ip4Address.valueOf("10.10.10.7")); |
| 201 | isisLinkTed.setMaximumLinkBandwidth(Bandwidth.bps(0)); |
| 202 | isisLinkTed.setMaximumReservableLinkBandwidth(Bandwidth.bps(1.0)); |
| 203 | List<Bandwidth> unresList = new ArrayList<>(); |
| 204 | unresList.add(Bandwidth.bps(0.0)); |
| 205 | unresList.add(Bandwidth.bps(1.0)); |
| 206 | unresList.add(Bandwidth.bps(2.0)); |
| 207 | unresList.add(Bandwidth.bps(3.0)); |
| 208 | isisLinkTed.setUnreservedBandwidth(unresList); |
| 209 | isisLink.setLinkTed(isisLinkTed); |
| 210 | for (IsisRouterListener l : controller.nodeListener) { |
| 211 | l.routerAdded(isisRouter); |
| 212 | deviceAddCount = nodeRegistry.connected.size(); |
| 213 | assertTrue(deviceAddCount == 1); |
| 214 | l.routerAdded(isisRouter1); |
| 215 | deviceAddCount = nodeRegistry.connected.size(); |
| 216 | assertTrue(deviceAddCount == 2); |
| 217 | } |
| 218 | for (IsisLinkListener l : controller.linkListener) { |
| 219 | l.addLink(isisLink); |
| 220 | l.deleteLink(isisLink); |
| 221 | |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | |
| 226 | /* Class implement device test registry */ |
| 227 | private class TestDeviceRegistry implements DeviceProviderRegistry { |
| 228 | DeviceProvider provider; |
| 229 | |
| 230 | Set<DeviceId> connected = new HashSet<>(); |
| 231 | |
| 232 | @Override |
| 233 | public DeviceProviderService register(DeviceProvider provider) { |
| 234 | this.provider = provider; |
| 235 | return new TestProviderService(); |
| 236 | } |
| 237 | |
| 238 | @Override |
| 239 | public void unregister(DeviceProvider provider) { |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public Set<ProviderId> getProviders() { |
| 244 | return null; |
| 245 | } |
| 246 | |
| 247 | private class TestProviderService implements DeviceProviderService { |
| 248 | |
| 249 | @Override |
| 250 | public DeviceProvider provider() { |
| 251 | return null; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { |
| 256 | |
| 257 | connected.add(deviceId); |
| 258 | |
| 259 | } |
| 260 | |
| 261 | |
| 262 | @Override |
| 263 | public void deviceDisconnected(DeviceId deviceId) { |
| 264 | |
| 265 | connected.remove(deviceId); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | @Override |
| 270 | public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { |
| 271 | // TODO Auto-generated method stub |
| 272 | |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) { |
| 277 | // TODO Auto-generated method stub |
| 278 | |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) { |
| 283 | // TODO Auto-generated method stub |
| 284 | |
| 285 | } |
| 286 | |
| 287 | |
| 288 | @Override |
| 289 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
| 290 | // TODO Auto-generated method stub |
| 291 | |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | |
| 297 | |
| 298 | private class TestDeviceService extends DeviceServiceAdapter { |
| 299 | private DeviceListener listener; |
| 300 | |
| 301 | @Override |
| 302 | public void addListener(DeviceListener listener) { |
| 303 | this.listener = listener; |
| 304 | } |
| 305 | |
| 306 | @Override |
| 307 | public Iterable<Device> getDevices() { |
| 308 | return Collections.emptyList(); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | private class TestLinkService extends LinkServiceAdapter { |
| 313 | private LinkListener listener; |
| 314 | |
| 315 | @Override |
| 316 | public void addListener(LinkListener listener) { |
| 317 | this.listener = listener; |
| 318 | } |
| 319 | |
| 320 | @Override |
| 321 | public Iterable<Link> getLinks() { |
| 322 | return Collections.emptyList(); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* Class implement device test registry */ |
| 327 | private class TestLinkRegistry implements LinkProviderRegistry { |
| 328 | LinkProvider provider; |
| 329 | |
| 330 | Set<DeviceId> connected = new HashSet<>(); |
| 331 | |
| 332 | @Override |
| 333 | public LinkProviderService register(LinkProvider provider) { |
| 334 | this.provider = provider; |
| 335 | return new TestLinkProviderService(); |
| 336 | } |
| 337 | |
| 338 | @Override |
| 339 | public void unregister(LinkProvider provider) { |
| 340 | |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public Set<ProviderId> getProviders() { |
| 345 | return null; |
| 346 | } |
| 347 | |
| 348 | private class TestLinkProviderService implements LinkProviderService { |
| 349 | |
| 350 | @Override |
| 351 | public void linkDetected(LinkDescription linkDescription) { |
| 352 | |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public void linkVanished(LinkDescription linkDescription) { |
| 357 | |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void linksVanished(ConnectPoint connectPoint) { |
| 362 | |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public void linksVanished(DeviceId deviceId) { |
| 367 | |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public LinkProvider provider() { |
| 372 | return null; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /* class implement test controller */ |
| 378 | private class TestController implements IsisController { |
| 379 | protected Set<IsisRouterListener> nodeListener = new CopyOnWriteArraySet<>(); |
| 380 | protected Set<IsisLinkListener> linkListener = new CopyOnWriteArraySet<>(); |
| 381 | |
| 382 | @Override |
| 383 | public void addRouterListener(IsisRouterListener nodeListener) { |
| 384 | this.nodeListener.add(nodeListener); |
| 385 | } |
| 386 | |
| 387 | @Override |
| 388 | public void removeRouterListener(IsisRouterListener nodeListener) { |
| 389 | this.nodeListener.remove(nodeListener); |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public void addLinkListener(IsisLinkListener listener) { |
| 394 | this.linkListener.add(listener); |
| 395 | } |
| 396 | |
| 397 | @Override |
| 398 | public void removeLinkListener(IsisLinkListener listener) { |
| 399 | this.linkListener.remove(listener); |
| 400 | } |
| 401 | |
| 402 | @Override |
| 403 | public void updateConfig(JsonNode processesNode) { |
| 404 | |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public List<IsisProcess> allConfiguredProcesses() { |
| 409 | return null; |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public Set<IsisRouterListener> listener() { |
| 414 | return null; |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public Set<IsisLinkListener> linkListener() { |
| 419 | return null; |
| 420 | } |
| 421 | |
| 422 | } |
| 423 | |
| 424 | /* Mock test for device service */ |
| 425 | private class MockNetConfigRegistryAdapter extends NetworkConfigRegistryAdapter { |
| 426 | private ConfigFactory cfgFactory; |
| 427 | private Map<ConnectPoint, BandwidthCapacity> classConfig = new HashMap<>(); |
| 428 | |
| 429 | @Override |
| 430 | public void registerConfigFactory(ConfigFactory configFactory) { |
| 431 | cfgFactory = configFactory; |
| 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
| 436 | cfgFactory = null; |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) { |
| 441 | if (configClass == BandwidthCapacity.class) { |
| 442 | BandwidthCapacity devCap = new BandwidthCapacity(); |
| 443 | classConfig.put((ConnectPoint) subject, devCap); |
| 444 | |
| 445 | JsonNode node = new ObjectNode(new MockJsonNode()); |
| 446 | ObjectMapper mapper = new ObjectMapper(); |
| 447 | ConfigApplyDelegate delegate = new InternalApplyDelegate(); |
| 448 | devCap.init((ConnectPoint) subject, null, node, mapper, delegate); |
| 449 | return (C) devCap; |
| 450 | } |
| 451 | |
| 452 | return null; |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | public <S, C extends Config<S>> void removeConfig(S subject, Class<C> configClass) { |
| 457 | classConfig.remove(subject); |
| 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 462 | if (configClass == BandwidthCapacity.class) { |
| 463 | return (C) classConfig.get(subject); |
| 464 | } |
| 465 | return null; |
| 466 | } |
| 467 | |
| 468 | private class MockJsonNode extends JsonNodeFactory { |
| 469 | } |
| 470 | |
| 471 | // Auxiliary delegate to receive notifications about changes applied to |
| 472 | // the network configuration - by the apps. |
| 473 | private class InternalApplyDelegate implements ConfigApplyDelegate { |
| 474 | @Override |
| 475 | public void onApply(Config config) { |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | } |