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