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 | */ |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 16 | package org.onosproject.pcep.controller.impl; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 19 | import static org.onosproject.pcep.controller.PcepSyncStatus.IN_SYNC; |
| 20 | import static org.onosproject.pcep.controller.PcepSyncStatus.SYNCED; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 21 | |
| 22 | import java.util.Collection; |
| 23 | import java.util.Iterator; |
| 24 | import java.util.HashSet; |
| 25 | import java.util.List; |
| 26 | import java.util.LinkedList; |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 30 | import org.onlab.packet.IpAddress; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 31 | import org.onosproject.incubator.net.resource.label.DefaultLabelResource; |
| 32 | import org.onosproject.incubator.net.resource.label.LabelResource; |
| 33 | import org.onosproject.incubator.net.resource.label.LabelResourceId; |
| 34 | import org.onosproject.incubator.net.resource.label.LabelResourceAdminService; |
| 35 | import org.onosproject.incubator.net.resource.label.LabelResourceService; |
| 36 | import org.onosproject.incubator.net.tunnel.DefaultLabelStack; |
| 37 | import org.onosproject.incubator.net.tunnel.LabelStack; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceService; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 39 | import org.onosproject.pcelabelstore.PcepLabelOp; |
| 40 | import org.onosproject.pcelabelstore.api.PceLabelStore; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 41 | import org.onosproject.net.Device; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 42 | import org.onosproject.net.DeviceId; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 43 | import org.onosproject.net.Link; |
| 44 | import org.onosproject.net.Path; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 45 | import org.onosproject.pcep.controller.PccId; |
| 46 | import org.onosproject.pcep.controller.PcepClient; |
| 47 | import org.onosproject.pcep.controller.PcepClientController; |
| 48 | import org.onosproject.pcep.controller.SrpIdGenerators; |
| 49 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 50 | import org.onosproject.pcepio.protocol.PcepFecObjectIPv4; |
| 51 | import org.onosproject.pcepio.protocol.PcepFecObjectIPv4Adjacency; |
| 52 | import org.onosproject.pcepio.protocol.PcepLabelObject; |
| 53 | import org.onosproject.pcepio.protocol.PcepLabelUpdate; |
| 54 | import org.onosproject.pcepio.protocol.PcepLabelUpdateMsg; |
| 55 | import org.onosproject.pcepio.protocol.PcepSrpObject; |
| 56 | import org.onosproject.pcepio.types.PcepLabelMap; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
| 60 | import com.google.common.collect.ArrayListMultimap; |
| 61 | import 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 | */ |
| 70 | public 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 76 | 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 78 | 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 81 | private static PceccSrTeBeHandler srTeHandlerInstance = null; |
| 82 | private LabelResourceAdminService labelRsrcAdminService; |
| 83 | private LabelResourceService labelRsrcService; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 84 | private DeviceService deviceService; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 85 | private PcepClientController clientController; |
| 86 | private PceLabelStore pceStore; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 87 | |
| 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 112 | * @param deviceService device service |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 113 | */ |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 114 | public void initialize(LabelResourceAdminService labelRsrcAdminService, |
| 115 | LabelResourceService labelRsrcService, |
| 116 | PcepClientController clientController, |
| 117 | PceLabelStore pceStore, |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 118 | DeviceService deviceService) { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 119 | this.labelRsrcAdminService = labelRsrcAdminService; |
| 120 | this.labelRsrcService = labelRsrcService; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 121 | this.clientController = clientController; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 122 | this.pceStore = pceStore; |
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 | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 211 | 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 221 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 222 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 225 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 226 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 233 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 234 | // Push to device |
| 235 | // Push label information of specificDeviceId to otherDevId in list and vice versa. |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 236 | if (!otherDevId.equals(specificDeviceId)) { |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 237 | try { |
| 238 | pushGlobalNodeLabel(getPcepClient(otherDevId), |
| 239 | specificLabelId, |
| 240 | IpAddress.valueOf(specificLsrId).getIp4Address().toInt(), |
| 241 | PcepLabelOp.ADD, |
| 242 | false); |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 243 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 244 | 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-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 252 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 253 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 254 | 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 263 | * @return success or failure |
| 264 | */ |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 265 | public boolean releaseNodeLabel(DeviceId specificDeviceId, String specificLsrId) { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 266 | checkNotNull(specificDeviceId, DEVICE_ID_NULL); |
| 267 | checkNotNull(specificLsrId, LSR_ID_NULL); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 268 | 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 276 | log.error("Unable to retrieve label of a device id {} from store.", specificDeviceId.toString()); |
| 277 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 278 | } |
| 279 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 280 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 283 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 284 | // 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-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 287 | 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 296 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 297 | } |
| 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 312 | 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 330 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 337 | // 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 341 | log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString()); |
| 342 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 343 | } |
| 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 Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 348 | labelId = defaultLabelResource.labelResourceId(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 349 | if (labelId == null) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 350 | log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString()); |
| 351 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 352 | } |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 353 | 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] | 354 | |
| 355 | // Push adjacency label to device |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 356 | 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 363 | |
| 364 | // Save in store |
| 365 | pceStore.addAdjLabel(link, labelId); |
| 366 | return true; |
| 367 | } |
| 368 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 369 | /** |
| 370 | * Releases unused adjacency labels from device pools. |
| 371 | * |
| 372 | * @param link between devices |
| 373 | * @return success or failure |
| 374 | */ |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 375 | public boolean releaseAdjacencyLabel(Link link) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 376 | checkNotNull(link, LINK_NULL); |
| 377 | checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL); |
| 378 | checkNotNull(pceStore, PCE_STORE_NULL); |
| 379 | boolean retValue = true; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 380 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 381 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 387 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 388 | // Device |
| 389 | DeviceId srcDeviceId = link.src().deviceId(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 390 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 391 | // Release adjacency label from device |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 392 | 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 400 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 401 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 409 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 410 | // 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 416 | } |
| 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-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 439 | link = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 440 | // Add adjacency label for this link |
| 441 | adjLabelId = pceStore.getAdjLabel(link); |
| 442 | if (adjLabelId == null) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 443 | log.error("Adjacency label id is null for a link {}.", link.toString()); |
| 444 | return null; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 445 | } |
| 446 | labelStack.add(adjLabelId); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 447 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 448 | 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 S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 455 | } |
| 456 | } else { |
| 457 | log.debug("Empty link in path."); |
| 458 | return null; |
| 459 | } |
| 460 | return new DefaultLabelStack(labelStack); |
| 461 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 462 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 463 | //Pushes node labels to the specified device. |
| 464 | void pushGlobalNodeLabel(PcepClient pc, LabelResourceId labelId, |
| 465 | int labelForNode, PcepLabelOp type, boolean isBos) throws PcepParseException { |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 466 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 467 | checkNotNull(pc); |
| 468 | checkNotNull(labelId); |
| 469 | checkNotNull(type); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 470 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 471 | LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>(); |
| 472 | PcepFecObjectIPv4 fecObject = pc.factory().buildFecObjectIpv4() |
| 473 | .setNodeID(labelForNode) |
| 474 | .build(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 475 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 476 | 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-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 481 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 482 | PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 483 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 484 | //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-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 509 | //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-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 513 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 514 | checkNotNull(pc); |
| 515 | checkNotNull(labelId); |
| 516 | checkNotNull(type); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 517 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 518 | 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-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 528 | } |
| 529 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 530 | PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 531 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 532 | //Adjacency label object |
| 533 | PcepLabelObject labelObject = pc.factory().buildLabelObject() |
| 534 | .setLabel((int) labelId.labelId()) |
| 535 | .build(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 536 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 537 | 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-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 560 | } |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 561 | |
| 562 | srpObj = pc.factory().buildSrpObject() |
| 563 | .setRFlag(bRFlag) |
| 564 | .setSFlag(bSFlag) |
| 565 | .setSrpID(SrpIdGenerators.create()) |
| 566 | .build(); |
| 567 | |
| 568 | return srpObj; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | /** |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 572 | * Returns PCEP client. |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 573 | * |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 574 | * @return PCEP client |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 575 | */ |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 576 | private PcepClient getPcepClient(DeviceId deviceId) { |
| 577 | Device device = deviceService.getDevice(deviceId); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 578 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 579 | // 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-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 583 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 584 | } |