blob: 8439bb98550858f90de6344972f03e3ee1d67b32 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070016import org.osgi.service.component.annotations.Activate;
17import org.osgi.service.component.annotations.Component;
18import org.osgi.service.component.annotations.Deactivate;
19import org.osgi.service.component.annotations.Reference;
20import org.osgi.service.component.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
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700113 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530114 protected DeviceProviderRegistry deviceProviderRegistry;
115
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700116 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka Bfc51c952016-03-26 14:30:33 +0530117 protected LinkProviderRegistry linkProviderRegistry;
118
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700119 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530120 protected BgpController controller;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530121
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700122 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka Bfc51c952016-03-26 14:30:33 +0530123 protected LinkService linkService;
124
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka Bfc51c952016-03-26 14:30:33 +0530126 protected DeviceService deviceService;
127
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka B658aa6982016-05-27 21:34:49 +0530129 protected MastershipService mastershipService;
130
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700131 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka B658aa6982016-05-27 21:34:49 +0530132 protected LabelResourceAdminService labelResourceAdminService;
133
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka B658aa6982016-05-27 21:34:49 +0530135 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
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700187 public boolean isRelevant(DeviceEvent event) {
188 return event.type() == DeviceEvent.Type.DEVICE_ADDED &&
189 mastershipService.isLocalMaster(event.subject().id());
190 }
191
192 @Override
Priyanka B4c3b4512016-07-22 11:41:49 +0530193 public void event(DeviceEvent event) {
194 Device device = event.subject();
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700195 // Reserve device label pool for L3 devices
196 if (device.annotations().value(LSRID) != null) {
197 createDevicePool(device.id());
Priyanka B658aa6982016-05-27 21:34:49 +0530198 }
199 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530200 }
201
202 /*
203 * Implements device and link update.
204 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530205 private class InternalBgpProvider implements BgpNodeListener, BgpLinkListener {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530206
207 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530208 public void addNode(BgpNodeLSNlriVer4 nodeNlri, PathAttrNlriDetails details) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530209 log.debug("Add node {}", nodeNlri.toString());
210
Priyanka B658aa6982016-05-27 21:34:49 +0530211 if (deviceProviderService == null || deviceService == null) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530212 return;
213 }
Priyanka Bfc51c952016-03-26 14:30:33 +0530214 Device.Type deviceType = ROUTER;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530215 BgpDpid nodeUri = new BgpDpid(nodeNlri);
216 DeviceId deviceId = deviceId(uri(nodeUri.toString()));
217 ChassisId cId = new ChassisId();
218
Priyanka B658aa6982016-05-27 21:34:49 +0530219 /*
220 * Check if device is already there (available) , if yes not updating to core.
221 */
222 if (deviceService.isAvailable(deviceId)) {
223 return;
224 }
225
Priyanka Bfc51c952016-03-26 14:30:33 +0530226 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
227
228 newBuilder.set(AnnotationKeys.TYPE, "L3");
229 newBuilder.set(ROUTING_UNIVERSE, Long.toString(nodeNlri.getIdentifier()));
230
231 List<BgpValueType> tlvs = nodeNlri.getLocalNodeDescriptors().getNodedescriptors().getSubTlvs();
232 for (BgpValueType tlv : tlvs) {
233 if (tlv instanceof AutonomousSystemTlv) {
234 newBuilder.set(AS_NUMBER, Integer.toString(((AutonomousSystemTlv) tlv).getAsNum()));
235 } else if (tlv instanceof BgpLSIdentifierTlv) {
236 newBuilder.set(DOMAIN_IDENTIFIER,
237 Integer.toString(((BgpLSIdentifierTlv) tlv).getBgpLsIdentifier()));
238 }
239 if (tlv.getType() == NodeDescriptors.IGP_ROUTERID_TYPE) {
240 if (tlv instanceof IsIsPseudonode) {
241 deviceType = VIRTUAL;
Priyanka B19c08732016-06-04 21:11:19 +0530242 newBuilder.set(AnnotationKeys.ROUTER_ID, nodeUri.isoNodeIdString(((IsIsPseudonode) tlv)
243 .getIsoNodeId()));
Priyanka Bfc51c952016-03-26 14:30:33 +0530244 } else if (tlv instanceof OspfPseudonode) {
245 deviceType = VIRTUAL;
246 newBuilder
247 .set(AnnotationKeys.ROUTER_ID, Integer.toString(((OspfPseudonode) tlv).getrouterID()));
248 } else if (tlv instanceof IsIsNonPseudonode) {
Priyanka B19c08732016-06-04 21:11:19 +0530249 newBuilder.set(AnnotationKeys.ROUTER_ID, nodeUri.isoNodeIdString(((IsIsNonPseudonode) tlv)
250 .getIsoNodeId()));
Priyanka Bfc51c952016-03-26 14:30:33 +0530251 } else if (tlv instanceof OspfNonPseudonode) {
252 newBuilder.set(AnnotationKeys.ROUTER_ID,
253 Integer.toString(((OspfNonPseudonode) tlv).getrouterID()));
254 }
255 }
256 }
Priyanka B658aa6982016-05-27 21:34:49 +0530257 DefaultAnnotations.Builder anntotations = DefaultAnnotations.builder();
258 anntotations = getAnnotations(newBuilder, true, details);
Priyanka Bfc51c952016-03-26 14:30:33 +0530259
260 DeviceDescription description = new DefaultDeviceDescription(uri(nodeUri.toString()), deviceType, UNKNOWN,
Priyanka B658aa6982016-05-27 21:34:49 +0530261 UNKNOWN, UNKNOWN, UNKNOWN, cId, anntotations.build());
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530262
Priyanka B658aa6982016-05-27 21:34:49 +0530263 deviceProviderService.deviceConnected(deviceId, description);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530264 }
265
266 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530267 public void deleteNode(BgpNodeLSNlriVer4 nodeNlri) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530268 log.debug("Delete node {}", nodeNlri.toString());
269
270 if (deviceProviderService == null) {
271 return;
272 }
273
Priyanka Bfc51c952016-03-26 14:30:33 +0530274 BgpDpid deviceUri = new BgpDpid(nodeNlri);
275 DeviceId deviceId = deviceId(uri(deviceUri.toString()));
Priyanka B658aa6982016-05-27 21:34:49 +0530276
277 if (labelResourceAdminService != null) {
278 //Destroy local device label pool reserved for that device
279 labelResourceAdminService.destroyDevicePool(deviceId);
280 }
281
Priyanka Bfc51c952016-03-26 14:30:33 +0530282 deviceProviderService.deviceDisconnected(deviceId);
283 }
284
Priyanka B2312d222016-06-06 14:15:07 +0530285 private List<PortDescription> buildPortDescriptions(DeviceId deviceId,
286 PortNumber portNumber) {
287
288 List<PortDescription> portList;
289
290 if (portMap.containsKey(deviceId)) {
291 portList = portMap.get(deviceId);
292 } else {
293 portList = new ArrayList<>();
294 }
295 if (portNumber != null) {
Yuta HIGUCHI53e47962018-03-01 23:50:48 -0800296 PortDescription portDescriptions = DefaultPortDescription.builder().withPortNumber(portNumber)
297 .isEnabled(true).build();
Priyanka B2312d222016-06-06 14:15:07 +0530298 portList.add(portDescriptions);
299 }
300
301 portMap.put(deviceId, portList);
302 return portList;
303 }
304
Priyanka Bfc51c952016-03-26 14:30:33 +0530305 @Override
306 public void addLink(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details) throws BgpParseException {
307 log.debug("Addlink {}", linkNlri.toString());
308
Priyanka B2312d222016-06-06 14:15:07 +0530309 LinkDescription linkDes = buildLinkDes(linkNlri, details, true);
310
Satish K690d5cf2017-04-04 15:12:38 +0530311
Priyanka B658aa6982016-05-27 21:34:49 +0530312
313 /*
314 * Update link ports and configure bandwidth on source and destination port using networkConfig service
315 * Only master of source link registers for bandwidth
316 */
317 if (mastershipService.isLocalMaster(linkDes.src().deviceId())) {
Satish K690d5cf2017-04-04 15:12:38 +0530318 registerBandwidthAndTeMetric(linkDes, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530319 }
320
321 //Updating ports of the link
Priyanka B2312d222016-06-06 14:15:07 +0530322 deviceProviderService.updatePorts(linkDes.src().deviceId(), buildPortDescriptions(linkDes.src().deviceId(),
323 linkDes.src().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530324
Priyanka B2312d222016-06-06 14:15:07 +0530325 deviceProviderService.updatePorts(linkDes.dst().deviceId(), buildPortDescriptions(linkDes.dst().deviceId(),
326 linkDes.dst().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530327
Priyanka Bfc51c952016-03-26 14:30:33 +0530328 linkProviderService.linkDetected(linkDes);
329 }
330
331 //Build link description.
332 private LinkDescription buildLinkDes(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details, boolean isAddLink)
333 throws BgpParseException {
334 long srcAddress = 0;
335 long dstAddress = 0;
336 boolean localPseduo = false;
337 boolean remotePseduo = false;
338
339 List<BgpValueType> localTlvs = linkNlri.getLinkIdentifier().localNodeDescriptors().getSubTlvs();
340 for (BgpValueType localTlv : localTlvs) {
341 if (localTlv instanceof IsIsPseudonode || localTlv instanceof OspfPseudonode) {
342 localPseduo = true;
343 }
344 }
345 List<BgpValueType> remoteTlvs = linkNlri.getLinkIdentifier().remoteNodeDescriptors().getSubTlvs();
346 for (BgpValueType remoteTlv : remoteTlvs) {
347 if (remoteTlv instanceof IsIsPseudonode || remoteTlv instanceof OspfPseudonode) {
348 remotePseduo = true;
349 }
350 }
351
352 List<BgpValueType> tlvs = linkNlri.getLinkIdentifier().linkDescriptors();
353 for (BgpValueType tlv : tlvs) {
354 if (tlv instanceof LinkLocalRemoteIdentifiersTlv) {
355 srcAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkLocalIdentifier();
356 //Set 32nd bit.
357 srcAddress = srcAddress | IDENTIFIER_SET;
358 dstAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkRemoteIdentifier();
359 dstAddress = dstAddress | IDENTIFIER_SET;
360 } else if (tlv instanceof IPv4AddressTlv) {
361 if (tlv.getType() == BgpLinkLSIdentifier.IPV4_INTERFACE_ADDRESS_TYPE) {
362 srcAddress = ((IPv4AddressTlv) tlv).address().toInt();
363 } else {
364 dstAddress = ((IPv4AddressTlv) tlv).address().toInt();
365 }
366 }
367 }
368
369 DeviceId srcId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_LOCAL).toString()));
370 DeviceId dstId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_REMOTE).toString()));
371
372 if (localPseduo && srcAddress == 0) {
373 srcAddress = PSEUDO_PORT;
374 } else if (remotePseduo && dstAddress == 0) {
375 dstAddress = PSEUDO_PORT;
376 }
377
378 ConnectPoint src = new ConnectPoint(srcId, PortNumber.portNumber(srcAddress));
379 ConnectPoint dst = new ConnectPoint(dstId, PortNumber.portNumber(dstAddress));
380 BgpNodeLSNlriVer4 srcNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
381 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().localNodeDescriptors()), false,
382 linkNlri.getRouteDistinguisher());
383
384 BgpNodeLSNlriVer4 dstNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
385 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().remoteNodeDescriptors()), false,
386 linkNlri.getRouteDistinguisher());
387
388 addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri,
389 dstNodeNlri, srcId, dstId, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530390 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
391 if (details != null) {
392 annotationBuilder = getAnnotations(annotationBuilder, false, details);
393 }
394
395 return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false, annotationBuilder.build());
Priyanka Bfc51c952016-03-26 14:30:33 +0530396 }
397
398 private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo,
399 BgpNodeLSNlriVer4 srcNodeNlri, BgpNodeLSNlriVer4 dstNodeNlri, DeviceId srcId, DeviceId dstId,
400 PathAttrNlriDetails details) {
401 if (isAddLink) {
402 if (localPseduo) {
403 if (deviceService.getDevice(srcId) == null) {
404 for (BgpNodeListener l : controller.listener()) {
405 l.addNode(srcNodeNlri, details);
406 }
407 }
408 } else if (remotePseduo) {
409 if (deviceService.getDevice(dstId) == null) {
410 for (BgpNodeListener l : controller.listener()) {
411 l.addNode(dstNodeNlri, details);
412 }
413 }
414 }
415 } else {
416 if (localPseduo) {
417 Set<Link> links = linkService.getDeviceLinks(srcId);
418 if (links == null || links.isEmpty()) {
419 for (BgpNodeListener l : controller.listener()) {
420 l.deleteNode(srcNodeNlri);
421 }
422 }
423 } else if (remotePseduo) {
424 log.info("Remote pseudo delete link ");
425 Set<Link> links = linkService.getDeviceLinks(dstId);
426 if (links == null || links.isEmpty()) {
427 for (BgpNodeListener l : controller.listener()) {
428 l.deleteNode(dstNodeNlri);
429 }
430 }
431 }
432 }
433 }
434
435 @Override
436 public void deleteLink(BgpLinkLsNlriVer4 linkNlri) throws BgpParseException {
437 log.debug("Delete link {}", linkNlri.toString());
438
439 if (linkProviderService == null) {
440 return;
441 }
442
443 LinkDescription linkDes = buildLinkDes(linkNlri, null, false);
Priyanka B658aa6982016-05-27 21:34:49 +0530444
445 /*
446 * Only master for the link src will release the bandwidth resource.
447 */
448 if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
449 // Releases registered resource for this link
Satish K690d5cf2017-04-04 15:12:38 +0530450 networkConfigService.removeConfig(LinkKey.linkKey(linkDes.src(), linkDes.dst()), TeLinkConfig.class);
Priyanka B658aa6982016-05-27 21:34:49 +0530451 }
452
Priyanka Bfc51c952016-03-26 14:30:33 +0530453 linkProviderService.linkVanished(linkDes);
Priyanka B4c3b4512016-07-22 11:41:49 +0530454
455 linkDes = new DefaultLinkDescription(linkDes.dst(), linkDes.src(), Link.Type.DIRECT,
456 false, linkDes.annotations());
457 linkProviderService.linkVanished(linkDes);
Satish K690d5cf2017-04-04 15:12:38 +0530458 if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
459 // Releases registered resource for this link
460 networkConfigService.removeConfig(LinkKey.linkKey(linkDes.src(), linkDes.dst()), TeLinkConfig.class);
461 }
462
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
Satish K690d5cf2017-04-04 15:12:38 +0530475 private void registerBandwidthAndTeMetric(LinkDescription linkDes, PathAttrNlriDetails details) {
Priyanka B658aa6982016-05-27 21:34:49 +0530476 if (details == null) {
Thomas Vachuska5b38dc02018-05-10 15:24:40 -0700477 log.error("Unable to register bandwidth ");
Priyanka B658aa6982016-05-27 21:34:49 +0530478 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();
Satish K690d5cf2017-04-04 15:12:38 +0530488 double maxReservableBw = 0;
489 List<Float> unreservedBw = new ArrayList<>();
490 int teMetric = 0;
491 int igpMetric = 0;
Priyanka B658aa6982016-05-27 21:34:49 +0530492
493 for (BgpValueType tlv : tlvs) {
494 switch (tlv.getType()) {
495 case LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH:
496 maxReservableBw = ((BgpLinkAttrMaxLinkBandwidth) tlv).linkAttrMaxLinkBandwidth();
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530497 //will get in bits/second , convert to MBPS to store in network config service
498 maxReservableBw = maxReservableBw / 1000000;
Priyanka B658aa6982016-05-27 21:34:49 +0530499 break;
Satish K690d5cf2017-04-04 15:12:38 +0530500 case LinkStateAttributes.ATTR_LINK_UNRES_BANDWIDTH:
501 unreservedBw = ((BgpLinkAttrUnRsrvdLinkBandwidth) tlv).getLinkAttrUnRsrvdLinkBandwidth();
502 break;
503 case LinkStateAttributes.ATTR_LINK_TE_DEFAULT_METRIC:
504 teMetric = ((BgpLinkAttrTeDefaultMetric) tlv).attrLinkDefTeMetric();
505 break;
506 case LinkStateAttributes.ATTR_LINK_IGP_METRIC:
507 igpMetric = ((BgpLinkAttrIgpMetric) tlv).attrLinkIgpMetric();
508 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530509 default: // do nothing
510 }
511 }
512
Satish K690d5cf2017-04-04 15:12:38 +0530513 //Configure bandwidth for src and dst port
514 TeLinkConfig config = networkConfigService.addConfig(LinkKey.linkKey(linkDes.src(), linkDes.dst()),
515 TeLinkConfig.class);
516 Double bw = 0.0;
517 if (unreservedBw.size() > 0) {
518 bw = unreservedBw.get(0).doubleValue(); //Low priority
Priyanka B658aa6982016-05-27 21:34:49 +0530519 }
520
Satish K690d5cf2017-04-04 15:12:38 +0530521 config.maxResvBandwidth(maxReservableBw)
522 .unResvBandwidth(bw).teCost(teMetric).igpCost(igpMetric);
523 //.apply();
Priyanka B658aa6982016-05-27 21:34:49 +0530524
Satish K690d5cf2017-04-04 15:12:38 +0530525 networkConfigService.applyConfig(LinkKey.linkKey(linkDes.src(),
526 linkDes.dst()), TeLinkConfig.class, config.node());
Priyanka B658aa6982016-05-27 21:34:49 +0530527 }
528
529 private DefaultAnnotations.Builder getAnnotations(DefaultAnnotations.Builder annotationBuilder, boolean isNode,
530 PathAttrNlriDetails details) {
531
532 List<BgpValueType> attribute = details.pathAttributes().stream()
533 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
534 if (attribute.isEmpty()) {
535 return annotationBuilder;
536 }
537 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
538 boolean abrBit = false;
539 boolean externalBit = false;
540 boolean pseudo = false;
Priyanka B658aa6982016-05-27 21:34:49 +0530541 byte[] areaId = null;
542 Ip4Address routerId = null;
543 for (BgpValueType tlv : tlvs) {
544 switch (tlv.getType()) {
545 case LinkStateAttributes.ATTR_NODE_FLAG_BITS:
546 abrBit = ((BgpAttrNodeFlagBitTlv) tlv).abrBit();
547 externalBit = ((BgpAttrNodeFlagBitTlv) tlv).externalBit();
548 break;
549 case NodeDescriptors.IGP_ROUTERID_TYPE:
550 if (tlv instanceof IsIsPseudonode || tlv instanceof OspfPseudonode) {
551 pseudo = true;
552 }
553 break;
554 case LinkStateAttributes.ATTR_NODE_ISIS_AREA_ID:
555 areaId = ((BgpAttrNodeIsIsAreaId) tlv).attrNodeIsIsAreaId();
556 break;
557 case LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID:
558 routerId = ((BgpAttrRouterIdV4) tlv).attrRouterId();
559 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530560 default: // do nothing
561 }
562 }
563
564 // Annotations for device
565 if (isNode) {
566 boolean internalBit = false;
567 if (!abrBit && !externalBit) {
568 internalBit = true;
569 }
570
571 annotationBuilder.set(EXTERNAL_BIT, String.valueOf(externalBit));
572 annotationBuilder.set(ABR_BIT, String.valueOf(abrBit));
573 annotationBuilder.set(INTERNAL_BIT, String.valueOf(internalBit));
574 annotationBuilder.set(PSEUDO, String.valueOf(pseudo));
575
576 if (areaId != null) {
577 annotationBuilder.set(AREAID, new String(areaId));
578 }
579 if (routerId != null) {
580 // LsrID
581 annotationBuilder.set(LSRID, String.valueOf(routerId));
582 }
Priyanka B658aa6982016-05-27 21:34:49 +0530583 }
584 return annotationBuilder;
585 }
586
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530587 @Override
588 public void triggerProbe(DeviceId deviceId) {
589 // TODO Auto-generated method stub
590 }
591
592 @Override
593 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
594 }
595
596 @Override
597 public boolean isReachable(DeviceId deviceId) {
598 // TODO Auto-generated method stub
599 return true;
600 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700601
602 @Override
603 public void changePortState(DeviceId deviceId, PortNumber portNumber,
604 boolean enable) {
605 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530606}