blob: 8d501499522080b166a1d8d4b3ad3f7a621bbd46 [file] [log] [blame]
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +053016package org.onosproject.pcep.server.impl;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053017
18import static com.google.common.base.Preconditions.checkNotNull;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +053019import static org.onosproject.pcep.server.PcepSyncStatus.IN_SYNC;
20import static org.onosproject.pcep.server.PcepSyncStatus.SYNCED;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053021
22import java.util.Collection;
23import java.util.Iterator;
24import java.util.HashSet;
25import java.util.List;
26import java.util.LinkedList;
27import java.util.Map;
28import java.util.Set;
29
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053030import org.onlab.packet.IpAddress;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053031import org.onosproject.incubator.net.resource.label.DefaultLabelResource;
32import org.onosproject.incubator.net.resource.label.LabelResource;
33import org.onosproject.incubator.net.resource.label.LabelResourceId;
34import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
35import org.onosproject.incubator.net.resource.label.LabelResourceService;
36import org.onosproject.incubator.net.tunnel.DefaultLabelStack;
37import org.onosproject.incubator.net.tunnel.LabelStack;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053038import org.onosproject.net.device.DeviceService;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053039import org.onosproject.pcelabelstore.PcepLabelOp;
40import org.onosproject.pcelabelstore.api.PceLabelStore;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053041import org.onosproject.net.Device;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053042import org.onosproject.net.DeviceId;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053043import org.onosproject.net.Link;
44import org.onosproject.net.Path;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +053045import org.onosproject.pcep.server.PccId;
46import org.onosproject.pcep.server.PcepClient;
47import org.onosproject.pcep.server.PcepClientController;
48import org.onosproject.pcep.server.SrpIdGenerators;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053049import org.onosproject.pcepio.exceptions.PcepParseException;
50import org.onosproject.pcepio.protocol.PcepFecObjectIPv4;
51import org.onosproject.pcepio.protocol.PcepFecObjectIPv4Adjacency;
52import org.onosproject.pcepio.protocol.PcepLabelObject;
53import org.onosproject.pcepio.protocol.PcepLabelUpdate;
54import org.onosproject.pcepio.protocol.PcepLabelUpdateMsg;
55import org.onosproject.pcepio.protocol.PcepSrpObject;
56import org.onosproject.pcepio.types.PcepLabelMap;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053057import org.slf4j.Logger;
58import org.slf4j.LoggerFactory;
59
60import com.google.common.collect.ArrayListMultimap;
61import com.google.common.collect.Multimap;
62
63/**
64 * PCE SR-BE and SR-TE functionality.
65 * SR-BE: Each node (PCC) is allocated a node-SID (label) by the PCECC. The PCECC sends PCLabelUpd to
66 * update the label map of each node to all the nodes in the domain.
67 * SR-TE: apart from node-SID, Adj-SID is used where each adjacency is allocated an Adj-SID (label) by the PCECC.
68 * The PCECC sends PCLabelUpd to update the label map of each Adj to the corresponding nodes in the domain.
69 */
70public final class PceccSrTeBeHandler {
71 private static final Logger log = LoggerFactory.getLogger(PceccSrTeBeHandler.class);
72
73 private static final String LABEL_RESOURCE_ADMIN_SERVICE_NULL = "Label Resource Admin Service cannot be null";
74 private static final String LABEL_RESOURCE_SERVICE_NULL = "Label Resource Service cannot be null";
75 private static final String PCE_STORE_NULL = "PCE Store cannot be null";
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053076 private static final String DEVICE_ID_NULL = "Device-Id cannot be null";
77 private static final String LSR_ID_NULL = "LSR-Id cannot be null";
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053078 private static final String LINK_NULL = "Link cannot be null";
79 private static final String PATH_NULL = "Path cannot be null";
80 private static final String LSR_ID = "lsrId";
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053081 private static PceccSrTeBeHandler srTeHandlerInstance = null;
82 private LabelResourceAdminService labelRsrcAdminService;
83 private LabelResourceService labelRsrcService;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053084 private DeviceService deviceService;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053085 private PcepClientController clientController;
86 private PceLabelStore pceStore;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053087
88 /**
89 * Initializes default values.
90 */
91 private PceccSrTeBeHandler() {
92 }
93
94 /**
95 * Returns single instance of this class.
96 *
97 * @return this class single instance
98 */
99 public static PceccSrTeBeHandler getInstance() {
100 if (srTeHandlerInstance == null) {
101 srTeHandlerInstance = new PceccSrTeBeHandler();
102 }
103 return srTeHandlerInstance;
104 }
105
106 /**
107 * Initialization of label manager interfaces and pce store.
108 *
109 * @param labelRsrcAdminService label resource admin service
110 * @param labelRsrcService label resource service
Ray Milkeyef794342016-11-09 16:20:29 -0800111 * @param clientController client controller
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530112 * @param pceStore PCE label store
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530113 * @param deviceService device service
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530114 */
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530115 public void initialize(LabelResourceAdminService labelRsrcAdminService,
116 LabelResourceService labelRsrcService,
117 PcepClientController clientController,
118 PceLabelStore pceStore,
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530119 DeviceService deviceService) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530120 this.labelRsrcAdminService = labelRsrcAdminService;
121 this.labelRsrcService = labelRsrcService;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530122 this.clientController = clientController;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530123 this.pceStore = pceStore;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530124 this.deviceService = deviceService;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530125 }
126
127 /**
128 * Reserves the global label pool.
129 *
130 * @param beginLabel minimum value of global label space
131 * @param endLabel maximum value of global label space
132 * @return success or failure
133 */
134 public boolean reserveGlobalPool(long beginLabel, long endLabel) {
135 checkNotNull(labelRsrcAdminService, LABEL_RESOURCE_ADMIN_SERVICE_NULL);
136 return labelRsrcAdminService.createGlobalPool(LabelResourceId.labelResourceId(beginLabel),
137 LabelResourceId.labelResourceId(endLabel));
138 }
139
140 /**
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530141 * Retrieve lsr-id from device annotation.
142 *
143 * @param deviceId specific device id from which lsr-id needs to be retrieved
144 * @return lsr-id of a device
145 */
146 public String getLsrId(DeviceId deviceId) {
147 checkNotNull(deviceId, DEVICE_ID_NULL);
148 Device device = deviceService.getDevice(deviceId);
149 if (device == null) {
150 log.debug("Device is not available for device id {} in device service.", deviceId.toString());
151 return null;
152 }
153
154 // Retrieve lsr-id from device
155 if (device.annotations() == null) {
156 log.debug("Device {} does not have annotation.", device.toString());
157 return null;
158 }
159
160 String lsrId = device.annotations().value(LSR_ID);
161 if (lsrId == null) {
162 log.debug("The lsr-id of device {} is null.", device.toString());
163 return null;
164 }
165 return lsrId;
166 }
167
168 /**
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530169 * Allocates node label from global node label pool to specific device.
170 * Configure this device with labels and lsrid mapping of all other devices and vice versa.
171 *
172 * @param specificDeviceId node label needs to be allocated to specific device
173 * @param specificLsrId lsrid of specific device
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530174 * @return success or failure
175 */
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530176 public boolean allocateNodeLabel(DeviceId specificDeviceId, String specificLsrId) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530177 long applyNum = 1; // For each node only one node label
178 LabelResourceId specificLabelId = null;
179
180 checkNotNull(specificDeviceId, DEVICE_ID_NULL);
181 checkNotNull(specificLsrId, LSR_ID_NULL);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530182 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
183 checkNotNull(pceStore, PCE_STORE_NULL);
184
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530185 // Check whether node-label was already configured for this specific device.
186 if (pceStore.getGlobalNodeLabel(specificDeviceId) != null) {
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530187 log.debug("Node label was already configured for device {}.", specificDeviceId.toString());
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530188 return false;
189 }
190
191 // The specificDeviceId is the new device and is not there in the pce store.
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530192 // So, first generate its label and configure label and its lsr-id to it.
193 Collection<LabelResource> result = labelRsrcService.applyFromGlobalPool(applyNum);
Jon Hallcbd1b392017-01-18 20:15:44 -0800194 if (!result.isEmpty()) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530195 // Only one element (label-id) to retrieve
196 Iterator<LabelResource> iterator = result.iterator();
197 DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
198 specificLabelId = defaultLabelResource.labelResourceId();
199 if (specificLabelId == null) {
200 log.error("Unable to retrieve global node label for a device id {}.", specificDeviceId.toString());
201 return false;
202 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530203 } else {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530204 log.error("Unable to allocate global node label for a device id {}.", specificDeviceId.toString());
205 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530206 }
207
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530208 // store it
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530209 pceStore.addGlobalNodeLabel(specificDeviceId, specificLabelId);
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530210
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530211 // Push its label information into specificDeviceId
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530212 PcepClient pcc = getPcepClient(specificDeviceId);
213 try {
214 pushGlobalNodeLabel(pcc,
215 specificLabelId,
216 IpAddress.valueOf(specificLsrId).getIp4Address().toInt(),
217 PcepLabelOp.ADD,
218 false);
219 } catch (PcepParseException e) {
220 log.error("Failed to push global node label for LSR {}.", specificLsrId.toString());
221 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530222
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530223 // Configure (node-label, lsr-id) mapping of each devices into specific device and vice versa.
224 for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) {
225 DeviceId otherDevId = element.getKey();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530226
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530227 // Get lsr-id of a device
228 String otherLsrId = getLsrId(otherDevId);
229 if (otherLsrId == null) {
230 log.error("The lsr-id of device id {} is null.", otherDevId.toString());
231 releaseNodeLabel(specificDeviceId, specificLsrId);
232 return false;
233 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530234
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530235 // Push to device
236 // Push label information of specificDeviceId to otherDevId in list and vice versa.
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530237 if (!otherDevId.equals(specificDeviceId)) {
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530238 try {
239 pushGlobalNodeLabel(getPcepClient(otherDevId),
240 specificLabelId,
241 IpAddress.valueOf(specificLsrId).getIp4Address().toInt(),
242 PcepLabelOp.ADD,
243 false);
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530244
Priyanka B1dd60122016-12-29 16:33:45 +0530245 pushGlobalNodeLabel(pcc, element.getValue(),
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530246 IpAddress.valueOf(otherLsrId).getIp4Address().toInt(),
247 PcepLabelOp.ADD,
248 false);
249 } catch (PcepParseException e) {
250 log.error("Failed to push global node label for LSR {} or LSR {}.", specificLsrId.toString(),
251 otherLsrId.toString());
252 }
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530253 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530254 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530255 return true;
256 }
257
258 /**
259 * Releases assigned node label of specific device from global node label pool and pce store.
260 * and remove configured this node label from all other devices.
261 *
262 * @param specificDeviceId node label needs to be released for specific device
263 * @param specificLsrId lsrid of specific device
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530264 * @return success or failure
265 */
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530266 public boolean releaseNodeLabel(DeviceId specificDeviceId, String specificLsrId) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530267 checkNotNull(specificDeviceId, DEVICE_ID_NULL);
268 checkNotNull(specificLsrId, LSR_ID_NULL);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530269 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
270 checkNotNull(pceStore, PCE_STORE_NULL);
271 boolean retValue = true;
272
273 // Release node label entry of this specific device from all other devices
274 // Retrieve node label of this specific device from store
275 LabelResourceId labelId = pceStore.getGlobalNodeLabel(specificDeviceId);
276 if (labelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530277 log.error("Unable to retrieve label of a device id {} from store.", specificDeviceId.toString());
278 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530279 }
280
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530281 // Go through all devices in the pce store and remove label entry from device
282 for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) {
283 DeviceId otherDevId = element.getKey();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530284
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530285 // Remove this specific device label information from all other nodes except
286 // this specific node where connection already lost.
287 if (!specificDeviceId.equals(otherDevId)) {
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530288 try {
289 pushGlobalNodeLabel(getPcepClient(otherDevId),
290 labelId,
291 IpAddress.valueOf(specificLsrId).getIp4Address().toInt(),
292 PcepLabelOp.REMOVE,
293 false);
294 } catch (PcepParseException e) {
295 log.error("Failed to push global node label for LSR {}.", specificLsrId.toString());
296 }
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530297 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530298 }
299
300 // Release from label manager
301 Set<LabelResourceId> release = new HashSet<>();
302 release.add(labelId);
303 if (!labelRsrcService.releaseToGlobalPool(release)) {
304 log.error("Unable to release label id {} from label manager.", labelId.toString());
305 retValue = false;
306 }
307
308 // Remove from store
309 if (!pceStore.removeGlobalNodeLabel(specificDeviceId)) {
310 log.error("Unable to remove global node label id {} from store.", labelId.toString());
311 retValue = false;
312 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530313 return retValue;
314 }
315
316 /**
317 * Allocates adjacency label to a link from local resource pool by a specific device id.
318 *
319 * @param link between devices
320 * @return success or failure
321 */
322 public boolean allocateAdjacencyLabel(Link link) {
323 long applyNum = 1; // Single label to each link.
324 DeviceId srcDeviceId = link.src().deviceId();
325 Collection<LabelResource> labelList;
326
327 checkNotNull(link, LINK_NULL);
328 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
329 checkNotNull(pceStore, PCE_STORE_NULL);
330
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530331 // Checks whether adjacency label was already allocated
332 LabelResourceId labelId = pceStore.getAdjLabel(link);
333 if (labelId != null) {
334 log.debug("Adjacency label {} was already allocated for a link {}.", labelId.toString(), link.toString());
335 return false;
336 }
337
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530338 // Allocate adjacency label to a link from label manager.
339 // Take label from source device pool to allocate.
340 labelList = labelRsrcService.applyFromDevicePool(srcDeviceId, applyNum);
Jon Hallcbd1b392017-01-18 20:15:44 -0800341 if (labelList.isEmpty()) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530342 log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
343 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530344 }
345
346 // Currently only one label to a device. So, no need to iterate through list
347 Iterator<LabelResource> iterator = labelList.iterator();
348 DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530349 labelId = defaultLabelResource.labelResourceId();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530350 if (labelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530351 log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
352 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530353 }
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530354 log.debug("Allocated adjacency label {} to a link {}.", labelId.toString(), link.toString());
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530355
356 // Push adjacency label to device
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530357 try {
358 pushAdjacencyLabel(getPcepClient(srcDeviceId), labelId, (int) link.src().port().toLong(),
359 (int) link.dst().port().toLong(), PcepLabelOp.ADD);
360 } catch (PcepParseException e) {
361 log.error("Failed to push adjacency label for link {}-{}.", (int) link.src().port().toLong(),
362 (int) link.dst().port().toLong());
363 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530364
365 // Save in store
366 pceStore.addAdjLabel(link, labelId);
367 return true;
368 }
369
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530370 /**
371 * Releases unused adjacency labels from device pools.
372 *
373 * @param link between devices
374 * @return success or failure
375 */
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530376 public boolean releaseAdjacencyLabel(Link link) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530377 checkNotNull(link, LINK_NULL);
378 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
379 checkNotNull(pceStore, PCE_STORE_NULL);
380 boolean retValue = true;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530381
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530382 // Retrieve link label from store
383 LabelResourceId labelId = pceStore.getAdjLabel(link);
384 if (labelId == null) {
385 log.error("Unabel to retrieve label for a link {} from store.", link.toString());
386 return false;
387 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530388
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530389 // Device
390 DeviceId srcDeviceId = link.src().deviceId();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530391
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530392 // Release adjacency label from device
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530393 try {
394 pushAdjacencyLabel(getPcepClient(srcDeviceId), labelId, (int) link.src().port().toLong(),
395 (int) link.dst().port().toLong(), PcepLabelOp.REMOVE);
396 } catch (PcepParseException e) {
397 log.error("Failed to push adjacency label for link {}-{}.", (int) link.src().port().toLong(),
398 (int) link.dst().port().toLong());
399 }
400
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530401
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530402 // Release link label from label manager
403 Multimap<DeviceId, LabelResource> release = ArrayListMultimap.create();
404 DefaultLabelResource defaultLabelResource = new DefaultLabelResource(srcDeviceId, labelId);
405 release.put(srcDeviceId, defaultLabelResource);
406 if (!labelRsrcService.releaseToDevicePool(release)) {
407 log.error("Unable to release label id {} from label manager.", labelId.toString());
408 retValue = false;
409 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530410
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530411 // Remove adjacency label from store
412 if (!pceStore.removeAdjLabel(link)) {
413 log.error("Unable to remove adjacency label id {} from store.", labelId.toString());
414 retValue = false;
415 }
416 return retValue;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530417 }
418
419 /**
420 * Computes label stack for a path.
421 *
422 * @param path lsp path
423 * @return label stack
424 */
425 public LabelStack computeLabelStack(Path path) {
426 checkNotNull(path, PATH_NULL);
427 // Label stack is linked list to make labels in order.
428 List<LabelResourceId> labelStack = new LinkedList<>();
429 List<Link> linkList = path.links();
Jon Hallcbd1b392017-01-18 20:15:44 -0800430 if ((linkList != null) && (!linkList.isEmpty())) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530431 // Path: [x] ---- [y] ---- [z]
432 // For other than last link, add only source[x] device label.
433 // For the last link, add both source[y] and destination[z] device labels.
434 // For all links add adjacency label
435 Link link = null;
436 LabelResourceId nodeLabelId = null;
437 LabelResourceId adjLabelId = null;
438 DeviceId deviceId = null;
439 for (Iterator<Link> iterator = linkList.iterator(); iterator.hasNext();) {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530440 link = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530441 // Add adjacency label for this link
442 adjLabelId = pceStore.getAdjLabel(link);
443 if (adjLabelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530444 log.error("Adjacency label id is null for a link {}.", link.toString());
445 return null;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530446 }
447 labelStack.add(adjLabelId);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530448
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530449 deviceId = link.dst().deviceId();
450 nodeLabelId = pceStore.getGlobalNodeLabel(deviceId);
451 if (nodeLabelId == null) {
452 log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
453 return null;
454 }
455 labelStack.add(nodeLabelId);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530456 }
457 } else {
458 log.debug("Empty link in path.");
459 return null;
460 }
461 return new DefaultLabelStack(labelStack);
462 }
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530463
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530464 //Pushes node labels to the specified device.
465 void pushGlobalNodeLabel(PcepClient pc, LabelResourceId labelId,
466 int labelForNode, PcepLabelOp type, boolean isBos) throws PcepParseException {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530467
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530468 checkNotNull(pc);
469 checkNotNull(labelId);
470 checkNotNull(type);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530471
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530472 LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>();
473 PcepFecObjectIPv4 fecObject = pc.factory().buildFecObjectIpv4()
474 .setNodeID(labelForNode)
475 .build();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530476
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530477 boolean bSFlag = false;
478 if (pc.labelDbSyncStatus() == IN_SYNC && !isBos) {
479 // Need to set sync flag in all messages till sync completes.
480 bSFlag = true;
481 }
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530482
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530483 PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530484
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530485 //Global NODE-SID as label object
486 PcepLabelObject labelObject = pc.factory().buildLabelObject()
487 .setLabel((int) labelId.labelId())
488 .build();
489
490 PcepLabelMap labelMap = new PcepLabelMap();
491 labelMap.setFecObject(fecObject);
492 labelMap.setLabelObject(labelObject);
493 labelMap.setSrpObject(srpObj);
494
495 labelUpdateList.add(pc.factory().buildPcepLabelUpdateObject()
496 .setLabelMap(labelMap)
497 .build());
498
499 PcepLabelUpdateMsg labelMsg = pc.factory().buildPcepLabelUpdateMsg()
500 .setPcLabelUpdateList(labelUpdateList)
501 .build();
502 pc.sendMessage(labelMsg);
503
504 if (isBos) {
505 // Sync is completed.
506 pc.setLabelDbSyncStatus(SYNCED);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530507 }
508 }
509
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530510 //Pushes adjacency labels to the specified device.
511 void pushAdjacencyLabel(PcepClient pc, LabelResourceId labelId, int srcPortNo,
512 int dstPortNo, PcepLabelOp type)
513 throws PcepParseException {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530514
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530515 checkNotNull(pc);
516 checkNotNull(labelId);
517 checkNotNull(type);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530518
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530519 LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>();
520 PcepFecObjectIPv4Adjacency fecAdjObject = pc.factory().buildFecIpv4Adjacency()
521 .seRemoteIPv4Address(dstPortNo)
522 .seLocalIPv4Address(srcPortNo)
523 .build();
524
525 boolean bSFlag = false;
526 if (pc.labelDbSyncStatus() == IN_SYNC) {
527 // Need to set sync flag in all messages till sync completes.
528 bSFlag = true;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530529 }
530
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530531 PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530532
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530533 //Adjacency label object
534 PcepLabelObject labelObject = pc.factory().buildLabelObject()
535 .setLabel((int) labelId.labelId())
536 .build();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530537
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530538 PcepLabelMap labelMap = new PcepLabelMap();
539 labelMap.setFecObject(fecAdjObject);
540 labelMap.setLabelObject(labelObject);
541 labelMap.setSrpObject(srpObj);
542
543 labelUpdateList.add(pc.factory().buildPcepLabelUpdateObject()
544 .setLabelMap(labelMap)
545 .build());
546
547 PcepLabelUpdateMsg labelMsg = pc.factory().buildPcepLabelUpdateMsg()
548 .setPcLabelUpdateList(labelUpdateList)
549 .build();
550 pc.sendMessage(labelMsg);
551 }
552
553 private PcepSrpObject getSrpObject(PcepClient pc, PcepLabelOp type, boolean bSFlag)
554 throws PcepParseException {
555 PcepSrpObject srpObj;
556 boolean bRFlag = false;
557
558 if (!type.equals(PcepLabelOp.ADD)) {
559 // To cleanup labels, R bit is set
560 bRFlag = true;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530561 }
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530562
563 srpObj = pc.factory().buildSrpObject()
564 .setRFlag(bRFlag)
565 .setSFlag(bSFlag)
566 .setSrpID(SrpIdGenerators.create())
567 .build();
568
569 return srpObj;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530570 }
571
572 /**
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530573 * Returns PCEP client.
Avantika-Huawei032a9872016-05-27 22:57:38 +0530574 *
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530575 * @return PCEP client
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530576 */
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530577 private PcepClient getPcepClient(DeviceId deviceId) {
578 Device device = deviceService.getDevice(deviceId);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530579
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530580 // In future projections instead of annotations will be used to fetch LSR ID.
581 String lsrId = device.annotations().value(LSR_ID);
582 PcepClient pcc = clientController.getClient(PccId.pccId(IpAddress.valueOf(lsrId)));
583 return pcc;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530584 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530585}