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 | |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 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; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 22 | import java.util.Set; |
| 23 | import java.util.concurrent.ConcurrentHashMap; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame^] | 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 30 | import org.apache.felix.scr.annotations.Service; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame^] | 31 | import org.onosproject.net.device.DeviceService; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 32 | import org.onosproject.pcep.controller.PccId; |
| 33 | import org.onosproject.pcep.controller.PcepClient; |
| 34 | import org.onosproject.pcep.controller.PcepClientController; |
| 35 | import org.onosproject.pcep.controller.PcepClientListener; |
| 36 | import org.onosproject.pcep.controller.PcepEventListener; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame^] | 37 | import org.onosproject.pcep.controller.PcepNodeListener; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 38 | import org.onosproject.pcep.controller.driver.PcepAgent; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 39 | import org.onosproject.pcepio.protocol.PcepError; |
| 40 | import org.onosproject.pcepio.protocol.PcepErrorInfo; |
| 41 | import org.onosproject.pcepio.protocol.PcepErrorMsg; |
| 42 | import org.onosproject.pcepio.protocol.PcepErrorObject; |
| 43 | import org.onosproject.pcepio.protocol.PcepFactory; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 44 | import org.onosproject.pcepio.protocol.PcepMessage; |
| 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
| 48 | import com.google.common.collect.Sets; |
| 49 | |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 50 | import static org.onosproject.pcepio.types.PcepErrorDetailInfo.ERROR_TYPE_19; |
| 51 | import static org.onosproject.pcepio.types.PcepErrorDetailInfo.ERROR_VALUE_5; |
| 52 | |
Phanendra Manda | 51fb9c2 | 2015-09-01 16:17:41 +0530 | [diff] [blame] | 53 | /** |
| 54 | * Implementation of PCEP client controller. |
| 55 | */ |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 56 | @Component(immediate = true) |
| 57 | @Service |
| 58 | public class PcepClientControllerImpl implements PcepClientController { |
| 59 | |
| 60 | private static final Logger log = LoggerFactory.getLogger(PcepClientControllerImpl.class); |
| 61 | |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame^] | 62 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 63 | protected DeviceService deviceService; |
| 64 | |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 65 | protected ConcurrentHashMap<PccId, PcepClient> connectedClients = |
Sho SHIMIZU | 9b8274c | 2015-09-04 15:54:24 -0700 | [diff] [blame] | 66 | new ConcurrentHashMap<>(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 67 | |
| 68 | protected PcepClientAgent agent = new PcepClientAgent(); |
| 69 | protected Set<PcepClientListener> pcepClientListener = new HashSet<>(); |
| 70 | |
| 71 | protected Set<PcepEventListener> pcepEventListener = Sets.newHashSet(); |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame^] | 72 | protected Set<PcepNodeListener> pcepNodeListener = Sets.newHashSet(); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 73 | |
| 74 | private final Controller ctrl = new Controller(); |
| 75 | |
| 76 | @Activate |
| 77 | public void activate() { |
| 78 | ctrl.start(agent); |
| 79 | log.info("Started"); |
| 80 | } |
| 81 | |
| 82 | @Deactivate |
| 83 | public void deactivate() { |
| 84 | // Close all connected clients |
| 85 | closeConnectedClients(); |
| 86 | ctrl.stop(); |
| 87 | log.info("Stopped"); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Collection<PcepClient> getClients() { |
| 92 | return connectedClients.values(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public PcepClient getClient(PccId pccId) { |
| 97 | return connectedClients.get(pccId); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void addListener(PcepClientListener listener) { |
| 102 | if (!pcepClientListener.contains(listener)) { |
| 103 | this.pcepClientListener.add(listener); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void removeListener(PcepClientListener listener) { |
| 109 | this.pcepClientListener.remove(listener); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void addEventListener(PcepEventListener listener) { |
| 114 | pcepEventListener.add(listener); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public void removeEventListener(PcepEventListener listener) { |
| 119 | pcepEventListener.remove(listener); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void writeMessage(PccId pccId, PcepMessage msg) { |
| 124 | this.getClient(pccId).sendMessage(msg); |
| 125 | } |
| 126 | |
| 127 | @Override |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame^] | 128 | public void addNodeListener(PcepNodeListener listener) { |
| 129 | pcepNodeListener.add(listener); |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public void removeNodeListener(PcepNodeListener listener) { |
| 134 | pcepNodeListener.remove(listener); |
| 135 | } |
| 136 | |
| 137 | @Override |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 138 | public void processClientMessage(PccId pccId, PcepMessage msg) { |
| 139 | PcepClient pc = getClient(pccId); |
| 140 | |
| 141 | switch (msg.getType()) { |
| 142 | case NONE: |
| 143 | break; |
| 144 | case OPEN: |
| 145 | break; |
| 146 | case KEEP_ALIVE: |
| 147 | break; |
| 148 | case PATH_COMPUTATION_REQUEST: |
| 149 | break; |
| 150 | case PATH_COMPUTATION_REPLY: |
| 151 | break; |
| 152 | case NOTIFICATION: |
| 153 | break; |
| 154 | case ERROR: |
| 155 | break; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 156 | case INITIATE: |
| 157 | if (!pc.capability().pcInstantiationCapability()) { |
| 158 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), ERROR_TYPE_19, |
| 159 | ERROR_VALUE_5))); |
| 160 | } |
| 161 | break; |
| 162 | case UPDATE: |
| 163 | if (!pc.capability().statefulPceCapability()) { |
| 164 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), ERROR_TYPE_19, |
| 165 | ERROR_VALUE_5))); |
| 166 | } |
| 167 | break; |
| 168 | case LABEL_UPDATE: |
| 169 | if (!pc.capability().pceccCapability()) { |
| 170 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), ERROR_TYPE_19, |
| 171 | ERROR_VALUE_5))); |
| 172 | } |
| 173 | break; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 174 | case CLOSE: |
| 175 | log.info("Sending Close Message to {" + pccId.toString() + "}"); |
| 176 | pc.sendMessage(Collections.singletonList(pc.factory().buildCloseMsg().build())); |
| 177 | //now disconnect client |
| 178 | pc.disconnectClient(); |
| 179 | break; |
| 180 | case REPORT: |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 181 | //Only update the listener if respective capability is supported else send PCEP-ERR msg |
| 182 | if (pc.capability().statefulPceCapability()) { |
| 183 | for (PcepEventListener l : pcepEventListener) { |
| 184 | l.handleMessage(pccId, msg); |
| 185 | } |
| 186 | } else { |
| 187 | // Send PCEP-ERROR message. |
| 188 | pc.sendMessage(Collections.singletonList(getErrMsg(pc.factory(), |
| 189 | ERROR_TYPE_19, ERROR_VALUE_5))); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 190 | } |
| 191 | break; |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 192 | case LABEL_RANGE_RESERV: |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 193 | break; |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame^] | 194 | case LS_REPORT: //TODO: need to handle LS report to add or remove node |
| 195 | break; |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 196 | case MAX: |
| 197 | break; |
| 198 | case END: |
| 199 | break; |
| 200 | default: |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | @Override |
| 206 | public void closeConnectedClients() { |
| 207 | PcepClient pc; |
| 208 | for (PccId id : connectedClients.keySet()) { |
| 209 | pc = getClient(id); |
| 210 | pc.disconnectClient(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
Priyanka B | d2b2888 | 2016-04-04 16:57:04 +0530 | [diff] [blame] | 215 | * Returns pcep error message with specific error type and value. |
| 216 | * |
| 217 | * @param factory represents pcep factory |
| 218 | * @param errorType pcep error type |
| 219 | * @param errorValue pcep error value |
| 220 | * @return pcep error message |
| 221 | */ |
| 222 | public PcepErrorMsg getErrMsg(PcepFactory factory, byte errorType, byte errorValue) { |
| 223 | LinkedList<PcepError> llPcepErr = new LinkedList<>(); |
| 224 | |
| 225 | LinkedList<PcepErrorObject> llerrObj = new LinkedList<>(); |
| 226 | PcepErrorMsg errMsg; |
| 227 | |
| 228 | PcepErrorObject errObj = factory.buildPcepErrorObject().setErrorValue(errorValue).setErrorType(errorType) |
| 229 | .build(); |
| 230 | |
| 231 | llerrObj.add(errObj); |
| 232 | PcepError pcepErr = factory.buildPcepError().setErrorObjList(llerrObj).build(); |
| 233 | |
| 234 | llPcepErr.add(pcepErr); |
| 235 | |
| 236 | PcepErrorInfo errInfo = factory.buildPcepErrorInfo().setPcepErrorList(llPcepErr).build(); |
| 237 | |
| 238 | errMsg = factory.buildPcepErrorMsg().setPcepErrorInfo(errInfo).build(); |
| 239 | return errMsg; |
| 240 | } |
| 241 | |
| 242 | /** |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 243 | * Implementation of an Pcep Agent which is responsible for |
| 244 | * keeping track of connected clients and the state in which |
| 245 | * they are. |
| 246 | */ |
| 247 | public class PcepClientAgent implements PcepAgent { |
| 248 | |
| 249 | private final Logger log = LoggerFactory.getLogger(PcepClientAgent.class); |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 250 | |
| 251 | @Override |
| 252 | public boolean addConnectedClient(PccId pccId, PcepClient pc) { |
| 253 | |
| 254 | if (connectedClients.get(pccId) != null) { |
| 255 | log.error("Trying to add connectedClient but found a previous " |
| 256 | + "value for pcc ip: {}", pccId.toString()); |
| 257 | return false; |
| 258 | } else { |
| 259 | log.debug("Added Client {}", pccId.toString()); |
| 260 | connectedClients.put(pccId, pc); |
| 261 | for (PcepClientListener l : pcepClientListener) { |
| 262 | l.clientConnected(pccId); |
| 263 | } |
| 264 | return true; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public boolean validActivation(PccId pccId) { |
| 270 | if (connectedClients.get(pccId) == null) { |
| 271 | log.error("Trying to activate client but is not in " |
| 272 | + "connected client: pccIp {}. Aborting ..", pccId.toString()); |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public void removeConnectedClient(PccId pccId) { |
| 281 | |
| 282 | connectedClients.remove(pccId); |
| 283 | for (PcepClientListener l : pcepClientListener) { |
| 284 | log.warn("removal for {}", pccId.toString()); |
| 285 | l.clientDisconnected(pccId); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public void processPcepMessage(PccId pccId, PcepMessage m) { |
| 291 | processClientMessage(pccId, m); |
| 292 | } |
Priyanka B | 94395bf | 2016-05-21 18:39:46 +0530 | [diff] [blame^] | 293 | |
| 294 | @Override |
| 295 | public void addNode(PcepClient pc) { |
| 296 | for (PcepNodeListener l : pcepNodeListener) { |
| 297 | l.addNode(pc); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | @Override |
| 302 | public void deleteNode(PccId pccId) { |
| 303 | for (PcepNodeListener l : pcepNodeListener) { |
| 304 | l.deleteNode(pccId); |
| 305 | } |
| 306 | } |
SureshBR | 25058b7 | 2015-08-13 13:05:06 +0530 | [diff] [blame] | 307 | } |
| 308 | } |