blob: cb1922abb99561cd3fbc254c51c6489fcdae99b9 [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;
Avantika-Huawei56c11842016-04-28 00:56:56 +053019
cheng fan48e832c2015-05-29 01:54:47 +080020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
chengfan2fff70f2015-08-24 18:20:19 -050023import org.apache.felix.scr.annotations.Property;
cheng fan48e832c2015-05-29 01:54:47 +080024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053027import org.onlab.packet.IpAddress;
chengfan2fff70f2015-08-24 18:20:19 -050028import org.onosproject.cfg.ComponentConfigService;
cheng fan48e832c2015-05-29 01:54:47 +080029import org.onosproject.core.DefaultGroupId;
30import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053031import org.onosproject.incubator.net.tunnel.DefaultTunnel;
cheng fan48e832c2015-05-29 01:54:47 +080032import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
chengfan2fff70f2015-08-24 18:20:19 -050033import org.onosproject.incubator.net.tunnel.DefaultTunnelStatistics;
Jonathan Hart51539b82015-10-29 09:53:04 -070034import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
cheng fan48e832c2015-05-29 01:54:47 +080035import org.onosproject.incubator.net.tunnel.OpticalLogicId;
36import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
37import org.onosproject.incubator.net.tunnel.Tunnel;
Avantika-Huawei56c11842016-04-28 00:56:56 +053038import org.onosproject.incubator.net.tunnel.Tunnel.State;
cheng fan48e832c2015-05-29 01:54:47 +080039import org.onosproject.incubator.net.tunnel.TunnelDescription;
40import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
41import org.onosproject.incubator.net.tunnel.TunnelId;
42import org.onosproject.incubator.net.tunnel.TunnelName;
43import org.onosproject.incubator.net.tunnel.TunnelProvider;
44import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
45import org.onosproject.incubator.net.tunnel.TunnelProviderService;
chengfan2fff70f2015-08-24 18:20:19 -050046import org.onosproject.incubator.net.tunnel.TunnelService;
47import org.onosproject.incubator.net.tunnel.TunnelStatistics;
cheng fan48e832c2015-05-29 01:54:47 +080048import org.onosproject.net.ConnectPoint;
49import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei56c11842016-04-28 00:56:56 +053050import org.onosproject.net.DefaultAnnotations.Builder;
cheng fan48e832c2015-05-29 01:54:47 +080051import org.onosproject.net.DefaultLink;
52import org.onosproject.net.DefaultPath;
53import org.onosproject.net.DeviceId;
54import org.onosproject.net.ElementId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053055import org.onosproject.net.IpElementId;
cheng fan48e832c2015-05-29 01:54:47 +080056import org.onosproject.net.Link;
57import org.onosproject.net.Path;
58import org.onosproject.net.PortNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053059import org.onosproject.net.SparseAnnotations;
cheng fan48e832c2015-05-29 01:54:47 +080060import org.onosproject.net.provider.AbstractProvider;
61import org.onosproject.net.provider.ProviderId;
62import org.onosproject.pcep.api.PcepController;
63import org.onosproject.pcep.api.PcepDpid;
64import org.onosproject.pcep.api.PcepHopNodeDescription;
65import org.onosproject.pcep.api.PcepOperator.OperationType;
66import org.onosproject.pcep.api.PcepTunnel;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053067import org.onosproject.pcep.api.PcepTunnel.PathState;
Jonathan Hart51539b82015-10-29 09:53:04 -070068import org.onosproject.pcep.api.PcepTunnel.PathType;
cheng fan48e832c2015-05-29 01:54:47 +080069import org.onosproject.pcep.api.PcepTunnelListener;
chengfan2fff70f2015-08-24 18:20:19 -050070import org.onosproject.pcep.api.PcepTunnelStatistics;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053071import org.onosproject.pcep.controller.PccId;
72import org.onosproject.pcep.controller.PcepClient;
73import org.onosproject.pcep.controller.PcepClientController;
74import org.onosproject.pcep.controller.PcepClientListener;
75import org.onosproject.pcep.controller.PcepEventListener;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +053076import org.onosproject.pcep.controller.PcepSyncStatus;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053077import org.onosproject.pcepio.exceptions.PcepParseException;
78import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
79import org.onosproject.pcepio.protocol.PcepAttribute;
80import org.onosproject.pcepio.protocol.PcepBandwidthObject;
81import org.onosproject.pcepio.protocol.PcepEndPointsObject;
82import org.onosproject.pcepio.protocol.PcepEroObject;
83import org.onosproject.pcepio.protocol.PcepInitiateMsg;
84import org.onosproject.pcepio.protocol.PcepLspObject;
85import org.onosproject.pcepio.protocol.PcepMessage;
86import org.onosproject.pcepio.protocol.PcepMsgPath;
87import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053088import org.onosproject.pcepio.protocol.PcepSrpObject;
89import org.onosproject.pcepio.protocol.PcepStateReport;
90import org.onosproject.pcepio.protocol.PcepUpdateMsg;
91import org.onosproject.pcepio.protocol.PcepUpdateRequest;
92import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +053093import org.onosproject.pcepio.types.PathSetupTypeTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053094import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huawei56c11842016-04-28 00:56:56 +053095import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053096import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -050097import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -070098import org.osgi.service.component.annotations.Modified;
99import org.slf4j.Logger;
100
101import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530102import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530103import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700104import java.util.Collections;
105import java.util.Dictionary;
106import java.util.HashMap;
107import java.util.LinkedList;
108import java.util.List;
109import java.util.ListIterator;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530110import java.util.Map;
Jonathan Hart51539b82015-10-29 09:53:04 -0700111import java.util.Optional;
112
113import static com.google.common.base.Preconditions.checkNotNull;
114import static com.google.common.base.Strings.isNullOrEmpty;
115import static org.onlab.util.Tools.get;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530116import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700117import static org.onosproject.net.DefaultAnnotations.EMPTY;
118import static org.onosproject.net.DeviceId.deviceId;
119import static org.onosproject.net.PortNumber.portNumber;
120import static org.onosproject.pcep.api.PcepDpid.uri;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530121import static org.onosproject.provider.pcep.tunnel.impl.LspType.WITH_SIGNALLING;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530122import static org.onosproject.provider.pcep.tunnel.impl.LspType.SR_WITHOUT_SIGNALLING;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530123import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.BANDWIDTH;
124import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.LOCAL_LSP_ID;
125import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.LSP_SIG_TYPE;
126import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PCC_TUNNEL_ID;
127import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PLSP_ID;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530128import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PCE_INIT;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530129import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
130import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
131import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
132import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530133import static org.onosproject.pcep.controller.PcepSyncStatus.IN_SYNC;
134import static org.onosproject.pcep.controller.PcepSyncStatus.SYNCED;
135import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
Jonathan Hart51539b82015-10-29 09:53:04 -0700136import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800137
138/**
139 * Provider which uses an PCEP controller to detect, update, create network
140 * tunnels.
141 */
142@Component(immediate = true)
143@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530144public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800145
146 private static final Logger log = getLogger(PcepTunnelProvider.class);
147 private static final long MAX_BANDWIDTH = 99999744;
148 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800149 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530150 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
cheng fan48e832c2015-05-29 01:54:47 +0800151
chengfan2fff70f2015-08-24 18:20:19 -0500152 static final int POLL_INTERVAL = 10;
153 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
154 label = "Frequency (in seconds) for polling tunnel statistics")
155 private int tunnelStatsPollFrequency = POLL_INTERVAL;
156
cheng fan48e832c2015-05-29 01:54:47 +0800157 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
160 protected TunnelProviderRegistry tunnelProviderRegistry;
161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
163 protected PcepController controller;
164
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
166 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
169 protected TunnelService tunnelService;
170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
172 protected ComponentConfigService cfgService;
173
cheng fan48e832c2015-05-29 01:54:47 +0800174 TunnelProviderService service;
175
176 HashMap<String, TunnelId> tunnelMap = new HashMap<String, TunnelId>();
chengfan2fff70f2015-08-24 18:20:19 -0500177 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400178 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800179
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530180 private InnerTunnelProvider listener = new InnerTunnelProvider();
181
Jonathan Hart51539b82015-10-29 09:53:04 -0700182 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530183 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800184
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530185 private Map<IpAddress, Map<TunnelId, Tunnel>> preSyncLspDbMap = new HashMap<>();
186 private Map<IpAddress, List<Tunnel>> syncCompleteDeleteList = new HashMap<>();
187 private Map<IpAddress, List<Tunnel>> syncCompleteUpdateList = new HashMap<>();
188
cheng fan48e832c2015-05-29 01:54:47 +0800189 /**
190 * Creates a Tunnel provider.
191 */
192 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530193 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800194 }
195
196 @Activate
197 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500198 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800199 service = tunnelProviderRegistry.register(this);
200 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530201 pcepClientController.addListener(listener);
202 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500203 tunnelService.queryAllTunnels().forEach(tunnel -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700204 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500205 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
206 tsc.start();
207 collectors.put(tunnel.tunnelId().id(), tsc);
208
209 });
210
cheng fan48e832c2015-05-29 01:54:47 +0800211 log.info("Started");
212 }
213
214 @Deactivate
215 public void deactivate() {
216 tunnelProviderRegistry.unregister(this);
217 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500218 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530219 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800220 log.info("Stopped");
221 }
222
chengfan2fff70f2015-08-24 18:20:19 -0500223 @Modified
224 public void modified(ComponentContext context) {
225 Dictionary<?, ?> properties = context.getProperties();
226 int newTunnelStatsPollFrequency;
227 try {
228 String s = get(properties, "tunnelStatsPollFrequency");
229 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
230
231 } catch (NumberFormatException | ClassCastException e) {
232 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
233 }
234
235 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
236 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
237 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
238 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
239 }
240
241 }
242
cheng fan48e832c2015-05-29 01:54:47 +0800243 @Override
244 public void setupTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530245 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530246 log.error("Tunnel Type MPLS is only supported");
247 return;
248 }
cheng fan48e832c2015-05-29 01:54:47 +0800249
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530250 // check for tunnel end points
251 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
252 log.error("Tunnel source or destination is not valid");
253 return;
254 }
255
256 // Get the pcc client
257 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
258
259 if (!(pc instanceof PcepClient)) {
260 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500261 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530262 return;
263 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530264
265 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
266 if (pc.capability().pcInstantiationCapability()) {
267 pcepSetupTunnel(tunnel, path, pc);
268 }
cheng fan48e832c2015-05-29 01:54:47 +0800269 }
270
271 @Override
272 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800273
Avantika-Huawei56c11842016-04-28 00:56:56 +0530274 if (tunnel.annotations().value(PLSP_ID) != null) {
275 updateTunnel(tunnel, path);
276 return;
277 }
278
279 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530280 log.error("Tunnel Type MPLS is only supported");
281 return;
282 }
283
284 if (!(srcElement instanceof IpElementId)) {
285 log.error("Element id is not valid");
286 return;
287 }
288
289 // check for tunnel end points
290 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
291 log.error("Tunnel source or destination is not valid");
292 return;
293 }
294
295 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
296
297 if (!(pc instanceof PcepClient)) {
298 log.error("There is no PCC connected with ip addresss {}"
299 + ((IpElementId) srcElement).ipAddress().toString());
300 return;
301 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530302
303 if (pc.capability().pcInstantiationCapability()) {
304 pcepSetupTunnel(tunnel, path, pc);
305 }
cheng fan48e832c2015-05-29 01:54:47 +0800306 }
307
308 @Override
309 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800310
Avantika-Huawei56c11842016-04-28 00:56:56 +0530311 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530312 log.error("Tunnel Type MPLS is only supported");
313 return;
314 }
315
316 // check for tunnel end points
317 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
318 log.error("Tunnel source or destination is not valid");
319 return;
320 }
321
322 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
323
324 if (!(pc instanceof PcepClient)) {
325 log.error("There is no PCC connected with ip addresss {}"
326 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
327 return;
328 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530329
330 if (pc.capability().pcInstantiationCapability()) {
331 pcepReleaseTunnel(tunnel, pc);
332 }
cheng fan48e832c2015-05-29 01:54:47 +0800333 }
334
335 @Override
336 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530337 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530338 log.error("Tunnel Type MPLS is only supported");
339 return;
340 }
cheng fan48e832c2015-05-29 01:54:47 +0800341
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530342 if (!(srcElement instanceof IpElementId)) {
343 log.error("Element id is not valid");
344 return;
345 }
346
347 // check for tunnel end points
348 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
349 log.error("Tunnel source or destination is not valid");
350 return;
351 }
352
353 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
354
355 if (!(pc instanceof PcepClient)) {
356 log.error("There is no PCC connected with ip addresss {}"
357 + ((IpElementId) srcElement).ipAddress().toString());
358 return;
359 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530360
361 if (pc.capability().pcInstantiationCapability()) {
362 pcepReleaseTunnel(tunnel, pc);
363 }
cheng fan48e832c2015-05-29 01:54:47 +0800364 }
365
366 @Override
367 public void updateTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530368 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530369 log.error("Tunnel Type MPLS is only supported");
370 return;
371 }
cheng fan48e832c2015-05-29 01:54:47 +0800372
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530373 // 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(((IpTunnelEndPoint) tunnel.src()).ip()));
380
381 if (!(pc instanceof PcepClient)) {
382 log.error("There is no PCC connected with ip addresss {}"
383 + ((IpTunnelEndPoint) tunnel.src()).ip().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 void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800394
Avantika-Huawei56c11842016-04-28 00:56:56 +0530395 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530396 log.error("Tunnel Type MPLS is only supported");
397 return;
398 }
399
400 if (!(srcElement instanceof IpElementId)) {
401 log.error("Element id is not valid");
402 return;
403 }
404
405 // check for tunnel end points
406 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
407 log.error("Tunnel source or destination is not valid");
408 return;
409 }
410
411 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
412
413 if (!(pc instanceof PcepClient)) {
414 log.error("There is no PCC connected with ip addresss {}"
415 + ((IpElementId) srcElement).ipAddress().toString());
416 return;
417 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530418
419 if (pc.capability().statefulPceCapability()) {
420 pcepUpdateTunnel(tunnel, path, pc);
421 }
cheng fan48e832c2015-05-29 01:54:47 +0800422 }
423
424 @Override
425 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530426 return handleTunnelAdded(tunnel, null);
427 }
428
429 public TunnelId tunnelAdded(TunnelDescription tunnel, State tunnelState) {
430 return handleTunnelAdded(tunnel, tunnelState);
431 }
432
433 private TunnelId handleTunnelAdded(TunnelDescription tunnel, State tunnelState) {
434
435 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700436 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530437
438 if (tunnelState == null) {
439 return service.tunnelAdded(tunnel);
440 } else {
441 return service.tunnelAdded(tunnel, tunnelState);
442 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530443 }
cheng fan48e832c2015-05-29 01:54:47 +0800444
Avantika-Huawei56c11842016-04-28 00:56:56 +0530445 long bandwidth = Long.parseLong(tunnel.annotations().value(BANDWIDTH));
cheng fan48e832c2015-05-29 01:54:47 +0800446
447 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800448 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800449 return null;
450 }
451
452 // endpoints
453 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
454 .src();
455 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
456 // devices
457 DeviceId srcId = (DeviceId) src.elementId().get();
458 DeviceId dstId = (DeviceId) dst.elementId().get();
459
460 // ports
461 long srcPort = src.portNumber().get().toLong();
462 long dstPort = dst.portNumber().get().toLong();
463
464 // type
465 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800466 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800467 return null;
468 }
469
470 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
471 dstPort, bandwidth,
472 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530473 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800474
475 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
476 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
477 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
478
479 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
480 return tunnelId;
481 }
482
483 @Override
484 public void tunnelRemoved(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530485 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700486 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530487 service.tunnelRemoved(tunnel);
488 }
489
cheng fan48e832c2015-05-29 01:54:47 +0800490 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
491 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
492 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800493 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800494 return;
495 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700496 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800497 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800498 if (!controller.deleteTunnel(pcepTunnelId)) {
499 error("Delete tunnel failed, Maybe some devices have been disconnected.");
500 return;
cheng fan48e832c2015-05-29 01:54:47 +0800501 }
502 tunnelMap.remove(pcepTunnelId);
503 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800504 }
505
506 @Override
507 public void tunnelUpdated(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530508 handleTunnelUpdate(tunnel, null);
509 }
510
511 public void tunnelUpdated(TunnelDescription tunnel, State tunnelState) {
512 handleTunnelUpdate(tunnel, tunnelState);
513 }
514
515 private void handleTunnelUpdate(TunnelDescription tunnel, State tunnelState) {
516 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700517 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530518
519 if (tunnelState == null) {
520 service.tunnelUpdated(tunnel);
521 } else {
522 service.tunnelUpdated(tunnel, tunnelState);
523 }
524 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530525 }
cheng fan48e832c2015-05-29 01:54:47 +0800526
527 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
528 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800529 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800530 return;
531 }
cheng fan7716ec92015-05-31 01:53:19 +0800532 long bandwidth = Long
533 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800534 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800535 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800536 return;
537 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700538 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800539
540 checkNotNull(pcepTunnelId, "Invalid tunnel id");
541 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
542
cheng fan7716ec92015-05-31 01:53:19 +0800543 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800544 return;
545
546 }
547 service.tunnelUpdated(tunnel);
548 }
549
cheng fan7716ec92015-05-31 01:53:19 +0800550 private void error(String info) {
551 System.err.println(info);
552 }
553
cheng fan48e832c2015-05-29 01:54:47 +0800554 // Short-hand for creating a connection point.
555 private ConnectPoint connectPoint(PcepDpid id, long port) {
556 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
557 }
558
559 // Short-hand for creating a link.
560 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800561 return DefaultLink.builder()
562 .providerId(id())
563 .src(connectPoint(src, sp))
564 .dst(connectPoint(dst, dp))
565 .type(Link.Type.TUNNEL)
566 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800567 }
568
569 // Creates a path that leads through the given devices.
570 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700571 PathType pathtype, PathState pathState) {
cheng fan48e832c2015-05-29 01:54:47 +0800572 if (hopList == null || hopList.size() == 0) {
573 return null;
574 }
575 List<Link> links = new ArrayList<>();
576 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
577 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
578 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
579 .get(i + 1).getPortNum()));
580 }
581
582 int hopNum = hopList.size() - 2;
583 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
584 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800585 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800586 .set("pathType", String.valueOf(pathtype)).build();
587 return new DefaultPath(id(), links, hopNum, extendAnnotations);
588 }
589
590 // convert the path description to a string.
591 public String pathToString(List<Link> links) {
592 StringBuilder builder = new StringBuilder();
593 builder.append("{");
594 for (Link link : links) {
595 builder.append("(Device:" + link.src().deviceId() + " Port:"
596 + link.src().port().toLong());
597 builder.append(" Device:" + link.dst().deviceId() + " Port:"
598 + link.dst().port().toLong());
599 builder.append(")");
600 }
601 builder.append("}");
602 return builder.toString();
603 }
604
605 // build a TunnelDescription.
606 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
607 TunnelId tunnelId) {
608 TunnelEndPoint srcPoint = null;
609 TunnelEndPoint dstPoint = null;
610 Tunnel.Type tunnelType = null;
611 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
612
613 // add path after codes of tunnel's path merged
614 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800615 pcepTunnel.getPathType(),
616 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800617
618 OpticalTunnelEndPoint.Type endPointType = null;
619 switch (pcepTunnel.type()) {
620 case OCH:
621 tunnelType = Tunnel.Type.OCH;
622 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
623 break;
624
625 case OTN:
626 tunnelType = Tunnel.Type.ODUK;
627 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
628 break;
629
630 case UNI:
631 tunnelType = Tunnel.Type.VLAN;
632 endPointType = null;
633 break;
634
635 default:
636 break;
637 }
638 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
639 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
640 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
641 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
642
643 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
644 Optional.of(srcPort), null,
645 endPointType,
646 OpticalLogicId.logicId(0),
647 true);
648 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
649 Optional.of(dstPort), null,
650 endPointType,
651 OpticalLogicId.logicId(0),
652 true);
653
654 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800655 DefaultAnnotations annotations = DefaultAnnotations
656 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800657 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800658 .set("bandwidth",
659 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800660 .set("index", String.valueOf(pcepTunnel.id())).build();
661
cheng fan48e832c2015-05-29 01:54:47 +0800662 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
663 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800664 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800665 DefaultAnnotations extendAnnotations = DefaultAnnotations
666 .builder()
667 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800668 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800669 annotations = DefaultAnnotations.merge(annotations,
670 extendAnnotations);
671
672 }
673 TunnelDescription tunnel = new DefaultTunnelDescription(
674 tunnelId,
675 srcPoint,
676 dstPoint,
677 tunnelType,
678 new DefaultGroupId(
679 0),
680 id(), name,
681 path,
682 annotations);
683 return tunnel;
684
685 }
686
687 /**
688 * Get the tunnelID according to the tunnel key.
689 *
690 * @param tunnelKey tunnel key
691 * @return corresponding tunnel id of the a tunnel key.
692 */
693 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800694 for (String key : tunnelMap.keySet()) {
695 if (key.equals(tunnelKey)) {
696 return tunnelMap.get(key);
697 }
698 }
699 return null;
700 }
701
702 /**
703 * Get the tunnel key according to the tunnelID.
704 *
705 * @param tunnelId tunnel id
706 * @return corresponding a tunnel key of the tunnel id.
707 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700708 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800709 for (String key : tunnelMap.keySet()) {
710 if (tunnelMap.get(key).id() == tunnelId.id()) {
711 return key;
712 }
713 }
714 return null;
715
716 }
717
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530718 /**
chengfan2fff70f2015-08-24 18:20:19 -0500719 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
720 *
721 * @param statistics statistics data from a PCEP tunnel
722 * @return TunnelStatistics
723 */
724 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
725 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
726 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
727 .setPacketLossRatio(statistics.packetLossRate())
728 .setFlowDelay(statistics.flowDelay())
729 .setAlarms(statistics.alarms())
730 .build();
731 return tunnelStatistics;
732 }
733 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530734 * Creates list of hops for ERO object from Path.
735 *
736 * @param path network path
737 * @return list of ipv4 subobjects
738 */
739 private LinkedList<PcepValueType> createPcepPath(Path path) {
740 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
741 List<Link> listLink = path.links();
742 ConnectPoint source = null;
743 ConnectPoint destination = null;
744 IpAddress ipDstAddress = null;
745 IpAddress ipSrcAddress = null;
746 PcepValueType subObj = null;
747
748 for (Link link : listLink) {
749 source = link.src();
750 if (!(source.equals(destination))) {
751 //set IPv4SubObject for ERO object
752 ipSrcAddress = source.ipElementId().ipAddress();
753 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
754 llSubObjects.add(subObj);
755 }
756
757 destination = link.dst();
758 ipDstAddress = destination.ipElementId().ipAddress();
759 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
760 llSubObjects.add(subObj);
761 }
762 return llSubObjects;
763 }
764
765 /**
766 * Creates PcInitiated lsp request list for setup tunnel.
767 *
768 * @param tunnel mpls tunnel
769 * @param path network path
770 * @param pc pcep client
771 * @param srpId unique id for pcep message
772 * @return list of PcInitiatedLspRequest
773 * @throws PcepParseException while building pcep objects fails
774 */
775 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
776 PcepClient pc, int srpId)
777 throws PcepParseException {
778 PcepValueType tlv;
779 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
780
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700781 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530782 log.error("There is no link information to create tunnel");
783 return null;
784 }
785
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530786 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530787
788 // set PathSetupTypeTlv of SRP object
789 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)).type());
790 llOptionalTlv.add(tlv);
791
792 // build SRP object
793 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
794 .setOptionalTlv(llOptionalTlv).build();
795
796 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530797 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530798
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530799 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530800 short localLspId = 0;
801 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
802 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
803 if (localLspIdString != null) {
804 localLspId = Short.valueOf(localLspIdString);
805 }
806 }
807
808 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
809 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
810 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530811 llOptionalTlv.add(tlv);
812 //set SymbolicPathNameTlv of LSP object
813 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
814 llOptionalTlv.add(tlv);
815
816 //build LSP object
817 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setOFlag((byte) 0).setPlspId(0)
818 .setOptionalTlv(llOptionalTlv).build();
819
820 //build ENDPOINTS object
821 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
822 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
823 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
824 .setPFlag(true).build();
825
826 //build ERO object
827 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
828
829 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530830 if (tunnel.annotations().value(BANDWIDTH) != null) {
831 iBandwidth = Integer.parseInt(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530832 }
833 // build bandwidth object
834 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
835 // build pcep attribute
836 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
837
838 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
839 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
840 .setPcepAttribute(pcepAttribute).build();
841 llPcInitiatedLspRequestList.add(initiateLspRequest);
842 return llPcInitiatedLspRequestList;
843 }
844
845 /**
846 * To send initiate tunnel message to pcc.
847 *
848 * @param tunnel mpls tunnel info
849 * @param path explicit route for the tunnel
850 * @param pc pcep client to send message
851 */
852 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
853 try {
854 int srpId = SrpIdGenerators.create();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530855 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530856
Jonathan Hart51539b82015-10-29 09:53:04 -0700857 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530858
859 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
860 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700861 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530862 log.error("Failed to create PcInitiatedLspRequestList");
863 return;
864 }
865
866 //build PCInitiate message
867 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
868 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
869 .build();
870
871 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
872
Jonathan Hart51539b82015-10-29 09:53:04 -0700873 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530874 } catch (PcepParseException e) {
875 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
876 }
877 }
878
879 /**
880 * To send Release tunnel message to pcc.
881 *
882 * @param tunnel mpls tunnel info
883 * @param pc pcep client to send message
884 */
885 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
886 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530887 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -0700888 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530889 int srpId = SrpIdGenerators.create();
890 TunnelId tunnelId = tunnel.tunnelId();
891 int plspId = 0;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530892
Jonathan Hart51539b82015-10-29 09:53:04 -0700893 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530894 log.error("Tunnel doesnot exists. Tunnel id {}" + tunnelId.toString());
895 return;
896 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -0700897 PcepTunnelData pcepTunnelDbData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530898 plspId = pcepTunnelDbData.plspId();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530899 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530900
901 PcepValueType tlv;
902 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530903
904 // set PathSetupTypeTlv of SRP object
905 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
906 .type());
907 llOptionalTlv.add(tlv);
908
909 // build SRP object
910 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
911 .setOptionalTlv(llOptionalTlv).build();
912
913 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530914 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
915
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530916 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
917 llOptionalTlv.add(tlv);
918 // build lsp object, set r flag as false to delete the tunnel
919 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
920 .setOptionalTlv(llOptionalTlv).build();
921
922 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
923 .setLspObject(lspobj).build();
924
925 llPcInitiatedLspRequestList.add(releaseLspRequest);
926
927 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
928 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
929
930 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
931
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
938 /**
939 * To send Update tunnel request message to pcc.
940 *
941 * @param tunnel mpls tunnel info
942 * @param path explicit route for the tunnel
943 * @param pc pcep client to send message
944 */
945 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
946 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530947 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -0700948 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530949 int srpId = SrpIdGenerators.create();
950 TunnelId tunnelId = tunnel.tunnelId();
951 PcepValueType tlv;
952 int plspId = 0;
953
954 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
955 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
956 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
957
Avantika-Huawei56c11842016-04-28 00:56:56 +0530958 // set PathSetupTypeTlv of SRP object
959 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
960 tlv = new PathSetupTypeTlv(lspSigType.type());
961 llOptionalTlv.add(tlv);
962
963 // build SRP object
964 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
965 .setOptionalTlv(llOptionalTlv).build();
966
967 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530968
Jonathan Hart51539b82015-10-29 09:53:04 -0700969 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530970 log.error("Tunnel doesnot exists in DB");
971 return;
972 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -0700973 PcepTunnelData pcepTunnelDBData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530974 plspId = pcepTunnelDBData.plspId();
975 }
976
Avantika-Huawei56c11842016-04-28 00:56:56 +0530977 if (lspSigType != WITH_SIGNALLING) {
978 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
979 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
980 short localLspId = 0;
981 short pccTunnelId = 0;
982
983 if (localLspIdString != null) {
984 localLspId = Short.valueOf(localLspIdString);
985 }
986
987 if (pccTunnelIdString != null) {
988 pccTunnelId = Short.valueOf(pccTunnelIdString);
989 }
990
991 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
992 .ip().getIp4Address().toInt()),
993 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
994 .getIp4Address().toInt()));
995 llOptionalTlv.add(tlv);
996 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530997
998 if (tunnel.tunnelName().value() != null) {
999 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1000 llOptionalTlv.add(tlv);
1001 }
1002
1003 // build lsp object
1004 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setPlspId(plspId)
1005 .setOptionalTlv(llOptionalTlv).build();
1006 // build ero object
1007 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
1008
1009 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301010 if (tunnel.annotations().value(BANDWIDTH) != null) {
1011 iBandwidth = Integer.parseInt(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301012 }
1013 // build bandwidth object
1014 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1015 // build pcep attribute
1016 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1017 // build pcep msg path
1018 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1019 .build();
1020
1021 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1022 .setLspObject(lspobj).setMsgPath(msgPath).build();
1023
1024 llUpdateRequestList.add(updateRequest);
1025
1026 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1027
1028 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001029 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301030 } catch (PcepParseException e) {
1031 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1032 }
1033 }
1034
chengfan2fff70f2015-08-24 18:20:19 -05001035
1036
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301037 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001038
1039 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001040 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001041 TunnelDescription tunnel = null;
1042 // instance and id identify a tunnel together
1043 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1044 + String.valueOf(pcepTunnel.id());
1045
1046 if (tunnelKey == null || "".equals(tunnelKey)) {
1047 log.error("Invalid PCEP tunnel");
1048 return;
1049 }
1050
1051 TunnelId tunnelId = getTunnelId(tunnelKey);
1052
1053 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1054
1055 OperationType operType = pcepTunnel.getOperationType();
1056 switch (operType) {
1057 case ADD:
1058 tunnelId = service.tunnelAdded(tunnel);
1059 tunnelMap.put(tunnelKey, tunnelId);
1060 break;
1061
1062 case UPDATE:
1063 service.tunnelUpdated(tunnel);
1064 break;
1065
1066 case DELETE:
1067 service.tunnelRemoved(tunnel);
1068 tunnelMap.remove(tunnelKey);
1069 break;
1070
1071 default:
1072 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301073 }
1074 }
cheng fan48e832c2015-05-29 01:54:47 +08001075
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301076 @Override
1077 public void handleMessage(PccId pccId, PcepMessage msg) {
1078 try {
1079 log.debug("tunnel provider handle message {}", msg.getType().toString());
1080 switch (msg.getType()) {
1081 case REPORT:
1082 int srpId = 0;
1083 LinkedList<PcepStateReport> llStateReportList = null;
1084 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1085 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1086 PcepSrpObject srpObj = null;
1087 PcepLspObject lspObj = null;
1088 while (listIterator.hasNext()) {
1089 PcepStateReport stateRpt = listIterator.next();
1090 srpObj = stateRpt.getSrpObject();
1091 lspObj = stateRpt.getLspObject();
1092
1093 if (srpObj instanceof PcepSrpObject) {
1094 srpId = srpObj.getSrpID();
1095 }
1096
1097 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1098 log.debug("SRP ID in handle message " + srpId);
1099
Jonathan Hart51539b82015-10-29 09:53:04 -07001100 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301101
1102 // Check the sync status
1103 if (lspObj.getSFlag()) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301104 if (pcepClientController.getClient(pccId).lspDbSyncStatus() != IN_SYNC) {
1105 pcepClientController.getClient(pccId).setLspDbSyncStatus(IN_SYNC);
1106
1107 // On starting LSP-DB sync, store LSP DB locally for this PCC.
1108 Map<TunnelId, Tunnel> preSyncLspDb = new HashMap<>();
1109 Collection<Tunnel> queriedTunnels = tunnelService.queryTunnel(MPLS);
1110
1111 for (Tunnel tunnel : queriedTunnels) {
1112 if (((IpTunnelEndPoint) tunnel.src()).ip().equals(pccId.ipAddress())) {
1113 preSyncLspDb.put(tunnel.tunnelId(), tunnel);
1114 }
1115 }
1116
1117 preSyncLspDbMap.put(pccId.ipAddress(), preSyncLspDb);
1118 syncCompleteDeleteList.put(pccId.ipAddress(), new LinkedList<>());
1119 syncCompleteUpdateList.put(pccId.ipAddress(), new LinkedList<>());
1120 }
1121 handleRptWithoutSrpId(stateRpt, pccId, IN_SYNC);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301122 continue;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301123
1124 } else if (pcepClientController.getClient(pccId).lspDbSyncStatus() == IN_SYNC) {
1125 // If sync flag is not set in the msg, and the
1126 // previous state was "in sync" means this is
1127 // end of sync message. PCRpt for end of sync
1128 // does not carry any LSP report.
1129 pcepClientController.getClient(pccId).setLspDbSyncStatus(SYNCED);
1130 handleEndOfSyncAction(pccId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301131 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301132 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301133
1134 // For PCRpt without matching SRP id not during LSPDB sync.
1135 handleRptWithoutSrpId(stateRpt, pccId, SYNCED);
1136 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301137 }
1138
Avantika-Huawei56c11842016-04-28 00:56:56 +05301139 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301140 }
1141 break;
1142
1143 default:
1144 log.debug("Received unsupported message type {}", msg.getType().toString());
1145 }
1146 } catch (Exception e) {
1147 log.error("Exception occured while processing report message {}", e.getMessage());
1148 }
1149 }
1150
1151 /**
1152 * Handles report message for setup/update/delete tunnel request.
1153 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301154 * @param srpId unique identifier for PCEP message
1155 * @param lspObj LSP object
1156 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301157 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301158 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301159 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001160 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301161
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301162 // store the values required from report message
1163 pcepTunnelData.setPlspId(lspObj.getPlspId());
1164 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1165 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1166 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1167
Avantika-Huawei56c11842016-04-28 00:56:56 +05301168 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301169 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1170 while (listTlvIterator.hasNext()) {
1171 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301172 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1173 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301174 break;
1175 }
1176 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001177 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301178 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001179 }
1180
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301181 Path path = pcepTunnelData.path();
1182 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301183 Builder annotationBuilder = DefaultAnnotations.builder();
1184 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1185
1186 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1187 if (tunnel.annotations().value(PLSP_ID) == null) {
1188 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1189 }
1190
1191 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1192 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1193 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1194 }
1195
1196 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301197 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1198 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1199 providerId, tunnel.tunnelName(), path,
1200 annotations);
1201
Avantika-Huawei56c11842016-04-28 00:56:56 +05301202 if (CREATE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001203 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301204 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001205 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301206 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301207 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001208 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1209 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301210 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301211
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301212 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1213
Avantika-Huawei56c11842016-04-28 00:56:56 +05301214 if (lspObj.getRFlag()) {
1215 tunnelRemoved(td);
1216 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301217 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301218 tunnelUpdated(td, tunnelState);
1219 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301220
1221 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1222 // though there are no labels to download for SR-TE.
1223 if ((pcepLspStatus == PcepLspStatus.GOING_UP)
1224 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING)) {
1225 updateTunnel(tunnel, tunnel.path());
1226 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301227 }
1228
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301229 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId, PcepSyncStatus syncStatus) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301230 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301231 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1232 checkNotNull(msgPath);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301233 PcepEroObject eroObj = msgPath.getEroObject();
1234 if (eroObj == null) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301235 log.error("ERO object is null in report message.");
Mahesh Poojary S5eab8572015-09-01 17:45:48 +05301236 return;
1237 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301238 Path path = buildPathFromEroObj(eroObj, providerId);
1239
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301240 int bandwidth = 0;
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001241 if (msgPath.getBandwidthObject() != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301242 bandwidth = msgPath.getBandwidthObject().getBandwidth();
1243 }
1244
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301245 /*
1246 * To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1247 * from PCE.
1248 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301249 PcepSrpObject srpObj = stateRpt.getSrpObject();
1250 LspType lspType = WITH_SIGNALLING;
1251
1252 if (null != srpObj) {
1253 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1254 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1255
1256 while (listIterator.hasNext()) {
1257 PcepValueType tlv = listIterator.next();
1258
1259 switch (tlv.getType()) {
1260 case PathSetupTypeTlv.TYPE:
1261 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1262 break;
1263
1264 default:
1265 break;
1266 }
1267 }
1268 }
1269
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301270 PcepLspObject lspObj = stateRpt.getLspObject();
1271 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1272 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1273 SymbolicPathNameTlv pathNameTlv = null;
1274
1275 while (listTlvIterator.hasNext()) {
1276 PcepValueType tlv = listTlvIterator.next();
1277 switch (tlv.getType()) {
1278 case StatefulIPv4LspIdentifiersTlv.TYPE:
1279 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1280 break;
1281
1282 case SymbolicPathNameTlv.TYPE:
1283 pathNameTlv = (SymbolicPathNameTlv) tlv;
1284 break;
1285
1286 default:
1287 break;
1288 }
1289 }
1290
1291 /*
1292 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1293 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1294 */
1295 if (ipv4LspIdenTlv == null) {
1296 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1297 return;
1298 }
1299
1300 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1301 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1302 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1303 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1304 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
1305
1306 Tunnel tunnel = null;
1307 // Asynchronous status change message from PCC for LSP reported earlier.
1308 for (Tunnel tunnelObj : tunnelQueryResult) {
1309 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1310 /*
1311 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1312 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1313 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1314 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1315 * session an LSP is "first" reported to a PCE.
1316 */
1317 if ((pathNameTlv != null)
1318 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1319 tunnel = tunnelObj;
1320 break;
1321 }
1322 continue;
1323 }
1324
1325 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId()) && (Integer
1326 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1327 tunnel = tunnelObj;
1328 break;
1329 }
1330 }
1331
1332 DefaultTunnelDescription td;
1333 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
1334 if (tunnel == null) {
1335 if (lspObj.getRFlag()) {
1336 /*
1337 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1338 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1339 */
1340 return;
1341 }
1342
1343 if (lspObj.getCFlag()) {
1344 /*
1345 * While in sync, if PCRpt is received for PCE init LSP and PCE doesn't have entry, mark to send
1346 * delete message on end of sync.
1347 */
1348 SparseAnnotations annotations = DefaultAnnotations.builder()
1349 .set(BANDWIDTH, (new Integer(bandwidth)).toString())
1350 .set(LSP_SIG_TYPE, lspType.name())
1351 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1352 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1353 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId())).build();
1354
1355 // Gnenerate tunnel id for the temporary tunnel.
1356 String onosTunnelId = "PCC" + String.valueOf(ipv4LspIdenTlv.getTunnelId());
1357 Tunnel tunnelToBeDeleted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1358 new DefaultGroupId(0), TunnelId.valueOf(onosTunnelId),
1359 TunnelName.tunnelName(String
1360 .valueOf(pathNameTlv.getValue())),
1361 path, annotations);
1362
1363 /*
1364 * Need to send PCInitiate delete msg for a tunnel which does not exist at PCE. For that some dummy
1365 * data-structures need to be populated.
1366 */
1367 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelToBeDeleted, path, RequestType.DELETE);
1368 pcepTunnelData.setPlspId(lspObj.getPlspId());
1369 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1370 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1371 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(0, pcepTunnelData);
1372
1373 /*
1374 * Add to the list of tunnels for which PCInit delete will be sent at the end of sync.
1375 */
1376 List<Tunnel> tunnelToBeDeletedList = syncCompleteDeleteList.get(pccId.ipAddress());
1377 tunnelToBeDeletedList.add(tunnelToBeDeleted);
1378 syncCompleteDeleteList.put(pccId.ipAddress(), tunnelToBeDeletedList);
1379 return;
1380 }
1381
1382 SparseAnnotations annotations = DefaultAnnotations.builder()
1383 .set(BANDWIDTH, (new Integer(bandwidth)).toString())
1384 .set(LSP_SIG_TYPE, lspType.name())
1385 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1386 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1387 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId())).build();
1388
1389 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1390 new DefaultGroupId(0), providerId,
1391 TunnelName.tunnelName(String.valueOf(pathNameTlv.getValue())), path,
1392 annotations);
1393
1394 TunnelId tId = tunnelAdded(td, tunnelState);
1395 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1396 tunnelState, new DefaultGroupId(0), tId,
1397 TunnelName.tunnelName(String.valueOf(pathNameTlv.getValue())),
1398 path, annotations);
1399
1400 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1401 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1402 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1403 return;
1404 }
1405
1406 if ((syncStatus == IN_SYNC) && (lspObj.getCFlag()) && (tunnelState != tunnel.state())) {
1407 // Mark to send PCUpd msg with state known at PCE.
1408 List<Tunnel> tunnelToBeUpdateList = syncCompleteUpdateList.get(pccId.ipAddress());
1409 tunnelToBeUpdateList.add(tunnel);
1410 syncCompleteUpdateList.put(pccId.ipAddress(), tunnelToBeUpdateList);
1411 return;
1412 }
1413
1414 td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1415 tunnel.type(), tunnel.groupId(), providerId,
1416 tunnel.tunnelName(), tunnel.path(),
1417 (SparseAnnotations) tunnel.annotations());
1418
1419 if (lspObj.getRFlag()) {
1420 tunnelRemoved(td);
1421 } else {
1422 if (syncStatus == IN_SYNC) {
1423 markLspDbEntryAsLatest(pccId, tunnel.tunnelId());
1424 }
1425 tunnelUpdated(td, tunnelState);
1426 }
1427 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301428 }
1429
1430 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301431 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301432 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301433 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301434 * @param providerId provider id
1435 * @return path object
1436 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301437 private Path buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId) {
1438 checkNotNull(eroObj);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301439 List<Link> links = new ArrayList<Link>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301440 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301441 if (0 == llSubObj.size()) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301442 log.error("ERO in report message does not have hop information");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301443 }
1444 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1445
1446 ConnectPoint src = null;
1447 ConnectPoint dst = null;
1448 boolean isSrcSet = false;
1449 while (tlvIterator.hasNext()) {
1450 PcepValueType subObj = tlvIterator.next();
1451 switch (subObj.getType()) {
1452
1453 case IPv4SubObject.TYPE:
1454
1455 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1456 if (!isSrcSet) {
1457 IpAddress srcIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1458 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1459 isSrcSet = true;
1460 } else {
1461 IpAddress dstIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1462 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
Ray Milkey2693bda2016-01-22 16:08:14 -08001463 Link link = DefaultLink.builder()
1464 .providerId(providerId)
1465 .src(src)
1466 .dst(dst)
1467 .type(Link.Type.DIRECT)
1468 .build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301469 links.add(link);
1470 src = dst;
1471 }
1472 break;
1473 default:
1474 // the other sub objects are not required
1475 }
1476 }
1477 return new DefaultPath(providerId, links, 0, EMPTY);
1478 }
1479
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301480 @Override
1481 public void clientConnected(PccId pccId) {
1482 // TODO
1483 }
1484
1485 @Override
1486 public void clientDisconnected(PccId pccId) {
1487 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001488 }
chengfan2fff70f2015-08-24 18:20:19 -05001489
chengfan2fff70f2015-08-24 18:20:19 -05001490 @Override
1491 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1492 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1493 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1494 tunnelStatisticsMap.put(id, tunnelStatistics);
1495 }
cheng fan48e832c2015-05-29 01:54:47 +08001496 }
1497
1498 @Override
1499 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1500 return service.tunnelQueryById(tunnelId);
1501 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301502
1503 /**
1504 * Removes the entry from temporary copy of LSPDB, signifying its status as upto date.
1505 *
1506 * @param pccId the key for temporary LSPDB
1507 * @param tunnelId the tunnel id for which information is updated.
1508 */
1509 private void markLspDbEntryAsLatest(PccId pccId, TunnelId tunnelId) {
1510 checkNotNull(pccId);
1511 checkNotNull(tunnelId);
1512
1513 Map<TunnelId, Tunnel> preSyncLspDb = preSyncLspDbMap.get(pccId.ipAddress());
1514 checkNotNull(preSyncLspDb);
1515
1516 preSyncLspDb.remove(tunnelId);
1517 preSyncLspDbMap.put(pccId.ipAddress(), preSyncLspDb);
1518 }
1519
1520 /**
1521 * Sends PCInit, PCInit(R) or PCUpd messages for initiated LSPs at the end
1522 * of LSP DB sync based on actions decided while sync was in progress. Also
1523 * triggers label DB sync.
1524 *
1525 * @param pccId the key for temporary DBs storing required end of sync
1526 * actions.
1527 */
1528 private void handleEndOfSyncAction(PccId pccId) {
1529
1530 Map<TunnelId, Tunnel> preSyncLspDb = preSyncLspDbMap.get(pccId.ipAddress());
1531 checkNotNull(preSyncLspDb);
1532
1533 for (Tunnel tunnel : preSyncLspDb.values()) {
1534
1535 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1536 tunnel.src(), tunnel.dst(),
1537 tunnel.type(),
1538 tunnel.groupId(),
1539 tunnel.providerId(),
1540 tunnel.tunnelName(),
1541 tunnel.path(),
1542 (SparseAnnotations) tunnel.annotations());
1543
1544 if ((tunnel.annotations().value(PCE_INIT) == null)
1545 || (tunnel.annotations().value(PCE_INIT).equals("false"))) {
1546
1547 tunnelRemoved(td);
1548 } else {
1549 // Send PCInit msg again after global reoptimization.
1550 tunnelUpdated(td, UNSTABLE);
1551
1552 // To remove the old tunnel from store whose PLSPID is not
1553 // recognized by ingress PCC.
1554 tunnelRemoved(td);
1555 }
1556 }
1557
1558 List<Tunnel> tunnelsToBeDeletedList = syncCompleteDeleteList.get(pccId.ipAddress());
1559 checkNotNull(tunnelsToBeDeletedList);
1560 for (Tunnel tunnel: tunnelsToBeDeletedList) {
1561 releaseTunnel(tunnel);
1562 }
1563
1564 List<Tunnel> tunnelsToBeUpdatedList = syncCompleteUpdateList.get(pccId.ipAddress());
1565 checkNotNull(tunnelsToBeUpdatedList);
1566 for (Tunnel tunnel: tunnelsToBeUpdatedList) {
1567 updateTunnel(tunnel, tunnel.path());
1568 }
1569
1570 /* On end of sync, empty all temporary data structures. */
1571 preSyncLspDbMap.remove(pccId.ipAddress());
1572 syncCompleteDeleteList.remove(pccId.ipAddress());
1573 syncCompleteUpdateList.remove(pccId.ipAddress());
1574
1575 // TODO: If SR capable, send a notification to
1576 // PCE APP to start label DB sync.
1577 if (true) {
1578 pcepClientController.getClient(pccId).setLabelDbSyncStatus(IN_SYNC);
1579 }
1580 }
cheng fan48e832c2015-05-29 01:54:47 +08001581}