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