Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.pce.pceservice; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
| 20 | import java.util.Collection; |
| 21 | import java.util.Iterator; |
| 22 | import java.util.HashSet; |
| 23 | import java.util.List; |
| 24 | import java.util.LinkedList; |
| 25 | import java.util.Map; |
| 26 | import java.util.Set; |
| 27 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 28 | import org.onlab.packet.IpAddress; |
| 29 | import org.onlab.packet.IpPrefix; |
| 30 | import org.onlab.packet.MplsLabel; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 32 | import org.onosproject.incubator.net.resource.label.DefaultLabelResource; |
| 33 | import org.onosproject.incubator.net.resource.label.LabelResource; |
| 34 | import org.onosproject.incubator.net.resource.label.LabelResourceId; |
| 35 | import org.onosproject.incubator.net.resource.label.LabelResourceAdminService; |
| 36 | import org.onosproject.incubator.net.resource.label.LabelResourceService; |
| 37 | import org.onosproject.incubator.net.tunnel.DefaultLabelStack; |
| 38 | import org.onosproject.incubator.net.tunnel.LabelStack; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceService; |
| 40 | import org.onosproject.net.Device; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 41 | import org.onosproject.net.DeviceId; |
| 42 | import org.onosproject.pce.pcestore.api.PceStore; |
| 43 | import org.onosproject.net.Link; |
| 44 | import org.onosproject.net.Path; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 45 | import org.onosproject.net.PortNumber; |
| 46 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 47 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 48 | import org.onosproject.net.flow.TrafficSelector; |
| 49 | import org.onosproject.net.flow.TrafficTreatment; |
| 50 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 51 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 52 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 53 | import org.onosproject.net.flowobjective.Objective; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
| 56 | |
| 57 | import com.google.common.collect.ArrayListMultimap; |
| 58 | import 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 | */ |
| 67 | public 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 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"; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 75 | 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-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 82 | private FlowObjectiveService flowObjectiveService; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 83 | private DeviceService deviceService; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 84 | private PceStore pceStore; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 85 | private ApplicationId appId; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 86 | |
| 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 Huawei | e2d87ff | 2016-05-27 12:37:46 +0530 | [diff] [blame] | 110 | * @param flowObjectiveService flow objective service to push device label information |
| 111 | * @param appId application id |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 112 | * @param pceStore PCE label store |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 113 | * @param deviceService device service |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 114 | */ |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 115 | public void initialize(LabelResourceAdminService labelRsrcAdminService, LabelResourceService labelRsrcService, |
| 116 | FlowObjectiveService flowObjectiveService, ApplicationId appId, PceStore pceStore, |
| 117 | DeviceService deviceService) { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 118 | this.labelRsrcAdminService = labelRsrcAdminService; |
| 119 | this.labelRsrcService = labelRsrcService; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 120 | this.flowObjectiveService = flowObjectiveService; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 121 | this.pceStore = pceStore; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 122 | this.appId = appId; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 123 | this.deviceService = deviceService; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 124 | } |
| 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 140 | * 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 168 | * 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 173 | * @return success or failure |
| 174 | */ |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 175 | public boolean allocateNodeLabel(DeviceId specificDeviceId, String specificLsrId) { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 176 | 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 181 | checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL); |
| 182 | checkNotNull(pceStore, PCE_STORE_NULL); |
| 183 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 184 | // Check whether node-label was already configured for this specific device. |
| 185 | if (pceStore.getGlobalNodeLabel(specificDeviceId) != null) { |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 186 | log.debug("Node label was already configured for device {}.", specificDeviceId.toString()); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 187 | return false; |
| 188 | } |
| 189 | |
| 190 | // The specificDeviceId is the new device and is not there in the pce store. |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 191 | // 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 194 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 202 | } else { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 203 | log.error("Unable to allocate global node label for a device id {}.", specificDeviceId.toString()); |
| 204 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 205 | } |
| 206 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 207 | // store it |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 208 | pceStore.addGlobalNodeLabel(specificDeviceId, specificLabelId); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 209 | |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 210 | // Push its label information into specificDeviceId |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 211 | advertiseNodeLabelRule(specificDeviceId, specificLabelId, |
| 212 | IpPrefix.valueOf(IpAddress.valueOf(specificLsrId), PREFIX_LENGTH), |
| 213 | Objective.Operation.ADD, false); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 214 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 215 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 219 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 220 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 227 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 228 | // Push to device |
| 229 | // Push label information of specificDeviceId to otherDevId in list and vice versa. |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 230 | 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 239 | } |
| 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 250 | * @return success or failure |
| 251 | */ |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 252 | public boolean releaseNodeLabel(DeviceId specificDeviceId, String specificLsrId) { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 253 | checkNotNull(specificDeviceId, DEVICE_ID_NULL); |
| 254 | checkNotNull(specificLsrId, LSR_ID_NULL); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 255 | 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 263 | log.error("Unable to retrieve label of a device id {} from store.", specificDeviceId.toString()); |
| 264 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 265 | } |
| 266 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 267 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 270 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 271 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 278 | } |
| 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 312 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 319 | // 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 323 | log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString()); |
| 324 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 325 | } |
| 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 330 | labelId = defaultLabelResource.labelResourceId(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 331 | if (labelId == null) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 332 | log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString()); |
| 333 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 334 | } |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 335 | log.debug("Allocated adjacency label {} to a link {}.", labelId.toString(), link.toString()); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 336 | |
| 337 | // Push adjacency label to device |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 338 | installAdjLabelRule(srcDeviceId, labelId, link.src().port(), link.dst().port(), Objective.Operation.ADD); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 339 | |
| 340 | // Save in store |
| 341 | pceStore.addAdjLabel(link, labelId); |
| 342 | return true; |
| 343 | } |
| 344 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 345 | /** |
| 346 | * Releases unused adjacency labels from device pools. |
| 347 | * |
| 348 | * @param link between devices |
| 349 | * @return success or failure |
| 350 | */ |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 351 | public boolean releaseAdjacencyLabel(Link link) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 352 | checkNotNull(link, LINK_NULL); |
| 353 | checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL); |
| 354 | checkNotNull(pceStore, PCE_STORE_NULL); |
| 355 | boolean retValue = true; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 356 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 357 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 363 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 364 | // Device |
| 365 | DeviceId srcDeviceId = link.src().deviceId(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 366 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 367 | // Release adjacency label from device |
| 368 | installAdjLabelRule(srcDeviceId, labelId, link.src().port(), link.dst().port(), Objective.Operation.REMOVE); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 369 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 370 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 378 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 379 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 385 | } |
| 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-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 408 | link = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 409 | // Add adjacency label for this link |
| 410 | adjLabelId = pceStore.getAdjLabel(link); |
| 411 | if (adjLabelId == null) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 412 | log.error("Adjacency label id is null for a link {}.", link.toString()); |
| 413 | return null; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 414 | } |
| 415 | labelStack.add(adjLabelId); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 416 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 417 | deviceId = link.dst().deviceId(); |
| 418 | nodeLabelId = pceStore.getGlobalNodeLabel(deviceId); |
| 419 | if (nodeLabelId == null) { |
| 420 | log.error("Unable to find node label for a device id {} in store.", deviceId.toString()); |
| 421 | return null; |
| 422 | } |
| 423 | labelStack.add(nodeLabelId); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 424 | } |
| 425 | } else { |
| 426 | log.debug("Empty link in path."); |
| 427 | return null; |
| 428 | } |
| 429 | return new DefaultLabelStack(labelStack); |
| 430 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 431 | |
| 432 | /** |
| 433 | * Install a rule for pushing unique global labels to the device. |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 434 | * |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 435 | * @param deviceId device to which flow should be pushed |
| 436 | * @param labelId label for the device |
| 437 | * @param type type of operation |
| 438 | */ |
| 439 | private void installNodeLabelRule(DeviceId deviceId, LabelResourceId labelId, Objective.Operation type) { |
| 440 | checkNotNull(flowObjectiveService); |
| 441 | checkNotNull(appId); |
| 442 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 443 | |
| 444 | selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue())); |
| 445 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 446 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 447 | |
| 448 | ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder() |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 449 | .withSelector(selectorBuilder.build()).withTreatment(treatment) |
| 450 | .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 451 | |
| 452 | if (type.equals(Objective.Operation.ADD)) { |
| 453 | |
| 454 | flowObjectiveService.forward(deviceId, forwardingObjective.add()); |
| 455 | } else { |
| 456 | flowObjectiveService.forward(deviceId, forwardingObjective.remove()); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Install a rule for pushing node labels to the device of other nodes. |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 462 | * |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 463 | * @param deviceId device to which flow should be pushed |
| 464 | * @param labelId label for the device |
| 465 | * @param ipPrefix device for which label is pushed |
| 466 | * @param type type of operation |
| 467 | * @param bBos is this the end of sync push |
| 468 | */ |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 469 | public void advertiseNodeLabelRule(DeviceId deviceId, LabelResourceId labelId, IpPrefix ipPrefix, |
| 470 | Objective.Operation type, boolean bBos) { |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 471 | checkNotNull(flowObjectiveService); |
| 472 | checkNotNull(appId); |
| 473 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 474 | |
| 475 | selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue())); |
| 476 | selectorBuilder.matchIPSrc(ipPrefix); |
| 477 | |
| 478 | if (bBos) { |
| 479 | selectorBuilder.matchMplsBos(bBos); |
| 480 | } |
| 481 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 482 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 483 | |
| 484 | ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder() |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 485 | .withSelector(selectorBuilder.build()).withTreatment(treatment) |
| 486 | .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 487 | |
| 488 | if (type.equals(Objective.Operation.ADD)) { |
| 489 | flowObjectiveService.forward(deviceId, forwardingObjective.add()); |
| 490 | } else { |
| 491 | flowObjectiveService.forward(deviceId, forwardingObjective.remove()); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /** |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 496 | * Install a rule for pushing Adjacency labels to the device. |
| 497 | * |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 498 | * @param deviceId device to which flow should be pushed |
| 499 | * @param labelId label for the adjacency |
| 500 | * @param srcPortNum local port of the adjacency |
| 501 | * @param dstPortNum remote port of the adjacency |
| 502 | * @param type type of operation |
| 503 | */ |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 504 | public void installAdjLabelRule(DeviceId deviceId, LabelResourceId labelId, PortNumber srcPortNum, |
| 505 | PortNumber dstPortNum, Objective.Operation type) { |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 506 | checkNotNull(flowObjectiveService); |
| 507 | checkNotNull(appId); |
| 508 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 509 | |
| 510 | selectorBuilder.matchMplsLabel(MplsLabel.mplsLabel(labelId.id().intValue())); |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 511 | selectorBuilder.matchIPSrc(IpPrefix.valueOf((int) srcPortNum.toLong(), 32)); |
| 512 | selectorBuilder.matchIPDst(IpPrefix.valueOf((int) dstPortNum.toLong(), 32)); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 513 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 514 | TrafficTreatment treatment = DefaultTrafficTreatment.builder().build(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 515 | |
| 516 | ForwardingObjective.Builder forwardingObjective = DefaultForwardingObjective.builder() |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 517 | .withSelector(selectorBuilder.build()).withTreatment(treatment) |
| 518 | .withFlag(ForwardingObjective.Flag.VERSATILE).fromApp(appId).makePermanent(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 519 | |
| 520 | if (type.equals(Objective.Operation.ADD)) { |
| 521 | flowObjectiveService.forward(deviceId, forwardingObjective.add()); |
| 522 | } else { |
| 523 | flowObjectiveService.forward(deviceId, forwardingObjective.remove()); |
| 524 | } |
| 525 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 526 | } |