blob: 7200c5c199537218881be0f4b7725dfe84ef7139 [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) {
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
Satish K690d5cf2017-04-04 15:12:38 +0530316
Priyanka B658aa6982016-05-27 21:34:49 +0530317
318 /*
319 * Update link ports and configure bandwidth on source and destination port using networkConfig service
320 * Only master of source link registers for bandwidth
321 */
322 if (mastershipService.isLocalMaster(linkDes.src().deviceId())) {
Satish K690d5cf2017-04-04 15:12:38 +0530323 registerBandwidthAndTeMetric(linkDes, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530324 }
325
326 //Updating ports of the link
Priyanka B2312d222016-06-06 14:15:07 +0530327 deviceProviderService.updatePorts(linkDes.src().deviceId(), buildPortDescriptions(linkDes.src().deviceId(),
328 linkDes.src().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530329
Priyanka B2312d222016-06-06 14:15:07 +0530330 deviceProviderService.updatePorts(linkDes.dst().deviceId(), buildPortDescriptions(linkDes.dst().deviceId(),
331 linkDes.dst().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530332
Priyanka Bfc51c952016-03-26 14:30:33 +0530333 linkProviderService.linkDetected(linkDes);
334 }
335
336 //Build link description.
337 private LinkDescription buildLinkDes(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details, boolean isAddLink)
338 throws BgpParseException {
339 long srcAddress = 0;
340 long dstAddress = 0;
341 boolean localPseduo = false;
342 boolean remotePseduo = false;
343
344 List<BgpValueType> localTlvs = linkNlri.getLinkIdentifier().localNodeDescriptors().getSubTlvs();
345 for (BgpValueType localTlv : localTlvs) {
346 if (localTlv instanceof IsIsPseudonode || localTlv instanceof OspfPseudonode) {
347 localPseduo = true;
348 }
349 }
350 List<BgpValueType> remoteTlvs = linkNlri.getLinkIdentifier().remoteNodeDescriptors().getSubTlvs();
351 for (BgpValueType remoteTlv : remoteTlvs) {
352 if (remoteTlv instanceof IsIsPseudonode || remoteTlv instanceof OspfPseudonode) {
353 remotePseduo = true;
354 }
355 }
356
357 List<BgpValueType> tlvs = linkNlri.getLinkIdentifier().linkDescriptors();
358 for (BgpValueType tlv : tlvs) {
359 if (tlv instanceof LinkLocalRemoteIdentifiersTlv) {
360 srcAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkLocalIdentifier();
361 //Set 32nd bit.
362 srcAddress = srcAddress | IDENTIFIER_SET;
363 dstAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkRemoteIdentifier();
364 dstAddress = dstAddress | IDENTIFIER_SET;
365 } else if (tlv instanceof IPv4AddressTlv) {
366 if (tlv.getType() == BgpLinkLSIdentifier.IPV4_INTERFACE_ADDRESS_TYPE) {
367 srcAddress = ((IPv4AddressTlv) tlv).address().toInt();
368 } else {
369 dstAddress = ((IPv4AddressTlv) tlv).address().toInt();
370 }
371 }
372 }
373
374 DeviceId srcId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_LOCAL).toString()));
375 DeviceId dstId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_REMOTE).toString()));
376
377 if (localPseduo && srcAddress == 0) {
378 srcAddress = PSEUDO_PORT;
379 } else if (remotePseduo && dstAddress == 0) {
380 dstAddress = PSEUDO_PORT;
381 }
382
383 ConnectPoint src = new ConnectPoint(srcId, PortNumber.portNumber(srcAddress));
384 ConnectPoint dst = new ConnectPoint(dstId, PortNumber.portNumber(dstAddress));
385 BgpNodeLSNlriVer4 srcNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
386 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().localNodeDescriptors()), false,
387 linkNlri.getRouteDistinguisher());
388
389 BgpNodeLSNlriVer4 dstNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
390 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().remoteNodeDescriptors()), false,
391 linkNlri.getRouteDistinguisher());
392
393 addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri,
394 dstNodeNlri, srcId, dstId, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530395 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
396 if (details != null) {
397 annotationBuilder = getAnnotations(annotationBuilder, false, details);
398 }
399
400 return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false, annotationBuilder.build());
Priyanka Bfc51c952016-03-26 14:30:33 +0530401 }
402
403 private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo,
404 BgpNodeLSNlriVer4 srcNodeNlri, BgpNodeLSNlriVer4 dstNodeNlri, DeviceId srcId, DeviceId dstId,
405 PathAttrNlriDetails details) {
406 if (isAddLink) {
407 if (localPseduo) {
408 if (deviceService.getDevice(srcId) == null) {
409 for (BgpNodeListener l : controller.listener()) {
410 l.addNode(srcNodeNlri, details);
411 }
412 }
413 } else if (remotePseduo) {
414 if (deviceService.getDevice(dstId) == null) {
415 for (BgpNodeListener l : controller.listener()) {
416 l.addNode(dstNodeNlri, details);
417 }
418 }
419 }
420 } else {
421 if (localPseduo) {
422 Set<Link> links = linkService.getDeviceLinks(srcId);
423 if (links == null || links.isEmpty()) {
424 for (BgpNodeListener l : controller.listener()) {
425 l.deleteNode(srcNodeNlri);
426 }
427 }
428 } else if (remotePseduo) {
429 log.info("Remote pseudo delete link ");
430 Set<Link> links = linkService.getDeviceLinks(dstId);
431 if (links == null || links.isEmpty()) {
432 for (BgpNodeListener l : controller.listener()) {
433 l.deleteNode(dstNodeNlri);
434 }
435 }
436 }
437 }
438 }
439
440 @Override
441 public void deleteLink(BgpLinkLsNlriVer4 linkNlri) throws BgpParseException {
442 log.debug("Delete link {}", linkNlri.toString());
443
444 if (linkProviderService == null) {
445 return;
446 }
447
448 LinkDescription linkDes = buildLinkDes(linkNlri, null, false);
Priyanka B658aa6982016-05-27 21:34:49 +0530449
450 /*
451 * Only master for the link src will release the bandwidth resource.
452 */
453 if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
454 // Releases registered resource for this link
Satish K690d5cf2017-04-04 15:12:38 +0530455 networkConfigService.removeConfig(LinkKey.linkKey(linkDes.src(), linkDes.dst()), TeLinkConfig.class);
Priyanka B658aa6982016-05-27 21:34:49 +0530456 }
457
Priyanka Bfc51c952016-03-26 14:30:33 +0530458 linkProviderService.linkVanished(linkDes);
Priyanka B4c3b4512016-07-22 11:41:49 +0530459
460 linkDes = new DefaultLinkDescription(linkDes.dst(), linkDes.src(), Link.Type.DIRECT,
461 false, linkDes.annotations());
462 linkProviderService.linkVanished(linkDes);
Satish K690d5cf2017-04-04 15:12:38 +0530463 if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
464 // Releases registered resource for this link
465 networkConfigService.removeConfig(LinkKey.linkKey(linkDes.src(), linkDes.dst()), TeLinkConfig.class);
466 }
467
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530468 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530469 }
470
Priyanka B658aa6982016-05-27 21:34:49 +0530471 // Creates label resource pool for the specific device. For all devices label range is 5122-9217
472 private void createDevicePool(DeviceId deviceId) {
473 if (labelResourceAdminService == null) {
474 return;
475 }
476
477 labelResourceAdminService.createDevicePool(deviceId, beginLabel, endLabel);
478 }
479
Satish K690d5cf2017-04-04 15:12:38 +0530480 private void registerBandwidthAndTeMetric(LinkDescription linkDes, PathAttrNlriDetails details) {
Priyanka B658aa6982016-05-27 21:34:49 +0530481 if (details == null) {
482 log.error("Couldnot able to register bandwidth ");
483 return;
484 }
485
486 List<BgpValueType> attribute = details.pathAttributes().stream()
487 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
488 if (attribute.isEmpty()) {
489 return;
490 }
491
492 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
Satish K690d5cf2017-04-04 15:12:38 +0530493 double maxReservableBw = 0;
494 List<Float> unreservedBw = new ArrayList<>();
495 int teMetric = 0;
496 int igpMetric = 0;
Priyanka B658aa6982016-05-27 21:34:49 +0530497
498 for (BgpValueType tlv : tlvs) {
499 switch (tlv.getType()) {
500 case LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH:
501 maxReservableBw = ((BgpLinkAttrMaxLinkBandwidth) tlv).linkAttrMaxLinkBandwidth();
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530502 //will get in bits/second , convert to MBPS to store in network config service
503 maxReservableBw = maxReservableBw / 1000000;
Priyanka B658aa6982016-05-27 21:34:49 +0530504 break;
Satish K690d5cf2017-04-04 15:12:38 +0530505 case LinkStateAttributes.ATTR_LINK_UNRES_BANDWIDTH:
506 unreservedBw = ((BgpLinkAttrUnRsrvdLinkBandwidth) tlv).getLinkAttrUnRsrvdLinkBandwidth();
507 break;
508 case LinkStateAttributes.ATTR_LINK_TE_DEFAULT_METRIC:
509 teMetric = ((BgpLinkAttrTeDefaultMetric) tlv).attrLinkDefTeMetric();
510 break;
511 case LinkStateAttributes.ATTR_LINK_IGP_METRIC:
512 igpMetric = ((BgpLinkAttrIgpMetric) tlv).attrLinkIgpMetric();
513 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530514 default: // do nothing
515 }
516 }
517
Satish K690d5cf2017-04-04 15:12:38 +0530518 //Configure bandwidth for src and dst port
519 TeLinkConfig config = networkConfigService.addConfig(LinkKey.linkKey(linkDes.src(), linkDes.dst()),
520 TeLinkConfig.class);
521 Double bw = 0.0;
522 if (unreservedBw.size() > 0) {
523 bw = unreservedBw.get(0).doubleValue(); //Low priority
Priyanka B658aa6982016-05-27 21:34:49 +0530524 }
525
Satish K690d5cf2017-04-04 15:12:38 +0530526 config.maxResvBandwidth(maxReservableBw)
527 .unResvBandwidth(bw).teCost(teMetric).igpCost(igpMetric);
528 //.apply();
Priyanka B658aa6982016-05-27 21:34:49 +0530529
Satish K690d5cf2017-04-04 15:12:38 +0530530 networkConfigService.applyConfig(LinkKey.linkKey(linkDes.src(),
531 linkDes.dst()), TeLinkConfig.class, config.node());
Priyanka B658aa6982016-05-27 21:34:49 +0530532 }
533
534 private DefaultAnnotations.Builder getAnnotations(DefaultAnnotations.Builder annotationBuilder, boolean isNode,
535 PathAttrNlriDetails details) {
536
537 List<BgpValueType> attribute = details.pathAttributes().stream()
538 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
539 if (attribute.isEmpty()) {
540 return annotationBuilder;
541 }
542 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
543 boolean abrBit = false;
544 boolean externalBit = false;
545 boolean pseudo = false;
Priyanka B658aa6982016-05-27 21:34:49 +0530546 byte[] areaId = null;
547 Ip4Address routerId = null;
548 for (BgpValueType tlv : tlvs) {
549 switch (tlv.getType()) {
550 case LinkStateAttributes.ATTR_NODE_FLAG_BITS:
551 abrBit = ((BgpAttrNodeFlagBitTlv) tlv).abrBit();
552 externalBit = ((BgpAttrNodeFlagBitTlv) tlv).externalBit();
553 break;
554 case NodeDescriptors.IGP_ROUTERID_TYPE:
555 if (tlv instanceof IsIsPseudonode || tlv instanceof OspfPseudonode) {
556 pseudo = true;
557 }
558 break;
559 case LinkStateAttributes.ATTR_NODE_ISIS_AREA_ID:
560 areaId = ((BgpAttrNodeIsIsAreaId) tlv).attrNodeIsIsAreaId();
561 break;
562 case LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID:
563 routerId = ((BgpAttrRouterIdV4) tlv).attrRouterId();
564 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530565 default: // do nothing
566 }
567 }
568
569 // Annotations for device
570 if (isNode) {
571 boolean internalBit = false;
572 if (!abrBit && !externalBit) {
573 internalBit = true;
574 }
575
576 annotationBuilder.set(EXTERNAL_BIT, String.valueOf(externalBit));
577 annotationBuilder.set(ABR_BIT, String.valueOf(abrBit));
578 annotationBuilder.set(INTERNAL_BIT, String.valueOf(internalBit));
579 annotationBuilder.set(PSEUDO, String.valueOf(pseudo));
580
581 if (areaId != null) {
582 annotationBuilder.set(AREAID, new String(areaId));
583 }
584 if (routerId != null) {
585 // LsrID
586 annotationBuilder.set(LSRID, String.valueOf(routerId));
587 }
Priyanka B658aa6982016-05-27 21:34:49 +0530588 }
589 return annotationBuilder;
590 }
591
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530592 @Override
593 public void triggerProbe(DeviceId deviceId) {
594 // TODO Auto-generated method stub
595 }
596
597 @Override
598 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
599 }
600
601 @Override
602 public boolean isReachable(DeviceId deviceId) {
603 // TODO Auto-generated method stub
604 return true;
605 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700606
607 @Override
608 public void changePortState(DeviceId deviceId, PortNumber portNumber,
609 boolean enable) {
610 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530611}