blob: 806c9611c0ad8c769a549caf9fdd0945d5a9360d [file] [log] [blame]
Shashikanth VH1ca26ce2015-11-20 23:19:49 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Shashikanth VH1ca26ce2015-11-20 23:19:49 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5 * the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11 * specific language governing permissions and limitations under the License.
12 */
13
14package org.onosproject.provider.bgp.topology.impl;
15
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053016import org.apache.felix.scr.annotations.Activate;
17import org.apache.felix.scr.annotations.Component;
18import org.apache.felix.scr.annotations.Deactivate;
19import org.apache.felix.scr.annotations.Reference;
20import org.apache.felix.scr.annotations.ReferenceCardinality;
Satish K690d5cf2017-04-04 15:12:38 +053021import org.onlab.packet.ChassisId;
22import org.onlab.packet.Ip4Address;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053023import org.onosproject.bgp.controller.BgpController;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053024import org.onosproject.bgp.controller.BgpDpid;
Priyanka Bfc51c952016-03-26 14:30:33 +053025import org.onosproject.bgp.controller.BgpLinkListener;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053026import org.onosproject.bgp.controller.BgpNodeListener;
Priyanka Bfc51c952016-03-26 14:30:33 +053027import org.onosproject.bgpio.exceptions.BgpParseException;
28import org.onosproject.bgpio.protocol.linkstate.BgpLinkLSIdentifier;
29import org.onosproject.bgpio.protocol.linkstate.BgpLinkLsNlriVer4;
30import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSIdentifier;
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +053031import org.onosproject.bgpio.protocol.linkstate.BgpNodeLSNlriVer4;
Priyanka Bfc51c952016-03-26 14:30:33 +053032import org.onosproject.bgpio.protocol.linkstate.NodeDescriptors;
33import org.onosproject.bgpio.protocol.linkstate.PathAttrNlriDetails;
34import org.onosproject.bgpio.types.AutonomousSystemTlv;
35import org.onosproject.bgpio.types.BgpLSIdentifierTlv;
36import org.onosproject.bgpio.types.BgpValueType;
37import org.onosproject.bgpio.types.IPv4AddressTlv;
38import org.onosproject.bgpio.types.IsIsNonPseudonode;
39import org.onosproject.bgpio.types.IsIsPseudonode;
40import org.onosproject.bgpio.types.LinkLocalRemoteIdentifiersTlv;
Priyanka B658aa6982016-05-27 21:34:49 +053041import org.onosproject.bgpio.types.LinkStateAttributes;
Priyanka Bfc51c952016-03-26 14:30:33 +053042import org.onosproject.bgpio.types.OspfNonPseudonode;
43import org.onosproject.bgpio.types.OspfPseudonode;
Priyanka B658aa6982016-05-27 21:34:49 +053044import org.onosproject.bgpio.types.attr.BgpAttrNodeFlagBitTlv;
45import org.onosproject.bgpio.types.attr.BgpAttrNodeIsIsAreaId;
46import org.onosproject.bgpio.types.attr.BgpAttrRouterIdV4;
47import org.onosproject.bgpio.types.attr.BgpLinkAttrIgpMetric;
48import org.onosproject.bgpio.types.attr.BgpLinkAttrMaxLinkBandwidth;
49import org.onosproject.bgpio.types.attr.BgpLinkAttrTeDefaultMetric;
Satish K690d5cf2017-04-04 15:12:38 +053050import org.onosproject.bgpio.types.attr.BgpLinkAttrUnRsrvdLinkBandwidth;
Priyanka B658aa6982016-05-27 21:34:49 +053051import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
52import org.onosproject.incubator.net.resource.label.LabelResourceId;
Priyanka B658aa6982016-05-27 21:34:49 +053053import org.onosproject.mastership.MastershipService;
Priyanka Bfc51c952016-03-26 14:30:33 +053054import org.onosproject.net.AnnotationKeys;
55import org.onosproject.net.ConnectPoint;
56import org.onosproject.net.DefaultAnnotations;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053057import org.onosproject.net.Device;
58import org.onosproject.net.DeviceId;
Priyanka Bfc51c952016-03-26 14:30:33 +053059import org.onosproject.net.Link;
Satish K690d5cf2017-04-04 15:12:38 +053060import org.onosproject.net.LinkKey;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053061import org.onosproject.net.MastershipRole;
Saurav Dasa2d37502016-03-25 17:50:40 -070062import org.onosproject.net.PortNumber;
Priyanka B658aa6982016-05-27 21:34:49 +053063import org.onosproject.net.config.NetworkConfigService;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053064import org.onosproject.net.device.DefaultDeviceDescription;
Priyanka B658aa6982016-05-27 21:34:49 +053065import org.onosproject.net.device.DefaultPortDescription;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053066import org.onosproject.net.device.DeviceDescription;
Priyanka B4c3b4512016-07-22 11:41:49 +053067import org.onosproject.net.device.DeviceEvent;
68import org.onosproject.net.device.DeviceListener;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053069import org.onosproject.net.device.DeviceProvider;
70import org.onosproject.net.device.DeviceProviderRegistry;
71import org.onosproject.net.device.DeviceProviderService;
Priyanka Bfc51c952016-03-26 14:30:33 +053072import org.onosproject.net.device.DeviceService;
Priyanka B658aa6982016-05-27 21:34:49 +053073import org.onosproject.net.device.PortDescription;
Priyanka Bfc51c952016-03-26 14:30:33 +053074import org.onosproject.net.link.DefaultLinkDescription;
75import org.onosproject.net.link.LinkDescription;
76import org.onosproject.net.link.LinkProvider;
77import org.onosproject.net.link.LinkProviderRegistry;
78import org.onosproject.net.link.LinkProviderService;
79import org.onosproject.net.link.LinkService;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053080import org.onosproject.net.provider.AbstractProvider;
81import org.onosproject.net.provider.ProviderId;
Satish K690d5cf2017-04-04 15:12:38 +053082import org.onosproject.pcep.api.TeLinkConfig;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053083import org.slf4j.Logger;
84import org.slf4j.LoggerFactory;
85
Satish K690d5cf2017-04-04 15:12:38 +053086import java.util.ArrayList;
87import java.util.HashMap;
88import java.util.List;
89import java.util.Set;
90
91import static java.util.stream.Collectors.toList;
92import static org.onosproject.bgp.controller.BgpDpid.uri;
93import static org.onosproject.incubator.net.resource.label.LabelResourceId.labelResourceId;
94import static org.onosproject.net.Device.Type.ROUTER;
95import static org.onosproject.net.Device.Type.VIRTUAL;
96import static org.onosproject.net.DeviceId.deviceId;
97
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053098/**
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)
Priyanka B658aa6982016-05-27 21:34:49 +0530129 protected MastershipService mastershipService;
130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected LabelResourceAdminService labelResourceAdminService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected NetworkConfigService networkConfigService;
136
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530137 private DeviceProviderService deviceProviderService;
Priyanka Bfc51c952016-03-26 14:30:33 +0530138 private LinkProviderService linkProviderService;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530139
Priyanka B4c3b4512016-07-22 11:41:49 +0530140 private DeviceListener deviceListener = new InternalDeviceListener();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530141 private InternalBgpProvider listener = new InternalBgpProvider();
142 private static final String UNKNOWN = "unknown";
Priyanka Bfc51c952016-03-26 14:30:33 +0530143 public static final long IDENTIFIER_SET = 0x100000000L;
144 public static final String AS_NUMBER = "asNumber";
145 public static final String DOMAIN_IDENTIFIER = "domainIdentifier";
146 public static final String ROUTING_UNIVERSE = "routingUniverse";
Priyanka B658aa6982016-05-27 21:34:49 +0530147
148 public static final String EXTERNAL_BIT = "externalBit";
149 public static final String ABR_BIT = "abrBit";
150 public static final String INTERNAL_BIT = "internalBit";
151 public static final String PSEUDO = "psuedo";
152 public static final String AREAID = "areaId";
153 public static final String LSRID = "lsrId";
154 public static final String COST = "cost";
155 public static final String TE_COST = "teCost";
156
Priyanka Bfc51c952016-03-26 14:30:33 +0530157 public static final long PSEUDO_PORT = 0xffffffff;
Priyanka B658aa6982016-05-27 21:34:49 +0530158 public static final int DELAY = 2;
159 private LabelResourceId beginLabel = labelResourceId(5122);
160 private LabelResourceId endLabel = labelResourceId(9217);
Priyanka B2312d222016-06-06 14:15:07 +0530161 private HashMap<DeviceId, List<PortDescription>> portMap = new HashMap<>();
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530162
163 @Activate
164 public void activate() {
Priyanka Bfc51c952016-03-26 14:30:33 +0530165 log.debug("BgpTopologyProvider activate");
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530166 deviceProviderService = deviceProviderRegistry.register(this);
Priyanka Bfc51c952016-03-26 14:30:33 +0530167 linkProviderService = linkProviderRegistry.register(this);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530168 controller.addListener(listener);
Priyanka B4c3b4512016-07-22 11:41:49 +0530169 deviceService.addListener(deviceListener);
Priyanka Bfc51c952016-03-26 14:30:33 +0530170 controller.addLinkListener(listener);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530171 }
172
173 @Deactivate
174 public void deactivate() {
Priyanka Bfc51c952016-03-26 14:30:33 +0530175 log.debug("BgpTopologyProvider deactivate");
176 deviceProviderRegistry.unregister(this);
177 deviceProviderService = null;
178 linkProviderRegistry.unregister(this);
179 linkProviderService = null;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530180 controller.removeListener(listener);
Priyanka Bfc51c952016-03-26 14:30:33 +0530181 controller.removeLinkListener(listener);
Priyanka B4c3b4512016-07-22 11:41:49 +0530182 deviceService.removeListener(deviceListener);
Priyanka B658aa6982016-05-27 21:34:49 +0530183 }
184
Priyanka B4c3b4512016-07-22 11:41:49 +0530185 private class InternalDeviceListener implements DeviceListener {
Priyanka B658aa6982016-05-27 21:34:49 +0530186 @Override
Priyanka B4c3b4512016-07-22 11:41:49 +0530187 public void event(DeviceEvent event) {
188 Device device = event.subject();
189
190 switch (event.type()) {
191 case DEVICE_ADDED:
192 if (!mastershipService.isLocalMaster(device.id())) {
193 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530194 }
Priyanka B4c3b4512016-07-22 11:41:49 +0530195
196 // Reserve device label pool for L3 devices
Priyanka B658aa6982016-05-27 21:34:49 +0530197 if (device.annotations().value(LSRID) != null) {
Priyanka B4c3b4512016-07-22 11:41:49 +0530198 createDevicePool(device.id());
Priyanka B658aa6982016-05-27 21:34:49 +0530199 }
Priyanka B4c3b4512016-07-22 11:41:49 +0530200 break;
201
202 default:
203 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530204 }
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) {
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800302 PortDescription portDescriptions = DefaultPortDescription.builder().withPortNumber(portNumber)
303 .isEnabled(true).build();
Priyanka B2312d222016-06-06 14:15:07 +0530304 portList.add(portDescriptions);
305 }
306
307 portMap.put(deviceId, portList);
308 return portList;
309 }
310
Priyanka Bfc51c952016-03-26 14:30:33 +0530311 @Override
312 public void addLink(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details) throws BgpParseException {
313 log.debug("Addlink {}", linkNlri.toString());
314
Priyanka B2312d222016-06-06 14:15:07 +0530315 LinkDescription linkDes = buildLinkDes(linkNlri, details, true);
316
Satish K690d5cf2017-04-04 15:12:38 +0530317
Priyanka B658aa6982016-05-27 21:34:49 +0530318
319 /*
320 * Update link ports and configure bandwidth on source and destination port using networkConfig service
321 * Only master of source link registers for bandwidth
322 */
323 if (mastershipService.isLocalMaster(linkDes.src().deviceId())) {
Satish K690d5cf2017-04-04 15:12:38 +0530324 registerBandwidthAndTeMetric(linkDes, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530325 }
326
327 //Updating ports of the link
Priyanka B2312d222016-06-06 14:15:07 +0530328 deviceProviderService.updatePorts(linkDes.src().deviceId(), buildPortDescriptions(linkDes.src().deviceId(),
329 linkDes.src().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530330
Priyanka B2312d222016-06-06 14:15:07 +0530331 deviceProviderService.updatePorts(linkDes.dst().deviceId(), buildPortDescriptions(linkDes.dst().deviceId(),
332 linkDes.dst().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530333
Priyanka Bfc51c952016-03-26 14:30:33 +0530334 linkProviderService.linkDetected(linkDes);
335 }
336
337 //Build link description.
338 private LinkDescription buildLinkDes(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details, boolean isAddLink)
339 throws BgpParseException {
340 long srcAddress = 0;
341 long dstAddress = 0;
342 boolean localPseduo = false;
343 boolean remotePseduo = false;
344
345 List<BgpValueType> localTlvs = linkNlri.getLinkIdentifier().localNodeDescriptors().getSubTlvs();
346 for (BgpValueType localTlv : localTlvs) {
347 if (localTlv instanceof IsIsPseudonode || localTlv instanceof OspfPseudonode) {
348 localPseduo = true;
349 }
350 }
351 List<BgpValueType> remoteTlvs = linkNlri.getLinkIdentifier().remoteNodeDescriptors().getSubTlvs();
352 for (BgpValueType remoteTlv : remoteTlvs) {
353 if (remoteTlv instanceof IsIsPseudonode || remoteTlv instanceof OspfPseudonode) {
354 remotePseduo = true;
355 }
356 }
357
358 List<BgpValueType> tlvs = linkNlri.getLinkIdentifier().linkDescriptors();
359 for (BgpValueType tlv : tlvs) {
360 if (tlv instanceof LinkLocalRemoteIdentifiersTlv) {
361 srcAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkLocalIdentifier();
362 //Set 32nd bit.
363 srcAddress = srcAddress | IDENTIFIER_SET;
364 dstAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkRemoteIdentifier();
365 dstAddress = dstAddress | IDENTIFIER_SET;
366 } else if (tlv instanceof IPv4AddressTlv) {
367 if (tlv.getType() == BgpLinkLSIdentifier.IPV4_INTERFACE_ADDRESS_TYPE) {
368 srcAddress = ((IPv4AddressTlv) tlv).address().toInt();
369 } else {
370 dstAddress = ((IPv4AddressTlv) tlv).address().toInt();
371 }
372 }
373 }
374
375 DeviceId srcId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_LOCAL).toString()));
376 DeviceId dstId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_REMOTE).toString()));
377
378 if (localPseduo && srcAddress == 0) {
379 srcAddress = PSEUDO_PORT;
380 } else if (remotePseduo && dstAddress == 0) {
381 dstAddress = PSEUDO_PORT;
382 }
383
384 ConnectPoint src = new ConnectPoint(srcId, PortNumber.portNumber(srcAddress));
385 ConnectPoint dst = new ConnectPoint(dstId, PortNumber.portNumber(dstAddress));
386 BgpNodeLSNlriVer4 srcNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
387 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().localNodeDescriptors()), false,
388 linkNlri.getRouteDistinguisher());
389
390 BgpNodeLSNlriVer4 dstNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
391 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().remoteNodeDescriptors()), false,
392 linkNlri.getRouteDistinguisher());
393
394 addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri,
395 dstNodeNlri, srcId, dstId, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530396 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
397 if (details != null) {
398 annotationBuilder = getAnnotations(annotationBuilder, false, details);
399 }
400
401 return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false, annotationBuilder.build());
Priyanka Bfc51c952016-03-26 14:30:33 +0530402 }
403
404 private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo,
405 BgpNodeLSNlriVer4 srcNodeNlri, BgpNodeLSNlriVer4 dstNodeNlri, DeviceId srcId, DeviceId dstId,
406 PathAttrNlriDetails details) {
407 if (isAddLink) {
408 if (localPseduo) {
409 if (deviceService.getDevice(srcId) == null) {
410 for (BgpNodeListener l : controller.listener()) {
411 l.addNode(srcNodeNlri, details);
412 }
413 }
414 } else if (remotePseduo) {
415 if (deviceService.getDevice(dstId) == null) {
416 for (BgpNodeListener l : controller.listener()) {
417 l.addNode(dstNodeNlri, details);
418 }
419 }
420 }
421 } else {
422 if (localPseduo) {
423 Set<Link> links = linkService.getDeviceLinks(srcId);
424 if (links == null || links.isEmpty()) {
425 for (BgpNodeListener l : controller.listener()) {
426 l.deleteNode(srcNodeNlri);
427 }
428 }
429 } else if (remotePseduo) {
430 log.info("Remote pseudo delete link ");
431 Set<Link> links = linkService.getDeviceLinks(dstId);
432 if (links == null || links.isEmpty()) {
433 for (BgpNodeListener l : controller.listener()) {
434 l.deleteNode(dstNodeNlri);
435 }
436 }
437 }
438 }
439 }
440
441 @Override
442 public void deleteLink(BgpLinkLsNlriVer4 linkNlri) throws BgpParseException {
443 log.debug("Delete link {}", linkNlri.toString());
444
445 if (linkProviderService == null) {
446 return;
447 }
448
449 LinkDescription linkDes = buildLinkDes(linkNlri, null, false);
Priyanka B658aa6982016-05-27 21:34:49 +0530450
451 /*
452 * Only master for the link src will release the bandwidth resource.
453 */
454 if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
455 // Releases registered resource for this link
Satish K690d5cf2017-04-04 15:12:38 +0530456 networkConfigService.removeConfig(LinkKey.linkKey(linkDes.src(), linkDes.dst()), TeLinkConfig.class);
Priyanka B658aa6982016-05-27 21:34:49 +0530457 }
458
Priyanka Bfc51c952016-03-26 14:30:33 +0530459 linkProviderService.linkVanished(linkDes);
Priyanka B4c3b4512016-07-22 11:41:49 +0530460
461 linkDes = new DefaultLinkDescription(linkDes.dst(), linkDes.src(), Link.Type.DIRECT,
462 false, linkDes.annotations());
463 linkProviderService.linkVanished(linkDes);
Satish K690d5cf2017-04-04 15:12:38 +0530464 if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
465 // Releases registered resource for this link
466 networkConfigService.removeConfig(LinkKey.linkKey(linkDes.src(), linkDes.dst()), TeLinkConfig.class);
467 }
468
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530469 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530470 }
471
Priyanka B658aa6982016-05-27 21:34:49 +0530472 // Creates label resource pool for the specific device. For all devices label range is 5122-9217
473 private void createDevicePool(DeviceId deviceId) {
474 if (labelResourceAdminService == null) {
475 return;
476 }
477
478 labelResourceAdminService.createDevicePool(deviceId, beginLabel, endLabel);
479 }
480
Satish K690d5cf2017-04-04 15:12:38 +0530481 private void registerBandwidthAndTeMetric(LinkDescription linkDes, PathAttrNlriDetails details) {
Priyanka B658aa6982016-05-27 21:34:49 +0530482 if (details == null) {
483 log.error("Couldnot able to register bandwidth ");
484 return;
485 }
486
487 List<BgpValueType> attribute = details.pathAttributes().stream()
488 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
489 if (attribute.isEmpty()) {
490 return;
491 }
492
493 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
Satish K690d5cf2017-04-04 15:12:38 +0530494 double maxReservableBw = 0;
495 List<Float> unreservedBw = new ArrayList<>();
496 int teMetric = 0;
497 int igpMetric = 0;
Priyanka B658aa6982016-05-27 21:34:49 +0530498
499 for (BgpValueType tlv : tlvs) {
500 switch (tlv.getType()) {
501 case LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH:
502 maxReservableBw = ((BgpLinkAttrMaxLinkBandwidth) tlv).linkAttrMaxLinkBandwidth();
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530503 //will get in bits/second , convert to MBPS to store in network config service
504 maxReservableBw = maxReservableBw / 1000000;
Priyanka B658aa6982016-05-27 21:34:49 +0530505 break;
Satish K690d5cf2017-04-04 15:12:38 +0530506 case LinkStateAttributes.ATTR_LINK_UNRES_BANDWIDTH:
507 unreservedBw = ((BgpLinkAttrUnRsrvdLinkBandwidth) tlv).getLinkAttrUnRsrvdLinkBandwidth();
508 break;
509 case LinkStateAttributes.ATTR_LINK_TE_DEFAULT_METRIC:
510 teMetric = ((BgpLinkAttrTeDefaultMetric) tlv).attrLinkDefTeMetric();
511 break;
512 case LinkStateAttributes.ATTR_LINK_IGP_METRIC:
513 igpMetric = ((BgpLinkAttrIgpMetric) tlv).attrLinkIgpMetric();
514 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530515 default: // do nothing
516 }
517 }
518
Satish K690d5cf2017-04-04 15:12:38 +0530519 //Configure bandwidth for src and dst port
520 TeLinkConfig config = networkConfigService.addConfig(LinkKey.linkKey(linkDes.src(), linkDes.dst()),
521 TeLinkConfig.class);
522 Double bw = 0.0;
523 if (unreservedBw.size() > 0) {
524 bw = unreservedBw.get(0).doubleValue(); //Low priority
Priyanka B658aa6982016-05-27 21:34:49 +0530525 }
526
Satish K690d5cf2017-04-04 15:12:38 +0530527 config.maxResvBandwidth(maxReservableBw)
528 .unResvBandwidth(bw).teCost(teMetric).igpCost(igpMetric);
529 //.apply();
Priyanka B658aa6982016-05-27 21:34:49 +0530530
Satish K690d5cf2017-04-04 15:12:38 +0530531 networkConfigService.applyConfig(LinkKey.linkKey(linkDes.src(),
532 linkDes.dst()), TeLinkConfig.class, config.node());
Priyanka B658aa6982016-05-27 21:34:49 +0530533 }
534
535 private DefaultAnnotations.Builder getAnnotations(DefaultAnnotations.Builder annotationBuilder, boolean isNode,
536 PathAttrNlriDetails details) {
537
538 List<BgpValueType> attribute = details.pathAttributes().stream()
539 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
540 if (attribute.isEmpty()) {
541 return annotationBuilder;
542 }
543 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
544 boolean abrBit = false;
545 boolean externalBit = false;
546 boolean pseudo = false;
Priyanka B658aa6982016-05-27 21:34:49 +0530547 byte[] areaId = null;
548 Ip4Address routerId = null;
549 for (BgpValueType tlv : tlvs) {
550 switch (tlv.getType()) {
551 case LinkStateAttributes.ATTR_NODE_FLAG_BITS:
552 abrBit = ((BgpAttrNodeFlagBitTlv) tlv).abrBit();
553 externalBit = ((BgpAttrNodeFlagBitTlv) tlv).externalBit();
554 break;
555 case NodeDescriptors.IGP_ROUTERID_TYPE:
556 if (tlv instanceof IsIsPseudonode || tlv instanceof OspfPseudonode) {
557 pseudo = true;
558 }
559 break;
560 case LinkStateAttributes.ATTR_NODE_ISIS_AREA_ID:
561 areaId = ((BgpAttrNodeIsIsAreaId) tlv).attrNodeIsIsAreaId();
562 break;
563 case LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID:
564 routerId = ((BgpAttrRouterIdV4) tlv).attrRouterId();
565 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530566 default: // do nothing
567 }
568 }
569
570 // Annotations for device
571 if (isNode) {
572 boolean internalBit = false;
573 if (!abrBit && !externalBit) {
574 internalBit = true;
575 }
576
577 annotationBuilder.set(EXTERNAL_BIT, String.valueOf(externalBit));
578 annotationBuilder.set(ABR_BIT, String.valueOf(abrBit));
579 annotationBuilder.set(INTERNAL_BIT, String.valueOf(internalBit));
580 annotationBuilder.set(PSEUDO, String.valueOf(pseudo));
581
582 if (areaId != null) {
583 annotationBuilder.set(AREAID, new String(areaId));
584 }
585 if (routerId != null) {
586 // LsrID
587 annotationBuilder.set(LSRID, String.valueOf(routerId));
588 }
Priyanka B658aa6982016-05-27 21:34:49 +0530589 }
590 return annotationBuilder;
591 }
592
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530593 @Override
594 public void triggerProbe(DeviceId deviceId) {
595 // TODO Auto-generated method stub
596 }
597
598 @Override
599 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
600 }
601
602 @Override
603 public boolean isReachable(DeviceId deviceId) {
604 // TODO Auto-generated method stub
605 return true;
606 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700607
608 @Override
609 public void changePortState(DeviceId deviceId, PortNumber portNumber,
610 boolean enable) {
611 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530612}