blob: e95eba097dac2a9c79b303d097d662e79c738c45 [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 B2312d222016-06-06 14:15:07 +053023import java.util.ArrayList;
Priyanka Bfc51c952016-03-26 14:30:33 +053024import java.util.List;
25import java.util.Set;
Priyanka B2312d222016-06-06 14:15:07 +053026import java.util.HashMap;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053027
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053028import org.onlab.packet.ChassisId;
Priyanka B658aa6982016-05-27 21:34:49 +053029import org.onlab.packet.Ip4Address;
30import org.onlab.util.Bandwidth;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053031import org.apache.felix.scr.annotations.Activate;
32import org.apache.felix.scr.annotations.Component;
33import org.apache.felix.scr.annotations.Deactivate;
34import org.apache.felix.scr.annotations.Reference;
35import org.apache.felix.scr.annotations.ReferenceCardinality;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053036import org.onosproject.bgp.controller.BgpController;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053037import org.onosproject.bgp.controller.BgpDpid;
Priyanka Bfc51c952016-03-26 14:30:33 +053038import org.onosproject.bgp.controller.BgpLinkListener;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053039import org.onosproject.bgp.controller.BgpNodeListener;
Priyanka Bfc51c952016-03-26 14:30:33 +053040import org.onosproject.bgpio.exceptions.BgpParseException;
41import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
42import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
43import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053044import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Priyanka Bfc51c952016-03-26 14:30:33 +053045import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
46import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
47import org.onosproject.bgpio.types.AutonomousSystemTlv;
48import org.onosproject.bgpio.types.BgpLSIdentifierTlv;
49import org.onosproject.bgpio.types.BgpValueType;
50import org.onosproject.bgpio.types.IPv4AddressTlv;
51import org.onosproject.bgpio.types.IsIsNonPseudonode;
52import org.onosproject.bgpio.types.IsIsPseudonode;
53import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv;
Priyanka B658aa6982016-05-27 21:34:49 +053054import org.onosproject.bgpio.types.LinkStateAttributes;
Priyanka Bfc51c952016-03-26 14:30:33 +053055import org.onosproject.bgpio.types.OspfNonPseudonode;
56import org.onosproject.bgpio.types.OspfPseudonode;
Priyanka B658aa6982016-05-27 21:34:49 +053057import org.onosproject.bgpio.types.attr.BgpAttrNodeFlagBitTlv;
58import org.onosproject.bgpio.types.attr.BgpAttrNodeIsIsAreaId;
59import org.onosproject.bgpio.types.attr.BgpAttrRouterIdV4;
60import org.onosproject.bgpio.types.attr.BgpLinkAttrIgpMetric;
61import org.onosproject.bgpio.types.attr.BgpLinkAttrMaxLinkBandwidth;
62import org.onosproject.bgpio.types.attr.BgpLinkAttrTeDefaultMetric;
Priyanka Bfc51c952016-03-26 14:30:33 +053063import org.onosproject.core.CoreService;
Priyanka B658aa6982016-05-27 21:34:49 +053064import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
65import org.onosproject.incubator.net.resource.label.LabelResourceId;
66import org.onosproject.mastership.MastershipEvent;
67import org.onosproject.mastership.MastershipListener;
68import org.onosproject.mastership.MastershipService;
Priyanka Bfc51c952016-03-26 14:30:33 +053069import org.onosproject.net.AnnotationKeys;
70import org.onosproject.net.ConnectPoint;
71import org.onosproject.net.DefaultAnnotations;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053072import org.onosproject.net.Device;
73import org.onosproject.net.DeviceId;
Priyanka Bfc51c952016-03-26 14:30:33 +053074import org.onosproject.net.Link;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053075import org.onosproject.net.MastershipRole;
Saurav Dasa2d37502016-03-25 17:50:40 -070076import org.onosproject.net.PortNumber;
Priyanka B658aa6982016-05-27 21:34:49 +053077import org.onosproject.net.config.NetworkConfigService;
Sho SHIMIZUb6c63a32016-05-26 12:07:19 -070078import org.onosproject.net.config.basics.BandwidthCapacity;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053079import org.onosproject.net.device.DefaultDeviceDescription;
Priyanka B658aa6982016-05-27 21:34:49 +053080import org.onosproject.net.device.DefaultPortDescription;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053081import org.onosproject.net.device.DeviceDescription;
82import org.onosproject.net.device.DeviceProvider;
83import org.onosproject.net.device.DeviceProviderRegistry;
84import org.onosproject.net.device.DeviceProviderService;
Priyanka Bfc51c952016-03-26 14:30:33 +053085import org.onosproject.net.device.DeviceService;
Priyanka B658aa6982016-05-27 21:34:49 +053086import org.onosproject.net.device.PortDescription;
Priyanka Bfc51c952016-03-26 14:30:33 +053087import org.onosproject.net.link.DefaultLinkDescription;
88import org.onosproject.net.link.LinkDescription;
89import org.onosproject.net.link.LinkProvider;
90import org.onosproject.net.link.LinkProviderRegistry;
91import org.onosproject.net.link.LinkProviderService;
92import org.onosproject.net.link.LinkService;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053093import org.onosproject.net.provider.AbstractProvider;
94import org.onosproject.net.provider.ProviderId;
95import org.slf4j.Logger;
96import org.slf4j.LoggerFactory;
97
98/**
99 * Provider which uses an BGP controller to detect network infrastructure topology.
100 */
101@Component(immediate = true)
Priyanka Bfc51c952016-03-26 14:30:33 +0530102public class BgpTopologyProvider extends AbstractProvider implements DeviceProvider, LinkProvider {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530103
Priyanka B9bee0802016-04-27 22:06:02 +0530104 /**
105 * Creates an instance of BGP topology provider.
106 */
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530107 public BgpTopologyProvider() {
Priyanka B9bee0802016-04-27 22:06:02 +0530108 super(new ProviderId("l3", "org.onosproject.provider.bgp"));
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530109 }
110
111 private static final Logger log = LoggerFactory.getLogger(BgpTopologyProvider.class);
112
113 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530114 protected DeviceProviderRegistry deviceProviderRegistry;
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Priyanka Bfc51c952016-03-26 14:30:33 +0530117 protected LinkProviderRegistry linkProviderRegistry;
118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530120 protected BgpController controller;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530121
Priyanka Bfc51c952016-03-26 14:30:33 +0530122 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
123 protected LinkService linkService;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected DeviceService deviceService;
127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected CoreService coreService;
130
Priyanka B658aa6982016-05-27 21:34:49 +0530131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected MastershipService mastershipService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected LabelResourceAdminService labelResourceAdminService;
136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected NetworkConfigService networkConfigService;
139
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530140 private DeviceProviderService deviceProviderService;
Priyanka Bfc51c952016-03-26 14:30:33 +0530141 private LinkProviderService linkProviderService;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530142
Priyanka B658aa6982016-05-27 21:34:49 +0530143 private InternalMastershipListener masterListener = new InternalMastershipListener();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530144 private InternalBgpProvider listener = new InternalBgpProvider();
145 private static final String UNKNOWN = "unknown";
Priyanka Bfc51c952016-03-26 14:30:33 +0530146 public static final long IDENTIFIER_SET = 0x100000000L;
147 public static final String AS_NUMBER = "asNumber";
148 public static final String DOMAIN_IDENTIFIER = "domainIdentifier";
149 public static final String ROUTING_UNIVERSE = "routingUniverse";
Priyanka B658aa6982016-05-27 21:34:49 +0530150
151 public static final String EXTERNAL_BIT = "externalBit";
152 public static final String ABR_BIT = "abrBit";
153 public static final String INTERNAL_BIT = "internalBit";
154 public static final String PSEUDO = "psuedo";
155 public static final String AREAID = "areaId";
156 public static final String LSRID = "lsrId";
157 public static final String COST = "cost";
158 public static final String TE_COST = "teCost";
159
Priyanka Bfc51c952016-03-26 14:30:33 +0530160 public static final long PSEUDO_PORT = 0xffffffff;
Priyanka B658aa6982016-05-27 21:34:49 +0530161 public static final int DELAY = 2;
162 private LabelResourceId beginLabel = labelResourceId(5122);
163 private LabelResourceId endLabel = labelResourceId(9217);
Priyanka B2312d222016-06-06 14:15:07 +0530164 private HashMap<DeviceId, List<PortDescription>> portMap = new HashMap<>();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530165
166 @Activate
167 public void activate() {
Priyanka Bfc51c952016-03-26 14:30:33 +0530168 log.debug("BgpTopologyProvider activate");
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530169 deviceProviderService = deviceProviderRegistry.register(this);
Priyanka Bfc51c952016-03-26 14:30:33 +0530170 linkProviderService = linkProviderRegistry.register(this);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530171 controller.addListener(listener);
Priyanka B658aa6982016-05-27 21:34:49 +0530172 mastershipService.addListener(masterListener);
Priyanka Bfc51c952016-03-26 14:30:33 +0530173 controller.addLinkListener(listener);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530174 }
175
176 @Deactivate
177 public void deactivate() {
Priyanka Bfc51c952016-03-26 14:30:33 +0530178 log.debug("BgpTopologyProvider deactivate");
179 deviceProviderRegistry.unregister(this);
180 deviceProviderService = null;
181 linkProviderRegistry.unregister(this);
182 linkProviderService = null;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530183 controller.removeListener(listener);
Priyanka Bfc51c952016-03-26 14:30:33 +0530184 controller.removeLinkListener(listener);
Priyanka B658aa6982016-05-27 21:34:49 +0530185 mastershipService.removeListener(masterListener);
186 }
187
188 private class InternalMastershipListener implements MastershipListener {
189 @Override
190 public void event(MastershipEvent event) {
191 if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
192 if (mastershipService.getMasterFor(event.subject()) != null) {
193 //Only for L3 device create label pool for that device
194 Device device = deviceService.getDevice(event.subject());
195 if (device == null) {
196 log.debug("Device {} doesn't exist", event.subject());
197 return;
198 }
199 //Reserve device label pool for L3 devices
200 if (device.annotations().value(LSRID) != null) {
201 createDevicePool(event.subject());
202 }
203 }
204 }
205 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530206 }
207
208 /*
209 * Implements device and link update.
210 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530211 private class InternalBgpProvider implements BgpNodeListener, BgpLinkListener {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530212
213 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530214 public void addNode(BgpNodeLSNlriVer4 nodeNlri, PathAttrNlriDetails details) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530215 log.debug("Add node {}", nodeNlri.toString());
216
Priyanka B658aa6982016-05-27 21:34:49 +0530217 if (deviceProviderService == null || deviceService == null) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530218 return;
219 }
Priyanka Bfc51c952016-03-26 14:30:33 +0530220 Device.Type deviceType = ROUTER;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530221 BgpDpid nodeUri = new BgpDpid(nodeNlri);
222 DeviceId deviceId = deviceId(uri(nodeUri.toString()));
223 ChassisId cId = new ChassisId();
224
Priyanka B658aa6982016-05-27 21:34:49 +0530225 /*
226 * Check if device is already there (available) , if yes not updating to core.
227 */
228 if (deviceService.isAvailable(deviceId)) {
229 return;
230 }
231
Priyanka Bfc51c952016-03-26 14:30:33 +0530232 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
233
234 newBuilder.set(AnnotationKeys.TYPE, "L3");
235 newBuilder.set(ROUTING_UNIVERSE, Long.toString(nodeNlri.getIdentifier()));
236
237 List<BgpValueType> tlvs = nodeNlri.getLocalNodeDescriptors().getNodedescriptors().getSubTlvs();
238 for (BgpValueType tlv : tlvs) {
239 if (tlv instanceof AutonomousSystemTlv) {
240 newBuilder.set(AS_NUMBER, Integer.toString(((AutonomousSystemTlv) tlv).getAsNum()));
241 } else if (tlv instanceof BgpLSIdentifierTlv) {
242 newBuilder.set(DOMAIN_IDENTIFIER,
243 Integer.toString(((BgpLSIdentifierTlv) tlv).getBgpLsIdentifier()));
244 }
245 if (tlv.getType() == NodeDescriptors.IGP_ROUTERID_TYPE) {
246 if (tlv instanceof IsIsPseudonode) {
247 deviceType = VIRTUAL;
Priyanka B19c08732016-06-04 21:11:19 +0530248 newBuilder.set(AnnotationKeys.ROUTER_ID, nodeUri.isoNodeIdString(((IsIsPseudonode) tlv)
249 .getIsoNodeId()));
Priyanka Bfc51c952016-03-26 14:30:33 +0530250 } else if (tlv instanceof OspfPseudonode) {
251 deviceType = VIRTUAL;
252 newBuilder
253 .set(AnnotationKeys.ROUTER_ID, Integer.toString(((OspfPseudonode) tlv).getrouterID()));
254 } else if (tlv instanceof IsIsNonPseudonode) {
Priyanka B19c08732016-06-04 21:11:19 +0530255 newBuilder.set(AnnotationKeys.ROUTER_ID, nodeUri.isoNodeIdString(((IsIsNonPseudonode) tlv)
256 .getIsoNodeId()));
Priyanka Bfc51c952016-03-26 14:30:33 +0530257 } else if (tlv instanceof OspfNonPseudonode) {
258 newBuilder.set(AnnotationKeys.ROUTER_ID,
259 Integer.toString(((OspfNonPseudonode) tlv).getrouterID()));
260 }
261 }
262 }
Priyanka B658aa6982016-05-27 21:34:49 +0530263 DefaultAnnotations.Builder anntotations = DefaultAnnotations.builder();
264 anntotations = getAnnotations(newBuilder, true, details);
Priyanka Bfc51c952016-03-26 14:30:33 +0530265
266 DeviceDescription description = new DefaultDeviceDescription(uri(nodeUri.toString()), deviceType, UNKNOWN,
Priyanka B658aa6982016-05-27 21:34:49 +0530267 UNKNOWN, UNKNOWN, UNKNOWN, cId, anntotations.build());
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530268
Priyanka B658aa6982016-05-27 21:34:49 +0530269 deviceProviderService.deviceConnected(deviceId, description);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530270 }
271
272 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530273 public void deleteNode(BgpNodeLSNlriVer4 nodeNlri) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530274 log.debug("Delete node {}", nodeNlri.toString());
275
276 if (deviceProviderService == null) {
277 return;
278 }
279
Priyanka Bfc51c952016-03-26 14:30:33 +0530280 BgpDpid deviceUri = new BgpDpid(nodeNlri);
281 DeviceId deviceId = deviceId(uri(deviceUri.toString()));
Priyanka B658aa6982016-05-27 21:34:49 +0530282
283 if (labelResourceAdminService != null) {
284 //Destroy local device label pool reserved for that device
285 labelResourceAdminService.destroyDevicePool(deviceId);
286 }
287
Priyanka Bfc51c952016-03-26 14:30:33 +0530288 deviceProviderService.deviceDisconnected(deviceId);
289 }
290
Priyanka B2312d222016-06-06 14:15:07 +0530291 private List<PortDescription> buildPortDescriptions(DeviceId deviceId,
292 PortNumber portNumber) {
293
294 List<PortDescription> portList;
295
296 if (portMap.containsKey(deviceId)) {
297 portList = portMap.get(deviceId);
298 } else {
299 portList = new ArrayList<>();
300 }
301 if (portNumber != null) {
302 PortDescription portDescriptions = new DefaultPortDescription(portNumber, true);
303 portList.add(portDescriptions);
304 }
305
306 portMap.put(deviceId, portList);
307 return portList;
308 }
309
Priyanka Bfc51c952016-03-26 14:30:33 +0530310 @Override
311 public void addLink(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details) throws BgpParseException {
312 log.debug("Addlink {}", linkNlri.toString());
313
Priyanka B2312d222016-06-06 14:15:07 +0530314 LinkDescription linkDes = buildLinkDes(linkNlri, details, true);
315
316 //If already link exists, return
317 if (linkService.getLink(linkDes.src(), linkDes.dst()) != null || linkProviderService == null) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530318 return;
319 }
Priyanka B658aa6982016-05-27 21:34:49 +0530320
321 /*
322 * Update link ports and configure bandwidth on source and destination port using networkConfig service
323 * Only master of source link registers for bandwidth
324 */
325 if (mastershipService.isLocalMaster(linkDes.src().deviceId())) {
326 registerBandwidth(linkDes, details);
327 }
328
329 //Updating ports of the link
Priyanka B2312d222016-06-06 14:15:07 +0530330 deviceProviderService.updatePorts(linkDes.src().deviceId(), buildPortDescriptions(linkDes.src().deviceId(),
331 linkDes.src().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530332
Priyanka B2312d222016-06-06 14:15:07 +0530333 deviceProviderService.updatePorts(linkDes.dst().deviceId(), buildPortDescriptions(linkDes.dst().deviceId(),
334 linkDes.dst().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530335
Priyanka Bfc51c952016-03-26 14:30:33 +0530336 linkProviderService.linkDetected(linkDes);
337 }
338
339 //Build link description.
340 private LinkDescription buildLinkDes(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details, boolean isAddLink)
341 throws BgpParseException {
342 long srcAddress = 0;
343 long dstAddress = 0;
344 boolean localPseduo = false;
345 boolean remotePseduo = false;
346
347 List<BgpValueType> localTlvs = linkNlri.getLinkIdentifier().localNodeDescriptors().getSubTlvs();
348 for (BgpValueType localTlv : localTlvs) {
349 if (localTlv instanceof IsIsPseudonode || localTlv instanceof OspfPseudonode) {
350 localPseduo = true;
351 }
352 }
353 List<BgpValueType> remoteTlvs = linkNlri.getLinkIdentifier().remoteNodeDescriptors().getSubTlvs();
354 for (BgpValueType remoteTlv : remoteTlvs) {
355 if (remoteTlv instanceof IsIsPseudonode || remoteTlv instanceof OspfPseudonode) {
356 remotePseduo = true;
357 }
358 }
359
360 List<BgpValueType> tlvs = linkNlri.getLinkIdentifier().linkDescriptors();
361 for (BgpValueType tlv : tlvs) {
362 if (tlv instanceof LinkLocalRemoteIdentifiersTlv) {
363 srcAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkLocalIdentifier();
364 //Set 32nd bit.
365 srcAddress = srcAddress | IDENTIFIER_SET;
366 dstAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkRemoteIdentifier();
367 dstAddress = dstAddress | IDENTIFIER_SET;
368 } else if (tlv instanceof IPv4AddressTlv) {
369 if (tlv.getType() == BgpLinkLSIdentifier.IPV4_INTERFACE_ADDRESS_TYPE) {
370 srcAddress = ((IPv4AddressTlv) tlv).address().toInt();
371 } else {
372 dstAddress = ((IPv4AddressTlv) tlv).address().toInt();
373 }
374 }
375 }
376
377 DeviceId srcId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_LOCAL).toString()));
378 DeviceId dstId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_REMOTE).toString()));
379
380 if (localPseduo && srcAddress == 0) {
381 srcAddress = PSEUDO_PORT;
382 } else if (remotePseduo && dstAddress == 0) {
383 dstAddress = PSEUDO_PORT;
384 }
385
386 ConnectPoint src = new ConnectPoint(srcId, PortNumber.portNumber(srcAddress));
387 ConnectPoint dst = new ConnectPoint(dstId, PortNumber.portNumber(dstAddress));
388 BgpNodeLSNlriVer4 srcNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
389 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().localNodeDescriptors()), false,
390 linkNlri.getRouteDistinguisher());
391
392 BgpNodeLSNlriVer4 dstNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
393 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().remoteNodeDescriptors()), false,
394 linkNlri.getRouteDistinguisher());
395
396 addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri,
397 dstNodeNlri, srcId, dstId, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530398 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
399 if (details != null) {
400 annotationBuilder = getAnnotations(annotationBuilder, false, details);
401 }
402
403 return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false, annotationBuilder.build());
Priyanka Bfc51c952016-03-26 14:30:33 +0530404 }
405
406 private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo,
407 BgpNodeLSNlriVer4 srcNodeNlri, BgpNodeLSNlriVer4 dstNodeNlri, DeviceId srcId, DeviceId dstId,
408 PathAttrNlriDetails details) {
409 if (isAddLink) {
410 if (localPseduo) {
411 if (deviceService.getDevice(srcId) == null) {
412 for (BgpNodeListener l : controller.listener()) {
413 l.addNode(srcNodeNlri, details);
414 }
415 }
416 } else if (remotePseduo) {
417 if (deviceService.getDevice(dstId) == null) {
418 for (BgpNodeListener l : controller.listener()) {
419 l.addNode(dstNodeNlri, details);
420 }
421 }
422 }
423 } else {
424 if (localPseduo) {
425 Set<Link> links = linkService.getDeviceLinks(srcId);
426 if (links == null || links.isEmpty()) {
427 for (BgpNodeListener l : controller.listener()) {
428 l.deleteNode(srcNodeNlri);
429 }
430 }
431 } else if (remotePseduo) {
432 log.info("Remote pseudo delete link ");
433 Set<Link> links = linkService.getDeviceLinks(dstId);
434 if (links == null || links.isEmpty()) {
435 for (BgpNodeListener l : controller.listener()) {
436 l.deleteNode(dstNodeNlri);
437 }
438 }
439 }
440 }
441 }
442
443 @Override
444 public void deleteLink(BgpLinkLsNlriVer4 linkNlri) throws BgpParseException {
445 log.debug("Delete link {}", linkNlri.toString());
446
447 if (linkProviderService == null) {
448 return;
449 }
450
451 LinkDescription linkDes = buildLinkDes(linkNlri, null, false);
Priyanka B658aa6982016-05-27 21:34:49 +0530452
453 /*
454 * Only master for the link src will release the bandwidth resource.
455 */
456 if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
457 // Releases registered resource for this link
458 networkConfigService.removeConfig(linkDes.src(), BandwidthCapacity.class);
459 networkConfigService.removeConfig(linkDes.dst(), BandwidthCapacity.class);
460 }
461
Priyanka Bfc51c952016-03-26 14:30:33 +0530462 linkProviderService.linkVanished(linkDes);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530463 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530464 }
465
Priyanka B658aa6982016-05-27 21:34:49 +0530466 // Creates label resource pool for the specific device. For all devices label range is 5122-9217
467 private void createDevicePool(DeviceId deviceId) {
468 if (labelResourceAdminService == null) {
469 return;
470 }
471
472 labelResourceAdminService.createDevicePool(deviceId, beginLabel, endLabel);
473 }
474
475 private void registerBandwidth(LinkDescription linkDes, PathAttrNlriDetails details) {
476 if (details == null) {
477 log.error("Couldnot able to register bandwidth ");
478 return;
479 }
480
481 List<BgpValueType> attribute = details.pathAttributes().stream()
482 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
483 if (attribute.isEmpty()) {
484 return;
485 }
486
487 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
488 float maxReservableBw = 0;
489
490 for (BgpValueType tlv : tlvs) {
491 switch (tlv.getType()) {
492 case LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH:
493 maxReservableBw = ((BgpLinkAttrMaxLinkBandwidth) tlv).linkAttrMaxLinkBandwidth();
494 break;
495 default: // do nothing
496 }
497 }
498
499 if (maxReservableBw == 0.0) {
500 return;
501 }
502
503 //Configure bandwidth for src and dst port
504 BandwidthCapacity config = networkConfigService.addConfig(linkDes.src(), BandwidthCapacity.class);
505 config.capacity(Bandwidth.bps(maxReservableBw)).apply();
506
507 config = networkConfigService.addConfig(linkDes.dst(), BandwidthCapacity.class);
508 config.capacity(Bandwidth.bps(maxReservableBw)).apply();
509 }
510
511 private DefaultAnnotations.Builder getAnnotations(DefaultAnnotations.Builder annotationBuilder, boolean isNode,
512 PathAttrNlriDetails details) {
513
514 List<BgpValueType> attribute = details.pathAttributes().stream()
515 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
516 if (attribute.isEmpty()) {
517 return annotationBuilder;
518 }
519 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
520 boolean abrBit = false;
521 boolean externalBit = false;
522 boolean pseudo = false;
523 int igpMetric = 0;
524 int teMetric = 0;
525 byte[] areaId = null;
526 Ip4Address routerId = null;
527 for (BgpValueType tlv : tlvs) {
528 switch (tlv.getType()) {
529 case LinkStateAttributes.ATTR_NODE_FLAG_BITS:
530 abrBit = ((BgpAttrNodeFlagBitTlv) tlv).abrBit();
531 externalBit = ((BgpAttrNodeFlagBitTlv) tlv).externalBit();
532 break;
533 case NodeDescriptors.IGP_ROUTERID_TYPE:
534 if (tlv instanceof IsIsPseudonode || tlv instanceof OspfPseudonode) {
535 pseudo = true;
536 }
537 break;
538 case LinkStateAttributes.ATTR_NODE_ISIS_AREA_ID:
539 areaId = ((BgpAttrNodeIsIsAreaId) tlv).attrNodeIsIsAreaId();
540 break;
541 case LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID:
542 routerId = ((BgpAttrRouterIdV4) tlv).attrRouterId();
543 break;
544 case LinkStateAttributes.ATTR_LINK_IGP_METRIC:
545 igpMetric = ((BgpLinkAttrIgpMetric) tlv).attrLinkIgpMetric();
546 break;
547 case LinkStateAttributes.ATTR_LINK_TE_DEFAULT_METRIC:
548 teMetric = ((BgpLinkAttrTeDefaultMetric) tlv).attrLinkDefTeMetric();
549 break;
550 default: // do nothing
551 }
552 }
553
554 // Annotations for device
555 if (isNode) {
556 boolean internalBit = false;
557 if (!abrBit && !externalBit) {
558 internalBit = true;
559 }
560
561 annotationBuilder.set(EXTERNAL_BIT, String.valueOf(externalBit));
562 annotationBuilder.set(ABR_BIT, String.valueOf(abrBit));
563 annotationBuilder.set(INTERNAL_BIT, String.valueOf(internalBit));
564 annotationBuilder.set(PSEUDO, String.valueOf(pseudo));
565
566 if (areaId != null) {
567 annotationBuilder.set(AREAID, new String(areaId));
568 }
569 if (routerId != null) {
570 // LsrID
571 annotationBuilder.set(LSRID, String.valueOf(routerId));
572 }
573 } else {
574 // Annotations for link
575 if (igpMetric != 0) {
576 annotationBuilder.set(COST, String.valueOf(igpMetric));
577 }
578
579 if (teMetric != 0) {
580 annotationBuilder.set(TE_COST, String.valueOf(teMetric));
581 }
582 }
583 return annotationBuilder;
584 }
585
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530586 @Override
587 public void triggerProbe(DeviceId deviceId) {
588 // TODO Auto-generated method stub
589 }
590
591 @Override
592 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
593 }
594
595 @Override
596 public boolean isReachable(DeviceId deviceId) {
597 // TODO Auto-generated method stub
598 return true;
599 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700600
601 @Override
602 public void changePortState(DeviceId deviceId, PortNumber portNumber,
603 boolean enable) {
604 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530605}