SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +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 | */ |
| 16 | package org.onosproject.pcep.controller.impl; |
| 17 | |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 18 | import java.util.Arrays; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 19 | import java.util.Collection; |
| 20 | import java.util.Collections; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 21 | import java.util.HashMap; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 22 | import java.util.HashSet; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 23 | import java.util.Iterator; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 24 | import java.util.LinkedList; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 25 | import java.util.List; |
| 26 | import java.util.ListIterator; |
| 27 | import java.util.Map; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 28 | import java.util.Set; |
| 29 | import java.util.concurrent.ConcurrentHashMap; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 30 | |
| 31 | import org.apache.felix.scr.annotations.Activate; |
| 32 | import org.apache.felix.scr.annotations.Component; |
| 33 | import org.apache.felix.scr.annotations.Deactivate; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 34 | import org.apache.felix.scr.annotations.Reference; |
| 35 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 36 | import org.apache.felix.scr.annotations.Service; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 37 | import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint; |
| 38 | import org.onosproject.incubator.net.tunnel.Tunnel; |
| 39 | import org.onosproject.incubator.net.tunnel.TunnelService; |
| 40 | import org.onosproject.incubator.net.tunnel.Tunnel.State; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 41 | import org.onosproject.net.device.DeviceService; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 42 | import org.onosproject.pcep.controller.LspKey; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 43 | import org.onosproject.pcep.controller.PccId; |
| 44 | import org.onosproject.pcep.controller.PcepClient; |
| 45 | import org.onosproject.pcep.controller.PcepClientController; |
| 46 | import org.onosproject.pcep.controller.PcepClientListener; |
| 47 | import org.onosproject.pcep.controller.PcepEventListener; |
Priyanka B | 259847d | 2016-06-03 21:28:35 +0530 | [diff] [blame] | 48 | import org.onosproject.pcep.controller.PcepLspStatus; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 49 | import org.onosproject.pcep.controller.PcepNodeListener; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 50 | import org.onosproject.pcep.controller.PcepPacketListener; |
| 51 | import org.onosproject.pcep.controller.PcepSyncStatus; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 52 | import org.onosproject.pcep.controller.driver.PcepAgent; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 53 | import org.onosproject.pcepio.exceptions.PcepParseException; |
| 54 | import org.onosproject.pcepio.protocol.PcInitiatedLspRequest; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 55 | import org.onosproject.pcepio.protocol.PcepError; |
| 56 | import org.onosproject.pcepio.protocol.PcepErrorInfo; |
| 57 | import org.onosproject.pcepio.protocol.PcepErrorMsg; |
| 58 | import org.onosproject.pcepio.protocol.PcepErrorObject; |
| 59 | import org.onosproject.pcepio.protocol.PcepFactory; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 60 | import org.onosproject.pcepio.protocol.PcepInitiateMsg; |
| 61 | import org.onosproject.pcepio.protocol.PcepLspObject; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 62 | import org.onosproject.pcepio.protocol.PcepMessage; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 63 | import org.onosproject.pcepio.protocol.PcepReportMsg; |
| 64 | import org.onosproject.pcepio.protocol.PcepStateReport; |
| 65 | import org.onosproject.pcepio.types.PcepValueType; |
| 66 | import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv; |
| 67 | import org.onosproject.pcepio.types.SymbolicPathNameTlv; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 68 | import org.slf4j.Logger; |
| 69 | import org.slf4j.LoggerFactory; |
| 70 | |
| 71 | import com.google.common.collect.Sets; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 72 | import static com.google.common.base.Preconditions.checkNotNull; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 73 | |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 74 | import static org.onosproject.pcep.controller.PcepSyncStatus.IN_SYNC; |
| 75 | import static org.onosproject.pcep.controller.PcepLspSyncAction.REMOVE; |
| 76 | import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_UPDATE; |
| 77 | import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_DELETE; |
| 78 | import static org.onosproject.pcep.controller.PcepLspSyncAction.UNSTABLE; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 79 | import static org.onosproject.pcepio.types.PcepErrorDetailInfo.ERROR_TYPE_19; |
| 80 | import static org.onosproject.pcepio.types.PcepErrorDetailInfo.ERROR_VALUE_5; |
| 81 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 82 | /** |
| 83 | * Implementation of PCEP client controller. |
| 84 | */ |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 85 | @Component(immediate = true) |
| 86 | @Service |
| 87 | public class PcepClientControllerImpl implements PcepClientController { |
| 88 | |
| 89 | private static final Logger log = LoggerFactory.getLogger(PcepClientControllerImpl.class); |
| 90 | |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected DeviceService deviceService; |
| 93 | |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 94 | protected ConcurrentHashMap<PccId, PcepClient> connectedClients = |
Sho SHIMIZU | 9b8274c | 2015-09-04 15:54:24 -0700 | [diff] [blame] | 95 | new ConcurrentHashMap<>(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 96 | |
| 97 | protected PcepClientAgent agent = new PcepClientAgent(); |
| 98 | protected Set<PcepClientListener> pcepClientListener = new HashSet<>(); |
| 99 | |
| 100 | protected Set<PcepEventListener> pcepEventListener = Sets.newHashSet(); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 101 | protected Set<PcepNodeListener> pcepNodeListener = Sets.newHashSet(); |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 102 | protected Set<PcepPacketListener> pcepPacketListener = Sets.newHashSet(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 103 | |
| 104 | private final Controller ctrl = new Controller(); |
| 105 | |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 106 | public static final String BANDWIDTH = "bandwidth"; |
| 107 | public static final String LSP_SIG_TYPE = "lspSigType"; |
| 108 | public static final String PCC_TUNNEL_ID = "PccTunnelId"; |
| 109 | public static final String PLSP_ID = "PLspId"; |
| 110 | public static final String LOCAL_LSP_ID = "localLspId"; |
| 111 | public static final String PCE_INIT = "pceInit"; |
| 112 | public static final String COST_TYPE = "costType"; |
| 113 | public static final String DELEGATE = "delegation"; |
| 114 | |
| 115 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 116 | protected TunnelService tunnelService; |
| 117 | |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 118 | @Activate |
| 119 | public void activate() { |
| 120 | ctrl.start(agent); |
| 121 | log.info("Started"); |
| 122 | } |
| 123 | |
| 124 | @Deactivate |
| 125 | public void deactivate() { |
| 126 | // Close all connected clients |
| 127 | closeConnectedClients(); |
| 128 | ctrl.stop(); |
| 129 | log.info("Stopped"); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Collection<PcepClient> getClients() { |
| 134 | return connectedClients.values(); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public PcepClient getClient(PccId pccId) { |
| 139 | return connectedClients.get(pccId); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void addListener(PcepClientListener listener) { |
| 144 | if (!pcepClientListener.contains(listener)) { |
| 145 | this.pcepClientListener.add(listener); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void removeListener(PcepClientListener listener) { |
| 151 | this.pcepClientListener.remove(listener); |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void addEventListener(PcepEventListener listener) { |
| 156 | pcepEventListener.add(listener); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void removeEventListener(PcepEventListener listener) { |
| 161 | pcepEventListener.remove(listener); |
| 162 | } |
| 163 | |
| 164 | @Override |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 165 | public void addPacketListener(PcepPacketListener listener) { |
| 166 | pcepPacketListener.add(listener); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void removePacketListener(PcepPacketListener listener) { |
| 171 | pcepPacketListener.remove(listener); |
| 172 | } |
| 173 | |
| 174 | @Override |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 175 | public void writeMessage(PccId pccId, PcepMessage msg) { |
| 176 | this.getClient(pccId).sendMessage(msg); |
| 177 | } |
| 178 | |
| 179 | @Override |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 180 | public void addNodeListener(PcepNodeListener listener) { |
| 181 | pcepNodeListener.add(listener); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public void removeNodeListener(PcepNodeListener listener) { |
| 186 | pcepNodeListener.remove(listener); |
| 187 | } |
| 188 | |
| 189 | @Override |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 190 | public void processClientMessage(PccId pccId, PcepMessage msg) { |
| 191 | PcepClient pc = getClient(pccId); |
| 192 | |
| 193 | switch (msg.getType()) { |
| 194 | case NONE: |
| 195 | break; |
| 196 | case OPEN: |
| 197 | break; |
| 198 | case KEEP_ALIVE: |
| 199 | break; |
| 200 | case PATH_COMPUTATION_REQUEST: |
| 201 | break; |
| 202 | case PATH_COMPUTATION_REPLY: |
| 203 | break; |
| 204 | case NOTIFICATION: |
| 205 | break; |
| 206 | case ERROR: |
| 207 | break; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 208 | case INITIATE: |
| 209 | if (!pc.capability().pcInstantiationCapability()) { |
| 210 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), ERROR_TYPE_19, |
| 211 | ERROR_VALUE_5))); |
| 212 | } |
| 213 | break; |
| 214 | case UPDATE: |
| 215 | if (!pc.capability().statefulPceCapability()) { |
| 216 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), ERROR_TYPE_19, |
| 217 | ERROR_VALUE_5))); |
| 218 | } |
| 219 | break; |
| 220 | case LABEL_UPDATE: |
| 221 | if (!pc.capability().pceccCapability()) { |
| 222 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), ERROR_TYPE_19, |
| 223 | ERROR_VALUE_5))); |
| 224 | } |
| 225 | break; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 226 | case CLOSE: |
| 227 | log.info("Sending Close Message to {" + pccId.toString() + "}"); |
| 228 | pc.sendMessage(Collections.singletonList(pc.factory().buildCloseMsg().build())); |
| 229 | //now disconnect client |
| 230 | pc.disconnectClient(); |
| 231 | break; |
| 232 | case REPORT: |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 233 | //Only update the listener if respective capability is supported else send PCEP-ERR msg |
| 234 | if (pc.capability().statefulPceCapability()) { |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 235 | |
| 236 | ListIterator<PcepStateReport> listIterator = ((PcepReportMsg) msg).getStateReportList().listIterator(); |
| 237 | while (listIterator.hasNext()) { |
| 238 | PcepStateReport stateRpt = listIterator.next(); |
Avantika-Huawei | 3524d85 | 2016-06-04 20:44:13 +0530 | [diff] [blame] | 239 | PcepLspObject lspObj = stateRpt.getLspObject(); |
| 240 | if (lspObj.getSFlag()) { |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 241 | if (pc.lspDbSyncStatus() != PcepSyncStatus.IN_SYNC) { |
Avantika-Huawei | 3524d85 | 2016-06-04 20:44:13 +0530 | [diff] [blame] | 242 | log.debug("LSP DB sync started for PCC {}", pc.getPccId().id().toString()); |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 243 | // Initialize LSP DB sync and temporary cache. |
| 244 | pc.setLspDbSyncStatus(PcepSyncStatus.IN_SYNC); |
| 245 | pc.initializeSyncMsgList(pccId); |
| 246 | } |
| 247 | // Store stateRpt in temporary cache. |
| 248 | pc.addSyncMsgToList(pccId, stateRpt); |
| 249 | |
| 250 | // Don't send to provider as of now. |
| 251 | continue; |
Avantika-Huawei | 3524d85 | 2016-06-04 20:44:13 +0530 | [diff] [blame] | 252 | } else if (lspObj.getPlspId() == 0) { |
| 253 | if (pc.lspDbSyncStatus() == PcepSyncStatus.IN_SYNC |
| 254 | || pc.lspDbSyncStatus() == PcepSyncStatus.NOT_SYNCED) { |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 255 | // Set end of LSPDB sync. |
Avantika-Huawei | 3524d85 | 2016-06-04 20:44:13 +0530 | [diff] [blame] | 256 | log.debug("LSP DB sync completed for PCC {}", pc.getPccId().id().toString()); |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 257 | pc.setLspDbSyncStatus(PcepSyncStatus.SYNCED); |
| 258 | |
| 259 | // Call packet provider to initiate label DB sync (only if PCECC capable). |
| 260 | if (pc.capability().pceccCapability()) { |
Avantika-Huawei | 3524d85 | 2016-06-04 20:44:13 +0530 | [diff] [blame] | 261 | log.debug("Trigger label DB sync for PCC {}", pc.getPccId().id().toString()); |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 262 | pc.setLabelDbSyncStatus(IN_SYNC); |
| 263 | for (PcepPacketListener l : pcepPacketListener) { |
| 264 | l.sendPacketIn(pccId); |
| 265 | } |
| 266 | } else { |
| 267 | // If label db sync is not to be done, handle end of LSPDB sync actions. |
| 268 | agent.analyzeSyncMsgList(pccId); |
| 269 | } |
| 270 | continue; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // It's a usual report message while sync is not undergoing. So process it immediately. |
| 275 | LinkedList<PcepStateReport> llPcRptList = new LinkedList<>(); |
| 276 | llPcRptList.add(stateRpt); |
| 277 | PcepMessage pcReportMsg = pc.factory().buildReportMsg().setStateReportList((llPcRptList)) |
| 278 | .build(); |
| 279 | for (PcepEventListener l : pcepEventListener) { |
| 280 | l.handleMessage(pccId, pcReportMsg); |
| 281 | } |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 282 | } |
| 283 | } else { |
| 284 | // Send PCEP-ERROR message. |
| 285 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), |
| 286 | ERROR_TYPE_19, ERROR_VALUE_5))); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 287 | } |
| 288 | break; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 289 | case LABEL_RANGE_RESERV: |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 290 | break; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 291 | case LS_REPORT: //TODO: need to handle LS report to add or remove node |
| 292 | break; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 293 | case MAX: |
| 294 | break; |
| 295 | case END: |
| 296 | break; |
| 297 | default: |
| 298 | break; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void closeConnectedClients() { |
| 304 | PcepClient pc; |
| 305 | for (PccId id : connectedClients.keySet()) { |
| 306 | pc = getClient(id); |
| 307 | pc.disconnectClient(); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /** |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 312 | * Returns pcep error message with specific error type and value. |
| 313 | * |
| 314 | * @param factory represents pcep factory |
| 315 | * @param errorType pcep error type |
| 316 | * @param errorValue pcep error value |
| 317 | * @return pcep error message |
| 318 | */ |
| 319 | public PcepErrorMsg getErrMsg(PcepFactory factory, byte errorType, byte errorValue) { |
| 320 | LinkedList<PcepError> llPcepErr = new LinkedList<>(); |
| 321 | |
| 322 | LinkedList<PcepErrorObject> llerrObj = new LinkedList<>(); |
| 323 | PcepErrorMsg errMsg; |
| 324 | |
| 325 | PcepErrorObject errObj = factory.buildPcepErrorObject().setErrorValue(errorValue).setErrorType(errorType) |
| 326 | .build(); |
| 327 | |
| 328 | llerrObj.add(errObj); |
| 329 | PcepError pcepErr = factory.buildPcepError().setErrorObjList(llerrObj).build(); |
| 330 | |
| 331 | llPcepErr.add(pcepErr); |
| 332 | |
| 333 | PcepErrorInfo errInfo = factory.buildPcepErrorInfo().setPcepErrorList(llPcepErr).build(); |
| 334 | |
| 335 | errMsg = factory.buildPcepErrorMsg().setPcepErrorInfo(errInfo).build(); |
| 336 | return errMsg; |
| 337 | } |
| 338 | |
| 339 | /** |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 340 | * Implementation of an Pcep Agent which is responsible for |
| 341 | * keeping track of connected clients and the state in which |
| 342 | * they are. |
| 343 | */ |
| 344 | public class PcepClientAgent implements PcepAgent { |
| 345 | |
| 346 | private final Logger log = LoggerFactory.getLogger(PcepClientAgent.class); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 347 | |
| 348 | @Override |
| 349 | public boolean addConnectedClient(PccId pccId, PcepClient pc) { |
| 350 | |
| 351 | if (connectedClients.get(pccId) != null) { |
| 352 | log.error("Trying to add connectedClient but found a previous " |
| 353 | + "value for pcc ip: {}", pccId.toString()); |
| 354 | return false; |
| 355 | } else { |
| 356 | log.debug("Added Client {}", pccId.toString()); |
| 357 | connectedClients.put(pccId, pc); |
| 358 | for (PcepClientListener l : pcepClientListener) { |
| 359 | l.clientConnected(pccId); |
| 360 | } |
| 361 | return true; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | @Override |
| 366 | public boolean validActivation(PccId pccId) { |
| 367 | if (connectedClients.get(pccId) == null) { |
| 368 | log.error("Trying to activate client but is not in " |
| 369 | + "connected client: pccIp {}. Aborting ..", pccId.toString()); |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | @Override |
| 377 | public void removeConnectedClient(PccId pccId) { |
| 378 | |
| 379 | connectedClients.remove(pccId); |
| 380 | for (PcepClientListener l : pcepClientListener) { |
| 381 | log.warn("removal for {}", pccId.toString()); |
| 382 | l.clientDisconnected(pccId); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public void processPcepMessage(PccId pccId, PcepMessage m) { |
| 388 | processClientMessage(pccId, m); |
| 389 | } |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 390 | |
| 391 | @Override |
| 392 | public void addNode(PcepClient pc) { |
| 393 | for (PcepNodeListener l : pcepNodeListener) { |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 394 | l.addDevicePcepConfig(pc); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public void deleteNode(PccId pccId) { |
| 400 | for (PcepNodeListener l : pcepNodeListener) { |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 401 | l.deleteDevicePcepConfig(pccId); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 402 | } |
| 403 | } |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 404 | |
| 405 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 406 | @Override |
| 407 | public boolean analyzeSyncMsgList(PccId pccId) { |
| 408 | PcepClient pc = getClient(pccId); |
| 409 | /* |
| 410 | * PLSP_ID is null while tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by PCC and |
| 411 | * in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic path name must be |
| 412 | * carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be included in the LSP object in |
| 413 | * the LSP State Report (PCRpt) message when during a given PCEP session an LSP is "first" reported to a |
| 414 | * PCE. So two separate lists with separate keys are maintained. |
| 415 | */ |
| 416 | Map<LspKey, Tunnel> preSyncLspDbByKey = new HashMap<>(); |
| 417 | Map<String, Tunnel> preSyncLspDbByName = new HashMap<>(); |
| 418 | |
| 419 | // Query tunnel service and fetch all the tunnels with this PCC as ingress. |
| 420 | // Organize into two maps, with LSP key if known otherwise with symbolic path name, for quick search. |
| 421 | Collection<Tunnel> queriedTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS); |
| 422 | for (Tunnel tunnel : queriedTunnels) { |
| 423 | if (((IpTunnelEndPoint) tunnel.src()).ip().equals(pccId.ipAddress())) { |
| 424 | String pLspId = tunnel.annotations().value(PLSP_ID); |
| 425 | if (pLspId != null) { |
| 426 | String localLspId = tunnel.annotations().value(LOCAL_LSP_ID); |
| 427 | checkNotNull(localLspId); |
| 428 | LspKey lspKey = new LspKey(Integer.valueOf(pLspId), Short.valueOf(localLspId)); |
| 429 | preSyncLspDbByKey.put(lspKey, tunnel); |
| 430 | } else { |
| 431 | preSyncLspDbByName.put(tunnel.tunnelName().value(), tunnel); |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | List<PcepStateReport> syncStateRptList = pc.getSyncMsgList(pccId); |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 437 | if (syncStateRptList == null) { |
| 438 | // When there are no LSPs to sync, directly end-of-sync PCRpt will come and the |
| 439 | // list will be null. |
| 440 | syncStateRptList = Collections.EMPTY_LIST; |
| 441 | } |
| 442 | |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 443 | Iterator<PcepStateReport> stateRptListIterator = syncStateRptList.iterator(); |
| 444 | |
| 445 | // For every report, fetch PLSP id, local LSP id and symbolic path name from the message. |
Avantika-Huawei | fc10dca | 2016-06-10 16:13:55 +0530 | [diff] [blame] | 446 | while (stateRptListIterator.hasNext()) { |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 447 | PcepStateReport stateRpt = stateRptListIterator.next(); |
| 448 | Tunnel tunnel = null; |
| 449 | |
| 450 | PcepLspObject lspObj = stateRpt.getLspObject(); |
| 451 | ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator(); |
| 452 | StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null; |
| 453 | SymbolicPathNameTlv pathNameTlv = null; |
| 454 | |
| 455 | while (listTlvIterator.hasNext()) { |
| 456 | PcepValueType tlv = listTlvIterator.next(); |
| 457 | switch (tlv.getType()) { |
| 458 | case StatefulIPv4LspIdentifiersTlv.TYPE: |
| 459 | ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv; |
| 460 | break; |
| 461 | |
| 462 | case SymbolicPathNameTlv.TYPE: |
| 463 | pathNameTlv = (SymbolicPathNameTlv) tlv; |
| 464 | break; |
| 465 | |
| 466 | default: |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | LspKey lspKeyOfRpt = new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()); |
| 472 | tunnel = preSyncLspDbByKey.get(lspKeyOfRpt); |
| 473 | // PCE tunnel is matched with PCRpt LSP. Now delete it from the preSyncLspDb list as the residual |
| 474 | // non-matching list will be processed at the end. |
| 475 | if (tunnel != null) { |
| 476 | preSyncLspDbByKey.remove(lspKeyOfRpt); |
| 477 | } else if (pathNameTlv != null) { |
| 478 | tunnel = preSyncLspDbByName.get(Arrays.toString(pathNameTlv.getValue())); |
| 479 | if (tunnel != null) { |
| 480 | preSyncLspDbByName.remove(tunnel.tunnelName()); |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | if (tunnel == null) { |
| 485 | // If remove flag is set, and tunnel is not known to PCE, ignore it. |
| 486 | if (lspObj.getCFlag() && !lspObj.getRFlag()) { |
| 487 | // For initiated LSP, need to send PCInit delete msg. |
| 488 | try { |
| 489 | PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest() |
| 490 | .setLspObject(lspObj).build(); |
| 491 | LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList |
| 492 | = new LinkedList<PcInitiatedLspRequest>(); |
| 493 | llPcInitiatedLspRequestList.add(releaseLspRequest); |
| 494 | |
| 495 | PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg() |
| 496 | .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build(); |
| 497 | |
| 498 | for (PcepEventListener l : pcepEventListener) { |
| 499 | l.handleEndOfSyncAction(pccId, pcInitiateMsg, SEND_DELETE); |
| 500 | } |
| 501 | |
| 502 | } catch (PcepParseException e) { |
| 503 | log.error("Exception occured while sending initiate delete message {}", e.getMessage()); |
| 504 | } |
| 505 | } |
| 506 | continue; |
| 507 | } |
| 508 | |
| 509 | if (!lspObj.getCFlag()) { |
| 510 | // For learned LSP process both add/update PCRpt. |
| 511 | LinkedList<PcepStateReport> llPcRptList = new LinkedList<>(); |
| 512 | llPcRptList.add(stateRpt); |
| 513 | PcepMessage pcReportMsg = pc.factory().buildReportMsg().setStateReportList((llPcRptList)) |
| 514 | .build(); |
| 515 | |
| 516 | for (PcepEventListener l : pcepEventListener) { |
| 517 | l.handleMessage(pccId, pcReportMsg); |
| 518 | } |
| 519 | continue; |
| 520 | } |
| 521 | |
| 522 | // Implied that tunnel != null and lspObj.getCFlag() is set |
| 523 | // State different for PCC sent LSP and PCE known LSP, send PCUpd msg. |
| 524 | State tunnelState = PcepLspStatus |
| 525 | .getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]); |
| 526 | if (tunnelState != tunnel.state()) { |
| 527 | for (PcepEventListener l : pcepEventListener) { |
| 528 | l.handleEndOfSyncAction(tunnel, SEND_UPDATE); |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | // Check which tunnels are extra at PCE that were not reported by PCC. |
| 534 | Map<Object, Tunnel> preSyncLspDb = (Map) preSyncLspDbByKey; |
| 535 | handleResidualTunnels(preSyncLspDb); |
| 536 | preSyncLspDbByKey = null; |
| 537 | |
| 538 | preSyncLspDb = (Map) preSyncLspDbByName; |
| 539 | handleResidualTunnels(preSyncLspDb); |
| 540 | preSyncLspDbByName = null; |
| 541 | preSyncLspDb = null; |
| 542 | |
| 543 | pc.removeSyncMsgList(pccId); |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Go through the tunnels which are known by PCE but were not reported by PCC during LSP DB sync and take |
| 549 | * appropriate actions. |
| 550 | */ |
| 551 | private void handleResidualTunnels(Map<Object, Tunnel> preSyncLspDb) { |
| 552 | for (Tunnel pceExtraTunnel : preSyncLspDb.values()) { |
| 553 | if (pceExtraTunnel.annotations().value(PCE_INIT) == null |
| 554 | || "false".equalsIgnoreCase(pceExtraTunnel.annotations().value(PCE_INIT))) { |
| 555 | // PCC initiated tunnels should be removed from tunnel store. |
| 556 | for (PcepEventListener l : pcepEventListener) { |
| 557 | l.handleEndOfSyncAction(pceExtraTunnel, REMOVE); |
| 558 | } |
| 559 | } else { |
| 560 | // PCE initiated tunnels should be initiated again. |
| 561 | for (PcepEventListener l : pcepEventListener) { |
| 562 | l.handleEndOfSyncAction(pceExtraTunnel, UNSTABLE); |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | } |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 567 | } |
| 568 | } |