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