blob: 9997659f407497aec7d5975f1895d7d13f901d29 [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;
Priyanka B658aa6982016-05-27 21:34:49 +053066import org.onosproject.mastership.MastershipService;
Priyanka Bfc51c952016-03-26 14:30:33 +053067import org.onosproject.net.AnnotationKeys;
68import org.onosproject.net.ConnectPoint;
69import org.onosproject.net.DefaultAnnotations;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053070import org.onosproject.net.Device;
71import org.onosproject.net.DeviceId;
Priyanka Bfc51c952016-03-26 14:30:33 +053072import org.onosproject.net.Link;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053073import org.onosproject.net.MastershipRole;
Saurav Dasa2d37502016-03-25 17:50:40 -070074import org.onosproject.net.PortNumber;
Priyanka B658aa6982016-05-27 21:34:49 +053075import org.onosproject.net.config.NetworkConfigService;
Sho SHIMIZUb6c63a32016-05-26 12:07:19 -070076import org.onosproject.net.config.basics.BandwidthCapacity;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053077import org.onosproject.net.device.DefaultDeviceDescription;
Priyanka B658aa6982016-05-27 21:34:49 +053078import org.onosproject.net.device.DefaultPortDescription;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053079import org.onosproject.net.device.DeviceDescription;
Priyanka B4c3b4512016-07-22 11:41:49 +053080import org.onosproject.net.device.DeviceEvent;
81import org.onosproject.net.device.DeviceListener;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +053082import 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 B4c3b4512016-07-22 11:41:49 +0530143 private DeviceListener deviceListener = new InternalDeviceListener();
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 B4c3b4512016-07-22 11:41:49 +0530172 deviceService.addListener(deviceListener);
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 B4c3b4512016-07-22 11:41:49 +0530185 deviceService.removeListener(deviceListener);
Priyanka B658aa6982016-05-27 21:34:49 +0530186 }
187
Priyanka B4c3b4512016-07-22 11:41:49 +0530188 private class InternalDeviceListener implements DeviceListener {
Priyanka B658aa6982016-05-27 21:34:49 +0530189 @Override
Priyanka B4c3b4512016-07-22 11:41:49 +0530190 public void event(DeviceEvent event) {
191 Device device = event.subject();
192
193 switch (event.type()) {
194 case DEVICE_ADDED:
195 if (!mastershipService.isLocalMaster(device.id())) {
196 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530197 }
Priyanka B4c3b4512016-07-22 11:41:49 +0530198
199 // Reserve device label pool for L3 devices
Priyanka B658aa6982016-05-27 21:34:49 +0530200 if (device.annotations().value(LSRID) != null) {
Priyanka B4c3b4512016-07-22 11:41:49 +0530201 createDevicePool(device.id());
Priyanka B658aa6982016-05-27 21:34:49 +0530202 }
Priyanka B4c3b4512016-07-22 11:41:49 +0530203 break;
204
205 default:
206 break;
Priyanka B658aa6982016-05-27 21:34:49 +0530207 }
208 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530209 }
210
211 /*
212 * Implements device and link update.
213 */
Priyanka Bfc51c952016-03-26 14:30:33 +0530214 private class InternalBgpProvider implements BgpNodeListener, BgpLinkListener {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530215
216 @Override
Priyanka Bfc51c952016-03-26 14:30:33 +0530217 public void addNode(BgpNodeLSNlriVer4 nodeNlri, PathAttrNlriDetails details) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530218 log.debug("Add node {}", nodeNlri.toString());
219
Priyanka B658aa6982016-05-27 21:34:49 +0530220 if (deviceProviderService == null || deviceService == null) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530221 return;
222 }
Priyanka Bfc51c952016-03-26 14:30:33 +0530223 Device.Type deviceType = ROUTER;
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530224 BgpDpid nodeUri = new BgpDpid(nodeNlri);
225 DeviceId deviceId = deviceId(uri(nodeUri.toString()));
226 ChassisId cId = new ChassisId();
227
Priyanka B658aa6982016-05-27 21:34:49 +0530228 /*
229 * Check if device is already there (available) , if yes not updating to core.
230 */
231 if (deviceService.isAvailable(deviceId)) {
232 return;
233 }
234
Priyanka Bfc51c952016-03-26 14:30:33 +0530235 DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
236
237 newBuilder.set(AnnotationKeys.TYPE, "L3");
238 newBuilder.set(ROUTING_UNIVERSE, Long.toString(nodeNlri.getIdentifier()));
239
240 List<BgpValueType> tlvs = nodeNlri.getLocalNodeDescriptors().getNodedescriptors().getSubTlvs();
241 for (BgpValueType tlv : tlvs) {
242 if (tlv instanceof AutonomousSystemTlv) {
243 newBuilder.set(AS_NUMBER, Integer.toString(((AutonomousSystemTlv) tlv).getAsNum()));
244 } else if (tlv instanceof BgpLSIdentifierTlv) {
245 newBuilder.set(DOMAIN_IDENTIFIER,
246 Integer.toString(((BgpLSIdentifierTlv) tlv).getBgpLsIdentifier()));
247 }
248 if (tlv.getType() == NodeDescriptors.IGP_ROUTERID_TYPE) {
249 if (tlv instanceof IsIsPseudonode) {
250 deviceType = VIRTUAL;
Priyanka B19c08732016-06-04 21:11:19 +0530251 newBuilder.set(AnnotationKeys.ROUTER_ID, nodeUri.isoNodeIdString(((IsIsPseudonode) tlv)
252 .getIsoNodeId()));
Priyanka Bfc51c952016-03-26 14:30:33 +0530253 } else if (tlv instanceof OspfPseudonode) {
254 deviceType = VIRTUAL;
255 newBuilder
256 .set(AnnotationKeys.ROUTER_ID, Integer.toString(((OspfPseudonode) tlv).getrouterID()));
257 } else if (tlv instanceof IsIsNonPseudonode) {
Priyanka B19c08732016-06-04 21:11:19 +0530258 newBuilder.set(AnnotationKeys.ROUTER_ID, nodeUri.isoNodeIdString(((IsIsNonPseudonode) tlv)
259 .getIsoNodeId()));
Priyanka Bfc51c952016-03-26 14:30:33 +0530260 } else if (tlv instanceof OspfNonPseudonode) {
261 newBuilder.set(AnnotationKeys.ROUTER_ID,
262 Integer.toString(((OspfNonPseudonode) tlv).getrouterID()));
263 }
264 }
265 }
Priyanka B658aa6982016-05-27 21:34:49 +0530266 DefaultAnnotations.Builder anntotations = DefaultAnnotations.builder();
267 anntotations = getAnnotations(newBuilder, true, details);
Priyanka Bfc51c952016-03-26 14:30:33 +0530268
269 DeviceDescription description = new DefaultDeviceDescription(uri(nodeUri.toString()), deviceType, UNKNOWN,
Priyanka B658aa6982016-05-27 21:34:49 +0530270 UNKNOWN, UNKNOWN, UNKNOWN, cId, anntotations.build());
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530271
Priyanka B658aa6982016-05-27 21:34:49 +0530272 deviceProviderService.deviceConnected(deviceId, description);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530273 }
274
275 @Override
Shashikanth VH5dd8dbe2015-11-26 13:22:18 +0530276 public void deleteNode(BgpNodeLSNlriVer4 nodeNlri) {
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530277 log.debug("Delete node {}", nodeNlri.toString());
278
279 if (deviceProviderService == null) {
280 return;
281 }
282
Priyanka Bfc51c952016-03-26 14:30:33 +0530283 BgpDpid deviceUri = new BgpDpid(nodeNlri);
284 DeviceId deviceId = deviceId(uri(deviceUri.toString()));
Priyanka B658aa6982016-05-27 21:34:49 +0530285
286 if (labelResourceAdminService != null) {
287 //Destroy local device label pool reserved for that device
288 labelResourceAdminService.destroyDevicePool(deviceId);
289 }
290
Priyanka Bfc51c952016-03-26 14:30:33 +0530291 deviceProviderService.deviceDisconnected(deviceId);
292 }
293
Priyanka B2312d222016-06-06 14:15:07 +0530294 private List<PortDescription> buildPortDescriptions(DeviceId deviceId,
295 PortNumber portNumber) {
296
297 List<PortDescription> portList;
298
299 if (portMap.containsKey(deviceId)) {
300 portList = portMap.get(deviceId);
301 } else {
302 portList = new ArrayList<>();
303 }
304 if (portNumber != null) {
305 PortDescription portDescriptions = new DefaultPortDescription(portNumber, true);
306 portList.add(portDescriptions);
307 }
308
309 portMap.put(deviceId, portList);
310 return portList;
311 }
312
Priyanka Bfc51c952016-03-26 14:30:33 +0530313 @Override
314 public void addLink(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details) throws BgpParseException {
315 log.debug("Addlink {}", linkNlri.toString());
316
Priyanka B2312d222016-06-06 14:15:07 +0530317 LinkDescription linkDes = buildLinkDes(linkNlri, details, true);
318
319 //If already link exists, return
320 if (linkService.getLink(linkDes.src(), linkDes.dst()) != null || linkProviderService == null) {
Priyanka Bfc51c952016-03-26 14:30:33 +0530321 return;
322 }
Priyanka B658aa6982016-05-27 21:34:49 +0530323
324 /*
325 * Update link ports and configure bandwidth on source and destination port using networkConfig service
326 * Only master of source link registers for bandwidth
327 */
328 if (mastershipService.isLocalMaster(linkDes.src().deviceId())) {
329 registerBandwidth(linkDes, details);
330 }
331
332 //Updating ports of the link
Priyanka B2312d222016-06-06 14:15:07 +0530333 deviceProviderService.updatePorts(linkDes.src().deviceId(), buildPortDescriptions(linkDes.src().deviceId(),
334 linkDes.src().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530335
Priyanka B2312d222016-06-06 14:15:07 +0530336 deviceProviderService.updatePorts(linkDes.dst().deviceId(), buildPortDescriptions(linkDes.dst().deviceId(),
337 linkDes.dst().port()));
Priyanka B658aa6982016-05-27 21:34:49 +0530338
Priyanka Bfc51c952016-03-26 14:30:33 +0530339 linkProviderService.linkDetected(linkDes);
340 }
341
342 //Build link description.
343 private LinkDescription buildLinkDes(BgpLinkLsNlriVer4 linkNlri, PathAttrNlriDetails details, boolean isAddLink)
344 throws BgpParseException {
345 long srcAddress = 0;
346 long dstAddress = 0;
347 boolean localPseduo = false;
348 boolean remotePseduo = false;
349
350 List<BgpValueType> localTlvs = linkNlri.getLinkIdentifier().localNodeDescriptors().getSubTlvs();
351 for (BgpValueType localTlv : localTlvs) {
352 if (localTlv instanceof IsIsPseudonode || localTlv instanceof OspfPseudonode) {
353 localPseduo = true;
354 }
355 }
356 List<BgpValueType> remoteTlvs = linkNlri.getLinkIdentifier().remoteNodeDescriptors().getSubTlvs();
357 for (BgpValueType remoteTlv : remoteTlvs) {
358 if (remoteTlv instanceof IsIsPseudonode || remoteTlv instanceof OspfPseudonode) {
359 remotePseduo = true;
360 }
361 }
362
363 List<BgpValueType> tlvs = linkNlri.getLinkIdentifier().linkDescriptors();
364 for (BgpValueType tlv : tlvs) {
365 if (tlv instanceof LinkLocalRemoteIdentifiersTlv) {
366 srcAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkLocalIdentifier();
367 //Set 32nd bit.
368 srcAddress = srcAddress | IDENTIFIER_SET;
369 dstAddress = ((LinkLocalRemoteIdentifiersTlv) tlv).getLinkRemoteIdentifier();
370 dstAddress = dstAddress | IDENTIFIER_SET;
371 } else if (tlv instanceof IPv4AddressTlv) {
372 if (tlv.getType() == BgpLinkLSIdentifier.IPV4_INTERFACE_ADDRESS_TYPE) {
373 srcAddress = ((IPv4AddressTlv) tlv).address().toInt();
374 } else {
375 dstAddress = ((IPv4AddressTlv) tlv).address().toInt();
376 }
377 }
378 }
379
380 DeviceId srcId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_LOCAL).toString()));
381 DeviceId dstId = deviceId(uri(new BgpDpid(linkNlri, BgpDpid.NODE_DESCRIPTOR_REMOTE).toString()));
382
383 if (localPseduo && srcAddress == 0) {
384 srcAddress = PSEUDO_PORT;
385 } else if (remotePseduo && dstAddress == 0) {
386 dstAddress = PSEUDO_PORT;
387 }
388
389 ConnectPoint src = new ConnectPoint(srcId, PortNumber.portNumber(srcAddress));
390 ConnectPoint dst = new ConnectPoint(dstId, PortNumber.portNumber(dstAddress));
391 BgpNodeLSNlriVer4 srcNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
392 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().localNodeDescriptors()), false,
393 linkNlri.getRouteDistinguisher());
394
395 BgpNodeLSNlriVer4 dstNodeNlri = new BgpNodeLSNlriVer4(linkNlri.getIdentifier(), linkNlri.getProtocolId()
396 .getType(), new BgpNodeLSIdentifier(linkNlri.getLinkIdentifier().remoteNodeDescriptors()), false,
397 linkNlri.getRouteDistinguisher());
398
399 addOrDeletePseudoNode(isAddLink, localPseduo, remotePseduo, srcNodeNlri,
400 dstNodeNlri, srcId, dstId, details);
Priyanka B658aa6982016-05-27 21:34:49 +0530401 DefaultAnnotations.Builder annotationBuilder = DefaultAnnotations.builder();
402 if (details != null) {
403 annotationBuilder = getAnnotations(annotationBuilder, false, details);
404 }
405
406 return new DefaultLinkDescription(src, dst, Link.Type.DIRECT, false, annotationBuilder.build());
Priyanka Bfc51c952016-03-26 14:30:33 +0530407 }
408
409 private void addOrDeletePseudoNode(boolean isAddLink, boolean localPseduo, boolean remotePseduo,
410 BgpNodeLSNlriVer4 srcNodeNlri, BgpNodeLSNlriVer4 dstNodeNlri, DeviceId srcId, DeviceId dstId,
411 PathAttrNlriDetails details) {
412 if (isAddLink) {
413 if (localPseduo) {
414 if (deviceService.getDevice(srcId) == null) {
415 for (BgpNodeListener l : controller.listener()) {
416 l.addNode(srcNodeNlri, details);
417 }
418 }
419 } else if (remotePseduo) {
420 if (deviceService.getDevice(dstId) == null) {
421 for (BgpNodeListener l : controller.listener()) {
422 l.addNode(dstNodeNlri, details);
423 }
424 }
425 }
426 } else {
427 if (localPseduo) {
428 Set<Link> links = linkService.getDeviceLinks(srcId);
429 if (links == null || links.isEmpty()) {
430 for (BgpNodeListener l : controller.listener()) {
431 l.deleteNode(srcNodeNlri);
432 }
433 }
434 } else if (remotePseduo) {
435 log.info("Remote pseudo delete link ");
436 Set<Link> links = linkService.getDeviceLinks(dstId);
437 if (links == null || links.isEmpty()) {
438 for (BgpNodeListener l : controller.listener()) {
439 l.deleteNode(dstNodeNlri);
440 }
441 }
442 }
443 }
444 }
445
446 @Override
447 public void deleteLink(BgpLinkLsNlriVer4 linkNlri) throws BgpParseException {
448 log.debug("Delete link {}", linkNlri.toString());
449
450 if (linkProviderService == null) {
451 return;
452 }
453
454 LinkDescription linkDes = buildLinkDes(linkNlri, null, false);
Priyanka B658aa6982016-05-27 21:34:49 +0530455
456 /*
457 * Only master for the link src will release the bandwidth resource.
458 */
459 if (networkConfigService != null && mastershipService.isLocalMaster(linkDes.src().deviceId())) {
460 // Releases registered resource for this link
461 networkConfigService.removeConfig(linkDes.src(), BandwidthCapacity.class);
462 networkConfigService.removeConfig(linkDes.dst(), BandwidthCapacity.class);
463 }
464
Priyanka Bfc51c952016-03-26 14:30:33 +0530465 linkProviderService.linkVanished(linkDes);
Priyanka B4c3b4512016-07-22 11:41:49 +0530466
467 linkDes = new DefaultLinkDescription(linkDes.dst(), linkDes.src(), Link.Type.DIRECT,
468 false, linkDes.annotations());
469 linkProviderService.linkVanished(linkDes);
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530470 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530471 }
472
Priyanka B658aa6982016-05-27 21:34:49 +0530473 // Creates label resource pool for the specific device. For all devices label range is 5122-9217
474 private void createDevicePool(DeviceId deviceId) {
475 if (labelResourceAdminService == null) {
476 return;
477 }
478
479 labelResourceAdminService.createDevicePool(deviceId, beginLabel, endLabel);
480 }
481
482 private void registerBandwidth(LinkDescription linkDes, PathAttrNlriDetails details) {
483 if (details == null) {
484 log.error("Couldnot able to register bandwidth ");
485 return;
486 }
487
488 List<BgpValueType> attribute = details.pathAttributes().stream()
489 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
490 if (attribute.isEmpty()) {
491 return;
492 }
493
494 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
495 float maxReservableBw = 0;
496
497 for (BgpValueType tlv : tlvs) {
498 switch (tlv.getType()) {
499 case LinkStateAttributes.ATTR_LINK_MAX_RES_BANDWIDTH:
500 maxReservableBw = ((BgpLinkAttrMaxLinkBandwidth) tlv).linkAttrMaxLinkBandwidth();
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530501 //will get in bits/second , convert to MBPS to store in network config service
502 maxReservableBw = maxReservableBw / 1000000;
Priyanka B658aa6982016-05-27 21:34:49 +0530503 break;
504 default: // do nothing
505 }
506 }
507
508 if (maxReservableBw == 0.0) {
509 return;
510 }
511
512 //Configure bandwidth for src and dst port
513 BandwidthCapacity config = networkConfigService.addConfig(linkDes.src(), BandwidthCapacity.class);
514 config.capacity(Bandwidth.bps(maxReservableBw)).apply();
515
516 config = networkConfigService.addConfig(linkDes.dst(), BandwidthCapacity.class);
517 config.capacity(Bandwidth.bps(maxReservableBw)).apply();
518 }
519
520 private DefaultAnnotations.Builder getAnnotations(DefaultAnnotations.Builder annotationBuilder, boolean isNode,
521 PathAttrNlriDetails details) {
522
523 List<BgpValueType> attribute = details.pathAttributes().stream()
524 .filter(attr -> attr instanceof LinkStateAttributes).collect(toList());
525 if (attribute.isEmpty()) {
526 return annotationBuilder;
527 }
528 List<BgpValueType> tlvs = ((LinkStateAttributes) attribute.iterator().next()).linkStateAttributes();
529 boolean abrBit = false;
530 boolean externalBit = false;
531 boolean pseudo = false;
532 int igpMetric = 0;
533 int teMetric = 0;
534 byte[] areaId = null;
535 Ip4Address routerId = null;
536 for (BgpValueType tlv : tlvs) {
537 switch (tlv.getType()) {
538 case LinkStateAttributes.ATTR_NODE_FLAG_BITS:
539 abrBit = ((BgpAttrNodeFlagBitTlv) tlv).abrBit();
540 externalBit = ((BgpAttrNodeFlagBitTlv) tlv).externalBit();
541 break;
542 case NodeDescriptors.IGP_ROUTERID_TYPE:
543 if (tlv instanceof IsIsPseudonode || tlv instanceof OspfPseudonode) {
544 pseudo = true;
545 }
546 break;
547 case LinkStateAttributes.ATTR_NODE_ISIS_AREA_ID:
548 areaId = ((BgpAttrNodeIsIsAreaId) tlv).attrNodeIsIsAreaId();
549 break;
550 case LinkStateAttributes.ATTR_NODE_IPV4_LOCAL_ROUTER_ID:
551 routerId = ((BgpAttrRouterIdV4) tlv).attrRouterId();
552 break;
553 case LinkStateAttributes.ATTR_LINK_IGP_METRIC:
554 igpMetric = ((BgpLinkAttrIgpMetric) tlv).attrLinkIgpMetric();
555 break;
556 case LinkStateAttributes.ATTR_LINK_TE_DEFAULT_METRIC:
557 teMetric = ((BgpLinkAttrTeDefaultMetric) tlv).attrLinkDefTeMetric();
558 break;
559 default: // do nothing
560 }
561 }
562
563 // Annotations for device
564 if (isNode) {
565 boolean internalBit = false;
566 if (!abrBit && !externalBit) {
567 internalBit = true;
568 }
569
570 annotationBuilder.set(EXTERNAL_BIT, String.valueOf(externalBit));
571 annotationBuilder.set(ABR_BIT, String.valueOf(abrBit));
572 annotationBuilder.set(INTERNAL_BIT, String.valueOf(internalBit));
573 annotationBuilder.set(PSEUDO, String.valueOf(pseudo));
574
575 if (areaId != null) {
576 annotationBuilder.set(AREAID, new String(areaId));
577 }
578 if (routerId != null) {
579 // LsrID
580 annotationBuilder.set(LSRID, String.valueOf(routerId));
581 }
582 } else {
583 // Annotations for link
584 if (igpMetric != 0) {
585 annotationBuilder.set(COST, String.valueOf(igpMetric));
586 }
587
588 if (teMetric != 0) {
589 annotationBuilder.set(TE_COST, String.valueOf(teMetric));
590 }
591 }
592 return annotationBuilder;
593 }
594
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530595 @Override
596 public void triggerProbe(DeviceId deviceId) {
597 // TODO Auto-generated method stub
598 }
599
600 @Override
601 public void roleChanged(DeviceId deviceId, MastershipRole newRole) {
602 }
603
604 @Override
605 public boolean isReachable(DeviceId deviceId) {
606 // TODO Auto-generated method stub
607 return true;
608 }
Saurav Dasa2d37502016-03-25 17:50:40 -0700609
610 @Override
611 public void changePortState(DeviceId deviceId, PortNumber portNumber,
612 boolean enable) {
613 }
Shashikanth VH1ca26ce2015-11-20 23:19:49 +0530614}