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