blob: 83fc69365fb6030d14296e0b7e4d30c2dd9a9b2c [file] [log] [blame]
Shashikanth VH1ca26ce2015-11-20 23:19:49 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 */
13
14package org.onosproject.provider.bgp.topology.impl;
15
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053016import static org.onosproject.bgp.controller.BgpDpid.uri;
17import static org.onosproject.net.DeviceId.deviceId;
Priyanka Bfc51c952016-03-26 14:30:33 +053018import static org.onosproject.net.Device.Type.ROUTER;
19import static org.onosproject.net.Device.Type.VIRTUAL;
Priyanka B658aa6982016-05-27 21:34:49 +053020import static org.onosproject.incubator.net.resource.label.LabelResourceId.labelResourceId;
21import static java.util.stream.Collectors.toList;
Priyanka Bfc51c952016-03-26 14:30:33 +053022
Priyanka B658aa6982016-05-27 21:34:49 +053023import java.util.LinkedList;
Priyanka Bfc51c952016-03-26 14:30:33 +053024import java.util.List;
25import java.util.Set;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053026
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053027import org.onlab.packet.ChassisId;
Priyanka B658aa6982016-05-27 21:34:49 +053028import org.onlab.packet.Ip4Address;
29import org.onlab.util.Bandwidth;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053030import org.apache.felix.scr.annotations.Activate;
31import org.apache.felix.scr.annotations.Component;
32import org.apache.felix.scr.annotations.Deactivate;
33import org.apache.felix.scr.annotations.Reference;
34import org.apache.felix.scr.annotations.ReferenceCardinality;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053035import org.onosproject.bgp.controller.BgpController;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053036import org.onosproject.bgp.controller.BgpDpid;
Priyanka Bfc51c952016-03-26 14:30:33 +053037import org.onosproject.bgp.controller.BgpLinkListener;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053038import org.onosproject.bgp.controller.BgpNodeListener;
Priyanka Bfc51c952016-03-26 14:30:33 +053039import org.onosproject.bgpio.exceptions.BgpParseException;
40import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
41import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
42import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053043import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Priyanka Bfc51c952016-03-26 14:30:33 +053044import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
45import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
46import org.onosproject.bgpio.types.AutonomousSystemTlv;
47import org.onosproject.bgpio.types.BgpLSIdentifierTlv;
48import org.onosproject.bgpio.types.BgpValueType;
49import org.onosproject.bgpio.types.IPv4AddressTlv;
50import org.onosproject.bgpio.types.IsIsNonPseudonode;
51import org.onosproject.bgpio.types.IsIsPseudonode;
52import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv;
Priyanka B658aa6982016-05-27 21:34:49 +053053import org.onosproject.bgpio.types.LinkStateAttributes;
Priyanka Bfc51c952016-03-26 14:30:33 +053054import org.onosproject.bgpio.types.OspfNonPseudonode;
55import org.onosproject.bgpio.types.OspfPseudonode;
Priyanka B658aa6982016-05-27 21:34:49 +053056import org.onosproject.bgpio.types.attr.BgpAttrNodeFlagBitTlv;
57import org.onosproject.bgpio.types.attr.BgpAttrNodeIsIsAreaId;
58import org.onosproject.bgpio.types.attr.BgpAttrRouterIdV4;
59import org.onosproject.bgpio.types.attr.BgpLinkAttrIgpMetric;
60import org.onosproject.bgpio.types.attr.BgpLinkAttrMaxLinkBandwidth;
61import org.onosproject.bgpio.types.attr.BgpLinkAttrTeDefaultMetric;
Priyanka Bfc51c952016-03-26 14:30:33 +053062import org.onosproject.core.CoreService;
Priyanka B658aa6982016-05-27 21:34:49 +053063import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
64import org.onosproject.incubator.net.resource.label.LabelResourceId;
65import org.onosproject.mastership.MastershipEvent;
66import org.onosproject.mastership.MastershipListener;
67import org.onosproject.mastership.MastershipService;
Priyanka Bfc51c952016-03-26 14:30:33 +053068import org.onosproject.net.AnnotationKeys;
69import org.onosproject.net.ConnectPoint;
70import org.onosproject.net.DefaultAnnotations;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053071import org.onosproject.net.Device;
72import org.onosproject.net.DeviceId;
Priyanka Bfc51c952016-03-26 14:30:33 +053073import org.onosproject.net.Link;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053074import org.onosproject.net.MastershipRole;
Saurav Dasa2d37502016-03-25 17:50:40 -070075import org.onosproject.net.PortNumber;
Priyanka B658aa6982016-05-27 21:34:49 +053076import org.onosproject.net.config.NetworkConfigService;
Sho SHIMIZUb6c63a32016-05-26 12:07:19 -070077import org.onosproject.net.config.basics.BandwidthCapacity;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053078import org.onosproject.net.device.DefaultDeviceDescription;
Priyanka B658aa6982016-05-27 21:34:49 +053079import org.onosproject.net.device.DefaultPortDescription;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053080import org.onosproject.net.device.DeviceDescription;
81import org.onosproject.net.device.DeviceProvider;
82import org.onosproject.net.device.DeviceProviderRegistry;
83import org.onosproject.net.device.DeviceProviderService;
Priyanka Bfc51c952016-03-26 14:30:33 +053084import org.onosproject.net.device.DeviceService;
Priyanka B658aa6982016-05-27 21:34:49 +053085import org.onosproject.net.device.PortDescription;
Priyanka Bfc51c952016-03-26 14:30:33 +053086import org.onosproject.net.link.DefaultLinkDescription;
87import org.onosproject.net.link.LinkDescription;
88import org.onosproject.net.link.LinkProvider;
89import org.onosproject.net.link.LinkProviderRegistry;
90import org.onosproject.net.link.LinkProviderService;
91import org.onosproject.net.link.LinkService;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053092import org.onosproject.net.provider.AbstractProvider;
93import org.onosproject.net.provider.ProviderId;
94import org.slf4j.Logger;
95import org.slf4j.LoggerFactory;
96
97/**
98 * Provider which uses an BGP controller to detect network infrastructure topology.
99 */
100@Component(immediate = true)
Priyanka Bfc51c952016-03-26 14:30:33 +0530101public class BgpTopologyProvider extends AbstractProvider implements DeviceProvider, LinkProvider {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530102
Priyanka B9bee0802016-04-27 22:06:02 +0530103 /**
104 * Creates an instance of BGP topology provider.
105 */
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530106 public BgpTopologyProvider() {
Priyanka B9bee0802016-04-27 22:06:02 +0530107 super(new ProviderId("l3", "org.onosproject.provider.bgp"));
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530108 }
109
110 private static final Logger log = LoggerFactory.getLogger(BgpTopologyProvider.class);
111
112 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530113 protected DeviceProviderRegistry deviceProviderRegistry;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Priyanka Bfc51c952016-03-26 14:30:33 +0530116 protected LinkProviderRegistry linkProviderRegistry;
117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530119 protected BgpController controller;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530120
Priyanka Bfc51c952016-03-26 14:30:33 +0530121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
122 protected LinkService linkService;
123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
125 protected DeviceService deviceService;
126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
128 protected CoreService coreService;
129
Priyanka B658aa6982016-05-27 21:34:49 +0530130 @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
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530139 private DeviceProviderService deviceProviderService;
Priyanka Bfc51c952016-03-26 14:30:33 +0530140 private LinkProviderService linkProviderService;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530141
Priyanka B658aa6982016-05-27 21:34:49 +0530142 private InternalMastershipListener masterListener = new InternalMastershipListener();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530143 private InternalBgpProvider listener = new InternalBgpProvider();
144 private static final String UNKNOWN = "unknown";
Priyanka Bfc51c952016-03-26 14:30:33 +0530145 public static final long IDENTIFIER_SET = 0x100000000L;
146 public static final String AS_NUMBER = "asNumber";
147 public static final String DOMAIN_IDENTIFIER = "domainIdentifier";
148 public static final String ROUTING_UNIVERSE = "routingUniverse";
Priyanka B658aa6982016-05-27 21:34:49 +0530149
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
Priyanka Bfc51c952016-03-26 14:30:33 +0530159 public static final long PSEUDO_PORT = 0xffffffff;
Priyanka B658aa6982016-05-27 21:34:49 +0530160 public static final int DELAY = 2;
161 private LabelResourceId beginLabel = labelResourceId(5122);
162 private LabelResourceId endLabel = labelResourceId(9217);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530163
164 @Activate
165 public void activate() {
Priyanka Bfc51c952016-03-26 14:30:33 +0530166 log.debug("BgpTopologyProvider activate");
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530167 deviceProviderService = deviceProviderRegistry.register(this);
Priyanka Bfc51c952016-03-26 14:30:33 +0530168 linkProviderService = linkProviderRegistry.register(this);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530169 controller.addListener(listener);
Priyanka B658aa6982016-05-27 21:34:49 +0530170 mastershipService.addListener(masterListener);
Priyanka Bfc51c952016-03-26 14:30:33 +0530171 controller.addLinkListener(listener);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530172 }
173
174 @Deactivate
175 public void deactivate() {
Priyanka Bfc51c952016-03-26 14:30:33 +0530176 log.debug("BgpTopologyProvider deactivate");
177 deviceProviderRegistry.unregister(this);
178 deviceProviderService = null;
179 linkProviderRegistry.unregister(this);
180 linkProviderService = null;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530181 controller.removeListener(listener);
Priyanka Bfc51c952016-03-26 14:30:33 +0530182 controller.removeLinkListener(listener);
Priyanka B658aa6982016-05-27 21:34:49 +0530183 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 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530204 }
205
206 /*
207 * Implements device and link update.
208 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530209 private class InternalBgpProvider implements BgpNodeListener, BgpLinkListener {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530210
211 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530212 public void addNode(BgpNodeLSNlriVer4 nodeNlri, PathAttrNlriDetails details) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530213 log.debug("Add node {}", nodeNlri.toString());
214
Priyanka B658aa6982016-05-27 21:34:49 +0530215 if (deviceProviderService == null || deviceService == null) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530216 return;
217 }
Priyanka Bfc51c952016-03-26 14:30:33 +0530218 Device.Type deviceType = ROUTER;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530219 BgpDpid nodeUri = new BgpDpid(nodeNlri);
220 DeviceId deviceId = deviceId(uri(nodeUri.toString()));
221 ChassisId cId = new ChassisId();
222
Priyanka B658aa6982016-05-27 21:34:49 +0530223 /*
224 * Check if device is already there (available) , if yes not updating to core.
225 */
226 if (deviceService.isAvailable(deviceId)) {
227 return;
228 }
229
Priyanka Bfc51c952016-03-26 14:30:33 +0530230 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
231
232 newBuilder.set(AnnotationKeys.TYPE, "L3");
233 newBuilder.set(ROUTING_UNIVERSE, Long.toString(nodeNlri.getIdentifier()));
234
235 List<BgpValueType> tlvs = nodeNlri.getLocalNodeDescriptors().getNodedescriptors().getSubTlvs();
236 for (BgpValueType tlv : tlvs) {
237 if (tlv instanceof AutonomousSystemTlv) {
238 newBuilder.set(AS_NUMBER, Integer.toString(((AutonomousSystemTlv) tlv).getAsNum()));
239 } else if (tlv instanceof BgpLSIdentifierTlv) {
240 newBuilder.set(DOMAIN_IDENTIFIER,
241 Integer.toString(((BgpLSIdentifierTlv) tlv).getBgpLsIdentifier()));
242 }
243 if (tlv.getType() == NodeDescriptors.IGP_ROUTERID_TYPE) {
244 if (tlv instanceof IsIsPseudonode) {
245 deviceType = VIRTUAL;
Priyanka B19c08732016-06-04 21:11:19 +0530246 newBuilder.set(AnnotationKeys.ROUTER_ID, nodeUri.isoNodeIdString(((IsIsPseudonode) tlv)
247 .getIsoNodeId()));
Priyanka Bfc51c952016-03-26 14:30:33 +0530248 } else if (tlv instanceof OspfPseudonode) {
249 deviceType = VIRTUAL;
250 newBuilder
251 .set(AnnotationKeys.ROUTER_ID, Integer.toString(((OspfPseudonode) tlv).getrouterID()));
252 } else if (tlv instanceof IsIsNonPseudonode) {
Priyanka B19c08732016-06-04 21:11:19 +0530253 newBuilder.set(AnnotationKeys.ROUTER_ID, nodeUri.isoNodeIdString(((IsIsNonPseudonode) tlv)
254 .getIsoNodeId()));
Priyanka Bfc51c952016-03-26 14:30:33 +0530255 } else if (tlv instanceof OspfNonPseudonode) {
256 newBuilder.set(AnnotationKeys.ROUTER_ID,
257 Integer.toString(((OspfNonPseudonode) tlv).getrouterID()));
258 }
259 }
260 }
Priyanka B658aa6982016-05-27 21:34:49 +0530261 DefaultAnnotations.Builder anntotations = DefaultAnnotations.builder();
262 anntotations = getAnnotations(newBuilder, true, details);
Priyanka Bfc51c952016-03-26 14:30:33 +0530263
264 DeviceDescription description = new DefaultDeviceDescription(uri(nodeUri.toString()), deviceType, UNKNOWN,
Priyanka B658aa6982016-05-27 21:34:49 +0530265 UNKNOWN, UNKNOWN, UNKNOWN, cId, anntotations.build());
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530266
Priyanka B658aa6982016-05-27 21:34:49 +0530267 deviceProviderService.deviceConnected(deviceId, description);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530268 }
269
270 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530271 public void deleteNode(BgpNodeLSNlriVer4 nodeNlri) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530272 log.debug("Delete node {}", nodeNlri.toString());
273
274 if (deviceProviderService == null) {
275 return;
276 }
277
Priyanka Bfc51c952016-03-26 14:30:33 +0530278 BgpDpid deviceUri = new BgpDpid(nodeNlri);
279 DeviceId deviceId = deviceId(uri(deviceUri.toString()));
Priyanka B658aa6982016-05-27 21:34:49 +0530280
281 if (labelResourceAdminService != null) {
282 //Destroy local device label pool reserved for that device
283 labelResourceAdminService.destroyDevicePool(deviceId);
284 }
285
Priyanka Bfc51c952016-03-26 14:30:33 +0530286 deviceProviderService.deviceDisconnected(deviceId);
287 }
288
289 @Override
290 public void addLink(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details) throws BgpParseException {
291 log.debug("Addlink {}", linkNlri.toString());
292
293 if (linkProviderService == null) {
294 return;
295 }
296 LinkDescription linkDes = buildLinkDes(linkNlri, details, true);
Priyanka B658aa6982016-05-27 21:34:49 +0530297
298 /*
299 * Update link ports and configure bandwidth on source and destination port using networkConfig service
300 * Only master of source link registers for bandwidth
301 */
302 if (mastershipService.isLocalMaster(linkDes.src().deviceId())) {
303 registerBandwidth(linkDes, details);
304 }
305
306 //Updating ports of the link
307 List<PortDescription> srcPortDescriptions = new LinkedList<>();
308 srcPortDescriptions.add(new DefaultPortDescription(linkDes.src().port(), true));
309 deviceProviderService.updatePorts(linkDes.src().deviceId(), srcPortDescriptions);
310
311 List<PortDescription> dstPortDescriptions = new LinkedList<>();
312 dstPortDescriptions.add(new DefaultPortDescription(linkDes.dst().port(), true));
313 deviceProviderService.updatePorts(linkDes.dst().deviceId(), dstPortDescriptions);
314
Priyanka Bfc51c952016-03-26 14:30:33 +0530315 linkProviderService.linkDetected(linkDes);
316 }
317
318 //Build link description.
319 private LinkDescription buildLinkDes(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details, boolean isAddLink)
320 throws BgpParseException {
321 long srcAddress = 0;
322 long dstAddress = 0;
323 boolean localPseduo = false;
324 boolean remotePseduo = false;
325
326 List<BgpValueType> localTlvs = linkNlri.getLinkIdentifier().localNodeDescriptors().getSubTlvs();
327 for (BgpValueType localTlv : localTlvs) {
328 if (localTlv instanceof IsIsPseudonode || localTlv instanceof OspfPseudonode) {
329 localPseduo = true;
330 }
331 }
332 List<BgpValueType> remoteTlvs = linkNlri.getLinkIdentifier().remoteNodeDescriptors().getSubTlvs();
333 for (BgpValueType remoteTlv : remoteTlvs) {
334 if (remoteTlv instanceof IsIsPseudonode || remoteTlv instanceof OspfPseudonode) {
335 remotePseduo = true;
336 }
337 }
338
339 List<BgpValueType> tlvs = linkNlri.getLinkIdentifier().linkDescriptors();
340 for (BgpValueType tlv : tlvs) {
341 if (tlv instanceof LinkLocalRemoteIdentifiersTlv) {
342 srcAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkLocalIdentifier();
343 //Set 32nd bit.
344 srcAddress = srcAddress | IDENTIFIER_SET;
345 dstAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkRemoteIdentifier();
346 dstAddress = dstAddress | IDENTIFIER_SET;
347 } else if (tlv instanceof IPv4AddressTlv) {
348 if (tlv.getType() == BgpLinkLSIdentifier.IPV4_INTERFACE_ADDRESS_TYPE) {
349 srcAddress = ((IPv4AddressTlv) tlv).address().toInt();
350 } else {
351 dstAddress = ((IPv4AddressTlv) tlv).address().toInt();
352 }
353 }
354 }
355
356 DeviceId srcId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_LOCAL).toString()));
357 DeviceId dstId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_REMOTE).toString()));
358
359 if (localPseduo && srcAddress == 0) {
360 srcAddress = PSEUDO_PORT;
361 } else if (remotePseduo && dstAddress == 0) {
362 dstAddress = PSEUDO_PORT;
363 }
364
365 ConnectPoint src = new ConnectPoint(srcId, PortNumber.portNumber(srcAddress));
366 ConnectPoint dst = new ConnectPoint(dstId, PortNumber.portNumber(dstAddress));
367 BgpNodeLSNlriVer4 srcNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
368 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().localNodeDescriptors()), false,
369 linkNlri.getRouteDistinguisher());
370
371 BgpNodeLSNlriVer4 dstNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
372 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().remoteNodeDescriptors()), false,
373 linkNlri.getRouteDistinguisher());
374
375 addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri,
376 dstNodeNlri, srcId, dstId, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530377 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
378 if (details != null) {
379 annotationBuilder = getAnnotations(annotationBuilder, false, details);
380 }
381
382 return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false, annotationBuilder.build());
Priyanka Bfc51c952016-03-26 14:30:33 +0530383 }
384
385 private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo,
386 BgpNodeLSNlriVer4 srcNodeNlri, BgpNodeLSNlriVer4 dstNodeNlri, DeviceId srcId, DeviceId dstId,
387 PathAttrNlriDetails details) {
388 if (isAddLink) {
389 if (localPseduo) {
390 if (deviceService.getDevice(srcId) == null) {
391 for (BgpNodeListener l : controller.listener()) {
392 l.addNode(srcNodeNlri, details);
393 }
394 }
395 } else if (remotePseduo) {
396 if (deviceService.getDevice(dstId) == null) {
397 for (BgpNodeListener l : controller.listener()) {
398 l.addNode(dstNodeNlri, details);
399 }
400 }
401 }
402 } else {
403 if (localPseduo) {
404 Set<Link> links = linkService.getDeviceLinks(srcId);
405 if (links == null || links.isEmpty()) {
406 for (BgpNodeListener l : controller.listener()) {
407 l.deleteNode(srcNodeNlri);
408 }
409 }
410 } else if (remotePseduo) {
411 log.info("Remote pseudo delete link ");
412 Set<Link> links = linkService.getDeviceLinks(dstId);
413 if (links == null || links.isEmpty()) {
414 for (BgpNodeListener l : controller.listener()) {
415 l.deleteNode(dstNodeNlri);
416 }
417 }
418 }
419 }
420 }
421
422 @Override
423 public void deleteLink(BgpLinkLsNlriVer4 linkNlri) throws BgpParseException {
424 log.debug("Delete link {}", linkNlri.toString());
425
426 if (linkProviderService == null) {
427 return;
428 }
429
430 LinkDescription linkDes = buildLinkDes(linkNlri, null, false);
Priyanka B658aa6982016-05-27 21:34:49 +0530431
432 /*
433 * Only master for the link src will release the bandwidth resource.
434 */
435 if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
436 // Releases registered resource for this link
437 networkConfigService.removeConfig(linkDes.src(), BandwidthCapacity.class);
438 networkConfigService.removeConfig(linkDes.dst(), BandwidthCapacity.class);
439 }
440
Priyanka Bfc51c952016-03-26 14:30:33 +0530441 linkProviderService.linkVanished(linkDes);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530442 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530443 }
444
Priyanka B658aa6982016-05-27 21:34:49 +0530445 // Creates label resource pool for the specific device. For all devices label range is 5122-9217
446 private void createDevicePool(DeviceId deviceId) {
447 if (labelResourceAdminService == null) {
448 return;
449 }
450
451 labelResourceAdminService.createDevicePool(deviceId, beginLabel, endLabel);
452 }
453
454 private void registerBandwidth(LinkDescription linkDes, PathAttrNlriDetails details) {
455 if (details == null) {
456 log.error("Couldnot able to register bandwidth ");
457 return;
458 }
459
460 List<BgpValueType> attribute = details.pathAttributes().stream()
461 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
462 if (attribute.isEmpty()) {
463 return;
464 }
465
466 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
467 float maxReservableBw = 0;
468
469 for (BgpValueType tlv : tlvs) {
470 switch (tlv.getType()) {
471 case LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH:
472 maxReservableBw = ((BgpLinkAttrMaxLinkBandwidth) tlv).linkAttrMaxLinkBandwidth();
473 break;
474 default: // do nothing
475 }
476 }
477
478 if (maxReservableBw == 0.0) {
479 return;
480 }
481
482 //Configure bandwidth for src and dst port
483 BandwidthCapacity config = networkConfigService.addConfig(linkDes.src(), BandwidthCapacity.class);
484 config.capacity(Bandwidth.bps(maxReservableBw)).apply();
485
486 config = networkConfigService.addConfig(linkDes.dst(), BandwidthCapacity.class);
487 config.capacity(Bandwidth.bps(maxReservableBw)).apply();
488 }
489
490 private DefaultAnnotations.Builder getAnnotations(DefaultAnnotations.Builder annotationBuilder, boolean isNode,
491 PathAttrNlriDetails details) {
492
493 List<BgpValueType> attribute = details.pathAttributes().stream()
494 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
495 if (attribute.isEmpty()) {
496 return annotationBuilder;
497 }
498 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
499 boolean abrBit = false;
500 boolean externalBit = false;
501 boolean pseudo = false;
502 int igpMetric = 0;
503 int teMetric = 0;
504 byte[] areaId = null;
505 Ip4Address routerId = null;
506 for (BgpValueType tlv : tlvs) {
507 switch (tlv.getType()) {
508 case LinkStateAttributes.ATTR_NODE_FLAG_BITS:
509 abrBit = ((BgpAttrNodeFlagBitTlv) tlv).abrBit();
510 externalBit = ((BgpAttrNodeFlagBitTlv) tlv).externalBit();
511 break;
512 case NodeDescriptors.IGP_ROUTERID_TYPE:
513 if (tlv instanceof IsIsPseudonode || tlv instanceof OspfPseudonode) {
514 pseudo = true;
515 }
516 break;
517 case LinkStateAttributes.ATTR_NODE_ISIS_AREA_ID:
518 areaId = ((BgpAttrNodeIsIsAreaId) tlv).attrNodeIsIsAreaId();
519 break;
520 case LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID:
521 routerId = ((BgpAttrRouterIdV4) tlv).attrRouterId();
522 break;
523 case LinkStateAttributes.ATTR_LINK_IGP_METRIC:
524 igpMetric = ((BgpLinkAttrIgpMetric) tlv).attrLinkIgpMetric();
525 break;
526 case LinkStateAttributes.ATTR_LINK_TE_DEFAULT_METRIC:
527 teMetric = ((BgpLinkAttrTeDefaultMetric) tlv).attrLinkDefTeMetric();
528 break;
529 default: // do nothing
530 }
531 }
532
533 // Annotations for device
534 if (isNode) {
535 boolean internalBit = false;
536 if (!abrBit && !externalBit) {
537 internalBit = true;
538 }
539
540 annotationBuilder.set(EXTERNAL_BIT, String.valueOf(externalBit));
541 annotationBuilder.set(ABR_BIT, String.valueOf(abrBit));
542 annotationBuilder.set(INTERNAL_BIT, String.valueOf(internalBit));
543 annotationBuilder.set(PSEUDO, String.valueOf(pseudo));
544
545 if (areaId != null) {
546 annotationBuilder.set(AREAID, new String(areaId));
547 }
548 if (routerId != null) {
549 // LsrID
550 annotationBuilder.set(LSRID, String.valueOf(routerId));
551 }
552 } else {
553 // Annotations for link
554 if (igpMetric != 0) {
555 annotationBuilder.set(COST, String.valueOf(igpMetric));
556 }
557
558 if (teMetric != 0) {
559 annotationBuilder.set(TE_COST, String.valueOf(teMetric));
560 }
561 }
562 return annotationBuilder;
563 }
564
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530565 @Override
566 public void triggerProbe(DeviceId deviceId) {
567 // TODO Auto-generated method stub
568 }
569
570 @Override
571 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
572 }
573
574 @Override
575 public boolean isReachable(DeviceId deviceId) {
576 // TODO Auto-generated method stub
577 return true;
578 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700579
580 @Override
581 public void changePortState(DeviceId deviceId, PortNumber portNumber,
582 boolean enable) {
583 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530584}