blob: f6fe132025d317e50121abb44da083f6cd526236 [file] [log] [blame]
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +05301/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
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 */
Avantika-Huawei9e848e82016-09-01 12:12:42 +053016package org.onosproject.pcep.controller.impl;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053017
18import static com.google.common.base.Preconditions.checkNotNull;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053019import static org.onosproject.pcep.controller.PcepSyncStatus.IN_SYNC;
20import static org.onosproject.pcep.controller.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;
Avantika-Huawei9e848e82016-09-01 12:12:42 +053045import org.onosproject.pcep.controller.PccId;
46import org.onosproject.pcep.controller.PcepClient;
47import org.onosproject.pcep.controller.PcepClientController;
48import org.onosproject.pcep.controller.SrpIdGenerators;
49import 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
111 * @param pceStore PCE label store
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530112 * @param deviceService device service
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530113 */
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530114 public void initialize(LabelResourceAdminService labelRsrcAdminService,
115 LabelResourceService labelRsrcService,
116 PcepClientController clientController,
117 PceLabelStore pceStore,
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530118 DeviceService deviceService) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530119 this.labelRsrcAdminService = labelRsrcAdminService;
120 this.labelRsrcService = labelRsrcService;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530121 this.clientController = clientController;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530122 this.pceStore = pceStore;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530123 this.deviceService = deviceService;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530124 }
125
126 /**
127 * Reserves the global label pool.
128 *
129 * @param beginLabel minimum value of global label space
130 * @param endLabel maximum value of global label space
131 * @return success or failure
132 */
133 public boolean reserveGlobalPool(long beginLabel, long endLabel) {
134 checkNotNull(labelRsrcAdminService, LABEL_RESOURCE_ADMIN_SERVICE_NULL);
135 return labelRsrcAdminService.createGlobalPool(LabelResourceId.labelResourceId(beginLabel),
136 LabelResourceId.labelResourceId(endLabel));
137 }
138
139 /**
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530140 * Retrieve lsr-id from device annotation.
141 *
142 * @param deviceId specific device id from which lsr-id needs to be retrieved
143 * @return lsr-id of a device
144 */
145 public String getLsrId(DeviceId deviceId) {
146 checkNotNull(deviceId, DEVICE_ID_NULL);
147 Device device = deviceService.getDevice(deviceId);
148 if (device == null) {
149 log.debug("Device is not available for device id {} in device service.", deviceId.toString());
150 return null;
151 }
152
153 // Retrieve lsr-id from device
154 if (device.annotations() == null) {
155 log.debug("Device {} does not have annotation.", device.toString());
156 return null;
157 }
158
159 String lsrId = device.annotations().value(LSR_ID);
160 if (lsrId == null) {
161 log.debug("The lsr-id of device {} is null.", device.toString());
162 return null;
163 }
164 return lsrId;
165 }
166
167 /**
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530168 * Allocates node label from global node label pool to specific device.
169 * Configure this device with labels and lsrid mapping of all other devices and vice versa.
170 *
171 * @param specificDeviceId node label needs to be allocated to specific device
172 * @param specificLsrId lsrid of specific device
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530173 * @return success or failure
174 */
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530175 public boolean allocateNodeLabel(DeviceId specificDeviceId, String specificLsrId) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530176 long applyNum = 1; // For each node only one node label
177 LabelResourceId specificLabelId = null;
178
179 checkNotNull(specificDeviceId, DEVICE_ID_NULL);
180 checkNotNull(specificLsrId, LSR_ID_NULL);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530181 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
182 checkNotNull(pceStore, PCE_STORE_NULL);
183
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530184 // Check whether node-label was already configured for this specific device.
185 if (pceStore.getGlobalNodeLabel(specificDeviceId) != null) {
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530186 log.debug("Node label was already configured for device {}.", specificDeviceId.toString());
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530187 return false;
188 }
189
190 // The specificDeviceId is the new device and is not there in the pce store.
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530191 // So, first generate its label and configure label and its lsr-id to it.
192 Collection<LabelResource> result = labelRsrcService.applyFromGlobalPool(applyNum);
193 if (result.size() > 0) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530194 // Only one element (label-id) to retrieve
195 Iterator<LabelResource> iterator = result.iterator();
196 DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
197 specificLabelId = defaultLabelResource.labelResourceId();
198 if (specificLabelId == null) {
199 log.error("Unable to retrieve global node label for a device id {}.", specificDeviceId.toString());
200 return false;
201 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530202 } else {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530203 log.error("Unable to allocate global node label for a device id {}.", specificDeviceId.toString());
204 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530205 }
206
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530207 // store it
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530208 pceStore.addGlobalNodeLabel(specificDeviceId, specificLabelId);
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530209
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530210 // Push its label information into specificDeviceId
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530211 PcepClient pcc = getPcepClient(specificDeviceId);
212 try {
213 pushGlobalNodeLabel(pcc,
214 specificLabelId,
215 IpAddress.valueOf(specificLsrId).getIp4Address().toInt(),
216 PcepLabelOp.ADD,
217 false);
218 } catch (PcepParseException e) {
219 log.error("Failed to push global node label for LSR {}.", specificLsrId.toString());
220 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530221
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530222 // Configure (node-label, lsr-id) mapping of each devices into specific device and vice versa.
223 for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) {
224 DeviceId otherDevId = element.getKey();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530225
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530226 // Get lsr-id of a device
227 String otherLsrId = getLsrId(otherDevId);
228 if (otherLsrId == null) {
229 log.error("The lsr-id of device id {} is null.", otherDevId.toString());
230 releaseNodeLabel(specificDeviceId, specificLsrId);
231 return false;
232 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530233
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530234 // Push to device
235 // Push label information of specificDeviceId to otherDevId in list and vice versa.
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530236 if (!otherDevId.equals(specificDeviceId)) {
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530237 try {
238 pushGlobalNodeLabel(getPcepClient(otherDevId),
239 specificLabelId,
240 IpAddress.valueOf(specificLsrId).getIp4Address().toInt(),
241 PcepLabelOp.ADD,
242 false);
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530243
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530244 pushGlobalNodeLabel(pcc, specificLabelId,
245 IpAddress.valueOf(otherLsrId).getIp4Address().toInt(),
246 PcepLabelOp.ADD,
247 false);
248 } catch (PcepParseException e) {
249 log.error("Failed to push global node label for LSR {} or LSR {}.", specificLsrId.toString(),
250 otherLsrId.toString());
251 }
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530252 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530253 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530254 return true;
255 }
256
257 /**
258 * Releases assigned node label of specific device from global node label pool and pce store.
259 * and remove configured this node label from all other devices.
260 *
261 * @param specificDeviceId node label needs to be released for specific device
262 * @param specificLsrId lsrid of specific device
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530263 * @return success or failure
264 */
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530265 public boolean releaseNodeLabel(DeviceId specificDeviceId, String specificLsrId) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530266 checkNotNull(specificDeviceId, DEVICE_ID_NULL);
267 checkNotNull(specificLsrId, LSR_ID_NULL);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530268 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
269 checkNotNull(pceStore, PCE_STORE_NULL);
270 boolean retValue = true;
271
272 // Release node label entry of this specific device from all other devices
273 // Retrieve node label of this specific device from store
274 LabelResourceId labelId = pceStore.getGlobalNodeLabel(specificDeviceId);
275 if (labelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530276 log.error("Unable to retrieve label of a device id {} from store.", specificDeviceId.toString());
277 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530278 }
279
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530280 // Go through all devices in the pce store and remove label entry from device
281 for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) {
282 DeviceId otherDevId = element.getKey();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530283
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530284 // Remove this specific device label information from all other nodes except
285 // this specific node where connection already lost.
286 if (!specificDeviceId.equals(otherDevId)) {
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530287 try {
288 pushGlobalNodeLabel(getPcepClient(otherDevId),
289 labelId,
290 IpAddress.valueOf(specificLsrId).getIp4Address().toInt(),
291 PcepLabelOp.REMOVE,
292 false);
293 } catch (PcepParseException e) {
294 log.error("Failed to push global node label for LSR {}.", specificLsrId.toString());
295 }
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530296 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530297 }
298
299 // Release from label manager
300 Set<LabelResourceId> release = new HashSet<>();
301 release.add(labelId);
302 if (!labelRsrcService.releaseToGlobalPool(release)) {
303 log.error("Unable to release label id {} from label manager.", labelId.toString());
304 retValue = false;
305 }
306
307 // Remove from store
308 if (!pceStore.removeGlobalNodeLabel(specificDeviceId)) {
309 log.error("Unable to remove global node label id {} from store.", labelId.toString());
310 retValue = false;
311 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530312 return retValue;
313 }
314
315 /**
316 * Allocates adjacency label to a link from local resource pool by a specific device id.
317 *
318 * @param link between devices
319 * @return success or failure
320 */
321 public boolean allocateAdjacencyLabel(Link link) {
322 long applyNum = 1; // Single label to each link.
323 DeviceId srcDeviceId = link.src().deviceId();
324 Collection<LabelResource> labelList;
325
326 checkNotNull(link, LINK_NULL);
327 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
328 checkNotNull(pceStore, PCE_STORE_NULL);
329
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530330 // Checks whether adjacency label was already allocated
331 LabelResourceId labelId = pceStore.getAdjLabel(link);
332 if (labelId != null) {
333 log.debug("Adjacency label {} was already allocated for a link {}.", labelId.toString(), link.toString());
334 return false;
335 }
336
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530337 // Allocate adjacency label to a link from label manager.
338 // Take label from source device pool to allocate.
339 labelList = labelRsrcService.applyFromDevicePool(srcDeviceId, applyNum);
340 if (labelList.size() <= 0) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530341 log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
342 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530343 }
344
345 // Currently only one label to a device. So, no need to iterate through list
346 Iterator<LabelResource> iterator = labelList.iterator();
347 DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530348 labelId = defaultLabelResource.labelResourceId();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530349 if (labelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530350 log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
351 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530352 }
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530353 log.debug("Allocated adjacency label {} to a link {}.", labelId.toString(), link.toString());
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530354
355 // Push adjacency label to device
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530356 try {
357 pushAdjacencyLabel(getPcepClient(srcDeviceId), labelId, (int) link.src().port().toLong(),
358 (int) link.dst().port().toLong(), PcepLabelOp.ADD);
359 } catch (PcepParseException e) {
360 log.error("Failed to push adjacency label for link {}-{}.", (int) link.src().port().toLong(),
361 (int) link.dst().port().toLong());
362 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530363
364 // Save in store
365 pceStore.addAdjLabel(link, labelId);
366 return true;
367 }
368
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530369 /**
370 * Releases unused adjacency labels from device pools.
371 *
372 * @param link between devices
373 * @return success or failure
374 */
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530375 public boolean releaseAdjacencyLabel(Link link) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530376 checkNotNull(link, LINK_NULL);
377 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
378 checkNotNull(pceStore, PCE_STORE_NULL);
379 boolean retValue = true;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530380
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530381 // Retrieve link label from store
382 LabelResourceId labelId = pceStore.getAdjLabel(link);
383 if (labelId == null) {
384 log.error("Unabel to retrieve label for a link {} from store.", link.toString());
385 return false;
386 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530387
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530388 // Device
389 DeviceId srcDeviceId = link.src().deviceId();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530390
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530391 // Release adjacency label from device
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530392 try {
393 pushAdjacencyLabel(getPcepClient(srcDeviceId), labelId, (int) link.src().port().toLong(),
394 (int) link.dst().port().toLong(), PcepLabelOp.REMOVE);
395 } catch (PcepParseException e) {
396 log.error("Failed to push adjacency label for link {}-{}.", (int) link.src().port().toLong(),
397 (int) link.dst().port().toLong());
398 }
399
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530400
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530401 // Release link label from label manager
402 Multimap<DeviceId, LabelResource> release = ArrayListMultimap.create();
403 DefaultLabelResource defaultLabelResource = new DefaultLabelResource(srcDeviceId, labelId);
404 release.put(srcDeviceId, defaultLabelResource);
405 if (!labelRsrcService.releaseToDevicePool(release)) {
406 log.error("Unable to release label id {} from label manager.", labelId.toString());
407 retValue = false;
408 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530409
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530410 // Remove adjacency label from store
411 if (!pceStore.removeAdjLabel(link)) {
412 log.error("Unable to remove adjacency label id {} from store.", labelId.toString());
413 retValue = false;
414 }
415 return retValue;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530416 }
417
418 /**
419 * Computes label stack for a path.
420 *
421 * @param path lsp path
422 * @return label stack
423 */
424 public LabelStack computeLabelStack(Path path) {
425 checkNotNull(path, PATH_NULL);
426 // Label stack is linked list to make labels in order.
427 List<LabelResourceId> labelStack = new LinkedList<>();
428 List<Link> linkList = path.links();
429 if ((linkList != null) && (linkList.size() > 0)) {
430 // Path: [x] ---- [y] ---- [z]
431 // For other than last link, add only source[x] device label.
432 // For the last link, add both source[y] and destination[z] device labels.
433 // For all links add adjacency label
434 Link link = null;
435 LabelResourceId nodeLabelId = null;
436 LabelResourceId adjLabelId = null;
437 DeviceId deviceId = null;
438 for (Iterator<Link> iterator = linkList.iterator(); iterator.hasNext();) {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530439 link = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530440 // Add adjacency label for this link
441 adjLabelId = pceStore.getAdjLabel(link);
442 if (adjLabelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530443 log.error("Adjacency label id is null for a link {}.", link.toString());
444 return null;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530445 }
446 labelStack.add(adjLabelId);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530447
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530448 deviceId = link.dst().deviceId();
449 nodeLabelId = pceStore.getGlobalNodeLabel(deviceId);
450 if (nodeLabelId == null) {
451 log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
452 return null;
453 }
454 labelStack.add(nodeLabelId);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530455 }
456 } else {
457 log.debug("Empty link in path.");
458 return null;
459 }
460 return new DefaultLabelStack(labelStack);
461 }
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530462
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530463 //Pushes node labels to the specified device.
464 void pushGlobalNodeLabel(PcepClient pc, LabelResourceId labelId,
465 int labelForNode, PcepLabelOp type, boolean isBos) throws PcepParseException {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530466
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530467 checkNotNull(pc);
468 checkNotNull(labelId);
469 checkNotNull(type);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530470
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530471 LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>();
472 PcepFecObjectIPv4 fecObject = pc.factory().buildFecObjectIpv4()
473 .setNodeID(labelForNode)
474 .build();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530475
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530476 boolean bSFlag = false;
477 if (pc.labelDbSyncStatus() == IN_SYNC && !isBos) {
478 // Need to set sync flag in all messages till sync completes.
479 bSFlag = true;
480 }
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530481
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530482 PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530483
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530484 //Global NODE-SID as label object
485 PcepLabelObject labelObject = pc.factory().buildLabelObject()
486 .setLabel((int) labelId.labelId())
487 .build();
488
489 PcepLabelMap labelMap = new PcepLabelMap();
490 labelMap.setFecObject(fecObject);
491 labelMap.setLabelObject(labelObject);
492 labelMap.setSrpObject(srpObj);
493
494 labelUpdateList.add(pc.factory().buildPcepLabelUpdateObject()
495 .setLabelMap(labelMap)
496 .build());
497
498 PcepLabelUpdateMsg labelMsg = pc.factory().buildPcepLabelUpdateMsg()
499 .setPcLabelUpdateList(labelUpdateList)
500 .build();
501 pc.sendMessage(labelMsg);
502
503 if (isBos) {
504 // Sync is completed.
505 pc.setLabelDbSyncStatus(SYNCED);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530506 }
507 }
508
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530509 //Pushes adjacency labels to the specified device.
510 void pushAdjacencyLabel(PcepClient pc, LabelResourceId labelId, int srcPortNo,
511 int dstPortNo, PcepLabelOp type)
512 throws PcepParseException {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530513
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530514 checkNotNull(pc);
515 checkNotNull(labelId);
516 checkNotNull(type);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530517
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530518 LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>();
519 PcepFecObjectIPv4Adjacency fecAdjObject = pc.factory().buildFecIpv4Adjacency()
520 .seRemoteIPv4Address(dstPortNo)
521 .seLocalIPv4Address(srcPortNo)
522 .build();
523
524 boolean bSFlag = false;
525 if (pc.labelDbSyncStatus() == IN_SYNC) {
526 // Need to set sync flag in all messages till sync completes.
527 bSFlag = true;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530528 }
529
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530530 PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530531
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530532 //Adjacency label object
533 PcepLabelObject labelObject = pc.factory().buildLabelObject()
534 .setLabel((int) labelId.labelId())
535 .build();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530536
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530537 PcepLabelMap labelMap = new PcepLabelMap();
538 labelMap.setFecObject(fecAdjObject);
539 labelMap.setLabelObject(labelObject);
540 labelMap.setSrpObject(srpObj);
541
542 labelUpdateList.add(pc.factory().buildPcepLabelUpdateObject()
543 .setLabelMap(labelMap)
544 .build());
545
546 PcepLabelUpdateMsg labelMsg = pc.factory().buildPcepLabelUpdateMsg()
547 .setPcLabelUpdateList(labelUpdateList)
548 .build();
549 pc.sendMessage(labelMsg);
550 }
551
552 private PcepSrpObject getSrpObject(PcepClient pc, PcepLabelOp type, boolean bSFlag)
553 throws PcepParseException {
554 PcepSrpObject srpObj;
555 boolean bRFlag = false;
556
557 if (!type.equals(PcepLabelOp.ADD)) {
558 // To cleanup labels, R bit is set
559 bRFlag = true;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530560 }
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530561
562 srpObj = pc.factory().buildSrpObject()
563 .setRFlag(bRFlag)
564 .setSFlag(bSFlag)
565 .setSrpID(SrpIdGenerators.create())
566 .build();
567
568 return srpObj;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530569 }
570
571 /**
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530572 * Returns PCEP client.
Avantika-Huawei032a9872016-05-27 22:57:38 +0530573 *
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530574 * @return PCEP client
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530575 */
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530576 private PcepClient getPcepClient(DeviceId deviceId) {
577 Device device = deviceService.getDevice(deviceId);
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530578
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530579 // In future projections instead of annotations will be used to fetch LSR ID.
580 String lsrId = device.annotations().value(LSR_ID);
581 PcepClient pcc = clientController.getClient(PccId.pccId(IpAddress.valueOf(lsrId)));
582 return pcc;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530583 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530584}