blob: f7a7ce1305ea98840d817f683bf4dedb0442c70f [file] [log] [blame]
Shashikanth VH1ca26ce2015-11-20 23:19:49 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Shashikanth VH1ca26ce2015-11-20 23:19:49 +05303 *
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 */
13package org.onosproject.provider.bgp.topology.impl;
14
Satish K690d5cf2017-04-04 15:12:38 +053015import com.fasterxml.jackson.databind.JsonNode;
16import com.fasterxml.jackson.databind.ObjectMapper;
17import com.fasterxml.jackson.databind.node.JsonNodeFactory;
18import com.fasterxml.jackson.databind.node.ObjectNode;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053019import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Priyanka B658aa6982016-05-27 21:34:49 +053022import org.onlab.junit.TestUtils;
23import org.onlab.junit.TestUtils.TestUtilsException;
Priyanka Bfc51c952016-03-26 14:30:33 +053024import org.onlab.packet.ChassisId;
Priyanka B658aa6982016-05-27 21:34:49 +053025import org.onlab.packet.Ip4Address;
Priyanka Bfc51c952016-03-26 14:30:33 +053026import org.onosproject.bgp.controller.BgpLinkListener;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053027import org.onosproject.bgp.controller.BgpNodeListener;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053028import org.onosproject.bgpio.exceptions.BgpParseException;
Priyanka Bfc51c952016-03-26 14:30:33 +053029import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
30import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
32import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Priyanka Bfc51c952016-03-26 14:30:33 +053033import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4.ProtocolType;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053034import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
Priyanka Bfc51c952016-03-26 14:30:33 +053035import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053036import org.onosproject.bgpio.types.AutonomousSystemTlv;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053037import org.onosproject.bgpio.types.BgpValueType;
Priyanka Bfc51c952016-03-26 14:30:33 +053038import org.onosproject.bgpio.types.IsIsNonPseudonode;
39import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv;
Satish K690d5cf2017-04-04 15:12:38 +053040import org.onosproject.bgpio.types.LinkStateAttributes;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053041import org.onosproject.bgpio.types.RouteDistinguisher;
Priyanka B658aa6982016-05-27 21:34:49 +053042import org.onosproject.bgpio.types.attr.BgpAttrNodeFlagBitTlv;
43import org.onosproject.bgpio.types.attr.BgpAttrNodeIsIsAreaId;
44import org.onosproject.bgpio.types.attr.BgpAttrRouterIdV4;
45import org.onosproject.bgpio.types.attr.BgpLinkAttrIgpMetric;
46import org.onosproject.bgpio.types.attr.BgpLinkAttrMaxLinkBandwidth;
47import org.onosproject.bgpio.types.attr.BgpLinkAttrTeDefaultMetric;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053048import org.onosproject.bgpio.util.Constants;
Priyanka B658aa6982016-05-27 21:34:49 +053049import org.onosproject.cluster.NodeId;
Satish K690d5cf2017-04-04 15:12:38 +053050import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
51import org.onosproject.incubator.net.resource.label.LabelResourceId;
52import org.onosproject.incubator.net.resource.label.LabelResourcePool;
53import org.onosproject.mastership.MastershipServiceAdapter;
Priyanka Bfc51c952016-03-26 14:30:33 +053054import org.onosproject.net.ConnectPoint;
Priyanka B4c3b4512016-07-22 11:41:49 +053055import org.onosproject.net.DefaultAnnotations;
Priyanka Bfc51c952016-03-26 14:30:33 +053056import org.onosproject.net.DefaultDevice;
57import org.onosproject.net.DefaultLink;
58import org.onosproject.net.Device;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053059import org.onosproject.net.DeviceId;
Priyanka Bfc51c952016-03-26 14:30:33 +053060import org.onosproject.net.Link;
Satish K690d5cf2017-04-04 15:12:38 +053061import org.onosproject.net.LinkKey;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053062import org.onosproject.net.MastershipRole;
Priyanka B658aa6982016-05-27 21:34:49 +053063import org.onosproject.net.PortNumber;
64import org.onosproject.net.config.Config;
65import org.onosproject.net.config.ConfigApplyDelegate;
66import org.onosproject.net.config.ConfigFactory;
67import org.onosproject.net.config.NetworkConfigRegistryAdapter;
Satish K690d5cf2017-04-04 15:12:38 +053068import org.onosproject.net.config.basics.BandwidthCapacity;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053069import org.onosproject.net.device.DeviceDescription;
Priyanka B4c3b4512016-07-22 11:41:49 +053070import org.onosproject.net.device.DeviceEvent;
71import org.onosproject.net.device.DeviceListener;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053072import org.onosproject.net.device.DeviceProvider;
73import org.onosproject.net.device.DeviceProviderRegistry;
74import org.onosproject.net.device.DeviceProviderService;
Priyanka Bfc51c952016-03-26 14:30:33 +053075import org.onosproject.net.device.DeviceServiceAdapter;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053076import org.onosproject.net.device.PortDescription;
77import org.onosproject.net.device.PortStatistics;
Priyanka Bfc51c952016-03-26 14:30:33 +053078import org.onosproject.net.link.LinkDescription;
79import org.onosproject.net.link.LinkProvider;
80import org.onosproject.net.link.LinkProviderRegistry;
81import org.onosproject.net.link.LinkProviderService;
Satish K690d5cf2017-04-04 15:12:38 +053082import org.onosproject.net.link.LinkServiceAdapter;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053083import org.onosproject.net.provider.ProviderId;
Priyanka B658aa6982016-05-27 21:34:49 +053084import org.onosproject.net.resource.Resource;
85import org.onosproject.net.resource.ResourceAdminService;
86import org.onosproject.net.resource.ResourceId;
Satish K690d5cf2017-04-04 15:12:38 +053087import org.onosproject.pcep.api.TeLinkConfig;
Priyanka B658aa6982016-05-27 21:34:49 +053088
Satish K690d5cf2017-04-04 15:12:38 +053089import java.util.Collection;
90import java.util.HashMap;
91import java.util.HashSet;
92import java.util.LinkedList;
93import java.util.List;
94import java.util.Map;
95import java.util.Set;
96import java.util.concurrent.CopyOnWriteArraySet;
97
98import static org.hamcrest.MatcherAssert.assertThat;
99import static org.hamcrest.Matchers.nullValue;
100import static org.hamcrest.core.Is.is;
101import static org.hamcrest.core.IsNot.not;
102import static org.onosproject.net.Link.State.ACTIVE;
103import static org.onosproject.net.MastershipRole.MASTER;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530104
Priyanka Bfc51c952016-03-26 14:30:33 +0530105/**
106 * Test for BGP topology provider.
107 */
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530108public class BgpTopologyProviderTest {
Priyanka B9bee0802016-04-27 22:06:02 +0530109 private static final DeviceId DID2 = DeviceId.deviceId("l3:rd=0::routinguniverse=0:asn=10");
Sho SHIMIZUca596942016-08-18 09:41:20 -0700110 private static final String UNKNOWN = "unknown";
Priyanka B9bee0802016-04-27 22:06:02 +0530111 public static ProviderId providerId = new ProviderId("l3", "foo");
Priyanka B658aa6982016-05-27 21:34:49 +0530112 private static final NodeId NODE1 = new NodeId("Master1");
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530113
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530114 private final BgpTopologyProvider provider = new BgpTopologyProvider();
115 private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry();
Priyanka Bfc51c952016-03-26 14:30:33 +0530116 private final TestLinkRegistry linkRegistry = new TestLinkRegistry();
117 private final MockBgpController controller = new MockBgpController();
118 private MockDeviceService deviceService = new MockDeviceService();
Priyanka B658aa6982016-05-27 21:34:49 +0530119 private MockLinkService linkService = new MockLinkService();
120 private MockMastershipService mastershipService = new MockMastershipService();
121 private MockNetConfigRegistryAdapter networkConfigService = new MockNetConfigRegistryAdapter();
122 private MockLabelResourceService labelResourceAdminService = new MockLabelResourceService();
Priyanka Bfc51c952016-03-26 14:30:33 +0530123 private Map<DeviceId, Device> deviceMap = new HashMap<>();
Priyanka B4c3b4512016-07-22 11:41:49 +0530124 private DeviceListener listener;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530125
126 @Before
Priyanka B658aa6982016-05-27 21:34:49 +0530127 public void startUp() throws TestUtilsException {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530128 provider.deviceProviderRegistry = nodeRegistry;
Priyanka Bfc51c952016-03-26 14:30:33 +0530129 provider.linkProviderRegistry = linkRegistry;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530130 provider.controller = controller;
Priyanka Bfc51c952016-03-26 14:30:33 +0530131 provider.deviceService = deviceService;
Priyanka B658aa6982016-05-27 21:34:49 +0530132 provider.linkService = linkService;
133 provider.labelResourceAdminService = labelResourceAdminService;
134 provider.mastershipService = mastershipService;
135 provider.networkConfigService = networkConfigService;
Priyanka B4c3b4512016-07-22 11:41:49 +0530136 listener = TestUtils.getField(provider, "deviceListener");
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530137 provider.activate();
Priyanka Bfc51c952016-03-26 14:30:33 +0530138 assertThat("device provider should be registered", not(nodeRegistry.provider));
139 assertThat("link provider should be registered", not(linkRegistry.linkProvider));
140 assertThat("node listener should be registered", not(controller.nodeListener));
141 assertThat("link listener should be registered", not(controller.linkListener));
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530142 }
143
144 @After
145 public void tearDown() {
146 provider.deactivate();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530147 provider.controller = null;
Priyanka Bfc51c952016-03-26 14:30:33 +0530148 provider.deviceService = null;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530149 provider.deviceProviderRegistry = null;
Priyanka B658aa6982016-05-27 21:34:49 +0530150 provider.linkService = null;
151 provider.mastershipService = null;
152 provider.networkConfigService = null;
153 provider.labelResourceAdminService = null;
Priyanka Bfc51c952016-03-26 14:30:33 +0530154 assertThat(controller.nodeListener, is(new HashSet<BgpNodeListener>()));
155 assertThat(controller.linkListener, is(new HashSet<BgpLinkListener>()));
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530156 }
157
Priyanka B658aa6982016-05-27 21:34:49 +0530158 private class MockLabelResourceService implements LabelResourceAdminService {
159
160 Map<DeviceId, LabelResourcePool> resourcePool = new HashMap<>();
161
162 @Override
163 public boolean createDevicePool(DeviceId deviceId, LabelResourceId beginLabel, LabelResourceId endLabel) {
164 LabelResourcePool labelResource = new LabelResourcePool(deviceId.toString(),
165 beginLabel.labelId(),
166 endLabel.labelId());
167 if (resourcePool.containsValue(labelResource)) {
168 return false;
169 }
170
171 resourcePool.put(deviceId, labelResource);
172 return true;
173 }
174
175 @Override
176 public boolean createGlobalPool(LabelResourceId beginLabel, LabelResourceId endLabel) {
177 // TODO Auto-generated method stub
178 return false;
179 }
180
181 @Override
182 public boolean destroyDevicePool(DeviceId deviceId) {
183 LabelResourcePool devicePool = resourcePool.get(deviceId);
184
185 if (devicePool == null) {
186 return false;
187 }
188
189 resourcePool.remove(deviceId);
190 return true;
191 }
192
193 @Override
194 public boolean destroyGlobalPool() {
195 // TODO Auto-generated method stub
196 return false;
197 }
198 }
199
200 /* Mock test for device service */
201 private class MockNetConfigRegistryAdapter extends NetworkConfigRegistryAdapter {
202 private ConfigFactory cfgFactory;
203 private Map<ConnectPoint, BandwidthCapacity> classConfig = new HashMap<>();
Satish K690d5cf2017-04-04 15:12:38 +0530204 private Map<LinkKey, TeLinkConfig> teLinkConfig = new HashMap<>();
205
206 public Map<LinkKey, TeLinkConfig> getTeLinkConfig() {
207 return teLinkConfig;
208 }
Priyanka B658aa6982016-05-27 21:34:49 +0530209
210 @Override
211 public void registerConfigFactory(ConfigFactory configFactory) {
212 cfgFactory = configFactory;
213 }
214
215 @Override
216 public void unregisterConfigFactory(ConfigFactory configFactory) {
217 cfgFactory = null;
218 }
219
220 @Override
221 public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
222 if (configClass == BandwidthCapacity.class) {
223 BandwidthCapacity devCap = new BandwidthCapacity();
224 classConfig.put((ConnectPoint) subject, devCap);
225
226 JsonNode node = new ObjectNode(new MockJsonNode());
227 ObjectMapper mapper = new ObjectMapper();
228 ConfigApplyDelegate delegate = new InternalApplyDelegate();
229 devCap.init((ConnectPoint) subject, null, node, mapper, delegate);
230 return (C) devCap;
Satish K690d5cf2017-04-04 15:12:38 +0530231 } else if (configClass == TeLinkConfig.class) {
232 TeLinkConfig linkConfig = new TeLinkConfig();
233 teLinkConfig.put((LinkKey) subject, linkConfig);
234
235 JsonNode node = new ObjectNode(new MockJsonNode());
236 ObjectMapper mapper = new ObjectMapper();
237 ConfigApplyDelegate delegate = new InternalApplyDelegate();
238 linkConfig.init((LinkKey) subject, null, node, mapper, delegate);
239 return (C) linkConfig;
Priyanka B658aa6982016-05-27 21:34:49 +0530240 }
241
242 return null;
243 }
244
245 @Override
246 public <S, C extends Config<S>> void removeConfig(S subject, Class<C> configClass) {
Satish K690d5cf2017-04-04 15:12:38 +0530247 if (configClass == BandwidthCapacity.class) {
248 classConfig.remove(subject);
249 } else if (configClass == TeLinkConfig.class) {
250 teLinkConfig.remove(subject);
251 }
Priyanka B658aa6982016-05-27 21:34:49 +0530252 }
253
254 @Override
255 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
256 if (configClass == BandwidthCapacity.class) {
257 return (C) classConfig.get(subject);
Satish K690d5cf2017-04-04 15:12:38 +0530258 } else if (configClass == TeLinkConfig.class) {
259 return (C) teLinkConfig.get(subject);
Priyanka B658aa6982016-05-27 21:34:49 +0530260 }
261 return null;
262 }
263
264 private class MockJsonNode extends JsonNodeFactory {
265 }
266
267 // Auxiliary delegate to receive notifications about changes applied to
268 // the network configuration - by the apps.
269 private class InternalApplyDelegate implements ConfigApplyDelegate {
270 @Override
271 public void onApply(Config config) {
272 }
273 }
274 }
275
276 private class MockMastershipService extends MastershipServiceAdapter {
277 @Override
278 public MastershipRole getLocalRole(DeviceId deviceId) {
279 return MASTER;
280 }
281
282 @Override
283 public boolean isLocalMaster(DeviceId deviceId) {
284 return getLocalRole(deviceId) == MASTER;
285 }
286
287 @Override
288 public NodeId getMasterFor(DeviceId deviceId) {
289 return NODE1;
290 }
291 }
292
293 private class MockResourceAdminService implements ResourceAdminService {
294 Map<ResourceId, List<Resource>> registeredRes = new HashMap<>();
295
296 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700297 public boolean register(List<? extends Resource> resources) {
Priyanka B658aa6982016-05-27 21:34:49 +0530298 for (Resource res : resources) {
299 List<Resource> resource = new LinkedList<>();
300 resource.add(res);
301 if (registeredRes.containsKey(res.id())) {
302 resource.addAll(registeredRes.get(res.id()));
303 }
304 registeredRes.put(res.id(), resource);
305 }
306 return true;
307 }
308
309 @Override
Sho SHIMIZUef835c92016-08-08 13:51:17 -0700310 public boolean unregister(List<? extends ResourceId> ids) {
Priyanka B658aa6982016-05-27 21:34:49 +0530311 for (ResourceId id : ids) {
312 if (registeredRes.containsKey(id)) {
313 registeredRes.remove(id);
314 } else {
315 return false;
316 }
317 }
318 return true;
319 }
320 }
321
322 private class MockLinkService extends LinkServiceAdapter {
323
324 @Override
325 public Link getLink(ConnectPoint src, ConnectPoint dst) {
326 for (Link link : linkRegistry.links) {
327 if (link.src().equals(src) && link.dst().equals(dst)) {
328 return link;
329 }
330 }
331 return null;
332 }
333 }
334
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530335 /* Class implement device test registry */
336 private class TestDeviceRegistry implements DeviceProviderRegistry {
337 DeviceProvider provider;
338
339 Set<DeviceId> connected = new HashSet<>();
Priyanka B658aa6982016-05-27 21:34:49 +0530340 Map<DeviceId, List<PortDescription>> portUpdated = new HashMap<>();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530341
342 @Override
343 public DeviceProviderService register(DeviceProvider provider) {
344 this.provider = provider;
345 return new TestProviderService();
346 }
347
348 @Override
349 public void unregister(DeviceProvider provider) {
350 }
351
352 @Override
353 public Set<ProviderId> getProviders() {
354 return null;
355 }
356
357 private class TestProviderService implements DeviceProviderService {
358
359 @Override
360 public DeviceProvider provider() {
361 return null;
362 }
363
364 @Override
365 public void deviceConnected(DeviceId deviceId, DeviceDescription deviceDescription) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530366 if (!deviceId.equals(DID2)) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530367 connected.add(deviceId);
Priyanka Bfc51c952016-03-26 14:30:33 +0530368 Device device = new DefaultDevice(BgpTopologyProviderTest.providerId, deviceId, Device.Type.ROUTER,
Priyanka B658aa6982016-05-27 21:34:49 +0530369 UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, new ChassisId(), deviceDescription.annotations());
Priyanka Bfc51c952016-03-26 14:30:33 +0530370 deviceMap.put(deviceId, device);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530371 }
372 }
373
374 @Override
375 public void deviceDisconnected(DeviceId deviceId) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530376 if (!deviceId.equals(DID2)) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530377 connected.remove(deviceId);
Priyanka Bfc51c952016-03-26 14:30:33 +0530378 deviceMap.remove(deviceId);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530379 }
380 }
381
382 @Override
383 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
Priyanka B658aa6982016-05-27 21:34:49 +0530384 portUpdated.put(deviceId, portDescriptions);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530385 }
386
387 @Override
388 public void portStatusChanged(DeviceId deviceId, PortDescription portDescription) {
389 // TODO Auto-generated method stub
390
391 }
392
393 @Override
394 public void receivedRoleReply(DeviceId deviceId, MastershipRole requested, MastershipRole response) {
395 // TODO Auto-generated method stub
396
397 }
398
399 @Override
400 public void updatePortStatistics(DeviceId deviceId, Collection<PortStatistics> portStatistics) {
401 // TODO Auto-generated method stub
402
403 }
404 }
405 }
406
Priyanka Bfc51c952016-03-26 14:30:33 +0530407 /* Class implement device test registry */
408 private class TestLinkRegistry implements LinkProviderRegistry {
409 LinkProvider linkProvider;
Satish K690d5cf2017-04-04 15:12:38 +0530410 LinkedList<Link> links = new LinkedList<>();
Priyanka Bfc51c952016-03-26 14:30:33 +0530411
412 @Override
413 public LinkProviderService register(LinkProvider provider) {
414 this.linkProvider = provider;
415 return new TestProviderService();
416 }
417
418 @Override
419 public void unregister(LinkProvider provider) {
420 // TODO Auto-generated method stub
421 }
422
423 @Override
424 public Set<ProviderId> getProviders() {
425 return null;
426 }
427
428 private class TestProviderService implements LinkProviderService {
429
430 @Override
431 public void linkDetected(LinkDescription linkDescription) {
Priyanka B658aa6982016-05-27 21:34:49 +0530432 links.add(DefaultLink.builder()
433 .src(linkDescription.src())
434 .dst(linkDescription.dst())
435 .state(ACTIVE)
436 .type(linkDescription.type())
437 .providerId(BgpTopologyProviderTest.providerId)
438 .annotations(linkDescription.annotations())
439 .build());
Priyanka Bfc51c952016-03-26 14:30:33 +0530440 }
441
442 @Override
443 public void linkVanished(LinkDescription linkDescription) {
444 links.remove(DefaultLink.builder().src(linkDescription.src())
445 .dst(linkDescription.dst()).state(ACTIVE).type(linkDescription.type())
446 .providerId(BgpTopologyProviderTest.providerId).build());
447 }
448
449 @Override
450 public void linksVanished(ConnectPoint connectPoint) {
451 // TODO Auto-generated method stub
452 }
453
454 @Override
455 public void linksVanished(DeviceId deviceId) {
456 // TODO Auto-generated method stub
457 }
458
459 @Override
460 public LinkProvider provider() {
461 // TODO Auto-generated method stub
462 return null;
463 }
464 }
465 }
466
467 /* Test class for BGP controller */
468 private class MockBgpController extends BgpControllerAdapter {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530469 protected Set<BgpNodeListener> nodeListener = new CopyOnWriteArraySet<>();
Priyanka Bfc51c952016-03-26 14:30:33 +0530470 protected Set<BgpLinkListener> linkListener = new CopyOnWriteArraySet<>();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530471
472 @Override
473 public void addListener(BgpNodeListener nodeListener) {
474 this.nodeListener.add(nodeListener);
475 }
476
477 @Override
478 public void removeListener(BgpNodeListener nodeListener) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530479 this.nodeListener.remove(nodeListener);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530480 }
481
482 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530483 public void addLinkListener(BgpLinkListener linkListener) {
484 this.linkListener.add(linkListener);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530485 }
486
487 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530488 public void removeLinkListener(BgpLinkListener linkListener) {
489 this.linkListener.remove(linkListener);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530490 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530491 }
492
Priyanka Bfc51c952016-03-26 14:30:33 +0530493 /* Mock test for device service */
494 private class MockDeviceService extends DeviceServiceAdapter {
495 @Override
496 public Device getDevice(DeviceId deviceId) {
497 return deviceMap.get(deviceId);
498 }
499 }
500
501 /**
502 * Validate node is added to the device validating URI, RIB should get updated properly.
503 */
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530504 @Test
505 public void bgpTopologyProviderTestAddDevice1() {
Priyanka Bfc51c952016-03-26 14:30:33 +0530506 LinkedList<BgpValueType> subTlvs = new LinkedList<>();
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530507 BgpValueType tlv = new AutonomousSystemTlv(100);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530508 short deslength = AutonomousSystemTlv.LENGTH;
509 short desType = AutonomousSystemTlv.TYPE;
510
511 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530512 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530513 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530514 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530515 new RouteDistinguisher());
516
Priyanka Bfc51c952016-03-26 14:30:33 +0530517 PathAttrNlriDetails details = new PathAttrNlriDetails();
518 details.setIdentifier(0);
519 details.setProtocolID(ProtocolType.DIRECT);
520 List<BgpValueType> pathAttributes = new LinkedList<>();
521 details.setPathAttribute(pathAttributes);
522
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530523 for (BgpNodeListener l : controller.nodeListener) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530524 l.addNode(nodeNlri, details);
525 assertThat(nodeRegistry.connected.size(), is(1));
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530526 l.deleteNode(nodeNlri);
Priyanka Bfc51c952016-03-26 14:30:33 +0530527 assertThat(nodeRegistry.connected.size(), is(0));
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530528 }
529 }
530
Priyanka Bfc51c952016-03-26 14:30:33 +0530531 /**
532 * Validate node is not added to the device for invalid URI, RIB count should be zero.
533 */
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530534 @Test
535 public void bgpTopologyProviderTestAddDevice2() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530536 LinkedList<BgpValueType> subTlvs;
537 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530538 short deslength = AutonomousSystemTlv.LENGTH;
539 short desType = AutonomousSystemTlv.TYPE;
540
Priyanka Bfc51c952016-03-26 14:30:33 +0530541 PathAttrNlriDetails details = new PathAttrNlriDetails();
542 details.setIdentifier(0);
543 details.setProtocolID(ProtocolType.DIRECT);
544 List<BgpValueType> pathAttributes = new LinkedList<>();
545 details.setPathAttribute(pathAttributes);
546
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530547 subTlvs = new LinkedList<>();
548 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530549 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530550 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530551 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530552 new RouteDistinguisher());
553
Priyanka Bfc51c952016-03-26 14:30:33 +0530554
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530555 for (BgpNodeListener l : controller.nodeListener) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530556 l.addNode(nodeNlri, details);
557 assertThat(nodeRegistry.connected.size(), is(0));
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530558 }
559 }
560
Priyanka Bfc51c952016-03-26 14:30:33 +0530561 /**
562 * Delete node when node does not exist, RIB count should be zero.
563 */
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530564 @Test
565 public void bgpTopologyProviderTestAddDevice3() {
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530566 LinkedList<BgpValueType> subTlvs;
567 BgpValueType tlv = new AutonomousSystemTlv(10);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530568 short deslength = AutonomousSystemTlv.LENGTH;
569 short desType = AutonomousSystemTlv.TYPE;
570
571 subTlvs = new LinkedList<>();
572 subTlvs.add(tlv);
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530573 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530574 desType));
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530575 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530576 new RouteDistinguisher());
577
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530578 for (BgpNodeListener l : controller.nodeListener) {
579 l.deleteNode(nodeNlri);
Priyanka Bfc51c952016-03-26 14:30:33 +0530580 assertThat(nodeRegistry.connected.size(), is(0));
581 }
582 }
583
Priyanka B658aa6982016-05-27 21:34:49 +0530584
585 /**
586 * Validate node is added to the device with all device annotations.
587 */
588 @Test
589 public void bgpTopologyProviderTestAddDevice4() {
590 LinkedList<BgpValueType> subTlvs = new LinkedList<>();
591 BgpValueType tlv = new AutonomousSystemTlv(100);
592 short deslength = AutonomousSystemTlv.LENGTH;
593 short desType = AutonomousSystemTlv.TYPE;
594
595 subTlvs.add(tlv);
596 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
597 desType));
598 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
599 new RouteDistinguisher());
600
601 PathAttrNlriDetails details = new PathAttrNlriDetails();
602 details.setIdentifier(0);
603 details.setProtocolID(ProtocolType.DIRECT);
604 List<BgpValueType> pathAttributes = new LinkedList<>();
605 List<BgpValueType> linkStateAttr = new LinkedList<>();
606 tlv = BgpAttrNodeFlagBitTlv.of(true, true, true, false);
607 linkStateAttr.add(tlv);
608 tlv = BgpAttrNodeIsIsAreaId.of(new byte[] {01, 01, 01, 01});
609 linkStateAttr.add(tlv);
610 tlv = BgpAttrRouterIdV4.of(Ip4Address.valueOf("1.1.1.1"), LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID);
611 linkStateAttr.add(tlv);
612 pathAttributes.add(new LinkStateAttributes(linkStateAttr));
613 details.setPathAttribute(pathAttributes);
614
615 for (BgpNodeListener l : controller.nodeListener) {
616 l.addNode(nodeNlri, details);
617
618 assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.ABR_BIT),
619 is("false"));
620 assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.EXTERNAL_BIT),
621 is("true"));
622 assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.INTERNAL_BIT),
623 is("false"));
624 assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.PSEUDO),
625 is("false"));
626 assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.AREAID).getBytes(),
627 is(new byte[] {01, 01, 01, 01}));
628 assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.LSRID),
629 is("1.1.1.1"));
630
631 assertThat(nodeRegistry.connected.size(), is(1));
632 l.deleteNode(nodeNlri);
633 assertThat(nodeRegistry.connected.size(), is(0));
634 }
635 }
636
Priyanka Bfc51c952016-03-26 14:30:33 +0530637 /**
638 * Add a link and two devices.
639 *
640 * @throws BgpParseException while adding a link.
641 */
642 @Test
643 public void bgpTopologyProviderTestAddLink1() throws BgpParseException {
644 LinkedList<BgpValueType> localTlvs = new LinkedList<>();
645 LinkedList<BgpValueType> remoteTlvs = new LinkedList<>();
646 LinkedList<BgpValueType> linkdes = new LinkedList<>();
647 BgpValueType tlv = new AutonomousSystemTlv(10);
648 short deslength = AutonomousSystemTlv.LENGTH;
649 short desType = AutonomousSystemTlv.TYPE;
650
651 localTlvs.add(tlv);
652 remoteTlvs.add(tlv);
653 tlv = IsIsNonPseudonode.of(new byte[] {20, 20, 20, 20, 00, 20});
654 localTlvs.add(tlv);
655 tlv = IsIsNonPseudonode.of(new byte[] {30, 30, 30, 30, 00, 30});
656 remoteTlvs.add(tlv);
657 NodeDescriptors localNode = new NodeDescriptors(localTlvs, deslength, desType);
658 NodeDescriptors remoteNode = new NodeDescriptors(remoteTlvs, deslength, desType);
659 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(localNode);
660 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
661 new RouteDistinguisher());
662
663 BgpNodeLSIdentifier remoteNodeDescriptors = new BgpNodeLSIdentifier(remoteNode);
664 BgpNodeLSNlriVer4 remNodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, remoteNodeDescriptors, false,
665 new RouteDistinguisher());
666
667 PathAttrNlriDetails details = new PathAttrNlriDetails();
668 details.setIdentifier(0);
669 details.setProtocolID(ProtocolType.DIRECT);
670 List<BgpValueType> pathAttributes = new LinkedList<>();
671 details.setPathAttribute(pathAttributes);
672
673 tlv = LinkLocalRemoteIdentifiersTlv.of(99, 100);
674 linkdes.add(tlv);
675 BgpLinkLSIdentifier linkId = new BgpLinkLSIdentifier(localNode, remoteNode, linkdes);
676 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4((byte) Constants.DIRECT, 0, linkId,
677 new RouteDistinguisher(), false);
678 for (BgpNodeListener l : controller.nodeListener) {
679 l.addNode(nodeNlri, details);
680 assertThat(nodeRegistry.connected.size(), is(1));
681 l.addNode(remNodeNlri, details);
682 assertThat(nodeRegistry.connected.size(), is(2));
683 }
684 for (BgpLinkListener l : controller.linkListener) {
685 l.addLink(linkNlri, details);
686 assertThat(linkRegistry.links.size(), is(1));
687 }
688 }
689
690 /**
691 * Add a link and delete a link.
692 *
693 * @throws BgpParseException while adding or removing the link
694 */
695 @Test
696 public void bgpTopologyProviderTestAddLink2() throws BgpParseException {
697 LinkedList<BgpValueType> localTlvs = new LinkedList<>();
698 LinkedList<BgpValueType> remoteTlvs = new LinkedList<>();
699 LinkedList<BgpValueType> linkdes = new LinkedList<>();
700 BgpValueType tlv = new AutonomousSystemTlv(10);
701 short deslength = AutonomousSystemTlv.LENGTH;
702 short desType = AutonomousSystemTlv.TYPE;
703
704 localTlvs.add(tlv);
705 remoteTlvs.add(tlv);
706 tlv = IsIsNonPseudonode.of(new byte[] {20, 20, 20, 20, 00, 20});
707 localTlvs.add(tlv);
708 tlv = IsIsNonPseudonode.of(new byte[] {30, 30, 30, 30, 00, 30});
709 remoteTlvs.add(tlv);
710 NodeDescriptors localNode = new NodeDescriptors(localTlvs, deslength, desType);
711 NodeDescriptors remoteNode = new NodeDescriptors(remoteTlvs, deslength, desType);
712 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(localNode);
713 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
714 new RouteDistinguisher());
715
716 BgpNodeLSIdentifier remoteNodeDescriptors = new BgpNodeLSIdentifier(remoteNode);
717 BgpNodeLSNlriVer4 remNodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, remoteNodeDescriptors, false,
718 new RouteDistinguisher());
719
720 PathAttrNlriDetails details = new PathAttrNlriDetails();
721 details.setIdentifier(0);
722 details.setProtocolID(ProtocolType.DIRECT);
723 List<BgpValueType> pathAttributes = new LinkedList<>();
724 details.setPathAttribute(pathAttributes);
725
726 tlv = LinkLocalRemoteIdentifiersTlv.of(99, 100);
727 linkdes.add(tlv);
728 BgpLinkLSIdentifier linkId = new BgpLinkLSIdentifier(localNode, remoteNode, linkdes);
729 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4((byte) Constants.DIRECT, 0, linkId,
730 new RouteDistinguisher(), false);
731 for (BgpNodeListener l : controller.nodeListener) {
732 l.addNode(nodeNlri, details);
733 assertThat(nodeRegistry.connected.size(), is(1));
734 l.addNode(remNodeNlri, details);
735 assertThat(nodeRegistry.connected.size(), is(2));
736 l.deleteNode(nodeNlri);
737 assertThat(nodeRegistry.connected.size(), is(1));
738 }
739 for (BgpLinkListener l : controller.linkListener) {
740 l.addLink(linkNlri, details);
741 assertThat(linkRegistry.links.size(), is(1));
742 l.deleteLink(linkNlri);
743 assertThat(linkRegistry.links.size(), is(0));
744 }
745 }
746
747 /**
Priyanka B658aa6982016-05-27 21:34:49 +0530748 * Add a link and delete a link with registering/unregistering bandwidth.
749 *
750 * @throws BgpParseException while adding or removing the link
751 * @throws InterruptedException while registering for bandwidth
752 */
753 @Test
754 public void bgpTopologyProviderTestAddLink3() throws BgpParseException, InterruptedException {
755 LinkedList<BgpValueType> localTlvs = new LinkedList<>();
756 LinkedList<BgpValueType> remoteTlvs = new LinkedList<>();
757 LinkedList<BgpValueType> linkdes = new LinkedList<>();
758 BgpValueType tlv = new AutonomousSystemTlv(10);
759 short deslength = AutonomousSystemTlv.LENGTH;
760 short desType = AutonomousSystemTlv.TYPE;
761
762 localTlvs.add(tlv);
763 remoteTlvs.add(tlv);
764 tlv = IsIsNonPseudonode.of(new byte[] {20, 20, 20, 20, 00, 20});
765 localTlvs.add(tlv);
766 tlv = IsIsNonPseudonode.of(new byte[] {30, 30, 30, 30, 00, 30});
767 remoteTlvs.add(tlv);
768 NodeDescriptors localNode = new NodeDescriptors(localTlvs, deslength, desType);
769 NodeDescriptors remoteNode = new NodeDescriptors(remoteTlvs, deslength, desType);
770 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(localNode);
771 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
772 new RouteDistinguisher());
773
774 BgpNodeLSIdentifier remoteNodeDescriptors = new BgpNodeLSIdentifier(remoteNode);
775 BgpNodeLSNlriVer4 remNodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, remoteNodeDescriptors, false,
776 new RouteDistinguisher());
777
778 PathAttrNlriDetails details = new PathAttrNlriDetails();
779 details.setIdentifier(0);
780 details.setProtocolID(ProtocolType.DIRECT);
781 List<BgpValueType> pathAttributes = new LinkedList<>();
782 details.setPathAttribute(pathAttributes);
783
784 tlv = LinkLocalRemoteIdentifiersTlv.of(99, 100);
785 linkdes.add(tlv);
786 BgpLinkLSIdentifier linkId = new BgpLinkLSIdentifier(localNode, remoteNode, linkdes);
787 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4((byte) Constants.DIRECT, 0, linkId,
788 new RouteDistinguisher(), false);
789
790 for (BgpNodeListener l : controller.nodeListener) {
791 l.addNode(nodeNlri, details);
792 assertThat(nodeRegistry.connected.size(), is(1));
793 l.addNode(remNodeNlri, details);
794 assertThat(nodeRegistry.connected.size(), is(2));
795 l.deleteNode(remNodeNlri);
796 assertThat(nodeRegistry.connected.size(), is(1));
797 }
798
799 List<BgpValueType> linkPathAttributes = new LinkedList<>();
800 List<BgpValueType> linkStateAttr = new LinkedList<>();
801 tlv = BgpLinkAttrIgpMetric.of(10, 4);
802 linkStateAttr.add(tlv);
803 tlv = BgpLinkAttrTeDefaultMetric.of(20);
804 linkStateAttr.add(tlv);
805 tlv = BgpLinkAttrMaxLinkBandwidth.of(30, LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH);
806 linkStateAttr.add(tlv);
807 linkPathAttributes.add(new LinkStateAttributes(linkStateAttr));
808 details.setPathAttribute(linkPathAttributes);
809
810 for (BgpLinkListener l : controller.linkListener) {
811 l.addLink(linkNlri, details);
812 assertThat(linkRegistry.links.size(), is(1));
Satish K690d5cf2017-04-04 15:12:38 +0530813 TeLinkConfig config = networkConfigService.getTeLinkConfig().get(LinkKey.linkKey(linkRegistry.links
814 .getFirst().src(), linkRegistry.links.getLast().dst()));
815
816 assertThat(config.igpCost(), is(10));
817 assertThat(config.teCost(), is(20));
Priyanka B658aa6982016-05-27 21:34:49 +0530818
819 l.deleteLink(linkNlri);
820 assertThat(linkRegistry.links.size(), is(0));
821 }
822 }
823
824 /**
Priyanka Bfc51c952016-03-26 14:30:33 +0530825 * Invalid link.
826 *
827 * @throws BgpParseException while adding or deleting a link
828 */
829 @Test
830 public void bgpTopologyProviderTestDeleteLink3() throws BgpParseException {
831 LinkedList<BgpValueType> localTlvs = new LinkedList<>();
832 LinkedList<BgpValueType> remoteTlvs = new LinkedList<>();
833 LinkedList<BgpValueType> linkdes = new LinkedList<>();
834 BgpValueType tlv = new AutonomousSystemTlv(10);
835 short deslength = AutonomousSystemTlv.LENGTH;
836 short desType = AutonomousSystemTlv.TYPE;
837
838 localTlvs.add(tlv);
839 remoteTlvs.add(tlv);
840 tlv = IsIsNonPseudonode.of(new byte[] {20, 20, 20, 20, 00, 20});
841 localTlvs.add(tlv);
842 tlv = IsIsNonPseudonode.of(new byte[] {30, 30, 30, 30, 00, 30});
843 remoteTlvs.add(tlv);
844 NodeDescriptors localNode = new NodeDescriptors(localTlvs, deslength, desType);
845 NodeDescriptors remoteNode = new NodeDescriptors(remoteTlvs, deslength, desType);
846 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(localNode);
847 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
848 new RouteDistinguisher());
849
850 BgpNodeLSIdentifier remoteNodeDescriptors = new BgpNodeLSIdentifier(remoteNode);
851 BgpNodeLSNlriVer4 remNodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, remoteNodeDescriptors, false,
852 new RouteDistinguisher());
853
854 PathAttrNlriDetails details = new PathAttrNlriDetails();
855 details.setIdentifier(0);
856 details.setProtocolID(ProtocolType.DIRECT);
857 List<BgpValueType> pathAttributes = new LinkedList<>();
858 details.setPathAttribute(pathAttributes);
859
860 tlv = LinkLocalRemoteIdentifiersTlv.of(99, 100);
861 linkdes.add(tlv);
862 BgpLinkLSIdentifier linkId = new BgpLinkLSIdentifier(localNode, remoteNode, linkdes);
863 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4((byte) Constants.DIRECT, 0, linkId,
864 new RouteDistinguisher(), false);
865 for (BgpNodeListener l : controller.nodeListener) {
866 l.addNode(nodeNlri, details);
867 l.addNode(remNodeNlri, details);
868 assertThat(nodeRegistry.connected.size(), is(2));
869 l.deleteNode(nodeNlri);
870 assertThat(nodeRegistry.connected.size(), is(1));
871 }
872 for (BgpLinkListener l : controller.linkListener) {
873 l.deleteLink(linkNlri);
874 assertThat(linkRegistry.links.size(), is(0));
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530875 }
876 }
Priyanka B658aa6982016-05-27 21:34:49 +0530877
878 /**
879 * Add device check label registration is done.
880 *
881 * @throws BgpParseException while adding a device
882 */
883 @Test
884 public void bgpTopologyProviderDeviceTestLabel1() throws BgpParseException {
885 LinkedList<BgpValueType> subTlvs = new LinkedList<>();
886 BgpValueType tlv = new AutonomousSystemTlv(100);
887 short deslength = AutonomousSystemTlv.LENGTH;
888 short desType = AutonomousSystemTlv.TYPE;
889
890 subTlvs.add(tlv);
891 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
892 desType));
893 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
894 new RouteDistinguisher());
895
896 PathAttrNlriDetails details = new PathAttrNlriDetails();
897 details.setIdentifier(0);
898 details.setProtocolID(ProtocolType.DIRECT);
899 List<BgpValueType> pathAttributes = new LinkedList<>();
900 List<BgpValueType> linkStateAttributes = new LinkedList<>();
901 tlv = BgpAttrRouterIdV4.of(Ip4Address.valueOf("1.1.1.1"), LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID);
902 linkStateAttributes.add(tlv);
903 pathAttributes.add(new LinkStateAttributes(linkStateAttributes));
904 details.setPathAttribute(pathAttributes);
905
906 for (BgpNodeListener l : controller.nodeListener) {
907 l.addNode(nodeNlri, details);
908 assertThat(nodeRegistry.connected.size(), is(1));
909 }
Priyanka B4c3b4512016-07-22 11:41:49 +0530910 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
Priyanka B658aa6982016-05-27 21:34:49 +0530911
Priyanka B4c3b4512016-07-22 11:41:49 +0530912 newBuilder.set("lsrId", "1.1.1.1");
Priyanka B658aa6982016-05-27 21:34:49 +0530913
Priyanka B4c3b4512016-07-22 11:41:49 +0530914 Device device = new DefaultDevice(BgpTopologyProviderTest.providerId, nodeRegistry.connected.iterator().next(),
915 Device.Type.ROUTER, UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, new ChassisId(), newBuilder.build());
916
917 DeviceEvent event = new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device);
Priyanka B658aa6982016-05-27 21:34:49 +0530918 listener.event(event);
919 assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(1));
920 }
921
922 /**
923 * Add device check label registration is done and delete node destroy label pool.
924 *
925 * @throws BgpParseException while adding a device
926 */
927 @Test
928 public void bgpTopologyProviderDeviceTestLabel2() throws BgpParseException {
929 LinkedList<BgpValueType> subTlvs = new LinkedList<>();
930 BgpValueType tlv = new AutonomousSystemTlv(100);
931 short deslength = AutonomousSystemTlv.LENGTH;
932 short desType = AutonomousSystemTlv.TYPE;
933
934 subTlvs.add(tlv);
935 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
936 desType));
937 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
938 new RouteDistinguisher());
939
940 PathAttrNlriDetails details = new PathAttrNlriDetails();
941 details.setIdentifier(0);
942 details.setProtocolID(ProtocolType.DIRECT);
943 List<BgpValueType> pathAttributes = new LinkedList<>();
944 List<BgpValueType> linkStateAttributes = new LinkedList<>();
945 tlv = BgpAttrRouterIdV4.of(Ip4Address.valueOf("1.1.1.1"), LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID);
946 linkStateAttributes.add(tlv);
947 pathAttributes.add(new LinkStateAttributes(linkStateAttributes));
948 details.setPathAttribute(pathAttributes);
949
950 for (BgpNodeListener l : controller.nodeListener) {
951 l.addNode(nodeNlri, details);
952 assertThat(nodeRegistry.connected.size(), is(1));
Priyanka B4c3b4512016-07-22 11:41:49 +0530953
954 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
955
956 newBuilder.set("lsrId", "1.1.1.1");
957
958 Device device = new DefaultDevice(BgpTopologyProviderTest.providerId,
959 nodeRegistry.connected.iterator().next(), Device.Type.ROUTER, UNKNOWN,
960 UNKNOWN, UNKNOWN, UNKNOWN, new ChassisId(), newBuilder.build());
961
962 DeviceEvent event = new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device);
Priyanka B658aa6982016-05-27 21:34:49 +0530963 listener.event(event);
964 assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(1));
965
966 l.deleteNode(nodeNlri);
967 assertThat(nodeRegistry.connected.size(), is(0));
968 assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(0));
969 }
970 }
971
972 /**
973 * Add a link register bandwidth and remove link unregister bandwidth.
974 *
975 * @throws BgpParseException while registering/unregistering bandwidth
976 */
977 @Test
978 public void bgpTopologyProviderDeviceTestLabel3() throws BgpParseException {
979 LinkedList<BgpValueType> localTlvs = new LinkedList<>();
980 LinkedList<BgpValueType> remoteTlvs = new LinkedList<>();
981 LinkedList<BgpValueType> linkdes = new LinkedList<>();
982 BgpValueType tlv = new AutonomousSystemTlv(10);
983 short deslength = AutonomousSystemTlv.LENGTH;
984 short desType = AutonomousSystemTlv.TYPE;
985
986 localTlvs.add(tlv);
987 remoteTlvs.add(tlv);
988 tlv = IsIsNonPseudonode.of(new byte[] {20, 20, 20, 20, 00, 20});
989 localTlvs.add(tlv);
990 tlv = IsIsNonPseudonode.of(new byte[] {30, 30, 30, 30, 00, 30});
991 remoteTlvs.add(tlv);
992 NodeDescriptors localNode = new NodeDescriptors(localTlvs, deslength, desType);
993 NodeDescriptors remoteNode = new NodeDescriptors(remoteTlvs, deslength, desType);
994 BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(localNode);
995 BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
996 new RouteDistinguisher());
997
998 BgpNodeLSIdentifier remoteNodeDescriptors = new BgpNodeLSIdentifier(remoteNode);
999 BgpNodeLSNlriVer4 remNodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, remoteNodeDescriptors, false,
1000 new RouteDistinguisher());
1001
1002 PathAttrNlriDetails details = new PathAttrNlriDetails();
1003 details.setIdentifier(0);
1004 details.setProtocolID(ProtocolType.DIRECT);
1005 List<BgpValueType> pathAttributes = new LinkedList<>();
1006 List<BgpValueType> linkStateAttributes = new LinkedList<>();
1007 tlv = BgpAttrRouterIdV4.of(Ip4Address.valueOf("1.1.1.1"), LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID);
1008 linkStateAttributes.add(tlv);
1009 pathAttributes.add(new LinkStateAttributes(linkStateAttributes));
1010 details.setPathAttribute(pathAttributes);
1011
1012 tlv = LinkLocalRemoteIdentifiersTlv.of(99, 100);
1013 linkdes.add(tlv);
1014 BgpLinkLSIdentifier linkId = new BgpLinkLSIdentifier(localNode, remoteNode, linkdes);
1015 BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4((byte) Constants.DIRECT, 0, linkId,
1016 new RouteDistinguisher(), false);
1017
1018 for (BgpNodeListener l : controller.nodeListener) {
1019 l.addNode(nodeNlri, details);
1020 assertThat(nodeRegistry.connected.size(), is(1));
1021 //Check label resource reserved for that device
Priyanka B4c3b4512016-07-22 11:41:49 +05301022 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
1023
1024 newBuilder.set("lsrId", "1.1.1.1");
1025
1026 Device device = new DefaultDevice(BgpTopologyProviderTest.providerId,
1027 nodeRegistry.connected.iterator().next(), Device.Type.ROUTER,
1028 UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, new ChassisId(), newBuilder.build());
1029
1030 DeviceEvent event = new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, device);
Priyanka B658aa6982016-05-27 21:34:49 +05301031 listener.event(event);
1032 assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(1));
1033 l.addNode(remNodeNlri, details);
1034 assertThat(nodeRegistry.connected.size(), is(2));
1035 l.deleteNode(remNodeNlri);
1036 assertThat(nodeRegistry.connected.size(), is(1));
1037 assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(1));
1038 }
1039
1040 List<BgpValueType> linkPathAttributes = new LinkedList<>();
1041 List<BgpValueType> linkStateAttr = new LinkedList<>();
1042 tlv = BgpLinkAttrIgpMetric.of(10, 4);
1043 linkStateAttr.add(tlv);
1044 tlv = BgpLinkAttrTeDefaultMetric.of(20);
1045 linkStateAttr.add(tlv);
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301046 tlv = BgpLinkAttrMaxLinkBandwidth.of((float) 70 * 1_000_000L,
1047 LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH);
Priyanka B658aa6982016-05-27 21:34:49 +05301048 linkStateAttr.add(tlv);
1049 linkPathAttributes.add(new LinkStateAttributes(linkStateAttr));
1050 details.setPathAttribute(linkPathAttributes);
1051
1052 for (BgpLinkListener l : controller.linkListener) {
1053 l.addLink(linkNlri, details);
Satish K690d5cf2017-04-04 15:12:38 +05301054 LinkKey linkKey = LinkKey.linkKey(linkRegistry.links.getFirst().src(),
1055 linkRegistry.links.getLast().dst());
Priyanka B658aa6982016-05-27 21:34:49 +05301056 assertThat(linkRegistry.links.size(), is(1));
Satish K690d5cf2017-04-04 15:12:38 +05301057 TeLinkConfig config = networkConfigService.getTeLinkConfig().get(linkKey);
1058
1059 assertThat(config.igpCost(), is(10));
1060 assertThat(config.teCost(), is(20));
Priyanka B658aa6982016-05-27 21:34:49 +05301061
1062 ConnectPoint src = new ConnectPoint(
1063 DeviceId.deviceId("l3:rd=0::routinguniverse=0:asn=10:isoid=1414.1414.0014"),
1064 PortNumber.portNumber(4294967395L));
1065 ConnectPoint dst = new ConnectPoint(
1066 DeviceId.deviceId("l3:rd=0::routinguniverse=0:asn=10:isoid=1e1e.1e1e.001e"),
1067 PortNumber.portNumber(4294967396L));
Priyanka B658aa6982016-05-27 21:34:49 +05301068
Satish K690d5cf2017-04-04 15:12:38 +05301069 assertThat(config.maxResvBandwidth(), is(70.0));
Priyanka B658aa6982016-05-27 21:34:49 +05301070
1071 l.deleteLink(linkNlri);
1072 assertThat(linkRegistry.links.size(), is(0));
Satish K690d5cf2017-04-04 15:12:38 +05301073 config = networkConfigService.getTeLinkConfig().get(linkKey);
1074 assertThat(config, is(nullValue()));
Priyanka B658aa6982016-05-27 21:34:49 +05301075 }
1076 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +05301077}