blob: a2d21bc900ab970c76a5332198bb964e38a79170 [file] [log] [blame]
cheng fan48e832c2015-05-29 01:54:47 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Yi Tsengfa394de2017-02-01 11:26:40 -080030import org.onosproject.core.GroupId;
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;
harikrushna-Huawei6ecfc772017-04-10 18:22:00 +053081import org.onosproject.pcep.server.LspKey;
82import org.onosproject.pcep.server.LspType;
83import org.onosproject.pcep.server.PccId;
84import org.onosproject.pcep.server.PcepClient;
85import org.onosproject.pcep.server.PcepClientController;
86import org.onosproject.pcep.server.PcepClientListener;
87import org.onosproject.pcep.server.PcepEventListener;
88import org.onosproject.pcep.server.PcepLspStatus;
89import org.onosproject.pcep.server.PcepLspSyncAction;
90import org.onosproject.pcep.server.SrpIdGenerators;
91import org.onosproject.pcep.server.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;
103import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530104import org.onosproject.pcepio.protocol.PcepSrpObject;
105import org.onosproject.pcepio.protocol.PcepStateReport;
106import org.onosproject.pcepio.protocol.PcepUpdateMsg;
107import org.onosproject.pcepio.protocol.PcepUpdateRequest;
108import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530109import org.onosproject.pcepio.types.PathSetupTypeTlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530110import org.onosproject.pcepio.types.PcepNaiIpv4Adjacency;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530111import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530112import org.onosproject.pcepio.types.SrEroSubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530113import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530114import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -0500115import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -0700116import org.osgi.service.component.annotations.Modified;
117import org.slf4j.Logger;
118
119import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530120import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530121import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700122import java.util.Collections;
123import java.util.Dictionary;
124import java.util.HashMap;
125import java.util.LinkedList;
126import java.util.List;
127import java.util.ListIterator;
Sho SHIMIZUc218bfa2016-08-18 14:22:50 -0700128import java.util.Objects;
Jonathan Hart51539b82015-10-29 09:53:04 -0700129import java.util.Optional;
Priyanka B413fbe82016-05-26 11:44:45 +0530130import java.util.concurrent.Executors;
131import java.util.concurrent.ScheduledExecutorService;
132import java.util.concurrent.TimeUnit;
Jonathan Hart51539b82015-10-29 09:53:04 -0700133
134import static com.google.common.base.Preconditions.checkNotNull;
135import static com.google.common.base.Strings.isNullOrEmpty;
136import static org.onlab.util.Tools.get;
Priyanka B4c3cef02016-06-14 20:27:53 +0530137import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530138import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700139import static org.onosproject.net.DefaultAnnotations.EMPTY;
140import static org.onosproject.net.DeviceId.deviceId;
141import static org.onosproject.net.PortNumber.portNumber;
142import static org.onosproject.pcep.api.PcepDpid.uri;
harikrushna-Huawei6ecfc772017-04-10 18:22:00 +0530143import static org.onosproject.pcep.server.LspType.WITH_SIGNALLING;
144import static org.onosproject.pcep.server.LspType.SR_WITHOUT_SIGNALLING;
145import static org.onosproject.pcep.server.LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
146import static org.onosproject.pcep.server.PcepAnnotationKeys.BANDWIDTH;
147import static org.onosproject.pcep.server.PcepAnnotationKeys.LOCAL_LSP_ID;
148import static org.onosproject.pcep.server.PcepAnnotationKeys.LSP_SIG_TYPE;
149import static org.onosproject.pcep.server.PcepAnnotationKeys.PCC_TUNNEL_ID;
150import static org.onosproject.pcep.server.PcepAnnotationKeys.PCE_INIT;
151import static org.onosproject.pcep.server.PcepAnnotationKeys.PLSP_ID;
152import static org.onosproject.pcep.server.PcepAnnotationKeys.DELEGATE;
153import static org.onosproject.pcep.server.PcepAnnotationKeys.COST_TYPE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530154import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
155import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
156import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
157import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530158import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
harikrushna-Huawei6ecfc772017-04-10 18:22:00 +0530159import static org.onosproject.pcep.server.PcepLspSyncAction.REMOVE;
160import static org.onosproject.pcep.server.PcepLspSyncAction.SEND_UPDATE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530161import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.IGP_METRIC;
162import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.TE_METRIC;
Jonathan Hart51539b82015-10-29 09:53:04 -0700163import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800164
165/**
166 * Provider which uses an PCEP controller to detect, update, create network
167 * tunnels.
168 */
169@Component(immediate = true)
170@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530171public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800172
173 private static final Logger log = getLogger(PcepTunnelProvider.class);
174 private static final long MAX_BANDWIDTH = 99999744;
175 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800176 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530177 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
Priyanka B413fbe82016-05-26 11:44:45 +0530178 public static final long IDENTIFIER_SET = 0x100000000L;
179 public static final long SET = 0xFFFFFFFFL;
180 private static final int DELAY = 2;
181 private static final int WAIT_TIME = 5;
182 public static final String LSRID = "lsrId";
cheng fan48e832c2015-05-29 01:54:47 +0800183
chengfan2fff70f2015-08-24 18:20:19 -0500184 static final int POLL_INTERVAL = 10;
185 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
186 label = "Frequency (in seconds) for polling tunnel statistics")
187 private int tunnelStatsPollFrequency = POLL_INTERVAL;
188
cheng fan48e832c2015-05-29 01:54:47 +0800189 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
190
191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
192 protected TunnelProviderRegistry tunnelProviderRegistry;
193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
195 protected PcepController controller;
196
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
198 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500199
200 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
201 protected TunnelService tunnelService;
202
203 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
204 protected ComponentConfigService cfgService;
205
Priyanka B413fbe82016-05-26 11:44:45 +0530206 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
207 protected TunnelAdminService tunnelAdminService;
208
209 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
210 protected MastershipService mastershipService;
211
212 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
213 protected DeviceService deviceService;
214
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530215 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
216 protected LinkService linkService;
217
cheng fan48e832c2015-05-29 01:54:47 +0800218 TunnelProviderService service;
219
220 HashMap<String, TunnelId> tunnelMap = new HashMap<String, TunnelId>();
chengfan2fff70f2015-08-24 18:20:19 -0500221 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400222 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800223
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530224 private InnerTunnelProvider listener = new InnerTunnelProvider();
225
Jonathan Hart51539b82015-10-29 09:53:04 -0700226 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530227 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800228
229 /**
230 * Creates a Tunnel provider.
231 */
232 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530233 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800234 }
235
236 @Activate
237 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500238 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800239 service = tunnelProviderRegistry.register(this);
240 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530241 pcepClientController.addListener(listener);
242 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500243 tunnelService.queryAllTunnels().forEach(tunnel -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700244 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500245 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
246 tsc.start();
247 collectors.put(tunnel.tunnelId().id(), tsc);
248
249 });
250
cheng fan48e832c2015-05-29 01:54:47 +0800251 log.info("Started");
252 }
253
254 @Deactivate
255 public void deactivate() {
256 tunnelProviderRegistry.unregister(this);
257 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500258 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530259 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800260 log.info("Stopped");
261 }
262
chengfan2fff70f2015-08-24 18:20:19 -0500263 @Modified
264 public void modified(ComponentContext context) {
265 Dictionary<?, ?> properties = context.getProperties();
266 int newTunnelStatsPollFrequency;
267 try {
268 String s = get(properties, "tunnelStatsPollFrequency");
269 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
270
271 } catch (NumberFormatException | ClassCastException e) {
272 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
273 }
274
275 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
276 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
277 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
278 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
279 }
chengfan2fff70f2015-08-24 18:20:19 -0500280 }
281
cheng fan48e832c2015-05-29 01:54:47 +0800282 @Override
283 public void setupTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530284 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530285 log.error("Tunnel Type MPLS is only supported");
286 return;
287 }
cheng fan48e832c2015-05-29 01:54:47 +0800288
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530289 // check for tunnel end points
290 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
291 log.error("Tunnel source or destination is not valid");
292 return;
293 }
294
295 // Get the pcc client
296 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
297
298 if (!(pc instanceof PcepClient)) {
299 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500300 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530301 return;
302 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530303
304 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
Priyanka B413fbe82016-05-26 11:44:45 +0530305 //Only master will initiate setup tunnel
306 if (pc.capability().pcInstantiationCapability() && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530307 pcepSetupTunnel(tunnel, path, pc);
308 }
cheng fan48e832c2015-05-29 01:54:47 +0800309 }
310
311 @Override
312 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800313
Priyanka B4c3cef02016-06-14 20:27:53 +0530314 //TODO: tunnel which is passed doesn't have tunnelID
Avantika-Huawei56c11842016-04-28 00:56:56 +0530315 if (tunnel.annotations().value(PLSP_ID) != null) {
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530316 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITHOUT_SIGNALLING_AND_WITHOUT_SR) {
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530317 updateTunnel(tunnel, path);
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530318 } else {
319 // Download labels and send update message.
320 // To get new tunnel ID (modified tunnel ID)
321 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
322 for (Tunnel t : tunnels) {
323 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
324 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
325 tunnel.dst(), tunnel.type(),
326 t.state(), tunnel.groupId(),
327 t.tunnelId(),
328 tunnel.tunnelName(),
329 tunnel.path(),
330 tunnel.resource(),
331 tunnel.annotations());
332 break;
333 }
334 }
335 if (!pcepClientController.allocateLocalLabel(tunnel)) {
336 log.error("Unable to allocate labels for the tunnel {}.", tunnel.toString());
337 }
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530338 }
Avantika-Huawei56c11842016-04-28 00:56:56 +0530339 return;
340 }
341
342 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530343 log.error("Tunnel Type MPLS is only supported");
344 return;
345 }
346
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530347 // check for tunnel end points
348 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
349 log.error("Tunnel source or destination is not valid");
350 return;
351 }
352
Priyanka Bcdf9b102016-06-07 20:01:38 +0530353 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530354
355 if (!(pc instanceof PcepClient)) {
Priyanka B4c3cef02016-06-14 20:27:53 +0530356 log.error("There is no PCC connected with this device {}"
357 + srcElement.toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530358 return;
359 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530360
Priyanka B413fbe82016-05-26 11:44:45 +0530361 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
362 //Only master will initiate setup tunnel
363 if (pc.capability().pcInstantiationCapability()
364 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530365 pcepSetupTunnel(tunnel, path, pc);
366 }
cheng fan48e832c2015-05-29 01:54:47 +0800367 }
368
369 @Override
370 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800371
Avantika-Huawei56c11842016-04-28 00:56:56 +0530372 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530373 log.error("Tunnel Type MPLS is only supported");
374 return;
375 }
376
377 // check for tunnel end points
378 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
379 log.error("Tunnel source or destination is not valid");
380 return;
381 }
382
383 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
384
385 if (!(pc instanceof PcepClient)) {
386 log.error("There is no PCC connected with ip addresss {}"
387 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
388 return;
389 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530390
Priyanka B413fbe82016-05-26 11:44:45 +0530391 //Only master will release tunnel
392 if (pc.capability().pcInstantiationCapability()
393 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530394 pcepReleaseTunnel(tunnel, pc);
395 }
cheng fan48e832c2015-05-29 01:54:47 +0800396 }
397
398 @Override
399 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530400 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530401 log.error("Tunnel Type MPLS is only supported");
402 return;
403 }
cheng fan48e832c2015-05-29 01:54:47 +0800404
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530405 if (!(srcElement instanceof IpElementId)) {
406 log.error("Element id is not valid");
407 return;
408 }
409
410 // check for tunnel end points
411 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
412 log.error("Tunnel source or destination is not valid");
413 return;
414 }
415
416 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
417
418 if (!(pc instanceof PcepClient)) {
419 log.error("There is no PCC connected with ip addresss {}"
420 + ((IpElementId) srcElement).ipAddress().toString());
421 return;
422 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530423
Priyanka B413fbe82016-05-26 11:44:45 +0530424 //Only master will release tunnel
425 if (pc.capability().pcInstantiationCapability()
426 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530427 pcepReleaseTunnel(tunnel, pc);
428 }
cheng fan48e832c2015-05-29 01:54:47 +0800429 }
430
431 @Override
432 public void updateTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530433 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530434 log.error("Tunnel Type MPLS is only supported");
435 return;
436 }
cheng fan48e832c2015-05-29 01:54:47 +0800437
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530438 // check for tunnel end points
439 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
440 log.error("Tunnel source or destination is not valid");
441 return;
442 }
443
Priyanka B4c3cef02016-06-14 20:27:53 +0530444 //To get new tunnel ID (modified tunnel ID)
445 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
446 for (Tunnel t : tunnels) {
447 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
448 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
449 tunnel.dst(), tunnel.type(),
450 t.state(), tunnel.groupId(),
451 t.tunnelId(),
452 tunnel.tunnelName(),
453 tunnel.path(),
454 tunnel.resource(),
455 tunnel.annotations());
456 break;
457 }
458 }
459
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530460 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
461
462 if (!(pc instanceof PcepClient)) {
463 log.error("There is no PCC connected with ip addresss {}"
464 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
465 return;
466 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530467
Priyanka B4c3cef02016-06-14 20:27:53 +0530468 //PCInitiate tunnels are always have D flag set, else check for tunnels who are delegated via LspKey
469 if (pc.capability().statefulPceCapability()) {
470 if (tunnel.annotations().value(PCE_INIT) != null && tunnel.annotations().value(PCE_INIT).equals("true")) {
471 pcepUpdateTunnel(tunnel, path, pc);
Priyanka B4c3b4512016-07-22 11:41:49 +0530472 } else {
473 // If delegation flag is set then only send update message[means delegated PCE can send update msg for
474 // that LSP. If annotation is null D flag is not set else it is set.
475 Short localLspId = 0;
476 for (Tunnel t : tunnels) {
477 if (!t.tunnelId().equals(tunnel.tunnelId()) && t.tunnelName().equals(tunnel.tunnelName())) {
478 localLspId = Short.valueOf(t.annotations().value(LOCAL_LSP_ID));
479 }
480 }
481
482 if (localLspId == 0) {
483 log.error("Local LSP ID for old tunnel not found");
484 return;
485 }
486
487 if (pc.delegationInfo(new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)),
488 localLspId.shortValue())) != null) {
489
490 pcepUpdateTunnel(tunnel, path, pc);
491 }
Priyanka B4c3cef02016-06-14 20:27:53 +0530492 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530493 }
cheng fan48e832c2015-05-29 01:54:47 +0800494 }
495
496 @Override
497 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800498
Avantika-Huawei56c11842016-04-28 00:56:56 +0530499 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530500 log.error("Tunnel Type MPLS is only supported");
501 return;
502 }
503
504 if (!(srcElement instanceof IpElementId)) {
505 log.error("Element id is not valid");
506 return;
507 }
508
509 // check for tunnel end points
510 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
511 log.error("Tunnel source or destination is not valid");
512 return;
513 }
514
515 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
516
517 if (!(pc instanceof PcepClient)) {
518 log.error("There is no PCC connected with ip addresss {}"
519 + ((IpElementId) srcElement).ipAddress().toString());
520 return;
521 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530522
Priyanka B413fbe82016-05-26 11:44:45 +0530523 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
524 // LSP].If annotation is null D flag is not set else it is set.
525 if (pc.capability().statefulPceCapability()
526 && pc.delegationInfo(
527 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
528 .annotations().value(LOCAL_LSP_ID)))) != null) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530529 pcepUpdateTunnel(tunnel, path, pc);
530 }
cheng fan48e832c2015-05-29 01:54:47 +0800531 }
532
533 @Override
534 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530535 return handleTunnelAdded(tunnel, null);
536 }
537
538 public TunnelId tunnelAdded(TunnelDescription tunnel, State tunnelState) {
539 return handleTunnelAdded(tunnel, tunnelState);
540 }
541
542 private TunnelId handleTunnelAdded(TunnelDescription tunnel, State tunnelState) {
543
544 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700545 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530546
547 if (tunnelState == null) {
548 return service.tunnelAdded(tunnel);
549 } else {
550 return service.tunnelAdded(tunnel, tunnelState);
551 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530552 }
cheng fan48e832c2015-05-29 01:54:47 +0800553
Avantika-Huawei56c11842016-04-28 00:56:56 +0530554 long bandwidth = Long.parseLong(tunnel.annotations().value(BANDWIDTH));
cheng fan48e832c2015-05-29 01:54:47 +0800555
556 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800557 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800558 return null;
559 }
560
561 // endpoints
562 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
563 .src();
564 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
565 // devices
566 DeviceId srcId = (DeviceId) src.elementId().get();
567 DeviceId dstId = (DeviceId) dst.elementId().get();
568
569 // ports
570 long srcPort = src.portNumber().get().toLong();
571 long dstPort = dst.portNumber().get().toLong();
572
573 // type
574 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800575 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800576 return null;
577 }
578
579 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
580 dstPort, bandwidth,
581 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530582 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800583
584 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
585 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
586 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
587
588 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
589 return tunnelId;
590 }
591
Priyanka B4c3b4512016-07-22 11:41:49 +0530592 private void tunnelUpdated(Tunnel tunnel, Path path, State tunnelState) {
593 handleTunnelUpdate(tunnel, path, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +0530594 }
595
596 //Handles tunnel updated using tunnel admin service[specially to update annotations].
Priyanka B4c3b4512016-07-22 11:41:49 +0530597 private void handleTunnelUpdate(Tunnel tunnel, Path path, State tunnelState) {
Priyanka B413fbe82016-05-26 11:44:45 +0530598
599 if (tunnel.type() == MPLS) {
600 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.tunnelId());
601
Priyanka B4c3b4512016-07-22 11:41:49 +0530602 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
603 tunnel.type(), tunnel.groupId(), tunnel.providerId(),
604 tunnel.tunnelName(), path, tunnel.resource(),
605 (SparseAnnotations) tunnel.annotations());
Priyanka B413fbe82016-05-26 11:44:45 +0530606
Priyanka B4c3b4512016-07-22 11:41:49 +0530607 service.tunnelUpdated(td, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +0530608 return;
609 }
610
611 Tunnel tunnelOld = tunnelQueryById(tunnel.tunnelId());
612 if (tunnelOld.type() != Tunnel.Type.VLAN) {
613 error("Illegal tunnel type. Only support VLAN tunnel update.");
614 return;
615 }
616
617 long bandwidth = Long
618 .parseLong(tunnel.annotations().value("bandwidth"));
619 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
620 error("Update failed, invalid bandwidth.");
621 return;
622 }
623 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
624
625 checkNotNull(pcepTunnelId, "Invalid tunnel id");
626 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
627 error("Update failed,maybe invalid bandwidth.");
628 return;
629 }
630 tunnelAdminService.updateTunnel(tunnel, path);
631 }
632
cheng fan48e832c2015-05-29 01:54:47 +0800633 @Override
634 public void tunnelRemoved(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530635 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700636 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530637 service.tunnelRemoved(tunnel);
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530638 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530639 }
640
cheng fan48e832c2015-05-29 01:54:47 +0800641 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
642 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
643 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800644 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800645 return;
646 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700647 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800648 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800649 if (!controller.deleteTunnel(pcepTunnelId)) {
650 error("Delete tunnel failed, Maybe some devices have been disconnected.");
651 return;
cheng fan48e832c2015-05-29 01:54:47 +0800652 }
653 tunnelMap.remove(pcepTunnelId);
654 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800655 }
656
657 @Override
658 public void tunnelUpdated(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530659 handleTunnelUpdate(tunnel, null);
660 }
661
662 public void tunnelUpdated(TunnelDescription tunnel, State tunnelState) {
663 handleTunnelUpdate(tunnel, tunnelState);
664 }
665
666 private void handleTunnelUpdate(TunnelDescription tunnel, State tunnelState) {
667 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700668 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530669
670 if (tunnelState == null) {
671 service.tunnelUpdated(tunnel);
672 } else {
673 service.tunnelUpdated(tunnel, tunnelState);
674 }
675 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530676 }
cheng fan48e832c2015-05-29 01:54:47 +0800677
678 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
679 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800680 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800681 return;
682 }
cheng fan7716ec92015-05-31 01:53:19 +0800683 long bandwidth = Long
684 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800685 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800686 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800687 return;
688 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700689 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800690
691 checkNotNull(pcepTunnelId, "Invalid tunnel id");
692 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
693
cheng fan7716ec92015-05-31 01:53:19 +0800694 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800695 return;
696
697 }
698 service.tunnelUpdated(tunnel);
699 }
700
cheng fan7716ec92015-05-31 01:53:19 +0800701 private void error(String info) {
702 System.err.println(info);
703 }
704
cheng fan48e832c2015-05-29 01:54:47 +0800705 // Short-hand for creating a connection point.
706 private ConnectPoint connectPoint(PcepDpid id, long port) {
707 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
708 }
709
710 // Short-hand for creating a link.
711 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800712 return DefaultLink.builder()
713 .providerId(id())
714 .src(connectPoint(src, sp))
715 .dst(connectPoint(dst, dp))
716 .type(Link.Type.TUNNEL)
717 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800718 }
719
720 // Creates a path that leads through the given devices.
721 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700722 PathType pathtype, PathState pathState) {
Jon Hallcbd1b392017-01-18 20:15:44 -0800723 if (hopList == null || hopList.isEmpty()) {
cheng fan48e832c2015-05-29 01:54:47 +0800724 return null;
725 }
726 List<Link> links = new ArrayList<>();
727 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
728 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
729 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
730 .get(i + 1).getPortNum()));
731 }
732
733 int hopNum = hopList.size() - 2;
734 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
735 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800736 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800737 .set("pathType", String.valueOf(pathtype)).build();
738 return new DefaultPath(id(), links, hopNum, extendAnnotations);
739 }
740
741 // convert the path description to a string.
742 public String pathToString(List<Link> links) {
743 StringBuilder builder = new StringBuilder();
744 builder.append("{");
745 for (Link link : links) {
746 builder.append("(Device:" + link.src().deviceId() + " Port:"
747 + link.src().port().toLong());
748 builder.append(" Device:" + link.dst().deviceId() + " Port:"
749 + link.dst().port().toLong());
750 builder.append(")");
751 }
752 builder.append("}");
753 return builder.toString();
754 }
755
756 // build a TunnelDescription.
757 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
758 TunnelId tunnelId) {
759 TunnelEndPoint srcPoint = null;
760 TunnelEndPoint dstPoint = null;
761 Tunnel.Type tunnelType = null;
762 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
763
764 // add path after codes of tunnel's path merged
765 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800766 pcepTunnel.getPathType(),
767 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800768
769 OpticalTunnelEndPoint.Type endPointType = null;
770 switch (pcepTunnel.type()) {
771 case OCH:
772 tunnelType = Tunnel.Type.OCH;
773 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
774 break;
775
776 case OTN:
777 tunnelType = Tunnel.Type.ODUK;
778 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
779 break;
780
781 case UNI:
782 tunnelType = Tunnel.Type.VLAN;
783 endPointType = null;
784 break;
785
786 default:
787 break;
788 }
789 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
790 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
791 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
792 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
793
794 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
795 Optional.of(srcPort), null,
796 endPointType,
797 OpticalLogicId.logicId(0),
798 true);
799 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
800 Optional.of(dstPort), null,
801 endPointType,
802 OpticalLogicId.logicId(0),
803 true);
804
805 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800806 DefaultAnnotations annotations = DefaultAnnotations
807 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800808 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800809 .set("bandwidth",
810 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800811 .set("index", String.valueOf(pcepTunnel.id())).build();
812
cheng fan48e832c2015-05-29 01:54:47 +0800813 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
814 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800815 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800816 DefaultAnnotations extendAnnotations = DefaultAnnotations
817 .builder()
818 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800819 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800820 annotations = DefaultAnnotations.merge(annotations,
821 extendAnnotations);
822
823 }
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530824 TunnelDescription tunnel = new DefaultTunnelDescription(tunnelId,
cheng fan48e832c2015-05-29 01:54:47 +0800825 srcPoint,
826 dstPoint,
827 tunnelType,
Yi Tsengfa394de2017-02-01 11:26:40 -0800828 new GroupId(0),
cheng fan48e832c2015-05-29 01:54:47 +0800829 id(), name,
830 path,
831 annotations);
832 return tunnel;
cheng fan48e832c2015-05-29 01:54:47 +0800833 }
834
835 /**
836 * Get the tunnelID according to the tunnel key.
837 *
838 * @param tunnelKey tunnel key
839 * @return corresponding tunnel id of the a tunnel key.
840 */
841 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800842 for (String key : tunnelMap.keySet()) {
843 if (key.equals(tunnelKey)) {
844 return tunnelMap.get(key);
845 }
846 }
847 return null;
848 }
849
850 /**
851 * Get the tunnel key according to the tunnelID.
852 *
853 * @param tunnelId tunnel id
854 * @return corresponding a tunnel key of the tunnel id.
855 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700856 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800857 for (String key : tunnelMap.keySet()) {
Sho SHIMIZUc218bfa2016-08-18 14:22:50 -0700858 if (Objects.equals(tunnelMap.get(key).id(), tunnelId.id())) {
cheng fan48e832c2015-05-29 01:54:47 +0800859 return key;
860 }
861 }
862 return null;
863
864 }
865
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530866 /**
chengfan2fff70f2015-08-24 18:20:19 -0500867 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
868 *
869 * @param statistics statistics data from a PCEP tunnel
870 * @return TunnelStatistics
871 */
872 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
873 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
874 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
875 .setPacketLossRatio(statistics.packetLossRate())
876 .setFlowDelay(statistics.flowDelay())
877 .setAlarms(statistics.alarms())
878 .build();
879 return tunnelStatistics;
880 }
881 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530882 * Creates list of hops for ERO object from Path.
883 *
884 * @param path network path
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530885 * @return list of ERO subobjects
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530886 */
887 private LinkedList<PcepValueType> createPcepPath(Path path) {
888 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
889 List<Link> listLink = path.links();
890 ConnectPoint source = null;
891 ConnectPoint destination = null;
892 IpAddress ipDstAddress = null;
893 IpAddress ipSrcAddress = null;
894 PcepValueType subObj = null;
Priyanka Bcdf9b102016-06-07 20:01:38 +0530895 long portNo;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530896
897 for (Link link : listLink) {
898 source = link.src();
899 if (!(source.equals(destination))) {
900 //set IPv4SubObject for ERO object
Priyanka Bcdf9b102016-06-07 20:01:38 +0530901 portNo = source.port().toLong();
902 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
903 ipSrcAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530904 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
905 llSubObjects.add(subObj);
906 }
907
908 destination = link.dst();
Priyanka Bcdf9b102016-06-07 20:01:38 +0530909 portNo = destination.port().toLong();
910 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
911 ipDstAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530912 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
913 llSubObjects.add(subObj);
914 }
Priyanka Bcdf9b102016-06-07 20:01:38 +0530915
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530916 return llSubObjects;
917 }
918
919 /**
920 * Creates PcInitiated lsp request list for setup tunnel.
921 *
922 * @param tunnel mpls tunnel
923 * @param path network path
924 * @param pc pcep client
925 * @param srpId unique id for pcep message
926 * @return list of PcInitiatedLspRequest
927 * @throws PcepParseException while building pcep objects fails
928 */
929 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
930 PcepClient pc, int srpId)
931 throws PcepParseException {
932 PcepValueType tlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530933 LinkedList<PcepValueType> llSubObjects = null;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530934 LspType lspType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530935
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530936 if (lspType == SR_WITHOUT_SIGNALLING) {
937 NetworkResource labelStack = tunnel.resource();
938 if (labelStack == null || !(labelStack instanceof DefaultLabelStack)) {
939 labelStack = pcepClientController.computeLabelStack(tunnel.path());
940 if (labelStack == null) {
941 log.error("Unable to create label stack.");
942 return null;
943 }
944 }
945 llSubObjects = pcepClientController.createPcepLabelStack((DefaultLabelStack) labelStack, path);
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530946 } else {
947 llSubObjects = createPcepPath(path);
948 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530949
Jon Hallcbd1b392017-01-18 20:15:44 -0800950 if (llSubObjects == null || llSubObjects.isEmpty()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530951 log.error("There is no link information to create tunnel");
952 return null;
953 }
954
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530955 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530956
957 // set PathSetupTypeTlv of SRP object
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530958 tlv = new PathSetupTypeTlv(lspType.type());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530959 llOptionalTlv.add(tlv);
960
961 // build SRP object
962 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
963 .setOptionalTlv(llOptionalTlv).build();
964
965 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530966 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530967
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530968 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530969 short localLspId = 0;
970 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
971 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
972 if (localLspIdString != null) {
973 localLspId = Short.valueOf(localLspIdString);
974 }
975 }
976
Priyanka B4c3cef02016-06-14 20:27:53 +0530977 tunnel.annotations().value(LSP_SIG_TYPE);
Avantika-Huawei56c11842016-04-28 00:56:56 +0530978 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
979 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
980 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530981 llOptionalTlv.add(tlv);
982 //set SymbolicPathNameTlv of LSP object
983 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
984 llOptionalTlv.add(tlv);
985
986 //build LSP object
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530987 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setDFlag(true).setOFlag((byte) 0)
988 .setPlspId(0).setOptionalTlv(llOptionalTlv).build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530989
990 //build ENDPOINTS object
991 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
992 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
993 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
994 .setPFlag(true).build();
995
996 //build ERO object
997 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
998
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530999 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301000 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301001 iBandwidth = Float.valueOf(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301002 }
1003 // build bandwidth object
1004 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1005 // build pcep attribute
1006 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1007
1008 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1009 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
1010 .setPcepAttribute(pcepAttribute).build();
1011 llPcInitiatedLspRequestList.add(initiateLspRequest);
1012 return llPcInitiatedLspRequestList;
1013 }
1014
1015 /**
1016 * To send initiate tunnel message to pcc.
1017 *
1018 * @param tunnel mpls tunnel info
1019 * @param path explicit route for the tunnel
1020 * @param pc pcep client to send message
1021 */
1022 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1023 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301024 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1025 log.error("Setup tunnel has failed as LSP DB sync is not finished");
1026 return;
1027 }
1028
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301029 int srpId = SrpIdGenerators.create();
Priyanka B4c3cef02016-06-14 20:27:53 +05301030 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
1031 for (Tunnel t : tunnels) {
1032 if (t.tunnelName().equals(tunnel.tunnelName())) {
1033 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
1034 tunnel.dst(), tunnel.type(),
1035 t.state(), tunnel.groupId(),
1036 t.tunnelId(),
1037 tunnel.tunnelName(),
1038 tunnel.path(),
1039 tunnel.resource(),
1040 tunnel.annotations());
Priyanka B4c3b4512016-07-22 11:41:49 +05301041 break;
Priyanka B4c3cef02016-06-14 20:27:53 +05301042 }
1043 }
1044
1045 if (tunnel.tunnelId() == null) {
1046 log.error("Tunnel ID not found");
1047 return;
1048 }
1049
Avantika-Huawei56c11842016-04-28 00:56:56 +05301050 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301051
Jonathan Hart51539b82015-10-29 09:53:04 -07001052 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301053
1054 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
1055 pc, srpId);
Jon Hallcbd1b392017-01-18 20:15:44 -08001056 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.isEmpty()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301057 log.error("Failed to create PcInitiatedLspRequestList");
1058 return;
1059 }
1060
1061 //build PCInitiate message
1062 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1063 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
1064 .build();
1065
1066 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1067
Jonathan Hart51539b82015-10-29 09:53:04 -07001068 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301069 } catch (PcepParseException e) {
1070 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
1071 }
1072 }
1073
1074 /**
1075 * To send Release tunnel message to pcc.
1076 *
1077 * @param tunnel mpls tunnel info
1078 * @param pc pcep client to send message
1079 */
1080 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
1081 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301082 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1083 log.error("Release tunnel has failed as LSP DB sync is not finished");
1084 return;
1085 }
1086
Avantika-Huawei56c11842016-04-28 00:56:56 +05301087 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001088 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301089 int srpId = SrpIdGenerators.create();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301090
1091 PcepValueType tlv;
1092 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301093
1094 // set PathSetupTypeTlv of SRP object
1095 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
1096 .type());
1097 llOptionalTlv.add(tlv);
1098
1099 // build SRP object
1100 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
1101 .setOptionalTlv(llOptionalTlv).build();
1102
1103 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301104 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
1105
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301106 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1107 llOptionalTlv.add(tlv);
Priyanka B4c3cef02016-06-14 20:27:53 +05301108
1109 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1110 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301111 String pLspIdString = tunnel.annotations().value(PLSP_ID);
1112
Priyanka B4c3cef02016-06-14 20:27:53 +05301113 short localLspId = 0;
1114 short pccTunnelId = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301115 int plspId = 0;
Priyanka B4c3cef02016-06-14 20:27:53 +05301116
1117 if (localLspIdString != null) {
1118 localLspId = Short.valueOf(localLspIdString);
1119 }
1120
1121 if (pccTunnelIdString != null) {
1122 pccTunnelId = Short.valueOf(pccTunnelIdString);
1123 }
1124
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301125 if (pLspIdString != null) {
1126 plspId = Integer.valueOf(pLspIdString);
1127 }
1128
Priyanka B4c3cef02016-06-14 20:27:53 +05301129 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1130 .ip().getIp4Address().toInt()),
1131 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1132 .getIp4Address().toInt()));
1133 llOptionalTlv.add(tlv);
1134
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301135 // build lsp object, set r flag as false to delete the tunnel
1136 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
1137 .setOptionalTlv(llOptionalTlv).build();
1138
1139 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1140 .setLspObject(lspobj).build();
1141
1142 llPcInitiatedLspRequestList.add(releaseLspRequest);
1143
1144 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1145 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1146
1147 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1148
Jonathan Hart51539b82015-10-29 09:53:04 -07001149 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301150 } catch (PcepParseException e) {
1151 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1152 }
1153 }
1154
1155 /**
1156 * To send Update tunnel request message to pcc.
1157 *
1158 * @param tunnel mpls tunnel info
1159 * @param path explicit route for the tunnel
1160 * @param pc pcep client to send message
1161 */
1162 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1163 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301164 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001165 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301166 int srpId = SrpIdGenerators.create();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301167 PcepValueType tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301168
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301169 LinkedList<PcepValueType> llSubObjects = null;
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301170 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
1171
1172 if (lspSigType == SR_WITHOUT_SIGNALLING) {
1173 NetworkResource labelStack = tunnel.resource();
1174 if (labelStack == null || !(labelStack instanceof DefaultLabelStack)) {
1175 labelStack = pcepClientController.computeLabelStack(tunnel.path());
1176 if (labelStack == null) {
1177 log.error("Unable to create label stack.");
1178 return;
1179 }
1180 }
1181 llSubObjects = pcepClientController.createPcepLabelStack((DefaultLabelStack) labelStack, path);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301182 } else {
1183 llSubObjects = createPcepPath(path);
1184 }
1185
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301186 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
1187 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
1188
Avantika-Huawei56c11842016-04-28 00:56:56 +05301189 // set PathSetupTypeTlv of SRP object
Avantika-Huawei56c11842016-04-28 00:56:56 +05301190 tlv = new PathSetupTypeTlv(lspSigType.type());
1191 llOptionalTlv.add(tlv);
1192
1193 // build SRP object
1194 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
1195 .setOptionalTlv(llOptionalTlv).build();
1196
1197 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301198
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301199 // Lsp Identifier tlv is required for all modes of lsp
1200 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1201 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1202 short localLspId = 0;
1203 short pccTunnelId = 0;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301204
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301205 if (localLspIdString != null) {
1206 localLspId = Short.valueOf(localLspIdString);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301207 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301208
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301209 if (pccTunnelIdString != null) {
1210 pccTunnelId = Short.valueOf(pccTunnelIdString);
1211 }
1212
1213 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1214 .ip().getIp4Address().toInt()),
1215 localLspId, pccTunnelId,
1216 ((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt(),
1217 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
1218 llOptionalTlv.add(tlv);
1219
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301220 if (tunnel.tunnelName().value() != null) {
1221 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1222 llOptionalTlv.add(tlv);
1223 }
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301224 boolean delegated = (tunnel.annotations().value(DELEGATE) == null) ? false
1225 : Boolean.valueOf(tunnel.annotations()
1226 .value(DELEGATE));
1227 boolean initiated = (tunnel.annotations().value(PCE_INIT) == null) ? false
1228 : Boolean.valueOf(tunnel.annotations()
1229 .value(PCE_INIT));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301230 // build lsp object
Priyanka B4c3cef02016-06-14 20:27:53 +05301231 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true)
1232 .setPlspId(Integer.valueOf(tunnel.annotations().value(PLSP_ID)))
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301233 .setDFlag(delegated)
1234 .setCFlag(initiated)
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301235 .setOptionalTlv(llOptionalTlv).build();
1236 // build ero object
1237 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
Priyanka B4c3cef02016-06-14 20:27:53 +05301238 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301239 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301240 iBandwidth = Float.parseFloat(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301241 }
1242 // build bandwidth object
1243 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1244 // build pcep attribute
1245 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1246 // build pcep msg path
1247 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1248 .build();
1249
1250 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1251 .setLspObject(lspobj).setMsgPath(msgPath).build();
1252
1253 llUpdateRequestList.add(updateRequest);
1254
1255 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1256
1257 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001258 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301259 } catch (PcepParseException e) {
1260 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1261 }
1262 }
1263
1264 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001265
1266 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001267 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001268 TunnelDescription tunnel = null;
1269 // instance and id identify a tunnel together
1270 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1271 + String.valueOf(pcepTunnel.id());
1272
1273 if (tunnelKey == null || "".equals(tunnelKey)) {
1274 log.error("Invalid PCEP tunnel");
1275 return;
1276 }
1277
1278 TunnelId tunnelId = getTunnelId(tunnelKey);
cheng fan48e832c2015-05-29 01:54:47 +08001279 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1280
1281 OperationType operType = pcepTunnel.getOperationType();
1282 switch (operType) {
1283 case ADD:
1284 tunnelId = service.tunnelAdded(tunnel);
1285 tunnelMap.put(tunnelKey, tunnelId);
1286 break;
1287
1288 case UPDATE:
1289 service.tunnelUpdated(tunnel);
1290 break;
1291
1292 case DELETE:
1293 service.tunnelRemoved(tunnel);
1294 tunnelMap.remove(tunnelKey);
1295 break;
1296
1297 default:
1298 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301299 }
1300 }
cheng fan48e832c2015-05-29 01:54:47 +08001301
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301302 @Override
1303 public void handleMessage(PccId pccId, PcepMessage msg) {
1304 try {
1305 log.debug("tunnel provider handle message {}", msg.getType().toString());
1306 switch (msg.getType()) {
1307 case REPORT:
1308 int srpId = 0;
1309 LinkedList<PcepStateReport> llStateReportList = null;
1310 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1311 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1312 PcepSrpObject srpObj = null;
1313 PcepLspObject lspObj = null;
1314 while (listIterator.hasNext()) {
1315 PcepStateReport stateRpt = listIterator.next();
1316 srpObj = stateRpt.getSrpObject();
1317 lspObj = stateRpt.getLspObject();
1318
1319 if (srpObj instanceof PcepSrpObject) {
1320 srpId = srpObj.getSrpID();
1321 }
1322
1323 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1324 log.debug("SRP ID in handle message " + srpId);
1325
Jonathan Hart51539b82015-10-29 09:53:04 -07001326 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301327 // For PCRpt without matching SRP id.
1328 handleRptWithoutSrpId(stateRpt, pccId);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301329 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301330 }
1331
Avantika-Huawei56c11842016-04-28 00:56:56 +05301332 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301333 }
1334 break;
1335
1336 default:
1337 log.debug("Received unsupported message type {}", msg.getType().toString());
1338 }
1339 } catch (Exception e) {
Frank Wang3a98e0a2017-08-11 11:09:30 +08001340 log.error("Exception occurred while processing report message {}", e.getMessage());
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301341 }
1342 }
1343
1344 /**
1345 * Handles report message for setup/update/delete tunnel request.
1346 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301347 * @param srpId unique identifier for PCEP message
1348 * @param lspObj LSP object
1349 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301350 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301351 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301352 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001353 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301354
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301355 // store the values required from report message
1356 pcepTunnelData.setPlspId(lspObj.getPlspId());
1357 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1358 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1359 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1360
Avantika-Huawei56c11842016-04-28 00:56:56 +05301361 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301362 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1363 while (listTlvIterator.hasNext()) {
1364 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301365 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1366 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301367 break;
1368 }
1369 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001370 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301371 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001372 }
1373
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301374 Path path = pcepTunnelData.path();
1375 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301376 Builder annotationBuilder = DefaultAnnotations.builder();
1377 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1378
1379 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1380 if (tunnel.annotations().value(PLSP_ID) == null) {
1381 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1382 }
1383
1384 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1385 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1386 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1387 }
1388
Priyanka B4c3cef02016-06-14 20:27:53 +05301389 if (tunnel.annotations().value(PCC_TUNNEL_ID) == null) {
1390 annotationBuilder.set(PCC_TUNNEL_ID, String.valueOf(ipv4LspTlv.getTunnelId()));
1391 }
1392
Avantika-Huawei56c11842016-04-28 00:56:56 +05301393 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301394 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1395 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1396 providerId, tunnel.tunnelName(), path,
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301397 tunnel.resource(), annotations);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301398
Avantika-Huawei56c11842016-04-28 00:56:56 +05301399 if (CREATE == pcepTunnelData.requestType()) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301400 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Jonathan Hart51539b82015-10-29 09:53:04 -07001401 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301402 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001403 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301404 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301405 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001406 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1407 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301408 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301409
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301410 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1411
Avantika-Huawei56c11842016-04-28 00:56:56 +05301412 if (lspObj.getRFlag()) {
1413 tunnelRemoved(td);
1414 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301415 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301416 tunnelUpdated(td, tunnelState);
1417 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301418
1419 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1420 // though there are no labels to download for SR-TE.
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301421 if (((pcepLspStatus == PcepLspStatus.GOING_UP)
1422 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING))
1423 // For PCInit tunnel up, few PCC expects PCUpd message after PCInit message,
1424 || ((tunnel.state() == State.INIT)
1425 && (pcepLspStatus == PcepLspStatus.DOWN)
1426 && (tunnel.annotations().value(PCE_INIT) != null
1427 && tunnel.annotations().value(PCE_INIT).equals("true"))
1428 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == WITH_SIGNALLING))) {
1429
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301430 // Query again to get latest tunnel updated with protocol values from PCRpt msg.
1431 updateTunnel(service.tunnelQueryById(tunnel.tunnelId()), tunnel.path());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301432 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301433 }
1434
Priyanka B413fbe82016-05-26 11:44:45 +05301435 private SparseAnnotations getAnnotations(PcepLspObject lspObj, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv,
Priyanka B4c3b4512016-07-22 11:41:49 +05301436 String bandwidth, LspType lspType, String costType, boolean isPceInit) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301437 Builder builder = DefaultAnnotations.builder();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301438 /*
1439 * [RFC 5440] The absence of the METRIC object MUST be interpreted by the PCE as a path computation request
1440 * for which no constraints need be applied to any of the metrics.
1441 */
1442 if (costType != null) {
1443 builder.set(COST_TYPE, costType);
1444 }
1445
Priyanka B4c3b4512016-07-22 11:41:49 +05301446 if (isPceInit) {
1447 builder.set(PCE_INIT, String.valueOf(isPceInit));
1448 }
1449
1450 if (bandwidth != null) {
1451 builder.set(BANDWIDTH, bandwidth);
1452 }
1453
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301454 SparseAnnotations annotations = builder
Priyanka B4c3b4512016-07-22 11:41:49 +05301455 .set(LSP_SIG_TYPE, lspType.name())
Priyanka B413fbe82016-05-26 11:44:45 +05301456 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1457 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1458 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId()))
1459 .set(DELEGATE, String.valueOf(lspObj.getDFlag()))
1460 .build();
1461 return annotations;
1462 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301463
Priyanka B413fbe82016-05-26 11:44:45 +05301464 private LspType getLspType(PcepSrpObject srpObj) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301465 LspType lspType = WITH_SIGNALLING;
1466
1467 if (null != srpObj) {
1468 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1469 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1470
1471 while (listIterator.hasNext()) {
1472 PcepValueType tlv = listIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301473 switch (tlv.getType()) {
1474 case PathSetupTypeTlv.TYPE:
1475 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1476 break;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301477 default:
1478 break;
1479 }
1480 }
1481 }
Priyanka B413fbe82016-05-26 11:44:45 +05301482 return lspType;
1483 }
1484
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301485 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId) {
Priyanka B413fbe82016-05-26 11:44:45 +05301486 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301487 String costType = null;
Priyanka B413fbe82016-05-26 11:44:45 +05301488 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1489 checkNotNull(msgPath);
1490 PcepEroObject eroObj = msgPath.getEroObject();
1491 if (eroObj == null) {
1492 log.error("ERO object is null in report message.");
1493 return;
1494 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301495 PcepAttribute attributes = msgPath.getPcepAttribute();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301496 float bandwidth = 0;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301497 int cost = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301498 if (attributes != null) {
1499 if (attributes.getMetricObjectList() != null) {
1500 ListIterator<PcepMetricObject> iterator = attributes.getMetricObjectList().listIterator();
1501 PcepMetricObject metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301502
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301503 while (metricObj != null) {
1504 if (metricObj.getBType() == IGP_METRIC) {
1505 costType = "COST";
1506 } else if (metricObj.getBType() == TE_METRIC) {
1507 costType = "TE_COST";
1508 }
1509 if (costType != null) {
1510 cost = metricObj.getMetricVal();
1511 log.debug("Path cost {}", cost);
1512 break;
1513 }
1514 metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301515 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301516 }
1517 if (attributes.getBandwidthObject() != null) {
1518 bandwidth = attributes.getBandwidthObject().getBandwidth();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301519 }
1520 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301521 PcepLspObject lspObj = stateRpt.getLspObject();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301522 List<Object> eroSubObjList = buildPathFromEroObj(eroObj, providerId);
1523 List<Link> links = new ArrayList<>();
1524 List<LabelResourceId> labels = new ArrayList<>();
1525 for (Object linkOrLabel : eroSubObjList) {
1526 if (linkOrLabel instanceof Link) {
1527 links.add((Link) linkOrLabel);
1528 } else if (linkOrLabel instanceof Integer) {
1529 labels.add(LabelResourceId.labelResourceId(((Integer) linkOrLabel).longValue()));
1530 }
Priyanka B413fbe82016-05-26 11:44:45 +05301531 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301532 Path path = null;
1533 if (!links.isEmpty()) {
1534 path = new DefaultPath(providerId, links, cost, EMPTY);
1535 } else if (!lspObj.getRFlag()) {
1536 return;
1537 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301538 NetworkResource labelStack = new DefaultLabelStack(labels);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301539 // To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1540 // from PCE.
Priyanka B413fbe82016-05-26 11:44:45 +05301541 PcepSrpObject srpObj = stateRpt.getSrpObject();
1542 LspType lspType = getLspType(srpObj);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301543 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1544 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1545 SymbolicPathNameTlv pathNameTlv = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301546 while (listTlvIterator.hasNext()) {
1547 PcepValueType tlv = listTlvIterator.next();
1548 switch (tlv.getType()) {
1549 case StatefulIPv4LspIdentifiersTlv.TYPE:
1550 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1551 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301552 case SymbolicPathNameTlv.TYPE:
1553 pathNameTlv = (SymbolicPathNameTlv) tlv;
1554 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301555 default:
1556 break;
1557 }
1558 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301559 /*
1560 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1561 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1562 */
1563 if (ipv4LspIdenTlv == null) {
1564 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1565 return;
1566 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301567 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1568 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1569 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1570 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1571 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
Priyanka B413fbe82016-05-26 11:44:45 +05301572 // Store delegation flag info and that LSP info because only delegated PCE sends update message
1573 // Storing if D flag is set, if not dont store. while checking whether delegation if annotation for D flag
1574 // not present then non-delegated , if present it is delegated.
1575 if (lspObj.getDFlag()) {
1576 pcepClientController.getClient(pccId).setLspAndDelegationInfo(
1577 new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()), lspObj.getDFlag());
1578 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301579 Tunnel tunnel = null;
Priyanka B4c3b4512016-07-22 11:41:49 +05301580 SparseAnnotations oldTunnelAnnotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301581 // Asynchronous status change message from PCC for LSP reported earlier.
1582 for (Tunnel tunnelObj : tunnelQueryResult) {
1583 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1584 /*
1585 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1586 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1587 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1588 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1589 * session an LSP is "first" reported to a PCE.
1590 */
1591 if ((pathNameTlv != null)
1592 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1593 tunnel = tunnelObj;
1594 break;
1595 }
1596 continue;
1597 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301598 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId())) {
1599 if ((Integer
1600 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1601 tunnel = tunnelObj;
1602 }
1603 if ((Integer
1604 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) != ipv4LspIdenTlv.getLspId())) {
1605 oldTunnelAnnotations = (SparseAnnotations) tunnelObj.annotations();
1606 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301607 }
1608 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301609 DefaultTunnelDescription td;
Priyanka B413fbe82016-05-26 11:44:45 +05301610 SparseAnnotations annotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301611 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
1612 if (tunnel == null) {
1613 if (lspObj.getRFlag()) {
1614 /*
1615 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1616 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1617 */
1618 return;
1619 }
Priyanka B413fbe82016-05-26 11:44:45 +05301620 DeviceId deviceId = getDevice(pccId);
1621 if (deviceId == null) {
1622 log.error("Ingress deviceId not found");
1623 return;
1624 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301625 String tempBandwidth = null;
1626 String temoCostType = null;
1627 if (oldTunnelAnnotations != null) {
1628 tempBandwidth = oldTunnelAnnotations.value(BANDWIDTH);
1629 temoCostType = oldTunnelAnnotations.value(COST_TYPE);
1630 }
1631 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, tempBandwidth, lspType,
1632 temoCostType, lspObj.getCFlag());
Yi Tsengfa394de2017-02-01 11:26:40 -08001633 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new GroupId(
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301634 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path, labelStack,
Priyanka B413fbe82016-05-26 11:44:45 +05301635 annotations);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301636 // Do not support PCC initiated LSP after LSP DB sync is completed.
1637 if (!lspObj.getSFlag() && !lspObj.getCFlag()) {
1638 log.error("Received PCC initiated LSP while not in sync.");
1639 return;
1640 }
Priyanka B413fbe82016-05-26 11:44:45 +05301641 /*
1642 * If ONOS instance is master for PCC then set delegated flag as annotation and add the tunnel to store.
1643 * Because all LSPs need not be delegated, hence mastership for the PCC is confirmed whereas not the
1644 * delegation set to all LSPs.If ONOS is not the master for that PCC then check if D flag is set, if yes
1645 * wait for 2 seconds [while master has added the tunnel to the store] then update the tunnel. Tunnel is
1646 * updated because in case of resilency only delegated LSPs are recomputed and only delegated PCE can
1647 * send update message to that client.
1648 *
1649 * 1)Master can 1st get the Rpt message
1650 * a)Master adds the tunnel into core.
1651 * b)If a non-master for ingress gets Rpt message with D flag set[as delegation owner]
1652 * after master, then runs timer then update the tunnel with D flag set.
1653 * 2)Non-Master can 1st get the Rpt message
1654 * a)Non-Master runs the timer check for the tunnel then updates the tunnel with D flag set
1655 * b)Master would have got the message while the non-master running timer, hence master adds
1656 * tunnel to core
1657 *
1658 * In general always master adds the tunnel to the core
1659 * while delegated owner [master or non-master with D flag set] always updates the tunnel running timer
1660 */
1661 if (mastershipService.isLocalMaster(deviceId)) {
1662 TunnelId tId = tunnelAdded(td, tunnelState);
1663 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
Yi Tsengfa394de2017-02-01 11:26:40 -08001664 tunnelState, new GroupId(0), tId, TunnelName.tunnelName(String.valueOf(pathNameTlv
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301665 .getValue())), path, labelStack, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301666
Priyanka B413fbe82016-05-26 11:44:45 +05301667 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1668 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1669 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1670 } else if (!mastershipService.isLocalMaster(deviceId) && lspObj.getDFlag()) {
1671 //Start timer then update the tunnel with D flag
Priyanka B4c3b4512016-07-22 11:41:49 +05301672 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId, tunnelState, ipv4LspIdenTlv);
Priyanka B413fbe82016-05-26 11:44:45 +05301673 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301674 return;
1675 }
Priyanka B413fbe82016-05-26 11:44:45 +05301676 //delegated owner will update can be a master or non-master
Priyanka B4c3b4512016-07-22 11:41:49 +05301677 if (lspObj.getDFlag() && !lspObj.getRFlag()) {
1678 tunnelUpdateForDelegatedLsp(tunnel, lspObj,
1679 lspType, tunnelState, pccId, labelStack, ipv4LspIdenTlv);
1680 return;
Priyanka B413fbe82016-05-26 11:44:45 +05301681 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301682 removeOrUpdatetunnel(tunnel, lspObj, providerId, tunnelState, ipv4LspIdenTlv);
Priyanka B413fbe82016-05-26 11:44:45 +05301683 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301684
Priyanka B4c3b4512016-07-22 11:41:49 +05301685 private void tunnelUpdateForDelegatedLsp(Tunnel tunnel, PcepLspObject lspObj,
1686 LspType lspType, State tunnelState, PccId pccId,
1687 NetworkResource labelStack,
1688 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv) {
1689 SparseAnnotations annotations = null;
1690 DefaultTunnelDescription td;
1691 boolean isPceInit = tunnel.annotations().value(PCE_INIT) == null ? false :
1692 Boolean.valueOf((tunnel.annotations().value(PCE_INIT))).booleanValue();
1693 annotations = getAnnotations(lspObj, ipv4LspIdenTlv,
1694 tunnel.annotations().value(BANDWIDTH), lspType,
1695 tunnel.annotations().value(COST_TYPE), isPceInit);
Yi Tsengfa394de2017-02-01 11:26:40 -08001696 td = new DefaultTunnelDescription(null, tunnel.src(), tunnel.dst(), MPLS, new GroupId(
Priyanka B4c3b4512016-07-22 11:41:49 +05301697 0), tunnel.providerId(), tunnel.tunnelName(),
1698 tunnel.path(), labelStack, annotations);
1699 tunnelUpdateInDelegatedCase(pccId, annotations, td, tunnel.providerId(), tunnelState, ipv4LspIdenTlv);
1700 }
1701
1702 private void removeOrUpdatetunnel(Tunnel tunnel, PcepLspObject lspObj, ProviderId providerId,
1703 State tunnelState, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv) {
Priyanka B413fbe82016-05-26 11:44:45 +05301704 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1705 tunnel.type(), tunnel.groupId(), providerId, tunnel.tunnelName(), tunnel.path(),
1706 (SparseAnnotations) tunnel.annotations());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301707 if (lspObj.getRFlag()) {
1708 tunnelRemoved(td);
1709 } else {
Priyanka B4c3b4512016-07-22 11:41:49 +05301710 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, tunnel.path(), LSP_STATE_RPT);
1711 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1712 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301713 tunnelUpdated(td, tunnelState);
1714 }
Priyanka B413fbe82016-05-26 11:44:45 +05301715 }
1716
1717 private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
Priyanka B4c3b4512016-07-22 11:41:49 +05301718 DefaultTunnelDescription td, ProviderId providerId, State tunnelState,
1719 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv) {
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301720 // Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
Priyanka B413fbe82016-05-26 11:44:45 +05301721
1722 /*
1723 * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
1724 * master has added the tunnel to the store] then update the tunnel.
1725 */
1726 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1727
1728 // Thread is started after 2 seconds first time later periodically after 2 seconds to update the tunnel
1729 executor.scheduleAtFixedRate(new UpdateDelegation(td, providerId, annotations, pccId,
Priyanka B4c3b4512016-07-22 11:41:49 +05301730 executor, tunnelState, ipv4LspIdentifiersTlv), DELAY, DELAY, TimeUnit.SECONDS);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301731 }
1732
1733 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301734 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301735 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301736 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301737 * @param providerId provider id
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301738 * @return list of links and labels
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301739 */
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301740 private List<Object> buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301741 checkNotNull(eroObj);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301742 List<Object> subObjList = new ArrayList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301743 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Jon Hallcbd1b392017-01-18 20:15:44 -08001744 if (llSubObj.isEmpty()) {
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301745 log.error("ERO in report message does not have hop information");
Priyanka B4c3b4512016-07-22 11:41:49 +05301746 return new ArrayList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301747 }
1748 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1749
1750 ConnectPoint src = null;
1751 ConnectPoint dst = null;
1752 boolean isSrcSet = false;
1753 while (tlvIterator.hasNext()) {
1754 PcepValueType subObj = tlvIterator.next();
1755 switch (subObj.getType()) {
1756
1757 case IPv4SubObject.TYPE:
1758
1759 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1760 if (!isSrcSet) {
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301761 Iterable<Link> links = linkService.getActiveLinks();
1762 for (Link l : links) {
1763 if (l.src().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1764 src = l.src();
1765 isSrcSet = true;
1766 break;
1767 } else if (l.dst().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1768 src = l.dst();
1769 isSrcSet = true;
1770 break;
1771 }
1772 }
1773 } else {
1774 Iterable<Link> links = linkService.getActiveLinks();
1775 for (Link l : links) {
1776 if (l.src().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1777 dst = l.src();
1778 break;
1779 } else if (l.dst().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1780 dst = l.dst();
1781 break;
1782 }
1783 }
Ray Milkey2693bda2016-01-22 16:08:14 -08001784 Link link = DefaultLink.builder()
1785 .providerId(providerId)
1786 .src(src)
1787 .dst(dst)
1788 .type(Link.Type.DIRECT)
1789 .build();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301790 subObjList.add(link);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301791 src = dst;
1792 }
1793 break;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301794 case SrEroSubObject.TYPE:
1795 SrEroSubObject srEroSubObj = (SrEroSubObject) subObj;
1796 subObjList.add(srEroSubObj.getSid());
1797
1798 if (srEroSubObj.getSt() == PcepNaiIpv4Adjacency.ST_TYPE) {
1799 PcepNaiIpv4Adjacency nai = (PcepNaiIpv4Adjacency) (srEroSubObj.getNai());
Priyanka B4c3b4512016-07-22 11:41:49 +05301800 int srcIp = nai.getLocalIpv4Addr();
1801 int dstIp = nai.getRemoteIpv4Addr();
1802 Iterable<Link> links = linkService.getActiveLinks();
1803 for (Link l : links) {
1804 long lSrc = l.src().port().toLong();
1805 long lDst = l.dst().port().toLong();
1806 if (lSrc == srcIp) {
1807 src = l.src();
1808 } else if (lDst == srcIp) {
1809 src = l.dst();
1810 }
1811 if (lSrc == dstIp) {
1812 dst = l.src();
1813 } else if (lDst == dstIp) {
1814 dst = l.dst();
1815 }
1816 if (src != null && dst != null) {
1817 break;
1818 }
1819 }
1820 if (src == null || dst == null) {
1821 return new ArrayList<>();
1822 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301823 Link link = DefaultLink.builder()
1824 .providerId(providerId)
1825 .src(src)
1826 .dst(dst)
1827 .type(Link.Type.DIRECT)
1828 .build();
1829 subObjList.add(link);
1830 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301831 default:
1832 // the other sub objects are not required
1833 }
1834 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301835 return subObjList;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301836 }
1837
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301838 @Override
1839 public void clientConnected(PccId pccId) {
1840 // TODO
1841 }
1842
1843 @Override
1844 public void clientDisconnected(PccId pccId) {
1845 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001846 }
chengfan2fff70f2015-08-24 18:20:19 -05001847
chengfan2fff70f2015-08-24 18:20:19 -05001848 @Override
1849 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1850 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1851 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1852 tunnelStatisticsMap.put(id, tunnelStatistics);
1853 }
cheng fan48e832c2015-05-29 01:54:47 +08001854
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301855 @Override
1856 public void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301857
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301858 if (endOfSyncAction == SEND_UPDATE) {
1859 updateTunnel(tunnel, tunnel.path());
1860 return;
1861 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301862
1863 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1864 tunnel.src(), tunnel.dst(),
1865 tunnel.type(),
1866 tunnel.groupId(),
1867 tunnel.providerId(),
1868 tunnel.tunnelName(),
1869 tunnel.path(),
1870 (SparseAnnotations) tunnel.annotations());
1871
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301872
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301873 if (endOfSyncAction == PcepLspSyncAction.UNSTABLE) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301874 // Send PCInit msg again after global reoptimization.
1875 tunnelUpdated(td, UNSTABLE);
1876
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301877 // To remove the old tunnel from store whose PLSPID is not recognized by ingress PCC.
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301878 tunnelRemoved(td);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301879 } else if (endOfSyncAction == REMOVE) {
1880 tunnelRemoved(td);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301881 }
1882 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301883 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301884 @Override
1885 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1886 return service.tunnelQueryById(tunnelId);
1887 }
1888
Priyanka B413fbe82016-05-26 11:44:45 +05301889 private DeviceId getDevice(PccId pccId) {
1890 // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID.
1891 IpAddress lsrId = pccId.ipAddress();
1892 String lsrIdentifier = String.valueOf(lsrId);
1893
1894 // Find PCC deviceID from lsrId stored as annotations
1895 Iterable<Device> devices = deviceService.getAvailableDevices();
1896 for (Device dev : devices) {
1897 if (dev.annotations().value(AnnotationKeys.TYPE).equals("L3")
1898 && dev.annotations().value(LSRID).equals(lsrIdentifier)) {
1899 return dev.id();
1900 }
1901 }
1902 return null;
1903 }
1904
1905 /**
1906 * Updates the tunnel with updated tunnel annotation after a delay of two seconds and checks it till
1907 * tunnel is found.
1908 */
1909 private class UpdateDelegation implements Runnable {
1910 DefaultTunnelDescription td;
1911 ProviderId providerId;
1912 SparseAnnotations annotations;
1913 PccId pccId;
1914 ScheduledExecutorService executor;
Priyanka B4c3b4512016-07-22 11:41:49 +05301915 State tunnelState;
1916 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv;
Priyanka B413fbe82016-05-26 11:44:45 +05301917
1918 /**
1919 * Creates an instance of UpdateDelegation.
1920 *
1921 * @param td tunnel description
1922 * @param providerId provider id
1923 * @param annotations tunnel annotations
1924 * @param pccId PCEP client id
1925 * @param executor service of delegated owner
1926 */
1927 public UpdateDelegation(DefaultTunnelDescription td, ProviderId providerId, SparseAnnotations annotations,
Priyanka B4c3b4512016-07-22 11:41:49 +05301928 PccId pccId, ScheduledExecutorService executor, State tunnelState,
1929 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv) {
Priyanka B413fbe82016-05-26 11:44:45 +05301930 this.td = td;
1931 this.providerId = providerId;
1932 this.annotations = annotations;
1933 this.pccId = pccId;
1934 this.executor = executor;
Priyanka B4c3b4512016-07-22 11:41:49 +05301935 this.tunnelState = tunnelState;
1936 this.ipv4LspIdentifiersTlv = ipv4LspIdentifiersTlv;
Priyanka B413fbe82016-05-26 11:44:45 +05301937 }
1938
1939 //Temporary using annotations later will use projection/network config service
1940 @Override
1941 public void run() {
1942 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(td.src(), td.dst());
1943 TunnelId tempTunnelId = null;
1944 for (Tunnel t : tunnelQueryResult) {
1945 if (t.annotations().value(LOCAL_LSP_ID) == null || t.annotations().value(PLSP_ID) == null) {
1946 continue;
1947 }
1948
1949 if (t.annotations().value(LOCAL_LSP_ID).equals(td.annotations().value(LOCAL_LSP_ID))
1950 && t.annotations().value(PLSP_ID).equals(td.annotations().value(PLSP_ID))
1951 && ((IpTunnelEndPoint) t.src()).ip().equals(pccId.id())) {
1952 tempTunnelId = t.tunnelId();
1953 break;
1954 }
1955 }
1956
1957 //If tunnel is found update the tunnel and shutdown the thread otherwise thread will be executing
1958 //periodically
1959 if (tempTunnelId != null) {
Yi Tsengfa394de2017-02-01 11:26:40 -08001960 Tunnel tunnel = new DefaultTunnel(providerId, td.src(), td.dst(), MPLS, new GroupId(0),
Priyanka B413fbe82016-05-26 11:44:45 +05301961 tempTunnelId, td.tunnelName(), td.path(), annotations);
Priyanka B4c3b4512016-07-22 11:41:49 +05301962 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, tunnel.path(), LSP_STATE_RPT);
1963 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdentifiersTlv);
1964 pcepTunnelData.setLspDFlag(Boolean.valueOf(tunnel.annotations().value(DELEGATE)));
1965 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1966 tunnelUpdated(tunnel, td.path(), tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +05301967 executor.shutdown();
1968 try {
1969 executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
1970 } catch (InterruptedException e) {
1971 log.error("updating delegation failed");
1972 }
1973 }
1974 }
1975 }
cheng fan48e832c2015-05-29 01:54:47 +08001976}