blob: 45ae339520625d7dd4c2a3fedd499dcdbbaadc04 [file] [log] [blame]
cheng fan48e832c2015-05-29 01:54:47 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
cheng fan48e832c2015-05-29 01:54:47 +08003 *
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
chengfan2fff70f2015-08-24 18:20:19 -050018import com.google.common.collect.Maps;
cheng fan48e832c2015-05-29 01:54:47 +080019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
chengfan2fff70f2015-08-24 18:20:19 -050022import org.apache.felix.scr.annotations.Property;
cheng fan48e832c2015-05-29 01:54:47 +080023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053026import org.onlab.packet.IpAddress;
chengfan2fff70f2015-08-24 18:20:19 -050027import org.onosproject.cfg.ComponentConfigService;
cheng fan48e832c2015-05-29 01:54:47 +080028import org.onosproject.core.DefaultGroupId;
29import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053030import org.onosproject.incubator.net.tunnel.DefaultTunnel;
cheng fan48e832c2015-05-29 01:54:47 +080031import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
chengfan2fff70f2015-08-24 18:20:19 -050032import org.onosproject.incubator.net.tunnel.DefaultTunnelStatistics;
Jonathan Hart51539b82015-10-29 09:53:04 -070033import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
cheng fan48e832c2015-05-29 01:54:47 +080034import org.onosproject.incubator.net.tunnel.OpticalLogicId;
35import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
36import org.onosproject.incubator.net.tunnel.Tunnel;
37import org.onosproject.incubator.net.tunnel.TunnelDescription;
38import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
39import org.onosproject.incubator.net.tunnel.TunnelId;
40import org.onosproject.incubator.net.tunnel.TunnelName;
41import org.onosproject.incubator.net.tunnel.TunnelProvider;
42import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
43import org.onosproject.incubator.net.tunnel.TunnelProviderService;
chengfan2fff70f2015-08-24 18:20:19 -050044import org.onosproject.incubator.net.tunnel.TunnelService;
45import org.onosproject.incubator.net.tunnel.TunnelStatistics;
cheng fan48e832c2015-05-29 01:54:47 +080046import org.onosproject.net.ConnectPoint;
47import org.onosproject.net.DefaultAnnotations;
48import org.onosproject.net.DefaultLink;
49import org.onosproject.net.DefaultPath;
50import org.onosproject.net.DeviceId;
51import org.onosproject.net.ElementId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053052import org.onosproject.net.IpElementId;
cheng fan48e832c2015-05-29 01:54:47 +080053import org.onosproject.net.Link;
54import org.onosproject.net.Path;
55import org.onosproject.net.PortNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053056import org.onosproject.net.SparseAnnotations;
cheng fan48e832c2015-05-29 01:54:47 +080057import org.onosproject.net.provider.AbstractProvider;
58import org.onosproject.net.provider.ProviderId;
59import org.onosproject.pcep.api.PcepController;
60import org.onosproject.pcep.api.PcepDpid;
61import org.onosproject.pcep.api.PcepHopNodeDescription;
62import org.onosproject.pcep.api.PcepOperator.OperationType;
63import org.onosproject.pcep.api.PcepTunnel;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053064import org.onosproject.pcep.api.PcepTunnel.PathState;
Jonathan Hart51539b82015-10-29 09:53:04 -070065import org.onosproject.pcep.api.PcepTunnel.PathType;
cheng fan48e832c2015-05-29 01:54:47 +080066import org.onosproject.pcep.api.PcepTunnelListener;
chengfan2fff70f2015-08-24 18:20:19 -050067import org.onosproject.pcep.api.PcepTunnelStatistics;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053068import org.onosproject.pcep.controller.PccId;
69import org.onosproject.pcep.controller.PcepClient;
70import org.onosproject.pcep.controller.PcepClientController;
71import org.onosproject.pcep.controller.PcepClientListener;
72import org.onosproject.pcep.controller.PcepEventListener;
73import org.onosproject.pcepio.exceptions.PcepParseException;
74import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
75import org.onosproject.pcepio.protocol.PcepAttribute;
76import org.onosproject.pcepio.protocol.PcepBandwidthObject;
77import org.onosproject.pcepio.protocol.PcepEndPointsObject;
78import org.onosproject.pcepio.protocol.PcepEroObject;
79import org.onosproject.pcepio.protocol.PcepInitiateMsg;
80import org.onosproject.pcepio.protocol.PcepLspObject;
81import org.onosproject.pcepio.protocol.PcepMessage;
82import org.onosproject.pcepio.protocol.PcepMsgPath;
83import org.onosproject.pcepio.protocol.PcepReportMsg;
84import org.onosproject.pcepio.protocol.PcepRroObject;
85import org.onosproject.pcepio.protocol.PcepSrpObject;
86import org.onosproject.pcepio.protocol.PcepStateReport;
87import org.onosproject.pcepio.protocol.PcepUpdateMsg;
88import org.onosproject.pcepio.protocol.PcepUpdateRequest;
89import org.onosproject.pcepio.types.IPv4SubObject;
90import org.onosproject.pcepio.types.PcepValueType;
91import org.onosproject.pcepio.types.StatefulIPv4LspIdentidiersTlv;
92import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -050093import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -070094import org.osgi.service.component.annotations.Modified;
95import org.slf4j.Logger;
96
97import java.util.ArrayList;
98import java.util.Collections;
99import java.util.Dictionary;
100import java.util.HashMap;
101import java.util.LinkedList;
102import java.util.List;
103import java.util.ListIterator;
104import java.util.Optional;
105
106import static com.google.common.base.Preconditions.checkNotNull;
107import static com.google.common.base.Strings.isNullOrEmpty;
108import static org.onlab.util.Tools.get;
109import static org.onosproject.net.DefaultAnnotations.EMPTY;
110import static org.onosproject.net.DeviceId.deviceId;
111import static org.onosproject.net.PortNumber.portNumber;
112import static org.onosproject.pcep.api.PcepDpid.uri;
113import static org.slf4j.LoggerFactory.getLogger;
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
Jonathan Hart51539b82015-10-29 09:53:04 -0700159 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530160 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 -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700177 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500178 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 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530237
238 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
239 if (pc.capability().pcInstantiationCapability()) {
240 pcepSetupTunnel(tunnel, path, pc);
241 }
cheng fan48e832c2015-05-29 01:54:47 +0800242 }
243
244 @Override
245 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800246
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530247 if (tunnel.type() != Tunnel.Type.MPLS) {
248 log.error("Tunnel Type MPLS is only supported");
249 return;
250 }
251
252 if (!(srcElement instanceof IpElementId)) {
253 log.error("Element id is not valid");
254 return;
255 }
256
257 // check for tunnel end points
258 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
259 log.error("Tunnel source or destination is not valid");
260 return;
261 }
262
263 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
264
265 if (!(pc instanceof PcepClient)) {
266 log.error("There is no PCC connected with ip addresss {}"
267 + ((IpElementId) srcElement).ipAddress().toString());
268 return;
269 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530270
271 if (pc.capability().pcInstantiationCapability()) {
272 pcepSetupTunnel(tunnel, path, pc);
273 }
cheng fan48e832c2015-05-29 01:54:47 +0800274 }
275
276 @Override
277 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800278
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530279 if (tunnel.type() != Tunnel.Type.MPLS) {
280 log.error("Tunnel Type MPLS is only supported");
281 return;
282 }
283
284 // check for tunnel end points
285 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
286 log.error("Tunnel source or destination is not valid");
287 return;
288 }
289
290 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
291
292 if (!(pc instanceof PcepClient)) {
293 log.error("There is no PCC connected with ip addresss {}"
294 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
295 return;
296 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530297
298 if (pc.capability().pcInstantiationCapability()) {
299 pcepReleaseTunnel(tunnel, pc);
300 }
cheng fan48e832c2015-05-29 01:54:47 +0800301 }
302
303 @Override
304 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530305 if (tunnel.type() != Tunnel.Type.MPLS) {
306 log.error("Tunnel Type MPLS is only supported");
307 return;
308 }
cheng fan48e832c2015-05-29 01:54:47 +0800309
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530310 if (!(srcElement instanceof IpElementId)) {
311 log.error("Element id is not valid");
312 return;
313 }
314
315 // check for tunnel end points
316 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
317 log.error("Tunnel source or destination is not valid");
318 return;
319 }
320
321 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
322
323 if (!(pc instanceof PcepClient)) {
324 log.error("There is no PCC connected with ip addresss {}"
325 + ((IpElementId) srcElement).ipAddress().toString());
326 return;
327 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530328
329 if (pc.capability().pcInstantiationCapability()) {
330 pcepReleaseTunnel(tunnel, pc);
331 }
cheng fan48e832c2015-05-29 01:54:47 +0800332 }
333
334 @Override
335 public void updateTunnel(Tunnel tunnel, Path path) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530336 if (tunnel.type() != Tunnel.Type.MPLS) {
337 log.error("Tunnel Type MPLS is only supported");
338 return;
339 }
cheng fan48e832c2015-05-29 01:54:47 +0800340
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530341 // check for tunnel end points
342 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
343 log.error("Tunnel source or destination is not valid");
344 return;
345 }
346
347 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
348
349 if (!(pc instanceof PcepClient)) {
350 log.error("There is no PCC connected with ip addresss {}"
351 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
352 return;
353 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530354
355 if (pc.capability().statefulPceCapability()) {
356 pcepUpdateTunnel(tunnel, path, pc);
357 }
cheng fan48e832c2015-05-29 01:54:47 +0800358 }
359
360 @Override
361 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800362
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530363 if (tunnel.type() != Tunnel.Type.MPLS) {
364 log.error("Tunnel Type MPLS is only supported");
365 return;
366 }
367
368 if (!(srcElement instanceof IpElementId)) {
369 log.error("Element id is not valid");
370 return;
371 }
372
373 // check for tunnel end points
374 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
375 log.error("Tunnel source or destination is not valid");
376 return;
377 }
378
379 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
380
381 if (!(pc instanceof PcepClient)) {
382 log.error("There is no PCC connected with ip addresss {}"
383 + ((IpElementId) srcElement).ipAddress().toString());
384 return;
385 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530386
387 if (pc.capability().statefulPceCapability()) {
388 pcepUpdateTunnel(tunnel, path, pc);
389 }
cheng fan48e832c2015-05-29 01:54:47 +0800390 }
391
392 @Override
393 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530394 if (tunnel.type() == Tunnel.Type.MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700395 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530396 return service.tunnelAdded(tunnel);
397 }
cheng fan48e832c2015-05-29 01:54:47 +0800398
cheng fan7716ec92015-05-31 01:53:19 +0800399 long bandwidth = Long
400 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800401
402 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800403 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800404 return null;
405 }
406
407 // endpoints
408 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
409 .src();
410 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
411 // devices
412 DeviceId srcId = (DeviceId) src.elementId().get();
413 DeviceId dstId = (DeviceId) dst.elementId().get();
414
415 // ports
416 long srcPort = src.portNumber().get().toLong();
417 long dstPort = dst.portNumber().get().toLong();
418
419 // type
420 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800421 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800422 return null;
423 }
424
425 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
426 dstPort, bandwidth,
427 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530428 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800429
430 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
431 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
432 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
433
434 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
435 return tunnelId;
436 }
437
438 @Override
439 public void tunnelRemoved(TunnelDescription tunnel) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530440 if (tunnel.type() == Tunnel.Type.MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700441 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530442 service.tunnelRemoved(tunnel);
443 }
444
cheng fan48e832c2015-05-29 01:54:47 +0800445 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
446 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
447 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800448 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800449 return;
450 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700451 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800452 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800453 if (!controller.deleteTunnel(pcepTunnelId)) {
454 error("Delete tunnel failed, Maybe some devices have been disconnected.");
455 return;
cheng fan48e832c2015-05-29 01:54:47 +0800456 }
457 tunnelMap.remove(pcepTunnelId);
458 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800459 }
460
461 @Override
462 public void tunnelUpdated(TunnelDescription tunnel) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530463 if (tunnel.type() == Tunnel.Type.MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700464 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530465 service.tunnelUpdated(tunnel);
466 }
cheng fan48e832c2015-05-29 01:54:47 +0800467
468 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
469 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800470 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800471 return;
472 }
cheng fan7716ec92015-05-31 01:53:19 +0800473 long bandwidth = Long
474 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800475 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800476 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800477 return;
478 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700479 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800480
481 checkNotNull(pcepTunnelId, "Invalid tunnel id");
482 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
483
cheng fan7716ec92015-05-31 01:53:19 +0800484 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800485 return;
486
487 }
488 service.tunnelUpdated(tunnel);
489 }
490
cheng fan7716ec92015-05-31 01:53:19 +0800491 private void error(String info) {
492 System.err.println(info);
493 }
494
cheng fan48e832c2015-05-29 01:54:47 +0800495 // Short-hand for creating a connection point.
496 private ConnectPoint connectPoint(PcepDpid id, long port) {
497 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
498 }
499
500 // Short-hand for creating a link.
501 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800502 return DefaultLink.builder()
503 .providerId(id())
504 .src(connectPoint(src, sp))
505 .dst(connectPoint(dst, dp))
506 .type(Link.Type.TUNNEL)
507 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800508 }
509
510 // Creates a path that leads through the given devices.
511 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700512 PathType pathtype, PathState pathState) {
cheng fan48e832c2015-05-29 01:54:47 +0800513 if (hopList == null || hopList.size() == 0) {
514 return null;
515 }
516 List<Link> links = new ArrayList<>();
517 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
518 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
519 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
520 .get(i + 1).getPortNum()));
521 }
522
523 int hopNum = hopList.size() - 2;
524 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
525 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800526 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800527 .set("pathType", String.valueOf(pathtype)).build();
528 return new DefaultPath(id(), links, hopNum, extendAnnotations);
529 }
530
531 // convert the path description to a string.
532 public String pathToString(List<Link> links) {
533 StringBuilder builder = new StringBuilder();
534 builder.append("{");
535 for (Link link : links) {
536 builder.append("(Device:" + link.src().deviceId() + " Port:"
537 + link.src().port().toLong());
538 builder.append(" Device:" + link.dst().deviceId() + " Port:"
539 + link.dst().port().toLong());
540 builder.append(")");
541 }
542 builder.append("}");
543 return builder.toString();
544 }
545
546 // build a TunnelDescription.
547 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
548 TunnelId tunnelId) {
549 TunnelEndPoint srcPoint = null;
550 TunnelEndPoint dstPoint = null;
551 Tunnel.Type tunnelType = null;
552 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
553
554 // add path after codes of tunnel's path merged
555 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800556 pcepTunnel.getPathType(),
557 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800558
559 OpticalTunnelEndPoint.Type endPointType = null;
560 switch (pcepTunnel.type()) {
561 case OCH:
562 tunnelType = Tunnel.Type.OCH;
563 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
564 break;
565
566 case OTN:
567 tunnelType = Tunnel.Type.ODUK;
568 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
569 break;
570
571 case UNI:
572 tunnelType = Tunnel.Type.VLAN;
573 endPointType = null;
574 break;
575
576 default:
577 break;
578 }
579 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
580 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
581 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
582 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
583
584 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
585 Optional.of(srcPort), null,
586 endPointType,
587 OpticalLogicId.logicId(0),
588 true);
589 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
590 Optional.of(dstPort), null,
591 endPointType,
592 OpticalLogicId.logicId(0),
593 true);
594
595 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800596 DefaultAnnotations annotations = DefaultAnnotations
597 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800598 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800599 .set("bandwidth",
600 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800601 .set("index", String.valueOf(pcepTunnel.id())).build();
602
cheng fan48e832c2015-05-29 01:54:47 +0800603 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
604 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800605 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800606 DefaultAnnotations extendAnnotations = DefaultAnnotations
607 .builder()
608 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800609 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800610 annotations = DefaultAnnotations.merge(annotations,
611 extendAnnotations);
612
613 }
614 TunnelDescription tunnel = new DefaultTunnelDescription(
615 tunnelId,
616 srcPoint,
617 dstPoint,
618 tunnelType,
619 new DefaultGroupId(
620 0),
621 id(), name,
622 path,
623 annotations);
624 return tunnel;
625
626 }
627
628 /**
629 * Get the tunnelID according to the tunnel key.
630 *
631 * @param tunnelKey tunnel key
632 * @return corresponding tunnel id of the a tunnel key.
633 */
634 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800635 for (String key : tunnelMap.keySet()) {
636 if (key.equals(tunnelKey)) {
637 return tunnelMap.get(key);
638 }
639 }
640 return null;
641 }
642
643 /**
644 * Get the tunnel key according to the tunnelID.
645 *
646 * @param tunnelId tunnel id
647 * @return corresponding a tunnel key of the tunnel id.
648 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700649 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800650 for (String key : tunnelMap.keySet()) {
651 if (tunnelMap.get(key).id() == tunnelId.id()) {
652 return key;
653 }
654 }
655 return null;
656
657 }
658
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530659 /**
chengfan2fff70f2015-08-24 18:20:19 -0500660 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
661 *
662 * @param statistics statistics data from a PCEP tunnel
663 * @return TunnelStatistics
664 */
665 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
666 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
667 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
668 .setPacketLossRatio(statistics.packetLossRate())
669 .setFlowDelay(statistics.flowDelay())
670 .setAlarms(statistics.alarms())
671 .build();
672 return tunnelStatistics;
673 }
674 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530675 * Creates list of hops for ERO object from Path.
676 *
677 * @param path network path
678 * @return list of ipv4 subobjects
679 */
680 private LinkedList<PcepValueType> createPcepPath(Path path) {
681 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
682 List<Link> listLink = path.links();
683 ConnectPoint source = null;
684 ConnectPoint destination = null;
685 IpAddress ipDstAddress = null;
686 IpAddress ipSrcAddress = null;
687 PcepValueType subObj = null;
688
689 for (Link link : listLink) {
690 source = link.src();
691 if (!(source.equals(destination))) {
692 //set IPv4SubObject for ERO object
693 ipSrcAddress = source.ipElementId().ipAddress();
694 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
695 llSubObjects.add(subObj);
696 }
697
698 destination = link.dst();
699 ipDstAddress = destination.ipElementId().ipAddress();
700 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
701 llSubObjects.add(subObj);
702 }
703 return llSubObjects;
704 }
705
706 /**
707 * Creates PcInitiated lsp request list for setup tunnel.
708 *
709 * @param tunnel mpls tunnel
710 * @param path network path
711 * @param pc pcep client
712 * @param srpId unique id for pcep message
713 * @return list of PcInitiatedLspRequest
714 * @throws PcepParseException while building pcep objects fails
715 */
716 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
717 PcepClient pc, int srpId)
718 throws PcepParseException {
719 PcepValueType tlv;
720 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
721
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700722 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530723 log.error("There is no link information to create tunnel");
724 return null;
725 }
726
727 //build SRP object
728 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false).build();
729
730 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
731 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
732 // set LSP identifiers TLV
733 tlv = new StatefulIPv4LspIdentidiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
734 (short) 0, (short) 0, 0,
735 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
736 llOptionalTlv.add(tlv);
737 //set SymbolicPathNameTlv of LSP object
738 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
739 llOptionalTlv.add(tlv);
740
741 //build LSP object
742 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setOFlag((byte) 0).setPlspId(0)
743 .setOptionalTlv(llOptionalTlv).build();
744
745 //build ENDPOINTS object
746 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
747 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
748 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
749 .setPFlag(true).build();
750
751 //build ERO object
752 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
753
754 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700755 if (tunnel.annotations().value("bandwidth") != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530756 iBandwidth = Integer.parseInt(tunnel.annotations().value("bandwidth"));
757 }
758 // build bandwidth object
759 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
760 // build pcep attribute
761 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
762
763 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
764 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
765 .setPcepAttribute(pcepAttribute).build();
766 llPcInitiatedLspRequestList.add(initiateLspRequest);
767 return llPcInitiatedLspRequestList;
768 }
769
770 /**
771 * To send initiate tunnel message to pcc.
772 *
773 * @param tunnel mpls tunnel info
774 * @param path explicit route for the tunnel
775 * @param pc pcep client to send message
776 */
777 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
778 try {
779 int srpId = SrpIdGenerators.create();
780 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, RequestType.CREATE);
781
Jonathan Hart51539b82015-10-29 09:53:04 -0700782 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530783
784 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
785 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700786 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530787 log.error("Failed to create PcInitiatedLspRequestList");
788 return;
789 }
790
791 //build PCInitiate message
792 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
793 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
794 .build();
795
796 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
797
Jonathan Hart51539b82015-10-29 09:53:04 -0700798 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530799 } catch (PcepParseException e) {
800 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
801 }
802 }
803
804 /**
805 * To send Release tunnel message to pcc.
806 *
807 * @param tunnel mpls tunnel info
808 * @param pc pcep client to send message
809 */
810 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
811 try {
812 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, RequestType.DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -0700813 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530814 int srpId = SrpIdGenerators.create();
815 TunnelId tunnelId = tunnel.tunnelId();
816 int plspId = 0;
817 StatefulIPv4LspIdentidiersTlv statefulIpv4IndentifierTlv = null;
818
Jonathan Hart51539b82015-10-29 09:53:04 -0700819 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530820 log.error("Tunnel doesnot exists. Tunnel id {}" + tunnelId.toString());
821 return;
822 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -0700823 PcepTunnelData pcepTunnelDbData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530824 plspId = pcepTunnelDbData.plspId();
825 statefulIpv4IndentifierTlv = pcepTunnelDbData.statefulIpv4IndentifierTlv();
826 }
827 // build srp object
828 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true).build();
829
830 PcepValueType tlv;
831 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
832 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
833
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700834 if (statefulIpv4IndentifierTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530835 tlv = statefulIpv4IndentifierTlv;
836 } else {
837 tlv = new StatefulIPv4LspIdentidiersTlv((
838 ((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
839 (short) 0, (short) 0, 0,
840 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
841 }
842 llOptionalTlv.add(tlv);
843 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
844 llOptionalTlv.add(tlv);
845 // build lsp object, set r flag as false to delete the tunnel
846 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
847 .setOptionalTlv(llOptionalTlv).build();
848
849 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
850 .setLspObject(lspobj).build();
851
852 llPcInitiatedLspRequestList.add(releaseLspRequest);
853
854 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
855 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
856
857 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
858
Jonathan Hart51539b82015-10-29 09:53:04 -0700859 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530860 } catch (PcepParseException e) {
861 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
862 }
863 }
864
865 /**
866 * To send Update tunnel request message to pcc.
867 *
868 * @param tunnel mpls tunnel info
869 * @param path explicit route for the tunnel
870 * @param pc pcep client to send message
871 */
872 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
873 try {
874 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, RequestType.UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -0700875 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530876 int srpId = SrpIdGenerators.create();
877 TunnelId tunnelId = tunnel.tunnelId();
878 PcepValueType tlv;
879 int plspId = 0;
880
881 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
882 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
883 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
884
885 //build SRP object
886 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false).build();
887
Jonathan Hart51539b82015-10-29 09:53:04 -0700888 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530889 log.error("Tunnel doesnot exists in DB");
890 return;
891 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -0700892 PcepTunnelData pcepTunnelDBData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530893 plspId = pcepTunnelDBData.plspId();
894 }
895
896 tlv = new StatefulIPv4LspIdentidiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
897 (short) 0, (short) 0, 0,
898 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
899 llOptionalTlv.add(tlv);
900
901 if (tunnel.tunnelName().value() != null) {
902 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
903 llOptionalTlv.add(tlv);
904 }
905
906 // build lsp object
907 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setPlspId(plspId)
908 .setOptionalTlv(llOptionalTlv).build();
909 // build ero object
910 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
911
912 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700913 if (tunnel.annotations().value("bandwidth") != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530914 iBandwidth = Integer.parseInt(tunnel.annotations().value("bandwidth"));
915 }
916 // build bandwidth object
917 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
918 // build pcep attribute
919 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
920 // build pcep msg path
921 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
922 .build();
923
924 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
925 .setLspObject(lspobj).setMsgPath(msgPath).build();
926
927 llUpdateRequestList.add(updateRequest);
928
929 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
930
931 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -0700932 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530933 } catch (PcepParseException e) {
934 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
935 }
936 }
937
chengfan2fff70f2015-08-24 18:20:19 -0500938
939
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530940 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +0800941
942 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700943 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800944 TunnelDescription tunnel = null;
945 // instance and id identify a tunnel together
946 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
947 + String.valueOf(pcepTunnel.id());
948
949 if (tunnelKey == null || "".equals(tunnelKey)) {
950 log.error("Invalid PCEP tunnel");
951 return;
952 }
953
954 TunnelId tunnelId = getTunnelId(tunnelKey);
955
956 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
957
958 OperationType operType = pcepTunnel.getOperationType();
959 switch (operType) {
960 case ADD:
961 tunnelId = service.tunnelAdded(tunnel);
962 tunnelMap.put(tunnelKey, tunnelId);
963 break;
964
965 case UPDATE:
966 service.tunnelUpdated(tunnel);
967 break;
968
969 case DELETE:
970 service.tunnelRemoved(tunnel);
971 tunnelMap.remove(tunnelKey);
972 break;
973
974 default:
975 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530976 }
977 }
cheng fan48e832c2015-05-29 01:54:47 +0800978
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530979 @Override
980 public void handleMessage(PccId pccId, PcepMessage msg) {
981 try {
982 log.debug("tunnel provider handle message {}", msg.getType().toString());
983 switch (msg.getType()) {
984 case REPORT:
985 int srpId = 0;
986 LinkedList<PcepStateReport> llStateReportList = null;
987 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
988 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
989 PcepSrpObject srpObj = null;
990 PcepLspObject lspObj = null;
991 while (listIterator.hasNext()) {
992 PcepStateReport stateRpt = listIterator.next();
993 srpObj = stateRpt.getSrpObject();
994 lspObj = stateRpt.getLspObject();
995
996 if (srpObj instanceof PcepSrpObject) {
997 srpId = srpObj.getSrpID();
998 }
999
1000 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1001 log.debug("SRP ID in handle message " + srpId);
1002
Jonathan Hart51539b82015-10-29 09:53:04 -07001003 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301004
1005 // Check the sync status
1006 if (lspObj.getSFlag()) {
1007 handleSyncReport(stateRpt);
1008 } else if (!pcepClientController.getClient(pccId).isSyncComplete()) {
1009 // sync is done
1010 pcepClientController.getClient(pccId).setIsSyncComplete(true);
1011 }
1012 continue;
1013 }
1014
1015 handleReportMessage(srpId, lspObj);
1016 }
1017 break;
1018
1019 default:
1020 log.debug("Received unsupported message type {}", msg.getType().toString());
1021 }
1022 } catch (Exception e) {
1023 log.error("Exception occured while processing report message {}", e.getMessage());
1024 }
1025 }
1026
1027 /**
1028 * Handles report message for setup/update/delete tunnel request.
1029 *
1030 * @param srpId unique identifier for pcep message
1031 * @param lspObj lsp object
1032 */
1033 private void handleReportMessage(int srpId, PcepLspObject lspObj) {
1034 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001035 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301036 SparseAnnotations annotations = (SparseAnnotations) pcepTunnelData.tunnel().annotations();
1037
1038 // store the values required from report message
1039 pcepTunnelData.setPlspId(lspObj.getPlspId());
1040 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1041 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1042 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1043
1044 StatefulIPv4LspIdentidiersTlv ipv4LspTlv = null;
1045 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1046 while (listTlvIterator.hasNext()) {
1047 PcepValueType tlv = listTlvIterator.next();
1048 if (tlv.getType() == StatefulIPv4LspIdentidiersTlv.TYPE) {
1049 ipv4LspTlv = (StatefulIPv4LspIdentidiersTlv) tlv;
1050 break;
1051 }
1052 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001053 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301054 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001055 }
1056
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301057 Path path = pcepTunnelData.path();
1058 Tunnel tunnel = pcepTunnelData.tunnel();
1059 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1060 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1061 providerId, tunnel.tunnelName(), path,
1062 annotations);
1063
1064 if (RequestType.CREATE == pcepTunnelData.requestType()) {
1065 log.debug("Report received for create request");
1066
Jonathan Hart51539b82015-10-29 09:53:04 -07001067 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301068 if (0 == lspObj.getOFlag()) {
1069 log.warn("The tunnel is in down state");
1070 }
1071 tunnelAdded(td);
1072 }
1073 if (RequestType.DELETE == pcepTunnelData.requestType()) {
1074 log.debug("Report received for delete request");
Jonathan Hart51539b82015-10-29 09:53:04 -07001075 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301076 tunnelRemoved(td);
1077 }
1078
1079 if (RequestType.UPDATE == pcepTunnelData.requestType()) {
1080 log.debug("Report received for update request");
1081 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001082 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1083 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301084
1085 if (0 == lspObj.getOFlag()) {
1086 log.warn("The tunnel is in down state");
1087 }
Jonathan Hart51539b82015-10-29 09:53:04 -07001088 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301089 tunnelUpdated(td);
1090 }
1091 }
1092 }
1093
1094 /**
1095 * Handles sync report received from pcc.
1096 *
1097 * @param stateRpt pcep state report
1098 */
1099 private void handleSyncReport(PcepStateReport stateRpt) {
1100 PcepLspObject lspObj = stateRpt.getLspObject();
1101 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1102 checkNotNull(msgPath);
1103 PcepRroObject rroObj = msgPath.getRroObject();
Mahesh Poojary S5eab8572015-09-01 17:45:48 +05301104 if (rroObj == null) {
1105 log.debug("RRO object is null in sate report");
1106 return;
1107 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301108 int bandwidth = 0;
1109
1110 log.debug("Handle Sync report received from PCC.");
1111
1112 if (0 == lspObj.getOFlag()) {
1113 log.warn("The PCC reported tunnel is in down state");
1114 }
1115 log.debug("Sync report received");
1116
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001117 if (msgPath.getBandwidthObject() != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301118 bandwidth = msgPath.getBandwidthObject().getBandwidth();
1119 }
1120
1121 buildAndStorePcepTunnelData(lspObj, rroObj, bandwidth);
1122 }
1123
1124 /**
1125 * To build Path in network from RRO object.
1126 *
1127 * @param rroObj rro object
1128 * @param providerId provider id
1129 * @return path object
1130 */
1131 private Path buildPathFromRroObj(PcepRroObject rroObj, ProviderId providerId) {
1132 checkNotNull(rroObj);
1133 List<Link> links = new ArrayList<Link>();
1134 LinkedList<PcepValueType> llSubObj = rroObj.getSubObjects();
1135 if (0 == llSubObj.size()) {
1136 log.error("RRO in report message does not have hop information");
1137 }
1138 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1139
1140 ConnectPoint src = null;
1141 ConnectPoint dst = null;
1142 boolean isSrcSet = false;
1143 while (tlvIterator.hasNext()) {
1144 PcepValueType subObj = tlvIterator.next();
1145 switch (subObj.getType()) {
1146
1147 case IPv4SubObject.TYPE:
1148
1149 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1150 if (!isSrcSet) {
1151 IpAddress srcIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1152 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1153 isSrcSet = true;
1154 } else {
1155 IpAddress dstIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1156 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
Ray Milkey2693bda2016-01-22 16:08:14 -08001157 Link link = DefaultLink.builder()
1158 .providerId(providerId)
1159 .src(src)
1160 .dst(dst)
1161 .type(Link.Type.DIRECT)
1162 .build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301163 links.add(link);
1164 src = dst;
1165 }
1166 break;
1167 default:
1168 // the other sub objects are not required
1169 }
1170 }
1171 return new DefaultPath(providerId, links, 0, EMPTY);
1172 }
1173
1174 /**
1175 * To build pcepTunnelData and informs core about the pcc reported tunnel.
1176 *
1177 * @param lspObj pcep lsp object
1178 * @param rroObj pcep rro object
1179 * @param bandwidth bandwidth of tunnel
1180 */
1181 private void buildAndStorePcepTunnelData(PcepLspObject lspObj, PcepRroObject rroObj,
1182 int bandwidth) {
1183
1184 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
1185
1186 // StatefulIPv4LspIdentidiersTlv in LSP object will have the source and destination address.
1187 StatefulIPv4LspIdentidiersTlv lspIdenTlv = null;
1188 SymbolicPathNameTlv pathNameTlv = null;
1189 LinkedList<PcepValueType> llOptionalTlv = lspObj.getOptionalTlv();
1190 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1191 while (listIterator.hasNext()) {
1192 PcepValueType tlv = listIterator.next();
1193 switch (tlv.getType()) {
1194 case StatefulIPv4LspIdentidiersTlv.TYPE:
1195 lspIdenTlv = (StatefulIPv4LspIdentidiersTlv) tlv;
1196 break;
1197 case SymbolicPathNameTlv.TYPE:
1198 pathNameTlv = (SymbolicPathNameTlv) tlv;
1199 break;
1200 default:
1201 // currently this tlv is not required
1202 }
1203 }
1204
1205 IpTunnelEndPoint tunnelEndPointSrc;
1206 tunnelEndPointSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(lspIdenTlv.getIpv4IngressAddress()));
1207 IpTunnelEndPoint tunnelEndPointDst;
1208 tunnelEndPointDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(lspIdenTlv.getIpv4EgressAddress()));
1209
1210 Path path = buildPathFromRroObj(rroObj, providerId);
1211
1212 SparseAnnotations annotations = DefaultAnnotations.builder()
1213 .set("bandwidth", (new Integer(bandwidth)).toString())
1214 .build();
1215
1216 DefaultTunnelDescription td = new DefaultTunnelDescription(null, tunnelEndPointSrc,
1217 tunnelEndPointDst, Tunnel.Type.MPLS,
1218 new DefaultGroupId(0), providerId,
1219 TunnelName.tunnelName(pathNameTlv.toString()),
1220 path, annotations);
1221 TunnelId tId = tunnelAdded(td);
1222
1223 Tunnel tunnel = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, Tunnel.Type.MPLS,
1224 new DefaultGroupId(0), tId,
1225 TunnelName.tunnelName(pathNameTlv.toString()), path, annotations);
1226
1227 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, RequestType.LSP_STATE_RPT);
1228 pcepTunnelData.setStatefulIpv4IndentifierTlv(lspIdenTlv);
Jonathan Hart51539b82015-10-29 09:53:04 -07001229 pcepTunnelApiMapper.addPccTunnelDB(pcepTunnelData);
1230 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301231 }
1232
1233 @Override
1234 public void clientConnected(PccId pccId) {
1235 // TODO
1236 }
1237
1238 @Override
1239 public void clientDisconnected(PccId pccId) {
1240 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001241 }
chengfan2fff70f2015-08-24 18:20:19 -05001242
1243
1244
1245 @Override
1246 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1247 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1248 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1249 tunnelStatisticsMap.put(id, tunnelStatistics);
1250 }
cheng fan48e832c2015-05-29 01:54:47 +08001251 }
1252
1253 @Override
1254 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1255 return service.tunnelQueryById(tunnelId);
1256 }
cheng fan48e832c2015-05-29 01:54:47 +08001257}