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