blob: eebf554db23f7ec399399e3b571ca81c51bc5c55 [file] [log] [blame]
cheng fan48e832c2015-05-29 01:54:47 +08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
cheng fan48e832c2015-05-29 01:54:47 +08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.provider.pcep.tunnel.impl;
17
chengfan2fff70f2015-08-24 18:20:19 -050018import com.google.common.collect.Maps;
Avantika-Huawei56c11842016-04-28 00:56:56 +053019
cheng fan48e832c2015-05-29 01:54:47 +080020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
chengfan2fff70f2015-08-24 18:20:19 -050023import org.apache.felix.scr.annotations.Property;
cheng fan48e832c2015-05-29 01:54:47 +080024import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053027import org.onlab.packet.IpAddress;
chengfan2fff70f2015-08-24 18:20:19 -050028import org.onosproject.cfg.ComponentConfigService;
cheng fan48e832c2015-05-29 01:54:47 +080029import org.onosproject.core.DefaultGroupId;
30import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053031import org.onosproject.incubator.net.tunnel.DefaultTunnel;
cheng fan48e832c2015-05-29 01:54:47 +080032import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
chengfan2fff70f2015-08-24 18:20:19 -050033import org.onosproject.incubator.net.tunnel.DefaultTunnelStatistics;
Jonathan Hart51539b82015-10-29 09:53:04 -070034import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
cheng fan48e832c2015-05-29 01:54:47 +080035import org.onosproject.incubator.net.tunnel.OpticalLogicId;
36import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
37import org.onosproject.incubator.net.tunnel.Tunnel;
Avantika-Huawei56c11842016-04-28 00:56:56 +053038import org.onosproject.incubator.net.tunnel.Tunnel.State;
Priyanka B413fbe82016-05-26 11:44:45 +053039import org.onosproject.incubator.net.tunnel.TunnelAdminService;
cheng fan48e832c2015-05-29 01:54:47 +080040import org.onosproject.incubator.net.tunnel.TunnelDescription;
41import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
42import org.onosproject.incubator.net.tunnel.TunnelId;
43import org.onosproject.incubator.net.tunnel.TunnelName;
44import org.onosproject.incubator.net.tunnel.TunnelProvider;
45import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
46import org.onosproject.incubator.net.tunnel.TunnelProviderService;
chengfan2fff70f2015-08-24 18:20:19 -050047import org.onosproject.incubator.net.tunnel.TunnelService;
48import org.onosproject.incubator.net.tunnel.TunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053049import org.onosproject.mastership.MastershipService;
50import org.onosproject.net.AnnotationKeys;
cheng fan48e832c2015-05-29 01:54:47 +080051import org.onosproject.net.ConnectPoint;
52import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei56c11842016-04-28 00:56:56 +053053import org.onosproject.net.DefaultAnnotations.Builder;
cheng fan48e832c2015-05-29 01:54:47 +080054import org.onosproject.net.DefaultLink;
55import org.onosproject.net.DefaultPath;
Priyanka B413fbe82016-05-26 11:44:45 +053056import org.onosproject.net.Device;
cheng fan48e832c2015-05-29 01:54:47 +080057import org.onosproject.net.DeviceId;
58import org.onosproject.net.ElementId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053059import org.onosproject.net.IpElementId;
cheng fan48e832c2015-05-29 01:54:47 +080060import org.onosproject.net.Link;
61import org.onosproject.net.Path;
62import org.onosproject.net.PortNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053063import org.onosproject.net.SparseAnnotations;
Priyanka B413fbe82016-05-26 11:44:45 +053064import org.onosproject.net.device.DeviceService;
cheng fan48e832c2015-05-29 01:54:47 +080065import org.onosproject.net.provider.AbstractProvider;
66import org.onosproject.net.provider.ProviderId;
67import org.onosproject.pcep.api.PcepController;
68import org.onosproject.pcep.api.PcepDpid;
69import org.onosproject.pcep.api.PcepHopNodeDescription;
70import org.onosproject.pcep.api.PcepOperator.OperationType;
71import org.onosproject.pcep.api.PcepTunnel;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053072import org.onosproject.pcep.api.PcepTunnel.PathState;
Jonathan Hart51539b82015-10-29 09:53:04 -070073import org.onosproject.pcep.api.PcepTunnel.PathType;
cheng fan48e832c2015-05-29 01:54:47 +080074import org.onosproject.pcep.api.PcepTunnelListener;
chengfan2fff70f2015-08-24 18:20:19 -050075import org.onosproject.pcep.api.PcepTunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053076import org.onosproject.pcep.controller.LspKey;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053077import org.onosproject.pcep.controller.PccId;
78import org.onosproject.pcep.controller.PcepClient;
79import org.onosproject.pcep.controller.PcepClientController;
80import org.onosproject.pcep.controller.PcepClientListener;
81import org.onosproject.pcep.controller.PcepEventListener;
Priyanka B259847d2016-06-03 21:28:35 +053082import org.onosproject.pcep.controller.PcepLspStatus;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053083import org.onosproject.pcep.controller.PcepLspSyncAction;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053084import org.onosproject.pcepio.exceptions.PcepParseException;
85import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
86import org.onosproject.pcepio.protocol.PcepAttribute;
87import org.onosproject.pcepio.protocol.PcepBandwidthObject;
88import org.onosproject.pcepio.protocol.PcepEndPointsObject;
89import org.onosproject.pcepio.protocol.PcepEroObject;
90import org.onosproject.pcepio.protocol.PcepInitiateMsg;
91import org.onosproject.pcepio.protocol.PcepLspObject;
92import org.onosproject.pcepio.protocol.PcepMessage;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053093import org.onosproject.pcepio.protocol.PcepMetricObject;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053094import org.onosproject.pcepio.protocol.PcepMsgPath;
95import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053096import org.onosproject.pcepio.protocol.PcepSrpObject;
97import org.onosproject.pcepio.protocol.PcepStateReport;
98import org.onosproject.pcepio.protocol.PcepUpdateMsg;
99import org.onosproject.pcepio.protocol.PcepUpdateRequest;
100import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530101import org.onosproject.pcepio.types.PathSetupTypeTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530102import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530103import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530104import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -0500105import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -0700106import org.osgi.service.component.annotations.Modified;
107import org.slf4j.Logger;
108
109import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530110import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530111import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700112import java.util.Collections;
113import java.util.Dictionary;
114import java.util.HashMap;
115import java.util.LinkedList;
116import java.util.List;
117import java.util.ListIterator;
118import java.util.Optional;
Priyanka B413fbe82016-05-26 11:44:45 +0530119import java.util.concurrent.Executors;
120import java.util.concurrent.ScheduledExecutorService;
121import java.util.concurrent.TimeUnit;
Jonathan Hart51539b82015-10-29 09:53:04 -0700122
123import static com.google.common.base.Preconditions.checkNotNull;
124import static com.google.common.base.Strings.isNullOrEmpty;
125import static org.onlab.util.Tools.get;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530126import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700127import static org.onosproject.net.DefaultAnnotations.EMPTY;
128import static org.onosproject.net.DeviceId.deviceId;
129import static org.onosproject.net.PortNumber.portNumber;
130import static org.onosproject.pcep.api.PcepDpid.uri;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530131import static org.onosproject.provider.pcep.tunnel.impl.LspType.WITH_SIGNALLING;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530132import static org.onosproject.provider.pcep.tunnel.impl.LspType.SR_WITHOUT_SIGNALLING;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530133import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.BANDWIDTH;
134import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.LOCAL_LSP_ID;
135import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.LSP_SIG_TYPE;
136import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PCC_TUNNEL_ID;
137import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PLSP_ID;
Priyanka B413fbe82016-05-26 11:44:45 +0530138import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.DELEGATE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530139import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.COST_TYPE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530140import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
141import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
142import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
143import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530144import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530145import static org.onosproject.pcep.controller.PcepLspSyncAction.REMOVE;
146import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_UPDATE;
147import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_DELETE;
148import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.IGP_METRIC;
149import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.TE_METRIC;
Jonathan Hart51539b82015-10-29 09:53:04 -0700150import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800151
152/**
153 * Provider which uses an PCEP controller to detect, update, create network
154 * tunnels.
155 */
156@Component(immediate = true)
157@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530158public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800159
160 private static final Logger log = getLogger(PcepTunnelProvider.class);
161 private static final long MAX_BANDWIDTH = 99999744;
162 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800163 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530164 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
Priyanka B413fbe82016-05-26 11:44:45 +0530165 public static final long IDENTIFIER_SET = 0x100000000L;
166 public static final long SET = 0xFFFFFFFFL;
167 private static final int DELAY = 2;
168 private static final int WAIT_TIME = 5;
169 public static final String LSRID = "lsrId";
cheng fan48e832c2015-05-29 01:54:47 +0800170
chengfan2fff70f2015-08-24 18:20:19 -0500171 static final int POLL_INTERVAL = 10;
172 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
173 label = "Frequency (in seconds) for polling tunnel statistics")
174 private int tunnelStatsPollFrequency = POLL_INTERVAL;
175
cheng fan48e832c2015-05-29 01:54:47 +0800176 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
177
178 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
179 protected TunnelProviderRegistry tunnelProviderRegistry;
180
181 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
182 protected PcepController controller;
183
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530184 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
185 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500186
187 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
188 protected TunnelService tunnelService;
189
190 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
191 protected ComponentConfigService cfgService;
192
Priyanka B413fbe82016-05-26 11:44:45 +0530193 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
194 protected TunnelAdminService tunnelAdminService;
195
196 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
197 protected MastershipService mastershipService;
198
199 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
200 protected DeviceService deviceService;
201
cheng fan48e832c2015-05-29 01:54:47 +0800202 TunnelProviderService service;
203
204 HashMap<String, TunnelId> tunnelMap = new HashMap<String, TunnelId>();
chengfan2fff70f2015-08-24 18:20:19 -0500205 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400206 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800207
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530208 private InnerTunnelProvider listener = new InnerTunnelProvider();
209
Jonathan Hart51539b82015-10-29 09:53:04 -0700210 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530211 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800212
213 /**
214 * Creates a Tunnel provider.
215 */
216 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530217 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800218 }
219
220 @Activate
221 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500222 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800223 service = tunnelProviderRegistry.register(this);
224 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530225 pcepClientController.addListener(listener);
226 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500227 tunnelService.queryAllTunnels().forEach(tunnel -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700228 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500229 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
230 tsc.start();
231 collectors.put(tunnel.tunnelId().id(), tsc);
232
233 });
234
cheng fan48e832c2015-05-29 01:54:47 +0800235 log.info("Started");
236 }
237
238 @Deactivate
239 public void deactivate() {
240 tunnelProviderRegistry.unregister(this);
241 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500242 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530243 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800244 log.info("Stopped");
245 }
246
chengfan2fff70f2015-08-24 18:20:19 -0500247 @Modified
248 public void modified(ComponentContext context) {
249 Dictionary<?, ?> properties = context.getProperties();
250 int newTunnelStatsPollFrequency;
251 try {
252 String s = get(properties, "tunnelStatsPollFrequency");
253 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
254
255 } catch (NumberFormatException | ClassCastException e) {
256 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
257 }
258
259 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
260 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
261 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
262 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
263 }
264
265 }
266
cheng fan48e832c2015-05-29 01:54:47 +0800267 @Override
268 public void setupTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530269 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530270 log.error("Tunnel Type MPLS is only supported");
271 return;
272 }
cheng fan48e832c2015-05-29 01:54:47 +0800273
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530274 // check for tunnel end points
275 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
276 log.error("Tunnel source or destination is not valid");
277 return;
278 }
279
280 // Get the pcc client
281 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
282
283 if (!(pc instanceof PcepClient)) {
284 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500285 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530286 return;
287 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530288
289 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
Priyanka B413fbe82016-05-26 11:44:45 +0530290 //Only master will initiate setup tunnel
291 if (pc.capability().pcInstantiationCapability() && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530292 pcepSetupTunnel(tunnel, path, pc);
293 }
cheng fan48e832c2015-05-29 01:54:47 +0800294 }
295
296 @Override
297 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800298
Avantika-Huawei56c11842016-04-28 00:56:56 +0530299 if (tunnel.annotations().value(PLSP_ID) != null) {
300 updateTunnel(tunnel, path);
301 return;
302 }
303
304 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530305 log.error("Tunnel Type MPLS is only supported");
306 return;
307 }
308
309 if (!(srcElement instanceof IpElementId)) {
310 log.error("Element id is not valid");
311 return;
312 }
313
314 // check for tunnel end points
315 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
316 log.error("Tunnel source or destination is not valid");
317 return;
318 }
319
320 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
321
322 if (!(pc instanceof PcepClient)) {
323 log.error("There is no PCC connected with ip addresss {}"
324 + ((IpElementId) srcElement).ipAddress().toString());
325 return;
326 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530327
Priyanka B413fbe82016-05-26 11:44:45 +0530328 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
329 //Only master will initiate setup tunnel
330 if (pc.capability().pcInstantiationCapability()
331 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530332 pcepSetupTunnel(tunnel, path, pc);
333 }
cheng fan48e832c2015-05-29 01:54:47 +0800334 }
335
336 @Override
337 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800338
Avantika-Huawei56c11842016-04-28 00:56:56 +0530339 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530340 log.error("Tunnel Type MPLS is only supported");
341 return;
342 }
343
344 // 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
350 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
351
352 if (!(pc instanceof PcepClient)) {
353 log.error("There is no PCC connected with ip addresss {}"
354 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
355 return;
356 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530357
Priyanka B413fbe82016-05-26 11:44:45 +0530358 //Only master will release tunnel
359 if (pc.capability().pcInstantiationCapability()
360 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530361 pcepReleaseTunnel(tunnel, pc);
362 }
cheng fan48e832c2015-05-29 01:54:47 +0800363 }
364
365 @Override
366 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530367 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530368 log.error("Tunnel Type MPLS is only supported");
369 return;
370 }
cheng fan48e832c2015-05-29 01:54:47 +0800371
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530372 if (!(srcElement instanceof IpElementId)) {
373 log.error("Element id is not valid");
374 return;
375 }
376
377 // check for tunnel end points
378 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
379 log.error("Tunnel source or destination is not valid");
380 return;
381 }
382
383 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
384
385 if (!(pc instanceof PcepClient)) {
386 log.error("There is no PCC connected with ip addresss {}"
387 + ((IpElementId) srcElement).ipAddress().toString());
388 return;
389 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530390
Priyanka B413fbe82016-05-26 11:44:45 +0530391 //Only master will release tunnel
392 if (pc.capability().pcInstantiationCapability()
393 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530394 pcepReleaseTunnel(tunnel, pc);
395 }
cheng fan48e832c2015-05-29 01:54:47 +0800396 }
397
398 @Override
399 public void updateTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530400 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530401 log.error("Tunnel Type MPLS is only supported");
402 return;
403 }
cheng fan48e832c2015-05-29 01:54:47 +0800404
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530405 // check for tunnel end points
406 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
407 log.error("Tunnel source or destination is not valid");
408 return;
409 }
410
411 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
412
413 if (!(pc instanceof PcepClient)) {
414 log.error("There is no PCC connected with ip addresss {}"
415 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
416 return;
417 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530418
Priyanka B413fbe82016-05-26 11:44:45 +0530419 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
420 // LSP].If annotation is null D flag is not set else it is set.
421 if (pc.capability().statefulPceCapability()
422 && pc.delegationInfo(
423 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
424 .annotations().value(LOCAL_LSP_ID)))) != null) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530425 pcepUpdateTunnel(tunnel, path, pc);
426 }
cheng fan48e832c2015-05-29 01:54:47 +0800427 }
428
429 @Override
430 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800431
Avantika-Huawei56c11842016-04-28 00:56:56 +0530432 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530433 log.error("Tunnel Type MPLS is only supported");
434 return;
435 }
436
437 if (!(srcElement instanceof IpElementId)) {
438 log.error("Element id is not valid");
439 return;
440 }
441
442 // check for tunnel end points
443 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
444 log.error("Tunnel source or destination is not valid");
445 return;
446 }
447
448 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
449
450 if (!(pc instanceof PcepClient)) {
451 log.error("There is no PCC connected with ip addresss {}"
452 + ((IpElementId) srcElement).ipAddress().toString());
453 return;
454 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530455
Priyanka B413fbe82016-05-26 11:44:45 +0530456 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
457 // LSP].If annotation is null D flag is not set else it is set.
458 if (pc.capability().statefulPceCapability()
459 && pc.delegationInfo(
460 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
461 .annotations().value(LOCAL_LSP_ID)))) != null) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530462 pcepUpdateTunnel(tunnel, path, pc);
463 }
cheng fan48e832c2015-05-29 01:54:47 +0800464 }
465
466 @Override
467 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530468 return handleTunnelAdded(tunnel, null);
469 }
470
471 public TunnelId tunnelAdded(TunnelDescription tunnel, State tunnelState) {
472 return handleTunnelAdded(tunnel, tunnelState);
473 }
474
475 private TunnelId handleTunnelAdded(TunnelDescription tunnel, State tunnelState) {
476
477 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700478 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530479
480 if (tunnelState == null) {
481 return service.tunnelAdded(tunnel);
482 } else {
483 return service.tunnelAdded(tunnel, tunnelState);
484 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530485 }
cheng fan48e832c2015-05-29 01:54:47 +0800486
Avantika-Huawei56c11842016-04-28 00:56:56 +0530487 long bandwidth = Long.parseLong(tunnel.annotations().value(BANDWIDTH));
cheng fan48e832c2015-05-29 01:54:47 +0800488
489 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800490 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800491 return null;
492 }
493
494 // endpoints
495 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
496 .src();
497 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
498 // devices
499 DeviceId srcId = (DeviceId) src.elementId().get();
500 DeviceId dstId = (DeviceId) dst.elementId().get();
501
502 // ports
503 long srcPort = src.portNumber().get().toLong();
504 long dstPort = dst.portNumber().get().toLong();
505
506 // type
507 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800508 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800509 return null;
510 }
511
512 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
513 dstPort, bandwidth,
514 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530515 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800516
517 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
518 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
519 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
520
521 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
522 return tunnelId;
523 }
524
Priyanka B413fbe82016-05-26 11:44:45 +0530525 private void tunnelUpdated(Tunnel tunnel, Path path) {
526 handleTunnelUpdate(tunnel, path);
527 }
528
529 //Handles tunnel updated using tunnel admin service[specially to update annotations].
530 private void handleTunnelUpdate(Tunnel tunnel, Path path) {
531
532 if (tunnel.type() == MPLS) {
533 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.tunnelId());
534
535 tunnelAdminService.updateTunnel(tunnel, path);
536
537 return;
538 }
539
540 Tunnel tunnelOld = tunnelQueryById(tunnel.tunnelId());
541 if (tunnelOld.type() != Tunnel.Type.VLAN) {
542 error("Illegal tunnel type. Only support VLAN tunnel update.");
543 return;
544 }
545
546 long bandwidth = Long
547 .parseLong(tunnel.annotations().value("bandwidth"));
548 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
549 error("Update failed, invalid bandwidth.");
550 return;
551 }
552 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
553
554 checkNotNull(pcepTunnelId, "Invalid tunnel id");
555 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
556 error("Update failed,maybe invalid bandwidth.");
557 return;
558 }
559 tunnelAdminService.updateTunnel(tunnel, path);
560 }
561
cheng fan48e832c2015-05-29 01:54:47 +0800562 @Override
563 public void tunnelRemoved(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530564 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700565 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530566 service.tunnelRemoved(tunnel);
567 }
568
cheng fan48e832c2015-05-29 01:54:47 +0800569 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
570 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
571 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800572 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800573 return;
574 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700575 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800576 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800577 if (!controller.deleteTunnel(pcepTunnelId)) {
578 error("Delete tunnel failed, Maybe some devices have been disconnected.");
579 return;
cheng fan48e832c2015-05-29 01:54:47 +0800580 }
581 tunnelMap.remove(pcepTunnelId);
582 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800583 }
584
585 @Override
586 public void tunnelUpdated(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530587 handleTunnelUpdate(tunnel, null);
588 }
589
590 public void tunnelUpdated(TunnelDescription tunnel, State tunnelState) {
591 handleTunnelUpdate(tunnel, tunnelState);
592 }
593
594 private void handleTunnelUpdate(TunnelDescription tunnel, State tunnelState) {
595 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700596 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530597
598 if (tunnelState == null) {
599 service.tunnelUpdated(tunnel);
600 } else {
601 service.tunnelUpdated(tunnel, tunnelState);
602 }
603 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530604 }
cheng fan48e832c2015-05-29 01:54:47 +0800605
606 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
607 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800608 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800609 return;
610 }
cheng fan7716ec92015-05-31 01:53:19 +0800611 long bandwidth = Long
612 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800613 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800614 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800615 return;
616 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700617 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800618
619 checkNotNull(pcepTunnelId, "Invalid tunnel id");
620 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
621
cheng fan7716ec92015-05-31 01:53:19 +0800622 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800623 return;
624
625 }
626 service.tunnelUpdated(tunnel);
627 }
628
cheng fan7716ec92015-05-31 01:53:19 +0800629 private void error(String info) {
630 System.err.println(info);
631 }
632
cheng fan48e832c2015-05-29 01:54:47 +0800633 // Short-hand for creating a connection point.
634 private ConnectPoint connectPoint(PcepDpid id, long port) {
635 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
636 }
637
638 // Short-hand for creating a link.
639 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800640 return DefaultLink.builder()
641 .providerId(id())
642 .src(connectPoint(src, sp))
643 .dst(connectPoint(dst, dp))
644 .type(Link.Type.TUNNEL)
645 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800646 }
647
648 // Creates a path that leads through the given devices.
649 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700650 PathType pathtype, PathState pathState) {
cheng fan48e832c2015-05-29 01:54:47 +0800651 if (hopList == null || hopList.size() == 0) {
652 return null;
653 }
654 List<Link> links = new ArrayList<>();
655 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
656 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
657 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
658 .get(i + 1).getPortNum()));
659 }
660
661 int hopNum = hopList.size() - 2;
662 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
663 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800664 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800665 .set("pathType", String.valueOf(pathtype)).build();
666 return new DefaultPath(id(), links, hopNum, extendAnnotations);
667 }
668
669 // convert the path description to a string.
670 public String pathToString(List<Link> links) {
671 StringBuilder builder = new StringBuilder();
672 builder.append("{");
673 for (Link link : links) {
674 builder.append("(Device:" + link.src().deviceId() + " Port:"
675 + link.src().port().toLong());
676 builder.append(" Device:" + link.dst().deviceId() + " Port:"
677 + link.dst().port().toLong());
678 builder.append(")");
679 }
680 builder.append("}");
681 return builder.toString();
682 }
683
684 // build a TunnelDescription.
685 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
686 TunnelId tunnelId) {
687 TunnelEndPoint srcPoint = null;
688 TunnelEndPoint dstPoint = null;
689 Tunnel.Type tunnelType = null;
690 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
691
692 // add path after codes of tunnel's path merged
693 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800694 pcepTunnel.getPathType(),
695 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800696
697 OpticalTunnelEndPoint.Type endPointType = null;
698 switch (pcepTunnel.type()) {
699 case OCH:
700 tunnelType = Tunnel.Type.OCH;
701 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
702 break;
703
704 case OTN:
705 tunnelType = Tunnel.Type.ODUK;
706 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
707 break;
708
709 case UNI:
710 tunnelType = Tunnel.Type.VLAN;
711 endPointType = null;
712 break;
713
714 default:
715 break;
716 }
717 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
718 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
719 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
720 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
721
722 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
723 Optional.of(srcPort), null,
724 endPointType,
725 OpticalLogicId.logicId(0),
726 true);
727 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
728 Optional.of(dstPort), null,
729 endPointType,
730 OpticalLogicId.logicId(0),
731 true);
732
733 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800734 DefaultAnnotations annotations = DefaultAnnotations
735 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800736 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800737 .set("bandwidth",
738 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800739 .set("index", String.valueOf(pcepTunnel.id())).build();
740
cheng fan48e832c2015-05-29 01:54:47 +0800741 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
742 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800743 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800744 DefaultAnnotations extendAnnotations = DefaultAnnotations
745 .builder()
746 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800747 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800748 annotations = DefaultAnnotations.merge(annotations,
749 extendAnnotations);
750
751 }
752 TunnelDescription tunnel = new DefaultTunnelDescription(
753 tunnelId,
754 srcPoint,
755 dstPoint,
756 tunnelType,
757 new DefaultGroupId(
758 0),
759 id(), name,
760 path,
761 annotations);
762 return tunnel;
cheng fan48e832c2015-05-29 01:54:47 +0800763 }
764
765 /**
766 * Get the tunnelID according to the tunnel key.
767 *
768 * @param tunnelKey tunnel key
769 * @return corresponding tunnel id of the a tunnel key.
770 */
771 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800772 for (String key : tunnelMap.keySet()) {
773 if (key.equals(tunnelKey)) {
774 return tunnelMap.get(key);
775 }
776 }
777 return null;
778 }
779
780 /**
781 * Get the tunnel key according to the tunnelID.
782 *
783 * @param tunnelId tunnel id
784 * @return corresponding a tunnel key of the tunnel id.
785 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700786 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800787 for (String key : tunnelMap.keySet()) {
788 if (tunnelMap.get(key).id() == tunnelId.id()) {
789 return key;
790 }
791 }
792 return null;
793
794 }
795
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530796 /**
chengfan2fff70f2015-08-24 18:20:19 -0500797 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
798 *
799 * @param statistics statistics data from a PCEP tunnel
800 * @return TunnelStatistics
801 */
802 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
803 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
804 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
805 .setPacketLossRatio(statistics.packetLossRate())
806 .setFlowDelay(statistics.flowDelay())
807 .setAlarms(statistics.alarms())
808 .build();
809 return tunnelStatistics;
810 }
811 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530812 * Creates list of hops for ERO object from Path.
813 *
814 * @param path network path
815 * @return list of ipv4 subobjects
816 */
817 private LinkedList<PcepValueType> createPcepPath(Path path) {
818 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
819 List<Link> listLink = path.links();
820 ConnectPoint source = null;
821 ConnectPoint destination = null;
822 IpAddress ipDstAddress = null;
823 IpAddress ipSrcAddress = null;
824 PcepValueType subObj = null;
825
826 for (Link link : listLink) {
827 source = link.src();
828 if (!(source.equals(destination))) {
829 //set IPv4SubObject for ERO object
830 ipSrcAddress = source.ipElementId().ipAddress();
831 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
832 llSubObjects.add(subObj);
833 }
834
835 destination = link.dst();
836 ipDstAddress = destination.ipElementId().ipAddress();
837 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
838 llSubObjects.add(subObj);
839 }
840 return llSubObjects;
841 }
842
843 /**
844 * Creates PcInitiated lsp request list for setup tunnel.
845 *
846 * @param tunnel mpls tunnel
847 * @param path network path
848 * @param pc pcep client
849 * @param srpId unique id for pcep message
850 * @return list of PcInitiatedLspRequest
851 * @throws PcepParseException while building pcep objects fails
852 */
853 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
854 PcepClient pc, int srpId)
855 throws PcepParseException {
856 PcepValueType tlv;
857 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
858
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700859 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530860 log.error("There is no link information to create tunnel");
861 return null;
862 }
863
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530864 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530865
866 // set PathSetupTypeTlv of SRP object
867 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)).type());
868 llOptionalTlv.add(tlv);
869
870 // build SRP object
871 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
872 .setOptionalTlv(llOptionalTlv).build();
873
874 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530875 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530876
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530877 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530878 short localLspId = 0;
879 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
880 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
881 if (localLspIdString != null) {
882 localLspId = Short.valueOf(localLspIdString);
883 }
884 }
885
886 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
887 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
888 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530889 llOptionalTlv.add(tlv);
890 //set SymbolicPathNameTlv of LSP object
891 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
892 llOptionalTlv.add(tlv);
893
894 //build LSP object
895 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setOFlag((byte) 0).setPlspId(0)
896 .setOptionalTlv(llOptionalTlv).build();
897
898 //build ENDPOINTS object
899 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
900 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
901 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
902 .setPFlag(true).build();
903
904 //build ERO object
905 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
906
907 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530908 if (tunnel.annotations().value(BANDWIDTH) != null) {
909 iBandwidth = Integer.parseInt(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530910 }
911 // build bandwidth object
912 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
913 // build pcep attribute
914 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
915
916 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
917 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
918 .setPcepAttribute(pcepAttribute).build();
919 llPcInitiatedLspRequestList.add(initiateLspRequest);
920 return llPcInitiatedLspRequestList;
921 }
922
923 /**
924 * To send initiate tunnel message to pcc.
925 *
926 * @param tunnel mpls tunnel info
927 * @param path explicit route for the tunnel
928 * @param pc pcep client to send message
929 */
930 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
931 try {
932 int srpId = SrpIdGenerators.create();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530933 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530934
Jonathan Hart51539b82015-10-29 09:53:04 -0700935 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530936
937 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
938 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700939 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530940 log.error("Failed to create PcInitiatedLspRequestList");
941 return;
942 }
943
944 //build PCInitiate message
945 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
946 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
947 .build();
948
949 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
950
Jonathan Hart51539b82015-10-29 09:53:04 -0700951 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530952 } catch (PcepParseException e) {
953 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
954 }
955 }
956
957 /**
958 * To send Release tunnel message to pcc.
959 *
960 * @param tunnel mpls tunnel info
961 * @param pc pcep client to send message
962 */
963 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
964 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530965 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -0700966 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530967 int srpId = SrpIdGenerators.create();
968 TunnelId tunnelId = tunnel.tunnelId();
969 int plspId = 0;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530970
Jonathan Hart51539b82015-10-29 09:53:04 -0700971 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530972 log.error("Tunnel doesnot exists. Tunnel id {}" + tunnelId.toString());
973 return;
974 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -0700975 PcepTunnelData pcepTunnelDbData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530976 plspId = pcepTunnelDbData.plspId();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530977 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530978
979 PcepValueType tlv;
980 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530981
982 // set PathSetupTypeTlv of SRP object
983 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
984 .type());
985 llOptionalTlv.add(tlv);
986
987 // build SRP object
988 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
989 .setOptionalTlv(llOptionalTlv).build();
990
991 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530992 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
993
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530994 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
995 llOptionalTlv.add(tlv);
996 // build lsp object, set r flag as false to delete the tunnel
997 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
998 .setOptionalTlv(llOptionalTlv).build();
999
1000 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1001 .setLspObject(lspobj).build();
1002
1003 llPcInitiatedLspRequestList.add(releaseLspRequest);
1004
1005 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1006 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1007
1008 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1009
Jonathan Hart51539b82015-10-29 09:53:04 -07001010 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301011 } catch (PcepParseException e) {
1012 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1013 }
1014 }
1015
1016 /**
1017 * To send Update tunnel request message to pcc.
1018 *
1019 * @param tunnel mpls tunnel info
1020 * @param path explicit route for the tunnel
1021 * @param pc pcep client to send message
1022 */
1023 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1024 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301025 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001026 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301027 int srpId = SrpIdGenerators.create();
1028 TunnelId tunnelId = tunnel.tunnelId();
1029 PcepValueType tlv;
1030 int plspId = 0;
1031
1032 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
1033 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
1034 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
1035
Avantika-Huawei56c11842016-04-28 00:56:56 +05301036 // set PathSetupTypeTlv of SRP object
1037 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
1038 tlv = new PathSetupTypeTlv(lspSigType.type());
1039 llOptionalTlv.add(tlv);
1040
1041 // build SRP object
1042 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
1043 .setOptionalTlv(llOptionalTlv).build();
1044
1045 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301046
Jonathan Hart51539b82015-10-29 09:53:04 -07001047 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301048 log.error("Tunnel doesnot exists in DB");
1049 return;
1050 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -07001051 PcepTunnelData pcepTunnelDBData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301052 plspId = pcepTunnelDBData.plspId();
1053 }
1054
Avantika-Huawei56c11842016-04-28 00:56:56 +05301055 if (lspSigType != WITH_SIGNALLING) {
1056 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1057 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1058 short localLspId = 0;
1059 short pccTunnelId = 0;
1060
1061 if (localLspIdString != null) {
1062 localLspId = Short.valueOf(localLspIdString);
1063 }
1064
1065 if (pccTunnelIdString != null) {
1066 pccTunnelId = Short.valueOf(pccTunnelIdString);
1067 }
1068
1069 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1070 .ip().getIp4Address().toInt()),
1071 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1072 .getIp4Address().toInt()));
1073 llOptionalTlv.add(tlv);
1074 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301075
1076 if (tunnel.tunnelName().value() != null) {
1077 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1078 llOptionalTlv.add(tlv);
1079 }
1080
1081 // build lsp object
1082 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setPlspId(plspId)
1083 .setOptionalTlv(llOptionalTlv).build();
1084 // build ero object
1085 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
1086
1087 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301088 if (tunnel.annotations().value(BANDWIDTH) != null) {
1089 iBandwidth = Integer.parseInt(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301090 }
1091 // build bandwidth object
1092 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1093 // build pcep attribute
1094 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1095 // build pcep msg path
1096 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1097 .build();
1098
1099 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1100 .setLspObject(lspobj).setMsgPath(msgPath).build();
1101
1102 llUpdateRequestList.add(updateRequest);
1103
1104 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1105
1106 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001107 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301108 } catch (PcepParseException e) {
1109 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1110 }
1111 }
1112
chengfan2fff70f2015-08-24 18:20:19 -05001113
1114
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301115 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001116
1117 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001118 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001119 TunnelDescription tunnel = null;
1120 // instance and id identify a tunnel together
1121 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1122 + String.valueOf(pcepTunnel.id());
1123
1124 if (tunnelKey == null || "".equals(tunnelKey)) {
1125 log.error("Invalid PCEP tunnel");
1126 return;
1127 }
1128
1129 TunnelId tunnelId = getTunnelId(tunnelKey);
1130
1131 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1132
1133 OperationType operType = pcepTunnel.getOperationType();
1134 switch (operType) {
1135 case ADD:
1136 tunnelId = service.tunnelAdded(tunnel);
1137 tunnelMap.put(tunnelKey, tunnelId);
1138 break;
1139
1140 case UPDATE:
1141 service.tunnelUpdated(tunnel);
1142 break;
1143
1144 case DELETE:
1145 service.tunnelRemoved(tunnel);
1146 tunnelMap.remove(tunnelKey);
1147 break;
1148
1149 default:
1150 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301151 }
1152 }
cheng fan48e832c2015-05-29 01:54:47 +08001153
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301154 @Override
1155 public void handleMessage(PccId pccId, PcepMessage msg) {
1156 try {
1157 log.debug("tunnel provider handle message {}", msg.getType().toString());
1158 switch (msg.getType()) {
1159 case REPORT:
1160 int srpId = 0;
1161 LinkedList<PcepStateReport> llStateReportList = null;
1162 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1163 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1164 PcepSrpObject srpObj = null;
1165 PcepLspObject lspObj = null;
1166 while (listIterator.hasNext()) {
1167 PcepStateReport stateRpt = listIterator.next();
1168 srpObj = stateRpt.getSrpObject();
1169 lspObj = stateRpt.getLspObject();
1170
1171 if (srpObj instanceof PcepSrpObject) {
1172 srpId = srpObj.getSrpID();
1173 }
1174
1175 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1176 log.debug("SRP ID in handle message " + srpId);
1177
Jonathan Hart51539b82015-10-29 09:53:04 -07001178 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301179 // For PCRpt without matching SRP id.
1180 handleRptWithoutSrpId(stateRpt, pccId);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301181 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301182 }
1183
Avantika-Huawei56c11842016-04-28 00:56:56 +05301184 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301185 }
1186 break;
1187
1188 default:
1189 log.debug("Received unsupported message type {}", msg.getType().toString());
1190 }
1191 } catch (Exception e) {
1192 log.error("Exception occured while processing report message {}", e.getMessage());
1193 }
1194 }
1195
1196 /**
1197 * Handles report message for setup/update/delete tunnel request.
1198 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301199 * @param srpId unique identifier for PCEP message
1200 * @param lspObj LSP object
1201 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301202 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301203 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301204 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001205 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301206
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301207 // store the values required from report message
1208 pcepTunnelData.setPlspId(lspObj.getPlspId());
1209 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1210 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1211 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1212
Avantika-Huawei56c11842016-04-28 00:56:56 +05301213 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301214 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1215 while (listTlvIterator.hasNext()) {
1216 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301217 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1218 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301219 break;
1220 }
1221 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001222 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301223 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001224 }
1225
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301226 Path path = pcepTunnelData.path();
1227 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301228 Builder annotationBuilder = DefaultAnnotations.builder();
1229 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1230
1231 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1232 if (tunnel.annotations().value(PLSP_ID) == null) {
1233 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1234 }
1235
1236 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1237 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1238 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1239 }
1240
1241 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301242 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1243 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1244 providerId, tunnel.tunnelName(), path,
1245 annotations);
1246
Avantika-Huawei56c11842016-04-28 00:56:56 +05301247 if (CREATE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001248 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301249 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001250 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301251 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301252 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001253 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1254 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301255 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301256
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301257 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1258
Avantika-Huawei56c11842016-04-28 00:56:56 +05301259 if (lspObj.getRFlag()) {
1260 tunnelRemoved(td);
1261 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301262 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301263 tunnelUpdated(td, tunnelState);
1264 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301265
1266 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1267 // though there are no labels to download for SR-TE.
1268 if ((pcepLspStatus == PcepLspStatus.GOING_UP)
1269 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING)) {
1270 updateTunnel(tunnel, tunnel.path());
1271 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301272 }
1273
Priyanka B413fbe82016-05-26 11:44:45 +05301274 private SparseAnnotations getAnnotations(PcepLspObject lspObj, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv,
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301275 float bandwidth, LspType lspType, String costType) {
1276
1277 Builder builder = DefaultAnnotations.builder();
1278
1279 /*
1280 * [RFC 5440] The absence of the METRIC object MUST be interpreted by the PCE as a path computation request
1281 * for which no constraints need be applied to any of the metrics.
1282 */
1283 if (costType != null) {
1284 builder.set(COST_TYPE, costType);
1285 }
1286
1287 SparseAnnotations annotations = builder
Priyanka B413fbe82016-05-26 11:44:45 +05301288 .set(BANDWIDTH, (new Float(bandwidth)).toString()).set(LSP_SIG_TYPE, lspType.name())
1289 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1290 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1291 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId()))
1292 .set(DELEGATE, String.valueOf(lspObj.getDFlag()))
1293 .build();
1294 return annotations;
1295 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301296
Priyanka B413fbe82016-05-26 11:44:45 +05301297 private LspType getLspType(PcepSrpObject srpObj) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301298 LspType lspType = WITH_SIGNALLING;
1299
1300 if (null != srpObj) {
1301 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1302 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1303
1304 while (listIterator.hasNext()) {
1305 PcepValueType tlv = listIterator.next();
1306
1307 switch (tlv.getType()) {
1308 case PathSetupTypeTlv.TYPE:
1309 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1310 break;
1311
1312 default:
1313 break;
1314 }
1315 }
1316 }
Priyanka B413fbe82016-05-26 11:44:45 +05301317 return lspType;
1318 }
1319
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301320 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId) {
Priyanka B413fbe82016-05-26 11:44:45 +05301321 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301322 String costType = null;
Priyanka B413fbe82016-05-26 11:44:45 +05301323 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1324 checkNotNull(msgPath);
1325 PcepEroObject eroObj = msgPath.getEroObject();
1326 if (eroObj == null) {
1327 log.error("ERO object is null in report message.");
1328 return;
1329 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301330
1331 PcepAttribute attributes = msgPath.getPcepAttribute();
1332 int cost = 0;
1333 if (attributes != null && attributes.getMetricObjectList() != null) {
1334 ListIterator<PcepMetricObject> iterator = attributes.getMetricObjectList().listIterator();
1335 PcepMetricObject metricObj = iterator.next();
1336
1337 while (metricObj != null) {
1338 if (metricObj.getBType() == IGP_METRIC) {
1339 costType = "COST";
1340 } else if (metricObj.getBType() == TE_METRIC) {
1341 costType = "TE_COST";
1342 }
1343
1344 if (costType != null) {
1345 cost = metricObj.getMetricVal();
1346 log.debug("Path cost {}", cost);
1347 break;
1348 }
1349 metricObj = iterator.next();
1350 }
1351 }
1352
1353 Path path = buildPathFromEroObj(eroObj, providerId, cost);
Priyanka B413fbe82016-05-26 11:44:45 +05301354
1355 float bandwidth = 0;
1356 if (msgPath.getBandwidthObject() != null) {
1357 bandwidth = msgPath.getBandwidthObject().getBandwidth();
1358 }
1359
1360 /*
1361 * To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1362 * from PCE.
1363 */
1364 PcepSrpObject srpObj = stateRpt.getSrpObject();
1365 LspType lspType = getLspType(srpObj);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301366
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301367 PcepLspObject lspObj = stateRpt.getLspObject();
1368 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1369 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1370 SymbolicPathNameTlv pathNameTlv = null;
1371
1372 while (listTlvIterator.hasNext()) {
1373 PcepValueType tlv = listTlvIterator.next();
1374 switch (tlv.getType()) {
1375 case StatefulIPv4LspIdentifiersTlv.TYPE:
1376 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1377 break;
1378
1379 case SymbolicPathNameTlv.TYPE:
1380 pathNameTlv = (SymbolicPathNameTlv) tlv;
1381 break;
1382
1383 default:
1384 break;
1385 }
1386 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301387 /*
1388 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1389 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1390 */
1391 if (ipv4LspIdenTlv == null) {
1392 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1393 return;
1394 }
1395
1396 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1397 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1398 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1399 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1400 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
1401
Priyanka B413fbe82016-05-26 11:44:45 +05301402 // Store delegation flag info and that LSP info because only delegated PCE sends update message
1403 // Storing if D flag is set, if not dont store. while checking whether delegation if annotation for D flag
1404 // not present then non-delegated , if present it is delegated.
1405 if (lspObj.getDFlag()) {
1406 pcepClientController.getClient(pccId).setLspAndDelegationInfo(
1407 new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()), lspObj.getDFlag());
1408 }
1409
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301410 Tunnel tunnel = null;
1411 // Asynchronous status change message from PCC for LSP reported earlier.
1412 for (Tunnel tunnelObj : tunnelQueryResult) {
1413 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1414 /*
1415 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1416 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1417 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1418 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1419 * session an LSP is "first" reported to a PCE.
1420 */
1421 if ((pathNameTlv != null)
1422 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1423 tunnel = tunnelObj;
1424 break;
1425 }
1426 continue;
1427 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301428 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId()) && (Integer
1429 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1430 tunnel = tunnelObj;
1431 break;
1432 }
1433 }
1434
1435 DefaultTunnelDescription td;
Priyanka B413fbe82016-05-26 11:44:45 +05301436 SparseAnnotations annotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301437 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
1438 if (tunnel == null) {
1439 if (lspObj.getRFlag()) {
1440 /*
1441 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1442 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1443 */
1444 return;
1445 }
1446
Priyanka B413fbe82016-05-26 11:44:45 +05301447 DeviceId deviceId = getDevice(pccId);
1448 if (deviceId == null) {
1449 log.error("Ingress deviceId not found");
1450 return;
1451 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301452 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType, costType);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301453
Priyanka B413fbe82016-05-26 11:44:45 +05301454 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
1455 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path,
1456 annotations);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301457
1458 // Do not support PCC initiated LSP after LSP DB sync is completed.
1459 if (!lspObj.getSFlag() && !lspObj.getCFlag()) {
1460 log.error("Received PCC initiated LSP while not in sync.");
1461 return;
1462 }
1463
Priyanka B413fbe82016-05-26 11:44:45 +05301464 /*
1465 * If ONOS instance is master for PCC then set delegated flag as annotation and add the tunnel to store.
1466 * Because all LSPs need not be delegated, hence mastership for the PCC is confirmed whereas not the
1467 * delegation set to all LSPs.If ONOS is not the master for that PCC then check if D flag is set, if yes
1468 * wait for 2 seconds [while master has added the tunnel to the store] then update the tunnel. Tunnel is
1469 * updated because in case of resilency only delegated LSPs are recomputed and only delegated PCE can
1470 * send update message to that client.
1471 *
1472 * 1)Master can 1st get the Rpt message
1473 * a)Master adds the tunnel into core.
1474 * b)If a non-master for ingress gets Rpt message with D flag set[as delegation owner]
1475 * after master, then runs timer then update the tunnel with D flag set.
1476 * 2)Non-Master can 1st get the Rpt message
1477 * a)Non-Master runs the timer check for the tunnel then updates the tunnel with D flag set
1478 * b)Master would have got the message while the non-master running timer, hence master adds
1479 * tunnel to core
1480 *
1481 * In general always master adds the tunnel to the core
1482 * while delegated owner [master or non-master with D flag set] always updates the tunnel running timer
1483 */
1484 if (mastershipService.isLocalMaster(deviceId)) {
1485 TunnelId tId = tunnelAdded(td, tunnelState);
1486 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1487 tunnelState, new DefaultGroupId(0), tId, TunnelName.tunnelName(String.valueOf(pathNameTlv
1488 .getValue())), path, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301489
Priyanka B413fbe82016-05-26 11:44:45 +05301490 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1491 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1492 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1493 } else if (!mastershipService.isLocalMaster(deviceId) && lspObj.getDFlag()) {
1494 //Start timer then update the tunnel with D flag
1495 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1496 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301497 return;
1498 }
1499
Priyanka B413fbe82016-05-26 11:44:45 +05301500 //delegated owner will update can be a master or non-master
1501 if (lspObj.getDFlag()) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301502 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType, costType);
Priyanka B413fbe82016-05-26 11:44:45 +05301503 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
1504 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path,
1505 annotations);
1506 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1507 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301508 removeOrUpdatetunnel(tunnel, pccId, lspObj, providerId, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +05301509 return;
1510 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301511
Priyanka B413fbe82016-05-26 11:44:45 +05301512 private void removeOrUpdatetunnel(Tunnel tunnel, PccId pccId, PcepLspObject lspObj, ProviderId providerId,
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301513 State tunnelState) {
Priyanka B413fbe82016-05-26 11:44:45 +05301514 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1515 tunnel.type(), tunnel.groupId(), providerId, tunnel.tunnelName(), tunnel.path(),
1516 (SparseAnnotations) tunnel.annotations());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301517 if (lspObj.getRFlag()) {
1518 tunnelRemoved(td);
1519 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301520 tunnelUpdated(td, tunnelState);
1521 }
Priyanka B413fbe82016-05-26 11:44:45 +05301522 }
1523
1524 private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
1525 DefaultTunnelDescription td, ProviderId providerId) {
1526 //Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
1527
1528 /*
1529 * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
1530 * master has added the tunnel to the store] then update the tunnel.
1531 */
1532 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1533
1534 // Thread is started after 2 seconds first time later periodically after 2 seconds to update the tunnel
1535 executor.scheduleAtFixedRate(new UpdateDelegation(td, providerId, annotations, pccId,
1536 executor), DELAY, DELAY, TimeUnit.SECONDS);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301537 }
1538
1539 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301540 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301541 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301542 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301543 * @param providerId provider id
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301544 * @param cost cost of path
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301545 * @return path object
1546 */
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301547 private Path buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId, int cost) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301548 checkNotNull(eroObj);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301549 List<Link> links = new ArrayList<Link>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301550 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301551 if (0 == llSubObj.size()) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301552 log.error("ERO in report message does not have hop information");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301553 }
1554 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1555
1556 ConnectPoint src = null;
1557 ConnectPoint dst = null;
1558 boolean isSrcSet = false;
1559 while (tlvIterator.hasNext()) {
1560 PcepValueType subObj = tlvIterator.next();
1561 switch (subObj.getType()) {
1562
1563 case IPv4SubObject.TYPE:
1564
1565 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1566 if (!isSrcSet) {
1567 IpAddress srcIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1568 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1569 isSrcSet = true;
1570 } else {
1571 IpAddress dstIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1572 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
Ray Milkey2693bda2016-01-22 16:08:14 -08001573 Link link = DefaultLink.builder()
1574 .providerId(providerId)
1575 .src(src)
1576 .dst(dst)
1577 .type(Link.Type.DIRECT)
1578 .build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301579 links.add(link);
1580 src = dst;
1581 }
1582 break;
1583 default:
1584 // the other sub objects are not required
1585 }
1586 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301587
1588 return new DefaultPath(providerId, links, cost, EMPTY);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301589 }
1590
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301591 @Override
1592 public void clientConnected(PccId pccId) {
1593 // TODO
1594 }
1595
1596 @Override
1597 public void clientDisconnected(PccId pccId) {
1598 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001599 }
chengfan2fff70f2015-08-24 18:20:19 -05001600
chengfan2fff70f2015-08-24 18:20:19 -05001601 @Override
1602 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1603 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1604 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1605 tunnelStatisticsMap.put(id, tunnelStatistics);
1606 }
cheng fan48e832c2015-05-29 01:54:47 +08001607
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301608 @Override
1609 public void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301610
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301611 if (endOfSyncAction == SEND_UPDATE) {
1612 updateTunnel(tunnel, tunnel.path());
1613 return;
1614 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301615
1616 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1617 tunnel.src(), tunnel.dst(),
1618 tunnel.type(),
1619 tunnel.groupId(),
1620 tunnel.providerId(),
1621 tunnel.tunnelName(),
1622 tunnel.path(),
1623 (SparseAnnotations) tunnel.annotations());
1624
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301625
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301626 if (endOfSyncAction == PcepLspSyncAction.UNSTABLE) {
1627
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301628 // Send PCInit msg again after global reoptimization.
1629 tunnelUpdated(td, UNSTABLE);
1630
1631 // To remove the old tunnel from store whose PLSPID is not
1632 // recognized by ingress PCC.
1633 tunnelRemoved(td);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301634
1635 } else if (endOfSyncAction == REMOVE) {
1636 tunnelRemoved(td);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301637 }
1638 }
1639
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301640 @Override
1641 public void handleEndOfSyncAction(PccId pccId, PcepMessage msg, PcepLspSyncAction endOfSyncAction) {
1642 try {
1643 if ((msg instanceof PcepInitiateMsg) && (endOfSyncAction == SEND_DELETE)) {
1644 PcepClient pc = pcepClientController.getClient(pccId);
1645 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = ((PcepInitiateMsg) msg)
1646 .getPcInitiatedLspRequestList();
1647 PcInitiatedLspRequest pcInitMsg = llPcInitiatedLspRequestList.iterator().next();
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301648
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301649 if (pcInitMsg != null) {
1650 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(SrpIdGenerators.create())
1651 .setRFlag(true).build();
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301652
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301653 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest()
1654 .setLspObject(pcInitMsg.getLspObject()).setSrpObject(srpobj).build();
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301655
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301656 llPcInitiatedLspRequestList.remove(pcInitMsg);
1657 llPcInitiatedLspRequestList.add(releaseLspRequest);
1658
1659 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1660 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1661
1662 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1663 }
1664 }
1665 } catch (PcepParseException e) {
1666 log.error("Exception occured while sending initiate delete message {}", e.getMessage());
1667 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301668 }
1669 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301670 @Override
1671 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1672 return service.tunnelQueryById(tunnelId);
1673 }
1674
Priyanka B413fbe82016-05-26 11:44:45 +05301675
1676 private DeviceId getDevice(PccId pccId) {
1677 // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID.
1678 IpAddress lsrId = pccId.ipAddress();
1679 String lsrIdentifier = String.valueOf(lsrId);
1680
1681 // Find PCC deviceID from lsrId stored as annotations
1682 Iterable<Device> devices = deviceService.getAvailableDevices();
1683 for (Device dev : devices) {
1684 if (dev.annotations().value(AnnotationKeys.TYPE).equals("L3")
1685 && dev.annotations().value(LSRID).equals(lsrIdentifier)) {
1686 return dev.id();
1687 }
1688 }
1689 return null;
1690 }
1691
1692 /**
1693 * Updates the tunnel with updated tunnel annotation after a delay of two seconds and checks it till
1694 * tunnel is found.
1695 */
1696 private class UpdateDelegation implements Runnable {
1697 DefaultTunnelDescription td;
1698 ProviderId providerId;
1699 SparseAnnotations annotations;
1700 PccId pccId;
1701 ScheduledExecutorService executor;
1702
1703 /**
1704 * Creates an instance of UpdateDelegation.
1705 *
1706 * @param td tunnel description
1707 * @param providerId provider id
1708 * @param annotations tunnel annotations
1709 * @param pccId PCEP client id
1710 * @param executor service of delegated owner
1711 */
1712 public UpdateDelegation(DefaultTunnelDescription td, ProviderId providerId, SparseAnnotations annotations,
1713 PccId pccId, ScheduledExecutorService executor) {
1714 this.td = td;
1715 this.providerId = providerId;
1716 this.annotations = annotations;
1717 this.pccId = pccId;
1718 this.executor = executor;
1719 }
1720
1721 //Temporary using annotations later will use projection/network config service
1722 @Override
1723 public void run() {
1724 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(td.src(), td.dst());
1725 TunnelId tempTunnelId = null;
1726 for (Tunnel t : tunnelQueryResult) {
1727 if (t.annotations().value(LOCAL_LSP_ID) == null || t.annotations().value(PLSP_ID) == null) {
1728 continue;
1729 }
1730
1731 if (t.annotations().value(LOCAL_LSP_ID).equals(td.annotations().value(LOCAL_LSP_ID))
1732 && t.annotations().value(PLSP_ID).equals(td.annotations().value(PLSP_ID))
1733 && ((IpTunnelEndPoint) t.src()).ip().equals(pccId.id())) {
1734 tempTunnelId = t.tunnelId();
1735 break;
1736 }
1737 }
1738
1739 //If tunnel is found update the tunnel and shutdown the thread otherwise thread will be executing
1740 //periodically
1741 if (tempTunnelId != null) {
1742 Tunnel tunnel = new DefaultTunnel(providerId, td.src(), td.dst(), MPLS, new DefaultGroupId(0),
1743 tempTunnelId, td.tunnelName(), td.path(), annotations);
1744 tunnelUpdated(tunnel, td.path());
1745 executor.shutdown();
1746 try {
1747 executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
1748 } catch (InterruptedException e) {
1749 log.error("updating delegation failed");
1750 }
1751 }
1752 }
1753 }
cheng fan48e832c2015-05-29 01:54:47 +08001754}