blob: 83fd55c918828dc8ef1ea9d079e18f4745e2fa43 [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;
Priyanka Bcdf9b102016-06-07 20:01:38 +053027import org.onlab.packet.Ip4Address;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053028import org.onlab.packet.IpAddress;
chengfan2fff70f2015-08-24 18:20:19 -050029import org.onosproject.cfg.ComponentConfigService;
cheng fan48e832c2015-05-29 01:54:47 +080030import org.onosproject.core.DefaultGroupId;
Avantika-Huaweif849aab2016-06-21 22:29:15 +053031import org.onosproject.incubator.net.resource.label.LabelResourceId;
32import org.onosproject.incubator.net.tunnel.DefaultLabelStack;
cheng fan48e832c2015-05-29 01:54:47 +080033import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053034import org.onosproject.incubator.net.tunnel.DefaultTunnel;
cheng fan48e832c2015-05-29 01:54:47 +080035import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
chengfan2fff70f2015-08-24 18:20:19 -050036import org.onosproject.incubator.net.tunnel.DefaultTunnelStatistics;
Jonathan Hart51539b82015-10-29 09:53:04 -070037import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
cheng fan48e832c2015-05-29 01:54:47 +080038import org.onosproject.incubator.net.tunnel.OpticalLogicId;
39import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
40import org.onosproject.incubator.net.tunnel.Tunnel;
Avantika-Huawei56c11842016-04-28 00:56:56 +053041import org.onosproject.incubator.net.tunnel.Tunnel.State;
Priyanka B413fbe82016-05-26 11:44:45 +053042import org.onosproject.incubator.net.tunnel.TunnelAdminService;
cheng fan48e832c2015-05-29 01:54:47 +080043import org.onosproject.incubator.net.tunnel.TunnelDescription;
44import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
45import org.onosproject.incubator.net.tunnel.TunnelId;
46import org.onosproject.incubator.net.tunnel.TunnelName;
47import org.onosproject.incubator.net.tunnel.TunnelProvider;
48import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
49import org.onosproject.incubator.net.tunnel.TunnelProviderService;
chengfan2fff70f2015-08-24 18:20:19 -050050import org.onosproject.incubator.net.tunnel.TunnelService;
51import org.onosproject.incubator.net.tunnel.TunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053052import org.onosproject.mastership.MastershipService;
53import org.onosproject.net.AnnotationKeys;
cheng fan48e832c2015-05-29 01:54:47 +080054import org.onosproject.net.ConnectPoint;
55import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei56c11842016-04-28 00:56:56 +053056import org.onosproject.net.DefaultAnnotations.Builder;
cheng fan48e832c2015-05-29 01:54:47 +080057import org.onosproject.net.DefaultLink;
58import org.onosproject.net.DefaultPath;
Priyanka B413fbe82016-05-26 11:44:45 +053059import org.onosproject.net.Device;
cheng fan48e832c2015-05-29 01:54:47 +080060import org.onosproject.net.DeviceId;
61import org.onosproject.net.ElementId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053062import org.onosproject.net.IpElementId;
cheng fan48e832c2015-05-29 01:54:47 +080063import org.onosproject.net.Link;
Avantika-Huaweif849aab2016-06-21 22:29:15 +053064import org.onosproject.net.NetworkResource;
cheng fan48e832c2015-05-29 01:54:47 +080065import org.onosproject.net.Path;
66import org.onosproject.net.PortNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053067import org.onosproject.net.SparseAnnotations;
Priyanka B413fbe82016-05-26 11:44:45 +053068import org.onosproject.net.device.DeviceService;
cheng fan48e832c2015-05-29 01:54:47 +080069import org.onosproject.net.provider.AbstractProvider;
70import org.onosproject.net.provider.ProviderId;
71import org.onosproject.pcep.api.PcepController;
72import org.onosproject.pcep.api.PcepDpid;
73import org.onosproject.pcep.api.PcepHopNodeDescription;
74import org.onosproject.pcep.api.PcepOperator.OperationType;
75import org.onosproject.pcep.api.PcepTunnel;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053076import org.onosproject.pcep.api.PcepTunnel.PathState;
Jonathan Hart51539b82015-10-29 09:53:04 -070077import org.onosproject.pcep.api.PcepTunnel.PathType;
cheng fan48e832c2015-05-29 01:54:47 +080078import org.onosproject.pcep.api.PcepTunnelListener;
chengfan2fff70f2015-08-24 18:20:19 -050079import org.onosproject.pcep.api.PcepTunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053080import org.onosproject.pcep.controller.LspKey;
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +053081import org.onosproject.pcep.controller.LspType;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053082import org.onosproject.pcep.controller.PccId;
83import org.onosproject.pcep.controller.PcepClient;
84import org.onosproject.pcep.controller.PcepClientController;
85import org.onosproject.pcep.controller.PcepClientListener;
86import org.onosproject.pcep.controller.PcepEventListener;
Priyanka B259847d2016-06-03 21:28:35 +053087import org.onosproject.pcep.controller.PcepLspStatus;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053088import org.onosproject.pcep.controller.PcepLspSyncAction;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +053089import org.onosproject.pcep.controller.SrpIdGenerators;
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +053090import org.onosproject.pcep.controller.PcepSyncStatus;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053091import org.onosproject.pcepio.exceptions.PcepParseException;
92import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
93import org.onosproject.pcepio.protocol.PcepAttribute;
94import org.onosproject.pcepio.protocol.PcepBandwidthObject;
95import org.onosproject.pcepio.protocol.PcepEndPointsObject;
96import org.onosproject.pcepio.protocol.PcepEroObject;
97import org.onosproject.pcepio.protocol.PcepInitiateMsg;
98import org.onosproject.pcepio.protocol.PcepLspObject;
99import org.onosproject.pcepio.protocol.PcepMessage;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530100import org.onosproject.pcepio.protocol.PcepMetricObject;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530101import org.onosproject.pcepio.protocol.PcepMsgPath;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530102import org.onosproject.pcepio.protocol.PcepNai;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530103import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530104import org.onosproject.pcepio.protocol.PcepSrpObject;
105import org.onosproject.pcepio.protocol.PcepStateReport;
106import org.onosproject.pcepio.protocol.PcepUpdateMsg;
107import org.onosproject.pcepio.protocol.PcepUpdateRequest;
108import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530109import org.onosproject.pcepio.types.PathSetupTypeTlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530110import org.onosproject.pcepio.types.PcepNaiIpv4Adjacency;
111import org.onosproject.pcepio.types.PcepNaiIpv4NodeId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530112import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530113import org.onosproject.pcepio.types.SrEroSubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530114import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530115import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -0500116import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -0700117import org.osgi.service.component.annotations.Modified;
118import org.slf4j.Logger;
119
120import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530121import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530122import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700123import java.util.Collections;
124import java.util.Dictionary;
125import java.util.HashMap;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530126import java.util.Iterator;
Jonathan Hart51539b82015-10-29 09:53:04 -0700127import java.util.LinkedList;
128import java.util.List;
129import java.util.ListIterator;
130import java.util.Optional;
Priyanka B413fbe82016-05-26 11:44:45 +0530131import java.util.concurrent.Executors;
132import java.util.concurrent.ScheduledExecutorService;
133import java.util.concurrent.TimeUnit;
Jonathan Hart51539b82015-10-29 09:53:04 -0700134
135import static com.google.common.base.Preconditions.checkNotNull;
136import static com.google.common.base.Strings.isNullOrEmpty;
137import static org.onlab.util.Tools.get;
Priyanka B4c3cef02016-06-14 20:27:53 +0530138import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530139import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700140import static org.onosproject.net.DefaultAnnotations.EMPTY;
141import static org.onosproject.net.DeviceId.deviceId;
142import static org.onosproject.net.PortNumber.portNumber;
143import static org.onosproject.pcep.api.PcepDpid.uri;
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530144import static org.onosproject.pcep.controller.LspType.WITH_SIGNALLING;
145import static org.onosproject.pcep.controller.LspType.SR_WITHOUT_SIGNALLING;
146import static org.onosproject.pcep.controller.LspType.WITHOUT_SIGNALLING_AND_WITHOUT_SR;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530147import static org.onosproject.pcep.controller.PcepAnnotationKeys.BANDWIDTH;
148import static org.onosproject.pcep.controller.PcepAnnotationKeys.LOCAL_LSP_ID;
149import static org.onosproject.pcep.controller.PcepAnnotationKeys.LSP_SIG_TYPE;
150import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCC_TUNNEL_ID;
Priyanka B4c3cef02016-06-14 20:27:53 +0530151import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCE_INIT;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530152import static org.onosproject.pcep.controller.PcepAnnotationKeys.PLSP_ID;
153import static org.onosproject.pcep.controller.PcepAnnotationKeys.DELEGATE;
154import static org.onosproject.pcep.controller.PcepAnnotationKeys.COST_TYPE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530155import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
156import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
157import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
158import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530159import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530160import static org.onosproject.pcep.controller.PcepLspSyncAction.REMOVE;
161import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_UPDATE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530162import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.IGP_METRIC;
163import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.TE_METRIC;
Jonathan Hart51539b82015-10-29 09:53:04 -0700164import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800165
166/**
167 * Provider which uses an PCEP controller to detect, update, create network
168 * tunnels.
169 */
170@Component(immediate = true)
171@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530172public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800173
174 private static final Logger log = getLogger(PcepTunnelProvider.class);
175 private static final long MAX_BANDWIDTH = 99999744;
176 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800177 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530178 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
Priyanka B413fbe82016-05-26 11:44:45 +0530179 public static final long IDENTIFIER_SET = 0x100000000L;
180 public static final long SET = 0xFFFFFFFFL;
181 private static final int DELAY = 2;
182 private static final int WAIT_TIME = 5;
183 public static final String LSRID = "lsrId";
cheng fan48e832c2015-05-29 01:54:47 +0800184
chengfan2fff70f2015-08-24 18:20:19 -0500185 static final int POLL_INTERVAL = 10;
186 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
187 label = "Frequency (in seconds) for polling tunnel statistics")
188 private int tunnelStatsPollFrequency = POLL_INTERVAL;
189
cheng fan48e832c2015-05-29 01:54:47 +0800190 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
193 protected TunnelProviderRegistry tunnelProviderRegistry;
194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
196 protected PcepController controller;
197
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
199 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500200
201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
202 protected TunnelService tunnelService;
203
204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
205 protected ComponentConfigService cfgService;
206
Priyanka B413fbe82016-05-26 11:44:45 +0530207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
208 protected TunnelAdminService tunnelAdminService;
209
210 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
211 protected MastershipService mastershipService;
212
213 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
214 protected DeviceService deviceService;
215
cheng fan48e832c2015-05-29 01:54:47 +0800216 TunnelProviderService service;
217
218 HashMap<String, TunnelId> tunnelMap = new HashMap<String, TunnelId>();
chengfan2fff70f2015-08-24 18:20:19 -0500219 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400220 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800221
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530222 private InnerTunnelProvider listener = new InnerTunnelProvider();
223
Jonathan Hart51539b82015-10-29 09:53:04 -0700224 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530225 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800226
227 /**
228 * Creates a Tunnel provider.
229 */
230 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530231 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800232 }
233
234 @Activate
235 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500236 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800237 service = tunnelProviderRegistry.register(this);
238 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530239 pcepClientController.addListener(listener);
240 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500241 tunnelService.queryAllTunnels().forEach(tunnel -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700242 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500243 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
244 tsc.start();
245 collectors.put(tunnel.tunnelId().id(), tsc);
246
247 });
248
cheng fan48e832c2015-05-29 01:54:47 +0800249 log.info("Started");
250 }
251
252 @Deactivate
253 public void deactivate() {
254 tunnelProviderRegistry.unregister(this);
255 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500256 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530257 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800258 log.info("Stopped");
259 }
260
chengfan2fff70f2015-08-24 18:20:19 -0500261 @Modified
262 public void modified(ComponentContext context) {
263 Dictionary<?, ?> properties = context.getProperties();
264 int newTunnelStatsPollFrequency;
265 try {
266 String s = get(properties, "tunnelStatsPollFrequency");
267 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
268
269 } catch (NumberFormatException | ClassCastException e) {
270 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
271 }
272
273 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
274 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
275 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
276 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
277 }
278
279 }
280
cheng fan48e832c2015-05-29 01:54:47 +0800281 @Override
282 public void setupTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530283 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530284 log.error("Tunnel Type MPLS is only supported");
285 return;
286 }
cheng fan48e832c2015-05-29 01:54:47 +0800287
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530288 // check for tunnel end points
289 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
290 log.error("Tunnel source or destination is not valid");
291 return;
292 }
293
294 // Get the pcc client
295 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
296
297 if (!(pc instanceof PcepClient)) {
298 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500299 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530300 return;
301 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530302
303 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
Priyanka B413fbe82016-05-26 11:44:45 +0530304 //Only master will initiate setup tunnel
305 if (pc.capability().pcInstantiationCapability() && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530306 pcepSetupTunnel(tunnel, path, pc);
307 }
cheng fan48e832c2015-05-29 01:54:47 +0800308 }
309
310 @Override
311 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800312
Priyanka B4c3cef02016-06-14 20:27:53 +0530313 //TODO: tunnel which is passed doesn't have tunnelID
Avantika-Huawei56c11842016-04-28 00:56:56 +0530314 if (tunnel.annotations().value(PLSP_ID) != null) {
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +0530315 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITHOUT_SIGNALLING_AND_WITHOUT_SR) {
316 // For CR LSPs, BGP flow provider will send update message after pushing labels.
317 updateTunnel(tunnel, path);
318 }
Avantika-Huawei56c11842016-04-28 00:56:56 +0530319 return;
320 }
321
322 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530323 log.error("Tunnel Type MPLS is only supported");
324 return;
325 }
326
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530327 // check for tunnel end points
328 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
329 log.error("Tunnel source or destination is not valid");
330 return;
331 }
332
Priyanka Bcdf9b102016-06-07 20:01:38 +0530333 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530334
335 if (!(pc instanceof PcepClient)) {
Priyanka B4c3cef02016-06-14 20:27:53 +0530336 log.error("There is no PCC connected with this device {}"
337 + srcElement.toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530338 return;
339 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530340
Priyanka B413fbe82016-05-26 11:44:45 +0530341 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
342 //Only master will initiate setup tunnel
343 if (pc.capability().pcInstantiationCapability()
344 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530345 pcepSetupTunnel(tunnel, path, pc);
346 }
cheng fan48e832c2015-05-29 01:54:47 +0800347 }
348
349 @Override
350 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800351
Avantika-Huawei56c11842016-04-28 00:56:56 +0530352 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530353 log.error("Tunnel Type MPLS is only supported");
354 return;
355 }
356
357 // check for tunnel end points
358 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
359 log.error("Tunnel source or destination is not valid");
360 return;
361 }
362
363 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
364
365 if (!(pc instanceof PcepClient)) {
366 log.error("There is no PCC connected with ip addresss {}"
367 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
368 return;
369 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530370
Priyanka B413fbe82016-05-26 11:44:45 +0530371 //Only master will release tunnel
372 if (pc.capability().pcInstantiationCapability()
373 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530374 pcepReleaseTunnel(tunnel, pc);
375 }
cheng fan48e832c2015-05-29 01:54:47 +0800376 }
377
378 @Override
379 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530380 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530381 log.error("Tunnel Type MPLS is only supported");
382 return;
383 }
cheng fan48e832c2015-05-29 01:54:47 +0800384
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530385 if (!(srcElement instanceof IpElementId)) {
386 log.error("Element id is not valid");
387 return;
388 }
389
390 // check for tunnel end points
391 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
392 log.error("Tunnel source or destination is not valid");
393 return;
394 }
395
396 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
397
398 if (!(pc instanceof PcepClient)) {
399 log.error("There is no PCC connected with ip addresss {}"
400 + ((IpElementId) srcElement).ipAddress().toString());
401 return;
402 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530403
Priyanka B413fbe82016-05-26 11:44:45 +0530404 //Only master will release tunnel
405 if (pc.capability().pcInstantiationCapability()
406 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530407 pcepReleaseTunnel(tunnel, pc);
408 }
cheng fan48e832c2015-05-29 01:54:47 +0800409 }
410
411 @Override
412 public void updateTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530413 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530414 log.error("Tunnel Type MPLS is only supported");
415 return;
416 }
cheng fan48e832c2015-05-29 01:54:47 +0800417
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530418 // check for tunnel end points
419 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
420 log.error("Tunnel source or destination is not valid");
421 return;
422 }
423
Priyanka B4c3cef02016-06-14 20:27:53 +0530424 //To get new tunnel ID (modified tunnel ID)
425 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
426 for (Tunnel t : tunnels) {
427 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
428 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
429 tunnel.dst(), tunnel.type(),
430 t.state(), tunnel.groupId(),
431 t.tunnelId(),
432 tunnel.tunnelName(),
433 tunnel.path(),
434 tunnel.resource(),
435 tunnel.annotations());
436 break;
437 }
438 }
439
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530440 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
441
442 if (!(pc instanceof PcepClient)) {
443 log.error("There is no PCC connected with ip addresss {}"
444 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
445 return;
446 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530447
Priyanka B413fbe82016-05-26 11:44:45 +0530448 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
449 // LSP].If annotation is null D flag is not set else it is set.
Priyanka B4c3cef02016-06-14 20:27:53 +0530450 Short localLspId = 0;
451 for (Tunnel t : tunnels) {
452 if (!t.tunnelId().equals(tunnel.tunnelId()) && t.tunnelName().equals(tunnel.tunnelName())) {
453 localLspId = Short.valueOf(t.annotations().value(LOCAL_LSP_ID));
454 }
455 }
456
457 if (localLspId == 0) {
458 log.error("Local LSP ID for old tunnel not found");
459 return;
460 }
461
462 //PCInitiate tunnels are always have D flag set, else check for tunnels who are delegated via LspKey
463 if (pc.capability().statefulPceCapability()) {
464 if (tunnel.annotations().value(PCE_INIT) != null && tunnel.annotations().value(PCE_INIT).equals("true")) {
465 pcepUpdateTunnel(tunnel, path, pc);
466 } else if (pc.delegationInfo(
467 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)),
468 localLspId.shortValue())) != null) {
469 pcepUpdateTunnel(tunnel, path, pc);
470 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530471 }
cheng fan48e832c2015-05-29 01:54:47 +0800472 }
473
474 @Override
475 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800476
Avantika-Huawei56c11842016-04-28 00:56:56 +0530477 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530478 log.error("Tunnel Type MPLS is only supported");
479 return;
480 }
481
482 if (!(srcElement instanceof IpElementId)) {
483 log.error("Element id is not valid");
484 return;
485 }
486
487 // check for tunnel end points
488 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
489 log.error("Tunnel source or destination is not valid");
490 return;
491 }
492
493 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
494
495 if (!(pc instanceof PcepClient)) {
496 log.error("There is no PCC connected with ip addresss {}"
497 + ((IpElementId) srcElement).ipAddress().toString());
498 return;
499 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530500
Priyanka B413fbe82016-05-26 11:44:45 +0530501 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
502 // LSP].If annotation is null D flag is not set else it is set.
503 if (pc.capability().statefulPceCapability()
504 && pc.delegationInfo(
505 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
506 .annotations().value(LOCAL_LSP_ID)))) != null) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530507 pcepUpdateTunnel(tunnel, path, pc);
508 }
cheng fan48e832c2015-05-29 01:54:47 +0800509 }
510
511 @Override
512 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530513 return handleTunnelAdded(tunnel, null);
514 }
515
516 public TunnelId tunnelAdded(TunnelDescription tunnel, State tunnelState) {
517 return handleTunnelAdded(tunnel, tunnelState);
518 }
519
520 private TunnelId handleTunnelAdded(TunnelDescription tunnel, State tunnelState) {
521
522 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700523 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530524
525 if (tunnelState == null) {
526 return service.tunnelAdded(tunnel);
527 } else {
528 return service.tunnelAdded(tunnel, tunnelState);
529 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530530 }
cheng fan48e832c2015-05-29 01:54:47 +0800531
Avantika-Huawei56c11842016-04-28 00:56:56 +0530532 long bandwidth = Long.parseLong(tunnel.annotations().value(BANDWIDTH));
cheng fan48e832c2015-05-29 01:54:47 +0800533
534 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800535 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800536 return null;
537 }
538
539 // endpoints
540 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
541 .src();
542 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
543 // devices
544 DeviceId srcId = (DeviceId) src.elementId().get();
545 DeviceId dstId = (DeviceId) dst.elementId().get();
546
547 // ports
548 long srcPort = src.portNumber().get().toLong();
549 long dstPort = dst.portNumber().get().toLong();
550
551 // type
552 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800553 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800554 return null;
555 }
556
557 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
558 dstPort, bandwidth,
559 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530560 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800561
562 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
563 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
564 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
565
566 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
567 return tunnelId;
568 }
569
Priyanka B413fbe82016-05-26 11:44:45 +0530570 private void tunnelUpdated(Tunnel tunnel, Path path) {
571 handleTunnelUpdate(tunnel, path);
572 }
573
574 //Handles tunnel updated using tunnel admin service[specially to update annotations].
575 private void handleTunnelUpdate(Tunnel tunnel, Path path) {
576
577 if (tunnel.type() == MPLS) {
578 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.tunnelId());
579
580 tunnelAdminService.updateTunnel(tunnel, path);
581
582 return;
583 }
584
585 Tunnel tunnelOld = tunnelQueryById(tunnel.tunnelId());
586 if (tunnelOld.type() != Tunnel.Type.VLAN) {
587 error("Illegal tunnel type. Only support VLAN tunnel update.");
588 return;
589 }
590
591 long bandwidth = Long
592 .parseLong(tunnel.annotations().value("bandwidth"));
593 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
594 error("Update failed, invalid bandwidth.");
595 return;
596 }
597 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
598
599 checkNotNull(pcepTunnelId, "Invalid tunnel id");
600 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
601 error("Update failed,maybe invalid bandwidth.");
602 return;
603 }
604 tunnelAdminService.updateTunnel(tunnel, path);
605 }
606
cheng fan48e832c2015-05-29 01:54:47 +0800607 @Override
608 public void tunnelRemoved(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530609 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700610 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530611 service.tunnelRemoved(tunnel);
612 }
613
cheng fan48e832c2015-05-29 01:54:47 +0800614 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
615 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
616 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800617 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800618 return;
619 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700620 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800621 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800622 if (!controller.deleteTunnel(pcepTunnelId)) {
623 error("Delete tunnel failed, Maybe some devices have been disconnected.");
624 return;
cheng fan48e832c2015-05-29 01:54:47 +0800625 }
626 tunnelMap.remove(pcepTunnelId);
627 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800628 }
629
630 @Override
631 public void tunnelUpdated(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530632 handleTunnelUpdate(tunnel, null);
633 }
634
635 public void tunnelUpdated(TunnelDescription tunnel, State tunnelState) {
636 handleTunnelUpdate(tunnel, tunnelState);
637 }
638
639 private void handleTunnelUpdate(TunnelDescription tunnel, State tunnelState) {
640 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700641 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530642
643 if (tunnelState == null) {
644 service.tunnelUpdated(tunnel);
645 } else {
646 service.tunnelUpdated(tunnel, tunnelState);
647 }
648 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530649 }
cheng fan48e832c2015-05-29 01:54:47 +0800650
651 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
652 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800653 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800654 return;
655 }
cheng fan7716ec92015-05-31 01:53:19 +0800656 long bandwidth = Long
657 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800658 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800659 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800660 return;
661 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700662 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800663
664 checkNotNull(pcepTunnelId, "Invalid tunnel id");
665 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
666
cheng fan7716ec92015-05-31 01:53:19 +0800667 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800668 return;
669
670 }
671 service.tunnelUpdated(tunnel);
672 }
673
cheng fan7716ec92015-05-31 01:53:19 +0800674 private void error(String info) {
675 System.err.println(info);
676 }
677
cheng fan48e832c2015-05-29 01:54:47 +0800678 // Short-hand for creating a connection point.
679 private ConnectPoint connectPoint(PcepDpid id, long port) {
680 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
681 }
682
683 // Short-hand for creating a link.
684 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800685 return DefaultLink.builder()
686 .providerId(id())
687 .src(connectPoint(src, sp))
688 .dst(connectPoint(dst, dp))
689 .type(Link.Type.TUNNEL)
690 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800691 }
692
693 // Creates a path that leads through the given devices.
694 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700695 PathType pathtype, PathState pathState) {
cheng fan48e832c2015-05-29 01:54:47 +0800696 if (hopList == null || hopList.size() == 0) {
697 return null;
698 }
699 List<Link> links = new ArrayList<>();
700 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
701 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
702 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
703 .get(i + 1).getPortNum()));
704 }
705
706 int hopNum = hopList.size() - 2;
707 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
708 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800709 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800710 .set("pathType", String.valueOf(pathtype)).build();
711 return new DefaultPath(id(), links, hopNum, extendAnnotations);
712 }
713
714 // convert the path description to a string.
715 public String pathToString(List<Link> links) {
716 StringBuilder builder = new StringBuilder();
717 builder.append("{");
718 for (Link link : links) {
719 builder.append("(Device:" + link.src().deviceId() + " Port:"
720 + link.src().port().toLong());
721 builder.append(" Device:" + link.dst().deviceId() + " Port:"
722 + link.dst().port().toLong());
723 builder.append(")");
724 }
725 builder.append("}");
726 return builder.toString();
727 }
728
729 // build a TunnelDescription.
730 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
731 TunnelId tunnelId) {
732 TunnelEndPoint srcPoint = null;
733 TunnelEndPoint dstPoint = null;
734 Tunnel.Type tunnelType = null;
735 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
736
737 // add path after codes of tunnel's path merged
738 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800739 pcepTunnel.getPathType(),
740 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800741
742 OpticalTunnelEndPoint.Type endPointType = null;
743 switch (pcepTunnel.type()) {
744 case OCH:
745 tunnelType = Tunnel.Type.OCH;
746 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
747 break;
748
749 case OTN:
750 tunnelType = Tunnel.Type.ODUK;
751 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
752 break;
753
754 case UNI:
755 tunnelType = Tunnel.Type.VLAN;
756 endPointType = null;
757 break;
758
759 default:
760 break;
761 }
762 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
763 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
764 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
765 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
766
767 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
768 Optional.of(srcPort), null,
769 endPointType,
770 OpticalLogicId.logicId(0),
771 true);
772 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
773 Optional.of(dstPort), null,
774 endPointType,
775 OpticalLogicId.logicId(0),
776 true);
777
778 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800779 DefaultAnnotations annotations = DefaultAnnotations
780 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800781 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800782 .set("bandwidth",
783 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800784 .set("index", String.valueOf(pcepTunnel.id())).build();
785
cheng fan48e832c2015-05-29 01:54:47 +0800786 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
787 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800788 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800789 DefaultAnnotations extendAnnotations = DefaultAnnotations
790 .builder()
791 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800792 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800793 annotations = DefaultAnnotations.merge(annotations,
794 extendAnnotations);
795
796 }
797 TunnelDescription tunnel = new DefaultTunnelDescription(
798 tunnelId,
799 srcPoint,
800 dstPoint,
801 tunnelType,
802 new DefaultGroupId(
803 0),
804 id(), name,
805 path,
806 annotations);
807 return tunnel;
cheng fan48e832c2015-05-29 01:54:47 +0800808 }
809
810 /**
811 * Get the tunnelID according to the tunnel key.
812 *
813 * @param tunnelKey tunnel key
814 * @return corresponding tunnel id of the a tunnel key.
815 */
816 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800817 for (String key : tunnelMap.keySet()) {
818 if (key.equals(tunnelKey)) {
819 return tunnelMap.get(key);
820 }
821 }
822 return null;
823 }
824
825 /**
826 * Get the tunnel key according to the tunnelID.
827 *
828 * @param tunnelId tunnel id
829 * @return corresponding a tunnel key of the tunnel id.
830 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700831 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800832 for (String key : tunnelMap.keySet()) {
833 if (tunnelMap.get(key).id() == tunnelId.id()) {
834 return key;
835 }
836 }
837 return null;
838
839 }
840
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530841 /**
chengfan2fff70f2015-08-24 18:20:19 -0500842 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
843 *
844 * @param statistics statistics data from a PCEP tunnel
845 * @return TunnelStatistics
846 */
847 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
848 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
849 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
850 .setPacketLossRatio(statistics.packetLossRate())
851 .setFlowDelay(statistics.flowDelay())
852 .setAlarms(statistics.alarms())
853 .build();
854 return tunnelStatistics;
855 }
856 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530857 * Creates list of hops for ERO object from Path.
858 *
859 * @param path network path
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530860 * @return list of ERO subobjects
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530861 */
862 private LinkedList<PcepValueType> createPcepPath(Path path) {
863 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
864 List<Link> listLink = path.links();
865 ConnectPoint source = null;
866 ConnectPoint destination = null;
867 IpAddress ipDstAddress = null;
868 IpAddress ipSrcAddress = null;
869 PcepValueType subObj = null;
Priyanka Bcdf9b102016-06-07 20:01:38 +0530870 long portNo;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530871
872 for (Link link : listLink) {
873 source = link.src();
874 if (!(source.equals(destination))) {
875 //set IPv4SubObject for ERO object
Priyanka Bcdf9b102016-06-07 20:01:38 +0530876 portNo = source.port().toLong();
877 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
878 ipSrcAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530879 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
880 llSubObjects.add(subObj);
881 }
882
883 destination = link.dst();
Priyanka Bcdf9b102016-06-07 20:01:38 +0530884 portNo = destination.port().toLong();
885 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
886 ipDstAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530887 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
888 llSubObjects.add(subObj);
889 }
Priyanka Bcdf9b102016-06-07 20:01:38 +0530890
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530891 return llSubObjects;
892 }
893
894 /**
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530895 * Creates label stack for ERO object from network resource.
896 *
897 * @param labelStack
898 * @param path (hop list)
899 * @return list of ERO subobjects
900 */
901 private LinkedList<PcepValueType> createPcepLabelStack(DefaultLabelStack labelStack, Path path) {
902 checkNotNull(labelStack);
903
904 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
905 Iterator<Link> links = path.links().iterator();
906 LabelResourceId label = null;
907 Link link = null;
908 PcepValueType subObj = null;
909 PcepNai nai = null;
910 Device dstNode = null;
911 long srcPortNo, dstPortNo;
912
913 ListIterator<LabelResourceId> labelListIterator = labelStack.labelResources().listIterator();
914 while (labelListIterator.hasNext()) {
915 label = labelListIterator.next();
916 link = links.next();
917
918 srcPortNo = link.src().port().toLong();
919 srcPortNo = ((srcPortNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? srcPortNo & SET : srcPortNo;
920
921 dstPortNo = link.dst().port().toLong();
922 dstPortNo = ((dstPortNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? dstPortNo & SET : dstPortNo;
923
924 nai = new PcepNaiIpv4Adjacency((int) srcPortNo, (int) dstPortNo);
925 subObj = new SrEroSubObject(PcepNaiIpv4Adjacency.ST_TYPE, false, false, false, true, (int) label.labelId(),
926 nai);
927 llSubObjects.add(subObj);
928
929 dstNode = deviceService.getDevice(link.dst().deviceId());
930 nai = new PcepNaiIpv4NodeId(Ip4Address.valueOf(dstNode.annotations().value(LSRID)).toInt());
931
932 if (!labelListIterator.hasNext()) {
933 log.error("Malformed label stack.");
934 }
935 label = labelListIterator.next();
936 subObj = new SrEroSubObject(PcepNaiIpv4NodeId.ST_TYPE, false, false, false, true, (int) label.labelId(),
937 nai);
938 llSubObjects.add(subObj);
939 }
940 return llSubObjects;
941 }
942
943 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530944 * Creates PcInitiated lsp request list for setup tunnel.
945 *
946 * @param tunnel mpls tunnel
947 * @param path network path
948 * @param pc pcep client
949 * @param srpId unique id for pcep message
950 * @return list of PcInitiatedLspRequest
951 * @throws PcepParseException while building pcep objects fails
952 */
953 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
954 PcepClient pc, int srpId)
955 throws PcepParseException {
956 PcepValueType tlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530957 LinkedList<PcepValueType> llSubObjects = null;
958
959 NetworkResource labelStack = tunnel.resource();
960 if (labelStack != null && labelStack instanceof DefaultLabelStack) {
961 llSubObjects = createPcepLabelStack((DefaultLabelStack) labelStack, path);
962 } else {
963 llSubObjects = createPcepPath(path);
964 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530965
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700966 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530967 log.error("There is no link information to create tunnel");
968 return null;
969 }
970
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530971 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530972
973 // set PathSetupTypeTlv of SRP object
974 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)).type());
975 llOptionalTlv.add(tlv);
976
977 // build SRP object
978 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
979 .setOptionalTlv(llOptionalTlv).build();
980
981 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530982 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530983
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530984 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530985 short localLspId = 0;
986 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
987 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
988 if (localLspIdString != null) {
989 localLspId = Short.valueOf(localLspIdString);
990 }
991 }
992
Priyanka B4c3cef02016-06-14 20:27:53 +0530993 tunnel.annotations().value(LSP_SIG_TYPE);
Avantika-Huawei56c11842016-04-28 00:56:56 +0530994 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
995 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
996 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530997 llOptionalTlv.add(tlv);
998 //set SymbolicPathNameTlv of LSP object
999 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1000 llOptionalTlv.add(tlv);
1001
1002 //build LSP object
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301003 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setDFlag(true).setOFlag((byte) 0)
1004 .setPlspId(0).setOptionalTlv(llOptionalTlv).build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301005
1006 //build ENDPOINTS object
1007 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
1008 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
1009 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
1010 .setPFlag(true).build();
1011
1012 //build ERO object
1013 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
1014
Priyanka B4c3cef02016-06-14 20:27:53 +05301015 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301016 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301017 iBandwidth = Float.floatToIntBits(Float.parseFloat(tunnel.annotations().value(BANDWIDTH)));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301018 }
1019 // build bandwidth object
1020 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1021 // build pcep attribute
1022 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1023
1024 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1025 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
1026 .setPcepAttribute(pcepAttribute).build();
1027 llPcInitiatedLspRequestList.add(initiateLspRequest);
1028 return llPcInitiatedLspRequestList;
1029 }
1030
1031 /**
1032 * To send initiate tunnel message to pcc.
1033 *
1034 * @param tunnel mpls tunnel info
1035 * @param path explicit route for the tunnel
1036 * @param pc pcep client to send message
1037 */
1038 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1039 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301040 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1041 log.error("Setup tunnel has failed as LSP DB sync is not finished");
1042 return;
1043 }
1044
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301045 int srpId = SrpIdGenerators.create();
Priyanka B4c3cef02016-06-14 20:27:53 +05301046 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
1047 for (Tunnel t : tunnels) {
1048 if (t.tunnelName().equals(tunnel.tunnelName())) {
1049 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
1050 tunnel.dst(), tunnel.type(),
1051 t.state(), tunnel.groupId(),
1052 t.tunnelId(),
1053 tunnel.tunnelName(),
1054 tunnel.path(),
1055 tunnel.resource(),
1056 tunnel.annotations());
1057 break;
1058 }
1059 }
1060
1061 if (tunnel.tunnelId() == null) {
1062 log.error("Tunnel ID not found");
1063 return;
1064 }
1065
Avantika-Huawei56c11842016-04-28 00:56:56 +05301066 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301067
Jonathan Hart51539b82015-10-29 09:53:04 -07001068 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301069
1070 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
1071 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001072 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301073 log.error("Failed to create PcInitiatedLspRequestList");
1074 return;
1075 }
1076
1077 //build PCInitiate message
1078 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1079 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
1080 .build();
1081
1082 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1083
Jonathan Hart51539b82015-10-29 09:53:04 -07001084 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301085 } catch (PcepParseException e) {
1086 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
1087 }
1088 }
1089
1090 /**
1091 * To send Release tunnel message to pcc.
1092 *
1093 * @param tunnel mpls tunnel info
1094 * @param pc pcep client to send message
1095 */
1096 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
1097 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301098 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1099 log.error("Release tunnel has failed as LSP DB sync is not finished");
1100 return;
1101 }
1102
Avantika-Huawei56c11842016-04-28 00:56:56 +05301103 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001104 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301105 int srpId = SrpIdGenerators.create();
1106 TunnelId tunnelId = tunnel.tunnelId();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301107
1108 PcepValueType tlv;
1109 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301110
1111 // set PathSetupTypeTlv of SRP object
1112 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
1113 .type());
1114 llOptionalTlv.add(tlv);
1115
1116 // build SRP object
1117 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
1118 .setOptionalTlv(llOptionalTlv).build();
1119
1120 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301121 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
1122
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301123 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1124 llOptionalTlv.add(tlv);
Priyanka B4c3cef02016-06-14 20:27:53 +05301125
1126 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1127 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301128 String pLspIdString = tunnel.annotations().value(PLSP_ID);
1129
Priyanka B4c3cef02016-06-14 20:27:53 +05301130 short localLspId = 0;
1131 short pccTunnelId = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301132 int plspId = 0;
Priyanka B4c3cef02016-06-14 20:27:53 +05301133
1134 if (localLspIdString != null) {
1135 localLspId = Short.valueOf(localLspIdString);
1136 }
1137
1138 if (pccTunnelIdString != null) {
1139 pccTunnelId = Short.valueOf(pccTunnelIdString);
1140 }
1141
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301142 if (pLspIdString != null) {
1143 plspId = Integer.valueOf(pLspIdString);
1144 }
1145
Priyanka B4c3cef02016-06-14 20:27:53 +05301146 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1147 .ip().getIp4Address().toInt()),
1148 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1149 .getIp4Address().toInt()));
1150 llOptionalTlv.add(tlv);
1151
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301152 // build lsp object, set r flag as false to delete the tunnel
1153 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
1154 .setOptionalTlv(llOptionalTlv).build();
1155
1156 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1157 .setLspObject(lspobj).build();
1158
1159 llPcInitiatedLspRequestList.add(releaseLspRequest);
1160
1161 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1162 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1163
1164 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1165
Jonathan Hart51539b82015-10-29 09:53:04 -07001166 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301167 } catch (PcepParseException e) {
1168 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1169 }
1170 }
1171
1172 /**
1173 * To send Update tunnel request message to pcc.
1174 *
1175 * @param tunnel mpls tunnel info
1176 * @param path explicit route for the tunnel
1177 * @param pc pcep client to send message
1178 */
1179 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1180 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301181 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001182 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301183 int srpId = SrpIdGenerators.create();
1184 TunnelId tunnelId = tunnel.tunnelId();
1185 PcepValueType tlv;
1186 int plspId = 0;
1187
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301188 LinkedList<PcepValueType> llSubObjects = null;
1189 NetworkResource labelStack = tunnel.resource();
1190 if (labelStack != null && labelStack instanceof DefaultLabelStack) {
1191 llSubObjects = createPcepLabelStack((DefaultLabelStack) labelStack, path);
1192 } else {
1193 llSubObjects = createPcepPath(path);
1194 }
1195
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301196 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
1197 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
1198
Avantika-Huawei56c11842016-04-28 00:56:56 +05301199 // set PathSetupTypeTlv of SRP object
1200 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
1201 tlv = new PathSetupTypeTlv(lspSigType.type());
1202 llOptionalTlv.add(tlv);
1203
1204 // build SRP object
1205 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
1206 .setOptionalTlv(llOptionalTlv).build();
1207
1208 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301209
Avantika-Huawei56c11842016-04-28 00:56:56 +05301210 if (lspSigType != WITH_SIGNALLING) {
1211 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1212 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1213 short localLspId = 0;
1214 short pccTunnelId = 0;
1215
1216 if (localLspIdString != null) {
1217 localLspId = Short.valueOf(localLspIdString);
1218 }
1219
1220 if (pccTunnelIdString != null) {
1221 pccTunnelId = Short.valueOf(pccTunnelIdString);
1222 }
1223
1224 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1225 .ip().getIp4Address().toInt()),
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301226 localLspId, pccTunnelId,
1227 ((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt(),
1228 (((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt()));
Avantika-Huawei56c11842016-04-28 00:56:56 +05301229 llOptionalTlv.add(tlv);
1230 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301231
1232 if (tunnel.tunnelName().value() != null) {
1233 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1234 llOptionalTlv.add(tlv);
1235 }
1236
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301237 boolean delegated = (tunnel.annotations().value(DELEGATE) == null) ? false
1238 : Boolean.valueOf(tunnel.annotations()
1239 .value(DELEGATE));
1240 boolean initiated = (tunnel.annotations().value(PCE_INIT) == null) ? false
1241 : Boolean.valueOf(tunnel.annotations()
1242 .value(PCE_INIT));
1243
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301244 // build lsp object
Priyanka B4c3cef02016-06-14 20:27:53 +05301245 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true)
1246 .setPlspId(Integer.valueOf(tunnel.annotations().value(PLSP_ID)))
Avantika-Huawei3c2d3eb2016-06-22 09:34:00 +05301247 .setDFlag(delegated)
1248 .setCFlag(initiated)
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301249 .setOptionalTlv(llOptionalTlv).build();
1250 // build ero object
1251 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
1252
Priyanka B4c3cef02016-06-14 20:27:53 +05301253 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301254 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301255 iBandwidth = Float.parseFloat(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301256 }
1257 // build bandwidth object
1258 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1259 // build pcep attribute
1260 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1261 // build pcep msg path
1262 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1263 .build();
1264
1265 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1266 .setLspObject(lspobj).setMsgPath(msgPath).build();
1267
1268 llUpdateRequestList.add(updateRequest);
1269
1270 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1271
1272 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001273 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301274 } catch (PcepParseException e) {
1275 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1276 }
1277 }
1278
chengfan2fff70f2015-08-24 18:20:19 -05001279
1280
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301281 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001282
1283 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001284 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001285 TunnelDescription tunnel = null;
1286 // instance and id identify a tunnel together
1287 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1288 + String.valueOf(pcepTunnel.id());
1289
1290 if (tunnelKey == null || "".equals(tunnelKey)) {
1291 log.error("Invalid PCEP tunnel");
1292 return;
1293 }
1294
1295 TunnelId tunnelId = getTunnelId(tunnelKey);
1296
1297 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1298
1299 OperationType operType = pcepTunnel.getOperationType();
1300 switch (operType) {
1301 case ADD:
1302 tunnelId = service.tunnelAdded(tunnel);
1303 tunnelMap.put(tunnelKey, tunnelId);
1304 break;
1305
1306 case UPDATE:
1307 service.tunnelUpdated(tunnel);
1308 break;
1309
1310 case DELETE:
1311 service.tunnelRemoved(tunnel);
1312 tunnelMap.remove(tunnelKey);
1313 break;
1314
1315 default:
1316 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301317 }
1318 }
cheng fan48e832c2015-05-29 01:54:47 +08001319
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301320 @Override
1321 public void handleMessage(PccId pccId, PcepMessage msg) {
1322 try {
1323 log.debug("tunnel provider handle message {}", msg.getType().toString());
1324 switch (msg.getType()) {
1325 case REPORT:
1326 int srpId = 0;
1327 LinkedList<PcepStateReport> llStateReportList = null;
1328 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1329 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1330 PcepSrpObject srpObj = null;
1331 PcepLspObject lspObj = null;
1332 while (listIterator.hasNext()) {
1333 PcepStateReport stateRpt = listIterator.next();
1334 srpObj = stateRpt.getSrpObject();
1335 lspObj = stateRpt.getLspObject();
1336
1337 if (srpObj instanceof PcepSrpObject) {
1338 srpId = srpObj.getSrpID();
1339 }
1340
1341 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1342 log.debug("SRP ID in handle message " + srpId);
1343
Jonathan Hart51539b82015-10-29 09:53:04 -07001344 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301345 // For PCRpt without matching SRP id.
1346 handleRptWithoutSrpId(stateRpt, pccId);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301347 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301348 }
1349
Avantika-Huawei56c11842016-04-28 00:56:56 +05301350 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301351 }
1352 break;
1353
1354 default:
1355 log.debug("Received unsupported message type {}", msg.getType().toString());
1356 }
1357 } catch (Exception e) {
1358 log.error("Exception occured while processing report message {}", e.getMessage());
1359 }
1360 }
1361
1362 /**
1363 * Handles report message for setup/update/delete tunnel request.
1364 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301365 * @param srpId unique identifier for PCEP message
1366 * @param lspObj LSP object
1367 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301368 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301369 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301370 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001371 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301372
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301373 // store the values required from report message
1374 pcepTunnelData.setPlspId(lspObj.getPlspId());
1375 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1376 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1377 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1378
Avantika-Huawei56c11842016-04-28 00:56:56 +05301379 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301380 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1381 while (listTlvIterator.hasNext()) {
1382 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301383 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1384 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301385 break;
1386 }
1387 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001388 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301389 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001390 }
1391
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301392 Path path = pcepTunnelData.path();
1393 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301394 Builder annotationBuilder = DefaultAnnotations.builder();
1395 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1396
1397 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1398 if (tunnel.annotations().value(PLSP_ID) == null) {
1399 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1400 }
1401
1402 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1403 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1404 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1405 }
1406
Priyanka B4c3cef02016-06-14 20:27:53 +05301407 if (tunnel.annotations().value(PCC_TUNNEL_ID) == null) {
1408 annotationBuilder.set(PCC_TUNNEL_ID, String.valueOf(ipv4LspTlv.getTunnelId()));
1409 }
1410
Avantika-Huawei56c11842016-04-28 00:56:56 +05301411 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301412 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1413 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1414 providerId, tunnel.tunnelName(), path,
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301415 tunnel.resource(), annotations);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301416
Avantika-Huawei56c11842016-04-28 00:56:56 +05301417 if (CREATE == pcepTunnelData.requestType()) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301418 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Jonathan Hart51539b82015-10-29 09:53:04 -07001419 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301420 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001421 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301422 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301423 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001424 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1425 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301426 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301427
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301428 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1429
Avantika-Huawei56c11842016-04-28 00:56:56 +05301430 if (lspObj.getRFlag()) {
1431 tunnelRemoved(td);
1432 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301433 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301434 tunnelUpdated(td, tunnelState);
1435 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301436
1437 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1438 // though there are no labels to download for SR-TE.
1439 if ((pcepLspStatus == PcepLspStatus.GOING_UP)
1440 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING)) {
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301441 // Query again to get latest tunnel updated with protocol values from PCRpt msg.
1442 updateTunnel(service.tunnelQueryById(tunnel.tunnelId()), tunnel.path());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301443 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301444 }
1445
Priyanka B413fbe82016-05-26 11:44:45 +05301446 private SparseAnnotations getAnnotations(PcepLspObject lspObj, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv,
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301447 float bandwidth, LspType lspType, String costType) {
1448
1449 Builder builder = DefaultAnnotations.builder();
1450
1451 /*
1452 * [RFC 5440] The absence of the METRIC object MUST be interpreted by the PCE as a path computation request
1453 * for which no constraints need be applied to any of the metrics.
1454 */
1455 if (costType != null) {
1456 builder.set(COST_TYPE, costType);
1457 }
1458
1459 SparseAnnotations annotations = builder
Priyanka B413fbe82016-05-26 11:44:45 +05301460 .set(BANDWIDTH, (new Float(bandwidth)).toString()).set(LSP_SIG_TYPE, lspType.name())
1461 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1462 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1463 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId()))
1464 .set(DELEGATE, String.valueOf(lspObj.getDFlag()))
1465 .build();
1466 return annotations;
1467 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301468
Priyanka B413fbe82016-05-26 11:44:45 +05301469 private LspType getLspType(PcepSrpObject srpObj) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301470 LspType lspType = WITH_SIGNALLING;
1471
1472 if (null != srpObj) {
1473 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1474 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1475
1476 while (listIterator.hasNext()) {
1477 PcepValueType tlv = listIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301478 switch (tlv.getType()) {
1479 case PathSetupTypeTlv.TYPE:
1480 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1481 break;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301482 default:
1483 break;
1484 }
1485 }
1486 }
Priyanka B413fbe82016-05-26 11:44:45 +05301487 return lspType;
1488 }
1489
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301490 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId) {
Priyanka B413fbe82016-05-26 11:44:45 +05301491 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301492 String costType = null;
Priyanka B413fbe82016-05-26 11:44:45 +05301493 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1494 checkNotNull(msgPath);
1495 PcepEroObject eroObj = msgPath.getEroObject();
1496 if (eroObj == null) {
1497 log.error("ERO object is null in report message.");
1498 return;
1499 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301500
1501 PcepAttribute attributes = msgPath.getPcepAttribute();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301502 float bandwidth = 0;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301503 int cost = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301504 if (attributes != null) {
1505 if (attributes.getMetricObjectList() != null) {
1506 ListIterator<PcepMetricObject> iterator = attributes.getMetricObjectList().listIterator();
1507 PcepMetricObject metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301508
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301509 while (metricObj != null) {
1510 if (metricObj.getBType() == IGP_METRIC) {
1511 costType = "COST";
1512 } else if (metricObj.getBType() == TE_METRIC) {
1513 costType = "TE_COST";
1514 }
1515 if (costType != null) {
1516 cost = metricObj.getMetricVal();
1517 log.debug("Path cost {}", cost);
1518 break;
1519 }
1520 metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301521 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301522 }
1523 if (attributes.getBandwidthObject() != null) {
1524 bandwidth = attributes.getBandwidthObject().getBandwidth();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301525 }
1526 }
1527
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301528 List<Object> eroSubObjList = buildPathFromEroObj(eroObj, providerId);
1529 List<Link> links = new ArrayList<>();
1530 List<LabelResourceId> labels = new ArrayList<>();
1531 for (Object linkOrLabel : eroSubObjList) {
1532 if (linkOrLabel instanceof Link) {
1533 links.add((Link) linkOrLabel);
1534 } else if (linkOrLabel instanceof Integer) {
1535 labels.add(LabelResourceId.labelResourceId(((Integer) linkOrLabel).longValue()));
1536 }
Priyanka B413fbe82016-05-26 11:44:45 +05301537 }
1538
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301539 Path path = new DefaultPath(providerId, links, cost, EMPTY);
1540 NetworkResource labelStack = new DefaultLabelStack(labels);
1541
1542 // To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1543 // from PCE.
Priyanka B413fbe82016-05-26 11:44:45 +05301544 PcepSrpObject srpObj = stateRpt.getSrpObject();
1545 LspType lspType = getLspType(srpObj);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301546
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301547 PcepLspObject lspObj = stateRpt.getLspObject();
1548 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1549 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1550 SymbolicPathNameTlv pathNameTlv = null;
1551
1552 while (listTlvIterator.hasNext()) {
1553 PcepValueType tlv = listTlvIterator.next();
1554 switch (tlv.getType()) {
1555 case StatefulIPv4LspIdentifiersTlv.TYPE:
1556 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1557 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301558 case SymbolicPathNameTlv.TYPE:
1559 pathNameTlv = (SymbolicPathNameTlv) tlv;
1560 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301561 default:
1562 break;
1563 }
1564 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301565 /*
1566 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1567 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1568 */
1569 if (ipv4LspIdenTlv == null) {
1570 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1571 return;
1572 }
1573
1574 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1575 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1576 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1577 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1578 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
1579
Priyanka B413fbe82016-05-26 11:44:45 +05301580 // Store delegation flag info and that LSP info because only delegated PCE sends update message
1581 // Storing if D flag is set, if not dont store. while checking whether delegation if annotation for D flag
1582 // not present then non-delegated , if present it is delegated.
1583 if (lspObj.getDFlag()) {
1584 pcepClientController.getClient(pccId).setLspAndDelegationInfo(
1585 new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()), lspObj.getDFlag());
1586 }
1587
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301588 Tunnel tunnel = null;
1589 // Asynchronous status change message from PCC for LSP reported earlier.
1590 for (Tunnel tunnelObj : tunnelQueryResult) {
1591 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1592 /*
1593 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1594 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1595 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1596 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1597 * session an LSP is "first" reported to a PCE.
1598 */
1599 if ((pathNameTlv != null)
1600 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1601 tunnel = tunnelObj;
1602 break;
1603 }
1604 continue;
1605 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301606 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId()) && (Integer
1607 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1608 tunnel = tunnelObj;
1609 break;
1610 }
1611 }
1612
1613 DefaultTunnelDescription td;
Priyanka B413fbe82016-05-26 11:44:45 +05301614 SparseAnnotations annotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301615 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
1616 if (tunnel == null) {
1617 if (lspObj.getRFlag()) {
1618 /*
1619 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1620 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1621 */
1622 return;
1623 }
1624
Priyanka B413fbe82016-05-26 11:44:45 +05301625 DeviceId deviceId = getDevice(pccId);
1626 if (deviceId == null) {
1627 log.error("Ingress deviceId not found");
1628 return;
1629 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301630 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType, costType);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301631
Priyanka B413fbe82016-05-26 11:44:45 +05301632 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301633 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path, labelStack,
Priyanka B413fbe82016-05-26 11:44:45 +05301634 annotations);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301635
1636 // Do not support PCC initiated LSP after LSP DB sync is completed.
1637 if (!lspObj.getSFlag() && !lspObj.getCFlag()) {
1638 log.error("Received PCC initiated LSP while not in sync.");
1639 return;
1640 }
1641
Priyanka B413fbe82016-05-26 11:44:45 +05301642 /*
1643 * If ONOS instance is master for PCC then set delegated flag as annotation and add the tunnel to store.
1644 * Because all LSPs need not be delegated, hence mastership for the PCC is confirmed whereas not the
1645 * delegation set to all LSPs.If ONOS is not the master for that PCC then check if D flag is set, if yes
1646 * wait for 2 seconds [while master has added the tunnel to the store] then update the tunnel. Tunnel is
1647 * updated because in case of resilency only delegated LSPs are recomputed and only delegated PCE can
1648 * send update message to that client.
1649 *
1650 * 1)Master can 1st get the Rpt message
1651 * a)Master adds the tunnel into core.
1652 * b)If a non-master for ingress gets Rpt message with D flag set[as delegation owner]
1653 * after master, then runs timer then update the tunnel with D flag set.
1654 * 2)Non-Master can 1st get the Rpt message
1655 * a)Non-Master runs the timer check for the tunnel then updates the tunnel with D flag set
1656 * b)Master would have got the message while the non-master running timer, hence master adds
1657 * tunnel to core
1658 *
1659 * In general always master adds the tunnel to the core
1660 * while delegated owner [master or non-master with D flag set] always updates the tunnel running timer
1661 */
1662 if (mastershipService.isLocalMaster(deviceId)) {
1663 TunnelId tId = tunnelAdded(td, tunnelState);
1664 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1665 tunnelState, new DefaultGroupId(0), tId, TunnelName.tunnelName(String.valueOf(pathNameTlv
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301666 .getValue())), path, labelStack, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301667
Priyanka B413fbe82016-05-26 11:44:45 +05301668 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1669 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1670 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1671 } else if (!mastershipService.isLocalMaster(deviceId) && lspObj.getDFlag()) {
1672 //Start timer then update the tunnel with D flag
1673 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1674 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301675 return;
1676 }
1677
Priyanka B413fbe82016-05-26 11:44:45 +05301678 //delegated owner will update can be a master or non-master
1679 if (lspObj.getDFlag()) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301680 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType, costType);
Priyanka B413fbe82016-05-26 11:44:45 +05301681 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301682 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path, labelStack,
Priyanka B413fbe82016-05-26 11:44:45 +05301683 annotations);
1684 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1685 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301686 removeOrUpdatetunnel(tunnel, pccId, lspObj, providerId, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +05301687 return;
1688 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301689
Priyanka B413fbe82016-05-26 11:44:45 +05301690 private void removeOrUpdatetunnel(Tunnel tunnel, PccId pccId, PcepLspObject lspObj, ProviderId providerId,
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301691 State tunnelState) {
Priyanka B413fbe82016-05-26 11:44:45 +05301692 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1693 tunnel.type(), tunnel.groupId(), providerId, tunnel.tunnelName(), tunnel.path(),
1694 (SparseAnnotations) tunnel.annotations());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301695 if (lspObj.getRFlag()) {
1696 tunnelRemoved(td);
1697 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301698 tunnelUpdated(td, tunnelState);
1699 }
Priyanka B413fbe82016-05-26 11:44:45 +05301700 }
1701
1702 private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
1703 DefaultTunnelDescription td, ProviderId providerId) {
1704 //Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
1705
1706 /*
1707 * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
1708 * master has added the tunnel to the store] then update the tunnel.
1709 */
1710 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1711
1712 // Thread is started after 2 seconds first time later periodically after 2 seconds to update the tunnel
1713 executor.scheduleAtFixedRate(new UpdateDelegation(td, providerId, annotations, pccId,
1714 executor), DELAY, DELAY, TimeUnit.SECONDS);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301715 }
1716
1717 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301718 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301719 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301720 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301721 * @param providerId provider id
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301722 * @return list of links and labels
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301723 */
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301724 private List<Object> buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301725 checkNotNull(eroObj);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301726 List<Object> subObjList = new ArrayList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301727 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301728 if (0 == llSubObj.size()) {
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301729 log.error("ERO in report message does not have hop information");
Priyanka B4c3cef02016-06-14 20:27:53 +05301730 return null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301731 }
1732 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1733
1734 ConnectPoint src = null;
1735 ConnectPoint dst = null;
1736 boolean isSrcSet = false;
1737 while (tlvIterator.hasNext()) {
1738 PcepValueType subObj = tlvIterator.next();
1739 switch (subObj.getType()) {
1740
1741 case IPv4SubObject.TYPE:
1742
1743 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1744 if (!isSrcSet) {
1745 IpAddress srcIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1746 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1747 isSrcSet = true;
1748 } else {
1749 IpAddress dstIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1750 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
Ray Milkey2693bda2016-01-22 16:08:14 -08001751 Link link = DefaultLink.builder()
1752 .providerId(providerId)
1753 .src(src)
1754 .dst(dst)
1755 .type(Link.Type.DIRECT)
1756 .build();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301757 subObjList.add(link);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301758 src = dst;
1759 }
1760 break;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301761 case SrEroSubObject.TYPE:
1762 SrEroSubObject srEroSubObj = (SrEroSubObject) subObj;
1763 subObjList.add(srEroSubObj.getSid());
1764
1765 if (srEroSubObj.getSt() == PcepNaiIpv4Adjacency.ST_TYPE) {
1766 PcepNaiIpv4Adjacency nai = (PcepNaiIpv4Adjacency) (srEroSubObj.getNai());
1767 IpAddress srcIp = IpAddress.valueOf(nai.getLocalIpv4Addr());
1768 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1769 IpAddress dstIp = IpAddress.valueOf(nai.getRemoteIpv4Addr());
1770 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
1771 Link link = DefaultLink.builder()
1772 .providerId(providerId)
1773 .src(src)
1774 .dst(dst)
1775 .type(Link.Type.DIRECT)
1776 .build();
1777 subObjList.add(link);
1778 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301779 default:
1780 // the other sub objects are not required
1781 }
1782 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301783
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301784 return subObjList;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301785 }
1786
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301787 @Override
1788 public void clientConnected(PccId pccId) {
1789 // TODO
1790 }
1791
1792 @Override
1793 public void clientDisconnected(PccId pccId) {
1794 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001795 }
chengfan2fff70f2015-08-24 18:20:19 -05001796
chengfan2fff70f2015-08-24 18:20:19 -05001797 @Override
1798 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1799 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1800 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1801 tunnelStatisticsMap.put(id, tunnelStatistics);
1802 }
cheng fan48e832c2015-05-29 01:54:47 +08001803
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301804 @Override
1805 public void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301806
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301807 if (endOfSyncAction == SEND_UPDATE) {
1808 updateTunnel(tunnel, tunnel.path());
1809 return;
1810 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301811
1812 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1813 tunnel.src(), tunnel.dst(),
1814 tunnel.type(),
1815 tunnel.groupId(),
1816 tunnel.providerId(),
1817 tunnel.tunnelName(),
1818 tunnel.path(),
1819 (SparseAnnotations) tunnel.annotations());
1820
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301821
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301822 if (endOfSyncAction == PcepLspSyncAction.UNSTABLE) {
1823
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301824 // Send PCInit msg again after global reoptimization.
1825 tunnelUpdated(td, UNSTABLE);
1826
1827 // To remove the old tunnel from store whose PLSPID is not
1828 // recognized by ingress PCC.
1829 tunnelRemoved(td);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301830
1831 } else if (endOfSyncAction == REMOVE) {
1832 tunnelRemoved(td);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301833 }
1834 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301835 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301836 @Override
1837 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1838 return service.tunnelQueryById(tunnelId);
1839 }
1840
Priyanka B413fbe82016-05-26 11:44:45 +05301841
1842 private DeviceId getDevice(PccId pccId) {
1843 // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID.
1844 IpAddress lsrId = pccId.ipAddress();
1845 String lsrIdentifier = String.valueOf(lsrId);
1846
1847 // Find PCC deviceID from lsrId stored as annotations
1848 Iterable<Device> devices = deviceService.getAvailableDevices();
1849 for (Device dev : devices) {
1850 if (dev.annotations().value(AnnotationKeys.TYPE).equals("L3")
1851 && dev.annotations().value(LSRID).equals(lsrIdentifier)) {
1852 return dev.id();
1853 }
1854 }
1855 return null;
1856 }
1857
1858 /**
1859 * Updates the tunnel with updated tunnel annotation after a delay of two seconds and checks it till
1860 * tunnel is found.
1861 */
1862 private class UpdateDelegation implements Runnable {
1863 DefaultTunnelDescription td;
1864 ProviderId providerId;
1865 SparseAnnotations annotations;
1866 PccId pccId;
1867 ScheduledExecutorService executor;
1868
1869 /**
1870 * Creates an instance of UpdateDelegation.
1871 *
1872 * @param td tunnel description
1873 * @param providerId provider id
1874 * @param annotations tunnel annotations
1875 * @param pccId PCEP client id
1876 * @param executor service of delegated owner
1877 */
1878 public UpdateDelegation(DefaultTunnelDescription td, ProviderId providerId, SparseAnnotations annotations,
1879 PccId pccId, ScheduledExecutorService executor) {
1880 this.td = td;
1881 this.providerId = providerId;
1882 this.annotations = annotations;
1883 this.pccId = pccId;
1884 this.executor = executor;
1885 }
1886
1887 //Temporary using annotations later will use projection/network config service
1888 @Override
1889 public void run() {
1890 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(td.src(), td.dst());
1891 TunnelId tempTunnelId = null;
1892 for (Tunnel t : tunnelQueryResult) {
1893 if (t.annotations().value(LOCAL_LSP_ID) == null || t.annotations().value(PLSP_ID) == null) {
1894 continue;
1895 }
1896
1897 if (t.annotations().value(LOCAL_LSP_ID).equals(td.annotations().value(LOCAL_LSP_ID))
1898 && t.annotations().value(PLSP_ID).equals(td.annotations().value(PLSP_ID))
1899 && ((IpTunnelEndPoint) t.src()).ip().equals(pccId.id())) {
1900 tempTunnelId = t.tunnelId();
1901 break;
1902 }
1903 }
1904
1905 //If tunnel is found update the tunnel and shutdown the thread otherwise thread will be executing
1906 //periodically
1907 if (tempTunnelId != null) {
1908 Tunnel tunnel = new DefaultTunnel(providerId, td.src(), td.dst(), MPLS, new DefaultGroupId(0),
1909 tempTunnelId, td.tunnelName(), td.path(), annotations);
1910 tunnelUpdated(tunnel, td.path());
1911 executor.shutdown();
1912 try {
1913 executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
1914 } catch (InterruptedException e) {
1915 log.error("updating delegation failed");
1916 }
1917 }
1918 }
1919 }
cheng fan48e832c2015-05-29 01:54:47 +08001920}