Priyanka B
Committed by Gerrit Code Review

[ONOS-4166] Add the capabilities for links and devices in the resource manager. …

…Also update the resource information from BGP-LS update.

Change-Id: I3ac50941c2841c104393fc543107496bffe92bbc
...@@ -106,7 +106,7 @@ public class LinkStateAttributes implements BgpValueType { ...@@ -106,7 +106,7 @@ public class LinkStateAttributes implements BgpValueType {
106 * 106 *
107 * @param linkStateAttribList Linked list of Link, Node and Prefix TLVs 107 * @param linkStateAttribList Linked list of Link, Node and Prefix TLVs
108 */ 108 */
109 - LinkStateAttributes(List<BgpValueType> linkStateAttribList) { 109 + public LinkStateAttributes(List<BgpValueType> linkStateAttribList) {
110 this.linkStateAttribList = linkStateAttribList; 110 this.linkStateAttribList = linkStateAttribList;
111 this.isLinkStateAttribute = true; 111 this.isLinkStateAttribute = true;
112 } 112 }
......
...@@ -96,7 +96,7 @@ public final class BgpLinkAttrMaxLinkBandwidth implements BgpValueType { ...@@ -96,7 +96,7 @@ public final class BgpLinkAttrMaxLinkBandwidth implements BgpValueType {
96 * 96 *
97 * @return Maximum link bandwidth 97 * @return Maximum link bandwidth
98 */ 98 */
99 - float linkAttrMaxLinkBandwidth() { 99 + public float linkAttrMaxLinkBandwidth() {
100 return maxBandwidth; 100 return maxBandwidth;
101 } 101 }
102 102
......
...@@ -41,6 +41,11 @@ ...@@ -41,6 +41,11 @@
41 </dependency> 41 </dependency>
42 <dependency> 42 <dependency>
43 <groupId>org.onosproject</groupId> 43 <groupId>org.onosproject</groupId>
44 + <artifactId>onos-incubator-api</artifactId>
45 + <version>${project.version}</version>
46 + </dependency>
47 + <dependency>
48 + <groupId>org.onosproject</groupId>
44 <artifactId>onos-api</artifactId> 49 <artifactId>onos-api</artifactId>
45 <classifier>tests</classifier> 50 <classifier>tests</classifier>
46 <scope>test</scope> 51 <scope>test</scope>
......
...@@ -3,6 +3,7 @@ COMPILE_DEPS = [ ...@@ -3,6 +3,7 @@ COMPILE_DEPS = [
3 '//protocols/bgp/api:onos-protocols-bgp-api', 3 '//protocols/bgp/api:onos-protocols-bgp-api',
4 '//protocols/bgp/bgpio:onos-protocols-bgp-bgpio', 4 '//protocols/bgp/bgpio:onos-protocols-bgp-bgpio',
5 '//incubator/store:onos-incubator-store', 5 '//incubator/store:onos-incubator-store',
6 + '//incubator/api:onos-incubator-api',
6 ] 7 ]
7 8
8 TEST_DEPS = [ 9 TEST_DEPS = [
......
...@@ -17,11 +17,16 @@ import static org.onosproject.bgp.controller.BgpDpid.uri; ...@@ -17,11 +17,16 @@ import static org.onosproject.bgp.controller.BgpDpid.uri;
17 import static org.onosproject.net.DeviceId.deviceId; 17 import static org.onosproject.net.DeviceId.deviceId;
18 import static org.onosproject.net.Device.Type.ROUTER; 18 import static org.onosproject.net.Device.Type.ROUTER;
19 import static org.onosproject.net.Device.Type.VIRTUAL; 19 import static org.onosproject.net.Device.Type.VIRTUAL;
20 +import static org.onosproject.incubator.net.resource.label.LabelResourceId.labelResourceId;
21 +import static java.util.stream.Collectors.toList;
20 22
23 +import java.util.LinkedList;
21 import java.util.List; 24 import java.util.List;
22 import java.util.Set; 25 import java.util.Set;
23 26
24 import org.onlab.packet.ChassisId; 27 import org.onlab.packet.ChassisId;
28 +import org.onlab.packet.Ip4Address;
29 +import org.onlab.util.Bandwidth;
25 import org.apache.felix.scr.annotations.Activate; 30 import org.apache.felix.scr.annotations.Activate;
26 import org.apache.felix.scr.annotations.Component; 31 import org.apache.felix.scr.annotations.Component;
27 import org.apache.felix.scr.annotations.Deactivate; 32 import org.apache.felix.scr.annotations.Deactivate;
...@@ -45,9 +50,21 @@ import org.onosproject.bgpio.types.IPv4AddressTlv; ...@@ -45,9 +50,21 @@ import org.onosproject.bgpio.types.IPv4AddressTlv;
45 import org.onosproject.bgpio.types.IsIsNonPseudonode; 50 import org.onosproject.bgpio.types.IsIsNonPseudonode;
46 import org.onosproject.bgpio.types.IsIsPseudonode; 51 import org.onosproject.bgpio.types.IsIsPseudonode;
47 import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv; 52 import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv;
53 +import org.onosproject.bgpio.types.LinkStateAttributes;
48 import org.onosproject.bgpio.types.OspfNonPseudonode; 54 import org.onosproject.bgpio.types.OspfNonPseudonode;
49 import org.onosproject.bgpio.types.OspfPseudonode; 55 import org.onosproject.bgpio.types.OspfPseudonode;
56 +import org.onosproject.bgpio.types.attr.BgpAttrNodeFlagBitTlv;
57 +import org.onosproject.bgpio.types.attr.BgpAttrNodeIsIsAreaId;
58 +import org.onosproject.bgpio.types.attr.BgpAttrRouterIdV4;
59 +import org.onosproject.bgpio.types.attr.BgpLinkAttrIgpMetric;
60 +import org.onosproject.bgpio.types.attr.BgpLinkAttrMaxLinkBandwidth;
61 +import org.onosproject.bgpio.types.attr.BgpLinkAttrTeDefaultMetric;
50 import org.onosproject.core.CoreService; 62 import org.onosproject.core.CoreService;
63 +import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
64 +import org.onosproject.incubator.net.resource.label.LabelResourceId;
65 +import org.onosproject.mastership.MastershipEvent;
66 +import org.onosproject.mastership.MastershipListener;
67 +import org.onosproject.mastership.MastershipService;
51 import org.onosproject.net.AnnotationKeys; 68 import org.onosproject.net.AnnotationKeys;
52 import org.onosproject.net.ConnectPoint; 69 import org.onosproject.net.ConnectPoint;
53 import org.onosproject.net.DefaultAnnotations; 70 import org.onosproject.net.DefaultAnnotations;
...@@ -56,12 +73,15 @@ import org.onosproject.net.DeviceId; ...@@ -56,12 +73,15 @@ import org.onosproject.net.DeviceId;
56 import org.onosproject.net.Link; 73 import org.onosproject.net.Link;
57 import org.onosproject.net.MastershipRole; 74 import org.onosproject.net.MastershipRole;
58 import org.onosproject.net.PortNumber; 75 import org.onosproject.net.PortNumber;
76 +import org.onosproject.net.config.NetworkConfigService;
59 import org.onosproject.net.device.DefaultDeviceDescription; 77 import org.onosproject.net.device.DefaultDeviceDescription;
78 +import org.onosproject.net.device.DefaultPortDescription;
60 import org.onosproject.net.device.DeviceDescription; 79 import org.onosproject.net.device.DeviceDescription;
61 import org.onosproject.net.device.DeviceProvider; 80 import org.onosproject.net.device.DeviceProvider;
62 import org.onosproject.net.device.DeviceProviderRegistry; 81 import org.onosproject.net.device.DeviceProviderRegistry;
63 import org.onosproject.net.device.DeviceProviderService; 82 import org.onosproject.net.device.DeviceProviderService;
64 import org.onosproject.net.device.DeviceService; 83 import org.onosproject.net.device.DeviceService;
84 +import org.onosproject.net.device.PortDescription;
65 import org.onosproject.net.link.DefaultLinkDescription; 85 import org.onosproject.net.link.DefaultLinkDescription;
66 import org.onosproject.net.link.LinkDescription; 86 import org.onosproject.net.link.LinkDescription;
67 import org.onosproject.net.link.LinkProvider; 87 import org.onosproject.net.link.LinkProvider;
...@@ -70,6 +90,7 @@ import org.onosproject.net.link.LinkProviderService; ...@@ -70,6 +90,7 @@ import org.onosproject.net.link.LinkProviderService;
70 import org.onosproject.net.link.LinkService; 90 import org.onosproject.net.link.LinkService;
71 import org.onosproject.net.provider.AbstractProvider; 91 import org.onosproject.net.provider.AbstractProvider;
72 import org.onosproject.net.provider.ProviderId; 92 import org.onosproject.net.provider.ProviderId;
93 +import org.onosproject.net.resource.BandwidthCapacity;
73 import org.slf4j.Logger; 94 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory; 95 import org.slf4j.LoggerFactory;
75 96
...@@ -106,16 +127,39 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -106,16 +127,39 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected CoreService coreService; 128 protected CoreService coreService;
108 129
130 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
131 + protected MastershipService mastershipService;
132 +
133 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
134 + protected LabelResourceAdminService labelResourceAdminService;
135 +
136 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
137 + protected NetworkConfigService networkConfigService;
138 +
109 private DeviceProviderService deviceProviderService; 139 private DeviceProviderService deviceProviderService;
110 private LinkProviderService linkProviderService; 140 private LinkProviderService linkProviderService;
111 141
142 + private InternalMastershipListener masterListener = new InternalMastershipListener();
112 private InternalBgpProvider listener = new InternalBgpProvider(); 143 private InternalBgpProvider listener = new InternalBgpProvider();
113 private static final String UNKNOWN = "unknown"; 144 private static final String UNKNOWN = "unknown";
114 public static final long IDENTIFIER_SET = 0x100000000L; 145 public static final long IDENTIFIER_SET = 0x100000000L;
115 public static final String AS_NUMBER = "asNumber"; 146 public static final String AS_NUMBER = "asNumber";
116 public static final String DOMAIN_IDENTIFIER = "domainIdentifier"; 147 public static final String DOMAIN_IDENTIFIER = "domainIdentifier";
117 public static final String ROUTING_UNIVERSE = "routingUniverse"; 148 public static final String ROUTING_UNIVERSE = "routingUniverse";
149 +
150 + public static final String EXTERNAL_BIT = "externalBit";
151 + public static final String ABR_BIT = "abrBit";
152 + public static final String INTERNAL_BIT = "internalBit";
153 + public static final String PSEUDO = "psuedo";
154 + public static final String AREAID = "areaId";
155 + public static final String LSRID = "lsrId";
156 + public static final String COST = "cost";
157 + public static final String TE_COST = "teCost";
158 +
118 public static final long PSEUDO_PORT = 0xffffffff; 159 public static final long PSEUDO_PORT = 0xffffffff;
160 + public static final int DELAY = 2;
161 + private LabelResourceId beginLabel = labelResourceId(5122);
162 + private LabelResourceId endLabel = labelResourceId(9217);
119 163
120 @Activate 164 @Activate
121 public void activate() { 165 public void activate() {
...@@ -123,6 +167,7 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -123,6 +167,7 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
123 deviceProviderService = deviceProviderRegistry.register(this); 167 deviceProviderService = deviceProviderRegistry.register(this);
124 linkProviderService = linkProviderRegistry.register(this); 168 linkProviderService = linkProviderRegistry.register(this);
125 controller.addListener(listener); 169 controller.addListener(listener);
170 + mastershipService.addListener(masterListener);
126 controller.addLinkListener(listener); 171 controller.addLinkListener(listener);
127 } 172 }
128 173
...@@ -135,6 +180,27 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -135,6 +180,27 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
135 linkProviderService = null; 180 linkProviderService = null;
136 controller.removeListener(listener); 181 controller.removeListener(listener);
137 controller.removeLinkListener(listener); 182 controller.removeLinkListener(listener);
183 + mastershipService.removeListener(masterListener);
184 + }
185 +
186 + private class InternalMastershipListener implements MastershipListener {
187 + @Override
188 + public void event(MastershipEvent event) {
189 + if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
190 + if (mastershipService.getMasterFor(event.subject()) != null) {
191 + //Only for L3 device create label pool for that device
192 + Device device = deviceService.getDevice(event.subject());
193 + if (device == null) {
194 + log.debug("Device {} doesn't exist", event.subject());
195 + return;
196 + }
197 + //Reserve device label pool for L3 devices
198 + if (device.annotations().value(LSRID) != null) {
199 + createDevicePool(event.subject());
200 + }
201 + }
202 + }
203 + }
138 } 204 }
139 205
140 /* 206 /*
...@@ -146,7 +212,7 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -146,7 +212,7 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
146 public void addNode(BgpNodeLSNlriVer4 nodeNlri, PathAttrNlriDetails details) { 212 public void addNode(BgpNodeLSNlriVer4 nodeNlri, PathAttrNlriDetails details) {
147 log.debug("Add node {}", nodeNlri.toString()); 213 log.debug("Add node {}", nodeNlri.toString());
148 214
149 - if (deviceProviderService == null) { 215 + if (deviceProviderService == null || deviceService == null) {
150 return; 216 return;
151 } 217 }
152 Device.Type deviceType = ROUTER; 218 Device.Type deviceType = ROUTER;
...@@ -154,6 +220,13 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -154,6 +220,13 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
154 DeviceId deviceId = deviceId(uri(nodeUri.toString())); 220 DeviceId deviceId = deviceId(uri(nodeUri.toString()));
155 ChassisId cId = new ChassisId(); 221 ChassisId cId = new ChassisId();
156 222
223 + /*
224 + * Check if device is already there (available) , if yes not updating to core.
225 + */
226 + if (deviceService.isAvailable(deviceId)) {
227 + return;
228 + }
229 +
157 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder(); 230 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
158 231
159 newBuilder.set(AnnotationKeys.TYPE, "L3"); 232 newBuilder.set(AnnotationKeys.TYPE, "L3");
...@@ -183,11 +256,13 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -183,11 +256,13 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
183 } 256 }
184 } 257 }
185 } 258 }
259 + DefaultAnnotations.Builder anntotations = DefaultAnnotations.builder();
260 + anntotations = getAnnotations(newBuilder, true, details);
186 261
187 DeviceDescription description = new DefaultDeviceDescription(uri(nodeUri.toString()), deviceType, UNKNOWN, 262 DeviceDescription description = new DefaultDeviceDescription(uri(nodeUri.toString()), deviceType, UNKNOWN,
188 - UNKNOWN, UNKNOWN, UNKNOWN, cId, newBuilder.build()); 263 + UNKNOWN, UNKNOWN, UNKNOWN, cId, anntotations.build());
189 - deviceProviderService.deviceConnected(deviceId, description);
190 264
265 + deviceProviderService.deviceConnected(deviceId, description);
191 } 266 }
192 267
193 @Override 268 @Override
...@@ -200,6 +275,12 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -200,6 +275,12 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
200 275
201 BgpDpid deviceUri = new BgpDpid(nodeNlri); 276 BgpDpid deviceUri = new BgpDpid(nodeNlri);
202 DeviceId deviceId = deviceId(uri(deviceUri.toString())); 277 DeviceId deviceId = deviceId(uri(deviceUri.toString()));
278 +
279 + if (labelResourceAdminService != null) {
280 + //Destroy local device label pool reserved for that device
281 + labelResourceAdminService.destroyDevicePool(deviceId);
282 + }
283 +
203 deviceProviderService.deviceDisconnected(deviceId); 284 deviceProviderService.deviceDisconnected(deviceId);
204 } 285 }
205 286
...@@ -211,6 +292,24 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -211,6 +292,24 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
211 return; 292 return;
212 } 293 }
213 LinkDescription linkDes = buildLinkDes(linkNlri, details, true); 294 LinkDescription linkDes = buildLinkDes(linkNlri, details, true);
295 +
296 + /*
297 + * Update link ports and configure bandwidth on source and destination port using networkConfig service
298 + * Only master of source link registers for bandwidth
299 + */
300 + if (mastershipService.isLocalMaster(linkDes.src().deviceId())) {
301 + registerBandwidth(linkDes, details);
302 + }
303 +
304 + //Updating ports of the link
305 + List<PortDescription> srcPortDescriptions = new LinkedList<>();
306 + srcPortDescriptions.add(new DefaultPortDescription(linkDes.src().port(), true));
307 + deviceProviderService.updatePorts(linkDes.src().deviceId(), srcPortDescriptions);
308 +
309 + List<PortDescription> dstPortDescriptions = new LinkedList<>();
310 + dstPortDescriptions.add(new DefaultPortDescription(linkDes.dst().port(), true));
311 + deviceProviderService.updatePorts(linkDes.dst().deviceId(), dstPortDescriptions);
312 +
214 linkProviderService.linkDetected(linkDes); 313 linkProviderService.linkDetected(linkDes);
215 } 314 }
216 315
...@@ -273,7 +372,12 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -273,7 +372,12 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
273 372
274 addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri, 373 addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri,
275 dstNodeNlri, srcId, dstId, details); 374 dstNodeNlri, srcId, dstId, details);
276 - return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false); 375 + DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
376 + if (details != null) {
377 + annotationBuilder = getAnnotations(annotationBuilder, false, details);
378 + }
379 +
380 + return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false, annotationBuilder.build());
277 } 381 }
278 382
279 private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo, 383 private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo,
...@@ -322,10 +426,140 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi ...@@ -322,10 +426,140 @@ public class BgpTopologyProvider extends AbstractProvider implements DeviceProvi
322 } 426 }
323 427
324 LinkDescription linkDes = buildLinkDes(linkNlri, null, false); 428 LinkDescription linkDes = buildLinkDes(linkNlri, null, false);
429 +
430 + /*
431 + * Only master for the link src will release the bandwidth resource.
432 + */
433 + if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
434 + // Releases registered resource for this link
435 + networkConfigService.removeConfig(linkDes.src(), BandwidthCapacity.class);
436 + networkConfigService.removeConfig(linkDes.dst(), BandwidthCapacity.class);
437 + }
438 +
325 linkProviderService.linkVanished(linkDes); 439 linkProviderService.linkVanished(linkDes);
326 } 440 }
327 } 441 }
328 442
443 + // Creates label resource pool for the specific device. For all devices label range is 5122-9217
444 + private void createDevicePool(DeviceId deviceId) {
445 + if (labelResourceAdminService == null) {
446 + return;
447 + }
448 +
449 + labelResourceAdminService.createDevicePool(deviceId, beginLabel, endLabel);
450 + }
451 +
452 + private void registerBandwidth(LinkDescription linkDes, PathAttrNlriDetails details) {
453 + if (details == null) {
454 + log.error("Couldnot able to register bandwidth ");
455 + return;
456 + }
457 +
458 + List<BgpValueType> attribute = details.pathAttributes().stream()
459 + .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
460 + if (attribute.isEmpty()) {
461 + return;
462 + }
463 +
464 + List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
465 + float maxReservableBw = 0;
466 +
467 + for (BgpValueType tlv : tlvs) {
468 + switch (tlv.getType()) {
469 + case LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH:
470 + maxReservableBw = ((BgpLinkAttrMaxLinkBandwidth) tlv).linkAttrMaxLinkBandwidth();
471 + break;
472 + default: // do nothing
473 + }
474 + }
475 +
476 + if (maxReservableBw == 0.0) {
477 + return;
478 + }
479 +
480 + //Configure bandwidth for src and dst port
481 + BandwidthCapacity config = networkConfigService.addConfig(linkDes.src(), BandwidthCapacity.class);
482 + config.capacity(Bandwidth.bps(maxReservableBw)).apply();
483 +
484 + config = networkConfigService.addConfig(linkDes.dst(), BandwidthCapacity.class);
485 + config.capacity(Bandwidth.bps(maxReservableBw)).apply();
486 + }
487 +
488 + private DefaultAnnotations.Builder getAnnotations(DefaultAnnotations.Builder annotationBuilder, boolean isNode,
489 + PathAttrNlriDetails details) {
490 +
491 + List<BgpValueType> attribute = details.pathAttributes().stream()
492 + .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
493 + if (attribute.isEmpty()) {
494 + return annotationBuilder;
495 + }
496 + List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
497 + boolean abrBit = false;
498 + boolean externalBit = false;
499 + boolean pseudo = false;
500 + int igpMetric = 0;
501 + int teMetric = 0;
502 + byte[] areaId = null;
503 + Ip4Address routerId = null;
504 + for (BgpValueType tlv : tlvs) {
505 + switch (tlv.getType()) {
506 + case LinkStateAttributes.ATTR_NODE_FLAG_BITS:
507 + abrBit = ((BgpAttrNodeFlagBitTlv) tlv).abrBit();
508 + externalBit = ((BgpAttrNodeFlagBitTlv) tlv).externalBit();
509 + break;
510 + case NodeDescriptors.IGP_ROUTERID_TYPE:
511 + if (tlv instanceof IsIsPseudonode || tlv instanceof OspfPseudonode) {
512 + pseudo = true;
513 + }
514 + break;
515 + case LinkStateAttributes.ATTR_NODE_ISIS_AREA_ID:
516 + areaId = ((BgpAttrNodeIsIsAreaId) tlv).attrNodeIsIsAreaId();
517 + break;
518 + case LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID:
519 + routerId = ((BgpAttrRouterIdV4) tlv).attrRouterId();
520 + break;
521 + case LinkStateAttributes.ATTR_LINK_IGP_METRIC:
522 + igpMetric = ((BgpLinkAttrIgpMetric) tlv).attrLinkIgpMetric();
523 + break;
524 + case LinkStateAttributes.ATTR_LINK_TE_DEFAULT_METRIC:
525 + teMetric = ((BgpLinkAttrTeDefaultMetric) tlv).attrLinkDefTeMetric();
526 + break;
527 + default: // do nothing
528 + }
529 + }
530 +
531 + // Annotations for device
532 + if (isNode) {
533 + boolean internalBit = false;
534 + if (!abrBit && !externalBit) {
535 + internalBit = true;
536 + }
537 +
538 + annotationBuilder.set(EXTERNAL_BIT, String.valueOf(externalBit));
539 + annotationBuilder.set(ABR_BIT, String.valueOf(abrBit));
540 + annotationBuilder.set(INTERNAL_BIT, String.valueOf(internalBit));
541 + annotationBuilder.set(PSEUDO, String.valueOf(pseudo));
542 +
543 + if (areaId != null) {
544 + annotationBuilder.set(AREAID, new String(areaId));
545 + }
546 + if (routerId != null) {
547 + // LsrID
548 + annotationBuilder.set(LSRID, String.valueOf(routerId));
549 + }
550 + } else {
551 + // Annotations for link
552 + if (igpMetric != 0) {
553 + annotationBuilder.set(COST, String.valueOf(igpMetric));
554 + }
555 +
556 + if (teMetric != 0) {
557 + annotationBuilder.set(TE_COST, String.valueOf(teMetric));
558 + }
559 + }
560 + return annotationBuilder;
561 + }
562 +
329 @Override 563 @Override
330 public void triggerProbe(DeviceId deviceId) { 564 public void triggerProbe(DeviceId deviceId) {
331 // TODO Auto-generated method stub 565 // TODO Auto-generated method stub
......
...@@ -16,6 +16,8 @@ import static org.hamcrest.MatcherAssert.assertThat; ...@@ -16,6 +16,8 @@ import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.is; 16 import static org.hamcrest.core.Is.is;
17 import static org.hamcrest.core.IsNot.not; 17 import static org.hamcrest.core.IsNot.not;
18 import static org.onosproject.net.Link.State.ACTIVE; 18 import static org.onosproject.net.Link.State.ACTIVE;
19 +import static org.onosproject.net.MastershipRole.MASTER;
20 +import static org.hamcrest.Matchers.nullValue;
19 21
20 import java.util.Collection; 22 import java.util.Collection;
21 import java.util.HashMap; 23 import java.util.HashMap;
...@@ -29,7 +31,11 @@ import java.util.concurrent.CopyOnWriteArraySet; ...@@ -29,7 +31,11 @@ import java.util.concurrent.CopyOnWriteArraySet;
29 import org.junit.After; 31 import org.junit.After;
30 import org.junit.Before; 32 import org.junit.Before;
31 import org.junit.Test; 33 import org.junit.Test;
34 +import org.onlab.junit.TestUtils;
35 +import org.onlab.junit.TestUtils.TestUtilsException;
32 import org.onlab.packet.ChassisId; 36 import org.onlab.packet.ChassisId;
37 +import org.onlab.packet.Ip4Address;
38 +import org.onlab.util.Bandwidth;
33 import org.onosproject.bgp.controller.BgpLinkListener; 39 import org.onosproject.bgp.controller.BgpLinkListener;
34 import org.onosproject.bgp.controller.BgpNodeListener; 40 import org.onosproject.bgp.controller.BgpNodeListener;
35 import org.onosproject.bgpio.exceptions.BgpParseException; 41 import org.onosproject.bgpio.exceptions.BgpParseException;
...@@ -43,9 +49,26 @@ import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails; ...@@ -43,9 +49,26 @@ import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
43 import org.onosproject.bgpio.types.AutonomousSystemTlv; 49 import org.onosproject.bgpio.types.AutonomousSystemTlv;
44 import org.onosproject.bgpio.types.BgpValueType; 50 import org.onosproject.bgpio.types.BgpValueType;
45 import org.onosproject.bgpio.types.IsIsNonPseudonode; 51 import org.onosproject.bgpio.types.IsIsNonPseudonode;
52 +import org.onosproject.bgpio.types.LinkStateAttributes;
53 +import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
54 +import org.onosproject.incubator.net.resource.label.LabelResourceId;
55 +import org.onosproject.incubator.net.resource.label.LabelResourcePool;
56 +import org.onosproject.mastership.MastershipEvent;
57 +import org.onosproject.mastership.MastershipListener;
58 +import org.onosproject.mastership.MastershipServiceAdapter;
59 +import org.onosproject.mastership.MastershipEvent.Type;
60 +import org.onosproject.net.link.LinkServiceAdapter;
46 import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv; 61 import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv;
47 import org.onosproject.bgpio.types.RouteDistinguisher; 62 import org.onosproject.bgpio.types.RouteDistinguisher;
63 +import org.onosproject.bgpio.types.attr.BgpAttrNodeFlagBitTlv;
64 +import org.onosproject.bgpio.types.attr.BgpAttrNodeIsIsAreaId;
65 +import org.onosproject.bgpio.types.attr.BgpAttrRouterIdV4;
66 +import org.onosproject.bgpio.types.attr.BgpLinkAttrIgpMetric;
67 +import org.onosproject.bgpio.types.attr.BgpLinkAttrMaxLinkBandwidth;
68 +import org.onosproject.bgpio.types.attr.BgpLinkAttrTeDefaultMetric;
48 import org.onosproject.bgpio.util.Constants; 69 import org.onosproject.bgpio.util.Constants;
70 +import org.onosproject.cluster.NodeId;
71 +import org.onosproject.cluster.RoleInfo;
49 import org.onosproject.net.ConnectPoint; 72 import org.onosproject.net.ConnectPoint;
50 import org.onosproject.net.DefaultDevice; 73 import org.onosproject.net.DefaultDevice;
51 import org.onosproject.net.DefaultLink; 74 import org.onosproject.net.DefaultLink;
...@@ -53,6 +76,11 @@ import org.onosproject.net.Device; ...@@ -53,6 +76,11 @@ import org.onosproject.net.Device;
53 import org.onosproject.net.DeviceId; 76 import org.onosproject.net.DeviceId;
54 import org.onosproject.net.Link; 77 import org.onosproject.net.Link;
55 import org.onosproject.net.MastershipRole; 78 import org.onosproject.net.MastershipRole;
79 +import org.onosproject.net.PortNumber;
80 +import org.onosproject.net.config.Config;
81 +import org.onosproject.net.config.ConfigApplyDelegate;
82 +import org.onosproject.net.config.ConfigFactory;
83 +import org.onosproject.net.config.NetworkConfigRegistryAdapter;
56 import org.onosproject.net.device.DeviceDescription; 84 import org.onosproject.net.device.DeviceDescription;
57 import org.onosproject.net.device.DeviceProvider; 85 import org.onosproject.net.device.DeviceProvider;
58 import org.onosproject.net.device.DeviceProviderRegistry; 86 import org.onosproject.net.device.DeviceProviderRegistry;
...@@ -65,6 +93,15 @@ import org.onosproject.net.link.LinkProvider; ...@@ -65,6 +93,15 @@ import org.onosproject.net.link.LinkProvider;
65 import org.onosproject.net.link.LinkProviderRegistry; 93 import org.onosproject.net.link.LinkProviderRegistry;
66 import org.onosproject.net.link.LinkProviderService; 94 import org.onosproject.net.link.LinkProviderService;
67 import org.onosproject.net.provider.ProviderId; 95 import org.onosproject.net.provider.ProviderId;
96 +import org.onosproject.net.resource.BandwidthCapacity;
97 +import org.onosproject.net.resource.Resource;
98 +import org.onosproject.net.resource.ResourceAdminService;
99 +import org.onosproject.net.resource.ResourceId;
100 +
101 +import com.fasterxml.jackson.databind.JsonNode;
102 +import com.fasterxml.jackson.databind.ObjectMapper;
103 +import com.fasterxml.jackson.databind.node.JsonNodeFactory;
104 +import com.fasterxml.jackson.databind.node.ObjectNode;
68 105
69 /** 106 /**
70 * Test for BGP topology provider. 107 * Test for BGP topology provider.
...@@ -73,21 +110,31 @@ public class BgpTopologyProviderTest { ...@@ -73,21 +110,31 @@ public class BgpTopologyProviderTest {
73 private static final DeviceId DID2 = DeviceId.deviceId("l3:rd=0::routinguniverse=0:asn=10"); 110 private static final DeviceId DID2 = DeviceId.deviceId("l3:rd=0::routinguniverse=0:asn=10");
74 private static final String UNKNOWN = new String("unknown"); 111 private static final String UNKNOWN = new String("unknown");
75 public static ProviderId providerId = new ProviderId("l3", "foo"); 112 public static ProviderId providerId = new ProviderId("l3", "foo");
113 + private static final NodeId NODE1 = new NodeId("Master1");
76 114
77 private final BgpTopologyProvider provider = new BgpTopologyProvider(); 115 private final BgpTopologyProvider provider = new BgpTopologyProvider();
78 private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry(); 116 private final TestDeviceRegistry nodeRegistry = new TestDeviceRegistry();
79 private final TestLinkRegistry linkRegistry = new TestLinkRegistry(); 117 private final TestLinkRegistry linkRegistry = new TestLinkRegistry();
80 private final MockBgpController controller = new MockBgpController(); 118 private final MockBgpController controller = new MockBgpController();
81 private MockDeviceService deviceService = new MockDeviceService(); 119 private MockDeviceService deviceService = new MockDeviceService();
120 + private MockLinkService linkService = new MockLinkService();
121 + private MockMastershipService mastershipService = new MockMastershipService();
122 + private MockNetConfigRegistryAdapter networkConfigService = new MockNetConfigRegistryAdapter();
123 + private MockLabelResourceService labelResourceAdminService = new MockLabelResourceService();
82 private Map<DeviceId, Device> deviceMap = new HashMap<>(); 124 private Map<DeviceId, Device> deviceMap = new HashMap<>();
83 - 125 + private MastershipListener listener;
84 126
85 @Before 127 @Before
86 - public void startUp() { 128 + public void startUp() throws TestUtilsException {
87 provider.deviceProviderRegistry = nodeRegistry; 129 provider.deviceProviderRegistry = nodeRegistry;
88 provider.linkProviderRegistry = linkRegistry; 130 provider.linkProviderRegistry = linkRegistry;
89 provider.controller = controller; 131 provider.controller = controller;
90 provider.deviceService = deviceService; 132 provider.deviceService = deviceService;
133 + provider.linkService = linkService;
134 + provider.labelResourceAdminService = labelResourceAdminService;
135 + provider.mastershipService = mastershipService;
136 + provider.networkConfigService = networkConfigService;
137 + listener = TestUtils.getField(provider, "masterListener");
91 provider.activate(); 138 provider.activate();
92 assertThat("device provider should be registered", not(nodeRegistry.provider)); 139 assertThat("device provider should be registered", not(nodeRegistry.provider));
93 assertThat("link provider should be registered", not(linkRegistry.linkProvider)); 140 assertThat("link provider should be registered", not(linkRegistry.linkProvider));
...@@ -101,15 +148,177 @@ public class BgpTopologyProviderTest { ...@@ -101,15 +148,177 @@ public class BgpTopologyProviderTest {
101 provider.controller = null; 148 provider.controller = null;
102 provider.deviceService = null; 149 provider.deviceService = null;
103 provider.deviceProviderRegistry = null; 150 provider.deviceProviderRegistry = null;
151 + provider.linkService = null;
152 + provider.mastershipService = null;
153 + provider.networkConfigService = null;
154 + provider.labelResourceAdminService = null;
104 assertThat(controller.nodeListener, is(new HashSet<BgpNodeListener>())); 155 assertThat(controller.nodeListener, is(new HashSet<BgpNodeListener>()));
105 assertThat(controller.linkListener, is(new HashSet<BgpLinkListener>())); 156 assertThat(controller.linkListener, is(new HashSet<BgpLinkListener>()));
106 } 157 }
107 158
159 + private class MockLabelResourceService implements LabelResourceAdminService {
160 +
161 + Map<DeviceId, LabelResourcePool> resourcePool = new HashMap<>();
162 +
163 + @Override
164 + public boolean createDevicePool(DeviceId deviceId, LabelResourceId beginLabel, LabelResourceId endLabel) {
165 + LabelResourcePool labelResource = new LabelResourcePool(deviceId.toString(),
166 + beginLabel.labelId(),
167 + endLabel.labelId());
168 + if (resourcePool.containsValue(labelResource)) {
169 + return false;
170 + }
171 +
172 + resourcePool.put(deviceId, labelResource);
173 + return true;
174 + }
175 +
176 + @Override
177 + public boolean createGlobalPool(LabelResourceId beginLabel, LabelResourceId endLabel) {
178 + // TODO Auto-generated method stub
179 + return false;
180 + }
181 +
182 + @Override
183 + public boolean destroyDevicePool(DeviceId deviceId) {
184 + LabelResourcePool devicePool = resourcePool.get(deviceId);
185 +
186 + if (devicePool == null) {
187 + return false;
188 + }
189 +
190 + resourcePool.remove(deviceId);
191 + return true;
192 + }
193 +
194 + @Override
195 + public boolean destroyGlobalPool() {
196 + // TODO Auto-generated method stub
197 + return false;
198 + }
199 + }
200 +
201 + /* Mock test for device service */
202 + private class MockNetConfigRegistryAdapter extends NetworkConfigRegistryAdapter {
203 + private ConfigFactory cfgFactory;
204 + private Map<ConnectPoint, BandwidthCapacity> classConfig = new HashMap<>();
205 +
206 + @Override
207 + public void registerConfigFactory(ConfigFactory configFactory) {
208 + cfgFactory = configFactory;
209 + }
210 +
211 + @Override
212 + public void unregisterConfigFactory(ConfigFactory configFactory) {
213 + cfgFactory = null;
214 + }
215 +
216 + @Override
217 + public <S, C extends Config<S>> C addConfig(S subject, Class<C> configClass) {
218 + if (configClass == BandwidthCapacity.class) {
219 + BandwidthCapacity devCap = new BandwidthCapacity();
220 + classConfig.put((ConnectPoint) subject, devCap);
221 +
222 + JsonNode node = new ObjectNode(new MockJsonNode());
223 + ObjectMapper mapper = new ObjectMapper();
224 + ConfigApplyDelegate delegate = new InternalApplyDelegate();
225 + devCap.init((ConnectPoint) subject, null, node, mapper, delegate);
226 + return (C) devCap;
227 + }
228 +
229 + return null;
230 + }
231 +
232 + @Override
233 + public <S, C extends Config<S>> void removeConfig(S subject, Class<C> configClass) {
234 + classConfig.remove(subject);
235 + }
236 +
237 + @Override
238 + public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
239 + if (configClass == BandwidthCapacity.class) {
240 + return (C) classConfig.get(subject);
241 + }
242 + return null;
243 + }
244 +
245 + private class MockJsonNode extends JsonNodeFactory {
246 + }
247 +
248 + // Auxiliary delegate to receive notifications about changes applied to
249 + // the network configuration - by the apps.
250 + private class InternalApplyDelegate implements ConfigApplyDelegate {
251 + @Override
252 + public void onApply(Config config) {
253 + }
254 + }
255 + }
256 +
257 + private class MockMastershipService extends MastershipServiceAdapter {
258 + @Override
259 + public MastershipRole getLocalRole(DeviceId deviceId) {
260 + return MASTER;
261 + }
262 +
263 + @Override
264 + public boolean isLocalMaster(DeviceId deviceId) {
265 + return getLocalRole(deviceId) == MASTER;
266 + }
267 +
268 + @Override
269 + public NodeId getMasterFor(DeviceId deviceId) {
270 + return NODE1;
271 + }
272 + }
273 +
274 + private class MockResourceAdminService implements ResourceAdminService {
275 + Map<ResourceId, List<Resource>> registeredRes = new HashMap<>();
276 +
277 + @Override
278 + public boolean register(List<Resource> resources) {
279 + for (Resource res : resources) {
280 + List<Resource> resource = new LinkedList<>();
281 + resource.add(res);
282 + if (registeredRes.containsKey(res.id())) {
283 + resource.addAll(registeredRes.get(res.id()));
284 + }
285 + registeredRes.put(res.id(), resource);
286 + }
287 + return true;
288 + }
289 +
290 + @Override
291 + public boolean unregister(List<ResourceId> ids) {
292 + for (ResourceId id : ids) {
293 + if (registeredRes.containsKey(id)) {
294 + registeredRes.remove(id);
295 + } else {
296 + return false;
297 + }
298 + }
299 + return true;
300 + }
301 + }
302 +
303 + private class MockLinkService extends LinkServiceAdapter {
304 +
305 + @Override
306 + public Link getLink(ConnectPoint src, ConnectPoint dst) {
307 + for (Link link : linkRegistry.links) {
308 + if (link.src().equals(src) && link.dst().equals(dst)) {
309 + return link;
310 + }
311 + }
312 + return null;
313 + }
314 + }
315 +
108 /* Class implement device test registry */ 316 /* Class implement device test registry */
109 private class TestDeviceRegistry implements DeviceProviderRegistry { 317 private class TestDeviceRegistry implements DeviceProviderRegistry {
110 DeviceProvider provider; 318 DeviceProvider provider;
111 319
112 Set<DeviceId> connected = new HashSet<>(); 320 Set<DeviceId> connected = new HashSet<>();
321 + Map<DeviceId, List<PortDescription>> portUpdated = new HashMap<>();
113 322
114 @Override 323 @Override
115 public DeviceProviderService register(DeviceProvider provider) { 324 public DeviceProviderService register(DeviceProvider provider) {
...@@ -138,7 +347,7 @@ public class BgpTopologyProviderTest { ...@@ -138,7 +347,7 @@ public class BgpTopologyProviderTest {
138 if (!deviceId.equals(DID2)) { 347 if (!deviceId.equals(DID2)) {
139 connected.add(deviceId); 348 connected.add(deviceId);
140 Device device = new DefaultDevice(BgpTopologyProviderTest.providerId, deviceId, Device.Type.ROUTER, 349 Device device = new DefaultDevice(BgpTopologyProviderTest.providerId, deviceId, Device.Type.ROUTER,
141 - UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, new ChassisId()); 350 + UNKNOWN, UNKNOWN, UNKNOWN, UNKNOWN, new ChassisId(), deviceDescription.annotations());
142 deviceMap.put(deviceId, device); 351 deviceMap.put(deviceId, device);
143 } 352 }
144 } 353 }
...@@ -153,8 +362,7 @@ public class BgpTopologyProviderTest { ...@@ -153,8 +362,7 @@ public class BgpTopologyProviderTest {
153 362
154 @Override 363 @Override
155 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) { 364 public void updatePorts(DeviceId deviceId, List<PortDescription> portDescriptions) {
156 - // TODO Auto-generated method stub 365 + portUpdated.put(deviceId, portDescriptions);
157 -
158 } 366 }
159 367
160 @Override 368 @Override
...@@ -202,9 +410,14 @@ public class BgpTopologyProviderTest { ...@@ -202,9 +410,14 @@ public class BgpTopologyProviderTest {
202 410
203 @Override 411 @Override
204 public void linkDetected(LinkDescription linkDescription) { 412 public void linkDetected(LinkDescription linkDescription) {
205 - links.add(DefaultLink.builder().src(linkDescription.src()) 413 + links.add(DefaultLink.builder()
206 - .dst(linkDescription.dst()).state(ACTIVE).type(linkDescription.type()) 414 + .src(linkDescription.src())
207 - .providerId(BgpTopologyProviderTest.providerId).build()); 415 + .dst(linkDescription.dst())
416 + .state(ACTIVE)
417 + .type(linkDescription.type())
418 + .providerId(BgpTopologyProviderTest.providerId)
419 + .annotations(linkDescription.annotations())
420 + .build());
208 } 421 }
209 422
210 @Override 423 @Override
...@@ -349,6 +562,59 @@ public class BgpTopologyProviderTest { ...@@ -349,6 +562,59 @@ public class BgpTopologyProviderTest {
349 } 562 }
350 } 563 }
351 564
565 +
566 + /**
567 + * Validate node is added to the device with all device annotations.
568 + */
569 + @Test
570 + public void bgpTopologyProviderTestAddDevice4() {
571 + LinkedList<BgpValueType> subTlvs = new LinkedList<>();
572 + BgpValueType tlv = new AutonomousSystemTlv(100);
573 + short deslength = AutonomousSystemTlv.LENGTH;
574 + short desType = AutonomousSystemTlv.TYPE;
575 +
576 + subTlvs.add(tlv);
577 + BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
578 + desType));
579 + BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
580 + new RouteDistinguisher());
581 +
582 + PathAttrNlriDetails details = new PathAttrNlriDetails();
583 + details.setIdentifier(0);
584 + details.setProtocolID(ProtocolType.DIRECT);
585 + List<BgpValueType> pathAttributes = new LinkedList<>();
586 + List<BgpValueType> linkStateAttr = new LinkedList<>();
587 + tlv = BgpAttrNodeFlagBitTlv.of(true, true, true, false);
588 + linkStateAttr.add(tlv);
589 + tlv = BgpAttrNodeIsIsAreaId.of(new byte[] {01, 01, 01, 01});
590 + linkStateAttr.add(tlv);
591 + tlv = BgpAttrRouterIdV4.of(Ip4Address.valueOf("1.1.1.1"), LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID);
592 + linkStateAttr.add(tlv);
593 + pathAttributes.add(new LinkStateAttributes(linkStateAttr));
594 + details.setPathAttribute(pathAttributes);
595 +
596 + for (BgpNodeListener l : controller.nodeListener) {
597 + l.addNode(nodeNlri, details);
598 +
599 + assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.ABR_BIT),
600 + is("false"));
601 + assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.EXTERNAL_BIT),
602 + is("true"));
603 + assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.INTERNAL_BIT),
604 + is("false"));
605 + assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.PSEUDO),
606 + is("false"));
607 + assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.AREAID).getBytes(),
608 + is(new byte[] {01, 01, 01, 01}));
609 + assertThat(deviceMap.values().iterator().next().annotations().value(BgpTopologyProvider.LSRID),
610 + is("1.1.1.1"));
611 +
612 + assertThat(nodeRegistry.connected.size(), is(1));
613 + l.deleteNode(nodeNlri);
614 + assertThat(nodeRegistry.connected.size(), is(0));
615 + }
616 + }
617 +
352 /** 618 /**
353 * Add a link and two devices. 619 * Add a link and two devices.
354 * 620 *
...@@ -460,6 +726,82 @@ public class BgpTopologyProviderTest { ...@@ -460,6 +726,82 @@ public class BgpTopologyProviderTest {
460 } 726 }
461 727
462 /** 728 /**
729 + * Add a link and delete a link with registering/unregistering bandwidth.
730 + *
731 + * @throws BgpParseException while adding or removing the link
732 + * @throws InterruptedException while registering for bandwidth
733 + */
734 + @Test
735 + public void bgpTopologyProviderTestAddLink3() throws BgpParseException, InterruptedException {
736 + LinkedList<BgpValueType> localTlvs = new LinkedList<>();
737 + LinkedList<BgpValueType> remoteTlvs = new LinkedList<>();
738 + LinkedList<BgpValueType> linkdes = new LinkedList<>();
739 + BgpValueType tlv = new AutonomousSystemTlv(10);
740 + short deslength = AutonomousSystemTlv.LENGTH;
741 + short desType = AutonomousSystemTlv.TYPE;
742 +
743 + localTlvs.add(tlv);
744 + remoteTlvs.add(tlv);
745 + tlv = IsIsNonPseudonode.of(new byte[] {20, 20, 20, 20, 00, 20});
746 + localTlvs.add(tlv);
747 + tlv = IsIsNonPseudonode.of(new byte[] {30, 30, 30, 30, 00, 30});
748 + remoteTlvs.add(tlv);
749 + NodeDescriptors localNode = new NodeDescriptors(localTlvs, deslength, desType);
750 + NodeDescriptors remoteNode = new NodeDescriptors(remoteTlvs, deslength, desType);
751 + BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(localNode);
752 + BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
753 + new RouteDistinguisher());
754 +
755 + BgpNodeLSIdentifier remoteNodeDescriptors = new BgpNodeLSIdentifier(remoteNode);
756 + BgpNodeLSNlriVer4 remNodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, remoteNodeDescriptors, false,
757 + new RouteDistinguisher());
758 +
759 + PathAttrNlriDetails details = new PathAttrNlriDetails();
760 + details.setIdentifier(0);
761 + details.setProtocolID(ProtocolType.DIRECT);
762 + List<BgpValueType> pathAttributes = new LinkedList<>();
763 + details.setPathAttribute(pathAttributes);
764 +
765 + tlv = LinkLocalRemoteIdentifiersTlv.of(99, 100);
766 + linkdes.add(tlv);
767 + BgpLinkLSIdentifier linkId = new BgpLinkLSIdentifier(localNode, remoteNode, linkdes);
768 + BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4((byte) Constants.DIRECT, 0, linkId,
769 + new RouteDistinguisher(), false);
770 +
771 + for (BgpNodeListener l : controller.nodeListener) {
772 + l.addNode(nodeNlri, details);
773 + assertThat(nodeRegistry.connected.size(), is(1));
774 + l.addNode(remNodeNlri, details);
775 + assertThat(nodeRegistry.connected.size(), is(2));
776 + l.deleteNode(remNodeNlri);
777 + assertThat(nodeRegistry.connected.size(), is(1));
778 + }
779 +
780 + List<BgpValueType> linkPathAttributes = new LinkedList<>();
781 + List<BgpValueType> linkStateAttr = new LinkedList<>();
782 + tlv = BgpLinkAttrIgpMetric.of(10, 4);
783 + linkStateAttr.add(tlv);
784 + tlv = BgpLinkAttrTeDefaultMetric.of(20);
785 + linkStateAttr.add(tlv);
786 + tlv = BgpLinkAttrMaxLinkBandwidth.of(30, LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH);
787 + linkStateAttr.add(tlv);
788 + linkPathAttributes.add(new LinkStateAttributes(linkStateAttr));
789 + details.setPathAttribute(linkPathAttributes);
790 +
791 + for (BgpLinkListener l : controller.linkListener) {
792 + l.addLink(linkNlri, details);
793 + assertThat(linkRegistry.links.size(), is(1));
794 + assertThat(linkRegistry.links.iterator().next().annotations().value(BgpTopologyProvider.COST),
795 + is("10"));
796 + assertThat(linkRegistry.links.iterator().next().annotations().value(BgpTopologyProvider.TE_COST),
797 + is("20"));
798 +
799 + l.deleteLink(linkNlri);
800 + assertThat(linkRegistry.links.size(), is(0));
801 + }
802 + }
803 +
804 + /**
463 * Invalid link. 805 * Invalid link.
464 * 806 *
465 * @throws BgpParseException while adding or deleting a link 807 * @throws BgpParseException while adding or deleting a link
...@@ -512,4 +854,188 @@ public class BgpTopologyProviderTest { ...@@ -512,4 +854,188 @@ public class BgpTopologyProviderTest {
512 assertThat(linkRegistry.links.size(), is(0)); 854 assertThat(linkRegistry.links.size(), is(0));
513 } 855 }
514 } 856 }
857 +
858 + /**
859 + * Add device check label registration is done.
860 + *
861 + * @throws BgpParseException while adding a device
862 + */
863 + @Test
864 + public void bgpTopologyProviderDeviceTestLabel1() throws BgpParseException {
865 + LinkedList<BgpValueType> subTlvs = new LinkedList<>();
866 + BgpValueType tlv = new AutonomousSystemTlv(100);
867 + short deslength = AutonomousSystemTlv.LENGTH;
868 + short desType = AutonomousSystemTlv.TYPE;
869 +
870 + subTlvs.add(tlv);
871 + BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
872 + desType));
873 + BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
874 + new RouteDistinguisher());
875 +
876 + PathAttrNlriDetails details = new PathAttrNlriDetails();
877 + details.setIdentifier(0);
878 + details.setProtocolID(ProtocolType.DIRECT);
879 + List<BgpValueType> pathAttributes = new LinkedList<>();
880 + List<BgpValueType> linkStateAttributes = new LinkedList<>();
881 + tlv = BgpAttrRouterIdV4.of(Ip4Address.valueOf("1.1.1.1"), LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID);
882 + linkStateAttributes.add(tlv);
883 + pathAttributes.add(new LinkStateAttributes(linkStateAttributes));
884 + details.setPathAttribute(pathAttributes);
885 +
886 + for (BgpNodeListener l : controller.nodeListener) {
887 + l.addNode(nodeNlri, details);
888 + assertThat(nodeRegistry.connected.size(), is(1));
889 + }
890 +
891 + MastershipEvent event = new MastershipEvent(Type.MASTER_CHANGED, nodeRegistry.connected.iterator().next(),
892 + new RoleInfo(NodeId.nodeId("Node1"), new LinkedList<>()));
893 +
894 + listener.event(event);
895 + assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(1));
896 + }
897 +
898 + /**
899 + * Add device check label registration is done and delete node destroy label pool.
900 + *
901 + * @throws BgpParseException while adding a device
902 + */
903 + @Test
904 + public void bgpTopologyProviderDeviceTestLabel2() throws BgpParseException {
905 + LinkedList<BgpValueType> subTlvs = new LinkedList<>();
906 + BgpValueType tlv = new AutonomousSystemTlv(100);
907 + short deslength = AutonomousSystemTlv.LENGTH;
908 + short desType = AutonomousSystemTlv.TYPE;
909 +
910 + subTlvs.add(tlv);
911 + BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(new NodeDescriptors(subTlvs, deslength,
912 + desType));
913 + BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
914 + new RouteDistinguisher());
915 +
916 + PathAttrNlriDetails details = new PathAttrNlriDetails();
917 + details.setIdentifier(0);
918 + details.setProtocolID(ProtocolType.DIRECT);
919 + List<BgpValueType> pathAttributes = new LinkedList<>();
920 + List<BgpValueType> linkStateAttributes = new LinkedList<>();
921 + tlv = BgpAttrRouterIdV4.of(Ip4Address.valueOf("1.1.1.1"), LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID);
922 + linkStateAttributes.add(tlv);
923 + pathAttributes.add(new LinkStateAttributes(linkStateAttributes));
924 + details.setPathAttribute(pathAttributes);
925 +
926 + for (BgpNodeListener l : controller.nodeListener) {
927 + l.addNode(nodeNlri, details);
928 + assertThat(nodeRegistry.connected.size(), is(1));
929 + // Check label resource reserved for that device
930 + MastershipEvent event = new MastershipEvent(Type.MASTER_CHANGED, nodeRegistry.connected.iterator().next(),
931 + new RoleInfo(NodeId.nodeId("Node1"), new LinkedList<>()));
932 + listener.event(event);
933 + assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(1));
934 +
935 + l.deleteNode(nodeNlri);
936 + assertThat(nodeRegistry.connected.size(), is(0));
937 + assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(0));
938 + }
939 + }
940 +
941 + /**
942 + * Add a link register bandwidth and remove link unregister bandwidth.
943 + *
944 + * @throws BgpParseException while registering/unregistering bandwidth
945 + */
946 + @Test
947 + public void bgpTopologyProviderDeviceTestLabel3() throws BgpParseException {
948 + LinkedList<BgpValueType> localTlvs = new LinkedList<>();
949 + LinkedList<BgpValueType> remoteTlvs = new LinkedList<>();
950 + LinkedList<BgpValueType> linkdes = new LinkedList<>();
951 + BgpValueType tlv = new AutonomousSystemTlv(10);
952 + short deslength = AutonomousSystemTlv.LENGTH;
953 + short desType = AutonomousSystemTlv.TYPE;
954 +
955 + localTlvs.add(tlv);
956 + remoteTlvs.add(tlv);
957 + tlv = IsIsNonPseudonode.of(new byte[] {20, 20, 20, 20, 00, 20});
958 + localTlvs.add(tlv);
959 + tlv = IsIsNonPseudonode.of(new byte[] {30, 30, 30, 30, 00, 30});
960 + remoteTlvs.add(tlv);
961 + NodeDescriptors localNode = new NodeDescriptors(localTlvs, deslength, desType);
962 + NodeDescriptors remoteNode = new NodeDescriptors(remoteTlvs, deslength, desType);
963 + BgpNodeLSIdentifier localNodeDescriptors = new BgpNodeLSIdentifier(localNode);
964 + BgpNodeLSNlriVer4 nodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, localNodeDescriptors, false,
965 + new RouteDistinguisher());
966 +
967 + BgpNodeLSIdentifier remoteNodeDescriptors = new BgpNodeLSIdentifier(remoteNode);
968 + BgpNodeLSNlriVer4 remNodeNlri = new BgpNodeLSNlriVer4(0, (byte) Constants.DIRECT, remoteNodeDescriptors, false,
969 + new RouteDistinguisher());
970 +
971 + PathAttrNlriDetails details = new PathAttrNlriDetails();
972 + details.setIdentifier(0);
973 + details.setProtocolID(ProtocolType.DIRECT);
974 + List<BgpValueType> pathAttributes = new LinkedList<>();
975 + List<BgpValueType> linkStateAttributes = new LinkedList<>();
976 + tlv = BgpAttrRouterIdV4.of(Ip4Address.valueOf("1.1.1.1"), LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID);
977 + linkStateAttributes.add(tlv);
978 + pathAttributes.add(new LinkStateAttributes(linkStateAttributes));
979 + details.setPathAttribute(pathAttributes);
980 +
981 + tlv = LinkLocalRemoteIdentifiersTlv.of(99, 100);
982 + linkdes.add(tlv);
983 + BgpLinkLSIdentifier linkId = new BgpLinkLSIdentifier(localNode, remoteNode, linkdes);
984 + BgpLinkLsNlriVer4 linkNlri = new BgpLinkLsNlriVer4((byte) Constants.DIRECT, 0, linkId,
985 + new RouteDistinguisher(), false);
986 +
987 + for (BgpNodeListener l : controller.nodeListener) {
988 + l.addNode(nodeNlri, details);
989 + assertThat(nodeRegistry.connected.size(), is(1));
990 + //Check label resource reserved for that device
991 + MastershipEvent event = new MastershipEvent(Type.MASTER_CHANGED, nodeRegistry.connected.iterator().next(),
992 + new RoleInfo(NodeId.nodeId("Node1"), new LinkedList<>()));
993 + listener.event(event);
994 + assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(1));
995 + l.addNode(remNodeNlri, details);
996 + assertThat(nodeRegistry.connected.size(), is(2));
997 + l.deleteNode(remNodeNlri);
998 + assertThat(nodeRegistry.connected.size(), is(1));
999 + assertThat(labelResourceAdminService.resourcePool.keySet().size(), is(1));
1000 + }
1001 +
1002 + List<BgpValueType> linkPathAttributes = new LinkedList<>();
1003 + List<BgpValueType> linkStateAttr = new LinkedList<>();
1004 + tlv = BgpLinkAttrIgpMetric.of(10, 4);
1005 + linkStateAttr.add(tlv);
1006 + tlv = BgpLinkAttrTeDefaultMetric.of(20);
1007 + linkStateAttr.add(tlv);
1008 + tlv = BgpLinkAttrMaxLinkBandwidth.of(70, LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH);
1009 + linkStateAttr.add(tlv);
1010 + linkPathAttributes.add(new LinkStateAttributes(linkStateAttr));
1011 + details.setPathAttribute(linkPathAttributes);
1012 +
1013 + for (BgpLinkListener l : controller.linkListener) {
1014 + l.addLink(linkNlri, details);
1015 + assertThat(linkRegistry.links.size(), is(1));
1016 + assertThat(linkRegistry.links.iterator().next().annotations().value(BgpTopologyProvider.COST),
1017 + is("10"));
1018 + assertThat(linkRegistry.links.iterator().next().annotations().value(BgpTopologyProvider.TE_COST),
1019 + is("20"));
1020 +
1021 + ConnectPoint src = new ConnectPoint(
1022 + DeviceId.deviceId("l3:rd=0::routinguniverse=0:asn=10:isoid=1414.1414.0014"),
1023 + PortNumber.portNumber(4294967395L));
1024 + ConnectPoint dst = new ConnectPoint(
1025 + DeviceId.deviceId("l3:rd=0::routinguniverse=0:asn=10:isoid=1e1e.1e1e.001e"),
1026 + PortNumber.portNumber(4294967396L));
1027 + BandwidthCapacity bandwidth = networkConfigService.getConfig(src, BandwidthCapacity.class);
1028 + assertThat(bandwidth.capacity().bps(), is(70.0 * 1_000_000L));
1029 +
1030 + bandwidth = networkConfigService.getConfig(dst, BandwidthCapacity.class);
1031 + assertThat(bandwidth.capacity(), is(Bandwidth.bps(70.0 * 1_000_000L)));
1032 +
1033 + l.deleteLink(linkNlri);
1034 + assertThat(linkRegistry.links.size(), is(0));
1035 + bandwidth = networkConfigService.getConfig(src, BandwidthCapacity.class);
1036 + assertThat(bandwidth, is(nullValue()));
1037 + bandwidth = networkConfigService.getConfig(dst, BandwidthCapacity.class);
1038 + assertThat(bandwidth, is(nullValue()));
1039 + }
1040 + }
515 } 1041 }
......