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