blob: 51fd6dcffb2caf68490584bd12f4d781878b96b4 [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 */
16package org.onosproject.pce.pceservice;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Collection;
21import java.util.Iterator;
22import java.util.HashSet;
23import java.util.List;
24import java.util.LinkedList;
25import java.util.Map;
26import java.util.Set;
27
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053028import org.onlab.packet.IpAddress;
29import org.onlab.packet.IpPrefix;
30import org.onlab.packet.MplsLabel;
31import org.onlab.packet.TpPort;
32import org.onosproject.core.ApplicationId;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053033import org.onosproject.incubator.net.resource.label.DefaultLabelResource;
34import org.onosproject.incubator.net.resource.label.LabelResource;
35import org.onosproject.incubator.net.resource.label.LabelResourceId;
36import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
37import org.onosproject.incubator.net.resource.label.LabelResourceService;
38import org.onosproject.incubator.net.tunnel.DefaultLabelStack;
39import org.onosproject.incubator.net.tunnel.LabelStack;
40import org.onosproject.net.DeviceId;
41import org.onosproject.pce.pcestore.api.PceStore;
42import org.onosproject.net.Link;
43import org.onosproject.net.Path;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053044import org.onosproject.net.PortNumber;
45import org.onosproject.net.flow.DefaultTrafficSelector;
46import org.onosproject.net.flow.DefaultTrafficTreatment;
47import org.onosproject.net.flow.TrafficSelector;
48import org.onosproject.net.flow.TrafficTreatment;
49import org.onosproject.net.flowobjective.DefaultForwardingObjective;
50import org.onosproject.net.flowobjective.FlowObjectiveService;
51import org.onosproject.net.flowobjective.ForwardingObjective;
52import org.onosproject.net.flowobjective.Objective;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
55
56import com.google.common.collect.ArrayListMultimap;
57import com.google.common.collect.Multimap;
58
59/**
60 * PCE SR-BE and SR-TE functionality.
61 * SR-BE: Each node (PCC) is allocated a node-SID (label) by the PCECC. The PCECC sends PCLabelUpd to
62 * update the label map of each node to all the nodes in the domain.
63 * SR-TE: apart from node-SID, Adj-SID is used where each adjacency is allocated an Adj-SID (label) by the PCECC.
64 * The PCECC sends PCLabelUpd to update the label map of each Adj to the corresponding nodes in the domain.
65 */
66public final class PceccSrTeBeHandler {
67 private static final Logger log = LoggerFactory.getLogger(PceccSrTeBeHandler.class);
68
69 private static final String LABEL_RESOURCE_ADMIN_SERVICE_NULL = "Label Resource Admin Service cannot be null";
70 private static final String LABEL_RESOURCE_SERVICE_NULL = "Label Resource Service cannot be null";
71 private static final String PCE_STORE_NULL = "PCE Store cannot be null";
72 private static final String DEVICE_SERVICE_NULL = "Device Service cannot be null";
73 private static final String DEVICE_ID_NULL = "Device-Id cannot be null";
74 private static final String LSR_ID_NULL = "LSR-Id cannot be null";
75 private static final String DEVICE_ID_LSR_ID_MAP_NULL = "Device-Id and LSR-Id map cannot be null";
76 private static final String LINK_NULL = "Link cannot be null";
77 private static final String PATH_NULL = "Path cannot be null";
78 private static final String LSR_ID = "lsrId";
79 private static final int PREFIX_LENGTH = 32;
80 private static PceccSrTeBeHandler srTeHandlerInstance = null;
81 private LabelResourceAdminService labelRsrcAdminService;
82 private LabelResourceService labelRsrcService;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053083 private FlowObjectiveService flowObjectiveService;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053084 private PceStore pceStore;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053085 private ApplicationId appId;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053086
87 /**
88 * Initializes default values.
89 */
90 private PceccSrTeBeHandler() {
91 }
92
93 /**
94 * Returns single instance of this class.
95 *
96 * @return this class single instance
97 */
98 public static PceccSrTeBeHandler getInstance() {
99 if (srTeHandlerInstance == null) {
100 srTeHandlerInstance = new PceccSrTeBeHandler();
101 }
102 return srTeHandlerInstance;
103 }
104
105 /**
106 * Initialization of label manager interfaces and pce store.
107 *
108 * @param labelRsrcAdminService label resource admin service
109 * @param labelRsrcService label resource service
110 * @param pceStore PCE label store
111 */
112 public void initialize(LabelResourceAdminService labelRsrcAdminService,
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530113 LabelResourceService labelRsrcService,
114 FlowObjectiveService flowObjectiveService,
115 ApplicationId appId, PceStore pceStore) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530116 this.labelRsrcAdminService = labelRsrcAdminService;
117 this.labelRsrcService = labelRsrcService;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530118 this.flowObjectiveService = flowObjectiveService;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530119 this.pceStore = pceStore;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530120 this.appId = appId;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530121 }
122
123 /**
124 * Reserves the global label pool.
125 *
126 * @param beginLabel minimum value of global label space
127 * @param endLabel maximum value of global label space
128 * @return success or failure
129 */
130 public boolean reserveGlobalPool(long beginLabel, long endLabel) {
131 checkNotNull(labelRsrcAdminService, LABEL_RESOURCE_ADMIN_SERVICE_NULL);
132 return labelRsrcAdminService.createGlobalPool(LabelResourceId.labelResourceId(beginLabel),
133 LabelResourceId.labelResourceId(endLabel));
134 }
135
136 /**
137 * Allocates node label from global node label pool to specific device.
138 * Configure this device with labels and lsrid mapping of all other devices and vice versa.
139 *
140 * @param specificDeviceId node label needs to be allocated to specific device
141 * @param specificLsrId lsrid of specific device
142 * @param deviceIdLsrIdMap deviceid and lsrid mapping
143 * @return success or failure
144 */
145 public boolean allocateNodeLabel(DeviceId specificDeviceId, String specificLsrId,
146 Map<DeviceId, String> deviceIdLsrIdMap) {
147 long applyNum = 1; // For each node only one node label
148 LabelResourceId specificLabelId = null;
149
150 checkNotNull(specificDeviceId, DEVICE_ID_NULL);
151 checkNotNull(specificLsrId, LSR_ID_NULL);
152 checkNotNull(deviceIdLsrIdMap, DEVICE_ID_LSR_ID_MAP_NULL);
153 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
154 checkNotNull(pceStore, PCE_STORE_NULL);
155
156 // The specificDeviceId is the new device and is not there in the deviceIdLsrIdMap.
157 // So, first generate its label and configure label and its lsr-id to it.
158 Collection<LabelResource> result = labelRsrcService.applyFromGlobalPool(applyNum);
159 if (result.size() > 0) {
160 // Only one element (label-id) to retrieve
161 Iterator<LabelResource> iterator = result.iterator();
162 DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
163 specificLabelId = defaultLabelResource.labelResourceId();
164 if (specificLabelId == null) {
165 log.error("Unable to retrieve global node label for a device id {}.", specificDeviceId.toString());
166 return false;
167 }
168 } else {
169 log.error("Unable to allocate global node label for a device id {}.", specificDeviceId.toString());
170 return false;
171 }
172
173 pceStore.addGlobalNodeLabel(specificDeviceId, specificLabelId);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530174 // Push its label information into specificDeviceId
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530175 advertiseNodeLabelRule(specificDeviceId, specificLabelId,
176 IpPrefix.valueOf(IpAddress.valueOf(specificLsrId), PREFIX_LENGTH),
177 Objective.Operation.ADD, false);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530178
179 // Configure (node-label, lsr-id) mapping of each devices in list to specific device and vice versa.
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530180 for (Map.Entry<DeviceId, String> element:deviceIdLsrIdMap.entrySet()) {
181 DeviceId otherDevId = element.getKey();
182 String otherLsrId = element.getValue();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530183 if (otherLsrId == null) {
184 log.error("The lsr-id of device id {} is null.", otherDevId.toString());
185 releaseNodeLabel(specificDeviceId, specificLsrId, deviceIdLsrIdMap);
186 return false;
187 }
188
189 // Label for other device in list should be already allocated.
190 LabelResourceId otherLabelId = pceStore.getGlobalNodeLabel(otherDevId);
191 if (otherLabelId == null) {
192 log.error("Unable to find global node label in store for a device id {}.", otherDevId.toString());
193 releaseNodeLabel(specificDeviceId, specificLsrId, deviceIdLsrIdMap);
194 return false;
195 }
196
197 // Push to device
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530198 // Push label information of specificDeviceId to otherDevId in list and vice versa.
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530199 advertiseNodeLabelRule(otherDevId, specificLabelId, IpPrefix.valueOf(IpAddress.valueOf(specificLsrId),
200 PREFIX_LENGTH), Objective.Operation.ADD, false);
201 advertiseNodeLabelRule(specificDeviceId, otherLabelId, IpPrefix.valueOf(IpAddress.valueOf(otherLsrId),
202 PREFIX_LENGTH), Objective.Operation.ADD, false);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530203 }
204
205 return true;
206 }
207
208 /**
209 * Releases assigned node label of specific device from global node label pool and pce store.
210 * and remove configured this node label from all other devices.
211 *
212 * @param specificDeviceId node label needs to be released for specific device
213 * @param specificLsrId lsrid of specific device
214 * @param deviceIdLsrIdMap deviceid and lsrid mapping
215 * @return success or failure
216 */
217 public boolean releaseNodeLabel(DeviceId specificDeviceId, String specificLsrId,
218 Map<DeviceId, String> deviceIdLsrIdMap) {
219 checkNotNull(specificDeviceId, DEVICE_ID_NULL);
220 checkNotNull(specificLsrId, LSR_ID_NULL);
221 checkNotNull(deviceIdLsrIdMap, DEVICE_ID_LSR_ID_MAP_NULL);
222 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
223 checkNotNull(pceStore, PCE_STORE_NULL);
224 boolean retValue = true;
225
226 // Release node label entry of this specific device from all other devices
227 // Retrieve node label of this specific device from store
228 LabelResourceId labelId = pceStore.getGlobalNodeLabel(specificDeviceId);
229 if (labelId == null) {
230 log.error("Unable to retrieve label of a device id {} from store.", specificDeviceId.toString());
231 return false;
232 }
233
234 // Go through all devices in the map and remove label entry
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530235 for (Map.Entry<DeviceId, String> element:deviceIdLsrIdMap.entrySet()) {
236 DeviceId otherDevId = element.getKey();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530237
238 // Remove this specific device label information from all other nodes except
239 // this specific node where connection already lost.
240 if (!specificDeviceId.equals(otherDevId)) {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530241 advertiseNodeLabelRule(otherDevId, labelId, IpPrefix.valueOf(IpAddress.valueOf(specificLsrId),
242 PREFIX_LENGTH), Objective.Operation.REMOVE, false);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530243 }
244 }
245
246 // Release from label manager
247 Set<LabelResourceId> release = new HashSet<>();
248 release.add(labelId);
249 if (!labelRsrcService.releaseToGlobalPool(release)) {
250 log.error("Unable to release label id {} from label manager.", labelId.toString());
251 retValue = false;
252 }
253
254 // Remove from store
255 if (!pceStore.removeGlobalNodeLabel(specificDeviceId)) {
256 log.error("Unable to remove global node label id {} from store.", labelId.toString());
257 retValue = false;
258 }
259
260 return retValue;
261 }
262
263 /**
264 * Allocates adjacency label to a link from local resource pool by a specific device id.
265 *
266 * @param link between devices
267 * @return success or failure
268 */
269 public boolean allocateAdjacencyLabel(Link link) {
270 long applyNum = 1; // Single label to each link.
271 DeviceId srcDeviceId = link.src().deviceId();
272 Collection<LabelResource> labelList;
273
274 checkNotNull(link, LINK_NULL);
275 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
276 checkNotNull(pceStore, PCE_STORE_NULL);
277
278 // Allocate adjacency label to a link from label manager.
279 // Take label from source device pool to allocate.
280 labelList = labelRsrcService.applyFromDevicePool(srcDeviceId, applyNum);
281 if (labelList.size() <= 0) {
282 log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
283 return false;
284 }
285
286 // Currently only one label to a device. So, no need to iterate through list
287 Iterator<LabelResource> iterator = labelList.iterator();
288 DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
289 LabelResourceId labelId = defaultLabelResource.labelResourceId();
290 if (labelId == null) {
291 log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
292 return false;
293 }
294 log.debug("Allocated adjacency label {} to a link {}.", labelId.toString(),
295 link.toString());
296
297 // Push adjacency label to device
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530298 installAdjLabelRule(srcDeviceId, labelId, link.src().port(), link.dst().port(), Objective.Operation.ADD);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530299
300 // Save in store
301 pceStore.addAdjLabel(link, labelId);
302 return true;
303 }
304
305 /**
306 * Releases unused adjacency labels from device pools.
307 *
308 * @param link between devices
309 * @return success or failure
310 */
311 public boolean releaseAdjacencyLabel(Link link) {
312 checkNotNull(link, LINK_NULL);
313 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
314 checkNotNull(pceStore, PCE_STORE_NULL);
315 boolean retValue = true;
316
317 // Retrieve link label from store
318 LabelResourceId labelId = pceStore.getAdjLabel(link);
319 if (labelId == null) {
320 log.error("Unabel to retrieve label for a link {} from store.", link.toString());
321 return false;
322 }
323
324 // Device
325 DeviceId srcDeviceId = link.src().deviceId();
326
327 // Release adjacency label from device
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530328 installAdjLabelRule(srcDeviceId, labelId, link.src().port(), link.dst().port(), Objective.Operation.REMOVE);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530329
330 // Release link label from label manager
331 Multimap<DeviceId, LabelResource> release = ArrayListMultimap.create();
332 DefaultLabelResource defaultLabelResource = new DefaultLabelResource(srcDeviceId, labelId);
333 release.put(srcDeviceId, defaultLabelResource);
334 if (!labelRsrcService.releaseToDevicePool(release)) {
335 log.error("Unable to release label id {} from label manager.", labelId.toString());
336 retValue = false;
337 }
338
339 // Remove adjacency label from store
340 if (!pceStore.removeAdjLabel(link)) {
341 log.error("Unable to remove adjacency label id {} from store.", labelId.toString());
342 retValue = false;
343 }
344 return retValue;
345 }
346
347 /**
348 * Computes label stack for a path.
349 *
350 * @param path lsp path
351 * @return label stack
352 */
353 public LabelStack computeLabelStack(Path path) {
354 checkNotNull(path, PATH_NULL);
355 // Label stack is linked list to make labels in order.
356 List<LabelResourceId> labelStack = new LinkedList<>();
357 List<Link> linkList = path.links();
358 if ((linkList != null) && (linkList.size() > 0)) {
359 // Path: [x] ---- [y] ---- [z]
360 // For other than last link, add only source[x] device label.
361 // For the last link, add both source[y] and destination[z] device labels.
362 // For all links add adjacency label
363 Link link = null;
364 LabelResourceId nodeLabelId = null;
365 LabelResourceId adjLabelId = null;
366 DeviceId deviceId = null;
367 for (Iterator<Link> iterator = linkList.iterator(); iterator.hasNext();) {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530368 link = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530369 // Add source device label now
370 deviceId = link.src().deviceId();
371 nodeLabelId = pceStore.getGlobalNodeLabel(deviceId);
372 if (nodeLabelId == null) {
373 log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
374 return null;
375 }
376 labelStack.add(nodeLabelId);
377
378 // Add adjacency label for this link
379 adjLabelId = pceStore.getAdjLabel(link);
380 if (adjLabelId == null) {
381 log.error("Adjacency label id is null for a link {}.", link.toString());
382 return null;
383 }
384 labelStack.add(adjLabelId);
385 }
386
387 // This is the last link in path
388 // Add destination device label now.
389 if (link != null) {
390 deviceId = link.dst().deviceId();
391 nodeLabelId = pceStore.getGlobalNodeLabel(deviceId);
392 if (nodeLabelId == null) {
393 log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
394 return null;
395 }
396 labelStack.add(nodeLabelId);
397 }
398 } else {
399 log.debug("Empty link in path.");
400 return null;
401 }
402 return new DefaultLabelStack(labelStack);
403 }
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530404
405 /**
406 * Install a rule for pushing unique global labels to the device.
407 * @param deviceId device to which flow should be pushed
408 * @param labelId label for the device
409 * @param type type of operation
410 */
411 private void installNodeLabelRule(DeviceId deviceId, LabelResourceId labelId, Objective.Operation type) {
412 checkNotNull(flowObjectiveService);
413 checkNotNull(appId);
414 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
415
416 selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue()));
417
418 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
419 .build();
420
421 ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder()
422 .withSelector(selectorBuilder.build())
423 .withTreatment(treatment)
424 .withFlag(ForwardingObjective.Flag.VERSATILE)
425 .fromApp(appId)
426 .makePermanent();
427
428 if (type.equals(Objective.Operation.ADD)) {
429
430 flowObjectiveService.forward(deviceId, forwardingObjective.add());
431 } else {
432 flowObjectiveService.forward(deviceId, forwardingObjective.remove());
433 }
434 }
435
436 /**
437 * Install a rule for pushing node labels to the device of other nodes.
438 * @param deviceId device to which flow should be pushed
439 * @param labelId label for the device
440 * @param ipPrefix device for which label is pushed
441 * @param type type of operation
442 * @param bBos is this the end of sync push
443 */
444 public void advertiseNodeLabelRule(DeviceId deviceId, LabelResourceId labelId,
445 IpPrefix ipPrefix, Objective.Operation type, boolean bBos) {
446 checkNotNull(flowObjectiveService);
447 checkNotNull(appId);
448 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
449
450 selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue()));
451 selectorBuilder.matchIPSrc(ipPrefix);
452
453 if (bBos) {
454 selectorBuilder.matchMplsBos(bBos);
455 }
456
457 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
458 .build();
459
460 ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder()
461 .withSelector(selectorBuilder.build())
462 .withTreatment(treatment)
463 .withFlag(ForwardingObjective.Flag.VERSATILE)
464 .fromApp(appId)
465 .makePermanent();
466
467 if (type.equals(Objective.Operation.ADD)) {
468 flowObjectiveService.forward(deviceId, forwardingObjective.add());
469 } else {
470 flowObjectiveService.forward(deviceId, forwardingObjective.remove());
471 }
472 }
473
474 /**
475 * Install a rule for pushing Adjacency labels to the device.
476 * @param deviceId device to which flow should be pushed
477 * @param labelId label for the adjacency
478 * @param srcPortNum local port of the adjacency
479 * @param dstPortNum remote port of the adjacency
480 * @param type type of operation
481 */
482 public void installAdjLabelRule(DeviceId deviceId, LabelResourceId labelId,
483 PortNumber srcPortNum, PortNumber dstPortNum,
484 Objective.Operation type) {
485 checkNotNull(flowObjectiveService);
486 checkNotNull(appId);
487 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
488
489 selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue()));
490 selectorBuilder.matchTcpSrc(TpPort.tpPort((int) srcPortNum.toLong()));
491 selectorBuilder.matchTcpDst(TpPort.tpPort((int) dstPortNum.toLong()));
492
493 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
494 .build();
495
496 ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder()
497 .withSelector(selectorBuilder.build())
498 .withTreatment(treatment)
499 .withFlag(ForwardingObjective.Flag.VERSATILE)
500 .fromApp(appId)
501 .makePermanent();
502
503 if (type.equals(Objective.Operation.ADD)) {
504 flowObjectiveService.forward(deviceId, forwardingObjective.add());
505 } else {
506 flowObjectiveService.forward(deviceId, forwardingObjective.remove());
507 }
508 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530509}