blob: 4b7b6058cb59a6e3cddaddc78c54e5b6040a0dd6 [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;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080027import org.onlab.graph.ScalarWeight;
Priyanka Bcdf9b102016-06-07 20:01:38 +053028import org.onlab.packet.Ip4Address;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053029import org.onlab.packet.IpAddress;
chengfan2fff70f2015-08-24 18:20:19 -050030import org.onosproject.cfg.ComponentConfigService;
Yi Tsengfa394de2017-02-01 11:26:40 -080031import org.onosproject.core.GroupId;
Avantika-Huaweif849aab2016-06-21 22:29:15 +053032import org.onosproject.incubator.net.resource.label.LabelResourceId;
33import org.onosproject.incubator.net.tunnel.DefaultLabelStack;
cheng fan48e832c2015-05-29 01:54:47 +080034import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053035import org.onosproject.incubator.net.tunnel.DefaultTunnel;
cheng fan48e832c2015-05-29 01:54:47 +080036import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
chengfan2fff70f2015-08-24 18:20:19 -050037import org.onosproject.incubator.net.tunnel.DefaultTunnelStatistics;
Jonathan Hart51539b82015-10-29 09:53:04 -070038import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
cheng fan48e832c2015-05-29 01:54:47 +080039import org.onosproject.incubator.net.tunnel.OpticalLogicId;
40import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
41import org.onosproject.incubator.net.tunnel.Tunnel;
Avantika-Huawei56c11842016-04-28 00:56:56 +053042import org.onosproject.incubator.net.tunnel.Tunnel.State;
Priyanka B413fbe82016-05-26 11:44:45 +053043import org.onosproject.incubator.net.tunnel.TunnelAdminService;
cheng fan48e832c2015-05-29 01:54:47 +080044import org.onosproject.incubator.net.tunnel.TunnelDescription;
45import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
46import org.onosproject.incubator.net.tunnel.TunnelId;
47import org.onosproject.incubator.net.tunnel.TunnelName;
48import org.onosproject.incubator.net.tunnel.TunnelProvider;
49import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
50import org.onosproject.incubator.net.tunnel.TunnelProviderService;
chengfan2fff70f2015-08-24 18:20:19 -050051import org.onosproject.incubator.net.tunnel.TunnelService;
52import org.onosproject.incubator.net.tunnel.TunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053053import org.onosproject.mastership.MastershipService;
54import org.onosproject.net.AnnotationKeys;
cheng fan48e832c2015-05-29 01:54:47 +080055import org.onosproject.net.ConnectPoint;
56import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei56c11842016-04-28 00:56:56 +053057import org.onosproject.net.DefaultAnnotations.Builder;
cheng fan48e832c2015-05-29 01:54:47 +080058import org.onosproject.net.DefaultLink;
59import org.onosproject.net.DefaultPath;
Priyanka B413fbe82016-05-26 11:44:45 +053060import org.onosproject.net.Device;
cheng fan48e832c2015-05-29 01:54:47 +080061import org.onosproject.net.DeviceId;
62import org.onosproject.net.ElementId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053063import org.onosproject.net.IpElementId;
cheng fan48e832c2015-05-29 01:54:47 +080064import org.onosproject.net.Link;
Avantika-Huaweif849aab2016-06-21 22:29:15 +053065import org.onosproject.net.NetworkResource;
cheng fan48e832c2015-05-29 01:54:47 +080066import org.onosproject.net.Path;
67import org.onosproject.net.PortNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053068import org.onosproject.net.SparseAnnotations;
Priyanka B413fbe82016-05-26 11:44:45 +053069import org.onosproject.net.device.DeviceService;
Priyanka Bc1e4e4c2016-07-01 14:57:19 +053070import org.onosproject.net.link.LinkService;
cheng fan48e832c2015-05-29 01:54:47 +080071import org.onosproject.net.provider.AbstractProvider;
72import org.onosproject.net.provider.ProviderId;
73import org.onosproject.pcep.api.PcepController;
74import org.onosproject.pcep.api.PcepDpid;
75import org.onosproject.pcep.api.PcepHopNodeDescription;
76import org.onosproject.pcep.api.PcepOperator.OperationType;
77import org.onosproject.pcep.api.PcepTunnel;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053078import org.onosproject.pcep.api.PcepTunnel.PathState;
Jonathan Hart51539b82015-10-29 09:53:04 -070079import org.onosproject.pcep.api.PcepTunnel.PathType;
cheng fan48e832c2015-05-29 01:54:47 +080080import org.onosproject.pcep.api.PcepTunnelListener;
chengfan2fff70f2015-08-24 18:20:19 -050081import org.onosproject.pcep.api.PcepTunnelStatistics;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +053082import org.onosproject.pcep.server.LspKey;
83import org.onosproject.pcep.server.LspType;
84import org.onosproject.pcep.server.PccId;
85import org.onosproject.pcep.server.PcepClient;
86import org.onosproject.pcep.server.PcepClientController;
87import org.onosproject.pcep.server.PcepClientListener;
88import org.onosproject.pcep.server.PcepEventListener;
89import org.onosproject.pcep.server.PcepLspStatus;
90import org.onosproject.pcep.server.PcepLspSyncAction;
91import org.onosproject.pcep.server.SrpIdGenerators;
92import org.onosproject.pcep.server.PcepSyncStatus;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053093import org.onosproject.pcepio.exceptions.PcepParseException;
94import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
95import org.onosproject.pcepio.protocol.PcepAttribute;
96import org.onosproject.pcepio.protocol.PcepBandwidthObject;
97import org.onosproject.pcepio.protocol.PcepEndPointsObject;
98import org.onosproject.pcepio.protocol.PcepEroObject;
99import org.onosproject.pcepio.protocol.PcepInitiateMsg;
100import org.onosproject.pcepio.protocol.PcepLspObject;
101import org.onosproject.pcepio.protocol.PcepMessage;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530102import org.onosproject.pcepio.protocol.PcepMetricObject;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530103import org.onosproject.pcepio.protocol.PcepMsgPath;
104import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530105import org.onosproject.pcepio.protocol.PcepSrpObject;
106import org.onosproject.pcepio.protocol.PcepStateReport;
107import org.onosproject.pcepio.protocol.PcepUpdateMsg;
108import org.onosproject.pcepio.protocol.PcepUpdateRequest;
109import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530110import org.onosproject.pcepio.types.PathSetupTypeTlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530111import org.onosproject.pcepio.types.PcepNaiIpv4Adjacency;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530112import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530113import org.onosproject.pcepio.types.SrEroSubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530114import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530115import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -0500116import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -0700117import org.osgi.service.component.annotations.Modified;
118import org.slf4j.Logger;
119
120import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530121import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530122import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700123import java.util.Collections;
124import java.util.Dictionary;
125import java.util.HashMap;
126import java.util.LinkedList;
127import java.util.List;
128import java.util.ListIterator;
Sho SHIMIZUc218bfa2016-08-18 14:22:50 -0700129import java.util.Objects;
Jonathan Hart51539b82015-10-29 09:53:04 -0700130import java.util.Optional;
Priyanka B413fbe82016-05-26 11:44:45 +0530131import java.util.concurrent.Executors;
132import java.util.concurrent.ScheduledExecutorService;
133import java.util.concurrent.TimeUnit;
Jonathan Hart51539b82015-10-29 09:53:04 -0700134
135import static com.google.common.base.Preconditions.checkNotNull;
136import static com.google.common.base.Strings.isNullOrEmpty;
137import static org.onlab.util.Tools.get;
Priyanka B4c3cef02016-06-14 20:27:53 +0530138import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530139import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700140import static org.onosproject.net.DefaultAnnotations.EMPTY;
141import static org.onosproject.net.DeviceId.deviceId;
142import static org.onosproject.net.PortNumber.portNumber;
143import static org.onosproject.pcep.api.PcepDpid.uri;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +0530144import static org.onosproject.pcep.server.LspType.WITH_SIGNALLING;
145import static org.onosproject.pcep.server.LspType.SR_WITHOUT_SIGNALLING;
146import static org.onosproject.pcep.server.LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
147import static org.onosproject.pcep.server.PcepAnnotationKeys.BANDWIDTH;
148import static org.onosproject.pcep.server.PcepAnnotationKeys.LOCAL_LSP_ID;
149import static org.onosproject.pcep.server.PcepAnnotationKeys.LSP_SIG_TYPE;
150import static org.onosproject.pcep.server.PcepAnnotationKeys.PCC_TUNNEL_ID;
151import static org.onosproject.pcep.server.PcepAnnotationKeys.PCE_INIT;
152import static org.onosproject.pcep.server.PcepAnnotationKeys.PLSP_ID;
153import static org.onosproject.pcep.server.PcepAnnotationKeys.DELEGATE;
154import static org.onosproject.pcep.server.PcepAnnotationKeys.COST_TYPE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530155import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
156import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
157import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
158import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530159import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +0530160import static org.onosproject.pcep.server.PcepLspSyncAction.REMOVE;
161import static org.onosproject.pcep.server.PcepLspSyncAction.SEND_UPDATE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530162import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.IGP_METRIC;
163import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.TE_METRIC;
Jonathan Hart51539b82015-10-29 09:53:04 -0700164import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800165
166/**
167 * Provider which uses an PCEP controller to detect, update, create network
168 * tunnels.
169 */
170@Component(immediate = true)
171@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530172public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800173
174 private static final Logger log = getLogger(PcepTunnelProvider.class);
175 private static final long MAX_BANDWIDTH = 99999744;
176 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800177 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530178 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
Priyanka B413fbe82016-05-26 11:44:45 +0530179 public static final long IDENTIFIER_SET = 0x100000000L;
180 public static final long SET = 0xFFFFFFFFL;
181 private static final int DELAY = 2;
182 private static final int WAIT_TIME = 5;
183 public static final String LSRID = "lsrId";
cheng fan48e832c2015-05-29 01:54:47 +0800184
chengfan2fff70f2015-08-24 18:20:19 -0500185 static final int POLL_INTERVAL = 10;
186 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
187 label = "Frequency (in seconds) for polling tunnel statistics")
188 private int tunnelStatsPollFrequency = POLL_INTERVAL;
189
cheng fan48e832c2015-05-29 01:54:47 +0800190 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
193 protected TunnelProviderRegistry tunnelProviderRegistry;
194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
196 protected PcepController controller;
197
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
199 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500200
201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
202 protected TunnelService tunnelService;
203
204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
205 protected ComponentConfigService cfgService;
206
Priyanka B413fbe82016-05-26 11:44:45 +0530207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
208 protected TunnelAdminService tunnelAdminService;
209
210 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
211 protected MastershipService mastershipService;
212
213 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
214 protected DeviceService deviceService;
215
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530216 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
217 protected LinkService linkService;
218
cheng fan48e832c2015-05-29 01:54:47 +0800219 TunnelProviderService service;
220
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800221 HashMap<String, TunnelId> tunnelMap = new HashMap<>();
chengfan2fff70f2015-08-24 18:20:19 -0500222 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400223 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800224
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530225 private InnerTunnelProvider listener = new InnerTunnelProvider();
226
Jonathan Hart51539b82015-10-29 09:53:04 -0700227 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530228 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800229
230 /**
231 * Creates a Tunnel provider.
232 */
233 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530234 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800235 }
236
237 @Activate
238 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500239 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800240 service = tunnelProviderRegistry.register(this);
241 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530242 pcepClientController.addListener(listener);
243 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500244 tunnelService.queryAllTunnels().forEach(tunnel -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700245 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500246 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
247 tsc.start();
248 collectors.put(tunnel.tunnelId().id(), tsc);
249
250 });
251
cheng fan48e832c2015-05-29 01:54:47 +0800252 log.info("Started");
253 }
254
255 @Deactivate
256 public void deactivate() {
257 tunnelProviderRegistry.unregister(this);
258 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500259 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530260 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800261 log.info("Stopped");
262 }
263
chengfan2fff70f2015-08-24 18:20:19 -0500264 @Modified
265 public void modified(ComponentContext context) {
266 Dictionary<?, ?> properties = context.getProperties();
267 int newTunnelStatsPollFrequency;
268 try {
269 String s = get(properties, "tunnelStatsPollFrequency");
270 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
271
272 } catch (NumberFormatException | ClassCastException e) {
273 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
274 }
275
276 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
277 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
278 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
279 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
280 }
chengfan2fff70f2015-08-24 18:20:19 -0500281 }
282
cheng fan48e832c2015-05-29 01:54:47 +0800283 @Override
284 public void setupTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530285 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530286 log.error("Tunnel Type MPLS is only supported");
287 return;
288 }
cheng fan48e832c2015-05-29 01:54:47 +0800289
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530290 // check for tunnel end points
291 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
292 log.error("Tunnel source or destination is not valid");
293 return;
294 }
295
296 // Get the pcc client
297 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
298
Ray Milkeyef310052018-02-06 08:58:51 -0800299 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530300 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500301 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530302 return;
303 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530304
305 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
Priyanka B413fbe82016-05-26 11:44:45 +0530306 //Only master will initiate setup tunnel
307 if (pc.capability().pcInstantiationCapability() && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530308 pcepSetupTunnel(tunnel, path, pc);
309 }
cheng fan48e832c2015-05-29 01:54:47 +0800310 }
311
312 @Override
313 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800314
Priyanka B4c3cef02016-06-14 20:27:53 +0530315 //TODO: tunnel which is passed doesn't have tunnelID
Avantika-Huawei56c11842016-04-28 00:56:56 +0530316 if (tunnel.annotations().value(PLSP_ID) != null) {
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530317 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITHOUT_SIGNALLING_AND_WITHOUT_SR) {
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530318 updateTunnel(tunnel, path);
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530319 } else {
320 // Download labels and send update message.
321 // To get new tunnel ID (modified tunnel ID)
322 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
323 for (Tunnel t : tunnels) {
324 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
325 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
326 tunnel.dst(), tunnel.type(),
327 t.state(), tunnel.groupId(),
328 t.tunnelId(),
329 tunnel.tunnelName(),
330 tunnel.path(),
331 tunnel.resource(),
332 tunnel.annotations());
333 break;
334 }
335 }
336 if (!pcepClientController.allocateLocalLabel(tunnel)) {
337 log.error("Unable to allocate labels for the tunnel {}.", tunnel.toString());
338 }
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530339 }
Avantika-Huawei56c11842016-04-28 00:56:56 +0530340 return;
341 }
342
343 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530344 log.error("Tunnel Type MPLS is only supported");
345 return;
346 }
347
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530348 // check for tunnel end points
349 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
350 log.error("Tunnel source or destination is not valid");
351 return;
352 }
353
Priyanka Bcdf9b102016-06-07 20:01:38 +0530354 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530355
Ray Milkeyef310052018-02-06 08:58:51 -0800356 if (pc == null) {
Priyanka B4c3cef02016-06-14 20:27:53 +0530357 log.error("There is no PCC connected with this device {}"
358 + srcElement.toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530359 return;
360 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530361
Priyanka B413fbe82016-05-26 11:44:45 +0530362 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
363 //Only master will initiate setup tunnel
364 if (pc.capability().pcInstantiationCapability()
365 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530366 pcepSetupTunnel(tunnel, path, pc);
367 }
cheng fan48e832c2015-05-29 01:54:47 +0800368 }
369
370 @Override
371 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800372
Avantika-Huawei56c11842016-04-28 00:56:56 +0530373 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530374 log.error("Tunnel Type MPLS is only supported");
375 return;
376 }
377
378 // check for tunnel end points
379 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
380 log.error("Tunnel source or destination is not valid");
381 return;
382 }
383
384 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
385
Ray Milkeyef310052018-02-06 08:58:51 -0800386 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530387 log.error("There is no PCC connected with ip addresss {}"
388 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
389 return;
390 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530391
Priyanka B413fbe82016-05-26 11:44:45 +0530392 //Only master will release tunnel
393 if (pc.capability().pcInstantiationCapability()
394 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530395 pcepReleaseTunnel(tunnel, pc);
396 }
cheng fan48e832c2015-05-29 01:54:47 +0800397 }
398
399 @Override
400 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530401 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530402 log.error("Tunnel Type MPLS is only supported");
403 return;
404 }
cheng fan48e832c2015-05-29 01:54:47 +0800405
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530406 if (!(srcElement instanceof IpElementId)) {
407 log.error("Element id is not valid");
408 return;
409 }
410
411 // check for tunnel end points
412 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
413 log.error("Tunnel source or destination is not valid");
414 return;
415 }
416
417 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
418
Ray Milkeyef310052018-02-06 08:58:51 -0800419 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530420 log.error("There is no PCC connected with ip addresss {}"
421 + ((IpElementId) srcElement).ipAddress().toString());
422 return;
423 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530424
Priyanka B413fbe82016-05-26 11:44:45 +0530425 //Only master will release tunnel
426 if (pc.capability().pcInstantiationCapability()
427 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530428 pcepReleaseTunnel(tunnel, pc);
429 }
cheng fan48e832c2015-05-29 01:54:47 +0800430 }
431
432 @Override
433 public void updateTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530434 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530435 log.error("Tunnel Type MPLS is only supported");
436 return;
437 }
cheng fan48e832c2015-05-29 01:54:47 +0800438
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530439 // check for tunnel end points
440 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
441 log.error("Tunnel source or destination is not valid");
442 return;
443 }
444
Priyanka B4c3cef02016-06-14 20:27:53 +0530445 //To get new tunnel ID (modified tunnel ID)
446 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
447 for (Tunnel t : tunnels) {
448 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
449 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
450 tunnel.dst(), tunnel.type(),
451 t.state(), tunnel.groupId(),
452 t.tunnelId(),
453 tunnel.tunnelName(),
454 tunnel.path(),
455 tunnel.resource(),
456 tunnel.annotations());
457 break;
458 }
459 }
460
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530461 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
462
Ray Milkeyef310052018-02-06 08:58:51 -0800463 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530464 log.error("There is no PCC connected with ip addresss {}"
465 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
466 return;
467 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530468
Priyanka B4c3cef02016-06-14 20:27:53 +0530469 //PCInitiate tunnels are always have D flag set, else check for tunnels who are delegated via LspKey
470 if (pc.capability().statefulPceCapability()) {
471 if (tunnel.annotations().value(PCE_INIT) != null && tunnel.annotations().value(PCE_INIT).equals("true")) {
472 pcepUpdateTunnel(tunnel, path, pc);
Priyanka B4c3b4512016-07-22 11:41:49 +0530473 } else {
474 // If delegation flag is set then only send update message[means delegated PCE can send update msg for
475 // that LSP. If annotation is null D flag is not set else it is set.
476 Short localLspId = 0;
477 for (Tunnel t : tunnels) {
478 if (!t.tunnelId().equals(tunnel.tunnelId()) && t.tunnelName().equals(tunnel.tunnelName())) {
479 localLspId = Short.valueOf(t.annotations().value(LOCAL_LSP_ID));
480 }
481 }
482
483 if (localLspId == 0) {
484 log.error("Local LSP ID for old tunnel not found");
485 return;
486 }
487
488 if (pc.delegationInfo(new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)),
489 localLspId.shortValue())) != null) {
490
491 pcepUpdateTunnel(tunnel, path, pc);
492 }
Priyanka B4c3cef02016-06-14 20:27:53 +0530493 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530494 }
cheng fan48e832c2015-05-29 01:54:47 +0800495 }
496
497 @Override
498 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800499
Avantika-Huawei56c11842016-04-28 00:56:56 +0530500 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530501 log.error("Tunnel Type MPLS is only supported");
502 return;
503 }
504
505 if (!(srcElement instanceof IpElementId)) {
506 log.error("Element id is not valid");
507 return;
508 }
509
510 // check for tunnel end points
511 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
512 log.error("Tunnel source or destination is not valid");
513 return;
514 }
515
516 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
517
Ray Milkeyef310052018-02-06 08:58:51 -0800518 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530519 log.error("There is no PCC connected with ip addresss {}"
520 + ((IpElementId) srcElement).ipAddress().toString());
521 return;
522 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530523
Priyanka B413fbe82016-05-26 11:44:45 +0530524 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
525 // LSP].If annotation is null D flag is not set else it is set.
526 if (pc.capability().statefulPceCapability()
527 && pc.delegationInfo(
528 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
529 .annotations().value(LOCAL_LSP_ID)))) != null) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530530 pcepUpdateTunnel(tunnel, path, pc);
531 }
cheng fan48e832c2015-05-29 01:54:47 +0800532 }
533
534 @Override
535 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530536 return handleTunnelAdded(tunnel, null);
537 }
538
539 public TunnelId tunnelAdded(TunnelDescription tunnel, State tunnelState) {
540 return handleTunnelAdded(tunnel, tunnelState);
541 }
542
543 private TunnelId handleTunnelAdded(TunnelDescription tunnel, State tunnelState) {
544
545 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700546 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530547
548 if (tunnelState == null) {
549 return service.tunnelAdded(tunnel);
550 } else {
551 return service.tunnelAdded(tunnel, tunnelState);
552 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530553 }
cheng fan48e832c2015-05-29 01:54:47 +0800554
Avantika-Huawei56c11842016-04-28 00:56:56 +0530555 long bandwidth = Long.parseLong(tunnel.annotations().value(BANDWIDTH));
cheng fan48e832c2015-05-29 01:54:47 +0800556
557 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800558 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800559 return null;
560 }
561
562 // endpoints
563 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
564 .src();
565 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
566 // devices
567 DeviceId srcId = (DeviceId) src.elementId().get();
568 DeviceId dstId = (DeviceId) dst.elementId().get();
569
570 // ports
571 long srcPort = src.portNumber().get().toLong();
572 long dstPort = dst.portNumber().get().toLong();
573
574 // type
575 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800576 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800577 return null;
578 }
579
580 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
581 dstPort, bandwidth,
582 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530583 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800584
585 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
586 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
587 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
588
589 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
590 return tunnelId;
591 }
592
Priyanka B4c3b4512016-07-22 11:41:49 +0530593 private void tunnelUpdated(Tunnel tunnel, Path path, State tunnelState) {
594 handleTunnelUpdate(tunnel, path, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +0530595 }
596
597 //Handles tunnel updated using tunnel admin service[specially to update annotations].
Priyanka B4c3b4512016-07-22 11:41:49 +0530598 private void handleTunnelUpdate(Tunnel tunnel, Path path, State tunnelState) {
Priyanka B413fbe82016-05-26 11:44:45 +0530599
600 if (tunnel.type() == MPLS) {
601 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.tunnelId());
602
Priyanka B4c3b4512016-07-22 11:41:49 +0530603 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
604 tunnel.type(), tunnel.groupId(), tunnel.providerId(),
605 tunnel.tunnelName(), path, tunnel.resource(),
606 (SparseAnnotations) tunnel.annotations());
Priyanka B413fbe82016-05-26 11:44:45 +0530607
Priyanka B4c3b4512016-07-22 11:41:49 +0530608 service.tunnelUpdated(td, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +0530609 return;
610 }
611
612 Tunnel tunnelOld = tunnelQueryById(tunnel.tunnelId());
613 if (tunnelOld.type() != Tunnel.Type.VLAN) {
614 error("Illegal tunnel type. Only support VLAN tunnel update.");
615 return;
616 }
617
618 long bandwidth = Long
619 .parseLong(tunnel.annotations().value("bandwidth"));
620 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
621 error("Update failed, invalid bandwidth.");
622 return;
623 }
624 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
625
626 checkNotNull(pcepTunnelId, "Invalid tunnel id");
627 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
628 error("Update failed,maybe invalid bandwidth.");
629 return;
630 }
631 tunnelAdminService.updateTunnel(tunnel, path);
632 }
633
cheng fan48e832c2015-05-29 01:54:47 +0800634 @Override
635 public void tunnelRemoved(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530636 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700637 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530638 service.tunnelRemoved(tunnel);
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530639 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530640 }
641
cheng fan48e832c2015-05-29 01:54:47 +0800642 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
643 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
644 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800645 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800646 return;
647 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700648 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800649 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800650 if (!controller.deleteTunnel(pcepTunnelId)) {
651 error("Delete tunnel failed, Maybe some devices have been disconnected.");
652 return;
cheng fan48e832c2015-05-29 01:54:47 +0800653 }
654 tunnelMap.remove(pcepTunnelId);
655 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800656 }
657
658 @Override
659 public void tunnelUpdated(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530660 handleTunnelUpdate(tunnel, null);
661 }
662
663 public void tunnelUpdated(TunnelDescription tunnel, State tunnelState) {
664 handleTunnelUpdate(tunnel, tunnelState);
665 }
666
667 private void handleTunnelUpdate(TunnelDescription tunnel, State tunnelState) {
668 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700669 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530670
671 if (tunnelState == null) {
672 service.tunnelUpdated(tunnel);
673 } else {
674 service.tunnelUpdated(tunnel, tunnelState);
675 }
676 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530677 }
cheng fan48e832c2015-05-29 01:54:47 +0800678
679 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
680 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800681 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800682 return;
683 }
cheng fan7716ec92015-05-31 01:53:19 +0800684 long bandwidth = Long
685 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800686 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800687 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800688 return;
689 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700690 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800691
692 checkNotNull(pcepTunnelId, "Invalid tunnel id");
693 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
694
cheng fan7716ec92015-05-31 01:53:19 +0800695 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800696 return;
697
698 }
699 service.tunnelUpdated(tunnel);
700 }
701
cheng fan7716ec92015-05-31 01:53:19 +0800702 private void error(String info) {
703 System.err.println(info);
704 }
705
cheng fan48e832c2015-05-29 01:54:47 +0800706 // Short-hand for creating a connection point.
707 private ConnectPoint connectPoint(PcepDpid id, long port) {
708 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
709 }
710
711 // Short-hand for creating a link.
712 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800713 return DefaultLink.builder()
714 .providerId(id())
715 .src(connectPoint(src, sp))
716 .dst(connectPoint(dst, dp))
717 .type(Link.Type.TUNNEL)
718 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800719 }
720
721 // Creates a path that leads through the given devices.
722 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700723 PathType pathtype, PathState pathState) {
Jon Hallcbd1b392017-01-18 20:15:44 -0800724 if (hopList == null || hopList.isEmpty()) {
cheng fan48e832c2015-05-29 01:54:47 +0800725 return null;
726 }
727 List<Link> links = new ArrayList<>();
728 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
729 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
730 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
731 .get(i + 1).getPortNum()));
732 }
733
734 int hopNum = hopList.size() - 2;
735 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
736 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800737 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800738 .set("pathType", String.valueOf(pathtype)).build();
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800739 return new DefaultPath(id(), links, ScalarWeight.toWeight(hopNum), extendAnnotations);
cheng fan48e832c2015-05-29 01:54:47 +0800740 }
741
742 // convert the path description to a string.
743 public String pathToString(List<Link> links) {
744 StringBuilder builder = new StringBuilder();
745 builder.append("{");
746 for (Link link : links) {
747 builder.append("(Device:" + link.src().deviceId() + " Port:"
748 + link.src().port().toLong());
749 builder.append(" Device:" + link.dst().deviceId() + " Port:"
750 + link.dst().port().toLong());
751 builder.append(")");
752 }
753 builder.append("}");
754 return builder.toString();
755 }
756
757 // build a TunnelDescription.
758 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
759 TunnelId tunnelId) {
760 TunnelEndPoint srcPoint = null;
761 TunnelEndPoint dstPoint = null;
762 Tunnel.Type tunnelType = null;
763 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
764
765 // add path after codes of tunnel's path merged
766 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800767 pcepTunnel.getPathType(),
768 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800769
770 OpticalTunnelEndPoint.Type endPointType = null;
771 switch (pcepTunnel.type()) {
772 case OCH:
773 tunnelType = Tunnel.Type.OCH;
774 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
775 break;
776
777 case OTN:
778 tunnelType = Tunnel.Type.ODUK;
779 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
780 break;
781
782 case UNI:
783 tunnelType = Tunnel.Type.VLAN;
784 endPointType = null;
785 break;
786
787 default:
788 break;
789 }
790 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
791 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
792 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
793 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
794
795 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
796 Optional.of(srcPort), null,
797 endPointType,
798 OpticalLogicId.logicId(0),
799 true);
800 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
801 Optional.of(dstPort), null,
802 endPointType,
803 OpticalLogicId.logicId(0),
804 true);
805
806 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800807 DefaultAnnotations annotations = DefaultAnnotations
808 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800809 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800810 .set("bandwidth",
811 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800812 .set("index", String.valueOf(pcepTunnel.id())).build();
813
cheng fan48e832c2015-05-29 01:54:47 +0800814 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
815 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800816 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800817 DefaultAnnotations extendAnnotations = DefaultAnnotations
818 .builder()
819 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800820 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800821 annotations = DefaultAnnotations.merge(annotations,
822 extendAnnotations);
823
824 }
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530825 TunnelDescription tunnel = new DefaultTunnelDescription(tunnelId,
cheng fan48e832c2015-05-29 01:54:47 +0800826 srcPoint,
827 dstPoint,
828 tunnelType,
Yi Tsengfa394de2017-02-01 11:26:40 -0800829 new GroupId(0),
cheng fan48e832c2015-05-29 01:54:47 +0800830 id(), name,
831 path,
832 annotations);
833 return tunnel;
cheng fan48e832c2015-05-29 01:54:47 +0800834 }
835
836 /**
837 * Get the tunnelID according to the tunnel key.
838 *
839 * @param tunnelKey tunnel key
840 * @return corresponding tunnel id of the a tunnel key.
841 */
842 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800843 for (String key : tunnelMap.keySet()) {
844 if (key.equals(tunnelKey)) {
845 return tunnelMap.get(key);
846 }
847 }
848 return null;
849 }
850
851 /**
852 * Get the tunnel key according to the tunnelID.
853 *
854 * @param tunnelId tunnel id
855 * @return corresponding a tunnel key of the tunnel id.
856 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700857 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800858 for (String key : tunnelMap.keySet()) {
Sho SHIMIZUc218bfa2016-08-18 14:22:50 -0700859 if (Objects.equals(tunnelMap.get(key).id(), tunnelId.id())) {
cheng fan48e832c2015-05-29 01:54:47 +0800860 return key;
861 }
862 }
863 return null;
864
865 }
866
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530867 /**
chengfan2fff70f2015-08-24 18:20:19 -0500868 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
869 *
870 * @param statistics statistics data from a PCEP tunnel
871 * @return TunnelStatistics
872 */
873 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
874 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
875 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
876 .setPacketLossRatio(statistics.packetLossRate())
877 .setFlowDelay(statistics.flowDelay())
878 .setAlarms(statistics.alarms())
879 .build();
880 return tunnelStatistics;
881 }
882 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530883 * Creates list of hops for ERO object from Path.
884 *
885 * @param path network path
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530886 * @return list of ERO subobjects
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530887 */
888 private LinkedList<PcepValueType> createPcepPath(Path path) {
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800889 LinkedList<PcepValueType> llSubObjects = new LinkedList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530890 List<Link> listLink = path.links();
891 ConnectPoint source = null;
892 ConnectPoint destination = null;
893 IpAddress ipDstAddress = null;
894 IpAddress ipSrcAddress = null;
895 PcepValueType subObj = null;
Priyanka Bcdf9b102016-06-07 20:01:38 +0530896 long portNo;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530897
898 for (Link link : listLink) {
899 source = link.src();
900 if (!(source.equals(destination))) {
901 //set IPv4SubObject for ERO object
Priyanka Bcdf9b102016-06-07 20:01:38 +0530902 portNo = source.port().toLong();
903 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
904 ipSrcAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530905 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
906 llSubObjects.add(subObj);
907 }
908
909 destination = link.dst();
Priyanka Bcdf9b102016-06-07 20:01:38 +0530910 portNo = destination.port().toLong();
911 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
912 ipDstAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530913 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
914 llSubObjects.add(subObj);
915 }
Priyanka Bcdf9b102016-06-07 20:01:38 +0530916
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530917 return llSubObjects;
918 }
919
920 /**
921 * Creates PcInitiated lsp request list for setup tunnel.
922 *
923 * @param tunnel mpls tunnel
924 * @param path network path
925 * @param pc pcep client
926 * @param srpId unique id for pcep message
927 * @return list of PcInitiatedLspRequest
928 * @throws PcepParseException while building pcep objects fails
929 */
930 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
931 PcepClient pc, int srpId)
932 throws PcepParseException {
933 PcepValueType tlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530934 LinkedList<PcepValueType> llSubObjects = null;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530935 LspType lspType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530936
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530937 if (lspType == SR_WITHOUT_SIGNALLING) {
938 NetworkResource labelStack = tunnel.resource();
939 if (labelStack == null || !(labelStack instanceof DefaultLabelStack)) {
940 labelStack = pcepClientController.computeLabelStack(tunnel.path());
941 if (labelStack == null) {
942 log.error("Unable to create label stack.");
943 return null;
944 }
945 }
946 llSubObjects = pcepClientController.createPcepLabelStack((DefaultLabelStack) labelStack, path);
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530947 } else {
948 llSubObjects = createPcepPath(path);
949 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530950
Jon Hallcbd1b392017-01-18 20:15:44 -0800951 if (llSubObjects == null || llSubObjects.isEmpty()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530952 log.error("There is no link information to create tunnel");
953 return null;
954 }
955
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800956 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530957
958 // set PathSetupTypeTlv of SRP object
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530959 tlv = new PathSetupTypeTlv(lspType.type());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530960 llOptionalTlv.add(tlv);
961
962 // build SRP object
963 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
964 .setOptionalTlv(llOptionalTlv).build();
965
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800966 llOptionalTlv = new LinkedList<>();
967 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530968
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530969 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530970 short localLspId = 0;
971 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
972 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
973 if (localLspIdString != null) {
974 localLspId = Short.valueOf(localLspIdString);
975 }
976 }
977
Priyanka B4c3cef02016-06-14 20:27:53 +0530978 tunnel.annotations().value(LSP_SIG_TYPE);
Avantika-Huawei56c11842016-04-28 00:56:56 +0530979 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
980 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
981 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530982 llOptionalTlv.add(tlv);
983 //set SymbolicPathNameTlv of LSP object
984 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
985 llOptionalTlv.add(tlv);
986
987 //build LSP object
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530988 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setDFlag(true).setOFlag((byte) 0)
989 .setPlspId(0).setOptionalTlv(llOptionalTlv).build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530990
991 //build ENDPOINTS object
992 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
993 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
994 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
995 .setPFlag(true).build();
996
997 //build ERO object
998 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
999
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301000 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301001 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301002 iBandwidth = Float.valueOf(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301003 }
1004 // build bandwidth object
1005 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1006 // build pcep attribute
1007 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1008
1009 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1010 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
1011 .setPcepAttribute(pcepAttribute).build();
1012 llPcInitiatedLspRequestList.add(initiateLspRequest);
1013 return llPcInitiatedLspRequestList;
1014 }
1015
1016 /**
1017 * To send initiate tunnel message to pcc.
1018 *
1019 * @param tunnel mpls tunnel info
1020 * @param path explicit route for the tunnel
1021 * @param pc pcep client to send message
1022 */
1023 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1024 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301025 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1026 log.error("Setup tunnel has failed as LSP DB sync is not finished");
1027 return;
1028 }
1029
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301030 int srpId = SrpIdGenerators.create();
Priyanka B4c3cef02016-06-14 20:27:53 +05301031 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
1032 for (Tunnel t : tunnels) {
1033 if (t.tunnelName().equals(tunnel.tunnelName())) {
1034 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
1035 tunnel.dst(), tunnel.type(),
1036 t.state(), tunnel.groupId(),
1037 t.tunnelId(),
1038 tunnel.tunnelName(),
1039 tunnel.path(),
1040 tunnel.resource(),
1041 tunnel.annotations());
Priyanka B4c3b4512016-07-22 11:41:49 +05301042 break;
Priyanka B4c3cef02016-06-14 20:27:53 +05301043 }
1044 }
1045
1046 if (tunnel.tunnelId() == null) {
1047 log.error("Tunnel ID not found");
1048 return;
1049 }
1050
Avantika-Huawei56c11842016-04-28 00:56:56 +05301051 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301052
Jonathan Hart51539b82015-10-29 09:53:04 -07001053 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301054
1055 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
1056 pc, srpId);
Jon Hallcbd1b392017-01-18 20:15:44 -08001057 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.isEmpty()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301058 log.error("Failed to create PcInitiatedLspRequestList");
1059 return;
1060 }
1061
1062 //build PCInitiate message
1063 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1064 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
1065 .build();
1066
1067 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1068
Jonathan Hart51539b82015-10-29 09:53:04 -07001069 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301070 } catch (PcepParseException e) {
1071 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
1072 }
1073 }
1074
1075 /**
1076 * To send Release tunnel message to pcc.
1077 *
1078 * @param tunnel mpls tunnel info
1079 * @param pc pcep client to send message
1080 */
1081 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
1082 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301083 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1084 log.error("Release tunnel has failed as LSP DB sync is not finished");
1085 return;
1086 }
1087
Avantika-Huawei56c11842016-04-28 00:56:56 +05301088 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001089 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301090 int srpId = SrpIdGenerators.create();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301091
1092 PcepValueType tlv;
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001093 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301094
1095 // set PathSetupTypeTlv of SRP object
1096 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
1097 .type());
1098 llOptionalTlv.add(tlv);
1099
1100 // build SRP object
1101 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
1102 .setOptionalTlv(llOptionalTlv).build();
1103
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001104 llOptionalTlv = new LinkedList<>();
1105 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301106
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301107 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1108 llOptionalTlv.add(tlv);
Priyanka B4c3cef02016-06-14 20:27:53 +05301109
1110 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1111 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301112 String pLspIdString = tunnel.annotations().value(PLSP_ID);
1113
Priyanka B4c3cef02016-06-14 20:27:53 +05301114 short localLspId = 0;
1115 short pccTunnelId = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301116 int plspId = 0;
Priyanka B4c3cef02016-06-14 20:27:53 +05301117
1118 if (localLspIdString != null) {
1119 localLspId = Short.valueOf(localLspIdString);
1120 }
1121
1122 if (pccTunnelIdString != null) {
1123 pccTunnelId = Short.valueOf(pccTunnelIdString);
1124 }
1125
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301126 if (pLspIdString != null) {
1127 plspId = Integer.valueOf(pLspIdString);
1128 }
1129
Priyanka B4c3cef02016-06-14 20:27:53 +05301130 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1131 .ip().getIp4Address().toInt()),
1132 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1133 .getIp4Address().toInt()));
1134 llOptionalTlv.add(tlv);
1135
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301136 // build lsp object, set r flag as false to delete the tunnel
1137 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
1138 .setOptionalTlv(llOptionalTlv).build();
1139
1140 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1141 .setLspObject(lspobj).build();
1142
1143 llPcInitiatedLspRequestList.add(releaseLspRequest);
1144
1145 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1146 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1147
1148 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1149
Jonathan Hart51539b82015-10-29 09:53:04 -07001150 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301151 } catch (PcepParseException e) {
1152 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1153 }
1154 }
1155
1156 /**
1157 * To send Update tunnel request message to pcc.
1158 *
1159 * @param tunnel mpls tunnel info
1160 * @param path explicit route for the tunnel
1161 * @param pc pcep client to send message
1162 */
1163 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1164 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301165 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001166 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301167 int srpId = SrpIdGenerators.create();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301168 PcepValueType tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301169
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301170 LinkedList<PcepValueType> llSubObjects = null;
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301171 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
1172
1173 if (lspSigType == SR_WITHOUT_SIGNALLING) {
1174 NetworkResource labelStack = tunnel.resource();
1175 if (labelStack == null || !(labelStack instanceof DefaultLabelStack)) {
1176 labelStack = pcepClientController.computeLabelStack(tunnel.path());
1177 if (labelStack == null) {
1178 log.error("Unable to create label stack.");
1179 return;
1180 }
1181 }
1182 llSubObjects = pcepClientController.createPcepLabelStack((DefaultLabelStack) labelStack, path);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301183 } else {
1184 llSubObjects = createPcepPath(path);
1185 }
1186
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001187 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<>();
1188 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301189
Avantika-Huawei56c11842016-04-28 00:56:56 +05301190 // set PathSetupTypeTlv of SRP object
Avantika-Huawei56c11842016-04-28 00:56:56 +05301191 tlv = new PathSetupTypeTlv(lspSigType.type());
1192 llOptionalTlv.add(tlv);
1193
1194 // build SRP object
1195 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
1196 .setOptionalTlv(llOptionalTlv).build();
1197
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001198 llOptionalTlv = new LinkedList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301199
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301200 // Lsp Identifier tlv is required for all modes of lsp
1201 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1202 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1203 short localLspId = 0;
1204 short pccTunnelId = 0;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301205
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301206 if (localLspIdString != null) {
1207 localLspId = Short.valueOf(localLspIdString);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301208 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301209
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301210 if (pccTunnelIdString != null) {
1211 pccTunnelId = Short.valueOf(pccTunnelIdString);
1212 }
1213
1214 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1215 .ip().getIp4Address().toInt()),
1216 localLspId, pccTunnelId,
1217 ((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt(),
1218 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
1219 llOptionalTlv.add(tlv);
1220
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301221 if (tunnel.tunnelName().value() != null) {
1222 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1223 llOptionalTlv.add(tlv);
1224 }
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301225 boolean delegated = (tunnel.annotations().value(DELEGATE) == null) ? false
1226 : Boolean.valueOf(tunnel.annotations()
1227 .value(DELEGATE));
1228 boolean initiated = (tunnel.annotations().value(PCE_INIT) == null) ? false
1229 : Boolean.valueOf(tunnel.annotations()
1230 .value(PCE_INIT));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301231 // build lsp object
Priyanka B4c3cef02016-06-14 20:27:53 +05301232 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true)
1233 .setPlspId(Integer.valueOf(tunnel.annotations().value(PLSP_ID)))
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301234 .setDFlag(delegated)
1235 .setCFlag(initiated)
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301236 .setOptionalTlv(llOptionalTlv).build();
1237 // build ero object
1238 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
Priyanka B4c3cef02016-06-14 20:27:53 +05301239 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301240 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301241 iBandwidth = Float.parseFloat(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301242 }
1243 // build bandwidth object
1244 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1245 // build pcep attribute
1246 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1247 // build pcep msg path
1248 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1249 .build();
1250
1251 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1252 .setLspObject(lspobj).setMsgPath(msgPath).build();
1253
1254 llUpdateRequestList.add(updateRequest);
1255
1256 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1257
1258 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001259 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301260 } catch (PcepParseException e) {
1261 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1262 }
1263 }
1264
1265 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001266
1267 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001268 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001269 TunnelDescription tunnel = null;
1270 // instance and id identify a tunnel together
1271 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1272 + String.valueOf(pcepTunnel.id());
1273
1274 if (tunnelKey == null || "".equals(tunnelKey)) {
1275 log.error("Invalid PCEP tunnel");
1276 return;
1277 }
1278
1279 TunnelId tunnelId = getTunnelId(tunnelKey);
cheng fan48e832c2015-05-29 01:54:47 +08001280 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1281
1282 OperationType operType = pcepTunnel.getOperationType();
1283 switch (operType) {
1284 case ADD:
1285 tunnelId = service.tunnelAdded(tunnel);
1286 tunnelMap.put(tunnelKey, tunnelId);
1287 break;
1288
1289 case UPDATE:
1290 service.tunnelUpdated(tunnel);
1291 break;
1292
1293 case DELETE:
1294 service.tunnelRemoved(tunnel);
1295 tunnelMap.remove(tunnelKey);
1296 break;
1297
1298 default:
1299 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301300 }
1301 }
cheng fan48e832c2015-05-29 01:54:47 +08001302
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301303 @Override
1304 public void handleMessage(PccId pccId, PcepMessage msg) {
1305 try {
1306 log.debug("tunnel provider handle message {}", msg.getType().toString());
1307 switch (msg.getType()) {
1308 case REPORT:
1309 int srpId = 0;
1310 LinkedList<PcepStateReport> llStateReportList = null;
1311 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1312 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1313 PcepSrpObject srpObj = null;
1314 PcepLspObject lspObj = null;
1315 while (listIterator.hasNext()) {
1316 PcepStateReport stateRpt = listIterator.next();
1317 srpObj = stateRpt.getSrpObject();
1318 lspObj = stateRpt.getLspObject();
1319
Ray Milkeyef310052018-02-06 08:58:51 -08001320 if (srpObj != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301321 srpId = srpObj.getSrpID();
1322 }
1323
1324 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1325 log.debug("SRP ID in handle message " + srpId);
1326
Jonathan Hart51539b82015-10-29 09:53:04 -07001327 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301328 // For PCRpt without matching SRP id.
1329 handleRptWithoutSrpId(stateRpt, pccId);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301330 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301331 }
1332
Avantika-Huawei56c11842016-04-28 00:56:56 +05301333 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301334 }
1335 break;
1336
1337 default:
1338 log.debug("Received unsupported message type {}", msg.getType().toString());
1339 }
1340 } catch (Exception e) {
Frank Wangd8ab0962017-08-11 11:09:30 +08001341 log.error("Exception occurred while processing report message {}", e.getMessage());
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301342 }
1343 }
1344
1345 /**
1346 * Handles report message for setup/update/delete tunnel request.
1347 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301348 * @param srpId unique identifier for PCEP message
1349 * @param lspObj LSP object
1350 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301351 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301352 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301353 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001354 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301355
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301356 // store the values required from report message
1357 pcepTunnelData.setPlspId(lspObj.getPlspId());
1358 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1359 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1360 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1361
Avantika-Huawei56c11842016-04-28 00:56:56 +05301362 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301363 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1364 while (listTlvIterator.hasNext()) {
1365 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301366 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1367 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301368 break;
1369 }
1370 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001371 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301372 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001373 }
1374
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301375 Path path = pcepTunnelData.path();
1376 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301377 Builder annotationBuilder = DefaultAnnotations.builder();
1378 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1379
1380 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1381 if (tunnel.annotations().value(PLSP_ID) == null) {
1382 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1383 }
1384
1385 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1386 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1387 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1388 }
1389
Priyanka B4c3cef02016-06-14 20:27:53 +05301390 if (tunnel.annotations().value(PCC_TUNNEL_ID) == null) {
1391 annotationBuilder.set(PCC_TUNNEL_ID, String.valueOf(ipv4LspTlv.getTunnelId()));
1392 }
1393
Avantika-Huawei56c11842016-04-28 00:56:56 +05301394 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301395 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1396 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1397 providerId, tunnel.tunnelName(), path,
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301398 tunnel.resource(), annotations);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301399
Avantika-Huawei56c11842016-04-28 00:56:56 +05301400 if (CREATE == pcepTunnelData.requestType()) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301401 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Jonathan Hart51539b82015-10-29 09:53:04 -07001402 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301403 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001404 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301405 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301406 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001407 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1408 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301409 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301410
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301411 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1412
Avantika-Huawei56c11842016-04-28 00:56:56 +05301413 if (lspObj.getRFlag()) {
1414 tunnelRemoved(td);
1415 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301416 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301417 tunnelUpdated(td, tunnelState);
1418 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301419
1420 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1421 // though there are no labels to download for SR-TE.
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301422 if (((pcepLspStatus == PcepLspStatus.GOING_UP)
1423 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING))
1424 // For PCInit tunnel up, few PCC expects PCUpd message after PCInit message,
1425 || ((tunnel.state() == State.INIT)
1426 && (pcepLspStatus == PcepLspStatus.DOWN)
1427 && (tunnel.annotations().value(PCE_INIT) != null
1428 && tunnel.annotations().value(PCE_INIT).equals("true"))
1429 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == WITH_SIGNALLING))) {
1430
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301431 // Query again to get latest tunnel updated with protocol values from PCRpt msg.
1432 updateTunnel(service.tunnelQueryById(tunnel.tunnelId()), tunnel.path());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301433 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301434 }
1435
Priyanka B413fbe82016-05-26 11:44:45 +05301436 private SparseAnnotations getAnnotations(PcepLspObject lspObj, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv,
Priyanka B4c3b4512016-07-22 11:41:49 +05301437 String bandwidth, LspType lspType, String costType, boolean isPceInit) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301438 Builder builder = DefaultAnnotations.builder();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301439 /*
1440 * [RFC 5440] The absence of the METRIC object MUST be interpreted by the PCE as a path computation request
1441 * for which no constraints need be applied to any of the metrics.
1442 */
1443 if (costType != null) {
1444 builder.set(COST_TYPE, costType);
1445 }
1446
Priyanka B4c3b4512016-07-22 11:41:49 +05301447 if (isPceInit) {
1448 builder.set(PCE_INIT, String.valueOf(isPceInit));
1449 }
1450
1451 if (bandwidth != null) {
1452 builder.set(BANDWIDTH, bandwidth);
1453 }
1454
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301455 SparseAnnotations annotations = builder
Priyanka B4c3b4512016-07-22 11:41:49 +05301456 .set(LSP_SIG_TYPE, lspType.name())
Priyanka B413fbe82016-05-26 11:44:45 +05301457 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1458 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1459 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId()))
1460 .set(DELEGATE, String.valueOf(lspObj.getDFlag()))
1461 .build();
1462 return annotations;
1463 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301464
Priyanka B413fbe82016-05-26 11:44:45 +05301465 private LspType getLspType(PcepSrpObject srpObj) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301466 LspType lspType = WITH_SIGNALLING;
1467
1468 if (null != srpObj) {
1469 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1470 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1471
1472 while (listIterator.hasNext()) {
1473 PcepValueType tlv = listIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301474 switch (tlv.getType()) {
1475 case PathSetupTypeTlv.TYPE:
1476 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1477 break;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301478 default:
1479 break;
1480 }
1481 }
1482 }
Priyanka B413fbe82016-05-26 11:44:45 +05301483 return lspType;
1484 }
1485
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301486 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId) {
Priyanka B413fbe82016-05-26 11:44:45 +05301487 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301488 String costType = null;
Priyanka B413fbe82016-05-26 11:44:45 +05301489 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1490 checkNotNull(msgPath);
1491 PcepEroObject eroObj = msgPath.getEroObject();
1492 if (eroObj == null) {
1493 log.error("ERO object is null in report message.");
1494 return;
1495 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301496 PcepAttribute attributes = msgPath.getPcepAttribute();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301497 float bandwidth = 0;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301498 int cost = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301499 if (attributes != null) {
1500 if (attributes.getMetricObjectList() != null) {
1501 ListIterator<PcepMetricObject> iterator = attributes.getMetricObjectList().listIterator();
1502 PcepMetricObject metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301503
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301504 while (metricObj != null) {
1505 if (metricObj.getBType() == IGP_METRIC) {
1506 costType = "COST";
1507 } else if (metricObj.getBType() == TE_METRIC) {
1508 costType = "TE_COST";
1509 }
1510 if (costType != null) {
1511 cost = metricObj.getMetricVal();
1512 log.debug("Path cost {}", cost);
1513 break;
1514 }
1515 metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301516 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301517 }
1518 if (attributes.getBandwidthObject() != null) {
1519 bandwidth = attributes.getBandwidthObject().getBandwidth();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301520 }
1521 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301522 PcepLspObject lspObj = stateRpt.getLspObject();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301523 List<Object> eroSubObjList = buildPathFromEroObj(eroObj, providerId);
1524 List<Link> links = new ArrayList<>();
1525 List<LabelResourceId> labels = new ArrayList<>();
1526 for (Object linkOrLabel : eroSubObjList) {
1527 if (linkOrLabel instanceof Link) {
1528 links.add((Link) linkOrLabel);
1529 } else if (linkOrLabel instanceof Integer) {
1530 labels.add(LabelResourceId.labelResourceId(((Integer) linkOrLabel).longValue()));
1531 }
Priyanka B413fbe82016-05-26 11:44:45 +05301532 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301533 Path path = null;
1534 if (!links.isEmpty()) {
Ray Milkeya7cf8c82018-02-08 15:07:06 -08001535 path = new DefaultPath(providerId, links, ScalarWeight.toWeight(cost), EMPTY);
Priyanka B4c3b4512016-07-22 11:41:49 +05301536 } else if (!lspObj.getRFlag()) {
1537 return;
1538 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301539 NetworkResource labelStack = new DefaultLabelStack(labels);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301540 // To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1541 // from PCE.
Priyanka B413fbe82016-05-26 11:44:45 +05301542 PcepSrpObject srpObj = stateRpt.getSrpObject();
1543 LspType lspType = getLspType(srpObj);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301544 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1545 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1546 SymbolicPathNameTlv pathNameTlv = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301547 while (listTlvIterator.hasNext()) {
1548 PcepValueType tlv = listTlvIterator.next();
1549 switch (tlv.getType()) {
1550 case StatefulIPv4LspIdentifiersTlv.TYPE:
1551 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1552 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301553 case SymbolicPathNameTlv.TYPE:
1554 pathNameTlv = (SymbolicPathNameTlv) tlv;
1555 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301556 default:
1557 break;
1558 }
1559 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301560 /*
1561 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1562 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1563 */
1564 if (ipv4LspIdenTlv == null) {
1565 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1566 return;
1567 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301568 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1569 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1570 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1571 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1572 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
Priyanka B413fbe82016-05-26 11:44:45 +05301573 // Store delegation flag info and that LSP info because only delegated PCE sends update message
1574 // Storing if D flag is set, if not dont store. while checking whether delegation if annotation for D flag
1575 // not present then non-delegated , if present it is delegated.
1576 if (lspObj.getDFlag()) {
1577 pcepClientController.getClient(pccId).setLspAndDelegationInfo(
1578 new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()), lspObj.getDFlag());
1579 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301580 Tunnel tunnel = null;
Priyanka B4c3b4512016-07-22 11:41:49 +05301581 SparseAnnotations oldTunnelAnnotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301582 // Asynchronous status change message from PCC for LSP reported earlier.
1583 for (Tunnel tunnelObj : tunnelQueryResult) {
1584 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1585 /*
1586 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1587 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1588 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1589 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1590 * session an LSP is "first" reported to a PCE.
1591 */
1592 if ((pathNameTlv != null)
1593 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1594 tunnel = tunnelObj;
1595 break;
1596 }
1597 continue;
1598 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301599 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId())) {
1600 if ((Integer
1601 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1602 tunnel = tunnelObj;
1603 }
1604 if ((Integer
1605 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) != ipv4LspIdenTlv.getLspId())) {
1606 oldTunnelAnnotations = (SparseAnnotations) tunnelObj.annotations();
1607 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301608 }
1609 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301610 DefaultTunnelDescription td;
Priyanka B413fbe82016-05-26 11:44:45 +05301611 SparseAnnotations annotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301612 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
Ray Milkey74e59132018-01-17 15:24:52 -08001613 if (tunnel == null && pathNameTlv != null) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301614 if (lspObj.getRFlag()) {
1615 /*
1616 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1617 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1618 */
1619 return;
1620 }
Priyanka B413fbe82016-05-26 11:44:45 +05301621 DeviceId deviceId = getDevice(pccId);
1622 if (deviceId == null) {
1623 log.error("Ingress deviceId not found");
1624 return;
1625 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301626 String tempBandwidth = null;
1627 String temoCostType = null;
1628 if (oldTunnelAnnotations != null) {
1629 tempBandwidth = oldTunnelAnnotations.value(BANDWIDTH);
1630 temoCostType = oldTunnelAnnotations.value(COST_TYPE);
1631 }
1632 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, tempBandwidth, lspType,
1633 temoCostType, lspObj.getCFlag());
Yi Tsengfa394de2017-02-01 11:26:40 -08001634 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new GroupId(
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301635 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path, labelStack,
Priyanka B413fbe82016-05-26 11:44:45 +05301636 annotations);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301637 // Do not support PCC initiated LSP after LSP DB sync is completed.
1638 if (!lspObj.getSFlag() && !lspObj.getCFlag()) {
1639 log.error("Received PCC initiated LSP while not in sync.");
1640 return;
1641 }
Priyanka B413fbe82016-05-26 11:44:45 +05301642 /*
1643 * If ONOS instance is master for PCC then set delegated flag as annotation and add the tunnel to store.
1644 * Because all LSPs need not be delegated, hence mastership for the PCC is confirmed whereas not the
1645 * delegation set to all LSPs.If ONOS is not the master for that PCC then check if D flag is set, if yes
1646 * wait for 2 seconds [while master has added the tunnel to the store] then update the tunnel. Tunnel is
1647 * updated because in case of resilency only delegated LSPs are recomputed and only delegated PCE can
1648 * send update message to that client.
1649 *
1650 * 1)Master can 1st get the Rpt message
1651 * a)Master adds the tunnel into core.
1652 * b)If a non-master for ingress gets Rpt message with D flag set[as delegation owner]
1653 * after master, then runs timer then update the tunnel with D flag set.
1654 * 2)Non-Master can 1st get the Rpt message
1655 * a)Non-Master runs the timer check for the tunnel then updates the tunnel with D flag set
1656 * b)Master would have got the message while the non-master running timer, hence master adds
1657 * tunnel to core
1658 *
1659 * In general always master adds the tunnel to the core
1660 * while delegated owner [master or non-master with D flag set] always updates the tunnel running timer
1661 */
1662 if (mastershipService.isLocalMaster(deviceId)) {
1663 TunnelId tId = tunnelAdded(td, tunnelState);
1664 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001665 tunnelState, new GroupId(0), tId, TunnelName.tunnelName(Arrays.toString(pathNameTlv
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301666 .getValue())), path, labelStack, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301667
Priyanka B413fbe82016-05-26 11:44:45 +05301668 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1669 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1670 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1671 } else if (!mastershipService.isLocalMaster(deviceId) && lspObj.getDFlag()) {
1672 //Start timer then update the tunnel with D flag
Priyanka B4c3b4512016-07-22 11:41:49 +05301673 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId, tunnelState, ipv4LspIdenTlv);
Priyanka B413fbe82016-05-26 11:44:45 +05301674 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301675 return;
1676 }
Priyanka B413fbe82016-05-26 11:44:45 +05301677 //delegated owner will update can be a master or non-master
Priyanka B4c3b4512016-07-22 11:41:49 +05301678 if (lspObj.getDFlag() && !lspObj.getRFlag()) {
1679 tunnelUpdateForDelegatedLsp(tunnel, lspObj,
1680 lspType, tunnelState, pccId, labelStack, ipv4LspIdenTlv);
1681 return;
Priyanka B413fbe82016-05-26 11:44:45 +05301682 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301683 removeOrUpdatetunnel(tunnel, lspObj, providerId, tunnelState, ipv4LspIdenTlv);
Priyanka B413fbe82016-05-26 11:44:45 +05301684 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301685
Priyanka B4c3b4512016-07-22 11:41:49 +05301686 private void tunnelUpdateForDelegatedLsp(Tunnel tunnel, PcepLspObject lspObj,
1687 LspType lspType, State tunnelState, PccId pccId,
1688 NetworkResource labelStack,
1689 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv) {
1690 SparseAnnotations annotations = null;
1691 DefaultTunnelDescription td;
1692 boolean isPceInit = tunnel.annotations().value(PCE_INIT) == null ? false :
1693 Boolean.valueOf((tunnel.annotations().value(PCE_INIT))).booleanValue();
1694 annotations = getAnnotations(lspObj, ipv4LspIdenTlv,
1695 tunnel.annotations().value(BANDWIDTH), lspType,
1696 tunnel.annotations().value(COST_TYPE), isPceInit);
Yi Tsengfa394de2017-02-01 11:26:40 -08001697 td = new DefaultTunnelDescription(null, tunnel.src(), tunnel.dst(), MPLS, new GroupId(
Priyanka B4c3b4512016-07-22 11:41:49 +05301698 0), tunnel.providerId(), tunnel.tunnelName(),
1699 tunnel.path(), labelStack, annotations);
1700 tunnelUpdateInDelegatedCase(pccId, annotations, td, tunnel.providerId(), tunnelState, ipv4LspIdenTlv);
1701 }
1702
1703 private void removeOrUpdatetunnel(Tunnel tunnel, PcepLspObject lspObj, ProviderId providerId,
1704 State tunnelState, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv) {
Priyanka B413fbe82016-05-26 11:44:45 +05301705 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1706 tunnel.type(), tunnel.groupId(), providerId, tunnel.tunnelName(), tunnel.path(),
1707 (SparseAnnotations) tunnel.annotations());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301708 if (lspObj.getRFlag()) {
1709 tunnelRemoved(td);
1710 } else {
Priyanka B4c3b4512016-07-22 11:41:49 +05301711 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, tunnel.path(), LSP_STATE_RPT);
1712 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1713 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301714 tunnelUpdated(td, tunnelState);
1715 }
Priyanka B413fbe82016-05-26 11:44:45 +05301716 }
1717
1718 private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
Priyanka B4c3b4512016-07-22 11:41:49 +05301719 DefaultTunnelDescription td, ProviderId providerId, State tunnelState,
1720 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv) {
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301721 // Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
Priyanka B413fbe82016-05-26 11:44:45 +05301722
1723 /*
1724 * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
1725 * master has added the tunnel to the store] then update the tunnel.
1726 */
1727 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1728
1729 // Thread is started after 2 seconds first time later periodically after 2 seconds to update the tunnel
1730 executor.scheduleAtFixedRate(new UpdateDelegation(td, providerId, annotations, pccId,
Priyanka B4c3b4512016-07-22 11:41:49 +05301731 executor, tunnelState, ipv4LspIdentifiersTlv), DELAY, DELAY, TimeUnit.SECONDS);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301732 }
1733
1734 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301735 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301736 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301737 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301738 * @param providerId provider id
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301739 * @return list of links and labels
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301740 */
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301741 private List<Object> buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301742 checkNotNull(eroObj);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301743 List<Object> subObjList = new ArrayList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301744 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Jon Hallcbd1b392017-01-18 20:15:44 -08001745 if (llSubObj.isEmpty()) {
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301746 log.error("ERO in report message does not have hop information");
Priyanka B4c3b4512016-07-22 11:41:49 +05301747 return new ArrayList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301748 }
1749 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1750
1751 ConnectPoint src = null;
1752 ConnectPoint dst = null;
1753 boolean isSrcSet = false;
1754 while (tlvIterator.hasNext()) {
1755 PcepValueType subObj = tlvIterator.next();
1756 switch (subObj.getType()) {
1757
1758 case IPv4SubObject.TYPE:
1759
1760 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1761 if (!isSrcSet) {
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301762 Iterable<Link> links = linkService.getActiveLinks();
1763 for (Link l : links) {
1764 if (l.src().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1765 src = l.src();
1766 isSrcSet = true;
1767 break;
1768 } else if (l.dst().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1769 src = l.dst();
1770 isSrcSet = true;
1771 break;
1772 }
1773 }
1774 } else {
1775 Iterable<Link> links = linkService.getActiveLinks();
1776 for (Link l : links) {
1777 if (l.src().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1778 dst = l.src();
1779 break;
1780 } else if (l.dst().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1781 dst = l.dst();
1782 break;
1783 }
1784 }
Ray Milkey2693bda2016-01-22 16:08:14 -08001785 Link link = DefaultLink.builder()
1786 .providerId(providerId)
1787 .src(src)
1788 .dst(dst)
1789 .type(Link.Type.DIRECT)
1790 .build();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301791 subObjList.add(link);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301792 src = dst;
1793 }
1794 break;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301795 case SrEroSubObject.TYPE:
1796 SrEroSubObject srEroSubObj = (SrEroSubObject) subObj;
1797 subObjList.add(srEroSubObj.getSid());
1798
1799 if (srEroSubObj.getSt() == PcepNaiIpv4Adjacency.ST_TYPE) {
1800 PcepNaiIpv4Adjacency nai = (PcepNaiIpv4Adjacency) (srEroSubObj.getNai());
Priyanka B4c3b4512016-07-22 11:41:49 +05301801 int srcIp = nai.getLocalIpv4Addr();
1802 int dstIp = nai.getRemoteIpv4Addr();
1803 Iterable<Link> links = linkService.getActiveLinks();
1804 for (Link l : links) {
1805 long lSrc = l.src().port().toLong();
1806 long lDst = l.dst().port().toLong();
1807 if (lSrc == srcIp) {
1808 src = l.src();
1809 } else if (lDst == srcIp) {
1810 src = l.dst();
1811 }
1812 if (lSrc == dstIp) {
1813 dst = l.src();
1814 } else if (lDst == dstIp) {
1815 dst = l.dst();
1816 }
1817 if (src != null && dst != null) {
1818 break;
1819 }
1820 }
1821 if (src == null || dst == null) {
1822 return new ArrayList<>();
1823 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301824 Link link = DefaultLink.builder()
1825 .providerId(providerId)
1826 .src(src)
1827 .dst(dst)
1828 .type(Link.Type.DIRECT)
1829 .build();
1830 subObjList.add(link);
1831 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301832 default:
1833 // the other sub objects are not required
1834 }
1835 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301836 return subObjList;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301837 }
1838
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301839 @Override
1840 public void clientConnected(PccId pccId) {
1841 // TODO
1842 }
1843
1844 @Override
1845 public void clientDisconnected(PccId pccId) {
1846 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001847 }
chengfan2fff70f2015-08-24 18:20:19 -05001848
chengfan2fff70f2015-08-24 18:20:19 -05001849 @Override
1850 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1851 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1852 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1853 tunnelStatisticsMap.put(id, tunnelStatistics);
1854 }
cheng fan48e832c2015-05-29 01:54:47 +08001855
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301856 @Override
1857 public void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301858
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301859 if (endOfSyncAction == SEND_UPDATE) {
1860 updateTunnel(tunnel, tunnel.path());
1861 return;
1862 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301863
1864 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1865 tunnel.src(), tunnel.dst(),
1866 tunnel.type(),
1867 tunnel.groupId(),
1868 tunnel.providerId(),
1869 tunnel.tunnelName(),
1870 tunnel.path(),
1871 (SparseAnnotations) tunnel.annotations());
1872
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301873
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301874 if (endOfSyncAction == PcepLspSyncAction.UNSTABLE) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301875 // Send PCInit msg again after global reoptimization.
1876 tunnelUpdated(td, UNSTABLE);
1877
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301878 // To remove the old tunnel from store whose PLSPID is not recognized by ingress PCC.
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301879 tunnelRemoved(td);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301880 } else if (endOfSyncAction == REMOVE) {
1881 tunnelRemoved(td);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301882 }
1883 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301884 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301885 @Override
1886 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1887 return service.tunnelQueryById(tunnelId);
1888 }
1889
Priyanka B413fbe82016-05-26 11:44:45 +05301890 private DeviceId getDevice(PccId pccId) {
1891 // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID.
1892 IpAddress lsrId = pccId.ipAddress();
1893 String lsrIdentifier = String.valueOf(lsrId);
1894
1895 // Find PCC deviceID from lsrId stored as annotations
1896 Iterable<Device> devices = deviceService.getAvailableDevices();
1897 for (Device dev : devices) {
1898 if (dev.annotations().value(AnnotationKeys.TYPE).equals("L3")
1899 && dev.annotations().value(LSRID).equals(lsrIdentifier)) {
1900 return dev.id();
1901 }
1902 }
1903 return null;
1904 }
1905
1906 /**
1907 * Updates the tunnel with updated tunnel annotation after a delay of two seconds and checks it till
1908 * tunnel is found.
1909 */
1910 private class UpdateDelegation implements Runnable {
1911 DefaultTunnelDescription td;
1912 ProviderId providerId;
1913 SparseAnnotations annotations;
1914 PccId pccId;
1915 ScheduledExecutorService executor;
Priyanka B4c3b4512016-07-22 11:41:49 +05301916 State tunnelState;
1917 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv;
Priyanka B413fbe82016-05-26 11:44:45 +05301918
1919 /**
1920 * Creates an instance of UpdateDelegation.
1921 *
1922 * @param td tunnel description
1923 * @param providerId provider id
1924 * @param annotations tunnel annotations
1925 * @param pccId PCEP client id
1926 * @param executor service of delegated owner
1927 */
1928 public UpdateDelegation(DefaultTunnelDescription td, ProviderId providerId, SparseAnnotations annotations,
Priyanka B4c3b4512016-07-22 11:41:49 +05301929 PccId pccId, ScheduledExecutorService executor, State tunnelState,
1930 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv) {
Priyanka B413fbe82016-05-26 11:44:45 +05301931 this.td = td;
1932 this.providerId = providerId;
1933 this.annotations = annotations;
1934 this.pccId = pccId;
1935 this.executor = executor;
Priyanka B4c3b4512016-07-22 11:41:49 +05301936 this.tunnelState = tunnelState;
1937 this.ipv4LspIdentifiersTlv = ipv4LspIdentifiersTlv;
Priyanka B413fbe82016-05-26 11:44:45 +05301938 }
1939
1940 //Temporary using annotations later will use projection/network config service
1941 @Override
1942 public void run() {
1943 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(td.src(), td.dst());
1944 TunnelId tempTunnelId = null;
1945 for (Tunnel t : tunnelQueryResult) {
1946 if (t.annotations().value(LOCAL_LSP_ID) == null || t.annotations().value(PLSP_ID) == null) {
1947 continue;
1948 }
1949
1950 if (t.annotations().value(LOCAL_LSP_ID).equals(td.annotations().value(LOCAL_LSP_ID))
1951 && t.annotations().value(PLSP_ID).equals(td.annotations().value(PLSP_ID))
1952 && ((IpTunnelEndPoint) t.src()).ip().equals(pccId.id())) {
1953 tempTunnelId = t.tunnelId();
1954 break;
1955 }
1956 }
1957
1958 //If tunnel is found update the tunnel and shutdown the thread otherwise thread will be executing
1959 //periodically
1960 if (tempTunnelId != null) {
Yi Tsengfa394de2017-02-01 11:26:40 -08001961 Tunnel tunnel = new DefaultTunnel(providerId, td.src(), td.dst(), MPLS, new GroupId(0),
Priyanka B413fbe82016-05-26 11:44:45 +05301962 tempTunnelId, td.tunnelName(), td.path(), annotations);
Priyanka B4c3b4512016-07-22 11:41:49 +05301963 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, tunnel.path(), LSP_STATE_RPT);
1964 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdentifiersTlv);
1965 pcepTunnelData.setLspDFlag(Boolean.valueOf(tunnel.annotations().value(DELEGATE)));
1966 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1967 tunnelUpdated(tunnel, td.path(), tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +05301968 executor.shutdown();
1969 try {
1970 executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
1971 } catch (InterruptedException e) {
1972 log.error("updating delegation failed");
Ray Milkey5c7d4882018-02-05 14:50:39 -08001973 Thread.currentThread().interrupt();
Priyanka B413fbe82016-05-26 11:44:45 +05301974 }
1975 }
1976 }
1977 }
cheng fan48e832c2015-05-29 01:54:47 +08001978}