blob: 63eed1b3e30c720a8e7f66d2d53f4665bd9c2a82 [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;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053031import org.onosproject.core.ApplicationId;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053032import org.onosproject.incubator.net.resource.label.DefaultLabelResource;
33import org.onosproject.incubator.net.resource.label.LabelResource;
34import org.onosproject.incubator.net.resource.label.LabelResourceId;
35import org.onosproject.incubator.net.resource.label.LabelResourceAdminService;
36import org.onosproject.incubator.net.resource.label.LabelResourceService;
37import org.onosproject.incubator.net.tunnel.DefaultLabelStack;
38import org.onosproject.incubator.net.tunnel.LabelStack;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053039import org.onosproject.net.device.DeviceService;
40import org.onosproject.net.Device;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053041import org.onosproject.net.DeviceId;
42import org.onosproject.pce.pcestore.api.PceStore;
43import org.onosproject.net.Link;
44import org.onosproject.net.Path;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053045import org.onosproject.net.PortNumber;
46import org.onosproject.net.flow.DefaultTrafficSelector;
47import org.onosproject.net.flow.DefaultTrafficTreatment;
48import org.onosproject.net.flow.TrafficSelector;
49import org.onosproject.net.flow.TrafficTreatment;
50import org.onosproject.net.flowobjective.DefaultForwardingObjective;
51import org.onosproject.net.flowobjective.FlowObjectiveService;
52import org.onosproject.net.flowobjective.ForwardingObjective;
53import org.onosproject.net.flowobjective.Objective;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
56
57import com.google.common.collect.ArrayListMultimap;
58import com.google.common.collect.Multimap;
59
60/**
61 * PCE SR-BE and SR-TE functionality.
62 * SR-BE: Each node (PCC) is allocated a node-SID (label) by the PCECC. The PCECC sends PCLabelUpd to
63 * update the label map of each node to all the nodes in the domain.
64 * SR-TE: apart from node-SID, Adj-SID is used where each adjacency is allocated an Adj-SID (label) by the PCECC.
65 * The PCECC sends PCLabelUpd to update the label map of each Adj to the corresponding nodes in the domain.
66 */
67public final class PceccSrTeBeHandler {
68 private static final Logger log = LoggerFactory.getLogger(PceccSrTeBeHandler.class);
69
70 private static final String LABEL_RESOURCE_ADMIN_SERVICE_NULL = "Label Resource Admin Service cannot be null";
71 private static final String LABEL_RESOURCE_SERVICE_NULL = "Label Resource Service cannot be null";
72 private static final String PCE_STORE_NULL = "PCE Store cannot be null";
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053073 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";
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +053075 private static final String LINK_NULL = "Link cannot be null";
76 private static final String PATH_NULL = "Path cannot be null";
77 private static final String LSR_ID = "lsrId";
78 private static final int PREFIX_LENGTH = 32;
79 private static PceccSrTeBeHandler srTeHandlerInstance = null;
80 private LabelResourceAdminService labelRsrcAdminService;
81 private LabelResourceService labelRsrcService;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +053082 private FlowObjectiveService flowObjectiveService;
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +053083 private DeviceService deviceService;
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
Mahesh Poojary Huaweie2d87ff2016-05-27 12:37:46 +0530110 * @param flowObjectiveService flow objective service to push device label information
111 * @param appId application id
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 */
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530115 public void initialize(LabelResourceAdminService labelRsrcAdminService, LabelResourceService labelRsrcService,
116 FlowObjectiveService flowObjectiveService, ApplicationId appId, PceStore pceStore,
117 DeviceService deviceService) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530118 this.labelRsrcAdminService = labelRsrcAdminService;
119 this.labelRsrcService = labelRsrcService;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530120 this.flowObjectiveService = flowObjectiveService;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530121 this.pceStore = pceStore;
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530122 this.appId = appId;
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-Huaweidbdf7722016-05-21 14:20:31 +0530211 advertiseNodeLabelRule(specificDeviceId, specificLabelId,
212 IpPrefix.valueOf(IpAddress.valueOf(specificLsrId), PREFIX_LENGTH),
213 Objective.Operation.ADD, false);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530214
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530215 // Configure (node-label, lsr-id) mapping of each devices into specific device and vice versa.
216 for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) {
217 DeviceId otherDevId = element.getKey();
218 LabelResourceId otherLabelId = element.getValue();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530219
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530220 // Get lsr-id of a device
221 String otherLsrId = getLsrId(otherDevId);
222 if (otherLsrId == null) {
223 log.error("The lsr-id of device id {} is null.", otherDevId.toString());
224 releaseNodeLabel(specificDeviceId, specificLsrId);
225 return false;
226 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530227
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530228 // Push to device
229 // Push label information of specificDeviceId to otherDevId in list and vice versa.
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530230 if (!otherDevId.equals(specificDeviceId)) {
231 advertiseNodeLabelRule(otherDevId, specificLabelId,
232 IpPrefix.valueOf(IpAddress.valueOf(specificLsrId), PREFIX_LENGTH),
233 Objective.Operation.ADD, false);
234
235 advertiseNodeLabelRule(specificDeviceId, otherLabelId,
236 IpPrefix.valueOf(IpAddress.valueOf(otherLsrId), PREFIX_LENGTH),
237 Objective.Operation.ADD, false);
238 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530239 }
240
241 return true;
242 }
243
244 /**
245 * Releases assigned node label of specific device from global node label pool and pce store.
246 * and remove configured this node label from all other devices.
247 *
248 * @param specificDeviceId node label needs to be released for specific device
249 * @param specificLsrId lsrid of specific device
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530250 * @return success or failure
251 */
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530252 public boolean releaseNodeLabel(DeviceId specificDeviceId, String specificLsrId) {
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530253 checkNotNull(specificDeviceId, DEVICE_ID_NULL);
254 checkNotNull(specificLsrId, LSR_ID_NULL);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530255 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
256 checkNotNull(pceStore, PCE_STORE_NULL);
257 boolean retValue = true;
258
259 // Release node label entry of this specific device from all other devices
260 // Retrieve node label of this specific device from store
261 LabelResourceId labelId = pceStore.getGlobalNodeLabel(specificDeviceId);
262 if (labelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530263 log.error("Unable to retrieve label of a device id {} from store.", specificDeviceId.toString());
264 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530265 }
266
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530267 // Go through all devices in the pce store and remove label entry from device
268 for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) {
269 DeviceId otherDevId = element.getKey();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530270
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530271 // Remove this specific device label information from all other nodes except
272 // this specific node where connection already lost.
273 if (!specificDeviceId.equals(otherDevId)) {
274 advertiseNodeLabelRule(otherDevId, labelId,
275 IpPrefix.valueOf(IpAddress.valueOf(specificLsrId), PREFIX_LENGTH),
276 Objective.Operation.REMOVE, false);
277 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530278 }
279
280 // Release from label manager
281 Set<LabelResourceId> release = new HashSet<>();
282 release.add(labelId);
283 if (!labelRsrcService.releaseToGlobalPool(release)) {
284 log.error("Unable to release label id {} from label manager.", labelId.toString());
285 retValue = false;
286 }
287
288 // Remove from store
289 if (!pceStore.removeGlobalNodeLabel(specificDeviceId)) {
290 log.error("Unable to remove global node label id {} from store.", labelId.toString());
291 retValue = false;
292 }
293
294 return retValue;
295 }
296
297 /**
298 * Allocates adjacency label to a link from local resource pool by a specific device id.
299 *
300 * @param link between devices
301 * @return success or failure
302 */
303 public boolean allocateAdjacencyLabel(Link link) {
304 long applyNum = 1; // Single label to each link.
305 DeviceId srcDeviceId = link.src().deviceId();
306 Collection<LabelResource> labelList;
307
308 checkNotNull(link, LINK_NULL);
309 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
310 checkNotNull(pceStore, PCE_STORE_NULL);
311
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530312 // Checks whether adjacency label was already allocated
313 LabelResourceId labelId = pceStore.getAdjLabel(link);
314 if (labelId != null) {
315 log.debug("Adjacency label {} was already allocated for a link {}.", labelId.toString(), link.toString());
316 return false;
317 }
318
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530319 // Allocate adjacency label to a link from label manager.
320 // Take label from source device pool to allocate.
321 labelList = labelRsrcService.applyFromDevicePool(srcDeviceId, applyNum);
322 if (labelList.size() <= 0) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530323 log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
324 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530325 }
326
327 // Currently only one label to a device. So, no need to iterate through list
328 Iterator<LabelResource> iterator = labelList.iterator();
329 DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next();
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530330 labelId = defaultLabelResource.labelResourceId();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530331 if (labelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530332 log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString());
333 return false;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530334 }
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530335 log.debug("Allocated adjacency label {} to a link {}.", labelId.toString(), link.toString());
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530336
337 // Push adjacency label to device
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530338 installAdjLabelRule(srcDeviceId, labelId, link.src().port(), link.dst().port(), Objective.Operation.ADD);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530339
340 // Save in store
341 pceStore.addAdjLabel(link, labelId);
342 return true;
343 }
344
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530345 /**
346 * Releases unused adjacency labels from device pools.
347 *
348 * @param link between devices
349 * @return success or failure
350 */
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530351 public boolean releaseAdjacencyLabel(Link link) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530352 checkNotNull(link, LINK_NULL);
353 checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL);
354 checkNotNull(pceStore, PCE_STORE_NULL);
355 boolean retValue = true;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530356
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530357 // Retrieve link label from store
358 LabelResourceId labelId = pceStore.getAdjLabel(link);
359 if (labelId == null) {
360 log.error("Unabel to retrieve label for a link {} from store.", link.toString());
361 return false;
362 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530363
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530364 // Device
365 DeviceId srcDeviceId = link.src().deviceId();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530366
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530367 // Release adjacency label from device
368 installAdjLabelRule(srcDeviceId, labelId, link.src().port(), link.dst().port(), Objective.Operation.REMOVE);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530369
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530370 // Release link label from label manager
371 Multimap<DeviceId, LabelResource> release = ArrayListMultimap.create();
372 DefaultLabelResource defaultLabelResource = new DefaultLabelResource(srcDeviceId, labelId);
373 release.put(srcDeviceId, defaultLabelResource);
374 if (!labelRsrcService.releaseToDevicePool(release)) {
375 log.error("Unable to release label id {} from label manager.", labelId.toString());
376 retValue = false;
377 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530378
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530379 // Remove adjacency label from store
380 if (!pceStore.removeAdjLabel(link)) {
381 log.error("Unable to remove adjacency label id {} from store.", labelId.toString());
382 retValue = false;
383 }
384 return retValue;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530385 }
386
387 /**
388 * Computes label stack for a path.
389 *
390 * @param path lsp path
391 * @return label stack
392 */
393 public LabelStack computeLabelStack(Path path) {
394 checkNotNull(path, PATH_NULL);
395 // Label stack is linked list to make labels in order.
396 List<LabelResourceId> labelStack = new LinkedList<>();
397 List<Link> linkList = path.links();
398 if ((linkList != null) && (linkList.size() > 0)) {
399 // Path: [x] ---- [y] ---- [z]
400 // For other than last link, add only source[x] device label.
401 // For the last link, add both source[y] and destination[z] device labels.
402 // For all links add adjacency label
403 Link link = null;
404 LabelResourceId nodeLabelId = null;
405 LabelResourceId adjLabelId = null;
406 DeviceId deviceId = null;
407 for (Iterator<Link> iterator = linkList.iterator(); iterator.hasNext();) {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530408 link = iterator.next();
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530409 // Add source device label now
410 deviceId = link.src().deviceId();
411 nodeLabelId = pceStore.getGlobalNodeLabel(deviceId);
412 if (nodeLabelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530413 log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
414 return null;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530415 }
416 labelStack.add(nodeLabelId);
417
418 // Add adjacency label for this link
419 adjLabelId = pceStore.getAdjLabel(link);
420 if (adjLabelId == null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530421 log.error("Adjacency label id is null for a link {}.", link.toString());
422 return null;
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530423 }
424 labelStack.add(adjLabelId);
425 }
426
427 // This is the last link in path
428 // Add destination device label now.
429 if (link != null) {
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530430 deviceId = link.dst().deviceId();
431 nodeLabelId = pceStore.getGlobalNodeLabel(deviceId);
432 if (nodeLabelId == null) {
433 log.error("Unable to find node label for a device id {} in store.", deviceId.toString());
434 return null;
435 }
436 labelStack.add(nodeLabelId);
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530437 }
438 } else {
439 log.debug("Empty link in path.");
440 return null;
441 }
442 return new DefaultLabelStack(labelStack);
443 }
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530444
445 /**
446 * Install a rule for pushing unique global labels to the device.
Avantika-Huawei032a9872016-05-27 22:57:38 +0530447 *
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530448 * @param deviceId device to which flow should be pushed
449 * @param labelId label for the device
450 * @param type type of operation
451 */
452 private void installNodeLabelRule(DeviceId deviceId, LabelResourceId labelId, Objective.Operation type) {
453 checkNotNull(flowObjectiveService);
454 checkNotNull(appId);
455 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
456
457 selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue()));
458
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530459 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530460
461 ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder()
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530462 .withSelector(selectorBuilder.build()).withTreatment(treatment)
463 .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530464
465 if (type.equals(Objective.Operation.ADD)) {
466
467 flowObjectiveService.forward(deviceId, forwardingObjective.add());
468 } else {
469 flowObjectiveService.forward(deviceId, forwardingObjective.remove());
470 }
471 }
472
473 /**
474 * Install a rule for pushing node labels to the device of other nodes.
Avantika-Huawei032a9872016-05-27 22:57:38 +0530475 *
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530476 * @param deviceId device to which flow should be pushed
477 * @param labelId label for the device
478 * @param ipPrefix device for which label is pushed
479 * @param type type of operation
480 * @param bBos is this the end of sync push
481 */
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530482 public void advertiseNodeLabelRule(DeviceId deviceId, LabelResourceId labelId, IpPrefix ipPrefix,
483 Objective.Operation type, boolean bBos) {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530484 checkNotNull(flowObjectiveService);
485 checkNotNull(appId);
486 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
487
488 selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue()));
489 selectorBuilder.matchIPSrc(ipPrefix);
490
491 if (bBos) {
492 selectorBuilder.matchMplsBos(bBos);
493 }
494
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530495 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530496
497 ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder()
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530498 .withSelector(selectorBuilder.build()).withTreatment(treatment)
499 .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530500
501 if (type.equals(Objective.Operation.ADD)) {
502 flowObjectiveService.forward(deviceId, forwardingObjective.add());
503 } else {
504 flowObjectiveService.forward(deviceId, forwardingObjective.remove());
505 }
506 }
507
508 /**
Avantika-Huawei032a9872016-05-27 22:57:38 +0530509 * Install a rule for pushing Adjacency labels to the device.
510 *
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530511 * @param deviceId device to which flow should be pushed
512 * @param labelId label for the adjacency
513 * @param srcPortNum local port of the adjacency
514 * @param dstPortNum remote port of the adjacency
515 * @param type type of operation
516 */
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530517 public void installAdjLabelRule(DeviceId deviceId, LabelResourceId labelId, PortNumber srcPortNum,
518 PortNumber dstPortNum, Objective.Operation type) {
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530519 checkNotNull(flowObjectiveService);
520 checkNotNull(appId);
521 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
522
523 selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue()));
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530524 selectorBuilder.matchIPSrc(IpPrefix.valueOf((int) srcPortNum.toLong(), 32));
525 selectorBuilder.matchIPDst(IpPrefix.valueOf((int) dstPortNum.toLong(), 32));
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530526
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530527 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530528
529 ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder()
Mahesh Poojary Huawei1d17cad2016-06-02 12:53:41 +0530530 .withSelector(selectorBuilder.build()).withTreatment(treatment)
531 .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent();
Avantika-Huaweidbdf7722016-05-21 14:20:31 +0530532
533 if (type.equals(Objective.Operation.ADD)) {
534 flowObjectiveService.forward(deviceId, forwardingObjective.add());
535 } else {
536 flowObjectiveService.forward(deviceId, forwardingObjective.remove());
537 }
538 }
Mahesh Poojary Sa4df1aa2016-05-13 08:53:53 +0530539}