Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 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 | */ |
harikrushna-Huawei | a2c7c20 | 2017-04-10 18:22:00 +0530 | [diff] [blame^] | 16 | package org.onosproject.pcep.server.impl; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
harikrushna-Huawei | a2c7c20 | 2017-04-10 18:22:00 +0530 | [diff] [blame^] | 19 | import static org.onosproject.pcep.server.PcepSyncStatus.IN_SYNC; |
| 20 | import static org.onosproject.pcep.server.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; |
harikrushna-Huawei | a2c7c20 | 2017-04-10 18:22:00 +0530 | [diff] [blame^] | 45 | import org.onosproject.pcep.server.PccId; |
| 46 | import org.onosproject.pcep.server.PcepClient; |
| 47 | import org.onosproject.pcep.server.PcepClientController; |
| 48 | import org.onosproject.pcep.server.SrpIdGenerators; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 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 |
Ray Milkey | ef79434 | 2016-11-09 16:20:29 -0800 | [diff] [blame] | 111 | * @param clientController client controller |
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 | */ |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 115 | public void initialize(LabelResourceAdminService labelRsrcAdminService, |
| 116 | LabelResourceService labelRsrcService, |
| 117 | PcepClientController clientController, |
| 118 | PceLabelStore pceStore, |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 119 | DeviceService deviceService) { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 120 | this.labelRsrcAdminService = labelRsrcAdminService; |
| 121 | this.labelRsrcService = labelRsrcService; |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 122 | this.clientController = clientController; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 123 | this.pceStore = pceStore; |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 124 | this.deviceService = deviceService; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Reserves the global label pool. |
| 129 | * |
| 130 | * @param beginLabel minimum value of global label space |
| 131 | * @param endLabel maximum value of global label space |
| 132 | * @return success or failure |
| 133 | */ |
| 134 | public boolean reserveGlobalPool(long beginLabel, long endLabel) { |
| 135 | checkNotNull(labelRsrcAdminService, LABEL_RESOURCE_ADMIN_SERVICE_NULL); |
| 136 | return labelRsrcAdminService.createGlobalPool(LabelResourceId.labelResourceId(beginLabel), |
| 137 | LabelResourceId.labelResourceId(endLabel)); |
| 138 | } |
| 139 | |
| 140 | /** |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 141 | * Retrieve lsr-id from device annotation. |
| 142 | * |
| 143 | * @param deviceId specific device id from which lsr-id needs to be retrieved |
| 144 | * @return lsr-id of a device |
| 145 | */ |
| 146 | public String getLsrId(DeviceId deviceId) { |
| 147 | checkNotNull(deviceId, DEVICE_ID_NULL); |
| 148 | Device device = deviceService.getDevice(deviceId); |
| 149 | if (device == null) { |
| 150 | log.debug("Device is not available for device id {} in device service.", deviceId.toString()); |
| 151 | return null; |
| 152 | } |
| 153 | |
| 154 | // Retrieve lsr-id from device |
| 155 | if (device.annotations() == null) { |
| 156 | log.debug("Device {} does not have annotation.", device.toString()); |
| 157 | return null; |
| 158 | } |
| 159 | |
| 160 | String lsrId = device.annotations().value(LSR_ID); |
| 161 | if (lsrId == null) { |
| 162 | log.debug("The lsr-id of device {} is null.", device.toString()); |
| 163 | return null; |
| 164 | } |
| 165 | return lsrId; |
| 166 | } |
| 167 | |
| 168 | /** |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 169 | * Allocates node label from global node label pool to specific device. |
| 170 | * Configure this device with labels and lsrid mapping of all other devices and vice versa. |
| 171 | * |
| 172 | * @param specificDeviceId node label needs to be allocated to specific device |
| 173 | * @param specificLsrId lsrid of specific device |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 174 | * @return success or failure |
| 175 | */ |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 176 | public boolean allocateNodeLabel(DeviceId specificDeviceId, String specificLsrId) { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 177 | long applyNum = 1; // For each node only one node label |
| 178 | LabelResourceId specificLabelId = null; |
| 179 | |
| 180 | checkNotNull(specificDeviceId, DEVICE_ID_NULL); |
| 181 | checkNotNull(specificLsrId, LSR_ID_NULL); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 182 | checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL); |
| 183 | checkNotNull(pceStore, PCE_STORE_NULL); |
| 184 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 185 | // Check whether node-label was already configured for this specific device. |
| 186 | if (pceStore.getGlobalNodeLabel(specificDeviceId) != null) { |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 187 | log.debug("Node label was already configured for device {}.", specificDeviceId.toString()); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // 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] | 192 | // So, first generate its label and configure label and its lsr-id to it. |
| 193 | Collection<LabelResource> result = labelRsrcService.applyFromGlobalPool(applyNum); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 194 | if (!result.isEmpty()) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 195 | // Only one element (label-id) to retrieve |
| 196 | Iterator<LabelResource> iterator = result.iterator(); |
| 197 | DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next(); |
| 198 | specificLabelId = defaultLabelResource.labelResourceId(); |
| 199 | if (specificLabelId == null) { |
| 200 | log.error("Unable to retrieve global node label for a device id {}.", specificDeviceId.toString()); |
| 201 | return false; |
| 202 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 203 | } else { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 204 | log.error("Unable to allocate global node label for a device id {}.", specificDeviceId.toString()); |
| 205 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 206 | } |
| 207 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 208 | // store it |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 209 | pceStore.addGlobalNodeLabel(specificDeviceId, specificLabelId); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 210 | |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 211 | // Push its label information into specificDeviceId |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 212 | PcepClient pcc = getPcepClient(specificDeviceId); |
| 213 | try { |
| 214 | pushGlobalNodeLabel(pcc, |
| 215 | specificLabelId, |
| 216 | IpAddress.valueOf(specificLsrId).getIp4Address().toInt(), |
| 217 | PcepLabelOp.ADD, |
| 218 | false); |
| 219 | } catch (PcepParseException e) { |
| 220 | log.error("Failed to push global node label for LSR {}.", specificLsrId.toString()); |
| 221 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 222 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 223 | // Configure (node-label, lsr-id) mapping of each devices into specific device and vice versa. |
| 224 | for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) { |
| 225 | DeviceId otherDevId = element.getKey(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 226 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 227 | // Get lsr-id of a device |
| 228 | String otherLsrId = getLsrId(otherDevId); |
| 229 | if (otherLsrId == null) { |
| 230 | log.error("The lsr-id of device id {} is null.", otherDevId.toString()); |
| 231 | releaseNodeLabel(specificDeviceId, specificLsrId); |
| 232 | return false; |
| 233 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 234 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 235 | // Push to device |
| 236 | // Push label information of specificDeviceId to otherDevId in list and vice versa. |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 237 | if (!otherDevId.equals(specificDeviceId)) { |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 238 | try { |
| 239 | pushGlobalNodeLabel(getPcepClient(otherDevId), |
| 240 | specificLabelId, |
| 241 | IpAddress.valueOf(specificLsrId).getIp4Address().toInt(), |
| 242 | PcepLabelOp.ADD, |
| 243 | false); |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 244 | |
Priyanka B | 1dd6012 | 2016-12-29 16:33:45 +0530 | [diff] [blame] | 245 | pushGlobalNodeLabel(pcc, element.getValue(), |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 246 | IpAddress.valueOf(otherLsrId).getIp4Address().toInt(), |
| 247 | PcepLabelOp.ADD, |
| 248 | false); |
| 249 | } catch (PcepParseException e) { |
| 250 | log.error("Failed to push global node label for LSR {} or LSR {}.", specificLsrId.toString(), |
| 251 | otherLsrId.toString()); |
| 252 | } |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 253 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 254 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Releases assigned node label of specific device from global node label pool and pce store. |
| 260 | * and remove configured this node label from all other devices. |
| 261 | * |
| 262 | * @param specificDeviceId node label needs to be released for specific device |
| 263 | * @param specificLsrId lsrid of specific device |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 264 | * @return success or failure |
| 265 | */ |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 266 | public boolean releaseNodeLabel(DeviceId specificDeviceId, String specificLsrId) { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 267 | checkNotNull(specificDeviceId, DEVICE_ID_NULL); |
| 268 | checkNotNull(specificLsrId, LSR_ID_NULL); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 269 | checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL); |
| 270 | checkNotNull(pceStore, PCE_STORE_NULL); |
| 271 | boolean retValue = true; |
| 272 | |
| 273 | // Release node label entry of this specific device from all other devices |
| 274 | // Retrieve node label of this specific device from store |
| 275 | LabelResourceId labelId = pceStore.getGlobalNodeLabel(specificDeviceId); |
| 276 | if (labelId == null) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 277 | log.error("Unable to retrieve label of a device id {} from store.", specificDeviceId.toString()); |
| 278 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 279 | } |
| 280 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 281 | // Go through all devices in the pce store and remove label entry from device |
| 282 | for (Map.Entry<DeviceId, LabelResourceId> element : pceStore.getGlobalNodeLabels().entrySet()) { |
| 283 | DeviceId otherDevId = element.getKey(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 284 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 285 | // Remove this specific device label information from all other nodes except |
| 286 | // this specific node where connection already lost. |
| 287 | if (!specificDeviceId.equals(otherDevId)) { |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 288 | try { |
| 289 | pushGlobalNodeLabel(getPcepClient(otherDevId), |
| 290 | labelId, |
| 291 | IpAddress.valueOf(specificLsrId).getIp4Address().toInt(), |
| 292 | PcepLabelOp.REMOVE, |
| 293 | false); |
| 294 | } catch (PcepParseException e) { |
| 295 | log.error("Failed to push global node label for LSR {}.", specificLsrId.toString()); |
| 296 | } |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 297 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Release from label manager |
| 301 | Set<LabelResourceId> release = new HashSet<>(); |
| 302 | release.add(labelId); |
| 303 | if (!labelRsrcService.releaseToGlobalPool(release)) { |
| 304 | log.error("Unable to release label id {} from label manager.", labelId.toString()); |
| 305 | retValue = false; |
| 306 | } |
| 307 | |
| 308 | // Remove from store |
| 309 | if (!pceStore.removeGlobalNodeLabel(specificDeviceId)) { |
| 310 | log.error("Unable to remove global node label id {} from store.", labelId.toString()); |
| 311 | retValue = false; |
| 312 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 313 | return retValue; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Allocates adjacency label to a link from local resource pool by a specific device id. |
| 318 | * |
| 319 | * @param link between devices |
| 320 | * @return success or failure |
| 321 | */ |
| 322 | public boolean allocateAdjacencyLabel(Link link) { |
| 323 | long applyNum = 1; // Single label to each link. |
| 324 | DeviceId srcDeviceId = link.src().deviceId(); |
| 325 | Collection<LabelResource> labelList; |
| 326 | |
| 327 | checkNotNull(link, LINK_NULL); |
| 328 | checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL); |
| 329 | checkNotNull(pceStore, PCE_STORE_NULL); |
| 330 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 331 | // Checks whether adjacency label was already allocated |
| 332 | LabelResourceId labelId = pceStore.getAdjLabel(link); |
| 333 | if (labelId != null) { |
| 334 | log.debug("Adjacency label {} was already allocated for a link {}.", labelId.toString(), link.toString()); |
| 335 | return false; |
| 336 | } |
| 337 | |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 338 | // Allocate adjacency label to a link from label manager. |
| 339 | // Take label from source device pool to allocate. |
| 340 | labelList = labelRsrcService.applyFromDevicePool(srcDeviceId, applyNum); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 341 | if (labelList.isEmpty()) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 342 | log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString()); |
| 343 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | // Currently only one label to a device. So, no need to iterate through list |
| 347 | Iterator<LabelResource> iterator = labelList.iterator(); |
| 348 | DefaultLabelResource defaultLabelResource = (DefaultLabelResource) iterator.next(); |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 349 | labelId = defaultLabelResource.labelResourceId(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 350 | if (labelId == null) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 351 | log.error("Unable to allocate label to a device id {}.", srcDeviceId.toString()); |
| 352 | return false; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 353 | } |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 354 | 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] | 355 | |
| 356 | // Push adjacency label to device |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 357 | try { |
| 358 | pushAdjacencyLabel(getPcepClient(srcDeviceId), labelId, (int) link.src().port().toLong(), |
| 359 | (int) link.dst().port().toLong(), PcepLabelOp.ADD); |
| 360 | } catch (PcepParseException e) { |
| 361 | log.error("Failed to push adjacency label for link {}-{}.", (int) link.src().port().toLong(), |
| 362 | (int) link.dst().port().toLong()); |
| 363 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 364 | |
| 365 | // Save in store |
| 366 | pceStore.addAdjLabel(link, labelId); |
| 367 | return true; |
| 368 | } |
| 369 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 370 | /** |
| 371 | * Releases unused adjacency labels from device pools. |
| 372 | * |
| 373 | * @param link between devices |
| 374 | * @return success or failure |
| 375 | */ |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 376 | public boolean releaseAdjacencyLabel(Link link) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 377 | checkNotNull(link, LINK_NULL); |
| 378 | checkNotNull(labelRsrcService, LABEL_RESOURCE_SERVICE_NULL); |
| 379 | checkNotNull(pceStore, PCE_STORE_NULL); |
| 380 | boolean retValue = true; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 381 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 382 | // Retrieve link label from store |
| 383 | LabelResourceId labelId = pceStore.getAdjLabel(link); |
| 384 | if (labelId == null) { |
| 385 | log.error("Unabel to retrieve label for a link {} from store.", link.toString()); |
| 386 | return false; |
| 387 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 388 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 389 | // Device |
| 390 | DeviceId srcDeviceId = link.src().deviceId(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 391 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 392 | // Release adjacency label from device |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 393 | try { |
| 394 | pushAdjacencyLabel(getPcepClient(srcDeviceId), labelId, (int) link.src().port().toLong(), |
| 395 | (int) link.dst().port().toLong(), PcepLabelOp.REMOVE); |
| 396 | } catch (PcepParseException e) { |
| 397 | log.error("Failed to push adjacency label for link {}-{}.", (int) link.src().port().toLong(), |
| 398 | (int) link.dst().port().toLong()); |
| 399 | } |
| 400 | |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 401 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 402 | // Release link label from label manager |
| 403 | Multimap<DeviceId, LabelResource> release = ArrayListMultimap.create(); |
| 404 | DefaultLabelResource defaultLabelResource = new DefaultLabelResource(srcDeviceId, labelId); |
| 405 | release.put(srcDeviceId, defaultLabelResource); |
| 406 | if (!labelRsrcService.releaseToDevicePool(release)) { |
| 407 | log.error("Unable to release label id {} from label manager.", labelId.toString()); |
| 408 | retValue = false; |
| 409 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 410 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 411 | // Remove adjacency label from store |
| 412 | if (!pceStore.removeAdjLabel(link)) { |
| 413 | log.error("Unable to remove adjacency label id {} from store.", labelId.toString()); |
| 414 | retValue = false; |
| 415 | } |
| 416 | return retValue; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Computes label stack for a path. |
| 421 | * |
| 422 | * @param path lsp path |
| 423 | * @return label stack |
| 424 | */ |
| 425 | public LabelStack computeLabelStack(Path path) { |
| 426 | checkNotNull(path, PATH_NULL); |
| 427 | // Label stack is linked list to make labels in order. |
| 428 | List<LabelResourceId> labelStack = new LinkedList<>(); |
| 429 | List<Link> linkList = path.links(); |
Jon Hall | cbd1b39 | 2017-01-18 20:15:44 -0800 | [diff] [blame] | 430 | if ((linkList != null) && (!linkList.isEmpty())) { |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 431 | // Path: [x] ---- [y] ---- [z] |
| 432 | // For other than last link, add only source[x] device label. |
| 433 | // For the last link, add both source[y] and destination[z] device labels. |
| 434 | // For all links add adjacency label |
| 435 | Link link = null; |
| 436 | LabelResourceId nodeLabelId = null; |
| 437 | LabelResourceId adjLabelId = null; |
| 438 | DeviceId deviceId = null; |
| 439 | for (Iterator<Link> iterator = linkList.iterator(); iterator.hasNext();) { |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 440 | link = iterator.next(); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 441 | // Add adjacency label for this link |
| 442 | adjLabelId = pceStore.getAdjLabel(link); |
| 443 | if (adjLabelId == null) { |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 444 | log.error("Adjacency label id is null for a link {}.", link.toString()); |
| 445 | return null; |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 446 | } |
| 447 | labelStack.add(adjLabelId); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 448 | |
Mahesh Poojary Huawei | 1d17cad | 2016-06-02 12:53:41 +0530 | [diff] [blame] | 449 | deviceId = link.dst().deviceId(); |
| 450 | nodeLabelId = pceStore.getGlobalNodeLabel(deviceId); |
| 451 | if (nodeLabelId == null) { |
| 452 | log.error("Unable to find node label for a device id {} in store.", deviceId.toString()); |
| 453 | return null; |
| 454 | } |
| 455 | labelStack.add(nodeLabelId); |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 456 | } |
| 457 | } else { |
| 458 | log.debug("Empty link in path."); |
| 459 | return null; |
| 460 | } |
| 461 | return new DefaultLabelStack(labelStack); |
| 462 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 463 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 464 | //Pushes node labels to the specified device. |
| 465 | void pushGlobalNodeLabel(PcepClient pc, LabelResourceId labelId, |
| 466 | int labelForNode, PcepLabelOp type, boolean isBos) throws PcepParseException { |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 467 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 468 | checkNotNull(pc); |
| 469 | checkNotNull(labelId); |
| 470 | checkNotNull(type); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 471 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 472 | LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>(); |
| 473 | PcepFecObjectIPv4 fecObject = pc.factory().buildFecObjectIpv4() |
| 474 | .setNodeID(labelForNode) |
| 475 | .build(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 476 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 477 | boolean bSFlag = false; |
| 478 | if (pc.labelDbSyncStatus() == IN_SYNC && !isBos) { |
| 479 | // Need to set sync flag in all messages till sync completes. |
| 480 | bSFlag = true; |
| 481 | } |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 482 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 483 | PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 484 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 485 | //Global NODE-SID as label object |
| 486 | PcepLabelObject labelObject = pc.factory().buildLabelObject() |
| 487 | .setLabel((int) labelId.labelId()) |
| 488 | .build(); |
| 489 | |
| 490 | PcepLabelMap labelMap = new PcepLabelMap(); |
| 491 | labelMap.setFecObject(fecObject); |
| 492 | labelMap.setLabelObject(labelObject); |
| 493 | labelMap.setSrpObject(srpObj); |
| 494 | |
| 495 | labelUpdateList.add(pc.factory().buildPcepLabelUpdateObject() |
| 496 | .setLabelMap(labelMap) |
| 497 | .build()); |
| 498 | |
| 499 | PcepLabelUpdateMsg labelMsg = pc.factory().buildPcepLabelUpdateMsg() |
| 500 | .setPcLabelUpdateList(labelUpdateList) |
| 501 | .build(); |
| 502 | pc.sendMessage(labelMsg); |
| 503 | |
| 504 | if (isBos) { |
| 505 | // Sync is completed. |
| 506 | pc.setLabelDbSyncStatus(SYNCED); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 510 | //Pushes adjacency labels to the specified device. |
| 511 | void pushAdjacencyLabel(PcepClient pc, LabelResourceId labelId, int srcPortNo, |
| 512 | int dstPortNo, PcepLabelOp type) |
| 513 | throws PcepParseException { |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 514 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 515 | checkNotNull(pc); |
| 516 | checkNotNull(labelId); |
| 517 | checkNotNull(type); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 518 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 519 | LinkedList<PcepLabelUpdate> labelUpdateList = new LinkedList<>(); |
| 520 | PcepFecObjectIPv4Adjacency fecAdjObject = pc.factory().buildFecIpv4Adjacency() |
| 521 | .seRemoteIPv4Address(dstPortNo) |
| 522 | .seLocalIPv4Address(srcPortNo) |
| 523 | .build(); |
| 524 | |
| 525 | boolean bSFlag = false; |
| 526 | if (pc.labelDbSyncStatus() == IN_SYNC) { |
| 527 | // Need to set sync flag in all messages till sync completes. |
| 528 | bSFlag = true; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 529 | } |
| 530 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 531 | PcepSrpObject srpObj = getSrpObject(pc, type, bSFlag); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 532 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 533 | //Adjacency label object |
| 534 | PcepLabelObject labelObject = pc.factory().buildLabelObject() |
| 535 | .setLabel((int) labelId.labelId()) |
| 536 | .build(); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 537 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 538 | PcepLabelMap labelMap = new PcepLabelMap(); |
| 539 | labelMap.setFecObject(fecAdjObject); |
| 540 | labelMap.setLabelObject(labelObject); |
| 541 | labelMap.setSrpObject(srpObj); |
| 542 | |
| 543 | labelUpdateList.add(pc.factory().buildPcepLabelUpdateObject() |
| 544 | .setLabelMap(labelMap) |
| 545 | .build()); |
| 546 | |
| 547 | PcepLabelUpdateMsg labelMsg = pc.factory().buildPcepLabelUpdateMsg() |
| 548 | .setPcLabelUpdateList(labelUpdateList) |
| 549 | .build(); |
| 550 | pc.sendMessage(labelMsg); |
| 551 | } |
| 552 | |
| 553 | private PcepSrpObject getSrpObject(PcepClient pc, PcepLabelOp type, boolean bSFlag) |
| 554 | throws PcepParseException { |
| 555 | PcepSrpObject srpObj; |
| 556 | boolean bRFlag = false; |
| 557 | |
| 558 | if (!type.equals(PcepLabelOp.ADD)) { |
| 559 | // To cleanup labels, R bit is set |
| 560 | bRFlag = true; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 561 | } |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 562 | |
| 563 | srpObj = pc.factory().buildSrpObject() |
| 564 | .setRFlag(bRFlag) |
| 565 | .setSFlag(bSFlag) |
| 566 | .setSrpID(SrpIdGenerators.create()) |
| 567 | .build(); |
| 568 | |
| 569 | return srpObj; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 573 | * Returns PCEP client. |
Avantika-Huawei | 032a987 | 2016-05-27 22:57:38 +0530 | [diff] [blame] | 574 | * |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 575 | * @return PCEP client |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 576 | */ |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 577 | private PcepClient getPcepClient(DeviceId deviceId) { |
| 578 | Device device = deviceService.getDevice(deviceId); |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 579 | |
Avantika-Huawei | 9e848e8 | 2016-09-01 12:12:42 +0530 | [diff] [blame] | 580 | // In future projections instead of annotations will be used to fetch LSR ID. |
| 581 | String lsrId = device.annotations().value(LSR_ID); |
| 582 | PcepClient pcc = clientController.getClient(PccId.pccId(IpAddress.valueOf(lsrId))); |
| 583 | return pcc; |
Avantika-Huawei | dbdf772 | 2016-05-21 14:20:31 +0530 | [diff] [blame] | 584 | } |
Mahesh Poojary S | a4df1aa | 2016-05-13 08:53:53 +0530 | [diff] [blame] | 585 | } |