blob: f973d46483f79da9b059609c29e8bd2b3d14861b [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 }
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) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700376 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530377 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) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700422 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530423 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 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700432 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800433 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) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700445 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530446 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 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700460 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800461
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) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800483 return DefaultLink.builder()
484 .providerId(id())
485 .src(connectPoint(src, sp))
486 .dst(connectPoint(dst, dp))
487 .type(Link.Type.TUNNEL)
488 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800489 }
490
491 // Creates a path that leads through the given devices.
492 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700493 PathType pathtype, PathState pathState) {
cheng fan48e832c2015-05-29 01:54:47 +0800494 if (hopList == null || hopList.size() == 0) {
495 return null;
496 }
497 List<Link> links = new ArrayList<>();
498 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
499 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
500 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
501 .get(i + 1).getPortNum()));
502 }
503
504 int hopNum = hopList.size() - 2;
505 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
506 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800507 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800508 .set("pathType", String.valueOf(pathtype)).build();
509 return new DefaultPath(id(), links, hopNum, extendAnnotations);
510 }
511
512 // convert the path description to a string.
513 public String pathToString(List<Link> links) {
514 StringBuilder builder = new StringBuilder();
515 builder.append("{");
516 for (Link link : links) {
517 builder.append("(Device:" + link.src().deviceId() + " Port:"
518 + link.src().port().toLong());
519 builder.append(" Device:" + link.dst().deviceId() + " Port:"
520 + link.dst().port().toLong());
521 builder.append(")");
522 }
523 builder.append("}");
524 return builder.toString();
525 }
526
527 // build a TunnelDescription.
528 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
529 TunnelId tunnelId) {
530 TunnelEndPoint srcPoint = null;
531 TunnelEndPoint dstPoint = null;
532 Tunnel.Type tunnelType = null;
533 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
534
535 // add path after codes of tunnel's path merged
536 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800537 pcepTunnel.getPathType(),
538 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800539
540 OpticalTunnelEndPoint.Type endPointType = null;
541 switch (pcepTunnel.type()) {
542 case OCH:
543 tunnelType = Tunnel.Type.OCH;
544 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
545 break;
546
547 case OTN:
548 tunnelType = Tunnel.Type.ODUK;
549 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
550 break;
551
552 case UNI:
553 tunnelType = Tunnel.Type.VLAN;
554 endPointType = null;
555 break;
556
557 default:
558 break;
559 }
560 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
561 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
562 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
563 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
564
565 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
566 Optional.of(srcPort), null,
567 endPointType,
568 OpticalLogicId.logicId(0),
569 true);
570 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
571 Optional.of(dstPort), null,
572 endPointType,
573 OpticalLogicId.logicId(0),
574 true);
575
576 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800577 DefaultAnnotations annotations = DefaultAnnotations
578 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800579 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800580 .set("bandwidth",
581 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800582 .set("index", String.valueOf(pcepTunnel.id())).build();
583
cheng fan48e832c2015-05-29 01:54:47 +0800584 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
585 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800586 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800587 DefaultAnnotations extendAnnotations = DefaultAnnotations
588 .builder()
589 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800590 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800591 annotations = DefaultAnnotations.merge(annotations,
592 extendAnnotations);
593
594 }
595 TunnelDescription tunnel = new DefaultTunnelDescription(
596 tunnelId,
597 srcPoint,
598 dstPoint,
599 tunnelType,
600 new DefaultGroupId(
601 0),
602 id(), name,
603 path,
604 annotations);
605 return tunnel;
606
607 }
608
609 /**
610 * Get the tunnelID according to the tunnel key.
611 *
612 * @param tunnelKey tunnel key
613 * @return corresponding tunnel id of the a tunnel key.
614 */
615 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800616 for (String key : tunnelMap.keySet()) {
617 if (key.equals(tunnelKey)) {
618 return tunnelMap.get(key);
619 }
620 }
621 return null;
622 }
623
624 /**
625 * Get the tunnel key according to the tunnelID.
626 *
627 * @param tunnelId tunnel id
628 * @return corresponding a tunnel key of the tunnel id.
629 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700630 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800631 for (String key : tunnelMap.keySet()) {
632 if (tunnelMap.get(key).id() == tunnelId.id()) {
633 return key;
634 }
635 }
636 return null;
637
638 }
639
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530640 /**
chengfan2fff70f2015-08-24 18:20:19 -0500641 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
642 *
643 * @param statistics statistics data from a PCEP tunnel
644 * @return TunnelStatistics
645 */
646 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
647 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
648 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
649 .setPacketLossRatio(statistics.packetLossRate())
650 .setFlowDelay(statistics.flowDelay())
651 .setAlarms(statistics.alarms())
652 .build();
653 return tunnelStatistics;
654 }
655 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530656 * Creates list of hops for ERO object from Path.
657 *
658 * @param path network path
659 * @return list of ipv4 subobjects
660 */
661 private LinkedList<PcepValueType> createPcepPath(Path path) {
662 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
663 List<Link> listLink = path.links();
664 ConnectPoint source = null;
665 ConnectPoint destination = null;
666 IpAddress ipDstAddress = null;
667 IpAddress ipSrcAddress = null;
668 PcepValueType subObj = null;
669
670 for (Link link : listLink) {
671 source = link.src();
672 if (!(source.equals(destination))) {
673 //set IPv4SubObject for ERO object
674 ipSrcAddress = source.ipElementId().ipAddress();
675 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
676 llSubObjects.add(subObj);
677 }
678
679 destination = link.dst();
680 ipDstAddress = destination.ipElementId().ipAddress();
681 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
682 llSubObjects.add(subObj);
683 }
684 return llSubObjects;
685 }
686
687 /**
688 * Creates PcInitiated lsp request list for setup tunnel.
689 *
690 * @param tunnel mpls tunnel
691 * @param path network path
692 * @param pc pcep client
693 * @param srpId unique id for pcep message
694 * @return list of PcInitiatedLspRequest
695 * @throws PcepParseException while building pcep objects fails
696 */
697 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
698 PcepClient pc, int srpId)
699 throws PcepParseException {
700 PcepValueType tlv;
701 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
702
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700703 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530704 log.error("There is no link information to create tunnel");
705 return null;
706 }
707
708 //build SRP object
709 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false).build();
710
711 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
712 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
713 // set LSP identifiers TLV
714 tlv = new StatefulIPv4LspIdentidiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
715 (short) 0, (short) 0, 0,
716 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
717 llOptionalTlv.add(tlv);
718 //set SymbolicPathNameTlv of LSP object
719 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
720 llOptionalTlv.add(tlv);
721
722 //build LSP object
723 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setOFlag((byte) 0).setPlspId(0)
724 .setOptionalTlv(llOptionalTlv).build();
725
726 //build ENDPOINTS object
727 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
728 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
729 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
730 .setPFlag(true).build();
731
732 //build ERO object
733 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
734
735 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700736 if (tunnel.annotations().value("bandwidth") != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530737 iBandwidth = Integer.parseInt(tunnel.annotations().value("bandwidth"));
738 }
739 // build bandwidth object
740 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
741 // build pcep attribute
742 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
743
744 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
745 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
746 .setPcepAttribute(pcepAttribute).build();
747 llPcInitiatedLspRequestList.add(initiateLspRequest);
748 return llPcInitiatedLspRequestList;
749 }
750
751 /**
752 * To send initiate tunnel message to pcc.
753 *
754 * @param tunnel mpls tunnel info
755 * @param path explicit route for the tunnel
756 * @param pc pcep client to send message
757 */
758 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
759 try {
760 int srpId = SrpIdGenerators.create();
761 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, RequestType.CREATE);
762
Jonathan Hart51539b82015-10-29 09:53:04 -0700763 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530764
765 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
766 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700767 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530768 log.error("Failed to create PcInitiatedLspRequestList");
769 return;
770 }
771
772 //build PCInitiate message
773 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
774 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
775 .build();
776
777 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
778
Jonathan Hart51539b82015-10-29 09:53:04 -0700779 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530780 } catch (PcepParseException e) {
781 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
782 }
783 }
784
785 /**
786 * To send Release tunnel message to pcc.
787 *
788 * @param tunnel mpls tunnel info
789 * @param pc pcep client to send message
790 */
791 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
792 try {
793 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, RequestType.DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -0700794 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530795 int srpId = SrpIdGenerators.create();
796 TunnelId tunnelId = tunnel.tunnelId();
797 int plspId = 0;
798 StatefulIPv4LspIdentidiersTlv statefulIpv4IndentifierTlv = null;
799
Jonathan Hart51539b82015-10-29 09:53:04 -0700800 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530801 log.error("Tunnel doesnot exists. Tunnel id {}" + tunnelId.toString());
802 return;
803 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -0700804 PcepTunnelData pcepTunnelDbData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530805 plspId = pcepTunnelDbData.plspId();
806 statefulIpv4IndentifierTlv = pcepTunnelDbData.statefulIpv4IndentifierTlv();
807 }
808 // build srp object
809 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true).build();
810
811 PcepValueType tlv;
812 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
813 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
814
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700815 if (statefulIpv4IndentifierTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530816 tlv = statefulIpv4IndentifierTlv;
817 } else {
818 tlv = new StatefulIPv4LspIdentidiersTlv((
819 ((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
820 (short) 0, (short) 0, 0,
821 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
822 }
823 llOptionalTlv.add(tlv);
824 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
825 llOptionalTlv.add(tlv);
826 // build lsp object, set r flag as false to delete the tunnel
827 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
828 .setOptionalTlv(llOptionalTlv).build();
829
830 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
831 .setLspObject(lspobj).build();
832
833 llPcInitiatedLspRequestList.add(releaseLspRequest);
834
835 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
836 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
837
838 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
839
Jonathan Hart51539b82015-10-29 09:53:04 -0700840 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530841 } catch (PcepParseException e) {
842 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
843 }
844 }
845
846 /**
847 * To send Update tunnel request message to pcc.
848 *
849 * @param tunnel mpls tunnel info
850 * @param path explicit route for the tunnel
851 * @param pc pcep client to send message
852 */
853 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
854 try {
855 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, RequestType.UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -0700856 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530857 int srpId = SrpIdGenerators.create();
858 TunnelId tunnelId = tunnel.tunnelId();
859 PcepValueType tlv;
860 int plspId = 0;
861
862 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
863 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
864 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
865
866 //build SRP object
867 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false).build();
868
Jonathan Hart51539b82015-10-29 09:53:04 -0700869 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530870 log.error("Tunnel doesnot exists in DB");
871 return;
872 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -0700873 PcepTunnelData pcepTunnelDBData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530874 plspId = pcepTunnelDBData.plspId();
875 }
876
877 tlv = new StatefulIPv4LspIdentidiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
878 (short) 0, (short) 0, 0,
879 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
880 llOptionalTlv.add(tlv);
881
882 if (tunnel.tunnelName().value() != null) {
883 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
884 llOptionalTlv.add(tlv);
885 }
886
887 // build lsp object
888 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setPlspId(plspId)
889 .setOptionalTlv(llOptionalTlv).build();
890 // build ero object
891 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
892
893 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700894 if (tunnel.annotations().value("bandwidth") != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530895 iBandwidth = Integer.parseInt(tunnel.annotations().value("bandwidth"));
896 }
897 // build bandwidth object
898 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
899 // build pcep attribute
900 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
901 // build pcep msg path
902 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
903 .build();
904
905 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
906 .setLspObject(lspobj).setMsgPath(msgPath).build();
907
908 llUpdateRequestList.add(updateRequest);
909
910 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
911
912 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -0700913 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530914 } catch (PcepParseException e) {
915 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
916 }
917 }
918
chengfan2fff70f2015-08-24 18:20:19 -0500919
920
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530921 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +0800922
923 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -0700924 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800925 TunnelDescription tunnel = null;
926 // instance and id identify a tunnel together
927 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
928 + String.valueOf(pcepTunnel.id());
929
930 if (tunnelKey == null || "".equals(tunnelKey)) {
931 log.error("Invalid PCEP tunnel");
932 return;
933 }
934
935 TunnelId tunnelId = getTunnelId(tunnelKey);
936
937 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
938
939 OperationType operType = pcepTunnel.getOperationType();
940 switch (operType) {
941 case ADD:
942 tunnelId = service.tunnelAdded(tunnel);
943 tunnelMap.put(tunnelKey, tunnelId);
944 break;
945
946 case UPDATE:
947 service.tunnelUpdated(tunnel);
948 break;
949
950 case DELETE:
951 service.tunnelRemoved(tunnel);
952 tunnelMap.remove(tunnelKey);
953 break;
954
955 default:
956 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530957 }
958 }
cheng fan48e832c2015-05-29 01:54:47 +0800959
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530960 @Override
961 public void handleMessage(PccId pccId, PcepMessage msg) {
962 try {
963 log.debug("tunnel provider handle message {}", msg.getType().toString());
964 switch (msg.getType()) {
965 case REPORT:
966 int srpId = 0;
967 LinkedList<PcepStateReport> llStateReportList = null;
968 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
969 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
970 PcepSrpObject srpObj = null;
971 PcepLspObject lspObj = null;
972 while (listIterator.hasNext()) {
973 PcepStateReport stateRpt = listIterator.next();
974 srpObj = stateRpt.getSrpObject();
975 lspObj = stateRpt.getLspObject();
976
977 if (srpObj instanceof PcepSrpObject) {
978 srpId = srpObj.getSrpID();
979 }
980
981 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
982 log.debug("SRP ID in handle message " + srpId);
983
Jonathan Hart51539b82015-10-29 09:53:04 -0700984 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530985
986 // Check the sync status
987 if (lspObj.getSFlag()) {
988 handleSyncReport(stateRpt);
989 } else if (!pcepClientController.getClient(pccId).isSyncComplete()) {
990 // sync is done
991 pcepClientController.getClient(pccId).setIsSyncComplete(true);
992 }
993 continue;
994 }
995
996 handleReportMessage(srpId, lspObj);
997 }
998 break;
999
1000 default:
1001 log.debug("Received unsupported message type {}", msg.getType().toString());
1002 }
1003 } catch (Exception e) {
1004 log.error("Exception occured while processing report message {}", e.getMessage());
1005 }
1006 }
1007
1008 /**
1009 * Handles report message for setup/update/delete tunnel request.
1010 *
1011 * @param srpId unique identifier for pcep message
1012 * @param lspObj lsp object
1013 */
1014 private void handleReportMessage(int srpId, PcepLspObject lspObj) {
1015 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001016 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301017 SparseAnnotations annotations = (SparseAnnotations) pcepTunnelData.tunnel().annotations();
1018
1019 // store the values required from report message
1020 pcepTunnelData.setPlspId(lspObj.getPlspId());
1021 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1022 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1023 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1024
1025 StatefulIPv4LspIdentidiersTlv ipv4LspTlv = null;
1026 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1027 while (listTlvIterator.hasNext()) {
1028 PcepValueType tlv = listTlvIterator.next();
1029 if (tlv.getType() == StatefulIPv4LspIdentidiersTlv.TYPE) {
1030 ipv4LspTlv = (StatefulIPv4LspIdentidiersTlv) tlv;
1031 break;
1032 }
1033 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001034 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301035 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001036 }
1037
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301038 Path path = pcepTunnelData.path();
1039 Tunnel tunnel = pcepTunnelData.tunnel();
1040 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1041 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1042 providerId, tunnel.tunnelName(), path,
1043 annotations);
1044
1045 if (RequestType.CREATE == pcepTunnelData.requestType()) {
1046 log.debug("Report received for create request");
1047
Jonathan Hart51539b82015-10-29 09:53:04 -07001048 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301049 if (0 == lspObj.getOFlag()) {
1050 log.warn("The tunnel is in down state");
1051 }
1052 tunnelAdded(td);
1053 }
1054 if (RequestType.DELETE == pcepTunnelData.requestType()) {
1055 log.debug("Report received for delete request");
Jonathan Hart51539b82015-10-29 09:53:04 -07001056 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301057 tunnelRemoved(td);
1058 }
1059
1060 if (RequestType.UPDATE == pcepTunnelData.requestType()) {
1061 log.debug("Report received for update request");
1062 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001063 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1064 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301065
1066 if (0 == lspObj.getOFlag()) {
1067 log.warn("The tunnel is in down state");
1068 }
Jonathan Hart51539b82015-10-29 09:53:04 -07001069 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301070 tunnelUpdated(td);
1071 }
1072 }
1073 }
1074
1075 /**
1076 * Handles sync report received from pcc.
1077 *
1078 * @param stateRpt pcep state report
1079 */
1080 private void handleSyncReport(PcepStateReport stateRpt) {
1081 PcepLspObject lspObj = stateRpt.getLspObject();
1082 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1083 checkNotNull(msgPath);
1084 PcepRroObject rroObj = msgPath.getRroObject();
Mahesh Poojary S5eab8572015-09-01 17:45:48 +05301085 if (rroObj == null) {
1086 log.debug("RRO object is null in sate report");
1087 return;
1088 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301089 int bandwidth = 0;
1090
1091 log.debug("Handle Sync report received from PCC.");
1092
1093 if (0 == lspObj.getOFlag()) {
1094 log.warn("The PCC reported tunnel is in down state");
1095 }
1096 log.debug("Sync report received");
1097
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001098 if (msgPath.getBandwidthObject() != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301099 bandwidth = msgPath.getBandwidthObject().getBandwidth();
1100 }
1101
1102 buildAndStorePcepTunnelData(lspObj, rroObj, bandwidth);
1103 }
1104
1105 /**
1106 * To build Path in network from RRO object.
1107 *
1108 * @param rroObj rro object
1109 * @param providerId provider id
1110 * @return path object
1111 */
1112 private Path buildPathFromRroObj(PcepRroObject rroObj, ProviderId providerId) {
1113 checkNotNull(rroObj);
1114 List<Link> links = new ArrayList<Link>();
1115 LinkedList<PcepValueType> llSubObj = rroObj.getSubObjects();
1116 if (0 == llSubObj.size()) {
1117 log.error("RRO in report message does not have hop information");
1118 }
1119 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1120
1121 ConnectPoint src = null;
1122 ConnectPoint dst = null;
1123 boolean isSrcSet = false;
1124 while (tlvIterator.hasNext()) {
1125 PcepValueType subObj = tlvIterator.next();
1126 switch (subObj.getType()) {
1127
1128 case IPv4SubObject.TYPE:
1129
1130 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1131 if (!isSrcSet) {
1132 IpAddress srcIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1133 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1134 isSrcSet = true;
1135 } else {
1136 IpAddress dstIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1137 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
Ray Milkey2693bda2016-01-22 16:08:14 -08001138 Link link = DefaultLink.builder()
1139 .providerId(providerId)
1140 .src(src)
1141 .dst(dst)
1142 .type(Link.Type.DIRECT)
1143 .build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301144 links.add(link);
1145 src = dst;
1146 }
1147 break;
1148 default:
1149 // the other sub objects are not required
1150 }
1151 }
1152 return new DefaultPath(providerId, links, 0, EMPTY);
1153 }
1154
1155 /**
1156 * To build pcepTunnelData and informs core about the pcc reported tunnel.
1157 *
1158 * @param lspObj pcep lsp object
1159 * @param rroObj pcep rro object
1160 * @param bandwidth bandwidth of tunnel
1161 */
1162 private void buildAndStorePcepTunnelData(PcepLspObject lspObj, PcepRroObject rroObj,
1163 int bandwidth) {
1164
1165 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
1166
1167 // StatefulIPv4LspIdentidiersTlv in LSP object will have the source and destination address.
1168 StatefulIPv4LspIdentidiersTlv lspIdenTlv = null;
1169 SymbolicPathNameTlv pathNameTlv = null;
1170 LinkedList<PcepValueType> llOptionalTlv = lspObj.getOptionalTlv();
1171 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1172 while (listIterator.hasNext()) {
1173 PcepValueType tlv = listIterator.next();
1174 switch (tlv.getType()) {
1175 case StatefulIPv4LspIdentidiersTlv.TYPE:
1176 lspIdenTlv = (StatefulIPv4LspIdentidiersTlv) tlv;
1177 break;
1178 case SymbolicPathNameTlv.TYPE:
1179 pathNameTlv = (SymbolicPathNameTlv) tlv;
1180 break;
1181 default:
1182 // currently this tlv is not required
1183 }
1184 }
1185
1186 IpTunnelEndPoint tunnelEndPointSrc;
1187 tunnelEndPointSrc = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(lspIdenTlv.getIpv4IngressAddress()));
1188 IpTunnelEndPoint tunnelEndPointDst;
1189 tunnelEndPointDst = IpTunnelEndPoint.ipTunnelPoint(IpAddress.valueOf(lspIdenTlv.getIpv4EgressAddress()));
1190
1191 Path path = buildPathFromRroObj(rroObj, providerId);
1192
1193 SparseAnnotations annotations = DefaultAnnotations.builder()
1194 .set("bandwidth", (new Integer(bandwidth)).toString())
1195 .build();
1196
1197 DefaultTunnelDescription td = new DefaultTunnelDescription(null, tunnelEndPointSrc,
1198 tunnelEndPointDst, Tunnel.Type.MPLS,
1199 new DefaultGroupId(0), providerId,
1200 TunnelName.tunnelName(pathNameTlv.toString()),
1201 path, annotations);
1202 TunnelId tId = tunnelAdded(td);
1203
1204 Tunnel tunnel = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, Tunnel.Type.MPLS,
1205 new DefaultGroupId(0), tId,
1206 TunnelName.tunnelName(pathNameTlv.toString()), path, annotations);
1207
1208 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, RequestType.LSP_STATE_RPT);
1209 pcepTunnelData.setStatefulIpv4IndentifierTlv(lspIdenTlv);
Jonathan Hart51539b82015-10-29 09:53:04 -07001210 pcepTunnelApiMapper.addPccTunnelDB(pcepTunnelData);
1211 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301212 }
1213
1214 @Override
1215 public void clientConnected(PccId pccId) {
1216 // TODO
1217 }
1218
1219 @Override
1220 public void clientDisconnected(PccId pccId) {
1221 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001222 }
chengfan2fff70f2015-08-24 18:20:19 -05001223
1224
1225
1226 @Override
1227 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1228 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1229 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1230 tunnelStatisticsMap.put(id, tunnelStatistics);
1231 }
cheng fan48e832c2015-05-29 01:54:47 +08001232 }
1233
1234 @Override
1235 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1236 return service.tunnelQueryById(tunnelId);
1237 }
cheng fan48e832c2015-05-29 01:54:47 +08001238}