blob: fbfeda71024f89fb45f39ca01f871b2ec83e0a7e [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;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +053082import org.onosproject.pcep.controller.PcepSyncStatus;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053083import org.onosproject.pcepio.exceptions.PcepParseException;
84import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
85import org.onosproject.pcepio.protocol.PcepAttribute;
86import org.onosproject.pcepio.protocol.PcepBandwidthObject;
87import org.onosproject.pcepio.protocol.PcepEndPointsObject;
88import org.onosproject.pcepio.protocol.PcepEroObject;
89import org.onosproject.pcepio.protocol.PcepInitiateMsg;
90import org.onosproject.pcepio.protocol.PcepLspObject;
91import org.onosproject.pcepio.protocol.PcepMessage;
92import org.onosproject.pcepio.protocol.PcepMsgPath;
93import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053094import org.onosproject.pcepio.protocol.PcepSrpObject;
95import org.onosproject.pcepio.protocol.PcepStateReport;
96import org.onosproject.pcepio.protocol.PcepUpdateMsg;
97import org.onosproject.pcepio.protocol.PcepUpdateRequest;
98import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +053099import org.onosproject.pcepio.types.PathSetupTypeTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530100import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530101import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530102import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -0500103import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -0700104import org.osgi.service.component.annotations.Modified;
105import org.slf4j.Logger;
106
107import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530108import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530109import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700110import java.util.Collections;
111import java.util.Dictionary;
112import java.util.HashMap;
113import java.util.LinkedList;
114import java.util.List;
115import java.util.ListIterator;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530116import java.util.Map;
Jonathan Hart51539b82015-10-29 09:53:04 -0700117import java.util.Optional;
Priyanka B413fbe82016-05-26 11:44:45 +0530118import java.util.concurrent.Executors;
119import java.util.concurrent.ScheduledExecutorService;
120import java.util.concurrent.TimeUnit;
Jonathan Hart51539b82015-10-29 09:53:04 -0700121
122import static com.google.common.base.Preconditions.checkNotNull;
123import static com.google.common.base.Strings.isNullOrEmpty;
124import static org.onlab.util.Tools.get;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530125import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700126import static org.onosproject.net.DefaultAnnotations.EMPTY;
127import static org.onosproject.net.DeviceId.deviceId;
128import static org.onosproject.net.PortNumber.portNumber;
129import static org.onosproject.pcep.api.PcepDpid.uri;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530130import static org.onosproject.provider.pcep.tunnel.impl.LspType.WITH_SIGNALLING;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530131import static org.onosproject.provider.pcep.tunnel.impl.LspType.SR_WITHOUT_SIGNALLING;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530132import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.BANDWIDTH;
133import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.LOCAL_LSP_ID;
134import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.LSP_SIG_TYPE;
135import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PCC_TUNNEL_ID;
136import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PLSP_ID;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530137import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.PCE_INIT;
Priyanka B413fbe82016-05-26 11:44:45 +0530138import static org.onosproject.provider.pcep.tunnel.impl.PcepAnnotationKeys.DELEGATE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530139import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
140import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
141import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
142import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530143import static org.onosproject.pcep.controller.PcepSyncStatus.IN_SYNC;
144import static org.onosproject.pcep.controller.PcepSyncStatus.SYNCED;
145import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
Jonathan Hart51539b82015-10-29 09:53:04 -0700146import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800147
148/**
149 * Provider which uses an PCEP controller to detect, update, create network
150 * tunnels.
151 */
152@Component(immediate = true)
153@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530154public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800155
156 private static final Logger log = getLogger(PcepTunnelProvider.class);
157 private static final long MAX_BANDWIDTH = 99999744;
158 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800159 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530160 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
Priyanka B413fbe82016-05-26 11:44:45 +0530161 public static final long IDENTIFIER_SET = 0x100000000L;
162 public static final long SET = 0xFFFFFFFFL;
163 private static final int DELAY = 2;
164 private static final int WAIT_TIME = 5;
165 public static final String LSRID = "lsrId";
cheng fan48e832c2015-05-29 01:54:47 +0800166
chengfan2fff70f2015-08-24 18:20:19 -0500167 static final int POLL_INTERVAL = 10;
168 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
169 label = "Frequency (in seconds) for polling tunnel statistics")
170 private int tunnelStatsPollFrequency = POLL_INTERVAL;
171
cheng fan48e832c2015-05-29 01:54:47 +0800172 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
175 protected TunnelProviderRegistry tunnelProviderRegistry;
176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
178 protected PcepController controller;
179
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
181 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
184 protected TunnelService tunnelService;
185
186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
187 protected ComponentConfigService cfgService;
188
Priyanka B413fbe82016-05-26 11:44:45 +0530189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
190 protected TunnelAdminService tunnelAdminService;
191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
193 protected MastershipService mastershipService;
194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
196 protected DeviceService deviceService;
197
cheng fan48e832c2015-05-29 01:54:47 +0800198 TunnelProviderService service;
199
200 HashMap<String, TunnelId> tunnelMap = new HashMap<String, TunnelId>();
chengfan2fff70f2015-08-24 18:20:19 -0500201 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400202 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800203
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530204 private InnerTunnelProvider listener = new InnerTunnelProvider();
205
Jonathan Hart51539b82015-10-29 09:53:04 -0700206 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530207 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800208
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530209 private Map<IpAddress, Map<TunnelId, Tunnel>> preSyncLspDbMap = new HashMap<>();
210 private Map<IpAddress, List<Tunnel>> syncCompleteDeleteList = new HashMap<>();
211 private Map<IpAddress, List<Tunnel>> syncCompleteUpdateList = new HashMap<>();
212
cheng fan48e832c2015-05-29 01:54:47 +0800213 /**
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;
763
764 }
765
766 /**
767 * Get the tunnelID according to the tunnel key.
768 *
769 * @param tunnelKey tunnel key
770 * @return corresponding tunnel id of the a tunnel key.
771 */
772 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800773 for (String key : tunnelMap.keySet()) {
774 if (key.equals(tunnelKey)) {
775 return tunnelMap.get(key);
776 }
777 }
778 return null;
779 }
780
781 /**
782 * Get the tunnel key according to the tunnelID.
783 *
784 * @param tunnelId tunnel id
785 * @return corresponding a tunnel key of the tunnel id.
786 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700787 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800788 for (String key : tunnelMap.keySet()) {
789 if (tunnelMap.get(key).id() == tunnelId.id()) {
790 return key;
791 }
792 }
793 return null;
794
795 }
796
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530797 /**
chengfan2fff70f2015-08-24 18:20:19 -0500798 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
799 *
800 * @param statistics statistics data from a PCEP tunnel
801 * @return TunnelStatistics
802 */
803 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
804 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
805 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
806 .setPacketLossRatio(statistics.packetLossRate())
807 .setFlowDelay(statistics.flowDelay())
808 .setAlarms(statistics.alarms())
809 .build();
810 return tunnelStatistics;
811 }
812 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530813 * Creates list of hops for ERO object from Path.
814 *
815 * @param path network path
816 * @return list of ipv4 subobjects
817 */
818 private LinkedList<PcepValueType> createPcepPath(Path path) {
819 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
820 List<Link> listLink = path.links();
821 ConnectPoint source = null;
822 ConnectPoint destination = null;
823 IpAddress ipDstAddress = null;
824 IpAddress ipSrcAddress = null;
825 PcepValueType subObj = null;
826
827 for (Link link : listLink) {
828 source = link.src();
829 if (!(source.equals(destination))) {
830 //set IPv4SubObject for ERO object
831 ipSrcAddress = source.ipElementId().ipAddress();
832 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
833 llSubObjects.add(subObj);
834 }
835
836 destination = link.dst();
837 ipDstAddress = destination.ipElementId().ipAddress();
838 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
839 llSubObjects.add(subObj);
840 }
841 return llSubObjects;
842 }
843
844 /**
845 * Creates PcInitiated lsp request list for setup tunnel.
846 *
847 * @param tunnel mpls tunnel
848 * @param path network path
849 * @param pc pcep client
850 * @param srpId unique id for pcep message
851 * @return list of PcInitiatedLspRequest
852 * @throws PcepParseException while building pcep objects fails
853 */
854 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
855 PcepClient pc, int srpId)
856 throws PcepParseException {
857 PcepValueType tlv;
858 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
859
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700860 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530861 log.error("There is no link information to create tunnel");
862 return null;
863 }
864
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530865 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530866
867 // set PathSetupTypeTlv of SRP object
868 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)).type());
869 llOptionalTlv.add(tlv);
870
871 // build SRP object
872 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
873 .setOptionalTlv(llOptionalTlv).build();
874
875 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530876 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530877
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530878 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530879 short localLspId = 0;
880 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
881 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
882 if (localLspIdString != null) {
883 localLspId = Short.valueOf(localLspIdString);
884 }
885 }
886
887 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
888 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
889 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530890 llOptionalTlv.add(tlv);
891 //set SymbolicPathNameTlv of LSP object
892 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
893 llOptionalTlv.add(tlv);
894
895 //build LSP object
896 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setOFlag((byte) 0).setPlspId(0)
897 .setOptionalTlv(llOptionalTlv).build();
898
899 //build ENDPOINTS object
900 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
901 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
902 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
903 .setPFlag(true).build();
904
905 //build ERO object
906 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
907
908 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530909 if (tunnel.annotations().value(BANDWIDTH) != null) {
910 iBandwidth = Integer.parseInt(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530911 }
912 // build bandwidth object
913 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
914 // build pcep attribute
915 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
916
917 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
918 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
919 .setPcepAttribute(pcepAttribute).build();
920 llPcInitiatedLspRequestList.add(initiateLspRequest);
921 return llPcInitiatedLspRequestList;
922 }
923
924 /**
925 * To send initiate tunnel message to pcc.
926 *
927 * @param tunnel mpls tunnel info
928 * @param path explicit route for the tunnel
929 * @param pc pcep client to send message
930 */
931 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
932 try {
933 int srpId = SrpIdGenerators.create();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530934 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530935
Jonathan Hart51539b82015-10-29 09:53:04 -0700936 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530937
938 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
939 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700940 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530941 log.error("Failed to create PcInitiatedLspRequestList");
942 return;
943 }
944
945 //build PCInitiate message
946 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
947 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
948 .build();
949
950 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
951
Jonathan Hart51539b82015-10-29 09:53:04 -0700952 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530953 } catch (PcepParseException e) {
954 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
955 }
956 }
957
958 /**
959 * To send Release tunnel message to pcc.
960 *
961 * @param tunnel mpls tunnel info
962 * @param pc pcep client to send message
963 */
964 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
965 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530966 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -0700967 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530968 int srpId = SrpIdGenerators.create();
969 TunnelId tunnelId = tunnel.tunnelId();
970 int plspId = 0;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530971
Jonathan Hart51539b82015-10-29 09:53:04 -0700972 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530973 log.error("Tunnel doesnot exists. Tunnel id {}" + tunnelId.toString());
974 return;
975 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -0700976 PcepTunnelData pcepTunnelDbData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530977 plspId = pcepTunnelDbData.plspId();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530978 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530979
980 PcepValueType tlv;
981 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530982
983 // set PathSetupTypeTlv of SRP object
984 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
985 .type());
986 llOptionalTlv.add(tlv);
987
988 // build SRP object
989 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
990 .setOptionalTlv(llOptionalTlv).build();
991
992 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530993 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
994
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530995 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
996 llOptionalTlv.add(tlv);
997 // build lsp object, set r flag as false to delete the tunnel
998 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
999 .setOptionalTlv(llOptionalTlv).build();
1000
1001 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1002 .setLspObject(lspobj).build();
1003
1004 llPcInitiatedLspRequestList.add(releaseLspRequest);
1005
1006 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1007 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1008
1009 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1010
Jonathan Hart51539b82015-10-29 09:53:04 -07001011 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301012 } catch (PcepParseException e) {
1013 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1014 }
1015 }
1016
1017 /**
1018 * To send Update tunnel request message to pcc.
1019 *
1020 * @param tunnel mpls tunnel info
1021 * @param path explicit route for the tunnel
1022 * @param pc pcep client to send message
1023 */
1024 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1025 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301026 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001027 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301028 int srpId = SrpIdGenerators.create();
1029 TunnelId tunnelId = tunnel.tunnelId();
1030 PcepValueType tlv;
1031 int plspId = 0;
1032
1033 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
1034 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
1035 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
1036
Avantika-Huawei56c11842016-04-28 00:56:56 +05301037 // set PathSetupTypeTlv of SRP object
1038 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
1039 tlv = new PathSetupTypeTlv(lspSigType.type());
1040 llOptionalTlv.add(tlv);
1041
1042 // build SRP object
1043 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
1044 .setOptionalTlv(llOptionalTlv).build();
1045
1046 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301047
Jonathan Hart51539b82015-10-29 09:53:04 -07001048 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301049 log.error("Tunnel doesnot exists in DB");
1050 return;
1051 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -07001052 PcepTunnelData pcepTunnelDBData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301053 plspId = pcepTunnelDBData.plspId();
1054 }
1055
Avantika-Huawei56c11842016-04-28 00:56:56 +05301056 if (lspSigType != WITH_SIGNALLING) {
1057 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1058 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1059 short localLspId = 0;
1060 short pccTunnelId = 0;
1061
1062 if (localLspIdString != null) {
1063 localLspId = Short.valueOf(localLspIdString);
1064 }
1065
1066 if (pccTunnelIdString != null) {
1067 pccTunnelId = Short.valueOf(pccTunnelIdString);
1068 }
1069
1070 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1071 .ip().getIp4Address().toInt()),
1072 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1073 .getIp4Address().toInt()));
1074 llOptionalTlv.add(tlv);
1075 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301076
1077 if (tunnel.tunnelName().value() != null) {
1078 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1079 llOptionalTlv.add(tlv);
1080 }
1081
1082 // build lsp object
1083 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setPlspId(plspId)
1084 .setOptionalTlv(llOptionalTlv).build();
1085 // build ero object
1086 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
1087
1088 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301089 if (tunnel.annotations().value(BANDWIDTH) != null) {
1090 iBandwidth = Integer.parseInt(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301091 }
1092 // build bandwidth object
1093 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1094 // build pcep attribute
1095 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1096 // build pcep msg path
1097 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1098 .build();
1099
1100 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1101 .setLspObject(lspobj).setMsgPath(msgPath).build();
1102
1103 llUpdateRequestList.add(updateRequest);
1104
1105 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1106
1107 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001108 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301109 } catch (PcepParseException e) {
1110 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1111 }
1112 }
1113
chengfan2fff70f2015-08-24 18:20:19 -05001114
1115
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301116 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001117
1118 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001119 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001120 TunnelDescription tunnel = null;
1121 // instance and id identify a tunnel together
1122 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1123 + String.valueOf(pcepTunnel.id());
1124
1125 if (tunnelKey == null || "".equals(tunnelKey)) {
1126 log.error("Invalid PCEP tunnel");
1127 return;
1128 }
1129
1130 TunnelId tunnelId = getTunnelId(tunnelKey);
1131
1132 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1133
1134 OperationType operType = pcepTunnel.getOperationType();
1135 switch (operType) {
1136 case ADD:
1137 tunnelId = service.tunnelAdded(tunnel);
1138 tunnelMap.put(tunnelKey, tunnelId);
1139 break;
1140
1141 case UPDATE:
1142 service.tunnelUpdated(tunnel);
1143 break;
1144
1145 case DELETE:
1146 service.tunnelRemoved(tunnel);
1147 tunnelMap.remove(tunnelKey);
1148 break;
1149
1150 default:
1151 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301152 }
1153 }
cheng fan48e832c2015-05-29 01:54:47 +08001154
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301155 @Override
1156 public void handleMessage(PccId pccId, PcepMessage msg) {
1157 try {
1158 log.debug("tunnel provider handle message {}", msg.getType().toString());
1159 switch (msg.getType()) {
1160 case REPORT:
1161 int srpId = 0;
1162 LinkedList<PcepStateReport> llStateReportList = null;
1163 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1164 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1165 PcepSrpObject srpObj = null;
1166 PcepLspObject lspObj = null;
1167 while (listIterator.hasNext()) {
1168 PcepStateReport stateRpt = listIterator.next();
1169 srpObj = stateRpt.getSrpObject();
1170 lspObj = stateRpt.getLspObject();
1171
1172 if (srpObj instanceof PcepSrpObject) {
1173 srpId = srpObj.getSrpID();
1174 }
1175
1176 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1177 log.debug("SRP ID in handle message " + srpId);
1178
Jonathan Hart51539b82015-10-29 09:53:04 -07001179 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301180
1181 // Check the sync status
1182 if (lspObj.getSFlag()) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301183 if (pcepClientController.getClient(pccId).lspDbSyncStatus() != IN_SYNC) {
1184 pcepClientController.getClient(pccId).setLspDbSyncStatus(IN_SYNC);
1185
1186 // On starting LSP-DB sync, store LSP DB locally for this PCC.
1187 Map<TunnelId, Tunnel> preSyncLspDb = new HashMap<>();
1188 Collection<Tunnel> queriedTunnels = tunnelService.queryTunnel(MPLS);
1189
1190 for (Tunnel tunnel : queriedTunnels) {
1191 if (((IpTunnelEndPoint) tunnel.src()).ip().equals(pccId.ipAddress())) {
1192 preSyncLspDb.put(tunnel.tunnelId(), tunnel);
1193 }
1194 }
1195
1196 preSyncLspDbMap.put(pccId.ipAddress(), preSyncLspDb);
1197 syncCompleteDeleteList.put(pccId.ipAddress(), new LinkedList<>());
1198 syncCompleteUpdateList.put(pccId.ipAddress(), new LinkedList<>());
1199 }
1200 handleRptWithoutSrpId(stateRpt, pccId, IN_SYNC);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301201 continue;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301202
1203 } else if (pcepClientController.getClient(pccId).lspDbSyncStatus() == IN_SYNC) {
1204 // If sync flag is not set in the msg, and the
1205 // previous state was "in sync" means this is
1206 // end of sync message. PCRpt for end of sync
1207 // does not carry any LSP report.
1208 pcepClientController.getClient(pccId).setLspDbSyncStatus(SYNCED);
1209 handleEndOfSyncAction(pccId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301210 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301211 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301212
1213 // For PCRpt without matching SRP id not during LSPDB sync.
1214 handleRptWithoutSrpId(stateRpt, pccId, SYNCED);
1215 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301216 }
1217
Avantika-Huawei56c11842016-04-28 00:56:56 +05301218 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301219 }
1220 break;
1221
1222 default:
1223 log.debug("Received unsupported message type {}", msg.getType().toString());
1224 }
1225 } catch (Exception e) {
1226 log.error("Exception occured while processing report message {}", e.getMessage());
1227 }
1228 }
1229
1230 /**
1231 * Handles report message for setup/update/delete tunnel request.
1232 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301233 * @param srpId unique identifier for PCEP message
1234 * @param lspObj LSP object
1235 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301236 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301237 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301238 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001239 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301240
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301241 // store the values required from report message
1242 pcepTunnelData.setPlspId(lspObj.getPlspId());
1243 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1244 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1245 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1246
Avantika-Huawei56c11842016-04-28 00:56:56 +05301247 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301248 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1249 while (listTlvIterator.hasNext()) {
1250 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301251 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1252 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301253 break;
1254 }
1255 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001256 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301257 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001258 }
1259
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301260 Path path = pcepTunnelData.path();
1261 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301262 Builder annotationBuilder = DefaultAnnotations.builder();
1263 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1264
1265 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1266 if (tunnel.annotations().value(PLSP_ID) == null) {
1267 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1268 }
1269
1270 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1271 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1272 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1273 }
1274
1275 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301276 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1277 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1278 providerId, tunnel.tunnelName(), path,
1279 annotations);
1280
Avantika-Huawei56c11842016-04-28 00:56:56 +05301281 if (CREATE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001282 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301283 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001284 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301285 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301286 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001287 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1288 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301289 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301290
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301291 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1292
Avantika-Huawei56c11842016-04-28 00:56:56 +05301293 if (lspObj.getRFlag()) {
1294 tunnelRemoved(td);
1295 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301296 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301297 tunnelUpdated(td, tunnelState);
1298 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301299
1300 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1301 // though there are no labels to download for SR-TE.
1302 if ((pcepLspStatus == PcepLspStatus.GOING_UP)
1303 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING)) {
1304 updateTunnel(tunnel, tunnel.path());
1305 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301306 }
1307
Priyanka B413fbe82016-05-26 11:44:45 +05301308 private SparseAnnotations getAnnotations(PcepLspObject lspObj, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv,
1309 float bandwidth, LspType lspType) {
1310 SparseAnnotations annotations = DefaultAnnotations.builder()
1311 .set(BANDWIDTH, (new Float(bandwidth)).toString()).set(LSP_SIG_TYPE, lspType.name())
1312 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1313 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1314 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId()))
1315 .set(DELEGATE, String.valueOf(lspObj.getDFlag()))
1316 .build();
1317 return annotations;
1318 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301319
Priyanka B413fbe82016-05-26 11:44:45 +05301320 private LspType getLspType(PcepSrpObject srpObj) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301321 LspType lspType = WITH_SIGNALLING;
1322
1323 if (null != srpObj) {
1324 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1325 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1326
1327 while (listIterator.hasNext()) {
1328 PcepValueType tlv = listIterator.next();
1329
1330 switch (tlv.getType()) {
1331 case PathSetupTypeTlv.TYPE:
1332 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1333 break;
1334
1335 default:
1336 break;
1337 }
1338 }
1339 }
Priyanka B413fbe82016-05-26 11:44:45 +05301340 return lspType;
1341 }
1342
1343 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId, PcepSyncStatus syncStatus) {
1344 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
1345 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1346 checkNotNull(msgPath);
1347 PcepEroObject eroObj = msgPath.getEroObject();
1348 if (eroObj == null) {
1349 log.error("ERO object is null in report message.");
1350 return;
1351 }
1352 Path path = buildPathFromEroObj(eroObj, providerId);
1353
1354 float bandwidth = 0;
1355 if (msgPath.getBandwidthObject() != null) {
1356 bandwidth = msgPath.getBandwidthObject().getBandwidth();
1357 }
1358
1359 /*
1360 * To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1361 * from PCE.
1362 */
1363 PcepSrpObject srpObj = stateRpt.getSrpObject();
1364 LspType lspType = getLspType(srpObj);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301365
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301366 PcepLspObject lspObj = stateRpt.getLspObject();
1367 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1368 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1369 SymbolicPathNameTlv pathNameTlv = null;
1370
1371 while (listTlvIterator.hasNext()) {
1372 PcepValueType tlv = listTlvIterator.next();
1373 switch (tlv.getType()) {
1374 case StatefulIPv4LspIdentifiersTlv.TYPE:
1375 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1376 break;
1377
1378 case SymbolicPathNameTlv.TYPE:
1379 pathNameTlv = (SymbolicPathNameTlv) tlv;
1380 break;
1381
1382 default:
1383 break;
1384 }
1385 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301386 /*
1387 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1388 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1389 */
1390 if (ipv4LspIdenTlv == null) {
1391 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1392 return;
1393 }
1394
1395 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1396 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1397 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1398 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1399 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
1400
Priyanka B413fbe82016-05-26 11:44:45 +05301401 // Store delegation flag info and that LSP info because only delegated PCE sends update message
1402 // Storing if D flag is set, if not dont store. while checking whether delegation if annotation for D flag
1403 // not present then non-delegated , if present it is delegated.
1404 if (lspObj.getDFlag()) {
1405 pcepClientController.getClient(pccId).setLspAndDelegationInfo(
1406 new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()), lspObj.getDFlag());
1407 }
1408
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301409 Tunnel tunnel = null;
1410 // Asynchronous status change message from PCC for LSP reported earlier.
1411 for (Tunnel tunnelObj : tunnelQueryResult) {
1412 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1413 /*
1414 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1415 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1416 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1417 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1418 * session an LSP is "first" reported to a PCE.
1419 */
1420 if ((pathNameTlv != null)
1421 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1422 tunnel = tunnelObj;
1423 break;
1424 }
1425 continue;
1426 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301427 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId()) && (Integer
1428 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1429 tunnel = tunnelObj;
1430 break;
1431 }
1432 }
1433
1434 DefaultTunnelDescription td;
Priyanka B413fbe82016-05-26 11:44:45 +05301435 SparseAnnotations annotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301436 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
1437 if (tunnel == null) {
1438 if (lspObj.getRFlag()) {
1439 /*
1440 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1441 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1442 */
1443 return;
1444 }
1445
1446 if (lspObj.getCFlag()) {
1447 /*
1448 * While in sync, if PCRpt is received for PCE init LSP and PCE doesn't have entry, mark to send
1449 * delete message on end of sync.
1450 */
Priyanka B413fbe82016-05-26 11:44:45 +05301451 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301452
Priyanka B413fbe82016-05-26 11:44:45 +05301453 // Generate tunnel id for the temporary tunnel.
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301454 String onosTunnelId = "PCC" + String.valueOf(ipv4LspIdenTlv.getTunnelId());
1455 Tunnel tunnelToBeDeleted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1456 new DefaultGroupId(0), TunnelId.valueOf(onosTunnelId),
1457 TunnelName.tunnelName(String
1458 .valueOf(pathNameTlv.getValue())),
1459 path, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301460 /*
1461 * Need to send PCInitiate delete msg for a tunnel which does not exist at PCE. For that some dummy
1462 * data-structures need to be populated.
1463 */
1464 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelToBeDeleted, path, RequestType.DELETE);
1465 pcepTunnelData.setPlspId(lspObj.getPlspId());
1466 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1467 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1468 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(0, pcepTunnelData);
1469
1470 /*
1471 * Add to the list of tunnels for which PCInit delete will be sent at the end of sync.
1472 */
1473 List<Tunnel> tunnelToBeDeletedList = syncCompleteDeleteList.get(pccId.ipAddress());
1474 tunnelToBeDeletedList.add(tunnelToBeDeleted);
1475 syncCompleteDeleteList.put(pccId.ipAddress(), tunnelToBeDeletedList);
1476 return;
1477 }
Priyanka B413fbe82016-05-26 11:44:45 +05301478 DeviceId deviceId = getDevice(pccId);
1479 if (deviceId == null) {
1480 log.error("Ingress deviceId not found");
1481 return;
1482 }
1483 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301484
Priyanka B413fbe82016-05-26 11:44:45 +05301485 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
1486 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path,
1487 annotations);
1488 /*
1489 * If ONOS instance is master for PCC then set delegated flag as annotation and add the tunnel to store.
1490 * Because all LSPs need not be delegated, hence mastership for the PCC is confirmed whereas not the
1491 * delegation set to all LSPs.If ONOS is not the master for that PCC then check if D flag is set, if yes
1492 * wait for 2 seconds [while master has added the tunnel to the store] then update the tunnel. Tunnel is
1493 * updated because in case of resilency only delegated LSPs are recomputed and only delegated PCE can
1494 * send update message to that client.
1495 *
1496 * 1)Master can 1st get the Rpt message
1497 * a)Master adds the tunnel into core.
1498 * b)If a non-master for ingress gets Rpt message with D flag set[as delegation owner]
1499 * after master, then runs timer then update the tunnel with D flag set.
1500 * 2)Non-Master can 1st get the Rpt message
1501 * a)Non-Master runs the timer check for the tunnel then updates the tunnel with D flag set
1502 * b)Master would have got the message while the non-master running timer, hence master adds
1503 * tunnel to core
1504 *
1505 * In general always master adds the tunnel to the core
1506 * while delegated owner [master or non-master with D flag set] always updates the tunnel running timer
1507 */
1508 if (mastershipService.isLocalMaster(deviceId)) {
1509 TunnelId tId = tunnelAdded(td, tunnelState);
1510 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1511 tunnelState, new DefaultGroupId(0), tId, TunnelName.tunnelName(String.valueOf(pathNameTlv
1512 .getValue())), path, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301513
Priyanka B413fbe82016-05-26 11:44:45 +05301514 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1515 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1516 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1517 } else if (!mastershipService.isLocalMaster(deviceId) && lspObj.getDFlag()) {
1518 //Start timer then update the tunnel with D flag
1519 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1520 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301521 return;
1522 }
1523
Priyanka B413fbe82016-05-26 11:44:45 +05301524 //delegated owner will update can be a master or non-master
1525 if (lspObj.getDFlag()) {
1526 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType);
1527 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
1528 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path,
1529 annotations);
1530 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1531 }
1532
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301533 if ((syncStatus == IN_SYNC) && (lspObj.getCFlag()) && (tunnelState != tunnel.state())) {
1534 // Mark to send PCUpd msg with state known at PCE.
1535 List<Tunnel> tunnelToBeUpdateList = syncCompleteUpdateList.get(pccId.ipAddress());
1536 tunnelToBeUpdateList.add(tunnel);
1537 syncCompleteUpdateList.put(pccId.ipAddress(), tunnelToBeUpdateList);
1538 return;
1539 }
Priyanka B413fbe82016-05-26 11:44:45 +05301540 removeOrUpdatetunnel(tunnel, pccId, lspObj, providerId, syncStatus, tunnelState);
1541 return;
1542 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301543
Priyanka B413fbe82016-05-26 11:44:45 +05301544 private void removeOrUpdatetunnel(Tunnel tunnel, PccId pccId, PcepLspObject lspObj, ProviderId providerId,
1545 PcepSyncStatus syncStatus, State tunnelState) {
1546 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1547 tunnel.type(), tunnel.groupId(), providerId, tunnel.tunnelName(), tunnel.path(),
1548 (SparseAnnotations) tunnel.annotations());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301549 if (lspObj.getRFlag()) {
1550 tunnelRemoved(td);
1551 } else {
1552 if (syncStatus == IN_SYNC) {
1553 markLspDbEntryAsLatest(pccId, tunnel.tunnelId());
1554 }
1555 tunnelUpdated(td, tunnelState);
1556 }
Priyanka B413fbe82016-05-26 11:44:45 +05301557 }
1558
1559 private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
1560 DefaultTunnelDescription td, ProviderId providerId) {
1561 //Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
1562
1563 /*
1564 * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
1565 * master has added the tunnel to the store] then update the tunnel.
1566 */
1567 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1568
1569 // Thread is started after 2 seconds first time later periodically after 2 seconds to update the tunnel
1570 executor.scheduleAtFixedRate(new UpdateDelegation(td, providerId, annotations, pccId,
1571 executor), DELAY, DELAY, TimeUnit.SECONDS);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301572 }
1573
1574 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301575 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301576 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301577 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301578 * @param providerId provider id
1579 * @return path object
1580 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301581 private Path buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId) {
1582 checkNotNull(eroObj);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301583 List<Link> links = new ArrayList<Link>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301584 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301585 if (0 == llSubObj.size()) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301586 log.error("ERO in report message does not have hop information");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301587 }
1588 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1589
1590 ConnectPoint src = null;
1591 ConnectPoint dst = null;
1592 boolean isSrcSet = false;
1593 while (tlvIterator.hasNext()) {
1594 PcepValueType subObj = tlvIterator.next();
1595 switch (subObj.getType()) {
1596
1597 case IPv4SubObject.TYPE:
1598
1599 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1600 if (!isSrcSet) {
1601 IpAddress srcIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1602 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1603 isSrcSet = true;
1604 } else {
1605 IpAddress dstIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1606 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
Ray Milkey2693bda2016-01-22 16:08:14 -08001607 Link link = DefaultLink.builder()
1608 .providerId(providerId)
1609 .src(src)
1610 .dst(dst)
1611 .type(Link.Type.DIRECT)
1612 .build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301613 links.add(link);
1614 src = dst;
1615 }
1616 break;
1617 default:
1618 // the other sub objects are not required
1619 }
1620 }
1621 return new DefaultPath(providerId, links, 0, EMPTY);
1622 }
1623
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301624 @Override
1625 public void clientConnected(PccId pccId) {
1626 // TODO
1627 }
1628
1629 @Override
1630 public void clientDisconnected(PccId pccId) {
1631 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001632 }
chengfan2fff70f2015-08-24 18:20:19 -05001633
chengfan2fff70f2015-08-24 18:20:19 -05001634 @Override
1635 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1636 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1637 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1638 tunnelStatisticsMap.put(id, tunnelStatistics);
1639 }
cheng fan48e832c2015-05-29 01:54:47 +08001640 }
1641
1642 @Override
1643 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1644 return service.tunnelQueryById(tunnelId);
1645 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301646
1647 /**
1648 * Removes the entry from temporary copy of LSPDB, signifying its status as upto date.
1649 *
1650 * @param pccId the key for temporary LSPDB
1651 * @param tunnelId the tunnel id for which information is updated.
1652 */
1653 private void markLspDbEntryAsLatest(PccId pccId, TunnelId tunnelId) {
1654 checkNotNull(pccId);
1655 checkNotNull(tunnelId);
1656
1657 Map<TunnelId, Tunnel> preSyncLspDb = preSyncLspDbMap.get(pccId.ipAddress());
1658 checkNotNull(preSyncLspDb);
1659
1660 preSyncLspDb.remove(tunnelId);
1661 preSyncLspDbMap.put(pccId.ipAddress(), preSyncLspDb);
1662 }
1663
1664 /**
1665 * Sends PCInit, PCInit(R) or PCUpd messages for initiated LSPs at the end
1666 * of LSP DB sync based on actions decided while sync was in progress. Also
1667 * triggers label DB sync.
1668 *
1669 * @param pccId the key for temporary DBs storing required end of sync
1670 * actions.
1671 */
1672 private void handleEndOfSyncAction(PccId pccId) {
1673
1674 Map<TunnelId, Tunnel> preSyncLspDb = preSyncLspDbMap.get(pccId.ipAddress());
1675 checkNotNull(preSyncLspDb);
1676
1677 for (Tunnel tunnel : preSyncLspDb.values()) {
1678
1679 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1680 tunnel.src(), tunnel.dst(),
1681 tunnel.type(),
1682 tunnel.groupId(),
1683 tunnel.providerId(),
1684 tunnel.tunnelName(),
1685 tunnel.path(),
1686 (SparseAnnotations) tunnel.annotations());
1687
1688 if ((tunnel.annotations().value(PCE_INIT) == null)
1689 || (tunnel.annotations().value(PCE_INIT).equals("false"))) {
1690
1691 tunnelRemoved(td);
1692 } else {
1693 // Send PCInit msg again after global reoptimization.
1694 tunnelUpdated(td, UNSTABLE);
1695
1696 // To remove the old tunnel from store whose PLSPID is not
1697 // recognized by ingress PCC.
1698 tunnelRemoved(td);
1699 }
1700 }
1701
1702 List<Tunnel> tunnelsToBeDeletedList = syncCompleteDeleteList.get(pccId.ipAddress());
1703 checkNotNull(tunnelsToBeDeletedList);
1704 for (Tunnel tunnel: tunnelsToBeDeletedList) {
1705 releaseTunnel(tunnel);
1706 }
1707
1708 List<Tunnel> tunnelsToBeUpdatedList = syncCompleteUpdateList.get(pccId.ipAddress());
1709 checkNotNull(tunnelsToBeUpdatedList);
1710 for (Tunnel tunnel: tunnelsToBeUpdatedList) {
1711 updateTunnel(tunnel, tunnel.path());
1712 }
1713
1714 /* On end of sync, empty all temporary data structures. */
1715 preSyncLspDbMap.remove(pccId.ipAddress());
1716 syncCompleteDeleteList.remove(pccId.ipAddress());
1717 syncCompleteUpdateList.remove(pccId.ipAddress());
1718
1719 // TODO: If SR capable, send a notification to
1720 // PCE APP to start label DB sync.
1721 if (true) {
1722 pcepClientController.getClient(pccId).setLabelDbSyncStatus(IN_SYNC);
1723 }
1724 }
Priyanka B413fbe82016-05-26 11:44:45 +05301725
1726 private DeviceId getDevice(PccId pccId) {
1727 // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID.
1728 IpAddress lsrId = pccId.ipAddress();
1729 String lsrIdentifier = String.valueOf(lsrId);
1730
1731 // Find PCC deviceID from lsrId stored as annotations
1732 Iterable<Device> devices = deviceService.getAvailableDevices();
1733 for (Device dev : devices) {
1734 if (dev.annotations().value(AnnotationKeys.TYPE).equals("L3")
1735 && dev.annotations().value(LSRID).equals(lsrIdentifier)) {
1736 return dev.id();
1737 }
1738 }
1739 return null;
1740 }
1741
1742 /**
1743 * Updates the tunnel with updated tunnel annotation after a delay of two seconds and checks it till
1744 * tunnel is found.
1745 */
1746 private class UpdateDelegation implements Runnable {
1747 DefaultTunnelDescription td;
1748 ProviderId providerId;
1749 SparseAnnotations annotations;
1750 PccId pccId;
1751 ScheduledExecutorService executor;
1752
1753 /**
1754 * Creates an instance of UpdateDelegation.
1755 *
1756 * @param td tunnel description
1757 * @param providerId provider id
1758 * @param annotations tunnel annotations
1759 * @param pccId PCEP client id
1760 * @param executor service of delegated owner
1761 */
1762 public UpdateDelegation(DefaultTunnelDescription td, ProviderId providerId, SparseAnnotations annotations,
1763 PccId pccId, ScheduledExecutorService executor) {
1764 this.td = td;
1765 this.providerId = providerId;
1766 this.annotations = annotations;
1767 this.pccId = pccId;
1768 this.executor = executor;
1769 }
1770
1771 //Temporary using annotations later will use projection/network config service
1772 @Override
1773 public void run() {
1774 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(td.src(), td.dst());
1775 TunnelId tempTunnelId = null;
1776 for (Tunnel t : tunnelQueryResult) {
1777 if (t.annotations().value(LOCAL_LSP_ID) == null || t.annotations().value(PLSP_ID) == null) {
1778 continue;
1779 }
1780
1781 if (t.annotations().value(LOCAL_LSP_ID).equals(td.annotations().value(LOCAL_LSP_ID))
1782 && t.annotations().value(PLSP_ID).equals(td.annotations().value(PLSP_ID))
1783 && ((IpTunnelEndPoint) t.src()).ip().equals(pccId.id())) {
1784 tempTunnelId = t.tunnelId();
1785 break;
1786 }
1787 }
1788
1789 //If tunnel is found update the tunnel and shutdown the thread otherwise thread will be executing
1790 //periodically
1791 if (tempTunnelId != null) {
1792 Tunnel tunnel = new DefaultTunnel(providerId, td.src(), td.dst(), MPLS, new DefaultGroupId(0),
1793 tempTunnelId, td.tunnelName(), td.path(), annotations);
1794 tunnelUpdated(tunnel, td.path());
1795 executor.shutdown();
1796 try {
1797 executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
1798 } catch (InterruptedException e) {
1799 log.error("updating delegation failed");
1800 }
1801 }
1802 }
1803 }
cheng fan48e832c2015-05-29 01:54:47 +08001804}