Priyanka B | 94395bf | 2016-05-21 18:39:46 +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 | package org.onosproject.provider.pcep.topology.impl; |
| 14 | |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 15 | import static org.onosproject.net.Link.State.ACTIVE; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 16 | |
| 17 | import java.util.Collection; |
| 18 | import java.util.HashMap; |
| 19 | import java.util.HashSet; |
| 20 | import java.util.List; |
| 21 | import java.util.Map; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import org.junit.After; |
| 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
| 27 | import org.onlab.packet.ChassisId; |
| 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onosproject.net.ConnectPoint; |
| 30 | import org.onosproject.net.DefaultDevice; |
| 31 | import org.onosproject.net.DefaultLink; |
| 32 | import org.onosproject.net.Device; |
| 33 | import org.onosproject.net.DeviceId; |
| 34 | import org.onosproject.net.Link; |
| 35 | import org.onosproject.net.MastershipRole; |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 36 | import org.onosproject.net.config.Config; |
| 37 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 40 | import org.onosproject.net.device.DeviceDescription; |
| 41 | import org.onosproject.net.device.DeviceProvider; |
| 42 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 43 | import org.onosproject.net.device.DeviceProviderService; |
| 44 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 45 | import org.onosproject.net.device.PortDescription; |
| 46 | import org.onosproject.net.device.PortStatistics; |
| 47 | import org.onosproject.net.link.LinkDescription; |
| 48 | import org.onosproject.net.link.LinkProvider; |
| 49 | import org.onosproject.net.link.LinkProviderRegistry; |
| 50 | import org.onosproject.net.link.LinkProviderService; |
| 51 | import org.onosproject.net.provider.ProviderId; |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 52 | import org.onosproject.pcep.api.DeviceCapability; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 53 | import org.onosproject.pcep.controller.ClientCapability; |
| 54 | import org.onosproject.pcep.controller.PccId; |
| 55 | import org.onosproject.pcep.controller.PcepClient; |
| 56 | import org.onosproject.pcep.controller.PcepNodeListener; |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 57 | import com.fasterxml.jackson.databind.JsonNode; |
| 58 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 59 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 60 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 61 | |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 62 | import static org.hamcrest.MatcherAssert.assertThat; |
| 63 | import static org.hamcrest.core.Is.is; |
| 64 | import static org.hamcrest.core.IsNull.nullValue; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 65 | /** |
| 66 | * Test for PCEP topology provider. |
| 67 | */ |
| 68 | public class PcepTopologyProviderTest { |
Sho SHIMIZU | ca59694 | 2016-08-18 09:41:20 -0700 | [diff] [blame^] | 69 | private static final String UNKNOWN = "unknown"; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 70 | public static ProviderId providerId = new ProviderId("l3", "foo"); |
| 71 | private final PcepClientControllerAdapter clientController = new PcepClientControllerAdapter(); |
| 72 | private final PcepTopologyProvider provider = new PcepTopologyProvider(); |
| 73 | private final MockDeviceRegistry nodeRegistry = new MockDeviceRegistry(); |
| 74 | private final PcepControllerAdapter controller = new PcepControllerAdapter(); |
| 75 | private final MockLinkRegistry linkRegistry = new MockLinkRegistry(); |
| 76 | private final MockDeviceService deviceService = new MockDeviceService(); |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 77 | private final MockNetConfigRegistryAdapter netConfigRegistry = new MockNetConfigRegistryAdapter(); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 78 | private Map<DeviceId, Device> deviceMap = new HashMap<>(); |
| 79 | |
| 80 | @Before |
| 81 | public void startUp() { |
| 82 | provider.pcepClientController = clientController; |
| 83 | provider.deviceProviderRegistry = nodeRegistry; |
| 84 | provider.linkProviderRegistry = linkRegistry; |
| 85 | provider.controller = controller; |
| 86 | provider.deviceService = deviceService; |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 87 | provider.netConfigRegistry = netConfigRegistry; |
| 88 | provider.netConfigService = netConfigRegistry; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 89 | provider.activate(); |
| 90 | } |
| 91 | |
| 92 | @After |
| 93 | public void tearDown() { |
| 94 | provider.deactivate(); |
| 95 | provider.deviceProviderRegistry = null; |
| 96 | provider.pcepClientController = null; |
| 97 | provider.linkProviderRegistry = null; |
| 98 | provider.controller = null; |
| 99 | provider.deviceService = null; |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 100 | provider.netConfigRegistry = null; |
| 101 | provider.netConfigService = null; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /* Class implement device test registry */ |
| 105 | private class MockLinkRegistry implements LinkProviderRegistry { |
| 106 | LinkProvider linkProvider; |
| 107 | Set<Link> links = new HashSet<>(); |
| 108 | |
| 109 | @Override |
| 110 | public LinkProviderService register(LinkProvider provider) { |
| 111 | this.linkProvider = provider; |
| 112 | return new MockProviderService(); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void unregister(LinkProvider provider) { |
| 117 | // TODO Auto-generated method stub |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public Set<ProviderId> getProviders() { |
| 122 | return null; |
| 123 | } |
| 124 | |
| 125 | private class MockProviderService implements LinkProviderService { |
| 126 | |
| 127 | @Override |
| 128 | public void linkDetected(LinkDescription linkDescription) { |
| 129 | links.add(DefaultLink.builder().src(linkDescription.src()) |
| 130 | .dst(linkDescription.dst()).state(ACTIVE).type(linkDescription.type()) |
| 131 | .providerId(ProviderId.NONE).build()); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void linkVanished(LinkDescription linkDescription) { |
| 136 | links.remove(DefaultLink.builder().src(linkDescription.src()) |
| 137 | .dst(linkDescription.dst()).state(ACTIVE).type(linkDescription.type()) |
| 138 | .providerId(ProviderId.NONE).build()); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public void linksVanished(ConnectPoint connectPoint) { |
| 143 | // TODO Auto-generated method stub |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void linksVanished(DeviceId deviceId) { |
| 148 | // TODO Auto-generated method stub |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public LinkProvider provider() { |
| 153 | // TODO Auto-generated method stub |
| 154 | return null; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /* Class implement device test registry */ |
| 160 | private class MockDeviceRegistry implements DeviceProviderRegistry { |
| 161 | DeviceProvider provider; |
| 162 | |
| 163 | Set<DeviceId> connected = new HashSet<>(); |
| 164 | |
| 165 | @Override |
| 166 | public DeviceProviderService register(DeviceProvider provider) { |
| 167 | this.provider = provider; |
| 168 | return new MockProviderService(); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public void unregister(DeviceProvider provider) { |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public Set<ProviderId> getProviders() { |
| 177 | return null; |
| 178 | } |
| 179 | |
| 180 | private class MockProviderService implements DeviceProviderService { |
| 181 | |
| 182 | @Override |
| 183 | public DeviceProvider provider() { |
| 184 | return null; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) { |
| 189 | connected.add(deviceId); |
| 190 | Device device = new DefaultDevice(ProviderId.NONE, deviceId, Device.Type.ROUTER, UNKNOWN, UNKNOWN, |
| 191 | UNKNOWN, UNKNOWN, new ChassisId(), deviceDescription.annotations()); |
| 192 | deviceMap.put(deviceId, device); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void deviceDisconnected(DeviceId deviceId) { |
| 197 | connected.remove(deviceId); |
| 198 | deviceMap.remove(deviceId); |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { |
| 203 | // TODO Auto-generated method stub |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) { |
| 208 | // TODO Auto-generated method stub |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) { |
| 213 | // TODO Auto-generated method stub |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) { |
| 218 | // TODO Auto-generated method stub |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /* Mock test for device service */ |
| 224 | private class MockDeviceService extends DeviceServiceAdapter { |
| 225 | @Override |
| 226 | public Device getDevice(DeviceId deviceId) { |
| 227 | return deviceMap.get(deviceId); |
| 228 | } |
| 229 | } |
| 230 | |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 231 | /* Mock test for device service */ |
| 232 | private class MockNetConfigRegistryAdapter extends NetworkConfigRegistryAdapter { |
| 233 | private ConfigFactory cfgFactory; |
| 234 | private Map<DeviceId, DeviceCapability> classConfig = new HashMap<>(); |
| 235 | |
| 236 | @Override |
| 237 | public void registerConfigFactory(ConfigFactory configFactory) { |
| 238 | cfgFactory = configFactory; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
| 243 | cfgFactory = null; |
| 244 | } |
| 245 | |
| 246 | @Override |
| 247 | public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) { |
| 248 | if (configClass == DeviceCapability.class) { |
| 249 | DeviceCapability devCap = new DeviceCapability(); |
| 250 | classConfig.put((DeviceId) subject, devCap); |
| 251 | |
| 252 | JsonNode node = new ObjectNode(new MockJsonNode()); |
| 253 | ObjectMapper mapper = new ObjectMapper(); |
| 254 | ConfigApplyDelegate delegate = new InternalApplyDelegate(); |
| 255 | devCap.init((DeviceId) subject, null, node, mapper, delegate); |
| 256 | return (C) devCap; |
| 257 | } |
| 258 | |
| 259 | return null; |
| 260 | } |
| 261 | |
| 262 | @Override |
| 263 | public <S, C extends Config<S>> void removeConfig(S subject, Class<C> configClass) { |
| 264 | classConfig.remove(subject); |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 269 | if (configClass == DeviceCapability.class) { |
| 270 | return (C) classConfig.get(subject); |
| 271 | } |
| 272 | return null; |
| 273 | } |
| 274 | |
| 275 | private class MockJsonNode extends JsonNodeFactory { |
| 276 | } |
| 277 | |
| 278 | // Auxiliary delegate to receive notifications about changes applied to |
| 279 | // the network configuration - by the apps. |
| 280 | private class InternalApplyDelegate implements ConfigApplyDelegate { |
| 281 | @Override |
| 282 | public void onApply(Config config) { |
| 283 | //configs.put(config.subject(), config.node()); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 288 | /** |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 289 | * Adds the PCEP device with SR, label stack and local label capabilities and deletes the device. |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 290 | */ |
| 291 | @Test |
| 292 | public void testPcepTopologyProviderTestAddDevice1() { |
| 293 | PcepClient pc = clientController.getClient(PccId.pccId(IpAddress.valueOf("1.1.1.1"))); |
| 294 | for (PcepNodeListener l : clientController.pcepNodeListener) { |
| 295 | pc.setCapability(new ClientCapability(true, true, false, true, true)); |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 296 | l.addDevicePcepConfig(pc); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 297 | |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 298 | DeviceId pccDeviceId = DeviceId.deviceId(String.valueOf(pc.getPccId().ipAddress())); |
| 299 | DeviceCapability deviceCap = netConfigRegistry.getConfig(pccDeviceId, DeviceCapability.class); |
| 300 | assertThat(deviceCap.srCap(), is(true)); |
| 301 | assertThat(deviceCap.labelStackCap(), is(true)); |
| 302 | assertThat(deviceCap.localLabelCap(), is(true)); |
| 303 | |
| 304 | l.deleteDevicePcepConfig(pc.getPccId()); |
| 305 | deviceCap = netConfigRegistry.getConfig(pccDeviceId, DeviceCapability.class); |
| 306 | assertThat(deviceCap, is(nullValue())); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Adds the PCEP device with SR, and local label capabilities and deletes the device. |
| 312 | */ |
| 313 | @Test |
| 314 | public void testPcepTopologyProviderTestAddDevice2() { |
| 315 | PcepClient pc = clientController.getClient(PccId.pccId(IpAddress.valueOf("1.1.1.1"))); |
| 316 | for (PcepNodeListener l : clientController.pcepNodeListener) { |
| 317 | pc.setCapability(new ClientCapability(true, true, false, false, true)); |
| 318 | l.addDevicePcepConfig(pc); |
| 319 | |
| 320 | DeviceId pccDeviceId = DeviceId.deviceId(String.valueOf(pc.getPccId().ipAddress())); |
| 321 | DeviceCapability deviceCap = netConfigRegistry.getConfig(pccDeviceId, DeviceCapability.class); |
| 322 | assertThat(deviceCap.srCap(), is(true)); |
| 323 | assertThat(deviceCap.labelStackCap(), is(false)); |
| 324 | assertThat(deviceCap.localLabelCap(), is(true)); |
| 325 | |
| 326 | l.deleteDevicePcepConfig(pc.getPccId()); |
| 327 | deviceCap = netConfigRegistry.getConfig(pccDeviceId, DeviceCapability.class); |
| 328 | assertThat(deviceCap, is(nullValue())); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | } |