Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [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 | */ |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 16 | package org.onosproject.provider.pcep.tunnel.impl; |
| 17 | |
| 18 | import java.util.Collection; |
| 19 | import java.util.Collections; |
| 20 | import java.util.HashSet; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 21 | import java.util.LinkedList; |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 22 | import java.util.Set; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
| 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Deactivate; |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 27 | import org.onosproject.pcep.controller.PccId; |
| 28 | import org.onosproject.pcep.controller.PcepClient; |
| 29 | import org.onosproject.pcep.controller.PcepClientController; |
| 30 | import org.onosproject.pcep.controller.PcepClientListener; |
| 31 | import org.onosproject.pcep.controller.PcepEventListener; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 32 | import org.onosproject.pcep.controller.PcepNodeListener; |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 33 | import org.onosproject.pcep.controller.PcepPacketListener; |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 34 | import org.onosproject.pcep.controller.driver.PcepAgent; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 35 | import org.onosproject.pcepio.protocol.PcepError; |
| 36 | import org.onosproject.pcepio.protocol.PcepErrorInfo; |
| 37 | import org.onosproject.pcepio.protocol.PcepErrorMsg; |
| 38 | import org.onosproject.pcepio.protocol.PcepErrorObject; |
| 39 | import org.onosproject.pcepio.protocol.PcepFactory; |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 40 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 41 | import org.onosproject.pcepio.protocol.PcepVersion; |
| 42 | |
| 43 | import com.google.common.collect.Sets; |
| 44 | |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 45 | import static org.onosproject.pcepio.types.PcepErrorDetailInfo.ERROR_TYPE_19; |
| 46 | import static org.onosproject.pcepio.types.PcepErrorDetailInfo.ERROR_VALUE_5; |
| 47 | |
| 48 | /** |
| 49 | * Representation of PCEP client controller adapter. |
| 50 | */ |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 51 | public class PcepClientControllerAdapter implements PcepClientController { |
| 52 | |
| 53 | protected ConcurrentHashMap<PccId, PcepClient> connectedClients = |
| 54 | new ConcurrentHashMap<PccId, PcepClient>(); |
| 55 | |
| 56 | protected PcepClientAgent agent = new PcepClientAgent(); |
| 57 | protected Set<PcepClientListener> pcepClientListener = new HashSet<>(); |
| 58 | |
| 59 | protected Set<PcepEventListener> pcepEventListener = Sets.newHashSet(); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 60 | public Set<PcepNodeListener> pcepNodeListener = Sets.newHashSet(); |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 61 | protected Set<PcepPacketListener> pcepPacketListener = Sets.newHashSet(); |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 62 | |
| 63 | @Activate |
| 64 | public void activate() { |
| 65 | } |
| 66 | |
| 67 | @Deactivate |
| 68 | public void deactivate() { |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public Collection<PcepClient> getClients() { |
| 73 | return connectedClients.values(); |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public PcepClient getClient(PccId pccId) { |
Priyanka B | 413fbe8 | 2016-05-26 11:44:45 +0530 | [diff] [blame] | 78 | if (connectedClients.get(pccId) != null) { |
Avantika-Huawei | 56c1184 | 2016-04-28 00:56:56 +0530 | [diff] [blame] | 79 | return connectedClients.get(pccId); |
| 80 | } |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 81 | PcepClientAdapter pc = new PcepClientAdapter(); |
Priyanka B | 413fbe8 | 2016-05-26 11:44:45 +0530 | [diff] [blame] | 82 | |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 83 | pc.init(PccId.pccId(pccId.ipAddress()), PcepVersion.PCEP_1); |
Avantika-Huawei | 56c1184 | 2016-04-28 00:56:56 +0530 | [diff] [blame] | 84 | connectedClients.put(pccId, pc); |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 85 | return pc; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void addListener(PcepClientListener listener) { |
| 90 | if (!pcepClientListener.contains(listener)) { |
| 91 | this.pcepClientListener.add(listener); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 96 | public void addNodeListener(PcepNodeListener listener) { |
| 97 | pcepNodeListener.add(listener); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void removeNodeListener(PcepNodeListener listener) { |
| 102 | pcepNodeListener.remove(listener); |
| 103 | } |
| 104 | |
| 105 | @Override |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 106 | public void removeListener(PcepClientListener listener) { |
| 107 | this.pcepClientListener.remove(listener); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void addEventListener(PcepEventListener listener) { |
| 112 | pcepEventListener.add(listener); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public void removeEventListener(PcepEventListener listener) { |
| 117 | pcepEventListener.remove(listener); |
| 118 | } |
| 119 | |
| 120 | @Override |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 121 | public void addPacketListener(PcepPacketListener listener) { |
| 122 | pcepPacketListener.add(listener); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void removePacketListener(PcepPacketListener listener) { |
| 127 | pcepPacketListener.remove(listener); |
| 128 | } |
| 129 | |
| 130 | @Override |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 131 | public void writeMessage(PccId pccId, PcepMessage msg) { |
| 132 | this.getClient(pccId).sendMessage(msg); |
| 133 | } |
| 134 | |
| 135 | @Override |
| 136 | public void processClientMessage(PccId pccId, PcepMessage msg) { |
| 137 | |
| 138 | PcepClient pc = getClient(pccId); |
| 139 | |
| 140 | switch (msg.getType()) { |
| 141 | case NONE: |
| 142 | break; |
| 143 | case OPEN: |
| 144 | break; |
| 145 | case KEEP_ALIVE: |
| 146 | //log.debug("Sending Keep Alive Message to {" + pccIpAddress.toString() + "}"); |
| 147 | pc.sendMessage(Collections.singletonList(pc.factory().buildKeepaliveMsg().build())); |
| 148 | break; |
| 149 | case PATH_COMPUTATION_REQUEST: |
| 150 | break; |
| 151 | case PATH_COMPUTATION_REPLY: |
| 152 | break; |
| 153 | case NOTIFICATION: |
| 154 | break; |
| 155 | case ERROR: |
| 156 | break; |
| 157 | case CLOSE: |
| 158 | //log.debug("Sending Close Message to { }", pccIpAddress.toString()); |
| 159 | pc.sendMessage(Collections.singletonList(pc.factory().buildCloseMsg().build())); |
| 160 | break; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 161 | case INITIATE: |
| 162 | if (!pc.capability().pcInstantiationCapability()) { |
| 163 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), |
| 164 | ERROR_TYPE_19, ERROR_VALUE_5))); |
| 165 | } |
| 166 | break; |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 167 | case REPORT: |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 168 | //Only update the listener if respective capability is supported else send PCEP-ERR msg |
| 169 | if (pc.capability().statefulPceCapability()) { |
| 170 | for (PcepEventListener l : pcepEventListener) { |
| 171 | l.handleMessage(pccId, msg); |
| 172 | } |
| 173 | } else { |
| 174 | // Send PCEP-ERROR message. |
| 175 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), |
| 176 | ERROR_TYPE_19, ERROR_VALUE_5))); |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 177 | } |
| 178 | break; |
| 179 | case UPDATE: |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 180 | if (!pc.capability().statefulPceCapability()) { |
| 181 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), |
| 182 | ERROR_TYPE_19, ERROR_VALUE_5))); |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 183 | } |
| 184 | break; |
| 185 | case LABEL_UPDATE: |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 186 | if (!pc.capability().pceccCapability()) { |
| 187 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), |
| 188 | ERROR_TYPE_19, ERROR_VALUE_5))); |
| 189 | } |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 190 | break; |
| 191 | case MAX: |
| 192 | break; |
| 193 | case END: |
| 194 | break; |
| 195 | default: |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | @Override |
| 201 | public void closeConnectedClients() { |
| 202 | PcepClient pc; |
| 203 | for (PccId id : connectedClients.keySet()) { |
| 204 | pc = getClient(id); |
| 205 | pc.disconnectClient(); |
| 206 | } |
| 207 | } |
| 208 | |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 209 | private PcepErrorMsg getErrMsg(PcepFactory factory, byte errorType, byte errorValue) { |
| 210 | LinkedList<PcepError> llPcepErr = new LinkedList<>(); |
| 211 | |
| 212 | LinkedList<PcepErrorObject> llerrObj = new LinkedList<>(); |
| 213 | PcepErrorMsg errMsg; |
| 214 | |
| 215 | PcepErrorObject errObj = factory.buildPcepErrorObject().setErrorValue(errorValue).setErrorType(errorType) |
| 216 | .build(); |
| 217 | |
| 218 | llerrObj.add(errObj); |
| 219 | PcepError pcepErr = factory.buildPcepError().setErrorObjList(llerrObj).build(); |
| 220 | |
| 221 | llPcepErr.add(pcepErr); |
| 222 | |
| 223 | PcepErrorInfo errInfo = factory.buildPcepErrorInfo().setPcepErrorList(llPcepErr).build(); |
| 224 | |
| 225 | errMsg = factory.buildPcepErrorMsg().setPcepErrorInfo(errInfo).build(); |
| 226 | return errMsg; |
| 227 | } |
| 228 | |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 229 | /** |
| 230 | * Implementation of an Pcep Agent which is responsible for |
| 231 | * keeping track of connected clients and the state in which |
| 232 | * they are. |
| 233 | */ |
| 234 | public class PcepClientAgent implements PcepAgent { |
| 235 | |
| 236 | @Override |
| 237 | public boolean addConnectedClient(PccId pccId, PcepClient pc) { |
| 238 | |
| 239 | if (connectedClients.get(pccId) != null) { |
| 240 | return false; |
| 241 | } else { |
| 242 | connectedClients.put(pccId, pc); |
| 243 | for (PcepClientListener l : pcepClientListener) { |
| 244 | l.clientConnected(pccId); |
| 245 | } |
| 246 | return true; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public boolean validActivation(PccId pccId) { |
| 252 | if (connectedClients.get(pccId) == null) { |
| 253 | //log.error("Trying to activate client but is not in " |
| 254 | // + "connected switches: pccIp {}. Aborting ..", pccIpAddress.toString()); |
| 255 | return false; |
| 256 | } |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void removeConnectedClient(PccId pccId) { |
| 263 | connectedClients.remove(pccId); |
| 264 | for (PcepClientListener l : pcepClientListener) { |
| 265 | //log.warn("removal for {}", pccIpAddress.toString()); |
| 266 | l.clientDisconnected(pccId); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void processPcepMessage(PccId pccId, PcepMessage m) { |
| 272 | processClientMessage(pccId, m); |
| 273 | } |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 274 | |
| 275 | @Override |
| 276 | public void addNode(PcepClient pc) { |
| 277 | for (PcepNodeListener l : pcepNodeListener) { |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 278 | l.addDevicePcepConfig(pc); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | @Override |
| 283 | public void deleteNode(PccId pccId) { |
| 284 | for (PcepNodeListener l : pcepNodeListener) { |
Avantika-Huawei | fe44ea6 | 2016-05-27 19:21:24 +0530 | [diff] [blame] | 285 | l.deleteDevicePcepConfig(pccId); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame] | 286 | } |
| 287 | } |
Avantika-Huawei | d1e36bd | 2016-05-26 12:47:16 +0530 | [diff] [blame] | 288 | |
| 289 | @Override |
| 290 | public boolean analyzeSyncMsgList(PccId pccId) { |
| 291 | // TODO Auto-generated method stub |
| 292 | return false; |
| 293 | } |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 294 | } |
Phanendra Manda | 972ee9e | 2015-08-21 19:08:20 +0530 | [diff] [blame] | 295 | } |