blob: f821f21a287663948d6fc73e1c8433e60a1f7010 [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;
Priyanka Bcdf9b102016-06-07 20:01:38 +053027import org.onlab.packet.Ip4Address;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053028import org.onlab.packet.IpAddress;
chengfan2fff70f2015-08-24 18:20:19 -050029import org.onosproject.cfg.ComponentConfigService;
cheng fan48e832c2015-05-29 01:54:47 +080030import org.onosproject.core.DefaultGroupId;
Avantika-Huaweif849aab2016-06-21 22:29:15 +053031import org.onosproject.incubator.net.resource.label.LabelResourceId;
32import org.onosproject.incubator.net.tunnel.DefaultLabelStack;
cheng fan48e832c2015-05-29 01:54:47 +080033import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053034import org.onosproject.incubator.net.tunnel.DefaultTunnel;
cheng fan48e832c2015-05-29 01:54:47 +080035import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
chengfan2fff70f2015-08-24 18:20:19 -050036import org.onosproject.incubator.net.tunnel.DefaultTunnelStatistics;
Jonathan Hart51539b82015-10-29 09:53:04 -070037import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
cheng fan48e832c2015-05-29 01:54:47 +080038import org.onosproject.incubator.net.tunnel.OpticalLogicId;
39import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
40import org.onosproject.incubator.net.tunnel.Tunnel;
Avantika-Huawei56c11842016-04-28 00:56:56 +053041import org.onosproject.incubator.net.tunnel.Tunnel.State;
Priyanka B413fbe82016-05-26 11:44:45 +053042import org.onosproject.incubator.net.tunnel.TunnelAdminService;
cheng fan48e832c2015-05-29 01:54:47 +080043import org.onosproject.incubator.net.tunnel.TunnelDescription;
44import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
45import org.onosproject.incubator.net.tunnel.TunnelId;
46import org.onosproject.incubator.net.tunnel.TunnelName;
47import org.onosproject.incubator.net.tunnel.TunnelProvider;
48import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
49import org.onosproject.incubator.net.tunnel.TunnelProviderService;
chengfan2fff70f2015-08-24 18:20:19 -050050import org.onosproject.incubator.net.tunnel.TunnelService;
51import org.onosproject.incubator.net.tunnel.TunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053052import org.onosproject.mastership.MastershipService;
53import org.onosproject.net.AnnotationKeys;
cheng fan48e832c2015-05-29 01:54:47 +080054import org.onosproject.net.ConnectPoint;
55import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei56c11842016-04-28 00:56:56 +053056import org.onosproject.net.DefaultAnnotations.Builder;
cheng fan48e832c2015-05-29 01:54:47 +080057import org.onosproject.net.DefaultLink;
58import org.onosproject.net.DefaultPath;
Priyanka B413fbe82016-05-26 11:44:45 +053059import org.onosproject.net.Device;
cheng fan48e832c2015-05-29 01:54:47 +080060import org.onosproject.net.DeviceId;
61import org.onosproject.net.ElementId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053062import org.onosproject.net.IpElementId;
cheng fan48e832c2015-05-29 01:54:47 +080063import org.onosproject.net.Link;
Avantika-Huaweif849aab2016-06-21 22:29:15 +053064import org.onosproject.net.NetworkResource;
cheng fan48e832c2015-05-29 01:54:47 +080065import org.onosproject.net.Path;
66import org.onosproject.net.PortNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053067import org.onosproject.net.SparseAnnotations;
Priyanka B413fbe82016-05-26 11:44:45 +053068import org.onosproject.net.device.DeviceService;
Priyanka Bc1e4e4c2016-07-01 14:57:19 +053069import org.onosproject.net.link.LinkService;
cheng fan48e832c2015-05-29 01:54:47 +080070import org.onosproject.net.provider.AbstractProvider;
71import org.onosproject.net.provider.ProviderId;
72import org.onosproject.pcep.api.PcepController;
73import org.onosproject.pcep.api.PcepDpid;
74import org.onosproject.pcep.api.PcepHopNodeDescription;
75import org.onosproject.pcep.api.PcepOperator.OperationType;
76import org.onosproject.pcep.api.PcepTunnel;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053077import org.onosproject.pcep.api.PcepTunnel.PathState;
Jonathan Hart51539b82015-10-29 09:53:04 -070078import org.onosproject.pcep.api.PcepTunnel.PathType;
cheng fan48e832c2015-05-29 01:54:47 +080079import org.onosproject.pcep.api.PcepTunnelListener;
chengfan2fff70f2015-08-24 18:20:19 -050080import org.onosproject.pcep.api.PcepTunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053081import org.onosproject.pcep.controller.LspKey;
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +053082import org.onosproject.pcep.controller.LspType;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053083import org.onosproject.pcep.controller.PccId;
84import org.onosproject.pcep.controller.PcepClient;
85import org.onosproject.pcep.controller.PcepClientController;
86import org.onosproject.pcep.controller.PcepClientListener;
87import org.onosproject.pcep.controller.PcepEventListener;
Priyanka B259847d2016-06-03 21:28:35 +053088import org.onosproject.pcep.controller.PcepLspStatus;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053089import org.onosproject.pcep.controller.PcepLspSyncAction;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +053090import org.onosproject.pcep.controller.SrpIdGenerators;
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +053091import org.onosproject.pcep.controller.PcepSyncStatus;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053092import org.onosproject.pcepio.exceptions.PcepParseException;
93import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
94import org.onosproject.pcepio.protocol.PcepAttribute;
95import org.onosproject.pcepio.protocol.PcepBandwidthObject;
96import org.onosproject.pcepio.protocol.PcepEndPointsObject;
97import org.onosproject.pcepio.protocol.PcepEroObject;
98import org.onosproject.pcepio.protocol.PcepInitiateMsg;
99import org.onosproject.pcepio.protocol.PcepLspObject;
100import org.onosproject.pcepio.protocol.PcepMessage;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530101import org.onosproject.pcepio.protocol.PcepMetricObject;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530102import org.onosproject.pcepio.protocol.PcepMsgPath;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530103import org.onosproject.pcepio.protocol.PcepNai;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530104import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530105import org.onosproject.pcepio.protocol.PcepSrpObject;
106import org.onosproject.pcepio.protocol.PcepStateReport;
107import org.onosproject.pcepio.protocol.PcepUpdateMsg;
108import org.onosproject.pcepio.protocol.PcepUpdateRequest;
109import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530110import org.onosproject.pcepio.types.PathSetupTypeTlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530111import org.onosproject.pcepio.types.PcepNaiIpv4Adjacency;
112import org.onosproject.pcepio.types.PcepNaiIpv4NodeId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530113import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530114import org.onosproject.pcepio.types.SrEroSubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530115import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530116import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -0500117import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -0700118import org.osgi.service.component.annotations.Modified;
119import org.slf4j.Logger;
120
121import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530122import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530123import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700124import java.util.Collections;
125import java.util.Dictionary;
126import java.util.HashMap;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530127import java.util.Iterator;
Jonathan Hart51539b82015-10-29 09:53:04 -0700128import java.util.LinkedList;
129import java.util.List;
130import java.util.ListIterator;
Sho SHIMIZUc218bfa2016-08-18 14:22:50 -0700131import java.util.Objects;
Jonathan Hart51539b82015-10-29 09:53:04 -0700132import java.util.Optional;
Priyanka B413fbe82016-05-26 11:44:45 +0530133import java.util.concurrent.Executors;
134import java.util.concurrent.ScheduledExecutorService;
135import java.util.concurrent.TimeUnit;
Jonathan Hart51539b82015-10-29 09:53:04 -0700136
137import static com.google.common.base.Preconditions.checkNotNull;
138import static com.google.common.base.Strings.isNullOrEmpty;
139import static org.onlab.util.Tools.get;
Priyanka B4c3cef02016-06-14 20:27:53 +0530140import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530141import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700142import static org.onosproject.net.DefaultAnnotations.EMPTY;
143import static org.onosproject.net.DeviceId.deviceId;
144import static org.onosproject.net.PortNumber.portNumber;
145import static org.onosproject.pcep.api.PcepDpid.uri;
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530146import static org.onosproject.pcep.controller.LspType.WITH_SIGNALLING;
147import static org.onosproject.pcep.controller.LspType.SR_WITHOUT_SIGNALLING;
148import static org.onosproject.pcep.controller.LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530149import static org.onosproject.pcep.controller.PcepAnnotationKeys.BANDWIDTH;
150import static org.onosproject.pcep.controller.PcepAnnotationKeys.LOCAL_LSP_ID;
151import static org.onosproject.pcep.controller.PcepAnnotationKeys.LSP_SIG_TYPE;
152import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCC_TUNNEL_ID;
Priyanka B4c3cef02016-06-14 20:27:53 +0530153import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCE_INIT;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530154import static org.onosproject.pcep.controller.PcepAnnotationKeys.PLSP_ID;
155import static org.onosproject.pcep.controller.PcepAnnotationKeys.DELEGATE;
156import static org.onosproject.pcep.controller.PcepAnnotationKeys.COST_TYPE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530157import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
158import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
159import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
160import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530161import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530162import static org.onosproject.pcep.controller.PcepLspSyncAction.REMOVE;
163import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_UPDATE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530164import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.IGP_METRIC;
165import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.TE_METRIC;
Jonathan Hart51539b82015-10-29 09:53:04 -0700166import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800167
168/**
169 * Provider which uses an PCEP controller to detect, update, create network
170 * tunnels.
171 */
172@Component(immediate = true)
173@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530174public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800175
176 private static final Logger log = getLogger(PcepTunnelProvider.class);
177 private static final long MAX_BANDWIDTH = 99999744;
178 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800179 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530180 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
Priyanka B413fbe82016-05-26 11:44:45 +0530181 public static final long IDENTIFIER_SET = 0x100000000L;
182 public static final long SET = 0xFFFFFFFFL;
183 private static final int DELAY = 2;
184 private static final int WAIT_TIME = 5;
185 public static final String LSRID = "lsrId";
cheng fan48e832c2015-05-29 01:54:47 +0800186
chengfan2fff70f2015-08-24 18:20:19 -0500187 static final int POLL_INTERVAL = 10;
188 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
189 label = "Frequency (in seconds) for polling tunnel statistics")
190 private int tunnelStatsPollFrequency = POLL_INTERVAL;
191
cheng fan48e832c2015-05-29 01:54:47 +0800192 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
195 protected TunnelProviderRegistry tunnelProviderRegistry;
196
197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
198 protected PcepController controller;
199
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530200 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
201 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500202
203 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
204 protected TunnelService tunnelService;
205
206 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
207 protected ComponentConfigService cfgService;
208
Priyanka B413fbe82016-05-26 11:44:45 +0530209 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
210 protected TunnelAdminService tunnelAdminService;
211
212 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
213 protected MastershipService mastershipService;
214
215 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
216 protected DeviceService deviceService;
217
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530218 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
219 protected LinkService linkService;
220
cheng fan48e832c2015-05-29 01:54:47 +0800221 TunnelProviderService service;
222
223 HashMap<String, TunnelId> tunnelMap = new HashMap<String, TunnelId>();
chengfan2fff70f2015-08-24 18:20:19 -0500224 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400225 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800226
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530227 private InnerTunnelProvider listener = new InnerTunnelProvider();
228
Jonathan Hart51539b82015-10-29 09:53:04 -0700229 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530230 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800231
232 /**
233 * Creates a Tunnel provider.
234 */
235 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530236 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800237 }
238
239 @Activate
240 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500241 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800242 service = tunnelProviderRegistry.register(this);
243 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530244 pcepClientController.addListener(listener);
245 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500246 tunnelService.queryAllTunnels().forEach(tunnel -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700247 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500248 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
249 tsc.start();
250 collectors.put(tunnel.tunnelId().id(), tsc);
251
252 });
253
cheng fan48e832c2015-05-29 01:54:47 +0800254 log.info("Started");
255 }
256
257 @Deactivate
258 public void deactivate() {
259 tunnelProviderRegistry.unregister(this);
260 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500261 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530262 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800263 log.info("Stopped");
264 }
265
chengfan2fff70f2015-08-24 18:20:19 -0500266 @Modified
267 public void modified(ComponentContext context) {
268 Dictionary<?, ?> properties = context.getProperties();
269 int newTunnelStatsPollFrequency;
270 try {
271 String s = get(properties, "tunnelStatsPollFrequency");
272 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
273
274 } catch (NumberFormatException | ClassCastException e) {
275 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
276 }
277
278 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
279 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
280 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
281 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
282 }
283
284 }
285
cheng fan48e832c2015-05-29 01:54:47 +0800286 @Override
287 public void setupTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530288 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530289 log.error("Tunnel Type MPLS is only supported");
290 return;
291 }
cheng fan48e832c2015-05-29 01:54:47 +0800292
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530293 // check for tunnel end points
294 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
295 log.error("Tunnel source or destination is not valid");
296 return;
297 }
298
299 // Get the pcc client
300 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
301
302 if (!(pc instanceof PcepClient)) {
303 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500304 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530305 return;
306 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530307
308 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
Priyanka B413fbe82016-05-26 11:44:45 +0530309 //Only master will initiate setup tunnel
310 if (pc.capability().pcInstantiationCapability() && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530311 pcepSetupTunnel(tunnel, path, pc);
312 }
cheng fan48e832c2015-05-29 01:54:47 +0800313 }
314
315 @Override
316 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800317
Priyanka B4c3cef02016-06-14 20:27:53 +0530318 //TODO: tunnel which is passed doesn't have tunnelID
Avantika-Huawei56c11842016-04-28 00:56:56 +0530319 if (tunnel.annotations().value(PLSP_ID) != null) {
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530320 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITHOUT_SIGNALLING_AND_WITHOUT_SR) {
321 // For CR LSPs, BGP flow provider will send update message after pushing labels.
322 updateTunnel(tunnel, path);
323 }
Avantika-Huawei56c11842016-04-28 00:56:56 +0530324 return;
325 }
326
327 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530328 log.error("Tunnel Type MPLS is only supported");
329 return;
330 }
331
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530332 // check for tunnel end points
333 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
334 log.error("Tunnel source or destination is not valid");
335 return;
336 }
337
Priyanka Bcdf9b102016-06-07 20:01:38 +0530338 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530339
340 if (!(pc instanceof PcepClient)) {
Priyanka B4c3cef02016-06-14 20:27:53 +0530341 log.error("There is no PCC connected with this device {}"
342 + srcElement.toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530343 return;
344 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530345
Priyanka B413fbe82016-05-26 11:44:45 +0530346 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
347 //Only master will initiate setup tunnel
348 if (pc.capability().pcInstantiationCapability()
349 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530350 pcepSetupTunnel(tunnel, path, pc);
351 }
cheng fan48e832c2015-05-29 01:54:47 +0800352 }
353
354 @Override
355 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800356
Avantika-Huawei56c11842016-04-28 00:56:56 +0530357 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530358 log.error("Tunnel Type MPLS is only supported");
359 return;
360 }
361
362 // check for tunnel end points
363 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
364 log.error("Tunnel source or destination is not valid");
365 return;
366 }
367
368 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
369
370 if (!(pc instanceof PcepClient)) {
371 log.error("There is no PCC connected with ip addresss {}"
372 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
373 return;
374 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530375
Priyanka B413fbe82016-05-26 11:44:45 +0530376 //Only master will release tunnel
377 if (pc.capability().pcInstantiationCapability()
378 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530379 pcepReleaseTunnel(tunnel, pc);
380 }
cheng fan48e832c2015-05-29 01:54:47 +0800381 }
382
383 @Override
384 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530385 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530386 log.error("Tunnel Type MPLS is only supported");
387 return;
388 }
cheng fan48e832c2015-05-29 01:54:47 +0800389
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530390 if (!(srcElement instanceof IpElementId)) {
391 log.error("Element id is not valid");
392 return;
393 }
394
395 // check for tunnel end points
396 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
397 log.error("Tunnel source or destination is not valid");
398 return;
399 }
400
401 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
402
403 if (!(pc instanceof PcepClient)) {
404 log.error("There is no PCC connected with ip addresss {}"
405 + ((IpElementId) srcElement).ipAddress().toString());
406 return;
407 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530408
Priyanka B413fbe82016-05-26 11:44:45 +0530409 //Only master will release tunnel
410 if (pc.capability().pcInstantiationCapability()
411 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530412 pcepReleaseTunnel(tunnel, pc);
413 }
cheng fan48e832c2015-05-29 01:54:47 +0800414 }
415
416 @Override
417 public void updateTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530418 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530419 log.error("Tunnel Type MPLS is only supported");
420 return;
421 }
cheng fan48e832c2015-05-29 01:54:47 +0800422
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530423 // check for tunnel end points
424 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
425 log.error("Tunnel source or destination is not valid");
426 return;
427 }
428
Priyanka B4c3cef02016-06-14 20:27:53 +0530429 //To get new tunnel ID (modified tunnel ID)
430 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
431 for (Tunnel t : tunnels) {
432 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
433 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
434 tunnel.dst(), tunnel.type(),
435 t.state(), tunnel.groupId(),
436 t.tunnelId(),
437 tunnel.tunnelName(),
438 tunnel.path(),
439 tunnel.resource(),
440 tunnel.annotations());
441 break;
442 }
443 }
444
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530445 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
446
447 if (!(pc instanceof PcepClient)) {
448 log.error("There is no PCC connected with ip addresss {}"
449 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
450 return;
451 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530452
Priyanka B4c3cef02016-06-14 20:27:53 +0530453 //PCInitiate tunnels are always have D flag set, else check for tunnels who are delegated via LspKey
454 if (pc.capability().statefulPceCapability()) {
455 if (tunnel.annotations().value(PCE_INIT) != null && tunnel.annotations().value(PCE_INIT).equals("true")) {
456 pcepUpdateTunnel(tunnel, path, pc);
Priyanka B4c3b4512016-07-22 11:41:49 +0530457 } else {
458 // If delegation flag is set then only send update message[means delegated PCE can send update msg for
459 // that LSP. If annotation is null D flag is not set else it is set.
460 Short localLspId = 0;
461 for (Tunnel t : tunnels) {
462 if (!t.tunnelId().equals(tunnel.tunnelId()) && t.tunnelName().equals(tunnel.tunnelName())) {
463 localLspId = Short.valueOf(t.annotations().value(LOCAL_LSP_ID));
464 }
465 }
466
467 if (localLspId == 0) {
468 log.error("Local LSP ID for old tunnel not found");
469 return;
470 }
471
472 if (pc.delegationInfo(new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)),
473 localLspId.shortValue())) != null) {
474
475 pcepUpdateTunnel(tunnel, path, pc);
476 }
Priyanka B4c3cef02016-06-14 20:27:53 +0530477 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530478 }
cheng fan48e832c2015-05-29 01:54:47 +0800479 }
480
481 @Override
482 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800483
Avantika-Huawei56c11842016-04-28 00:56:56 +0530484 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530485 log.error("Tunnel Type MPLS is only supported");
486 return;
487 }
488
489 if (!(srcElement instanceof IpElementId)) {
490 log.error("Element id is not valid");
491 return;
492 }
493
494 // check for tunnel end points
495 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
496 log.error("Tunnel source or destination is not valid");
497 return;
498 }
499
500 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
501
502 if (!(pc instanceof PcepClient)) {
503 log.error("There is no PCC connected with ip addresss {}"
504 + ((IpElementId) srcElement).ipAddress().toString());
505 return;
506 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530507
Priyanka B413fbe82016-05-26 11:44:45 +0530508 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
509 // LSP].If annotation is null D flag is not set else it is set.
510 if (pc.capability().statefulPceCapability()
511 && pc.delegationInfo(
512 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
513 .annotations().value(LOCAL_LSP_ID)))) != null) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530514 pcepUpdateTunnel(tunnel, path, pc);
515 }
cheng fan48e832c2015-05-29 01:54:47 +0800516 }
517
518 @Override
519 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530520 return handleTunnelAdded(tunnel, null);
521 }
522
523 public TunnelId tunnelAdded(TunnelDescription tunnel, State tunnelState) {
524 return handleTunnelAdded(tunnel, tunnelState);
525 }
526
527 private TunnelId handleTunnelAdded(TunnelDescription tunnel, State tunnelState) {
528
529 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700530 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530531
532 if (tunnelState == null) {
533 return service.tunnelAdded(tunnel);
534 } else {
535 return service.tunnelAdded(tunnel, tunnelState);
536 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530537 }
cheng fan48e832c2015-05-29 01:54:47 +0800538
Avantika-Huawei56c11842016-04-28 00:56:56 +0530539 long bandwidth = Long.parseLong(tunnel.annotations().value(BANDWIDTH));
cheng fan48e832c2015-05-29 01:54:47 +0800540
541 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800542 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800543 return null;
544 }
545
546 // endpoints
547 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
548 .src();
549 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
550 // devices
551 DeviceId srcId = (DeviceId) src.elementId().get();
552 DeviceId dstId = (DeviceId) dst.elementId().get();
553
554 // ports
555 long srcPort = src.portNumber().get().toLong();
556 long dstPort = dst.portNumber().get().toLong();
557
558 // type
559 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800560 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800561 return null;
562 }
563
564 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
565 dstPort, bandwidth,
566 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530567 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800568
569 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
570 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
571 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
572
573 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
574 return tunnelId;
575 }
576
Priyanka B4c3b4512016-07-22 11:41:49 +0530577 private void tunnelUpdated(Tunnel tunnel, Path path, State tunnelState) {
578 handleTunnelUpdate(tunnel, path, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +0530579 }
580
581 //Handles tunnel updated using tunnel admin service[specially to update annotations].
Priyanka B4c3b4512016-07-22 11:41:49 +0530582 private void handleTunnelUpdate(Tunnel tunnel, Path path, State tunnelState) {
Priyanka B413fbe82016-05-26 11:44:45 +0530583
584 if (tunnel.type() == MPLS) {
585 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.tunnelId());
586
Priyanka B4c3b4512016-07-22 11:41:49 +0530587 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
588 tunnel.type(), tunnel.groupId(), tunnel.providerId(),
589 tunnel.tunnelName(), path, tunnel.resource(),
590 (SparseAnnotations) tunnel.annotations());
Priyanka B413fbe82016-05-26 11:44:45 +0530591
Priyanka B4c3b4512016-07-22 11:41:49 +0530592 service.tunnelUpdated(td, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +0530593 return;
594 }
595
596 Tunnel tunnelOld = tunnelQueryById(tunnel.tunnelId());
597 if (tunnelOld.type() != Tunnel.Type.VLAN) {
598 error("Illegal tunnel type. Only support VLAN tunnel update.");
599 return;
600 }
601
602 long bandwidth = Long
603 .parseLong(tunnel.annotations().value("bandwidth"));
604 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
605 error("Update failed, invalid bandwidth.");
606 return;
607 }
608 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
609
610 checkNotNull(pcepTunnelId, "Invalid tunnel id");
611 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
612 error("Update failed,maybe invalid bandwidth.");
613 return;
614 }
615 tunnelAdminService.updateTunnel(tunnel, path);
616 }
617
cheng fan48e832c2015-05-29 01:54:47 +0800618 @Override
619 public void tunnelRemoved(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530620 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700621 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530622 service.tunnelRemoved(tunnel);
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530623 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530624 }
625
cheng fan48e832c2015-05-29 01:54:47 +0800626 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
627 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
628 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800629 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800630 return;
631 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700632 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800633 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800634 if (!controller.deleteTunnel(pcepTunnelId)) {
635 error("Delete tunnel failed, Maybe some devices have been disconnected.");
636 return;
cheng fan48e832c2015-05-29 01:54:47 +0800637 }
638 tunnelMap.remove(pcepTunnelId);
639 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800640 }
641
642 @Override
643 public void tunnelUpdated(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530644 handleTunnelUpdate(tunnel, null);
645 }
646
647 public void tunnelUpdated(TunnelDescription tunnel, State tunnelState) {
648 handleTunnelUpdate(tunnel, tunnelState);
649 }
650
651 private void handleTunnelUpdate(TunnelDescription tunnel, State tunnelState) {
652 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700653 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530654
655 if (tunnelState == null) {
656 service.tunnelUpdated(tunnel);
657 } else {
658 service.tunnelUpdated(tunnel, tunnelState);
659 }
660 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530661 }
cheng fan48e832c2015-05-29 01:54:47 +0800662
663 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
664 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800665 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800666 return;
667 }
cheng fan7716ec92015-05-31 01:53:19 +0800668 long bandwidth = Long
669 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800670 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800671 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800672 return;
673 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700674 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800675
676 checkNotNull(pcepTunnelId, "Invalid tunnel id");
677 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
678
cheng fan7716ec92015-05-31 01:53:19 +0800679 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800680 return;
681
682 }
683 service.tunnelUpdated(tunnel);
684 }
685
cheng fan7716ec92015-05-31 01:53:19 +0800686 private void error(String info) {
687 System.err.println(info);
688 }
689
cheng fan48e832c2015-05-29 01:54:47 +0800690 // Short-hand for creating a connection point.
691 private ConnectPoint connectPoint(PcepDpid id, long port) {
692 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
693 }
694
695 // Short-hand for creating a link.
696 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800697 return DefaultLink.builder()
698 .providerId(id())
699 .src(connectPoint(src, sp))
700 .dst(connectPoint(dst, dp))
701 .type(Link.Type.TUNNEL)
702 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800703 }
704
705 // Creates a path that leads through the given devices.
706 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700707 PathType pathtype, PathState pathState) {
cheng fan48e832c2015-05-29 01:54:47 +0800708 if (hopList == null || hopList.size() == 0) {
709 return null;
710 }
711 List<Link> links = new ArrayList<>();
712 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
713 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
714 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
715 .get(i + 1).getPortNum()));
716 }
717
718 int hopNum = hopList.size() - 2;
719 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
720 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800721 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800722 .set("pathType", String.valueOf(pathtype)).build();
723 return new DefaultPath(id(), links, hopNum, extendAnnotations);
724 }
725
726 // convert the path description to a string.
727 public String pathToString(List<Link> links) {
728 StringBuilder builder = new StringBuilder();
729 builder.append("{");
730 for (Link link : links) {
731 builder.append("(Device:" + link.src().deviceId() + " Port:"
732 + link.src().port().toLong());
733 builder.append(" Device:" + link.dst().deviceId() + " Port:"
734 + link.dst().port().toLong());
735 builder.append(")");
736 }
737 builder.append("}");
738 return builder.toString();
739 }
740
741 // build a TunnelDescription.
742 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
743 TunnelId tunnelId) {
744 TunnelEndPoint srcPoint = null;
745 TunnelEndPoint dstPoint = null;
746 Tunnel.Type tunnelType = null;
747 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
748
749 // add path after codes of tunnel's path merged
750 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800751 pcepTunnel.getPathType(),
752 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800753
754 OpticalTunnelEndPoint.Type endPointType = null;
755 switch (pcepTunnel.type()) {
756 case OCH:
757 tunnelType = Tunnel.Type.OCH;
758 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
759 break;
760
761 case OTN:
762 tunnelType = Tunnel.Type.ODUK;
763 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
764 break;
765
766 case UNI:
767 tunnelType = Tunnel.Type.VLAN;
768 endPointType = null;
769 break;
770
771 default:
772 break;
773 }
774 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
775 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
776 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
777 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
778
779 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
780 Optional.of(srcPort), null,
781 endPointType,
782 OpticalLogicId.logicId(0),
783 true);
784 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
785 Optional.of(dstPort), null,
786 endPointType,
787 OpticalLogicId.logicId(0),
788 true);
789
790 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800791 DefaultAnnotations annotations = DefaultAnnotations
792 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800793 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800794 .set("bandwidth",
795 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800796 .set("index", String.valueOf(pcepTunnel.id())).build();
797
cheng fan48e832c2015-05-29 01:54:47 +0800798 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
799 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800800 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800801 DefaultAnnotations extendAnnotations = DefaultAnnotations
802 .builder()
803 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800804 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800805 annotations = DefaultAnnotations.merge(annotations,
806 extendAnnotations);
807
808 }
809 TunnelDescription tunnel = new DefaultTunnelDescription(
810 tunnelId,
811 srcPoint,
812 dstPoint,
813 tunnelType,
814 new DefaultGroupId(
815 0),
816 id(), name,
817 path,
818 annotations);
819 return tunnel;
cheng fan48e832c2015-05-29 01:54:47 +0800820 }
821
822 /**
823 * Get the tunnelID according to the tunnel key.
824 *
825 * @param tunnelKey tunnel key
826 * @return corresponding tunnel id of the a tunnel key.
827 */
828 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800829 for (String key : tunnelMap.keySet()) {
830 if (key.equals(tunnelKey)) {
831 return tunnelMap.get(key);
832 }
833 }
834 return null;
835 }
836
837 /**
838 * Get the tunnel key according to the tunnelID.
839 *
840 * @param tunnelId tunnel id
841 * @return corresponding a tunnel key of the tunnel id.
842 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700843 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800844 for (String key : tunnelMap.keySet()) {
Sho SHIMIZUc218bfa2016-08-18 14:22:50 -0700845 if (Objects.equals(tunnelMap.get(key).id(), tunnelId.id())) {
cheng fan48e832c2015-05-29 01:54:47 +0800846 return key;
847 }
848 }
849 return null;
850
851 }
852
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530853 /**
chengfan2fff70f2015-08-24 18:20:19 -0500854 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
855 *
856 * @param statistics statistics data from a PCEP tunnel
857 * @return TunnelStatistics
858 */
859 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
860 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
861 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
862 .setPacketLossRatio(statistics.packetLossRate())
863 .setFlowDelay(statistics.flowDelay())
864 .setAlarms(statistics.alarms())
865 .build();
866 return tunnelStatistics;
867 }
868 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530869 * Creates list of hops for ERO object from Path.
870 *
871 * @param path network path
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530872 * @return list of ERO subobjects
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530873 */
874 private LinkedList<PcepValueType> createPcepPath(Path path) {
875 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
876 List<Link> listLink = path.links();
877 ConnectPoint source = null;
878 ConnectPoint destination = null;
879 IpAddress ipDstAddress = null;
880 IpAddress ipSrcAddress = null;
881 PcepValueType subObj = null;
Priyanka Bcdf9b102016-06-07 20:01:38 +0530882 long portNo;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530883
884 for (Link link : listLink) {
885 source = link.src();
886 if (!(source.equals(destination))) {
887 //set IPv4SubObject for ERO object
Priyanka Bcdf9b102016-06-07 20:01:38 +0530888 portNo = source.port().toLong();
889 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
890 ipSrcAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530891 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
892 llSubObjects.add(subObj);
893 }
894
895 destination = link.dst();
Priyanka Bcdf9b102016-06-07 20:01:38 +0530896 portNo = destination.port().toLong();
897 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
898 ipDstAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530899 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
900 llSubObjects.add(subObj);
901 }
Priyanka Bcdf9b102016-06-07 20:01:38 +0530902
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530903 return llSubObjects;
904 }
905
906 /**
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530907 * Creates label stack for ERO object from network resource.
908 *
909 * @param labelStack
910 * @param path (hop list)
911 * @return list of ERO subobjects
912 */
913 private LinkedList<PcepValueType> createPcepLabelStack(DefaultLabelStack labelStack, Path path) {
914 checkNotNull(labelStack);
915
916 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
917 Iterator<Link> links = path.links().iterator();
918 LabelResourceId label = null;
919 Link link = null;
920 PcepValueType subObj = null;
921 PcepNai nai = null;
922 Device dstNode = null;
923 long srcPortNo, dstPortNo;
924
925 ListIterator<LabelResourceId> labelListIterator = labelStack.labelResources().listIterator();
926 while (labelListIterator.hasNext()) {
927 label = labelListIterator.next();
928 link = links.next();
929
930 srcPortNo = link.src().port().toLong();
931 srcPortNo = ((srcPortNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? srcPortNo & SET : srcPortNo;
932
933 dstPortNo = link.dst().port().toLong();
934 dstPortNo = ((dstPortNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? dstPortNo & SET : dstPortNo;
935
936 nai = new PcepNaiIpv4Adjacency((int) srcPortNo, (int) dstPortNo);
937 subObj = new SrEroSubObject(PcepNaiIpv4Adjacency.ST_TYPE, false, false, false, true, (int) label.labelId(),
938 nai);
939 llSubObjects.add(subObj);
940
941 dstNode = deviceService.getDevice(link.dst().deviceId());
942 nai = new PcepNaiIpv4NodeId(Ip4Address.valueOf(dstNode.annotations().value(LSRID)).toInt());
943
944 if (!labelListIterator.hasNext()) {
945 log.error("Malformed label stack.");
946 }
947 label = labelListIterator.next();
948 subObj = new SrEroSubObject(PcepNaiIpv4NodeId.ST_TYPE, false, false, false, true, (int) label.labelId(),
949 nai);
950 llSubObjects.add(subObj);
951 }
952 return llSubObjects;
953 }
954
955 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530956 * Creates PcInitiated lsp request list for setup tunnel.
957 *
958 * @param tunnel mpls tunnel
959 * @param path network path
960 * @param pc pcep client
961 * @param srpId unique id for pcep message
962 * @return list of PcInitiatedLspRequest
963 * @throws PcepParseException while building pcep objects fails
964 */
965 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
966 PcepClient pc, int srpId)
967 throws PcepParseException {
968 PcepValueType tlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530969 LinkedList<PcepValueType> llSubObjects = null;
970
971 NetworkResource labelStack = tunnel.resource();
972 if (labelStack != null && labelStack instanceof DefaultLabelStack) {
973 llSubObjects = createPcepLabelStack((DefaultLabelStack) labelStack, path);
974 } else {
975 llSubObjects = createPcepPath(path);
976 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530977
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700978 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530979 log.error("There is no link information to create tunnel");
980 return null;
981 }
982
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530983 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530984
985 // set PathSetupTypeTlv of SRP object
986 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)).type());
987 llOptionalTlv.add(tlv);
988
989 // build SRP object
990 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
991 .setOptionalTlv(llOptionalTlv).build();
992
993 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530994 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530995
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530996 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530997 short localLspId = 0;
998 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
999 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1000 if (localLspIdString != null) {
1001 localLspId = Short.valueOf(localLspIdString);
1002 }
1003 }
1004
Priyanka B4c3cef02016-06-14 20:27:53 +05301005 tunnel.annotations().value(LSP_SIG_TYPE);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301006 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
1007 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1008 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301009 llOptionalTlv.add(tlv);
1010 //set SymbolicPathNameTlv of LSP object
1011 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1012 llOptionalTlv.add(tlv);
1013
1014 //build LSP object
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301015 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setDFlag(true).setOFlag((byte) 0)
1016 .setPlspId(0).setOptionalTlv(llOptionalTlv).build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301017
1018 //build ENDPOINTS object
1019 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
1020 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
1021 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
1022 .setPFlag(true).build();
1023
1024 //build ERO object
1025 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
1026
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301027 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301028 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301029 iBandwidth = Float.valueOf(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301030 }
1031 // build bandwidth object
1032 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1033 // build pcep attribute
1034 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1035
1036 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1037 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
1038 .setPcepAttribute(pcepAttribute).build();
1039 llPcInitiatedLspRequestList.add(initiateLspRequest);
1040 return llPcInitiatedLspRequestList;
1041 }
1042
1043 /**
1044 * To send initiate tunnel message to pcc.
1045 *
1046 * @param tunnel mpls tunnel info
1047 * @param path explicit route for the tunnel
1048 * @param pc pcep client to send message
1049 */
1050 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1051 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301052 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1053 log.error("Setup tunnel has failed as LSP DB sync is not finished");
1054 return;
1055 }
1056
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301057 int srpId = SrpIdGenerators.create();
Priyanka B4c3cef02016-06-14 20:27:53 +05301058 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
1059 for (Tunnel t : tunnels) {
1060 if (t.tunnelName().equals(tunnel.tunnelName())) {
1061 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
1062 tunnel.dst(), tunnel.type(),
1063 t.state(), tunnel.groupId(),
1064 t.tunnelId(),
1065 tunnel.tunnelName(),
1066 tunnel.path(),
1067 tunnel.resource(),
1068 tunnel.annotations());
Priyanka B4c3b4512016-07-22 11:41:49 +05301069 break;
Priyanka B4c3cef02016-06-14 20:27:53 +05301070 }
1071 }
1072
1073 if (tunnel.tunnelId() == null) {
1074 log.error("Tunnel ID not found");
1075 return;
1076 }
1077
Avantika-Huawei56c11842016-04-28 00:56:56 +05301078 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301079
Jonathan Hart51539b82015-10-29 09:53:04 -07001080 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301081
1082 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
1083 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001084 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301085 log.error("Failed to create PcInitiatedLspRequestList");
1086 return;
1087 }
1088
1089 //build PCInitiate message
1090 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1091 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
1092 .build();
1093
1094 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1095
Jonathan Hart51539b82015-10-29 09:53:04 -07001096 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301097 } catch (PcepParseException e) {
1098 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
1099 }
1100 }
1101
1102 /**
1103 * To send Release tunnel message to pcc.
1104 *
1105 * @param tunnel mpls tunnel info
1106 * @param pc pcep client to send message
1107 */
1108 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
1109 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301110 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1111 log.error("Release tunnel has failed as LSP DB sync is not finished");
1112 return;
1113 }
1114
Avantika-Huawei56c11842016-04-28 00:56:56 +05301115 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001116 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301117 int srpId = SrpIdGenerators.create();
1118 TunnelId tunnelId = tunnel.tunnelId();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301119
1120 PcepValueType tlv;
1121 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301122
1123 // set PathSetupTypeTlv of SRP object
1124 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
1125 .type());
1126 llOptionalTlv.add(tlv);
1127
1128 // build SRP object
1129 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
1130 .setOptionalTlv(llOptionalTlv).build();
1131
1132 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301133 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
1134
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301135 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1136 llOptionalTlv.add(tlv);
Priyanka B4c3cef02016-06-14 20:27:53 +05301137
1138 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1139 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301140 String pLspIdString = tunnel.annotations().value(PLSP_ID);
1141
Priyanka B4c3cef02016-06-14 20:27:53 +05301142 short localLspId = 0;
1143 short pccTunnelId = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301144 int plspId = 0;
Priyanka B4c3cef02016-06-14 20:27:53 +05301145
1146 if (localLspIdString != null) {
1147 localLspId = Short.valueOf(localLspIdString);
1148 }
1149
1150 if (pccTunnelIdString != null) {
1151 pccTunnelId = Short.valueOf(pccTunnelIdString);
1152 }
1153
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301154 if (pLspIdString != null) {
1155 plspId = Integer.valueOf(pLspIdString);
1156 }
1157
Priyanka B4c3cef02016-06-14 20:27:53 +05301158 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1159 .ip().getIp4Address().toInt()),
1160 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1161 .getIp4Address().toInt()));
1162 llOptionalTlv.add(tlv);
1163
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301164 // build lsp object, set r flag as false to delete the tunnel
1165 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
1166 .setOptionalTlv(llOptionalTlv).build();
1167
1168 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1169 .setLspObject(lspobj).build();
1170
1171 llPcInitiatedLspRequestList.add(releaseLspRequest);
1172
1173 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1174 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1175
1176 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1177
Jonathan Hart51539b82015-10-29 09:53:04 -07001178 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301179 } catch (PcepParseException e) {
1180 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1181 }
1182 }
1183
1184 /**
1185 * To send Update tunnel request message to pcc.
1186 *
1187 * @param tunnel mpls tunnel info
1188 * @param path explicit route for the tunnel
1189 * @param pc pcep client to send message
1190 */
1191 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1192 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301193 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001194 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301195 int srpId = SrpIdGenerators.create();
1196 TunnelId tunnelId = tunnel.tunnelId();
1197 PcepValueType tlv;
1198 int plspId = 0;
1199
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301200 LinkedList<PcepValueType> llSubObjects = null;
1201 NetworkResource labelStack = tunnel.resource();
1202 if (labelStack != null && labelStack instanceof DefaultLabelStack) {
1203 llSubObjects = createPcepLabelStack((DefaultLabelStack) labelStack, path);
1204 } else {
1205 llSubObjects = createPcepPath(path);
1206 }
1207
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301208 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
1209 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
1210
Avantika-Huawei56c11842016-04-28 00:56:56 +05301211 // set PathSetupTypeTlv of SRP object
1212 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
1213 tlv = new PathSetupTypeTlv(lspSigType.type());
1214 llOptionalTlv.add(tlv);
1215
1216 // build SRP object
1217 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
1218 .setOptionalTlv(llOptionalTlv).build();
1219
1220 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301221
Avantika-Huawei56c11842016-04-28 00:56:56 +05301222 if (lspSigType != WITH_SIGNALLING) {
1223 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1224 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1225 short localLspId = 0;
1226 short pccTunnelId = 0;
1227
1228 if (localLspIdString != null) {
1229 localLspId = Short.valueOf(localLspIdString);
1230 }
1231
1232 if (pccTunnelIdString != null) {
1233 pccTunnelId = Short.valueOf(pccTunnelIdString);
1234 }
1235
1236 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1237 .ip().getIp4Address().toInt()),
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301238 localLspId, pccTunnelId,
1239 ((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt(),
1240 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
Avantika-Huawei56c11842016-04-28 00:56:56 +05301241 llOptionalTlv.add(tlv);
1242 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301243
1244 if (tunnel.tunnelName().value() != null) {
1245 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1246 llOptionalTlv.add(tlv);
1247 }
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301248 boolean delegated = (tunnel.annotations().value(DELEGATE) == null) ? false
1249 : Boolean.valueOf(tunnel.annotations()
1250 .value(DELEGATE));
1251 boolean initiated = (tunnel.annotations().value(PCE_INIT) == null) ? false
1252 : Boolean.valueOf(tunnel.annotations()
1253 .value(PCE_INIT));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301254 // build lsp object
Priyanka B4c3cef02016-06-14 20:27:53 +05301255 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true)
1256 .setPlspId(Integer.valueOf(tunnel.annotations().value(PLSP_ID)))
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301257 .setDFlag(delegated)
1258 .setCFlag(initiated)
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301259 .setOptionalTlv(llOptionalTlv).build();
1260 // build ero object
1261 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
Priyanka B4c3cef02016-06-14 20:27:53 +05301262 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301263 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301264 iBandwidth = Float.parseFloat(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301265 }
1266 // build bandwidth object
1267 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1268 // build pcep attribute
1269 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1270 // build pcep msg path
1271 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1272 .build();
1273
1274 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1275 .setLspObject(lspobj).setMsgPath(msgPath).build();
1276
1277 llUpdateRequestList.add(updateRequest);
1278
1279 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1280
1281 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001282 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301283 } catch (PcepParseException e) {
1284 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1285 }
1286 }
1287
chengfan2fff70f2015-08-24 18:20:19 -05001288
1289
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301290 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001291
1292 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001293 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001294 TunnelDescription tunnel = null;
1295 // instance and id identify a tunnel together
1296 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1297 + String.valueOf(pcepTunnel.id());
1298
1299 if (tunnelKey == null || "".equals(tunnelKey)) {
1300 log.error("Invalid PCEP tunnel");
1301 return;
1302 }
1303
1304 TunnelId tunnelId = getTunnelId(tunnelKey);
1305
1306 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1307
1308 OperationType operType = pcepTunnel.getOperationType();
1309 switch (operType) {
1310 case ADD:
1311 tunnelId = service.tunnelAdded(tunnel);
1312 tunnelMap.put(tunnelKey, tunnelId);
1313 break;
1314
1315 case UPDATE:
1316 service.tunnelUpdated(tunnel);
1317 break;
1318
1319 case DELETE:
1320 service.tunnelRemoved(tunnel);
1321 tunnelMap.remove(tunnelKey);
1322 break;
1323
1324 default:
1325 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301326 }
1327 }
cheng fan48e832c2015-05-29 01:54:47 +08001328
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301329 @Override
1330 public void handleMessage(PccId pccId, PcepMessage msg) {
1331 try {
1332 log.debug("tunnel provider handle message {}", msg.getType().toString());
1333 switch (msg.getType()) {
1334 case REPORT:
1335 int srpId = 0;
1336 LinkedList<PcepStateReport> llStateReportList = null;
1337 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1338 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1339 PcepSrpObject srpObj = null;
1340 PcepLspObject lspObj = null;
1341 while (listIterator.hasNext()) {
1342 PcepStateReport stateRpt = listIterator.next();
1343 srpObj = stateRpt.getSrpObject();
1344 lspObj = stateRpt.getLspObject();
1345
1346 if (srpObj instanceof PcepSrpObject) {
1347 srpId = srpObj.getSrpID();
1348 }
1349
1350 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1351 log.debug("SRP ID in handle message " + srpId);
1352
Jonathan Hart51539b82015-10-29 09:53:04 -07001353 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301354 // For PCRpt without matching SRP id.
1355 handleRptWithoutSrpId(stateRpt, pccId);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301356 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301357 }
1358
Avantika-Huawei56c11842016-04-28 00:56:56 +05301359 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301360 }
1361 break;
1362
1363 default:
1364 log.debug("Received unsupported message type {}", msg.getType().toString());
1365 }
1366 } catch (Exception e) {
1367 log.error("Exception occured while processing report message {}", e.getMessage());
1368 }
1369 }
1370
1371 /**
1372 * Handles report message for setup/update/delete tunnel request.
1373 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301374 * @param srpId unique identifier for PCEP message
1375 * @param lspObj LSP object
1376 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301377 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301378 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301379 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001380 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301381
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301382 // store the values required from report message
1383 pcepTunnelData.setPlspId(lspObj.getPlspId());
1384 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1385 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1386 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1387
Avantika-Huawei56c11842016-04-28 00:56:56 +05301388 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301389 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1390 while (listTlvIterator.hasNext()) {
1391 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301392 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1393 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301394 break;
1395 }
1396 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001397 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301398 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001399 }
1400
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301401 Path path = pcepTunnelData.path();
1402 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301403 Builder annotationBuilder = DefaultAnnotations.builder();
1404 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1405
1406 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1407 if (tunnel.annotations().value(PLSP_ID) == null) {
1408 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1409 }
1410
1411 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1412 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1413 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1414 }
1415
Priyanka B4c3cef02016-06-14 20:27:53 +05301416 if (tunnel.annotations().value(PCC_TUNNEL_ID) == null) {
1417 annotationBuilder.set(PCC_TUNNEL_ID, String.valueOf(ipv4LspTlv.getTunnelId()));
1418 }
1419
Avantika-Huawei56c11842016-04-28 00:56:56 +05301420 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301421 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1422 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1423 providerId, tunnel.tunnelName(), path,
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301424 tunnel.resource(), annotations);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301425
Avantika-Huawei56c11842016-04-28 00:56:56 +05301426 if (CREATE == pcepTunnelData.requestType()) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301427 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Jonathan Hart51539b82015-10-29 09:53:04 -07001428 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301429 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001430 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301431 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301432 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001433 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1434 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301435 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301436
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301437 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1438
Avantika-Huawei56c11842016-04-28 00:56:56 +05301439 if (lspObj.getRFlag()) {
1440 tunnelRemoved(td);
1441 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301442 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301443 tunnelUpdated(td, tunnelState);
1444 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301445
1446 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1447 // though there are no labels to download for SR-TE.
1448 if ((pcepLspStatus == PcepLspStatus.GOING_UP)
1449 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING)) {
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301450 // Query again to get latest tunnel updated with protocol values from PCRpt msg.
1451 updateTunnel(service.tunnelQueryById(tunnel.tunnelId()), tunnel.path());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301452 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301453 }
1454
Priyanka B413fbe82016-05-26 11:44:45 +05301455 private SparseAnnotations getAnnotations(PcepLspObject lspObj, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv,
Priyanka B4c3b4512016-07-22 11:41:49 +05301456 String bandwidth, LspType lspType, String costType, boolean isPceInit) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301457 Builder builder = DefaultAnnotations.builder();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301458 /*
1459 * [RFC 5440] The absence of the METRIC object MUST be interpreted by the PCE as a path computation request
1460 * for which no constraints need be applied to any of the metrics.
1461 */
1462 if (costType != null) {
1463 builder.set(COST_TYPE, costType);
1464 }
1465
Priyanka B4c3b4512016-07-22 11:41:49 +05301466 if (isPceInit) {
1467 builder.set(PCE_INIT, String.valueOf(isPceInit));
1468 }
1469
1470 if (bandwidth != null) {
1471 builder.set(BANDWIDTH, bandwidth);
1472 }
1473
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301474 SparseAnnotations annotations = builder
Priyanka B4c3b4512016-07-22 11:41:49 +05301475 .set(LSP_SIG_TYPE, lspType.name())
Priyanka B413fbe82016-05-26 11:44:45 +05301476 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1477 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1478 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId()))
1479 .set(DELEGATE, String.valueOf(lspObj.getDFlag()))
1480 .build();
1481 return annotations;
1482 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301483
Priyanka B413fbe82016-05-26 11:44:45 +05301484 private LspType getLspType(PcepSrpObject srpObj) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301485 LspType lspType = WITH_SIGNALLING;
1486
1487 if (null != srpObj) {
1488 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1489 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1490
1491 while (listIterator.hasNext()) {
1492 PcepValueType tlv = listIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301493 switch (tlv.getType()) {
1494 case PathSetupTypeTlv.TYPE:
1495 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1496 break;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301497 default:
1498 break;
1499 }
1500 }
1501 }
Priyanka B413fbe82016-05-26 11:44:45 +05301502 return lspType;
1503 }
1504
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301505 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId) {
Priyanka B413fbe82016-05-26 11:44:45 +05301506 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301507 String costType = null;
Priyanka B413fbe82016-05-26 11:44:45 +05301508 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1509 checkNotNull(msgPath);
1510 PcepEroObject eroObj = msgPath.getEroObject();
1511 if (eroObj == null) {
1512 log.error("ERO object is null in report message.");
1513 return;
1514 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301515 PcepAttribute attributes = msgPath.getPcepAttribute();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301516 float bandwidth = 0;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301517 int cost = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301518 if (attributes != null) {
1519 if (attributes.getMetricObjectList() != null) {
1520 ListIterator<PcepMetricObject> iterator = attributes.getMetricObjectList().listIterator();
1521 PcepMetricObject metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301522
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301523 while (metricObj != null) {
1524 if (metricObj.getBType() == IGP_METRIC) {
1525 costType = "COST";
1526 } else if (metricObj.getBType() == TE_METRIC) {
1527 costType = "TE_COST";
1528 }
1529 if (costType != null) {
1530 cost = metricObj.getMetricVal();
1531 log.debug("Path cost {}", cost);
1532 break;
1533 }
1534 metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301535 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301536 }
1537 if (attributes.getBandwidthObject() != null) {
1538 bandwidth = attributes.getBandwidthObject().getBandwidth();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301539 }
1540 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301541 PcepLspObject lspObj = stateRpt.getLspObject();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301542 List<Object> eroSubObjList = buildPathFromEroObj(eroObj, providerId);
1543 List<Link> links = new ArrayList<>();
1544 List<LabelResourceId> labels = new ArrayList<>();
1545 for (Object linkOrLabel : eroSubObjList) {
1546 if (linkOrLabel instanceof Link) {
1547 links.add((Link) linkOrLabel);
1548 } else if (linkOrLabel instanceof Integer) {
1549 labels.add(LabelResourceId.labelResourceId(((Integer) linkOrLabel).longValue()));
1550 }
Priyanka B413fbe82016-05-26 11:44:45 +05301551 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301552 Path path = null;
1553 if (!links.isEmpty()) {
1554 path = new DefaultPath(providerId, links, cost, EMPTY);
1555 } else if (!lspObj.getRFlag()) {
1556 return;
1557 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301558 NetworkResource labelStack = new DefaultLabelStack(labels);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301559 // To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1560 // from PCE.
Priyanka B413fbe82016-05-26 11:44:45 +05301561 PcepSrpObject srpObj = stateRpt.getSrpObject();
1562 LspType lspType = getLspType(srpObj);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301563 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1564 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1565 SymbolicPathNameTlv pathNameTlv = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301566 while (listTlvIterator.hasNext()) {
1567 PcepValueType tlv = listTlvIterator.next();
1568 switch (tlv.getType()) {
1569 case StatefulIPv4LspIdentifiersTlv.TYPE:
1570 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1571 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301572 case SymbolicPathNameTlv.TYPE:
1573 pathNameTlv = (SymbolicPathNameTlv) tlv;
1574 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301575 default:
1576 break;
1577 }
1578 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301579 /*
1580 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1581 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1582 */
1583 if (ipv4LspIdenTlv == null) {
1584 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1585 return;
1586 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301587 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1588 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1589 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1590 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1591 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
Priyanka B413fbe82016-05-26 11:44:45 +05301592 // Store delegation flag info and that LSP info because only delegated PCE sends update message
1593 // Storing if D flag is set, if not dont store. while checking whether delegation if annotation for D flag
1594 // not present then non-delegated , if present it is delegated.
1595 if (lspObj.getDFlag()) {
1596 pcepClientController.getClient(pccId).setLspAndDelegationInfo(
1597 new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()), lspObj.getDFlag());
1598 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301599 Tunnel tunnel = null;
Priyanka B4c3b4512016-07-22 11:41:49 +05301600 SparseAnnotations oldTunnelAnnotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301601 // Asynchronous status change message from PCC for LSP reported earlier.
1602 for (Tunnel tunnelObj : tunnelQueryResult) {
1603 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1604 /*
1605 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1606 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1607 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1608 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1609 * session an LSP is "first" reported to a PCE.
1610 */
1611 if ((pathNameTlv != null)
1612 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1613 tunnel = tunnelObj;
1614 break;
1615 }
1616 continue;
1617 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301618 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId())) {
1619 if ((Integer
1620 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1621 tunnel = tunnelObj;
1622 }
1623 if ((Integer
1624 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) != ipv4LspIdenTlv.getLspId())) {
1625 oldTunnelAnnotations = (SparseAnnotations) tunnelObj.annotations();
1626 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301627 }
1628 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301629 DefaultTunnelDescription td;
Priyanka B413fbe82016-05-26 11:44:45 +05301630 SparseAnnotations annotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301631 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
1632 if (tunnel == null) {
1633 if (lspObj.getRFlag()) {
1634 /*
1635 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1636 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1637 */
1638 return;
1639 }
Priyanka B413fbe82016-05-26 11:44:45 +05301640 DeviceId deviceId = getDevice(pccId);
1641 if (deviceId == null) {
1642 log.error("Ingress deviceId not found");
1643 return;
1644 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301645 String tempBandwidth = null;
1646 String temoCostType = null;
1647 if (oldTunnelAnnotations != null) {
1648 tempBandwidth = oldTunnelAnnotations.value(BANDWIDTH);
1649 temoCostType = oldTunnelAnnotations.value(COST_TYPE);
1650 }
1651 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, tempBandwidth, lspType,
1652 temoCostType, lspObj.getCFlag());
Priyanka B413fbe82016-05-26 11:44:45 +05301653 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301654 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path, labelStack,
Priyanka B413fbe82016-05-26 11:44:45 +05301655 annotations);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301656 // Do not support PCC initiated LSP after LSP DB sync is completed.
1657 if (!lspObj.getSFlag() && !lspObj.getCFlag()) {
1658 log.error("Received PCC initiated LSP while not in sync.");
1659 return;
1660 }
Priyanka B413fbe82016-05-26 11:44:45 +05301661 /*
1662 * If ONOS instance is master for PCC then set delegated flag as annotation and add the tunnel to store.
1663 * Because all LSPs need not be delegated, hence mastership for the PCC is confirmed whereas not the
1664 * delegation set to all LSPs.If ONOS is not the master for that PCC then check if D flag is set, if yes
1665 * wait for 2 seconds [while master has added the tunnel to the store] then update the tunnel. Tunnel is
1666 * updated because in case of resilency only delegated LSPs are recomputed and only delegated PCE can
1667 * send update message to that client.
1668 *
1669 * 1)Master can 1st get the Rpt message
1670 * a)Master adds the tunnel into core.
1671 * b)If a non-master for ingress gets Rpt message with D flag set[as delegation owner]
1672 * after master, then runs timer then update the tunnel with D flag set.
1673 * 2)Non-Master can 1st get the Rpt message
1674 * a)Non-Master runs the timer check for the tunnel then updates the tunnel with D flag set
1675 * b)Master would have got the message while the non-master running timer, hence master adds
1676 * tunnel to core
1677 *
1678 * In general always master adds the tunnel to the core
1679 * while delegated owner [master or non-master with D flag set] always updates the tunnel running timer
1680 */
1681 if (mastershipService.isLocalMaster(deviceId)) {
1682 TunnelId tId = tunnelAdded(td, tunnelState);
1683 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1684 tunnelState, new DefaultGroupId(0), tId, TunnelName.tunnelName(String.valueOf(pathNameTlv
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301685 .getValue())), path, labelStack, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301686
Priyanka B413fbe82016-05-26 11:44:45 +05301687 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1688 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1689 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1690 } else if (!mastershipService.isLocalMaster(deviceId) && lspObj.getDFlag()) {
1691 //Start timer then update the tunnel with D flag
Priyanka B4c3b4512016-07-22 11:41:49 +05301692 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId, tunnelState, ipv4LspIdenTlv);
Priyanka B413fbe82016-05-26 11:44:45 +05301693 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301694 return;
1695 }
Priyanka B413fbe82016-05-26 11:44:45 +05301696 //delegated owner will update can be a master or non-master
Priyanka B4c3b4512016-07-22 11:41:49 +05301697 if (lspObj.getDFlag() && !lspObj.getRFlag()) {
1698 tunnelUpdateForDelegatedLsp(tunnel, lspObj,
1699 lspType, tunnelState, pccId, labelStack, ipv4LspIdenTlv);
1700 return;
Priyanka B413fbe82016-05-26 11:44:45 +05301701 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301702 removeOrUpdatetunnel(tunnel, lspObj, providerId, tunnelState, ipv4LspIdenTlv);
Priyanka B413fbe82016-05-26 11:44:45 +05301703 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301704
Priyanka B4c3b4512016-07-22 11:41:49 +05301705 private void tunnelUpdateForDelegatedLsp(Tunnel tunnel, PcepLspObject lspObj,
1706 LspType lspType, State tunnelState, PccId pccId,
1707 NetworkResource labelStack,
1708 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv) {
1709 SparseAnnotations annotations = null;
1710 DefaultTunnelDescription td;
1711 boolean isPceInit = tunnel.annotations().value(PCE_INIT) == null ? false :
1712 Boolean.valueOf((tunnel.annotations().value(PCE_INIT))).booleanValue();
1713 annotations = getAnnotations(lspObj, ipv4LspIdenTlv,
1714 tunnel.annotations().value(BANDWIDTH), lspType,
1715 tunnel.annotations().value(COST_TYPE), isPceInit);
1716 td = new DefaultTunnelDescription(null, tunnel.src(), tunnel.dst(), MPLS, new DefaultGroupId(
1717 0), tunnel.providerId(), tunnel.tunnelName(),
1718 tunnel.path(), labelStack, annotations);
1719 tunnelUpdateInDelegatedCase(pccId, annotations, td, tunnel.providerId(), tunnelState, ipv4LspIdenTlv);
1720 }
1721
1722 private void removeOrUpdatetunnel(Tunnel tunnel, PcepLspObject lspObj, ProviderId providerId,
1723 State tunnelState, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv) {
Priyanka B413fbe82016-05-26 11:44:45 +05301724 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1725 tunnel.type(), tunnel.groupId(), providerId, tunnel.tunnelName(), tunnel.path(),
1726 (SparseAnnotations) tunnel.annotations());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301727 if (lspObj.getRFlag()) {
1728 tunnelRemoved(td);
1729 } else {
Priyanka B4c3b4512016-07-22 11:41:49 +05301730 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, tunnel.path(), LSP_STATE_RPT);
1731 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1732 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301733 tunnelUpdated(td, tunnelState);
1734 }
Priyanka B413fbe82016-05-26 11:44:45 +05301735 }
1736
1737 private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
Priyanka B4c3b4512016-07-22 11:41:49 +05301738 DefaultTunnelDescription td, ProviderId providerId, State tunnelState,
1739 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv) {
Priyanka B413fbe82016-05-26 11:44:45 +05301740 //Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
1741
1742 /*
1743 * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
1744 * master has added the tunnel to the store] then update the tunnel.
1745 */
1746 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1747
1748 // Thread is started after 2 seconds first time later periodically after 2 seconds to update the tunnel
1749 executor.scheduleAtFixedRate(new UpdateDelegation(td, providerId, annotations, pccId,
Priyanka B4c3b4512016-07-22 11:41:49 +05301750 executor, tunnelState, ipv4LspIdentifiersTlv), DELAY, DELAY, TimeUnit.SECONDS);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301751 }
1752
1753 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301754 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301755 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301756 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301757 * @param providerId provider id
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301758 * @return list of links and labels
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301759 */
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301760 private List<Object> buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301761 checkNotNull(eroObj);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301762 List<Object> subObjList = new ArrayList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301763 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301764 if (0 == llSubObj.size()) {
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301765 log.error("ERO in report message does not have hop information");
Priyanka B4c3b4512016-07-22 11:41:49 +05301766 return new ArrayList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301767 }
1768 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1769
1770 ConnectPoint src = null;
1771 ConnectPoint dst = null;
1772 boolean isSrcSet = false;
1773 while (tlvIterator.hasNext()) {
1774 PcepValueType subObj = tlvIterator.next();
1775 switch (subObj.getType()) {
1776
1777 case IPv4SubObject.TYPE:
1778
1779 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1780 if (!isSrcSet) {
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301781 Iterable<Link> links = linkService.getActiveLinks();
1782 for (Link l : links) {
1783 if (l.src().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1784 src = l.src();
1785 isSrcSet = true;
1786 break;
1787 } else if (l.dst().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1788 src = l.dst();
1789 isSrcSet = true;
1790 break;
1791 }
1792 }
1793 } else {
1794 Iterable<Link> links = linkService.getActiveLinks();
1795 for (Link l : links) {
1796 if (l.src().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1797 dst = l.src();
1798 break;
1799 } else if (l.dst().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1800 dst = l.dst();
1801 break;
1802 }
1803 }
Ray Milkey2693bda2016-01-22 16:08:14 -08001804 Link link = DefaultLink.builder()
1805 .providerId(providerId)
1806 .src(src)
1807 .dst(dst)
1808 .type(Link.Type.DIRECT)
1809 .build();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301810 subObjList.add(link);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301811 src = dst;
1812 }
1813 break;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301814 case SrEroSubObject.TYPE:
1815 SrEroSubObject srEroSubObj = (SrEroSubObject) subObj;
1816 subObjList.add(srEroSubObj.getSid());
1817
1818 if (srEroSubObj.getSt() == PcepNaiIpv4Adjacency.ST_TYPE) {
1819 PcepNaiIpv4Adjacency nai = (PcepNaiIpv4Adjacency) (srEroSubObj.getNai());
Priyanka B4c3b4512016-07-22 11:41:49 +05301820 int srcIp = nai.getLocalIpv4Addr();
1821 int dstIp = nai.getRemoteIpv4Addr();
1822 Iterable<Link> links = linkService.getActiveLinks();
1823 for (Link l : links) {
1824 long lSrc = l.src().port().toLong();
1825 long lDst = l.dst().port().toLong();
1826 if (lSrc == srcIp) {
1827 src = l.src();
1828 } else if (lDst == srcIp) {
1829 src = l.dst();
1830 }
1831 if (lSrc == dstIp) {
1832 dst = l.src();
1833 } else if (lDst == dstIp) {
1834 dst = l.dst();
1835 }
1836 if (src != null && dst != null) {
1837 break;
1838 }
1839 }
1840 if (src == null || dst == null) {
1841 return new ArrayList<>();
1842 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301843 Link link = DefaultLink.builder()
1844 .providerId(providerId)
1845 .src(src)
1846 .dst(dst)
1847 .type(Link.Type.DIRECT)
1848 .build();
1849 subObjList.add(link);
1850 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301851 default:
1852 // the other sub objects are not required
1853 }
1854 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301855 return subObjList;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301856 }
1857
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301858 @Override
1859 public void clientConnected(PccId pccId) {
1860 // TODO
1861 }
1862
1863 @Override
1864 public void clientDisconnected(PccId pccId) {
1865 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001866 }
chengfan2fff70f2015-08-24 18:20:19 -05001867
chengfan2fff70f2015-08-24 18:20:19 -05001868 @Override
1869 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1870 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1871 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1872 tunnelStatisticsMap.put(id, tunnelStatistics);
1873 }
cheng fan48e832c2015-05-29 01:54:47 +08001874
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301875 @Override
1876 public void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301877
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301878 if (endOfSyncAction == SEND_UPDATE) {
1879 updateTunnel(tunnel, tunnel.path());
1880 return;
1881 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301882
1883 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1884 tunnel.src(), tunnel.dst(),
1885 tunnel.type(),
1886 tunnel.groupId(),
1887 tunnel.providerId(),
1888 tunnel.tunnelName(),
1889 tunnel.path(),
1890 (SparseAnnotations) tunnel.annotations());
1891
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301892
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301893 if (endOfSyncAction == PcepLspSyncAction.UNSTABLE) {
1894
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301895 // Send PCInit msg again after global reoptimization.
1896 tunnelUpdated(td, UNSTABLE);
1897
1898 // To remove the old tunnel from store whose PLSPID is not
1899 // recognized by ingress PCC.
1900 tunnelRemoved(td);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301901
1902 } else if (endOfSyncAction == REMOVE) {
1903 tunnelRemoved(td);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301904 }
1905 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301906 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301907 @Override
1908 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1909 return service.tunnelQueryById(tunnelId);
1910 }
1911
Priyanka B413fbe82016-05-26 11:44:45 +05301912 private DeviceId getDevice(PccId pccId) {
1913 // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID.
1914 IpAddress lsrId = pccId.ipAddress();
1915 String lsrIdentifier = String.valueOf(lsrId);
1916
1917 // Find PCC deviceID from lsrId stored as annotations
1918 Iterable<Device> devices = deviceService.getAvailableDevices();
1919 for (Device dev : devices) {
1920 if (dev.annotations().value(AnnotationKeys.TYPE).equals("L3")
1921 && dev.annotations().value(LSRID).equals(lsrIdentifier)) {
1922 return dev.id();
1923 }
1924 }
1925 return null;
1926 }
1927
1928 /**
1929 * Updates the tunnel with updated tunnel annotation after a delay of two seconds and checks it till
1930 * tunnel is found.
1931 */
1932 private class UpdateDelegation implements Runnable {
1933 DefaultTunnelDescription td;
1934 ProviderId providerId;
1935 SparseAnnotations annotations;
1936 PccId pccId;
1937 ScheduledExecutorService executor;
Priyanka B4c3b4512016-07-22 11:41:49 +05301938 State tunnelState;
1939 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv;
Priyanka B413fbe82016-05-26 11:44:45 +05301940
1941 /**
1942 * Creates an instance of UpdateDelegation.
1943 *
1944 * @param td tunnel description
1945 * @param providerId provider id
1946 * @param annotations tunnel annotations
1947 * @param pccId PCEP client id
1948 * @param executor service of delegated owner
1949 */
1950 public UpdateDelegation(DefaultTunnelDescription td, ProviderId providerId, SparseAnnotations annotations,
Priyanka B4c3b4512016-07-22 11:41:49 +05301951 PccId pccId, ScheduledExecutorService executor, State tunnelState,
1952 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv) {
Priyanka B413fbe82016-05-26 11:44:45 +05301953 this.td = td;
1954 this.providerId = providerId;
1955 this.annotations = annotations;
1956 this.pccId = pccId;
1957 this.executor = executor;
Priyanka B4c3b4512016-07-22 11:41:49 +05301958 this.tunnelState = tunnelState;
1959 this.ipv4LspIdentifiersTlv = ipv4LspIdentifiersTlv;
Priyanka B413fbe82016-05-26 11:44:45 +05301960 }
1961
1962 //Temporary using annotations later will use projection/network config service
1963 @Override
1964 public void run() {
1965 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(td.src(), td.dst());
1966 TunnelId tempTunnelId = null;
1967 for (Tunnel t : tunnelQueryResult) {
1968 if (t.annotations().value(LOCAL_LSP_ID) == null || t.annotations().value(PLSP_ID) == null) {
1969 continue;
1970 }
1971
1972 if (t.annotations().value(LOCAL_LSP_ID).equals(td.annotations().value(LOCAL_LSP_ID))
1973 && t.annotations().value(PLSP_ID).equals(td.annotations().value(PLSP_ID))
1974 && ((IpTunnelEndPoint) t.src()).ip().equals(pccId.id())) {
1975 tempTunnelId = t.tunnelId();
1976 break;
1977 }
1978 }
1979
1980 //If tunnel is found update the tunnel and shutdown the thread otherwise thread will be executing
1981 //periodically
1982 if (tempTunnelId != null) {
1983 Tunnel tunnel = new DefaultTunnel(providerId, td.src(), td.dst(), MPLS, new DefaultGroupId(0),
1984 tempTunnelId, td.tunnelName(), td.path(), annotations);
Priyanka B4c3b4512016-07-22 11:41:49 +05301985 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, tunnel.path(), LSP_STATE_RPT);
1986 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdentifiersTlv);
1987 pcepTunnelData.setLspDFlag(Boolean.valueOf(tunnel.annotations().value(DELEGATE)));
1988 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1989 tunnelUpdated(tunnel, td.path(), tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +05301990 executor.shutdown();
1991 try {
1992 executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
1993 } catch (InterruptedException e) {
1994 log.error("updating delegation failed");
1995 }
1996 }
1997 }
1998 }
cheng fan48e832c2015-05-29 01:54:47 +08001999}