blob: 41215baf58b56dbbfa0747300497442111966dfd [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;
Ray Milkeya7cf8c82018-02-08 15:07:06 -080019import org.onlab.graph.ScalarWeight;
Priyanka Bcdf9b102016-06-07 20:01:38 +053020import org.onlab.packet.Ip4Address;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053021import org.onlab.packet.IpAddress;
chengfan2fff70f2015-08-24 18:20:19 -050022import org.onosproject.cfg.ComponentConfigService;
Yi Tsengfa394de2017-02-01 11:26:40 -080023import org.onosproject.core.GroupId;
Avantika-Huaweif849aab2016-06-21 22:29:15 +053024import org.onosproject.incubator.net.resource.label.LabelResourceId;
25import org.onosproject.incubator.net.tunnel.DefaultLabelStack;
cheng fan48e832c2015-05-29 01:54:47 +080026import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053027import org.onosproject.incubator.net.tunnel.DefaultTunnel;
cheng fan48e832c2015-05-29 01:54:47 +080028import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
chengfan2fff70f2015-08-24 18:20:19 -050029import org.onosproject.incubator.net.tunnel.DefaultTunnelStatistics;
Jonathan Hart51539b82015-10-29 09:53:04 -070030import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
cheng fan48e832c2015-05-29 01:54:47 +080031import org.onosproject.incubator.net.tunnel.OpticalLogicId;
32import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
33import org.onosproject.incubator.net.tunnel.Tunnel;
Avantika-Huawei56c11842016-04-28 00:56:56 +053034import org.onosproject.incubator.net.tunnel.Tunnel.State;
Priyanka B413fbe82016-05-26 11:44:45 +053035import org.onosproject.incubator.net.tunnel.TunnelAdminService;
cheng fan48e832c2015-05-29 01:54:47 +080036import org.onosproject.incubator.net.tunnel.TunnelDescription;
37import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
38import org.onosproject.incubator.net.tunnel.TunnelId;
39import org.onosproject.incubator.net.tunnel.TunnelName;
40import org.onosproject.incubator.net.tunnel.TunnelProvider;
41import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
42import org.onosproject.incubator.net.tunnel.TunnelProviderService;
chengfan2fff70f2015-08-24 18:20:19 -050043import org.onosproject.incubator.net.tunnel.TunnelService;
44import org.onosproject.incubator.net.tunnel.TunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053045import org.onosproject.mastership.MastershipService;
46import org.onosproject.net.AnnotationKeys;
cheng fan48e832c2015-05-29 01:54:47 +080047import org.onosproject.net.ConnectPoint;
48import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei56c11842016-04-28 00:56:56 +053049import org.onosproject.net.DefaultAnnotations.Builder;
cheng fan48e832c2015-05-29 01:54:47 +080050import org.onosproject.net.DefaultLink;
51import org.onosproject.net.DefaultPath;
Priyanka B413fbe82016-05-26 11:44:45 +053052import org.onosproject.net.Device;
cheng fan48e832c2015-05-29 01:54:47 +080053import org.onosproject.net.DeviceId;
54import org.onosproject.net.ElementId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053055import org.onosproject.net.IpElementId;
cheng fan48e832c2015-05-29 01:54:47 +080056import org.onosproject.net.Link;
Avantika-Huaweif849aab2016-06-21 22:29:15 +053057import org.onosproject.net.NetworkResource;
cheng fan48e832c2015-05-29 01:54:47 +080058import org.onosproject.net.Path;
59import org.onosproject.net.PortNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053060import org.onosproject.net.SparseAnnotations;
Priyanka B413fbe82016-05-26 11:44:45 +053061import org.onosproject.net.device.DeviceService;
Priyanka Bc1e4e4c2016-07-01 14:57:19 +053062import org.onosproject.net.link.LinkService;
cheng fan48e832c2015-05-29 01:54:47 +080063import org.onosproject.net.provider.AbstractProvider;
64import org.onosproject.net.provider.ProviderId;
65import org.onosproject.pcep.api.PcepController;
66import org.onosproject.pcep.api.PcepDpid;
67import org.onosproject.pcep.api.PcepHopNodeDescription;
68import org.onosproject.pcep.api.PcepOperator.OperationType;
69import org.onosproject.pcep.api.PcepTunnel;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053070import org.onosproject.pcep.api.PcepTunnel.PathState;
Jonathan Hart51539b82015-10-29 09:53:04 -070071import org.onosproject.pcep.api.PcepTunnel.PathType;
cheng fan48e832c2015-05-29 01:54:47 +080072import org.onosproject.pcep.api.PcepTunnelListener;
chengfan2fff70f2015-08-24 18:20:19 -050073import org.onosproject.pcep.api.PcepTunnelStatistics;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +053074import org.onosproject.pcep.server.LspKey;
75import org.onosproject.pcep.server.LspType;
76import org.onosproject.pcep.server.PccId;
77import org.onosproject.pcep.server.PcepClient;
78import org.onosproject.pcep.server.PcepClientController;
79import org.onosproject.pcep.server.PcepClientListener;
80import org.onosproject.pcep.server.PcepEventListener;
81import org.onosproject.pcep.server.PcepLspStatus;
82import org.onosproject.pcep.server.PcepLspSyncAction;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +053083import org.onosproject.pcep.server.PcepSyncStatus;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070084import org.onosproject.pcep.server.SrpIdGenerators;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053085import org.onosproject.pcepio.exceptions.PcepParseException;
86import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
87import org.onosproject.pcepio.protocol.PcepAttribute;
88import org.onosproject.pcepio.protocol.PcepBandwidthObject;
89import org.onosproject.pcepio.protocol.PcepEndPointsObject;
90import org.onosproject.pcepio.protocol.PcepEroObject;
91import org.onosproject.pcepio.protocol.PcepInitiateMsg;
92import org.onosproject.pcepio.protocol.PcepLspObject;
93import org.onosproject.pcepio.protocol.PcepMessage;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053094import org.onosproject.pcepio.protocol.PcepMetricObject;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053095import org.onosproject.pcepio.protocol.PcepMsgPath;
96import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053097import org.onosproject.pcepio.protocol.PcepSrpObject;
98import org.onosproject.pcepio.protocol.PcepStateReport;
99import org.onosproject.pcepio.protocol.PcepUpdateMsg;
100import org.onosproject.pcepio.protocol.PcepUpdateRequest;
101import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530102import org.onosproject.pcepio.types.PathSetupTypeTlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530103import org.onosproject.pcepio.types.PcepNaiIpv4Adjacency;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530104import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530105import org.onosproject.pcepio.types.SrEroSubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530106import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530107import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -0500108import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700109import org.osgi.service.component.annotations.Activate;
110import org.osgi.service.component.annotations.Component;
111import org.osgi.service.component.annotations.Deactivate;
Jonathan Hart51539b82015-10-29 09:53:04 -0700112import org.osgi.service.component.annotations.Modified;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700113import org.osgi.service.component.annotations.Reference;
114import org.osgi.service.component.annotations.ReferenceCardinality;
Jonathan Hart51539b82015-10-29 09:53:04 -0700115import org.slf4j.Logger;
116
117import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530118import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530119import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700120import java.util.Collections;
121import java.util.Dictionary;
122import java.util.HashMap;
123import java.util.LinkedList;
124import java.util.List;
125import java.util.ListIterator;
Sho SHIMIZUc218bfa2016-08-18 14:22:50 -0700126import java.util.Objects;
Jonathan Hart51539b82015-10-29 09:53:04 -0700127import java.util.Optional;
Priyanka B413fbe82016-05-26 11:44:45 +0530128import java.util.concurrent.Executors;
129import java.util.concurrent.ScheduledExecutorService;
130import java.util.concurrent.TimeUnit;
Jonathan Hart51539b82015-10-29 09:53:04 -0700131
132import static com.google.common.base.Preconditions.checkNotNull;
133import static com.google.common.base.Strings.isNullOrEmpty;
134import static org.onlab.util.Tools.get;
Priyanka B4c3cef02016-06-14 20:27:53 +0530135import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700136import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530137import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700138import static org.onosproject.net.DefaultAnnotations.EMPTY;
139import static org.onosproject.net.DeviceId.deviceId;
140import static org.onosproject.net.PortNumber.portNumber;
141import static org.onosproject.pcep.api.PcepDpid.uri;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +0530142import static org.onosproject.pcep.server.LspType.SR_WITHOUT_SIGNALLING;
143import static org.onosproject.pcep.server.LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700144import static org.onosproject.pcep.server.LspType.WITH_SIGNALLING;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +0530145import static org.onosproject.pcep.server.PcepAnnotationKeys.BANDWIDTH;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700146import static org.onosproject.pcep.server.PcepAnnotationKeys.COST_TYPE;
147import static org.onosproject.pcep.server.PcepAnnotationKeys.DELEGATE;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +0530148import 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;
harikrushna-Huaweia2c7c202017-04-10 18:22:00 +0530153import static org.onosproject.pcep.server.PcepLspSyncAction.REMOVE;
154import static org.onosproject.pcep.server.PcepLspSyncAction.SEND_UPDATE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530155import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.IGP_METRIC;
156import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.TE_METRIC;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700157import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
158import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
159import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
160import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Jonathan Hart51539b82015-10-29 09:53:04 -0700161import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800162
163/**
164 * Provider which uses an PCEP controller to detect, update, create network
165 * tunnels.
166 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700167@Component(immediate = true, service = TunnelProvider.class)
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530168public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800169
170 private static final Logger log = getLogger(PcepTunnelProvider.class);
171 private static final long MAX_BANDWIDTH = 99999744;
172 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800173 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530174 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
Priyanka B413fbe82016-05-26 11:44:45 +0530175 public static final long IDENTIFIER_SET = 0x100000000L;
176 public static final long SET = 0xFFFFFFFFL;
177 private static final int DELAY = 2;
178 private static final int WAIT_TIME = 5;
179 public static final String LSRID = "lsrId";
cheng fan48e832c2015-05-29 01:54:47 +0800180
chengfan2fff70f2015-08-24 18:20:19 -0500181 static final int POLL_INTERVAL = 10;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700182 //@Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
183 // label = "Frequency (in seconds) for polling tunnel statistics")
chengfan2fff70f2015-08-24 18:20:19 -0500184 private int tunnelStatsPollFrequency = POLL_INTERVAL;
185
cheng fan48e832c2015-05-29 01:54:47 +0800186 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
187
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700188 @Reference(cardinality = ReferenceCardinality.MANDATORY)
cheng fan48e832c2015-05-29 01:54:47 +0800189 protected TunnelProviderRegistry tunnelProviderRegistry;
190
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700191 @Reference(cardinality = ReferenceCardinality.MANDATORY)
cheng fan48e832c2015-05-29 01:54:47 +0800192 protected PcepController controller;
193
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700194 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530195 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500196
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700197 @Reference(cardinality = ReferenceCardinality.MANDATORY)
chengfan2fff70f2015-08-24 18:20:19 -0500198 protected TunnelService tunnelService;
199
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700200 @Reference(cardinality = ReferenceCardinality.MANDATORY)
chengfan2fff70f2015-08-24 18:20:19 -0500201 protected ComponentConfigService cfgService;
202
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700203 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka B413fbe82016-05-26 11:44:45 +0530204 protected TunnelAdminService tunnelAdminService;
205
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700206 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka B413fbe82016-05-26 11:44:45 +0530207 protected MastershipService mastershipService;
208
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700209 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka B413fbe82016-05-26 11:44:45 +0530210 protected DeviceService deviceService;
211
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700212 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530213 protected LinkService linkService;
214
cheng fan48e832c2015-05-29 01:54:47 +0800215 TunnelProviderService service;
216
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800217 HashMap<String, TunnelId> tunnelMap = new HashMap<>();
chengfan2fff70f2015-08-24 18:20:19 -0500218 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400219 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800220
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530221 private InnerTunnelProvider listener = new InnerTunnelProvider();
222
Jonathan Hart51539b82015-10-29 09:53:04 -0700223 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530224 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800225
226 /**
227 * Creates a Tunnel provider.
228 */
229 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530230 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800231 }
232
233 @Activate
234 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500235 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800236 service = tunnelProviderRegistry.register(this);
237 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530238 pcepClientController.addListener(listener);
239 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500240 tunnelService.queryAllTunnels().forEach(tunnel -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700241 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500242 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
243 tsc.start();
244 collectors.put(tunnel.tunnelId().id(), tsc);
245
246 });
247
cheng fan48e832c2015-05-29 01:54:47 +0800248 log.info("Started");
249 }
250
251 @Deactivate
252 public void deactivate() {
253 tunnelProviderRegistry.unregister(this);
254 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500255 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530256 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800257 log.info("Stopped");
258 }
259
chengfan2fff70f2015-08-24 18:20:19 -0500260 @Modified
261 public void modified(ComponentContext context) {
262 Dictionary<?, ?> properties = context.getProperties();
263 int newTunnelStatsPollFrequency;
264 try {
265 String s = get(properties, "tunnelStatsPollFrequency");
266 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
267
268 } catch (NumberFormatException | ClassCastException e) {
269 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
270 }
271
272 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
273 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
274 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
275 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
276 }
chengfan2fff70f2015-08-24 18:20:19 -0500277 }
278
cheng fan48e832c2015-05-29 01:54:47 +0800279 @Override
280 public void setupTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530281 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530282 log.error("Tunnel Type MPLS is only supported");
283 return;
284 }
cheng fan48e832c2015-05-29 01:54:47 +0800285
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530286 // check for tunnel end points
287 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
288 log.error("Tunnel source or destination is not valid");
289 return;
290 }
291
292 // Get the pcc client
293 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
294
Ray Milkeyef310052018-02-06 08:58:51 -0800295 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530296 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500297 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530298 return;
299 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530300
301 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
Priyanka B413fbe82016-05-26 11:44:45 +0530302 //Only master will initiate setup tunnel
303 if (pc.capability().pcInstantiationCapability() && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530304 pcepSetupTunnel(tunnel, path, pc);
305 }
cheng fan48e832c2015-05-29 01:54:47 +0800306 }
307
308 @Override
309 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800310
Priyanka B4c3cef02016-06-14 20:27:53 +0530311 //TODO: tunnel which is passed doesn't have tunnelID
Avantika-Huawei56c11842016-04-28 00:56:56 +0530312 if (tunnel.annotations().value(PLSP_ID) != null) {
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530313 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITHOUT_SIGNALLING_AND_WITHOUT_SR) {
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530314 updateTunnel(tunnel, path);
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530315 } else {
316 // Download labels and send update message.
317 // To get new tunnel ID (modified tunnel ID)
318 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
319 for (Tunnel t : tunnels) {
320 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
321 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
322 tunnel.dst(), tunnel.type(),
323 t.state(), tunnel.groupId(),
324 t.tunnelId(),
325 tunnel.tunnelName(),
326 tunnel.path(),
327 tunnel.resource(),
328 tunnel.annotations());
329 break;
330 }
331 }
332 if (!pcepClientController.allocateLocalLabel(tunnel)) {
333 log.error("Unable to allocate labels for the tunnel {}.", tunnel.toString());
334 }
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530335 }
Avantika-Huawei56c11842016-04-28 00:56:56 +0530336 return;
337 }
338
339 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530340 log.error("Tunnel Type MPLS is only supported");
341 return;
342 }
343
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530344 // check for tunnel end points
345 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
346 log.error("Tunnel source or destination is not valid");
347 return;
348 }
349
Priyanka Bcdf9b102016-06-07 20:01:38 +0530350 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530351
Ray Milkeyef310052018-02-06 08:58:51 -0800352 if (pc == null) {
Priyanka B4c3cef02016-06-14 20:27:53 +0530353 log.error("There is no PCC connected with this device {}"
354 + srcElement.toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530355 return;
356 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530357
Priyanka B413fbe82016-05-26 11:44:45 +0530358 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
359 //Only master will initiate setup tunnel
360 if (pc.capability().pcInstantiationCapability()
361 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530362 pcepSetupTunnel(tunnel, path, pc);
363 }
cheng fan48e832c2015-05-29 01:54:47 +0800364 }
365
366 @Override
367 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800368
Avantika-Huawei56c11842016-04-28 00:56:56 +0530369 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530370 log.error("Tunnel Type MPLS is only supported");
371 return;
372 }
373
374 // check for tunnel end points
375 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
376 log.error("Tunnel source or destination is not valid");
377 return;
378 }
379
380 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
381
Ray Milkeyef310052018-02-06 08:58:51 -0800382 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530383 log.error("There is no PCC connected with ip addresss {}"
384 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
385 return;
386 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530387
Priyanka B413fbe82016-05-26 11:44:45 +0530388 //Only master will release tunnel
389 if (pc.capability().pcInstantiationCapability()
390 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530391 pcepReleaseTunnel(tunnel, pc);
392 }
cheng fan48e832c2015-05-29 01:54:47 +0800393 }
394
395 @Override
396 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530397 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530398 log.error("Tunnel Type MPLS is only supported");
399 return;
400 }
cheng fan48e832c2015-05-29 01:54:47 +0800401
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530402 if (!(srcElement instanceof IpElementId)) {
403 log.error("Element id is not valid");
404 return;
405 }
406
407 // check for tunnel end points
408 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
409 log.error("Tunnel source or destination is not valid");
410 return;
411 }
412
413 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
414
Ray Milkeyef310052018-02-06 08:58:51 -0800415 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530416 log.error("There is no PCC connected with ip addresss {}"
417 + ((IpElementId) srcElement).ipAddress().toString());
418 return;
419 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530420
Priyanka B413fbe82016-05-26 11:44:45 +0530421 //Only master will release tunnel
422 if (pc.capability().pcInstantiationCapability()
423 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530424 pcepReleaseTunnel(tunnel, pc);
425 }
cheng fan48e832c2015-05-29 01:54:47 +0800426 }
427
428 @Override
429 public void updateTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530430 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530431 log.error("Tunnel Type MPLS is only supported");
432 return;
433 }
cheng fan48e832c2015-05-29 01:54:47 +0800434
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530435 // check for tunnel end points
436 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
437 log.error("Tunnel source or destination is not valid");
438 return;
439 }
440
Priyanka B4c3cef02016-06-14 20:27:53 +0530441 //To get new tunnel ID (modified tunnel ID)
442 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
443 for (Tunnel t : tunnels) {
444 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
445 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
446 tunnel.dst(), tunnel.type(),
447 t.state(), tunnel.groupId(),
448 t.tunnelId(),
449 tunnel.tunnelName(),
450 tunnel.path(),
451 tunnel.resource(),
452 tunnel.annotations());
453 break;
454 }
455 }
456
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530457 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
458
Ray Milkeyef310052018-02-06 08:58:51 -0800459 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530460 log.error("There is no PCC connected with ip addresss {}"
461 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
462 return;
463 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530464
Priyanka B4c3cef02016-06-14 20:27:53 +0530465 //PCInitiate tunnels are always have D flag set, else check for tunnels who are delegated via LspKey
466 if (pc.capability().statefulPceCapability()) {
467 if (tunnel.annotations().value(PCE_INIT) != null && tunnel.annotations().value(PCE_INIT).equals("true")) {
468 pcepUpdateTunnel(tunnel, path, pc);
Priyanka B4c3b4512016-07-22 11:41:49 +0530469 } else {
470 // If delegation flag is set then only send update message[means delegated PCE can send update msg for
471 // that LSP. If annotation is null D flag is not set else it is set.
472 Short localLspId = 0;
473 for (Tunnel t : tunnels) {
474 if (!t.tunnelId().equals(tunnel.tunnelId()) && t.tunnelName().equals(tunnel.tunnelName())) {
475 localLspId = Short.valueOf(t.annotations().value(LOCAL_LSP_ID));
476 }
477 }
478
479 if (localLspId == 0) {
480 log.error("Local LSP ID for old tunnel not found");
481 return;
482 }
483
484 if (pc.delegationInfo(new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)),
485 localLspId.shortValue())) != null) {
486
487 pcepUpdateTunnel(tunnel, path, pc);
488 }
Priyanka B4c3cef02016-06-14 20:27:53 +0530489 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530490 }
cheng fan48e832c2015-05-29 01:54:47 +0800491 }
492
493 @Override
494 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800495
Avantika-Huawei56c11842016-04-28 00:56:56 +0530496 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530497 log.error("Tunnel Type MPLS is only supported");
498 return;
499 }
500
501 if (!(srcElement instanceof IpElementId)) {
502 log.error("Element id is not valid");
503 return;
504 }
505
506 // check for tunnel end points
507 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
508 log.error("Tunnel source or destination is not valid");
509 return;
510 }
511
512 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
513
Ray Milkeyef310052018-02-06 08:58:51 -0800514 if (pc == null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530515 log.error("There is no PCC connected with ip addresss {}"
516 + ((IpElementId) srcElement).ipAddress().toString());
517 return;
518 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530519
Priyanka B413fbe82016-05-26 11:44:45 +0530520 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
521 // LSP].If annotation is null D flag is not set else it is set.
522 if (pc.capability().statefulPceCapability()
523 && pc.delegationInfo(
524 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
525 .annotations().value(LOCAL_LSP_ID)))) != null) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530526 pcepUpdateTunnel(tunnel, path, pc);
527 }
cheng fan48e832c2015-05-29 01:54:47 +0800528 }
529
530 @Override
531 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530532 return handleTunnelAdded(tunnel, null);
533 }
534
535 public TunnelId tunnelAdded(TunnelDescription tunnel, State tunnelState) {
536 return handleTunnelAdded(tunnel, tunnelState);
537 }
538
539 private TunnelId handleTunnelAdded(TunnelDescription tunnel, State tunnelState) {
540
541 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700542 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530543
544 if (tunnelState == null) {
545 return service.tunnelAdded(tunnel);
546 } else {
547 return service.tunnelAdded(tunnel, tunnelState);
548 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530549 }
cheng fan48e832c2015-05-29 01:54:47 +0800550
Avantika-Huawei56c11842016-04-28 00:56:56 +0530551 long bandwidth = Long.parseLong(tunnel.annotations().value(BANDWIDTH));
cheng fan48e832c2015-05-29 01:54:47 +0800552
553 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800554 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800555 return null;
556 }
557
558 // endpoints
559 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
560 .src();
561 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
562 // devices
563 DeviceId srcId = (DeviceId) src.elementId().get();
564 DeviceId dstId = (DeviceId) dst.elementId().get();
565
566 // ports
567 long srcPort = src.portNumber().get().toLong();
568 long dstPort = dst.portNumber().get().toLong();
569
570 // type
571 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800572 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800573 return null;
574 }
575
576 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
577 dstPort, bandwidth,
578 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530579 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800580
581 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
582 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
583 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
584
585 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
586 return tunnelId;
587 }
588
Priyanka B4c3b4512016-07-22 11:41:49 +0530589 private void tunnelUpdated(Tunnel tunnel, Path path, State tunnelState) {
590 handleTunnelUpdate(tunnel, path, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +0530591 }
592
593 //Handles tunnel updated using tunnel admin service[specially to update annotations].
Priyanka B4c3b4512016-07-22 11:41:49 +0530594 private void handleTunnelUpdate(Tunnel tunnel, Path path, State tunnelState) {
Priyanka B413fbe82016-05-26 11:44:45 +0530595
596 if (tunnel.type() == MPLS) {
597 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.tunnelId());
598
Priyanka B4c3b4512016-07-22 11:41:49 +0530599 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
600 tunnel.type(), tunnel.groupId(), tunnel.providerId(),
601 tunnel.tunnelName(), path, tunnel.resource(),
602 (SparseAnnotations) tunnel.annotations());
Priyanka B413fbe82016-05-26 11:44:45 +0530603
Priyanka B4c3b4512016-07-22 11:41:49 +0530604 service.tunnelUpdated(td, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +0530605 return;
606 }
607
608 Tunnel tunnelOld = tunnelQueryById(tunnel.tunnelId());
609 if (tunnelOld.type() != Tunnel.Type.VLAN) {
610 error("Illegal tunnel type. Only support VLAN tunnel update.");
611 return;
612 }
613
614 long bandwidth = Long
615 .parseLong(tunnel.annotations().value("bandwidth"));
616 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
617 error("Update failed, invalid bandwidth.");
618 return;
619 }
620 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
621
622 checkNotNull(pcepTunnelId, "Invalid tunnel id");
623 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
624 error("Update failed,maybe invalid bandwidth.");
625 return;
626 }
627 tunnelAdminService.updateTunnel(tunnel, path);
628 }
629
cheng fan48e832c2015-05-29 01:54:47 +0800630 @Override
631 public void tunnelRemoved(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530632 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700633 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530634 service.tunnelRemoved(tunnel);
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530635 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530636 }
637
cheng fan48e832c2015-05-29 01:54:47 +0800638 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
639 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
640 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800641 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800642 return;
643 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700644 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800645 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800646 if (!controller.deleteTunnel(pcepTunnelId)) {
647 error("Delete tunnel failed, Maybe some devices have been disconnected.");
648 return;
cheng fan48e832c2015-05-29 01:54:47 +0800649 }
650 tunnelMap.remove(pcepTunnelId);
651 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800652 }
653
654 @Override
655 public void tunnelUpdated(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530656 handleTunnelUpdate(tunnel, null);
657 }
658
659 public void tunnelUpdated(TunnelDescription tunnel, State tunnelState) {
660 handleTunnelUpdate(tunnel, tunnelState);
661 }
662
663 private void handleTunnelUpdate(TunnelDescription tunnel, State tunnelState) {
664 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700665 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530666
667 if (tunnelState == null) {
668 service.tunnelUpdated(tunnel);
669 } else {
670 service.tunnelUpdated(tunnel, tunnelState);
671 }
672 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530673 }
cheng fan48e832c2015-05-29 01:54:47 +0800674
675 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
676 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800677 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800678 return;
679 }
cheng fan7716ec92015-05-31 01:53:19 +0800680 long bandwidth = Long
681 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800682 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800683 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800684 return;
685 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700686 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800687
688 checkNotNull(pcepTunnelId, "Invalid tunnel id");
689 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
690
cheng fan7716ec92015-05-31 01:53:19 +0800691 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800692 return;
693
694 }
695 service.tunnelUpdated(tunnel);
696 }
697
cheng fan7716ec92015-05-31 01:53:19 +0800698 private void error(String info) {
699 System.err.println(info);
700 }
701
cheng fan48e832c2015-05-29 01:54:47 +0800702 // Short-hand for creating a connection point.
703 private ConnectPoint connectPoint(PcepDpid id, long port) {
704 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
705 }
706
707 // Short-hand for creating a link.
708 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800709 return DefaultLink.builder()
710 .providerId(id())
711 .src(connectPoint(src, sp))
712 .dst(connectPoint(dst, dp))
713 .type(Link.Type.TUNNEL)
714 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800715 }
716
717 // Creates a path that leads through the given devices.
718 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700719 PathType pathtype, PathState pathState) {
Jon Hallcbd1b392017-01-18 20:15:44 -0800720 if (hopList == null || hopList.isEmpty()) {
cheng fan48e832c2015-05-29 01:54:47 +0800721 return null;
722 }
723 List<Link> links = new ArrayList<>();
724 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
725 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
726 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
727 .get(i + 1).getPortNum()));
728 }
729
730 int hopNum = hopList.size() - 2;
731 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
732 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800733 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800734 .set("pathType", String.valueOf(pathtype)).build();
Ray Milkeya7cf8c82018-02-08 15:07:06 -0800735 return new DefaultPath(id(), links, ScalarWeight.toWeight(hopNum), extendAnnotations);
cheng fan48e832c2015-05-29 01:54:47 +0800736 }
737
738 // convert the path description to a string.
739 public String pathToString(List<Link> links) {
740 StringBuilder builder = new StringBuilder();
741 builder.append("{");
742 for (Link link : links) {
743 builder.append("(Device:" + link.src().deviceId() + " Port:"
744 + link.src().port().toLong());
745 builder.append(" Device:" + link.dst().deviceId() + " Port:"
746 + link.dst().port().toLong());
747 builder.append(")");
748 }
749 builder.append("}");
750 return builder.toString();
751 }
752
753 // build a TunnelDescription.
754 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
755 TunnelId tunnelId) {
756 TunnelEndPoint srcPoint = null;
757 TunnelEndPoint dstPoint = null;
758 Tunnel.Type tunnelType = null;
759 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
760
761 // add path after codes of tunnel's path merged
762 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800763 pcepTunnel.getPathType(),
764 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800765
766 OpticalTunnelEndPoint.Type endPointType = null;
767 switch (pcepTunnel.type()) {
768 case OCH:
769 tunnelType = Tunnel.Type.OCH;
770 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
771 break;
772
773 case OTN:
774 tunnelType = Tunnel.Type.ODUK;
775 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
776 break;
777
778 case UNI:
779 tunnelType = Tunnel.Type.VLAN;
780 endPointType = null;
781 break;
782
783 default:
784 break;
785 }
786 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
787 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
788 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
789 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
790
791 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
792 Optional.of(srcPort), null,
793 endPointType,
794 OpticalLogicId.logicId(0),
795 true);
796 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
797 Optional.of(dstPort), null,
798 endPointType,
799 OpticalLogicId.logicId(0),
800 true);
801
802 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800803 DefaultAnnotations annotations = DefaultAnnotations
804 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800805 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800806 .set("bandwidth",
807 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800808 .set("index", String.valueOf(pcepTunnel.id())).build();
809
cheng fan48e832c2015-05-29 01:54:47 +0800810 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
811 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800812 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800813 DefaultAnnotations extendAnnotations = DefaultAnnotations
814 .builder()
815 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800816 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800817 annotations = DefaultAnnotations.merge(annotations,
818 extendAnnotations);
819
820 }
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530821 TunnelDescription tunnel = new DefaultTunnelDescription(tunnelId,
cheng fan48e832c2015-05-29 01:54:47 +0800822 srcPoint,
823 dstPoint,
824 tunnelType,
Yi Tsengfa394de2017-02-01 11:26:40 -0800825 new GroupId(0),
cheng fan48e832c2015-05-29 01:54:47 +0800826 id(), name,
827 path,
828 annotations);
829 return tunnel;
cheng fan48e832c2015-05-29 01:54:47 +0800830 }
831
832 /**
833 * Get the tunnelID according to the tunnel key.
834 *
835 * @param tunnelKey tunnel key
836 * @return corresponding tunnel id of the a tunnel key.
837 */
838 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800839 for (String key : tunnelMap.keySet()) {
840 if (key.equals(tunnelKey)) {
841 return tunnelMap.get(key);
842 }
843 }
844 return null;
845 }
846
847 /**
848 * Get the tunnel key according to the tunnelID.
849 *
850 * @param tunnelId tunnel id
851 * @return corresponding a tunnel key of the tunnel id.
852 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700853 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800854 for (String key : tunnelMap.keySet()) {
Sho SHIMIZUc218bfa2016-08-18 14:22:50 -0700855 if (Objects.equals(tunnelMap.get(key).id(), tunnelId.id())) {
cheng fan48e832c2015-05-29 01:54:47 +0800856 return key;
857 }
858 }
859 return null;
860
861 }
862
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530863 /**
chengfan2fff70f2015-08-24 18:20:19 -0500864 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
865 *
866 * @param statistics statistics data from a PCEP tunnel
867 * @return TunnelStatistics
868 */
869 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
870 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
871 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
872 .setPacketLossRatio(statistics.packetLossRate())
873 .setFlowDelay(statistics.flowDelay())
874 .setAlarms(statistics.alarms())
875 .build();
876 return tunnelStatistics;
877 }
878 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530879 * Creates list of hops for ERO object from Path.
880 *
881 * @param path network path
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530882 * @return list of ERO subobjects
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530883 */
884 private LinkedList<PcepValueType> createPcepPath(Path path) {
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800885 LinkedList<PcepValueType> llSubObjects = new LinkedList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530886 List<Link> listLink = path.links();
887 ConnectPoint source = null;
888 ConnectPoint destination = null;
889 IpAddress ipDstAddress = null;
890 IpAddress ipSrcAddress = null;
891 PcepValueType subObj = null;
Priyanka Bcdf9b102016-06-07 20:01:38 +0530892 long portNo;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530893
894 for (Link link : listLink) {
895 source = link.src();
896 if (!(source.equals(destination))) {
897 //set IPv4SubObject for ERO object
Priyanka Bcdf9b102016-06-07 20:01:38 +0530898 portNo = source.port().toLong();
899 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
900 ipSrcAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530901 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
902 llSubObjects.add(subObj);
903 }
904
905 destination = link.dst();
Priyanka Bcdf9b102016-06-07 20:01:38 +0530906 portNo = destination.port().toLong();
907 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
908 ipDstAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530909 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
910 llSubObjects.add(subObj);
911 }
Priyanka Bcdf9b102016-06-07 20:01:38 +0530912
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530913 return llSubObjects;
914 }
915
916 /**
917 * Creates PcInitiated lsp request list for setup tunnel.
918 *
919 * @param tunnel mpls tunnel
920 * @param path network path
921 * @param pc pcep client
922 * @param srpId unique id for pcep message
923 * @return list of PcInitiatedLspRequest
924 * @throws PcepParseException while building pcep objects fails
925 */
926 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
927 PcepClient pc, int srpId)
928 throws PcepParseException {
929 PcepValueType tlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530930 LinkedList<PcepValueType> llSubObjects = null;
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530931 LspType lspType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530932
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530933 if (lspType == SR_WITHOUT_SIGNALLING) {
934 NetworkResource labelStack = tunnel.resource();
935 if (labelStack == null || !(labelStack instanceof DefaultLabelStack)) {
936 labelStack = pcepClientController.computeLabelStack(tunnel.path());
937 if (labelStack == null) {
938 log.error("Unable to create label stack.");
939 return null;
940 }
941 }
942 llSubObjects = pcepClientController.createPcepLabelStack((DefaultLabelStack) labelStack, path);
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530943 } else {
944 llSubObjects = createPcepPath(path);
945 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530946
Jon Hallcbd1b392017-01-18 20:15:44 -0800947 if (llSubObjects == null || llSubObjects.isEmpty()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530948 log.error("There is no link information to create tunnel");
949 return null;
950 }
951
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800952 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530953
954 // set PathSetupTypeTlv of SRP object
Avantika-Huawei9e848e82016-09-01 12:12:42 +0530955 tlv = new PathSetupTypeTlv(lspType.type());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530956 llOptionalTlv.add(tlv);
957
958 // build SRP object
959 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
960 .setOptionalTlv(llOptionalTlv).build();
961
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -0800962 llOptionalTlv = new LinkedList<>();
963 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530964
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530965 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530966 short localLspId = 0;
967 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
968 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
969 if (localLspIdString != null) {
970 localLspId = Short.valueOf(localLspIdString);
971 }
972 }
973
Priyanka B4c3cef02016-06-14 20:27:53 +0530974 tunnel.annotations().value(LSP_SIG_TYPE);
Avantika-Huawei56c11842016-04-28 00:56:56 +0530975 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
976 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
977 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530978 llOptionalTlv.add(tlv);
979 //set SymbolicPathNameTlv of LSP object
980 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
981 llOptionalTlv.add(tlv);
982
983 //build LSP object
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530984 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setDFlag(true).setOFlag((byte) 0)
985 .setPlspId(0).setOptionalTlv(llOptionalTlv).build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530986
987 //build ENDPOINTS object
988 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
989 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
990 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
991 .setPFlag(true).build();
992
993 //build ERO object
994 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
995
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530996 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530997 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka Bc1e4e4c2016-07-01 14:57:19 +0530998 iBandwidth = Float.valueOf(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530999 }
1000 // build bandwidth object
1001 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1002 // build pcep attribute
1003 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1004
1005 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1006 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
1007 .setPcepAttribute(pcepAttribute).build();
1008 llPcInitiatedLspRequestList.add(initiateLspRequest);
1009 return llPcInitiatedLspRequestList;
1010 }
1011
1012 /**
1013 * To send initiate tunnel message to pcc.
1014 *
1015 * @param tunnel mpls tunnel info
1016 * @param path explicit route for the tunnel
1017 * @param pc pcep client to send message
1018 */
1019 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1020 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301021 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1022 log.error("Setup tunnel has failed as LSP DB sync is not finished");
1023 return;
1024 }
1025
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301026 int srpId = SrpIdGenerators.create();
Priyanka B4c3cef02016-06-14 20:27:53 +05301027 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
1028 for (Tunnel t : tunnels) {
1029 if (t.tunnelName().equals(tunnel.tunnelName())) {
1030 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
1031 tunnel.dst(), tunnel.type(),
1032 t.state(), tunnel.groupId(),
1033 t.tunnelId(),
1034 tunnel.tunnelName(),
1035 tunnel.path(),
1036 tunnel.resource(),
1037 tunnel.annotations());
Priyanka B4c3b4512016-07-22 11:41:49 +05301038 break;
Priyanka B4c3cef02016-06-14 20:27:53 +05301039 }
1040 }
1041
1042 if (tunnel.tunnelId() == null) {
1043 log.error("Tunnel ID not found");
1044 return;
1045 }
1046
Avantika-Huawei56c11842016-04-28 00:56:56 +05301047 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301048
Jonathan Hart51539b82015-10-29 09:53:04 -07001049 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301050
1051 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
1052 pc, srpId);
Jon Hallcbd1b392017-01-18 20:15:44 -08001053 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.isEmpty()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301054 log.error("Failed to create PcInitiatedLspRequestList");
1055 return;
1056 }
1057
1058 //build PCInitiate message
1059 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1060 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
1061 .build();
1062
1063 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1064
Jonathan Hart51539b82015-10-29 09:53:04 -07001065 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301066 } catch (PcepParseException e) {
1067 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
1068 }
1069 }
1070
1071 /**
1072 * To send Release tunnel message to pcc.
1073 *
1074 * @param tunnel mpls tunnel info
1075 * @param pc pcep client to send message
1076 */
1077 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
1078 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301079 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1080 log.error("Release tunnel has failed as LSP DB sync is not finished");
1081 return;
1082 }
1083
Avantika-Huawei56c11842016-04-28 00:56:56 +05301084 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001085 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301086 int srpId = SrpIdGenerators.create();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301087
1088 PcepValueType tlv;
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001089 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301090
1091 // set PathSetupTypeTlv of SRP object
1092 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
1093 .type());
1094 llOptionalTlv.add(tlv);
1095
1096 // build SRP object
1097 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
1098 .setOptionalTlv(llOptionalTlv).build();
1099
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001100 llOptionalTlv = new LinkedList<>();
1101 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301102
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301103 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1104 llOptionalTlv.add(tlv);
Priyanka B4c3cef02016-06-14 20:27:53 +05301105
1106 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1107 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301108 String pLspIdString = tunnel.annotations().value(PLSP_ID);
1109
Priyanka B4c3cef02016-06-14 20:27:53 +05301110 short localLspId = 0;
1111 short pccTunnelId = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301112 int plspId = 0;
Priyanka B4c3cef02016-06-14 20:27:53 +05301113
1114 if (localLspIdString != null) {
1115 localLspId = Short.valueOf(localLspIdString);
1116 }
1117
1118 if (pccTunnelIdString != null) {
1119 pccTunnelId = Short.valueOf(pccTunnelIdString);
1120 }
1121
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301122 if (pLspIdString != null) {
1123 plspId = Integer.valueOf(pLspIdString);
1124 }
1125
Priyanka B4c3cef02016-06-14 20:27:53 +05301126 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1127 .ip().getIp4Address().toInt()),
1128 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1129 .getIp4Address().toInt()));
1130 llOptionalTlv.add(tlv);
1131
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301132 // build lsp object, set r flag as false to delete the tunnel
1133 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
1134 .setOptionalTlv(llOptionalTlv).build();
1135
1136 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1137 .setLspObject(lspobj).build();
1138
1139 llPcInitiatedLspRequestList.add(releaseLspRequest);
1140
1141 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1142 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1143
1144 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1145
Jonathan Hart51539b82015-10-29 09:53:04 -07001146 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301147 } catch (PcepParseException e) {
1148 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1149 }
1150 }
1151
1152 /**
1153 * To send Update tunnel request message to pcc.
1154 *
1155 * @param tunnel mpls tunnel info
1156 * @param path explicit route for the tunnel
1157 * @param pc pcep client to send message
1158 */
1159 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1160 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301161 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001162 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301163 int srpId = SrpIdGenerators.create();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301164 PcepValueType tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301165
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301166 LinkedList<PcepValueType> llSubObjects = null;
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301167 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
1168
1169 if (lspSigType == SR_WITHOUT_SIGNALLING) {
1170 NetworkResource labelStack = tunnel.resource();
1171 if (labelStack == null || !(labelStack instanceof DefaultLabelStack)) {
1172 labelStack = pcepClientController.computeLabelStack(tunnel.path());
1173 if (labelStack == null) {
1174 log.error("Unable to create label stack.");
1175 return;
1176 }
1177 }
1178 llSubObjects = pcepClientController.createPcepLabelStack((DefaultLabelStack) labelStack, path);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301179 } else {
1180 llSubObjects = createPcepPath(path);
1181 }
1182
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001183 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<>();
1184 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301185
Avantika-Huawei56c11842016-04-28 00:56:56 +05301186 // set PathSetupTypeTlv of SRP object
Avantika-Huawei56c11842016-04-28 00:56:56 +05301187 tlv = new PathSetupTypeTlv(lspSigType.type());
1188 llOptionalTlv.add(tlv);
1189
1190 // build SRP object
1191 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
1192 .setOptionalTlv(llOptionalTlv).build();
1193
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001194 llOptionalTlv = new LinkedList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301195
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301196 // Lsp Identifier tlv is required for all modes of lsp
1197 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1198 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1199 short localLspId = 0;
1200 short pccTunnelId = 0;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301201
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301202 if (localLspIdString != null) {
1203 localLspId = Short.valueOf(localLspIdString);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301204 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301205
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301206 if (pccTunnelIdString != null) {
1207 pccTunnelId = Short.valueOf(pccTunnelIdString);
1208 }
1209
1210 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1211 .ip().getIp4Address().toInt()),
1212 localLspId, pccTunnelId,
1213 ((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt(),
1214 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
1215 llOptionalTlv.add(tlv);
1216
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301217 if (tunnel.tunnelName().value() != null) {
1218 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1219 llOptionalTlv.add(tlv);
1220 }
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301221 boolean delegated = (tunnel.annotations().value(DELEGATE) == null) ? false
1222 : Boolean.valueOf(tunnel.annotations()
1223 .value(DELEGATE));
1224 boolean initiated = (tunnel.annotations().value(PCE_INIT) == null) ? false
1225 : Boolean.valueOf(tunnel.annotations()
1226 .value(PCE_INIT));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301227 // build lsp object
Priyanka B4c3cef02016-06-14 20:27:53 +05301228 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true)
1229 .setPlspId(Integer.valueOf(tunnel.annotations().value(PLSP_ID)))
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301230 .setDFlag(delegated)
1231 .setCFlag(initiated)
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301232 .setOptionalTlv(llOptionalTlv).build();
1233 // build ero object
1234 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
Priyanka B4c3cef02016-06-14 20:27:53 +05301235 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301236 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301237 iBandwidth = Float.parseFloat(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301238 }
1239 // build bandwidth object
1240 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1241 // build pcep attribute
1242 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1243 // build pcep msg path
1244 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1245 .build();
1246
1247 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1248 .setLspObject(lspobj).setMsgPath(msgPath).build();
1249
1250 llUpdateRequestList.add(updateRequest);
1251
1252 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1253
1254 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001255 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301256 } catch (PcepParseException e) {
1257 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1258 }
1259 }
1260
1261 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001262
1263 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001264 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001265 TunnelDescription tunnel = null;
1266 // instance and id identify a tunnel together
1267 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1268 + String.valueOf(pcepTunnel.id());
1269
1270 if (tunnelKey == null || "".equals(tunnelKey)) {
1271 log.error("Invalid PCEP tunnel");
1272 return;
1273 }
1274
1275 TunnelId tunnelId = getTunnelId(tunnelKey);
cheng fan48e832c2015-05-29 01:54:47 +08001276 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1277
1278 OperationType operType = pcepTunnel.getOperationType();
1279 switch (operType) {
1280 case ADD:
1281 tunnelId = service.tunnelAdded(tunnel);
1282 tunnelMap.put(tunnelKey, tunnelId);
1283 break;
1284
1285 case UPDATE:
1286 service.tunnelUpdated(tunnel);
1287 break;
1288
1289 case DELETE:
1290 service.tunnelRemoved(tunnel);
1291 tunnelMap.remove(tunnelKey);
1292 break;
1293
1294 default:
1295 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301296 }
1297 }
cheng fan48e832c2015-05-29 01:54:47 +08001298
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301299 @Override
1300 public void handleMessage(PccId pccId, PcepMessage msg) {
1301 try {
1302 log.debug("tunnel provider handle message {}", msg.getType().toString());
1303 switch (msg.getType()) {
1304 case REPORT:
1305 int srpId = 0;
1306 LinkedList<PcepStateReport> llStateReportList = null;
1307 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1308 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1309 PcepSrpObject srpObj = null;
1310 PcepLspObject lspObj = null;
1311 while (listIterator.hasNext()) {
1312 PcepStateReport stateRpt = listIterator.next();
1313 srpObj = stateRpt.getSrpObject();
1314 lspObj = stateRpt.getLspObject();
1315
Ray Milkeyef310052018-02-06 08:58:51 -08001316 if (srpObj != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301317 srpId = srpObj.getSrpID();
1318 }
1319
1320 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1321 log.debug("SRP ID in handle message " + srpId);
1322
Jonathan Hart51539b82015-10-29 09:53:04 -07001323 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301324 // For PCRpt without matching SRP id.
1325 handleRptWithoutSrpId(stateRpt, pccId);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301326 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301327 }
1328
Avantika-Huawei56c11842016-04-28 00:56:56 +05301329 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301330 }
1331 break;
1332
1333 default:
1334 log.debug("Received unsupported message type {}", msg.getType().toString());
1335 }
1336 } catch (Exception e) {
Frank Wangd8ab0962017-08-11 11:09:30 +08001337 log.error("Exception occurred while processing report message {}", e.getMessage());
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301338 }
1339 }
1340
1341 /**
1342 * Handles report message for setup/update/delete tunnel request.
1343 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301344 * @param srpId unique identifier for PCEP message
1345 * @param lspObj LSP object
1346 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301347 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301348 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301349 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001350 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301351
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301352 // store the values required from report message
1353 pcepTunnelData.setPlspId(lspObj.getPlspId());
1354 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1355 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1356 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1357
Avantika-Huawei56c11842016-04-28 00:56:56 +05301358 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301359 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1360 while (listTlvIterator.hasNext()) {
1361 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301362 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1363 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301364 break;
1365 }
1366 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001367 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301368 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001369 }
1370
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301371 Path path = pcepTunnelData.path();
1372 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301373 Builder annotationBuilder = DefaultAnnotations.builder();
1374 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1375
1376 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1377 if (tunnel.annotations().value(PLSP_ID) == null) {
1378 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1379 }
1380
1381 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1382 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1383 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1384 }
1385
Priyanka B4c3cef02016-06-14 20:27:53 +05301386 if (tunnel.annotations().value(PCC_TUNNEL_ID) == null) {
1387 annotationBuilder.set(PCC_TUNNEL_ID, String.valueOf(ipv4LspTlv.getTunnelId()));
1388 }
1389
Avantika-Huawei56c11842016-04-28 00:56:56 +05301390 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301391 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1392 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1393 providerId, tunnel.tunnelName(), path,
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301394 tunnel.resource(), annotations);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301395
Avantika-Huawei56c11842016-04-28 00:56:56 +05301396 if (CREATE == pcepTunnelData.requestType()) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301397 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Jonathan Hart51539b82015-10-29 09:53:04 -07001398 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301399 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001400 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301401 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301402 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001403 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1404 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301405 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301406
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301407 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1408
Avantika-Huawei56c11842016-04-28 00:56:56 +05301409 if (lspObj.getRFlag()) {
1410 tunnelRemoved(td);
1411 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301412 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301413 tunnelUpdated(td, tunnelState);
1414 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301415
1416 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1417 // though there are no labels to download for SR-TE.
harikrushna-Huawei8c31fe62017-02-25 15:28:16 +05301418 if (((pcepLspStatus == PcepLspStatus.GOING_UP)
1419 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING))
1420 // For PCInit tunnel up, few PCC expects PCUpd message after PCInit message,
1421 || ((tunnel.state() == State.INIT)
1422 && (pcepLspStatus == PcepLspStatus.DOWN)
1423 && (tunnel.annotations().value(PCE_INIT) != null
1424 && tunnel.annotations().value(PCE_INIT).equals("true"))
1425 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == WITH_SIGNALLING))) {
1426
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301427 // Query again to get latest tunnel updated with protocol values from PCRpt msg.
1428 updateTunnel(service.tunnelQueryById(tunnel.tunnelId()), tunnel.path());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301429 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301430 }
1431
Priyanka B413fbe82016-05-26 11:44:45 +05301432 private SparseAnnotations getAnnotations(PcepLspObject lspObj, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv,
Priyanka B4c3b4512016-07-22 11:41:49 +05301433 String bandwidth, LspType lspType, String costType, boolean isPceInit) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301434 Builder builder = DefaultAnnotations.builder();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301435 /*
1436 * [RFC 5440] The absence of the METRIC object MUST be interpreted by the PCE as a path computation request
1437 * for which no constraints need be applied to any of the metrics.
1438 */
1439 if (costType != null) {
1440 builder.set(COST_TYPE, costType);
1441 }
1442
Priyanka B4c3b4512016-07-22 11:41:49 +05301443 if (isPceInit) {
1444 builder.set(PCE_INIT, String.valueOf(isPceInit));
1445 }
1446
1447 if (bandwidth != null) {
1448 builder.set(BANDWIDTH, bandwidth);
1449 }
1450
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301451 SparseAnnotations annotations = builder
Priyanka B4c3b4512016-07-22 11:41:49 +05301452 .set(LSP_SIG_TYPE, lspType.name())
Priyanka B413fbe82016-05-26 11:44:45 +05301453 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1454 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1455 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId()))
1456 .set(DELEGATE, String.valueOf(lspObj.getDFlag()))
1457 .build();
1458 return annotations;
1459 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301460
Priyanka B413fbe82016-05-26 11:44:45 +05301461 private LspType getLspType(PcepSrpObject srpObj) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301462 LspType lspType = WITH_SIGNALLING;
1463
1464 if (null != srpObj) {
1465 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1466 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1467
1468 while (listIterator.hasNext()) {
1469 PcepValueType tlv = listIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301470 switch (tlv.getType()) {
1471 case PathSetupTypeTlv.TYPE:
1472 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1473 break;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301474 default:
1475 break;
1476 }
1477 }
1478 }
Priyanka B413fbe82016-05-26 11:44:45 +05301479 return lspType;
1480 }
1481
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301482 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId) {
Priyanka B413fbe82016-05-26 11:44:45 +05301483 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301484 String costType = null;
Priyanka B413fbe82016-05-26 11:44:45 +05301485 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1486 checkNotNull(msgPath);
1487 PcepEroObject eroObj = msgPath.getEroObject();
1488 if (eroObj == null) {
1489 log.error("ERO object is null in report message.");
1490 return;
1491 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301492 PcepAttribute attributes = msgPath.getPcepAttribute();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301493 float bandwidth = 0;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301494 int cost = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301495 if (attributes != null) {
1496 if (attributes.getMetricObjectList() != null) {
1497 ListIterator<PcepMetricObject> iterator = attributes.getMetricObjectList().listIterator();
1498 PcepMetricObject metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301499
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301500 while (metricObj != null) {
1501 if (metricObj.getBType() == IGP_METRIC) {
1502 costType = "COST";
1503 } else if (metricObj.getBType() == TE_METRIC) {
1504 costType = "TE_COST";
1505 }
1506 if (costType != null) {
1507 cost = metricObj.getMetricVal();
1508 log.debug("Path cost {}", cost);
1509 break;
1510 }
1511 metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301512 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301513 }
1514 if (attributes.getBandwidthObject() != null) {
1515 bandwidth = attributes.getBandwidthObject().getBandwidth();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301516 }
1517 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301518 PcepLspObject lspObj = stateRpt.getLspObject();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301519 List<Object> eroSubObjList = buildPathFromEroObj(eroObj, providerId);
1520 List<Link> links = new ArrayList<>();
1521 List<LabelResourceId> labels = new ArrayList<>();
1522 for (Object linkOrLabel : eroSubObjList) {
1523 if (linkOrLabel instanceof Link) {
1524 links.add((Link) linkOrLabel);
1525 } else if (linkOrLabel instanceof Integer) {
1526 labels.add(LabelResourceId.labelResourceId(((Integer) linkOrLabel).longValue()));
1527 }
Priyanka B413fbe82016-05-26 11:44:45 +05301528 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301529 Path path = null;
1530 if (!links.isEmpty()) {
Ray Milkeya7cf8c82018-02-08 15:07:06 -08001531 path = new DefaultPath(providerId, links, ScalarWeight.toWeight(cost), EMPTY);
Priyanka B4c3b4512016-07-22 11:41:49 +05301532 } else if (!lspObj.getRFlag()) {
1533 return;
1534 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301535 NetworkResource labelStack = new DefaultLabelStack(labels);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301536 // To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1537 // from PCE.
Priyanka B413fbe82016-05-26 11:44:45 +05301538 PcepSrpObject srpObj = stateRpt.getSrpObject();
1539 LspType lspType = getLspType(srpObj);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301540 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1541 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1542 SymbolicPathNameTlv pathNameTlv = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301543 while (listTlvIterator.hasNext()) {
1544 PcepValueType tlv = listTlvIterator.next();
1545 switch (tlv.getType()) {
1546 case StatefulIPv4LspIdentifiersTlv.TYPE:
1547 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1548 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301549 case SymbolicPathNameTlv.TYPE:
1550 pathNameTlv = (SymbolicPathNameTlv) tlv;
1551 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301552 default:
1553 break;
1554 }
1555 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301556 /*
1557 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1558 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1559 */
1560 if (ipv4LspIdenTlv == null) {
1561 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1562 return;
1563 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301564 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1565 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1566 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1567 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1568 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
Priyanka B413fbe82016-05-26 11:44:45 +05301569 // Store delegation flag info and that LSP info because only delegated PCE sends update message
1570 // Storing if D flag is set, if not dont store. while checking whether delegation if annotation for D flag
1571 // not present then non-delegated , if present it is delegated.
1572 if (lspObj.getDFlag()) {
1573 pcepClientController.getClient(pccId).setLspAndDelegationInfo(
1574 new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()), lspObj.getDFlag());
1575 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301576 Tunnel tunnel = null;
Priyanka B4c3b4512016-07-22 11:41:49 +05301577 SparseAnnotations oldTunnelAnnotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301578 // Asynchronous status change message from PCC for LSP reported earlier.
1579 for (Tunnel tunnelObj : tunnelQueryResult) {
1580 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1581 /*
1582 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1583 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1584 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1585 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1586 * session an LSP is "first" reported to a PCE.
1587 */
1588 if ((pathNameTlv != null)
1589 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1590 tunnel = tunnelObj;
1591 break;
1592 }
1593 continue;
1594 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301595 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId())) {
1596 if ((Integer
1597 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1598 tunnel = tunnelObj;
1599 }
1600 if ((Integer
1601 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) != ipv4LspIdenTlv.getLspId())) {
1602 oldTunnelAnnotations = (SparseAnnotations) tunnelObj.annotations();
1603 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301604 }
1605 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301606 DefaultTunnelDescription td;
Priyanka B413fbe82016-05-26 11:44:45 +05301607 SparseAnnotations annotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301608 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
Ray Milkey74e59132018-01-17 15:24:52 -08001609 if (tunnel == null && pathNameTlv != null) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301610 if (lspObj.getRFlag()) {
1611 /*
1612 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1613 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1614 */
1615 return;
1616 }
Priyanka B413fbe82016-05-26 11:44:45 +05301617 DeviceId deviceId = getDevice(pccId);
1618 if (deviceId == null) {
1619 log.error("Ingress deviceId not found");
1620 return;
1621 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301622 String tempBandwidth = null;
1623 String temoCostType = null;
1624 if (oldTunnelAnnotations != null) {
1625 tempBandwidth = oldTunnelAnnotations.value(BANDWIDTH);
1626 temoCostType = oldTunnelAnnotations.value(COST_TYPE);
1627 }
1628 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, tempBandwidth, lspType,
1629 temoCostType, lspObj.getCFlag());
Yi Tsengfa394de2017-02-01 11:26:40 -08001630 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new GroupId(
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301631 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path, labelStack,
Priyanka B413fbe82016-05-26 11:44:45 +05301632 annotations);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301633 // Do not support PCC initiated LSP after LSP DB sync is completed.
1634 if (!lspObj.getSFlag() && !lspObj.getCFlag()) {
1635 log.error("Received PCC initiated LSP while not in sync.");
1636 return;
1637 }
Priyanka B413fbe82016-05-26 11:44:45 +05301638 /*
1639 * If ONOS instance is master for PCC then set delegated flag as annotation and add the tunnel to store.
1640 * Because all LSPs need not be delegated, hence mastership for the PCC is confirmed whereas not the
1641 * delegation set to all LSPs.If ONOS is not the master for that PCC then check if D flag is set, if yes
1642 * wait for 2 seconds [while master has added the tunnel to the store] then update the tunnel. Tunnel is
1643 * updated because in case of resilency only delegated LSPs are recomputed and only delegated PCE can
1644 * send update message to that client.
1645 *
1646 * 1)Master can 1st get the Rpt message
1647 * a)Master adds the tunnel into core.
1648 * b)If a non-master for ingress gets Rpt message with D flag set[as delegation owner]
1649 * after master, then runs timer then update the tunnel with D flag set.
1650 * 2)Non-Master can 1st get the Rpt message
1651 * a)Non-Master runs the timer check for the tunnel then updates the tunnel with D flag set
1652 * b)Master would have got the message while the non-master running timer, hence master adds
1653 * tunnel to core
1654 *
1655 * In general always master adds the tunnel to the core
1656 * while delegated owner [master or non-master with D flag set] always updates the tunnel running timer
1657 */
1658 if (mastershipService.isLocalMaster(deviceId)) {
1659 TunnelId tId = tunnelAdded(td, tunnelState);
1660 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
Yuta HIGUCHI488a94c2018-01-26 17:24:09 -08001661 tunnelState, new GroupId(0), tId, TunnelName.tunnelName(Arrays.toString(pathNameTlv
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301662 .getValue())), path, labelStack, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301663
Priyanka B413fbe82016-05-26 11:44:45 +05301664 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1665 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1666 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1667 } else if (!mastershipService.isLocalMaster(deviceId) && lspObj.getDFlag()) {
1668 //Start timer then update the tunnel with D flag
Priyanka B4c3b4512016-07-22 11:41:49 +05301669 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId, tunnelState, ipv4LspIdenTlv);
Priyanka B413fbe82016-05-26 11:44:45 +05301670 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301671 return;
1672 }
Priyanka B413fbe82016-05-26 11:44:45 +05301673 //delegated owner will update can be a master or non-master
Priyanka B4c3b4512016-07-22 11:41:49 +05301674 if (lspObj.getDFlag() && !lspObj.getRFlag()) {
1675 tunnelUpdateForDelegatedLsp(tunnel, lspObj,
1676 lspType, tunnelState, pccId, labelStack, ipv4LspIdenTlv);
1677 return;
Priyanka B413fbe82016-05-26 11:44:45 +05301678 }
Priyanka B4c3b4512016-07-22 11:41:49 +05301679 removeOrUpdatetunnel(tunnel, lspObj, providerId, tunnelState, ipv4LspIdenTlv);
Priyanka B413fbe82016-05-26 11:44:45 +05301680 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301681
Priyanka B4c3b4512016-07-22 11:41:49 +05301682 private void tunnelUpdateForDelegatedLsp(Tunnel tunnel, PcepLspObject lspObj,
1683 LspType lspType, State tunnelState, PccId pccId,
1684 NetworkResource labelStack,
1685 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv) {
1686 SparseAnnotations annotations = null;
1687 DefaultTunnelDescription td;
1688 boolean isPceInit = tunnel.annotations().value(PCE_INIT) == null ? false :
1689 Boolean.valueOf((tunnel.annotations().value(PCE_INIT))).booleanValue();
1690 annotations = getAnnotations(lspObj, ipv4LspIdenTlv,
1691 tunnel.annotations().value(BANDWIDTH), lspType,
1692 tunnel.annotations().value(COST_TYPE), isPceInit);
Yi Tsengfa394de2017-02-01 11:26:40 -08001693 td = new DefaultTunnelDescription(null, tunnel.src(), tunnel.dst(), MPLS, new GroupId(
Priyanka B4c3b4512016-07-22 11:41:49 +05301694 0), tunnel.providerId(), tunnel.tunnelName(),
1695 tunnel.path(), labelStack, annotations);
1696 tunnelUpdateInDelegatedCase(pccId, annotations, td, tunnel.providerId(), tunnelState, ipv4LspIdenTlv);
1697 }
1698
1699 private void removeOrUpdatetunnel(Tunnel tunnel, PcepLspObject lspObj, ProviderId providerId,
1700 State tunnelState, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv) {
Priyanka B413fbe82016-05-26 11:44:45 +05301701 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1702 tunnel.type(), tunnel.groupId(), providerId, tunnel.tunnelName(), tunnel.path(),
1703 (SparseAnnotations) tunnel.annotations());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301704 if (lspObj.getRFlag()) {
1705 tunnelRemoved(td);
1706 } else {
Priyanka B4c3b4512016-07-22 11:41:49 +05301707 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, tunnel.path(), LSP_STATE_RPT);
1708 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1709 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301710 tunnelUpdated(td, tunnelState);
1711 }
Priyanka B413fbe82016-05-26 11:44:45 +05301712 }
1713
1714 private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
Priyanka B4c3b4512016-07-22 11:41:49 +05301715 DefaultTunnelDescription td, ProviderId providerId, State tunnelState,
1716 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv) {
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301717 // Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
Priyanka B413fbe82016-05-26 11:44:45 +05301718
1719 /*
1720 * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
1721 * master has added the tunnel to the store] then update the tunnel.
1722 */
1723 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1724
1725 // Thread is started after 2 seconds first time later periodically after 2 seconds to update the tunnel
1726 executor.scheduleAtFixedRate(new UpdateDelegation(td, providerId, annotations, pccId,
Priyanka B4c3b4512016-07-22 11:41:49 +05301727 executor, tunnelState, ipv4LspIdentifiersTlv), DELAY, DELAY, TimeUnit.SECONDS);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301728 }
1729
1730 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301731 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301732 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301733 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301734 * @param providerId provider id
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301735 * @return list of links and labels
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301736 */
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301737 private List<Object> buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301738 checkNotNull(eroObj);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301739 List<Object> subObjList = new ArrayList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301740 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Jon Hallcbd1b392017-01-18 20:15:44 -08001741 if (llSubObj.isEmpty()) {
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301742 log.error("ERO in report message does not have hop information");
Priyanka B4c3b4512016-07-22 11:41:49 +05301743 return new ArrayList<>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301744 }
1745 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1746
1747 ConnectPoint src = null;
1748 ConnectPoint dst = null;
1749 boolean isSrcSet = false;
1750 while (tlvIterator.hasNext()) {
1751 PcepValueType subObj = tlvIterator.next();
1752 switch (subObj.getType()) {
1753
1754 case IPv4SubObject.TYPE:
1755
1756 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1757 if (!isSrcSet) {
Priyanka Bc1e4e4c2016-07-01 14:57:19 +05301758 Iterable<Link> links = linkService.getActiveLinks();
1759 for (Link l : links) {
1760 if (l.src().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1761 src = l.src();
1762 isSrcSet = true;
1763 break;
1764 } else if (l.dst().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1765 src = l.dst();
1766 isSrcSet = true;
1767 break;
1768 }
1769 }
1770 } else {
1771 Iterable<Link> links = linkService.getActiveLinks();
1772 for (Link l : links) {
1773 if (l.src().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1774 dst = l.src();
1775 break;
1776 } else if (l.dst().port().equals(PortNumber.portNumber(ipv4SubObj.getIpAddress()))) {
1777 dst = l.dst();
1778 break;
1779 }
1780 }
Ray Milkey2693bda2016-01-22 16:08:14 -08001781 Link link = DefaultLink.builder()
1782 .providerId(providerId)
1783 .src(src)
1784 .dst(dst)
1785 .type(Link.Type.DIRECT)
1786 .build();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301787 subObjList.add(link);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301788 src = dst;
1789 }
1790 break;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301791 case SrEroSubObject.TYPE:
1792 SrEroSubObject srEroSubObj = (SrEroSubObject) subObj;
1793 subObjList.add(srEroSubObj.getSid());
1794
1795 if (srEroSubObj.getSt() == PcepNaiIpv4Adjacency.ST_TYPE) {
1796 PcepNaiIpv4Adjacency nai = (PcepNaiIpv4Adjacency) (srEroSubObj.getNai());
Priyanka B4c3b4512016-07-22 11:41:49 +05301797 int srcIp = nai.getLocalIpv4Addr();
1798 int dstIp = nai.getRemoteIpv4Addr();
1799 Iterable<Link> links = linkService.getActiveLinks();
1800 for (Link l : links) {
1801 long lSrc = l.src().port().toLong();
1802 long lDst = l.dst().port().toLong();
1803 if (lSrc == srcIp) {
1804 src = l.src();
1805 } else if (lDst == srcIp) {
1806 src = l.dst();
1807 }
1808 if (lSrc == dstIp) {
1809 dst = l.src();
1810 } else if (lDst == dstIp) {
1811 dst = l.dst();
1812 }
1813 if (src != null && dst != null) {
1814 break;
1815 }
1816 }
1817 if (src == null || dst == null) {
1818 return new ArrayList<>();
1819 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301820 Link link = DefaultLink.builder()
1821 .providerId(providerId)
1822 .src(src)
1823 .dst(dst)
1824 .type(Link.Type.DIRECT)
1825 .build();
1826 subObjList.add(link);
1827 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301828 default:
1829 // the other sub objects are not required
1830 }
1831 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301832 return subObjList;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301833 }
1834
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301835 @Override
1836 public void clientConnected(PccId pccId) {
1837 // TODO
1838 }
1839
1840 @Override
1841 public void clientDisconnected(PccId pccId) {
1842 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001843 }
chengfan2fff70f2015-08-24 18:20:19 -05001844
chengfan2fff70f2015-08-24 18:20:19 -05001845 @Override
1846 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1847 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1848 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1849 tunnelStatisticsMap.put(id, tunnelStatistics);
1850 }
cheng fan48e832c2015-05-29 01:54:47 +08001851
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301852 @Override
1853 public void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301854
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301855 if (endOfSyncAction == SEND_UPDATE) {
1856 updateTunnel(tunnel, tunnel.path());
1857 return;
1858 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301859
1860 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1861 tunnel.src(), tunnel.dst(),
1862 tunnel.type(),
1863 tunnel.groupId(),
1864 tunnel.providerId(),
1865 tunnel.tunnelName(),
1866 tunnel.path(),
1867 (SparseAnnotations) tunnel.annotations());
1868
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301869
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301870 if (endOfSyncAction == PcepLspSyncAction.UNSTABLE) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301871 // Send PCInit msg again after global reoptimization.
1872 tunnelUpdated(td, UNSTABLE);
1873
Avantika-Huawei9e848e82016-09-01 12:12:42 +05301874 // To remove the old tunnel from store whose PLSPID is not recognized by ingress PCC.
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301875 tunnelRemoved(td);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301876 } else if (endOfSyncAction == REMOVE) {
1877 tunnelRemoved(td);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301878 }
1879 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301880 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301881 @Override
1882 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1883 return service.tunnelQueryById(tunnelId);
1884 }
1885
Priyanka B413fbe82016-05-26 11:44:45 +05301886 private DeviceId getDevice(PccId pccId) {
1887 // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID.
1888 IpAddress lsrId = pccId.ipAddress();
1889 String lsrIdentifier = String.valueOf(lsrId);
1890
1891 // Find PCC deviceID from lsrId stored as annotations
1892 Iterable<Device> devices = deviceService.getAvailableDevices();
1893 for (Device dev : devices) {
1894 if (dev.annotations().value(AnnotationKeys.TYPE).equals("L3")
1895 && dev.annotations().value(LSRID).equals(lsrIdentifier)) {
1896 return dev.id();
1897 }
1898 }
1899 return null;
1900 }
1901
1902 /**
1903 * Updates the tunnel with updated tunnel annotation after a delay of two seconds and checks it till
1904 * tunnel is found.
1905 */
1906 private class UpdateDelegation implements Runnable {
1907 DefaultTunnelDescription td;
1908 ProviderId providerId;
1909 SparseAnnotations annotations;
1910 PccId pccId;
1911 ScheduledExecutorService executor;
Priyanka B4c3b4512016-07-22 11:41:49 +05301912 State tunnelState;
1913 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv;
Priyanka B413fbe82016-05-26 11:44:45 +05301914
1915 /**
1916 * Creates an instance of UpdateDelegation.
1917 *
1918 * @param td tunnel description
1919 * @param providerId provider id
1920 * @param annotations tunnel annotations
1921 * @param pccId PCEP client id
1922 * @param executor service of delegated owner
1923 */
1924 public UpdateDelegation(DefaultTunnelDescription td, ProviderId providerId, SparseAnnotations annotations,
Priyanka B4c3b4512016-07-22 11:41:49 +05301925 PccId pccId, ScheduledExecutorService executor, State tunnelState,
1926 StatefulIPv4LspIdentifiersTlv ipv4LspIdentifiersTlv) {
Priyanka B413fbe82016-05-26 11:44:45 +05301927 this.td = td;
1928 this.providerId = providerId;
1929 this.annotations = annotations;
1930 this.pccId = pccId;
1931 this.executor = executor;
Priyanka B4c3b4512016-07-22 11:41:49 +05301932 this.tunnelState = tunnelState;
1933 this.ipv4LspIdentifiersTlv = ipv4LspIdentifiersTlv;
Priyanka B413fbe82016-05-26 11:44:45 +05301934 }
1935
1936 //Temporary using annotations later will use projection/network config service
1937 @Override
1938 public void run() {
1939 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(td.src(), td.dst());
1940 TunnelId tempTunnelId = null;
1941 for (Tunnel t : tunnelQueryResult) {
1942 if (t.annotations().value(LOCAL_LSP_ID) == null || t.annotations().value(PLSP_ID) == null) {
1943 continue;
1944 }
1945
1946 if (t.annotations().value(LOCAL_LSP_ID).equals(td.annotations().value(LOCAL_LSP_ID))
1947 && t.annotations().value(PLSP_ID).equals(td.annotations().value(PLSP_ID))
1948 && ((IpTunnelEndPoint) t.src()).ip().equals(pccId.id())) {
1949 tempTunnelId = t.tunnelId();
1950 break;
1951 }
1952 }
1953
1954 //If tunnel is found update the tunnel and shutdown the thread otherwise thread will be executing
1955 //periodically
1956 if (tempTunnelId != null) {
Yi Tsengfa394de2017-02-01 11:26:40 -08001957 Tunnel tunnel = new DefaultTunnel(providerId, td.src(), td.dst(), MPLS, new GroupId(0),
Priyanka B413fbe82016-05-26 11:44:45 +05301958 tempTunnelId, td.tunnelName(), td.path(), annotations);
Priyanka B4c3b4512016-07-22 11:41:49 +05301959 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, tunnel.path(), LSP_STATE_RPT);
1960 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdentifiersTlv);
1961 pcepTunnelData.setLspDFlag(Boolean.valueOf(tunnel.annotations().value(DELEGATE)));
1962 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1963 tunnelUpdated(tunnel, td.path(), tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +05301964 executor.shutdown();
1965 try {
1966 executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
1967 } catch (InterruptedException e) {
1968 log.error("updating delegation failed");
Ray Milkey5c7d4882018-02-05 14:50:39 -08001969 Thread.currentThread().interrupt();
Priyanka B413fbe82016-05-26 11:44:45 +05301970 }
1971 }
1972 }
1973 }
cheng fan48e832c2015-05-29 01:54:47 +08001974}