blob: 648e500b1ae5335a6f09406bb34928eca8b3e926 [file] [log] [blame]
cheng fan48e832c2015-05-29 01:54:47 +08001/*
2 * Copyright 2015 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.tunnel.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
chengfan2fff70f2015-08-24 18:20:19 -050019import static com.google.common.base.Strings.isNullOrEmpty;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053020import static org.onosproject.net.DefaultAnnotations.EMPTY;
chengfan2fff70f2015-08-24 18:20:19 -050021import static org.onlab.util.Tools.get;
cheng fan48e832c2015-05-29 01:54:47 +080022import static org.onosproject.net.DeviceId.deviceId;
23import static org.onosproject.net.PortNumber.portNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053024import static org.onosproject.pcep.api.PcepDpid.uri;
cheng fan48e832c2015-05-29 01:54:47 +080025import static org.slf4j.LoggerFactory.getLogger;
26
27import java.util.ArrayList;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053028import java.util.Collections;
chengfan2fff70f2015-08-24 18:20:19 -050029import java.util.Dictionary;
cheng fan48e832c2015-05-29 01:54:47 +080030import java.util.HashMap;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053031import java.util.LinkedList;
cheng fan48e832c2015-05-29 01:54:47 +080032import java.util.List;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053033import java.util.ListIterator;
cheng fan48e832c2015-05-29 01:54:47 +080034import java.util.Optional;
35
chengfan2fff70f2015-08-24 18:20:19 -050036import com.google.common.collect.Maps;
cheng fan48e832c2015-05-29 01:54:47 +080037import org.apache.felix.scr.annotations.Activate;
38import org.apache.felix.scr.annotations.Component;
39import org.apache.felix.scr.annotations.Deactivate;
chengfan2fff70f2015-08-24 18:20:19 -050040import org.apache.felix.scr.annotations.Property;
cheng fan48e832c2015-05-29 01:54:47 +080041import org.apache.felix.scr.annotations.Reference;
42import org.apache.felix.scr.annotations.ReferenceCardinality;
43import org.apache.felix.scr.annotations.Service;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053044import org.onlab.packet.IpAddress;
chengfan2fff70f2015-08-24 18:20:19 -050045import org.onosproject.cfg.ComponentConfigService;
cheng fan48e832c2015-05-29 01:54:47 +080046import org.onosproject.core.DefaultGroupId;
47import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053048import org.onosproject.incubator.net.tunnel.DefaultTunnel;
cheng fan48e832c2015-05-29 01:54:47 +080049import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053050import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
chengfan2fff70f2015-08-24 18:20:19 -050051import org.onosproject.incubator.net.tunnel.DefaultTunnelStatistics;
cheng fan48e832c2015-05-29 01:54:47 +080052import org.onosproject.incubator.net.tunnel.OpticalLogicId;
53import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
54import org.onosproject.incubator.net.tunnel.Tunnel;
55import org.onosproject.incubator.net.tunnel.TunnelDescription;
56import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
57import org.onosproject.incubator.net.tunnel.TunnelId;
58import org.onosproject.incubator.net.tunnel.TunnelName;
59import org.onosproject.incubator.net.tunnel.TunnelProvider;
60import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
61import org.onosproject.incubator.net.tunnel.TunnelProviderService;
chengfan2fff70f2015-08-24 18:20:19 -050062import org.onosproject.incubator.net.tunnel.TunnelService;
63import org.onosproject.incubator.net.tunnel.TunnelStatistics;
cheng fan48e832c2015-05-29 01:54:47 +080064import org.onosproject.net.ConnectPoint;
65import org.onosproject.net.DefaultAnnotations;
66import org.onosproject.net.DefaultLink;
67import org.onosproject.net.DefaultPath;
68import org.onosproject.net.DeviceId;
69import org.onosproject.net.ElementId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053070import org.onosproject.net.IpElementId;
cheng fan48e832c2015-05-29 01:54:47 +080071import org.onosproject.net.Link;
72import org.onosproject.net.Path;
73import org.onosproject.net.PortNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053074import org.onosproject.net.SparseAnnotations;
cheng fan48e832c2015-05-29 01:54:47 +080075import org.onosproject.net.provider.AbstractProvider;
76import org.onosproject.net.provider.ProviderId;
77import org.onosproject.pcep.api.PcepController;
78import org.onosproject.pcep.api.PcepDpid;
79import org.onosproject.pcep.api.PcepHopNodeDescription;
80import org.onosproject.pcep.api.PcepOperator.OperationType;
81import org.onosproject.pcep.api.PcepTunnel;
82import org.onosproject.pcep.api.PcepTunnel.PATHTYPE;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053083import org.onosproject.pcep.api.PcepTunnel.PathState;
cheng fan48e832c2015-05-29 01:54:47 +080084import org.onosproject.pcep.api.PcepTunnelListener;
chengfan2fff70f2015-08-24 18:20:19 -050085import org.onosproject.pcep.api.PcepTunnelStatistics;
86import org.osgi.service.component.annotations.Modified;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053087import org.onosproject.pcep.controller.PccId;
88import org.onosproject.pcep.controller.PcepClient;
89import org.onosproject.pcep.controller.PcepClientController;
90import org.onosproject.pcep.controller.PcepClientListener;
91import org.onosproject.pcep.controller.PcepEventListener;
92import org.onosproject.pcepio.exceptions.PcepParseException;
93import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
94import org.onosproject.pcepio.protocol.PcepAttribute;
95import org.onosproject.pcepio.protocol.PcepBandwidthObject;
96import org.onosproject.pcepio.protocol.PcepEndPointsObject;
97import org.onosproject.pcepio.protocol.PcepEroObject;
98import org.onosproject.pcepio.protocol.PcepInitiateMsg;
99import org.onosproject.pcepio.protocol.PcepLspObject;
100import org.onosproject.pcepio.protocol.PcepMessage;
101import org.onosproject.pcepio.protocol.PcepMsgPath;
102import org.onosproject.pcepio.protocol.PcepReportMsg;
103import org.onosproject.pcepio.protocol.PcepRroObject;
104import org.onosproject.pcepio.protocol.PcepSrpObject;
105import org.onosproject.pcepio.protocol.PcepStateReport;
106import org.onosproject.pcepio.protocol.PcepUpdateMsg;
107import org.onosproject.pcepio.protocol.PcepUpdateRequest;
108import org.onosproject.pcepio.types.IPv4SubObject;
109import org.onosproject.pcepio.types.PcepValueType;
110import org.onosproject.pcepio.types.StatefulIPv4LspIdentidiersTlv;
111import org.onosproject.pcepio.types.SymbolicPathNameTlv;
cheng fan48e832c2015-05-29 01:54:47 +0800112import org.slf4j.Logger;
chengfan2fff70f2015-08-24 18:20:19 -0500113import org.osgi.service.component.ComponentContext;
cheng fan48e832c2015-05-29 01:54:47 +0800114
115/**
116 * Provider which uses an PCEP controller to detect, update, create network
117 * tunnels.
118 */
119@Component(immediate = true)
120@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530121public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800122
123 private static final Logger log = getLogger(PcepTunnelProvider.class);
124 private static final long MAX_BANDWIDTH = 99999744;
125 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800126 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530127 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
cheng fan48e832c2015-05-29 01:54:47 +0800128
chengfan2fff70f2015-08-24 18:20:19 -0500129 static final int POLL_INTERVAL = 10;
130 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
131 label = "Frequency (in seconds) for polling tunnel statistics")
132 private int tunnelStatsPollFrequency = POLL_INTERVAL;
133
cheng fan48e832c2015-05-29 01:54:47 +0800134 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
135
136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
137 protected TunnelProviderRegistry tunnelProviderRegistry;
138
139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
140 protected PcepController controller;
141
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
143 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500144
145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
146 protected TunnelService tunnelService;
147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
149 protected ComponentConfigService cfgService;
150
cheng fan48e832c2015-05-29 01:54:47 +0800151 TunnelProviderService service;
152
153 HashMap<String, TunnelId> tunnelMap = new HashMap<String, TunnelId>();
chengfan2fff70f2015-08-24 18:20:19 -0500154 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
155 private HashMap<Long, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800156
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530157 private InnerTunnelProvider listener = new InnerTunnelProvider();
158
159 protected PcepTunnelApiMapper pcepTunnelAPIMapper = new PcepTunnelApiMapper();
160 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800161
162 /**
163 * Creates a Tunnel provider.
164 */
165 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530166 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800167 }
168
169 @Activate
170 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500171 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800172 service = tunnelProviderRegistry.register(this);
173 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530174 pcepClientController.addListener(listener);
175 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500176 tunnelService.queryAllTunnels().forEach(tunnel -> {
177 String pcepTunnelId = getPCEPTunnelKey(tunnel.tunnelId());
178 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
179 tsc.start();
180 collectors.put(tunnel.tunnelId().id(), tsc);
181
182 });
183
cheng fan48e832c2015-05-29 01:54:47 +0800184 log.info("Started");
185 }
186
187 @Deactivate
188 public void deactivate() {
189 tunnelProviderRegistry.unregister(this);
190 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500191 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530192 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800193 log.info("Stopped");
194 }
195
chengfan2fff70f2015-08-24 18:20:19 -0500196 @Modified
197 public void modified(ComponentContext context) {
198 Dictionary<?, ?> properties = context.getProperties();
199 int newTunnelStatsPollFrequency;
200 try {
201 String s = get(properties, "tunnelStatsPollFrequency");
202 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
203
204 } catch (NumberFormatException | ClassCastException e) {
205 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
206 }
207
208 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
209 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
210 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
211 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
212 }
213
214 }
215
cheng fan48e832c2015-05-29 01:54:47 +0800216 @Override
217 public void setupTunnel(Tunnel tunnel, Path path) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530218 if (tunnel.type() != Tunnel.Type.MPLS) {
219 log.error("Tunnel Type MPLS is only supported");
220 return;
221 }
cheng fan48e832c2015-05-29 01:54:47 +0800222
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530223 // check for tunnel end points
224 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
225 log.error("Tunnel source or destination is not valid");
226 return;
227 }
228
229 // Get the pcc client
230 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
231
232 if (!(pc instanceof PcepClient)) {
233 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500234 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530235 return;
236 }
237 pcepSetupTunnel(tunnel, path, pc);
cheng fan48e832c2015-05-29 01:54:47 +0800238 }
239
240 @Override
241 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800242
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530243 if (tunnel.type() != Tunnel.Type.MPLS) {
244 log.error("Tunnel Type MPLS is only supported");
245 return;
246 }
247
248 if (!(srcElement instanceof IpElementId)) {
249 log.error("Element id is not valid");
250 return;
251 }
252
253 // check for tunnel end points
254 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
255 log.error("Tunnel source or destination is not valid");
256 return;
257 }
258
259 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
260
261 if (!(pc instanceof PcepClient)) {
262 log.error("There is no PCC connected with ip addresss {}"
263 + ((IpElementId) srcElement).ipAddress().toString());
264 return;
265 }
266 pcepSetupTunnel(tunnel, path, pc);
cheng fan48e832c2015-05-29 01:54:47 +0800267 }
268
269 @Override
270 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800271
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530272 if (tunnel.type() != Tunnel.Type.MPLS) {
273 log.error("Tunnel Type MPLS is only supported");
274 return;
275 }
276
277 // check for tunnel end points
278 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
279 log.error("Tunnel source or destination is not valid");
280 return;
281 }
282
283 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
284
285 if (!(pc instanceof PcepClient)) {
286 log.error("There is no PCC connected with ip addresss {}"
287 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
288 return;
289 }
290 pcepReleaseTunnel(tunnel, pc);
cheng fan48e832c2015-05-29 01:54:47 +0800291 }
292
293 @Override
294 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530295 if (tunnel.type() != Tunnel.Type.MPLS) {
296 log.error("Tunnel Type MPLS is only supported");
297 return;
298 }
cheng fan48e832c2015-05-29 01:54:47 +0800299
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530300 if (!(srcElement instanceof IpElementId)) {
301 log.error("Element id is not valid");
302 return;
303 }
304
305 // check for tunnel end points
306 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
307 log.error("Tunnel source or destination is not valid");
308 return;
309 }
310
311 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
312
313 if (!(pc instanceof PcepClient)) {
314 log.error("There is no PCC connected with ip addresss {}"
315 + ((IpElementId) srcElement).ipAddress().toString());
316 return;
317 }
318 pcepReleaseTunnel(tunnel, pc);
cheng fan48e832c2015-05-29 01:54:47 +0800319 }
320
321 @Override
322 public void updateTunnel(Tunnel tunnel, Path path) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530323 if (tunnel.type() != Tunnel.Type.MPLS) {
324 log.error("Tunnel Type MPLS is only supported");
325 return;
326 }
cheng fan48e832c2015-05-29 01:54:47 +0800327
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530328 // check for tunnel end points
329 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
330 log.error("Tunnel source or destination is not valid");
331 return;
332 }
333
334 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
335
336 if (!(pc instanceof PcepClient)) {
337 log.error("There is no PCC connected with ip addresss {}"
338 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
339 return;
340 }
341 pcepUpdateTunnel(tunnel, path, pc);
cheng fan48e832c2015-05-29 01:54:47 +0800342 }
343
344 @Override
345 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800346
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530347 if (tunnel.type() != Tunnel.Type.MPLS) {
348 log.error("Tunnel Type MPLS is only supported");
349 return;
350 }
351
352 if (!(srcElement instanceof IpElementId)) {
353 log.error("Element id is not valid");
354 return;
355 }
356
357 // check for tunnel end points
358 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
359 log.error("Tunnel source or destination is not valid");
360 return;
361 }
362
363 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
364
365 if (!(pc instanceof PcepClient)) {
366 log.error("There is no PCC connected with ip addresss {}"
367 + ((IpElementId) srcElement).ipAddress().toString());
368 return;
369 }
370 pcepUpdateTunnel(tunnel, path, pc);
cheng fan48e832c2015-05-29 01:54:47 +0800371 }
372
373 @Override
374 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530375 if (tunnel.type() == Tunnel.Type.MPLS) {
376 pcepTunnelAPIMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
377 return service.tunnelAdded(tunnel);
378 }
cheng fan48e832c2015-05-29 01:54:47 +0800379
cheng fan7716ec92015-05-31 01:53:19 +0800380 long bandwidth = Long
381 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800382
383 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800384 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800385 return null;
386 }
387
388 // endpoints
389 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
390 .src();
391 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
392 // devices
393 DeviceId srcId = (DeviceId) src.elementId().get();
394 DeviceId dstId = (DeviceId) dst.elementId().get();
395
396 // ports
397 long srcPort = src.portNumber().get().toLong();
398 long dstPort = dst.portNumber().get().toLong();
399
400 // type
401 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800402 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800403 return null;
404 }
405
406 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
407 dstPort, bandwidth,
408 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530409 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800410
411 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
412 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
413 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
414
415 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
416 return tunnelId;
417 }
418
419 @Override
420 public void tunnelRemoved(TunnelDescription tunnel) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530421 if (tunnel.type() == Tunnel.Type.MPLS) {
422 pcepTunnelAPIMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
423 service.tunnelRemoved(tunnel);
424 }
425
cheng fan48e832c2015-05-29 01:54:47 +0800426 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
427 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
428 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800429 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800430 return;
431 }
432 String pcepTunnelId = getPCEPTunnelKey(tunnel.id());
433 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800434 if (!controller.deleteTunnel(pcepTunnelId)) {
435 error("Delete tunnel failed, Maybe some devices have been disconnected.");
436 return;
cheng fan48e832c2015-05-29 01:54:47 +0800437 }
438 tunnelMap.remove(pcepTunnelId);
439 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800440 }
441
442 @Override
443 public void tunnelUpdated(TunnelDescription tunnel) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530444 if (tunnel.type() == Tunnel.Type.MPLS) {
445 pcepTunnelAPIMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
446 service.tunnelUpdated(tunnel);
447 }
cheng fan48e832c2015-05-29 01:54:47 +0800448
449 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
450 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800451 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800452 return;
453 }
cheng fan7716ec92015-05-31 01:53:19 +0800454 long bandwidth = Long
455 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800456 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800457 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800458 return;
459 }
460 String pcepTunnelId = getPCEPTunnelKey(tunnel.id());
461
462 checkNotNull(pcepTunnelId, "Invalid tunnel id");
463 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
464
cheng fan7716ec92015-05-31 01:53:19 +0800465 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800466 return;
467
468 }
469 service.tunnelUpdated(tunnel);
470 }
471
cheng fan7716ec92015-05-31 01:53:19 +0800472 private void error(String info) {
473 System.err.println(info);
474 }
475
cheng fan48e832c2015-05-29 01:54:47 +0800476 // Short-hand for creating a connection point.
477 private ConnectPoint connectPoint(PcepDpid id, long port) {
478 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
479 }
480
481 // Short-hand for creating a link.
482 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
483 return new DefaultLink(id(), connectPoint(src, sp), connectPoint(dst,
484 dp),
485 Link.Type.TUNNEL);
486 }
487
488 // Creates a path that leads through the given devices.
489 private Path createPath(List<PcepHopNodeDescription> hopList,
cheng fan93258c72015-06-02 23:42:32 +0800490 PATHTYPE pathtype, PathState pathState) {
cheng fan48e832c2015-05-29 01:54:47 +0800491 if (hopList == null || hopList.size() == 0) {
492 return null;
493 }
494 List<Link> links = new ArrayList<>();
495 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
496 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
497 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
498 .get(i + 1).getPortNum()));
499 }
500
501 int hopNum = hopList.size() - 2;
502 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
503 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800504 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800505 .set("pathType", String.valueOf(pathtype)).build();
506 return new DefaultPath(id(), links, hopNum, extendAnnotations);
507 }
508
509 // convert the path description to a string.
510 public String pathToString(List<Link> links) {
511 StringBuilder builder = new StringBuilder();
512 builder.append("{");
513 for (Link link : links) {
514 builder.append("(Device:" + link.src().deviceId() + " Port:"
515 + link.src().port().toLong());
516 builder.append(" Device:" + link.dst().deviceId() + " Port:"
517 + link.dst().port().toLong());
518 builder.append(")");
519 }
520 builder.append("}");
521 return builder.toString();
522 }
523
524 // build a TunnelDescription.
525 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
526 TunnelId tunnelId) {
527 TunnelEndPoint srcPoint = null;
528 TunnelEndPoint dstPoint = null;
529 Tunnel.Type tunnelType = null;
530 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
531
532 // add path after codes of tunnel's path merged
533 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800534 pcepTunnel.getPathType(),
535 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800536
537 OpticalTunnelEndPoint.Type endPointType = null;
538 switch (pcepTunnel.type()) {
539 case OCH:
540 tunnelType = Tunnel.Type.OCH;
541 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
542 break;
543
544 case OTN:
545 tunnelType = Tunnel.Type.ODUK;
546 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
547 break;
548
549 case UNI:
550 tunnelType = Tunnel.Type.VLAN;
551 endPointType = null;
552 break;
553
554 default:
555 break;
556 }
557 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
558 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
559 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
560 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
561
562 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
563 Optional.of(srcPort), null,
564 endPointType,
565 OpticalLogicId.logicId(0),
566 true);
567 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
568 Optional.of(dstPort), null,
569 endPointType,
570 OpticalLogicId.logicId(0),
571 true);
572
573 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800574 DefaultAnnotations annotations = DefaultAnnotations
575 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800576 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800577 .set("bandwidth",
578 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800579 .set("index", String.valueOf(pcepTunnel.id())).build();
580
cheng fan48e832c2015-05-29 01:54:47 +0800581 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
582 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800583 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800584 DefaultAnnotations extendAnnotations = DefaultAnnotations
585 .builder()
586 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800587 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800588 annotations = DefaultAnnotations.merge(annotations,
589 extendAnnotations);
590
591 }
592 TunnelDescription tunnel = new DefaultTunnelDescription(
593 tunnelId,
594 srcPoint,
595 dstPoint,
596 tunnelType,
597 new DefaultGroupId(
598 0),
599 id(), name,
600 path,
601 annotations);
602 return tunnel;
603
604 }
605
606 /**
607 * Get the tunnelID according to the tunnel key.
608 *
609 * @param tunnelKey tunnel key
610 * @return corresponding tunnel id of the a tunnel key.
611 */
612 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800613 for (String key : tunnelMap.keySet()) {
614 if (key.equals(tunnelKey)) {
615 return tunnelMap.get(key);
616 }
617 }
618 return null;
619 }
620
621 /**
622 * Get the tunnel key according to the tunnelID.
623 *
624 * @param tunnelId tunnel id
625 * @return corresponding a tunnel key of the tunnel id.
626 */
627 private String getPCEPTunnelKey(TunnelId tunnelId) {
628 for (String key : tunnelMap.keySet()) {
629 if (tunnelMap.get(key).id() == tunnelId.id()) {
630 return key;
631 }
632 }
633 return null;
634
635 }
636
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530637 /**
chengfan2fff70f2015-08-24 18:20:19 -0500638 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
639 *
640 * @param statistics statistics data from a PCEP tunnel
641 * @return TunnelStatistics
642 */
643 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
644 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
645 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
646 .setPacketLossRatio(statistics.packetLossRate())
647 .setFlowDelay(statistics.flowDelay())
648 .setAlarms(statistics.alarms())
649 .build();
650 return tunnelStatistics;
651 }
652 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530653 * Creates list of hops for ERO object from Path.
654 *
655 * @param path network path
656 * @return list of ipv4 subobjects
657 */
658 private LinkedList<PcepValueType> createPcepPath(Path path) {
659 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
660 List<Link> listLink = path.links();
661 ConnectPoint source = null;
662 ConnectPoint destination = null;
663 IpAddress ipDstAddress = null;
664 IpAddress ipSrcAddress = null;
665 PcepValueType subObj = null;
666
667 for (Link link : listLink) {
668 source = link.src();
669 if (!(source.equals(destination))) {
670 //set IPv4SubObject for ERO object
671 ipSrcAddress = source.ipElementId().ipAddress();
672 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
673 llSubObjects.add(subObj);
674 }
675
676 destination = link.dst();
677 ipDstAddress = destination.ipElementId().ipAddress();
678 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
679 llSubObjects.add(subObj);
680 }
681 return llSubObjects;
682 }
683
684 /**
685 * Creates PcInitiated lsp request list for setup tunnel.
686 *
687 * @param tunnel mpls tunnel
688 * @param path network path
689 * @param pc pcep client
690 * @param srpId unique id for pcep message
691 * @return list of PcInitiatedLspRequest
692 * @throws PcepParseException while building pcep objects fails
693 */
694 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
695 PcepClient pc, int srpId)
696 throws PcepParseException {
697 PcepValueType tlv;
698 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
699
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700700 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530701 log.error("There is no link information to create tunnel");
702 return null;
703 }
704
705 //build SRP object
706 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false).build();
707
708 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
709 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
710 // set LSP identifiers TLV
711 tlv = new StatefulIPv4LspIdentidiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
712 (short) 0, (short) 0, 0,
713 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
714 llOptionalTlv.add(tlv);
715 //set SymbolicPathNameTlv of LSP object
716 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
717 llOptionalTlv.add(tlv);
718
719 //build LSP object
720 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setOFlag((byte) 0).setPlspId(0)
721 .setOptionalTlv(llOptionalTlv).build();
722
723 //build ENDPOINTS object
724 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
725 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
726 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
727 .setPFlag(true).build();
728
729 //build ERO object
730 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
731
732 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700733 if (tunnel.annotations().value("bandwidth") != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530734 iBandwidth = Integer.parseInt(tunnel.annotations().value("bandwidth"));
735 }
736 // build bandwidth object
737 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
738 // build pcep attribute
739 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
740
741 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
742 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
743 .setPcepAttribute(pcepAttribute).build();
744 llPcInitiatedLspRequestList.add(initiateLspRequest);
745 return llPcInitiatedLspRequestList;
746 }
747
748 /**
749 * To send initiate tunnel message to pcc.
750 *
751 * @param tunnel mpls tunnel info
752 * @param path explicit route for the tunnel
753 * @param pc pcep client to send message
754 */
755 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
756 try {
757 int srpId = SrpIdGenerators.create();
758 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, RequestType.CREATE);
759
760 pcepTunnelAPIMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
761
762 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
763 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700764 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530765 log.error("Failed to create PcInitiatedLspRequestList");
766 return;
767 }
768
769 //build PCInitiate message
770 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
771 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
772 .build();
773
774 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
775
776 pcepTunnelAPIMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
777 } catch (PcepParseException e) {
778 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
779 }
780 }
781
782 /**
783 * To send Release tunnel message to pcc.
784 *
785 * @param tunnel mpls tunnel info
786 * @param pc pcep client to send message
787 */
788 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
789 try {
790 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, RequestType.DELETE);
791 pcepTunnelAPIMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
792 int srpId = SrpIdGenerators.create();
793 TunnelId tunnelId = tunnel.tunnelId();
794 int plspId = 0;
795 StatefulIPv4LspIdentidiersTlv statefulIpv4IndentifierTlv = null;
796
797 if (!(pcepTunnelAPIMapper.checkFromTunnelDBQueue(tunnelId))) {
798 log.error("Tunnel doesnot exists. Tunnel id {}" + tunnelId.toString());
799 return;
800 } else {
801 PcepTunnelData pcepTunnelDbData = pcepTunnelAPIMapper.getDataFromTunnelDBQueue(tunnelId);
802 plspId = pcepTunnelDbData.plspId();
803 statefulIpv4IndentifierTlv = pcepTunnelDbData.statefulIpv4IndentifierTlv();
804 }
805 // build srp object
806 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true).build();
807
808 PcepValueType tlv;
809 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
810 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
811
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700812 if (statefulIpv4IndentifierTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530813 tlv = statefulIpv4IndentifierTlv;
814 } else {
815 tlv = new StatefulIPv4LspIdentidiersTlv((
816 ((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
817 (short) 0, (short) 0, 0,
818 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
819 }
820 llOptionalTlv.add(tlv);
821 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
822 llOptionalTlv.add(tlv);
823 // build lsp object, set r flag as false to delete the tunnel
824 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
825 .setOptionalTlv(llOptionalTlv).build();
826
827 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
828 .setLspObject(lspobj).build();
829
830 llPcInitiatedLspRequestList.add(releaseLspRequest);
831
832 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
833 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
834
835 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
836
837 pcepTunnelAPIMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
838 } catch (PcepParseException e) {
839 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
840 }
841 }
842
843 /**
844 * To send Update tunnel request message to pcc.
845 *
846 * @param tunnel mpls tunnel info
847 * @param path explicit route for the tunnel
848 * @param pc pcep client to send message
849 */
850 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
851 try {
852 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, RequestType.UPDATE);
853 pcepTunnelAPIMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
854 int srpId = SrpIdGenerators.create();
855 TunnelId tunnelId = tunnel.tunnelId();
856 PcepValueType tlv;
857 int plspId = 0;
858
859 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
860 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
861 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
862
863 //build SRP object
864 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false).build();
865
866 if (!(pcepTunnelAPIMapper.checkFromTunnelDBQueue(tunnelId))) {
867 log.error("Tunnel doesnot exists in DB");
868 return;
869 } else {
870 PcepTunnelData pcepTunnelDBData = pcepTunnelAPIMapper.getDataFromTunnelDBQueue(tunnelId);
871 plspId = pcepTunnelDBData.plspId();
872 }
873
874 tlv = new StatefulIPv4LspIdentidiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
875 (short) 0, (short) 0, 0,
876 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
877 llOptionalTlv.add(tlv);
878
879 if (tunnel.tunnelName().value() != null) {
880 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
881 llOptionalTlv.add(tlv);
882 }
883
884 // build lsp object
885 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setPlspId(plspId)
886 .setOptionalTlv(llOptionalTlv).build();
887 // build ero object
888 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
889
890 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700891 if (tunnel.annotations().value("bandwidth") != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530892 iBandwidth = Integer.parseInt(tunnel.annotations().value("bandwidth"));
893 }
894 // build bandwidth object
895 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
896 // build pcep attribute
897 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
898 // build pcep msg path
899 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
900 .build();
901
902 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
903 .setLspObject(lspobj).setMsgPath(msgPath).build();
904
905 llUpdateRequestList.add(updateRequest);
906
907 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
908
909 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
910 pcepTunnelAPIMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
911 } catch (PcepParseException e) {
912 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
913 }
914 }
915
chengfan2fff70f2015-08-24 18:20:19 -0500916
917
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530918 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +0800919
920 @Override
921 public void handlePCEPTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800922 TunnelDescription tunnel = null;
923 // instance and id identify a tunnel together
924 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
925 + String.valueOf(pcepTunnel.id());
926
927 if (tunnelKey == null || "".equals(tunnelKey)) {
928 log.error("Invalid PCEP tunnel");
929 return;
930 }
931
932 TunnelId tunnelId = getTunnelId(tunnelKey);
933
934 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
935
936 OperationType operType = pcepTunnel.getOperationType();
937 switch (operType) {
938 case ADD:
939 tunnelId = service.tunnelAdded(tunnel);
940 tunnelMap.put(tunnelKey, tunnelId);
941 break;
942
943 case UPDATE:
944 service.tunnelUpdated(tunnel);
945 break;
946
947 case DELETE:
948 service.tunnelRemoved(tunnel);
949 tunnelMap.remove(tunnelKey);
950 break;
951
952 default:
953 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530954 }
955 }
cheng fan48e832c2015-05-29 01:54:47 +0800956
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530957 @Override
958 public void handleMessage(PccId pccId, PcepMessage msg) {
959 try {
960 log.debug("tunnel provider handle message {}", msg.getType().toString());
961 switch (msg.getType()) {
962 case REPORT:
963 int srpId = 0;
964 LinkedList<PcepStateReport> llStateReportList = null;
965 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
966 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
967 PcepSrpObject srpObj = null;
968 PcepLspObject lspObj = null;
969 while (listIterator.hasNext()) {
970 PcepStateReport stateRpt = listIterator.next();
971 srpObj = stateRpt.getSrpObject();
972 lspObj = stateRpt.getLspObject();
973
974 if (srpObj instanceof PcepSrpObject) {
975 srpId = srpObj.getSrpID();
976 }
977
978 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
979 log.debug("SRP ID in handle message " + srpId);
980
981 if (!(pcepTunnelAPIMapper.checkFromTunnelRequestQueue(srpId))) {
982
983 // Check the sync status
984 if (lspObj.getSFlag()) {
985 handleSyncReport(stateRpt);
986 } else if (!pcepClientController.getClient(pccId).isSyncComplete()) {
987 // sync is done
988 pcepClientController.getClient(pccId).setIsSyncComplete(true);
989 }
990 continue;
991 }
992
993 handleReportMessage(srpId, lspObj);
994 }
995 break;
996
997 default:
998 log.debug("Received unsupported message type {}", msg.getType().toString());
999 }
1000 } catch (Exception e) {
1001 log.error("Exception occured while processing report message {}", e.getMessage());
1002 }
1003 }
1004
1005 /**
1006 * Handles report message for setup/update/delete tunnel request.
1007 *
1008 * @param srpId unique identifier for pcep message
1009 * @param lspObj lsp object
1010 */
1011 private void handleReportMessage(int srpId, PcepLspObject lspObj) {
1012 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
1013 PcepTunnelData pcepTunnelData = pcepTunnelAPIMapper.getDataFromTunnelRequestQueue(srpId);
1014 SparseAnnotations annotations = (SparseAnnotations) pcepTunnelData.tunnel().annotations();
1015
1016 // store the values required from report message
1017 pcepTunnelData.setPlspId(lspObj.getPlspId());
1018 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1019 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1020 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1021
1022 StatefulIPv4LspIdentidiersTlv ipv4LspTlv = null;
1023 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1024 while (listTlvIterator.hasNext()) {
1025 PcepValueType tlv = listTlvIterator.next();
1026 if (tlv.getType() == StatefulIPv4LspIdentidiersTlv.TYPE) {
1027 ipv4LspTlv = (StatefulIPv4LspIdentidiersTlv) tlv;
1028 break;
1029 }
1030 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001031 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301032 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001033 }
1034
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301035 Path path = pcepTunnelData.path();
1036 Tunnel tunnel = pcepTunnelData.tunnel();
1037 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1038 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1039 providerId, tunnel.tunnelName(), path,
1040 annotations);
1041
1042 if (RequestType.CREATE == pcepTunnelData.requestType()) {
1043 log.debug("Report received for create request");
1044
1045 pcepTunnelAPIMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
1046 if (0 == lspObj.getOFlag()) {
1047 log.warn("The tunnel is in down state");
1048 }
1049 tunnelAdded(td);
1050 }
1051 if (RequestType.DELETE == pcepTunnelData.requestType()) {
1052 log.debug("Report received for delete request");
1053 pcepTunnelAPIMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
1054 tunnelRemoved(td);
1055 }
1056
1057 if (RequestType.UPDATE == pcepTunnelData.requestType()) {
1058 log.debug("Report received for update request");
1059 pcepTunnelData.setRptFlag(true);
1060 pcepTunnelAPIMapper.addToTunnelIdMap(pcepTunnelData);
1061 pcepTunnelAPIMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
1062
1063 if (0 == lspObj.getOFlag()) {
1064 log.warn("The tunnel is in down state");
1065 }
1066 if (!(pcepTunnelAPIMapper.checkFromTunnelRequestQueue(srpId))) {
1067 tunnelUpdated(td);
1068 }
1069 }
1070 }
1071
1072 /**
1073 * Handles sync report received from pcc.
1074 *
1075 * @param stateRpt pcep state report
1076 */
1077 private void handleSyncReport(PcepStateReport stateRpt) {
1078 PcepLspObject lspObj = stateRpt.getLspObject();
1079 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1080 checkNotNull(msgPath);
1081 PcepRroObject rroObj = msgPath.getRroObject();
Mahesh Poojary S5eab8572015-09-01 17:45:48 +05301082 if (rroObj == null) {
1083 log.debug("RRO object is null in sate report");
1084 return;
1085 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301086 int bandwidth = 0;
1087
1088 log.debug("Handle Sync report received from PCC.");
1089
1090 if (0 == lspObj.getOFlag()) {
1091 log.warn("The PCC reported tunnel is in down state");
1092 }
1093 log.debug("Sync report received");
1094
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001095 if (msgPath.getBandwidthObject() != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301096 bandwidth = msgPath.getBandwidthObject().getBandwidth();
1097 }
1098
1099 buildAndStorePcepTunnelData(lspObj, rroObj, bandwidth);
1100 }
1101
1102 /**
1103 * To build Path in network from RRO object.
1104 *
1105 * @param rroObj rro object
1106 * @param providerId provider id
1107 * @return path object
1108 */
1109 private Path buildPathFromRroObj(PcepRroObject rroObj, ProviderId providerId) {
1110 checkNotNull(rroObj);
1111 List<Link> links = new ArrayList<Link>();
1112 LinkedList<PcepValueType> llSubObj = rroObj.getSubObjects();
1113 if (0 == llSubObj.size()) {
1114 log.error("RRO in report message does not have hop information");
1115 }
1116 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1117
1118 ConnectPoint src = null;
1119 ConnectPoint dst = null;
1120 boolean isSrcSet = false;
1121 while (tlvIterator.hasNext()) {
1122 PcepValueType subObj = tlvIterator.next();
1123 switch (subObj.getType()) {
1124
1125 case IPv4SubObject.TYPE:
1126
1127 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1128 if (!isSrcSet) {
1129 IpAddress srcIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1130 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1131 isSrcSet = true;
1132 } else {
1133 IpAddress dstIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1134 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
1135 Link link = new DefaultLink(providerId, src, dst, Link.Type.DIRECT, EMPTY);
1136 links.add(link);
1137 src = dst;
1138 }
1139 break;
1140 default:
1141 // the other sub objects are not required
1142 }
1143 }
1144 return new DefaultPath(providerId, links, 0, EMPTY);
1145 }
1146
1147 /**
1148 * To build pcepTunnelData and informs core about the pcc reported tunnel.
1149 *
1150 * @param lspObj pcep lsp object
1151 * @param rroObj pcep rro object
1152 * @param bandwidth bandwidth of tunnel
1153 */
1154 private void buildAndStorePcepTunnelData(PcepLspObject lspObj, PcepRroObject rroObj,
1155 int bandwidth) {
1156
1157 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
1158
1159 // StatefulIPv4LspIdentidiersTlv in LSP object will have the source and destination address.
1160 StatefulIPv4LspIdentidiersTlv lspIdenTlv = null;
1161 SymbolicPathNameTlv pathNameTlv = null;
1162 LinkedList<PcepValueType> llOptionalTlv = lspObj.getOptionalTlv();
1163 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1164 while (listIterator.hasNext()) {
1165 PcepValueType tlv = listIterator.next();
1166 switch (tlv.getType()) {
1167 case StatefulIPv4LspIdentidiersTlv.TYPE:
1168 lspIdenTlv = (StatefulIPv4LspIdentidiersTlv) tlv;
1169 break;
1170 case SymbolicPathNameTlv.TYPE:
1171 pathNameTlv = (SymbolicPathNameTlv) tlv;
1172 break;
1173 default:
1174 // currently this tlv is not required
1175 }
1176 }
1177
1178 IpTunnelEndPoint tunnelEndPointSrc;
1179 tunnelEndPointSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(lspIdenTlv.getIpv4IngressAddress()));
1180 IpTunnelEndPoint tunnelEndPointDst;
1181 tunnelEndPointDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(lspIdenTlv.getIpv4EgressAddress()));
1182
1183 Path path = buildPathFromRroObj(rroObj, providerId);
1184
1185 SparseAnnotations annotations = DefaultAnnotations.builder()
1186 .set("bandwidth", (new Integer(bandwidth)).toString())
1187 .build();
1188
1189 DefaultTunnelDescription td = new DefaultTunnelDescription(null, tunnelEndPointSrc,
1190 tunnelEndPointDst, Tunnel.Type.MPLS,
1191 new DefaultGroupId(0), providerId,
1192 TunnelName.tunnelName(pathNameTlv.toString()),
1193 path, annotations);
1194 TunnelId tId = tunnelAdded(td);
1195
1196 Tunnel tunnel = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, Tunnel.Type.MPLS,
1197 new DefaultGroupId(0), tId,
1198 TunnelName.tunnelName(pathNameTlv.toString()), path, annotations);
1199
1200 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, RequestType.LSP_STATE_RPT);
1201 pcepTunnelData.setStatefulIpv4IndentifierTlv(lspIdenTlv);
1202 pcepTunnelAPIMapper.addPccTunnelDB(pcepTunnelData);
1203 pcepTunnelAPIMapper.addToTunnelIdMap(pcepTunnelData);
1204 }
1205
1206 @Override
1207 public void clientConnected(PccId pccId) {
1208 // TODO
1209 }
1210
1211 @Override
1212 public void clientDisconnected(PccId pccId) {
1213 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001214 }
chengfan2fff70f2015-08-24 18:20:19 -05001215
1216
1217
1218 @Override
1219 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1220 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1221 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1222 tunnelStatisticsMap.put(id, tunnelStatistics);
1223 }
cheng fan48e832c2015-05-29 01:54:47 +08001224 }
1225
1226 @Override
1227 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1228 return service.tunnelQueryById(tunnelId);
1229 }
cheng fan48e832c2015-05-29 01:54:47 +08001230}