blob: 0d1d1ea13cba7e3311cdbf000c0a0fef956c640d [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;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053077import org.onosproject.net.device.DefaultDeviceDescription;
Priyanka B658aa6982016-05-27 21:34:49 +053078import org.onosproject.net.device.DefaultPortDescription;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053079import org.onosproject.net.device.DeviceDescription;
80import org.onosproject.net.device.DeviceProvider;
81import org.onosproject.net.device.DeviceProviderRegistry;
82import org.onosproject.net.device.DeviceProviderService;
Priyanka Bfc51c952016-03-26 14:30:33 +053083import org.onosproject.net.device.DeviceService;
Priyanka B658aa6982016-05-27 21:34:49 +053084import org.onosproject.net.device.PortDescription;
Priyanka Bfc51c952016-03-26 14:30:33 +053085import org.onosproject.net.link.DefaultLinkDescription;
86import org.onosproject.net.link.LinkDescription;
87import org.onosproject.net.link.LinkProvider;
88import org.onosproject.net.link.LinkProviderRegistry;
89import org.onosproject.net.link.LinkProviderService;
90import org.onosproject.net.link.LinkService;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053091import org.onosproject.net.provider.AbstractProvider;
92import org.onosproject.net.provider.ProviderId;
Priyanka B658aa6982016-05-27 21:34:49 +053093import org.onosproject.net.resource.BandwidthCapacity;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053094import 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;
246 newBuilder.set(AnnotationKeys.ROUTER_ID, new String(((IsIsPseudonode) tlv).getIsoNodeId()));
247 } else if (tlv instanceof OspfPseudonode) {
248 deviceType = VIRTUAL;
249 newBuilder
250 .set(AnnotationKeys.ROUTER_ID, Integer.toString(((OspfPseudonode) tlv).getrouterID()));
251 } else if (tlv instanceof IsIsNonPseudonode) {
252 newBuilder.set(AnnotationKeys.ROUTER_ID, new String(((IsIsNonPseudonode) tlv).getIsoNodeId()));
253 } else if (tlv instanceof OspfNonPseudonode) {
254 newBuilder.set(AnnotationKeys.ROUTER_ID,
255 Integer.toString(((OspfNonPseudonode) tlv).getrouterID()));
256 }
257 }
258 }
Priyanka B658aa6982016-05-27 21:34:49 +0530259 DefaultAnnotations.Builder anntotations = DefaultAnnotations.builder();
260 anntotations = getAnnotations(newBuilder, true, details);
Priyanka Bfc51c952016-03-26 14:30:33 +0530261
262 DeviceDescription description = new DefaultDeviceDescription(uri(nodeUri.toString()), deviceType, UNKNOWN,
Priyanka B658aa6982016-05-27 21:34:49 +0530263 UNKNOWN, UNKNOWN, UNKNOWN, cId, anntotations.build());
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530264
Priyanka B658aa6982016-05-27 21:34:49 +0530265 deviceProviderService.deviceConnected(deviceId, description);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530266 }
267
268 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530269 public void deleteNode(BgpNodeLSNlriVer4 nodeNlri) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530270 log.debug("Delete node {}", nodeNlri.toString());
271
272 if (deviceProviderService == null) {
273 return;
274 }
275
Priyanka Bfc51c952016-03-26 14:30:33 +0530276 BgpDpid deviceUri = new BgpDpid(nodeNlri);
277 DeviceId deviceId = deviceId(uri(deviceUri.toString()));
Priyanka B658aa6982016-05-27 21:34:49 +0530278
279 if (labelResourceAdminService != null) {
280 //Destroy local device label pool reserved for that device
281 labelResourceAdminService.destroyDevicePool(deviceId);
282 }
283
Priyanka Bfc51c952016-03-26 14:30:33 +0530284 deviceProviderService.deviceDisconnected(deviceId);
285 }
286
287 @Override
288 public void addLink(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details) throws BgpParseException {
289 log.debug("Addlink {}", linkNlri.toString());
290
291 if (linkProviderService == null) {
292 return;
293 }
294 LinkDescription linkDes = buildLinkDes(linkNlri, details, true);
Priyanka B658aa6982016-05-27 21:34:49 +0530295
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
Priyanka Bfc51c952016-03-26 14:30:33 +0530313 linkProviderService.linkDetected(linkDes);
314 }
315
316 //Build link description.
317 private LinkDescription buildLinkDes(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details, boolean isAddLink)
318 throws BgpParseException {
319 long srcAddress = 0;
320 long dstAddress = 0;
321 boolean localPseduo = false;
322 boolean remotePseduo = false;
323
324 List<BgpValueType> localTlvs = linkNlri.getLinkIdentifier().localNodeDescriptors().getSubTlvs();
325 for (BgpValueType localTlv : localTlvs) {
326 if (localTlv instanceof IsIsPseudonode || localTlv instanceof OspfPseudonode) {
327 localPseduo = true;
328 }
329 }
330 List<BgpValueType> remoteTlvs = linkNlri.getLinkIdentifier().remoteNodeDescriptors().getSubTlvs();
331 for (BgpValueType remoteTlv : remoteTlvs) {
332 if (remoteTlv instanceof IsIsPseudonode || remoteTlv instanceof OspfPseudonode) {
333 remotePseduo = true;
334 }
335 }
336
337 List<BgpValueType> tlvs = linkNlri.getLinkIdentifier().linkDescriptors();
338 for (BgpValueType tlv : tlvs) {
339 if (tlv instanceof LinkLocalRemoteIdentifiersTlv) {
340 srcAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkLocalIdentifier();
341 //Set 32nd bit.
342 srcAddress = srcAddress | IDENTIFIER_SET;
343 dstAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkRemoteIdentifier();
344 dstAddress = dstAddress | IDENTIFIER_SET;
345 } else if (tlv instanceof IPv4AddressTlv) {
346 if (tlv.getType() == BgpLinkLSIdentifier.IPV4_INTERFACE_ADDRESS_TYPE) {
347 srcAddress = ((IPv4AddressTlv) tlv).address().toInt();
348 } else {
349 dstAddress = ((IPv4AddressTlv) tlv).address().toInt();
350 }
351 }
352 }
353
354 DeviceId srcId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_LOCAL).toString()));
355 DeviceId dstId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_REMOTE).toString()));
356
357 if (localPseduo && srcAddress == 0) {
358 srcAddress = PSEUDO_PORT;
359 } else if (remotePseduo && dstAddress == 0) {
360 dstAddress = PSEUDO_PORT;
361 }
362
363 ConnectPoint src = new ConnectPoint(srcId, PortNumber.portNumber(srcAddress));
364 ConnectPoint dst = new ConnectPoint(dstId, PortNumber.portNumber(dstAddress));
365 BgpNodeLSNlriVer4 srcNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
366 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().localNodeDescriptors()), false,
367 linkNlri.getRouteDistinguisher());
368
369 BgpNodeLSNlriVer4 dstNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
370 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().remoteNodeDescriptors()), false,
371 linkNlri.getRouteDistinguisher());
372
373 addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri,
374 dstNodeNlri, srcId, dstId, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530375 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());
Priyanka Bfc51c952016-03-26 14:30:33 +0530381 }
382
383 private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo,
384 BgpNodeLSNlriVer4 srcNodeNlri, BgpNodeLSNlriVer4 dstNodeNlri, DeviceId srcId, DeviceId dstId,
385 PathAttrNlriDetails details) {
386 if (isAddLink) {
387 if (localPseduo) {
388 if (deviceService.getDevice(srcId) == null) {
389 for (BgpNodeListener l : controller.listener()) {
390 l.addNode(srcNodeNlri, details);
391 }
392 }
393 } else if (remotePseduo) {
394 if (deviceService.getDevice(dstId) == null) {
395 for (BgpNodeListener l : controller.listener()) {
396 l.addNode(dstNodeNlri, details);
397 }
398 }
399 }
400 } else {
401 if (localPseduo) {
402 Set<Link> links = linkService.getDeviceLinks(srcId);
403 if (links == null || links.isEmpty()) {
404 for (BgpNodeListener l : controller.listener()) {
405 l.deleteNode(srcNodeNlri);
406 }
407 }
408 } else if (remotePseduo) {
409 log.info("Remote pseudo delete link ");
410 Set<Link> links = linkService.getDeviceLinks(dstId);
411 if (links == null || links.isEmpty()) {
412 for (BgpNodeListener l : controller.listener()) {
413 l.deleteNode(dstNodeNlri);
414 }
415 }
416 }
417 }
418 }
419
420 @Override
421 public void deleteLink(BgpLinkLsNlriVer4 linkNlri) throws BgpParseException {
422 log.debug("Delete link {}", linkNlri.toString());
423
424 if (linkProviderService == null) {
425 return;
426 }
427
428 LinkDescription linkDes = buildLinkDes(linkNlri, null, false);
Priyanka B658aa6982016-05-27 21:34:49 +0530429
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
Priyanka Bfc51c952016-03-26 14:30:33 +0530439 linkProviderService.linkVanished(linkDes);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530440 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530441 }
442
Priyanka B658aa6982016-05-27 21:34:49 +0530443 // 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
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530563 @Override
564 public void triggerProbe(DeviceId deviceId) {
565 // TODO Auto-generated method stub
566 }
567
568 @Override
569 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
570 }
571
572 @Override
573 public boolean isReachable(DeviceId deviceId) {
574 // TODO Auto-generated method stub
575 return true;
576 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700577
578 @Override
579 public void changePortState(DeviceId deviceId, PortNumber portNumber,
580 boolean enable) {
581 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530582}