blob: 32ebbe6bff640b6e19a198efdcd44db4928880af [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;
31import org.onosproject.incubator.net.tunnel.DefaultOpticalTunnelEndPoint;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053032import org.onosproject.incubator.net.tunnel.DefaultTunnel;
cheng fan48e832c2015-05-29 01:54:47 +080033import org.onosproject.incubator.net.tunnel.DefaultTunnelDescription;
chengfan2fff70f2015-08-24 18:20:19 -050034import org.onosproject.incubator.net.tunnel.DefaultTunnelStatistics;
Jonathan Hart51539b82015-10-29 09:53:04 -070035import org.onosproject.incubator.net.tunnel.IpTunnelEndPoint;
cheng fan48e832c2015-05-29 01:54:47 +080036import org.onosproject.incubator.net.tunnel.OpticalLogicId;
37import org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint;
38import org.onosproject.incubator.net.tunnel.Tunnel;
Avantika-Huawei56c11842016-04-28 00:56:56 +053039import org.onosproject.incubator.net.tunnel.Tunnel.State;
Priyanka B413fbe82016-05-26 11:44:45 +053040import org.onosproject.incubator.net.tunnel.TunnelAdminService;
cheng fan48e832c2015-05-29 01:54:47 +080041import org.onosproject.incubator.net.tunnel.TunnelDescription;
42import org.onosproject.incubator.net.tunnel.TunnelEndPoint;
43import org.onosproject.incubator.net.tunnel.TunnelId;
44import org.onosproject.incubator.net.tunnel.TunnelName;
45import org.onosproject.incubator.net.tunnel.TunnelProvider;
46import org.onosproject.incubator.net.tunnel.TunnelProviderRegistry;
47import org.onosproject.incubator.net.tunnel.TunnelProviderService;
chengfan2fff70f2015-08-24 18:20:19 -050048import org.onosproject.incubator.net.tunnel.TunnelService;
49import org.onosproject.incubator.net.tunnel.TunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053050import org.onosproject.mastership.MastershipService;
51import org.onosproject.net.AnnotationKeys;
cheng fan48e832c2015-05-29 01:54:47 +080052import org.onosproject.net.ConnectPoint;
53import org.onosproject.net.DefaultAnnotations;
Avantika-Huawei56c11842016-04-28 00:56:56 +053054import org.onosproject.net.DefaultAnnotations.Builder;
cheng fan48e832c2015-05-29 01:54:47 +080055import org.onosproject.net.DefaultLink;
56import org.onosproject.net.DefaultPath;
Priyanka B413fbe82016-05-26 11:44:45 +053057import org.onosproject.net.Device;
cheng fan48e832c2015-05-29 01:54:47 +080058import org.onosproject.net.DeviceId;
59import org.onosproject.net.ElementId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053060import org.onosproject.net.IpElementId;
cheng fan48e832c2015-05-29 01:54:47 +080061import org.onosproject.net.Link;
62import org.onosproject.net.Path;
63import org.onosproject.net.PortNumber;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053064import org.onosproject.net.SparseAnnotations;
Priyanka B413fbe82016-05-26 11:44:45 +053065import org.onosproject.net.device.DeviceService;
cheng fan48e832c2015-05-29 01:54:47 +080066import org.onosproject.net.provider.AbstractProvider;
67import org.onosproject.net.provider.ProviderId;
68import org.onosproject.pcep.api.PcepController;
69import org.onosproject.pcep.api.PcepDpid;
70import org.onosproject.pcep.api.PcepHopNodeDescription;
71import org.onosproject.pcep.api.PcepOperator.OperationType;
72import org.onosproject.pcep.api.PcepTunnel;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053073import org.onosproject.pcep.api.PcepTunnel.PathState;
Jonathan Hart51539b82015-10-29 09:53:04 -070074import org.onosproject.pcep.api.PcepTunnel.PathType;
cheng fan48e832c2015-05-29 01:54:47 +080075import org.onosproject.pcep.api.PcepTunnelListener;
chengfan2fff70f2015-08-24 18:20:19 -050076import org.onosproject.pcep.api.PcepTunnelStatistics;
Priyanka B413fbe82016-05-26 11:44:45 +053077import org.onosproject.pcep.controller.LspKey;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053078import org.onosproject.pcep.controller.PccId;
79import org.onosproject.pcep.controller.PcepClient;
80import org.onosproject.pcep.controller.PcepClientController;
81import org.onosproject.pcep.controller.PcepClientListener;
82import org.onosproject.pcep.controller.PcepEventListener;
Priyanka B259847d2016-06-03 21:28:35 +053083import org.onosproject.pcep.controller.PcepLspStatus;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053084import org.onosproject.pcep.controller.PcepLspSyncAction;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +053085import org.onosproject.pcep.controller.SrpIdGenerators;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053086import org.onosproject.pcepio.exceptions.PcepParseException;
87import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
88import org.onosproject.pcepio.protocol.PcepAttribute;
89import org.onosproject.pcepio.protocol.PcepBandwidthObject;
90import org.onosproject.pcepio.protocol.PcepEndPointsObject;
91import org.onosproject.pcepio.protocol.PcepEroObject;
92import org.onosproject.pcepio.protocol.PcepInitiateMsg;
93import org.onosproject.pcepio.protocol.PcepLspObject;
94import org.onosproject.pcepio.protocol.PcepMessage;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053095import org.onosproject.pcepio.protocol.PcepMetricObject;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053096import org.onosproject.pcepio.protocol.PcepMsgPath;
97import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053098import org.onosproject.pcepio.protocol.PcepSrpObject;
99import org.onosproject.pcepio.protocol.PcepStateReport;
100import org.onosproject.pcepio.protocol.PcepUpdateMsg;
101import org.onosproject.pcepio.protocol.PcepUpdateRequest;
102import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530103import org.onosproject.pcepio.types.PathSetupTypeTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530104import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530105import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530106import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -0500107import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -0700108import org.osgi.service.component.annotations.Modified;
109import org.slf4j.Logger;
110
111import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530112import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530113import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700114import java.util.Collections;
115import java.util.Dictionary;
116import java.util.HashMap;
117import java.util.LinkedList;
118import java.util.List;
119import java.util.ListIterator;
120import java.util.Optional;
Priyanka B413fbe82016-05-26 11:44:45 +0530121import java.util.concurrent.Executors;
122import java.util.concurrent.ScheduledExecutorService;
123import java.util.concurrent.TimeUnit;
Jonathan Hart51539b82015-10-29 09:53:04 -0700124
125import static com.google.common.base.Preconditions.checkNotNull;
126import static com.google.common.base.Strings.isNullOrEmpty;
127import static org.onlab.util.Tools.get;
Priyanka B4c3cef02016-06-14 20:27:53 +0530128import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530129import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700130import static org.onosproject.net.DefaultAnnotations.EMPTY;
131import static org.onosproject.net.DeviceId.deviceId;
132import static org.onosproject.net.PortNumber.portNumber;
133import static org.onosproject.pcep.api.PcepDpid.uri;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530134import static org.onosproject.provider.pcep.tunnel.impl.LspType.WITH_SIGNALLING;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530135import static org.onosproject.provider.pcep.tunnel.impl.LspType.SR_WITHOUT_SIGNALLING;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530136import static org.onosproject.pcep.controller.PcepAnnotationKeys.BANDWIDTH;
137import static org.onosproject.pcep.controller.PcepAnnotationKeys.LOCAL_LSP_ID;
138import static org.onosproject.pcep.controller.PcepAnnotationKeys.LSP_SIG_TYPE;
139import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCC_TUNNEL_ID;
Priyanka B4c3cef02016-06-14 20:27:53 +0530140import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCE_INIT;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530141import static org.onosproject.pcep.controller.PcepAnnotationKeys.PLSP_ID;
142import static org.onosproject.pcep.controller.PcepAnnotationKeys.DELEGATE;
143import static org.onosproject.pcep.controller.PcepAnnotationKeys.COST_TYPE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530144import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
145import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
146import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
147import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530148import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530149import static org.onosproject.pcep.controller.PcepLspSyncAction.REMOVE;
150import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_UPDATE;
151import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_DELETE;
152import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.IGP_METRIC;
153import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.TE_METRIC;
Jonathan Hart51539b82015-10-29 09:53:04 -0700154import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800155
156/**
157 * Provider which uses an PCEP controller to detect, update, create network
158 * tunnels.
159 */
160@Component(immediate = true)
161@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530162public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800163
164 private static final Logger log = getLogger(PcepTunnelProvider.class);
165 private static final long MAX_BANDWIDTH = 99999744;
166 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800167 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530168 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
Priyanka B413fbe82016-05-26 11:44:45 +0530169 public static final long IDENTIFIER_SET = 0x100000000L;
170 public static final long SET = 0xFFFFFFFFL;
171 private static final int DELAY = 2;
172 private static final int WAIT_TIME = 5;
173 public static final String LSRID = "lsrId";
cheng fan48e832c2015-05-29 01:54:47 +0800174
chengfan2fff70f2015-08-24 18:20:19 -0500175 static final int POLL_INTERVAL = 10;
176 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
177 label = "Frequency (in seconds) for polling tunnel statistics")
178 private int tunnelStatsPollFrequency = POLL_INTERVAL;
179
cheng fan48e832c2015-05-29 01:54:47 +0800180 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
181
182 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
183 protected TunnelProviderRegistry tunnelProviderRegistry;
184
185 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
186 protected PcepController controller;
187
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530188 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
189 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500190
191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
192 protected TunnelService tunnelService;
193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
195 protected ComponentConfigService cfgService;
196
Priyanka B413fbe82016-05-26 11:44:45 +0530197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
198 protected TunnelAdminService tunnelAdminService;
199
200 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
201 protected MastershipService mastershipService;
202
203 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
204 protected DeviceService deviceService;
205
cheng fan48e832c2015-05-29 01:54:47 +0800206 TunnelProviderService service;
207
208 HashMap<String, TunnelId> tunnelMap = new HashMap<String, TunnelId>();
chengfan2fff70f2015-08-24 18:20:19 -0500209 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400210 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800211
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530212 private InnerTunnelProvider listener = new InnerTunnelProvider();
213
Jonathan Hart51539b82015-10-29 09:53:04 -0700214 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530215 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800216
217 /**
218 * Creates a Tunnel provider.
219 */
220 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530221 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800222 }
223
224 @Activate
225 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500226 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800227 service = tunnelProviderRegistry.register(this);
228 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530229 pcepClientController.addListener(listener);
230 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500231 tunnelService.queryAllTunnels().forEach(tunnel -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700232 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500233 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
234 tsc.start();
235 collectors.put(tunnel.tunnelId().id(), tsc);
236
237 });
238
cheng fan48e832c2015-05-29 01:54:47 +0800239 log.info("Started");
240 }
241
242 @Deactivate
243 public void deactivate() {
244 tunnelProviderRegistry.unregister(this);
245 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500246 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530247 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800248 log.info("Stopped");
249 }
250
chengfan2fff70f2015-08-24 18:20:19 -0500251 @Modified
252 public void modified(ComponentContext context) {
253 Dictionary<?, ?> properties = context.getProperties();
254 int newTunnelStatsPollFrequency;
255 try {
256 String s = get(properties, "tunnelStatsPollFrequency");
257 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
258
259 } catch (NumberFormatException | ClassCastException e) {
260 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
261 }
262
263 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
264 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
265 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
266 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
267 }
268
269 }
270
cheng fan48e832c2015-05-29 01:54:47 +0800271 @Override
272 public void setupTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530273 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530274 log.error("Tunnel Type MPLS is only supported");
275 return;
276 }
cheng fan48e832c2015-05-29 01:54:47 +0800277
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530278 // check for tunnel end points
279 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
280 log.error("Tunnel source or destination is not valid");
281 return;
282 }
283
284 // Get the pcc client
285 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
286
287 if (!(pc instanceof PcepClient)) {
288 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500289 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530290 return;
291 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530292
293 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
Priyanka B413fbe82016-05-26 11:44:45 +0530294 //Only master will initiate setup tunnel
295 if (pc.capability().pcInstantiationCapability() && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530296 pcepSetupTunnel(tunnel, path, pc);
297 }
cheng fan48e832c2015-05-29 01:54:47 +0800298 }
299
300 @Override
301 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800302
Priyanka B4c3cef02016-06-14 20:27:53 +0530303 //TODO: tunnel which is passed doesn't have tunnelID
Avantika-Huawei56c11842016-04-28 00:56:56 +0530304 if (tunnel.annotations().value(PLSP_ID) != null) {
305 updateTunnel(tunnel, path);
306 return;
307 }
308
309 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530310 log.error("Tunnel Type MPLS is only supported");
311 return;
312 }
313
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530314 // check for tunnel end points
315 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
316 log.error("Tunnel source or destination is not valid");
317 return;
318 }
319
Priyanka Bcdf9b102016-06-07 20:01:38 +0530320 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530321
322 if (!(pc instanceof PcepClient)) {
Priyanka B4c3cef02016-06-14 20:27:53 +0530323 log.error("There is no PCC connected with this device {}"
324 + srcElement.toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530325 return;
326 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530327
Priyanka B413fbe82016-05-26 11:44:45 +0530328 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
329 //Only master will initiate setup tunnel
330 if (pc.capability().pcInstantiationCapability()
331 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530332 pcepSetupTunnel(tunnel, path, pc);
333 }
cheng fan48e832c2015-05-29 01:54:47 +0800334 }
335
336 @Override
337 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800338
Avantika-Huawei56c11842016-04-28 00:56:56 +0530339 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530340 log.error("Tunnel Type MPLS is only supported");
341 return;
342 }
343
344 // check for tunnel end points
345 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
346 log.error("Tunnel source or destination is not valid");
347 return;
348 }
349
350 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
351
352 if (!(pc instanceof PcepClient)) {
353 log.error("There is no PCC connected with ip addresss {}"
354 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
355 return;
356 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530357
Priyanka B413fbe82016-05-26 11:44:45 +0530358 //Only master will release tunnel
359 if (pc.capability().pcInstantiationCapability()
360 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530361 pcepReleaseTunnel(tunnel, pc);
362 }
cheng fan48e832c2015-05-29 01:54:47 +0800363 }
364
365 @Override
366 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530367 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530368 log.error("Tunnel Type MPLS is only supported");
369 return;
370 }
cheng fan48e832c2015-05-29 01:54:47 +0800371
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530372 if (!(srcElement instanceof IpElementId)) {
373 log.error("Element id is not valid");
374 return;
375 }
376
377 // check for tunnel end points
378 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
379 log.error("Tunnel source or destination is not valid");
380 return;
381 }
382
383 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
384
385 if (!(pc instanceof PcepClient)) {
386 log.error("There is no PCC connected with ip addresss {}"
387 + ((IpElementId) srcElement).ipAddress().toString());
388 return;
389 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530390
Priyanka B413fbe82016-05-26 11:44:45 +0530391 //Only master will release tunnel
392 if (pc.capability().pcInstantiationCapability()
393 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530394 pcepReleaseTunnel(tunnel, pc);
395 }
cheng fan48e832c2015-05-29 01:54:47 +0800396 }
397
398 @Override
399 public void updateTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530400 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530401 log.error("Tunnel Type MPLS is only supported");
402 return;
403 }
cheng fan48e832c2015-05-29 01:54:47 +0800404
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530405 // check for tunnel end points
406 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
407 log.error("Tunnel source or destination is not valid");
408 return;
409 }
410
Priyanka B4c3cef02016-06-14 20:27:53 +0530411 //To get new tunnel ID (modified tunnel ID)
412 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
413 for (Tunnel t : tunnels) {
414 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
415 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
416 tunnel.dst(), tunnel.type(),
417 t.state(), tunnel.groupId(),
418 t.tunnelId(),
419 tunnel.tunnelName(),
420 tunnel.path(),
421 tunnel.resource(),
422 tunnel.annotations());
423 break;
424 }
425 }
426
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530427 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
428
429 if (!(pc instanceof PcepClient)) {
430 log.error("There is no PCC connected with ip addresss {}"
431 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
432 return;
433 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530434
Priyanka B413fbe82016-05-26 11:44:45 +0530435 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
436 // LSP].If annotation is null D flag is not set else it is set.
Priyanka B4c3cef02016-06-14 20:27:53 +0530437 Short localLspId = 0;
438 for (Tunnel t : tunnels) {
439 if (!t.tunnelId().equals(tunnel.tunnelId()) && t.tunnelName().equals(tunnel.tunnelName())) {
440 localLspId = Short.valueOf(t.annotations().value(LOCAL_LSP_ID));
441 }
442 }
443
444 if (localLspId == 0) {
445 log.error("Local LSP ID for old tunnel not found");
446 return;
447 }
448
449 //PCInitiate tunnels are always have D flag set, else check for tunnels who are delegated via LspKey
450 if (pc.capability().statefulPceCapability()) {
451 if (tunnel.annotations().value(PCE_INIT) != null && tunnel.annotations().value(PCE_INIT).equals("true")) {
452 pcepUpdateTunnel(tunnel, path, pc);
453 } else if (pc.delegationInfo(
454 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)),
455 localLspId.shortValue())) != null) {
456 pcepUpdateTunnel(tunnel, path, pc);
457 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530458 }
cheng fan48e832c2015-05-29 01:54:47 +0800459 }
460
461 @Override
462 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800463
Avantika-Huawei56c11842016-04-28 00:56:56 +0530464 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530465 log.error("Tunnel Type MPLS is only supported");
466 return;
467 }
468
469 if (!(srcElement instanceof IpElementId)) {
470 log.error("Element id is not valid");
471 return;
472 }
473
474 // check for tunnel end points
475 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
476 log.error("Tunnel source or destination is not valid");
477 return;
478 }
479
480 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
481
482 if (!(pc instanceof PcepClient)) {
483 log.error("There is no PCC connected with ip addresss {}"
484 + ((IpElementId) srcElement).ipAddress().toString());
485 return;
486 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530487
Priyanka B413fbe82016-05-26 11:44:45 +0530488 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
489 // LSP].If annotation is null D flag is not set else it is set.
490 if (pc.capability().statefulPceCapability()
491 && pc.delegationInfo(
492 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
493 .annotations().value(LOCAL_LSP_ID)))) != null) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530494 pcepUpdateTunnel(tunnel, path, pc);
495 }
cheng fan48e832c2015-05-29 01:54:47 +0800496 }
497
498 @Override
499 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530500 return handleTunnelAdded(tunnel, null);
501 }
502
503 public TunnelId tunnelAdded(TunnelDescription tunnel, State tunnelState) {
504 return handleTunnelAdded(tunnel, tunnelState);
505 }
506
507 private TunnelId handleTunnelAdded(TunnelDescription tunnel, State tunnelState) {
508
509 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700510 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530511
512 if (tunnelState == null) {
513 return service.tunnelAdded(tunnel);
514 } else {
515 return service.tunnelAdded(tunnel, tunnelState);
516 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530517 }
cheng fan48e832c2015-05-29 01:54:47 +0800518
Avantika-Huawei56c11842016-04-28 00:56:56 +0530519 long bandwidth = Long.parseLong(tunnel.annotations().value(BANDWIDTH));
cheng fan48e832c2015-05-29 01:54:47 +0800520
521 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800522 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800523 return null;
524 }
525
526 // endpoints
527 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
528 .src();
529 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
530 // devices
531 DeviceId srcId = (DeviceId) src.elementId().get();
532 DeviceId dstId = (DeviceId) dst.elementId().get();
533
534 // ports
535 long srcPort = src.portNumber().get().toLong();
536 long dstPort = dst.portNumber().get().toLong();
537
538 // type
539 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800540 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800541 return null;
542 }
543
544 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
545 dstPort, bandwidth,
546 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530547 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800548
549 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
550 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
551 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
552
553 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
554 return tunnelId;
555 }
556
Priyanka B413fbe82016-05-26 11:44:45 +0530557 private void tunnelUpdated(Tunnel tunnel, Path path) {
558 handleTunnelUpdate(tunnel, path);
559 }
560
561 //Handles tunnel updated using tunnel admin service[specially to update annotations].
562 private void handleTunnelUpdate(Tunnel tunnel, Path path) {
563
564 if (tunnel.type() == MPLS) {
565 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.tunnelId());
566
567 tunnelAdminService.updateTunnel(tunnel, path);
568
569 return;
570 }
571
572 Tunnel tunnelOld = tunnelQueryById(tunnel.tunnelId());
573 if (tunnelOld.type() != Tunnel.Type.VLAN) {
574 error("Illegal tunnel type. Only support VLAN tunnel update.");
575 return;
576 }
577
578 long bandwidth = Long
579 .parseLong(tunnel.annotations().value("bandwidth"));
580 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
581 error("Update failed, invalid bandwidth.");
582 return;
583 }
584 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
585
586 checkNotNull(pcepTunnelId, "Invalid tunnel id");
587 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
588 error("Update failed,maybe invalid bandwidth.");
589 return;
590 }
591 tunnelAdminService.updateTunnel(tunnel, path);
592 }
593
cheng fan48e832c2015-05-29 01:54:47 +0800594 @Override
595 public void tunnelRemoved(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530596 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700597 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530598 service.tunnelRemoved(tunnel);
599 }
600
cheng fan48e832c2015-05-29 01:54:47 +0800601 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
602 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
603 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800604 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800605 return;
606 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700607 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800608 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800609 if (!controller.deleteTunnel(pcepTunnelId)) {
610 error("Delete tunnel failed, Maybe some devices have been disconnected.");
611 return;
cheng fan48e832c2015-05-29 01:54:47 +0800612 }
613 tunnelMap.remove(pcepTunnelId);
614 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800615 }
616
617 @Override
618 public void tunnelUpdated(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530619 handleTunnelUpdate(tunnel, null);
620 }
621
622 public void tunnelUpdated(TunnelDescription tunnel, State tunnelState) {
623 handleTunnelUpdate(tunnel, tunnelState);
624 }
625
626 private void handleTunnelUpdate(TunnelDescription tunnel, State tunnelState) {
627 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700628 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530629
630 if (tunnelState == null) {
631 service.tunnelUpdated(tunnel);
632 } else {
633 service.tunnelUpdated(tunnel, tunnelState);
634 }
635 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530636 }
cheng fan48e832c2015-05-29 01:54:47 +0800637
638 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
639 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800640 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800641 return;
642 }
cheng fan7716ec92015-05-31 01:53:19 +0800643 long bandwidth = Long
644 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800645 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800646 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800647 return;
648 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700649 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800650
651 checkNotNull(pcepTunnelId, "Invalid tunnel id");
652 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
653
cheng fan7716ec92015-05-31 01:53:19 +0800654 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800655 return;
656
657 }
658 service.tunnelUpdated(tunnel);
659 }
660
cheng fan7716ec92015-05-31 01:53:19 +0800661 private void error(String info) {
662 System.err.println(info);
663 }
664
cheng fan48e832c2015-05-29 01:54:47 +0800665 // Short-hand for creating a connection point.
666 private ConnectPoint connectPoint(PcepDpid id, long port) {
667 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
668 }
669
670 // Short-hand for creating a link.
671 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800672 return DefaultLink.builder()
673 .providerId(id())
674 .src(connectPoint(src, sp))
675 .dst(connectPoint(dst, dp))
676 .type(Link.Type.TUNNEL)
677 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800678 }
679
680 // Creates a path that leads through the given devices.
681 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700682 PathType pathtype, PathState pathState) {
cheng fan48e832c2015-05-29 01:54:47 +0800683 if (hopList == null || hopList.size() == 0) {
684 return null;
685 }
686 List<Link> links = new ArrayList<>();
687 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
688 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
689 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
690 .get(i + 1).getPortNum()));
691 }
692
693 int hopNum = hopList.size() - 2;
694 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
695 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800696 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800697 .set("pathType", String.valueOf(pathtype)).build();
698 return new DefaultPath(id(), links, hopNum, extendAnnotations);
699 }
700
701 // convert the path description to a string.
702 public String pathToString(List<Link> links) {
703 StringBuilder builder = new StringBuilder();
704 builder.append("{");
705 for (Link link : links) {
706 builder.append("(Device:" + link.src().deviceId() + " Port:"
707 + link.src().port().toLong());
708 builder.append(" Device:" + link.dst().deviceId() + " Port:"
709 + link.dst().port().toLong());
710 builder.append(")");
711 }
712 builder.append("}");
713 return builder.toString();
714 }
715
716 // build a TunnelDescription.
717 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
718 TunnelId tunnelId) {
719 TunnelEndPoint srcPoint = null;
720 TunnelEndPoint dstPoint = null;
721 Tunnel.Type tunnelType = null;
722 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
723
724 // add path after codes of tunnel's path merged
725 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800726 pcepTunnel.getPathType(),
727 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800728
729 OpticalTunnelEndPoint.Type endPointType = null;
730 switch (pcepTunnel.type()) {
731 case OCH:
732 tunnelType = Tunnel.Type.OCH;
733 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
734 break;
735
736 case OTN:
737 tunnelType = Tunnel.Type.ODUK;
738 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
739 break;
740
741 case UNI:
742 tunnelType = Tunnel.Type.VLAN;
743 endPointType = null;
744 break;
745
746 default:
747 break;
748 }
749 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
750 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
751 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
752 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
753
754 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
755 Optional.of(srcPort), null,
756 endPointType,
757 OpticalLogicId.logicId(0),
758 true);
759 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
760 Optional.of(dstPort), null,
761 endPointType,
762 OpticalLogicId.logicId(0),
763 true);
764
765 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800766 DefaultAnnotations annotations = DefaultAnnotations
767 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800768 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800769 .set("bandwidth",
770 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800771 .set("index", String.valueOf(pcepTunnel.id())).build();
772
cheng fan48e832c2015-05-29 01:54:47 +0800773 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
774 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800775 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800776 DefaultAnnotations extendAnnotations = DefaultAnnotations
777 .builder()
778 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800779 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800780 annotations = DefaultAnnotations.merge(annotations,
781 extendAnnotations);
782
783 }
784 TunnelDescription tunnel = new DefaultTunnelDescription(
785 tunnelId,
786 srcPoint,
787 dstPoint,
788 tunnelType,
789 new DefaultGroupId(
790 0),
791 id(), name,
792 path,
793 annotations);
794 return tunnel;
cheng fan48e832c2015-05-29 01:54:47 +0800795 }
796
797 /**
798 * Get the tunnelID according to the tunnel key.
799 *
800 * @param tunnelKey tunnel key
801 * @return corresponding tunnel id of the a tunnel key.
802 */
803 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800804 for (String key : tunnelMap.keySet()) {
805 if (key.equals(tunnelKey)) {
806 return tunnelMap.get(key);
807 }
808 }
809 return null;
810 }
811
812 /**
813 * Get the tunnel key according to the tunnelID.
814 *
815 * @param tunnelId tunnel id
816 * @return corresponding a tunnel key of the tunnel id.
817 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700818 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800819 for (String key : tunnelMap.keySet()) {
820 if (tunnelMap.get(key).id() == tunnelId.id()) {
821 return key;
822 }
823 }
824 return null;
825
826 }
827
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530828 /**
chengfan2fff70f2015-08-24 18:20:19 -0500829 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
830 *
831 * @param statistics statistics data from a PCEP tunnel
832 * @return TunnelStatistics
833 */
834 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
835 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
836 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
837 .setPacketLossRatio(statistics.packetLossRate())
838 .setFlowDelay(statistics.flowDelay())
839 .setAlarms(statistics.alarms())
840 .build();
841 return tunnelStatistics;
842 }
843 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530844 * Creates list of hops for ERO object from Path.
845 *
846 * @param path network path
847 * @return list of ipv4 subobjects
848 */
849 private LinkedList<PcepValueType> createPcepPath(Path path) {
850 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
851 List<Link> listLink = path.links();
852 ConnectPoint source = null;
853 ConnectPoint destination = null;
854 IpAddress ipDstAddress = null;
855 IpAddress ipSrcAddress = null;
856 PcepValueType subObj = null;
Priyanka Bcdf9b102016-06-07 20:01:38 +0530857 long portNo;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530858
859 for (Link link : listLink) {
860 source = link.src();
861 if (!(source.equals(destination))) {
862 //set IPv4SubObject for ERO object
Priyanka Bcdf9b102016-06-07 20:01:38 +0530863 portNo = source.port().toLong();
864 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
865 ipSrcAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530866 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
867 llSubObjects.add(subObj);
868 }
869
870 destination = link.dst();
Priyanka Bcdf9b102016-06-07 20:01:38 +0530871 portNo = destination.port().toLong();
872 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
873 ipDstAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530874 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
875 llSubObjects.add(subObj);
876 }
Priyanka Bcdf9b102016-06-07 20:01:38 +0530877
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530878 return llSubObjects;
879 }
880
881 /**
882 * Creates PcInitiated lsp request list for setup tunnel.
883 *
884 * @param tunnel mpls tunnel
885 * @param path network path
886 * @param pc pcep client
887 * @param srpId unique id for pcep message
888 * @return list of PcInitiatedLspRequest
889 * @throws PcepParseException while building pcep objects fails
890 */
891 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
892 PcepClient pc, int srpId)
893 throws PcepParseException {
894 PcepValueType tlv;
895 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
896
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700897 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530898 log.error("There is no link information to create tunnel");
899 return null;
900 }
901
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530902 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530903
904 // set PathSetupTypeTlv of SRP object
905 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)).type());
906 llOptionalTlv.add(tlv);
907
908 // build SRP object
909 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
910 .setOptionalTlv(llOptionalTlv).build();
911
912 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530913 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530914
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530915 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530916 short localLspId = 0;
917 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
918 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
919 if (localLspIdString != null) {
920 localLspId = Short.valueOf(localLspIdString);
921 }
922 }
923
Priyanka B4c3cef02016-06-14 20:27:53 +0530924 tunnel.annotations().value(LSP_SIG_TYPE);
Avantika-Huawei56c11842016-04-28 00:56:56 +0530925 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
926 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
927 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530928 llOptionalTlv.add(tlv);
929 //set SymbolicPathNameTlv of LSP object
930 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
931 llOptionalTlv.add(tlv);
932
933 //build LSP object
934 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setOFlag((byte) 0).setPlspId(0)
935 .setOptionalTlv(llOptionalTlv).build();
936
937 //build ENDPOINTS object
938 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
939 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
940 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
941 .setPFlag(true).build();
942
943 //build ERO object
944 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
945
Priyanka B4c3cef02016-06-14 20:27:53 +0530946 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530947 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +0530948 iBandwidth = Float.floatToIntBits(Float.parseFloat(tunnel.annotations().value(BANDWIDTH)));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530949 }
950 // build bandwidth object
951 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
952 // build pcep attribute
953 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
954
955 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
956 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
957 .setPcepAttribute(pcepAttribute).build();
958 llPcInitiatedLspRequestList.add(initiateLspRequest);
959 return llPcInitiatedLspRequestList;
960 }
961
962 /**
963 * To send initiate tunnel message to pcc.
964 *
965 * @param tunnel mpls tunnel info
966 * @param path explicit route for the tunnel
967 * @param pc pcep client to send message
968 */
969 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
970 try {
971 int srpId = SrpIdGenerators.create();
Priyanka B4c3cef02016-06-14 20:27:53 +0530972 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
973 for (Tunnel t : tunnels) {
974 if (t.tunnelName().equals(tunnel.tunnelName())) {
975 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
976 tunnel.dst(), tunnel.type(),
977 t.state(), tunnel.groupId(),
978 t.tunnelId(),
979 tunnel.tunnelName(),
980 tunnel.path(),
981 tunnel.resource(),
982 tunnel.annotations());
983 break;
984 }
985 }
986
987 if (tunnel.tunnelId() == null) {
988 log.error("Tunnel ID not found");
989 return;
990 }
991
Avantika-Huawei56c11842016-04-28 00:56:56 +0530992 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530993
Jonathan Hart51539b82015-10-29 09:53:04 -0700994 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530995
996 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
997 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700998 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530999 log.error("Failed to create PcInitiatedLspRequestList");
1000 return;
1001 }
1002
1003 //build PCInitiate message
1004 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1005 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
1006 .build();
1007
1008 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1009
Jonathan Hart51539b82015-10-29 09:53:04 -07001010 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301011 } catch (PcepParseException e) {
1012 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
1013 }
1014 }
1015
1016 /**
1017 * To send Release tunnel message to pcc.
1018 *
1019 * @param tunnel mpls tunnel info
1020 * @param pc pcep client to send message
1021 */
1022 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
1023 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301024 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001025 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301026 int srpId = SrpIdGenerators.create();
1027 TunnelId tunnelId = tunnel.tunnelId();
1028 int plspId = 0;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301029
Jonathan Hart51539b82015-10-29 09:53:04 -07001030 if (!(pcepTunnelApiMapper.checkFromTunnelDBQueue(tunnelId))) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301031 log.error("Tunnel doesnot exists. Tunnel id {}" + tunnelId.toString());
1032 return;
1033 } else {
Jonathan Hart51539b82015-10-29 09:53:04 -07001034 PcepTunnelData pcepTunnelDbData = pcepTunnelApiMapper.getDataFromTunnelDBQueue(tunnelId);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301035 plspId = pcepTunnelDbData.plspId();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301036 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301037
1038 PcepValueType tlv;
1039 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301040
1041 // set PathSetupTypeTlv of SRP object
1042 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
1043 .type());
1044 llOptionalTlv.add(tlv);
1045
1046 // build SRP object
1047 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
1048 .setOptionalTlv(llOptionalTlv).build();
1049
1050 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301051 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
1052
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301053 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1054 llOptionalTlv.add(tlv);
Priyanka B4c3cef02016-06-14 20:27:53 +05301055
1056 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1057 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1058 short localLspId = 0;
1059 short pccTunnelId = 0;
1060
1061 if (localLspIdString != null) {
1062 localLspId = Short.valueOf(localLspIdString);
1063 }
1064
1065 if (pccTunnelIdString != null) {
1066 pccTunnelId = Short.valueOf(pccTunnelIdString);
1067 }
1068
1069 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1070 .ip().getIp4Address().toInt()),
1071 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1072 .getIp4Address().toInt()));
1073 llOptionalTlv.add(tlv);
1074
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301075 // build lsp object, set r flag as false to delete the tunnel
1076 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
1077 .setOptionalTlv(llOptionalTlv).build();
1078
1079 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1080 .setLspObject(lspobj).build();
1081
1082 llPcInitiatedLspRequestList.add(releaseLspRequest);
1083
1084 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1085 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1086
1087 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1088
Jonathan Hart51539b82015-10-29 09:53:04 -07001089 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301090 } catch (PcepParseException e) {
1091 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1092 }
1093 }
1094
1095 /**
1096 * To send Update tunnel request message to pcc.
1097 *
1098 * @param tunnel mpls tunnel info
1099 * @param path explicit route for the tunnel
1100 * @param pc pcep client to send message
1101 */
1102 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1103 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301104 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001105 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301106 int srpId = SrpIdGenerators.create();
1107 TunnelId tunnelId = tunnel.tunnelId();
1108 PcepValueType tlv;
1109 int plspId = 0;
1110
1111 LinkedList<PcepValueType> llSubObjects = createPcepPath(path);
1112 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
1113 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
1114
Avantika-Huawei56c11842016-04-28 00:56:56 +05301115 // set PathSetupTypeTlv of SRP object
1116 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
1117 tlv = new PathSetupTypeTlv(lspSigType.type());
1118 llOptionalTlv.add(tlv);
1119
1120 // build SRP object
1121 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
1122 .setOptionalTlv(llOptionalTlv).build();
1123
1124 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301125
Avantika-Huawei56c11842016-04-28 00:56:56 +05301126 if (lspSigType != WITH_SIGNALLING) {
1127 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1128 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1129 short localLspId = 0;
1130 short pccTunnelId = 0;
1131
1132 if (localLspIdString != null) {
1133 localLspId = Short.valueOf(localLspIdString);
1134 }
1135
1136 if (pccTunnelIdString != null) {
1137 pccTunnelId = Short.valueOf(pccTunnelIdString);
1138 }
1139
1140 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1141 .ip().getIp4Address().toInt()),
1142 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1143 .getIp4Address().toInt()));
1144 llOptionalTlv.add(tlv);
1145 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301146
1147 if (tunnel.tunnelName().value() != null) {
1148 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1149 llOptionalTlv.add(tlv);
1150 }
1151
1152 // build lsp object
Priyanka B4c3cef02016-06-14 20:27:53 +05301153 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true)
1154 .setPlspId(Integer.valueOf(tunnel.annotations().value(PLSP_ID)))
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301155 .setOptionalTlv(llOptionalTlv).build();
1156 // build ero object
1157 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
1158
Priyanka B4c3cef02016-06-14 20:27:53 +05301159 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301160 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301161 iBandwidth = Float.parseFloat(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301162 }
1163 // build bandwidth object
1164 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1165 // build pcep attribute
1166 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1167 // build pcep msg path
1168 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1169 .build();
1170
1171 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1172 .setLspObject(lspobj).setMsgPath(msgPath).build();
1173
1174 llUpdateRequestList.add(updateRequest);
1175
1176 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1177
1178 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001179 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301180 } catch (PcepParseException e) {
1181 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1182 }
1183 }
1184
chengfan2fff70f2015-08-24 18:20:19 -05001185
1186
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301187 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001188
1189 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001190 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001191 TunnelDescription tunnel = null;
1192 // instance and id identify a tunnel together
1193 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1194 + String.valueOf(pcepTunnel.id());
1195
1196 if (tunnelKey == null || "".equals(tunnelKey)) {
1197 log.error("Invalid PCEP tunnel");
1198 return;
1199 }
1200
1201 TunnelId tunnelId = getTunnelId(tunnelKey);
1202
1203 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1204
1205 OperationType operType = pcepTunnel.getOperationType();
1206 switch (operType) {
1207 case ADD:
1208 tunnelId = service.tunnelAdded(tunnel);
1209 tunnelMap.put(tunnelKey, tunnelId);
1210 break;
1211
1212 case UPDATE:
1213 service.tunnelUpdated(tunnel);
1214 break;
1215
1216 case DELETE:
1217 service.tunnelRemoved(tunnel);
1218 tunnelMap.remove(tunnelKey);
1219 break;
1220
1221 default:
1222 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301223 }
1224 }
cheng fan48e832c2015-05-29 01:54:47 +08001225
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301226 @Override
1227 public void handleMessage(PccId pccId, PcepMessage msg) {
1228 try {
1229 log.debug("tunnel provider handle message {}", msg.getType().toString());
1230 switch (msg.getType()) {
1231 case REPORT:
1232 int srpId = 0;
1233 LinkedList<PcepStateReport> llStateReportList = null;
1234 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1235 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1236 PcepSrpObject srpObj = null;
1237 PcepLspObject lspObj = null;
1238 while (listIterator.hasNext()) {
1239 PcepStateReport stateRpt = listIterator.next();
1240 srpObj = stateRpt.getSrpObject();
1241 lspObj = stateRpt.getLspObject();
1242
1243 if (srpObj instanceof PcepSrpObject) {
1244 srpId = srpObj.getSrpID();
1245 }
1246
1247 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1248 log.debug("SRP ID in handle message " + srpId);
1249
Jonathan Hart51539b82015-10-29 09:53:04 -07001250 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301251 // For PCRpt without matching SRP id.
1252 handleRptWithoutSrpId(stateRpt, pccId);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301253 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301254 }
1255
Avantika-Huawei56c11842016-04-28 00:56:56 +05301256 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301257 }
1258 break;
1259
1260 default:
1261 log.debug("Received unsupported message type {}", msg.getType().toString());
1262 }
1263 } catch (Exception e) {
1264 log.error("Exception occured while processing report message {}", e.getMessage());
1265 }
1266 }
1267
1268 /**
1269 * Handles report message for setup/update/delete tunnel request.
1270 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301271 * @param srpId unique identifier for PCEP message
1272 * @param lspObj LSP object
1273 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301274 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301275 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301276 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001277 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301278
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301279 // store the values required from report message
1280 pcepTunnelData.setPlspId(lspObj.getPlspId());
1281 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1282 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1283 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1284
Avantika-Huawei56c11842016-04-28 00:56:56 +05301285 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301286 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1287 while (listTlvIterator.hasNext()) {
1288 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301289 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1290 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301291 break;
1292 }
1293 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001294 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301295 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001296 }
1297
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301298 Path path = pcepTunnelData.path();
1299 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301300 Builder annotationBuilder = DefaultAnnotations.builder();
1301 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1302
1303 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1304 if (tunnel.annotations().value(PLSP_ID) == null) {
1305 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1306 }
1307
1308 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1309 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1310 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1311 }
1312
Priyanka B4c3cef02016-06-14 20:27:53 +05301313 if (tunnel.annotations().value(PCC_TUNNEL_ID) == null) {
1314 annotationBuilder.set(PCC_TUNNEL_ID, String.valueOf(ipv4LspTlv.getTunnelId()));
1315 }
1316
Avantika-Huawei56c11842016-04-28 00:56:56 +05301317 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301318 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1319 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1320 providerId, tunnel.tunnelName(), path,
1321 annotations);
1322
Avantika-Huawei56c11842016-04-28 00:56:56 +05301323 if (CREATE == pcepTunnelData.requestType()) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301324 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Jonathan Hart51539b82015-10-29 09:53:04 -07001325 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301326 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001327 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301328 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301329 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001330 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1331 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301332 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301333
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301334 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1335
Avantika-Huawei56c11842016-04-28 00:56:56 +05301336 if (lspObj.getRFlag()) {
1337 tunnelRemoved(td);
1338 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301339 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301340 tunnelUpdated(td, tunnelState);
1341 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301342
1343 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1344 // though there are no labels to download for SR-TE.
1345 if ((pcepLspStatus == PcepLspStatus.GOING_UP)
1346 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING)) {
1347 updateTunnel(tunnel, tunnel.path());
1348 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301349 }
1350
Priyanka B413fbe82016-05-26 11:44:45 +05301351 private SparseAnnotations getAnnotations(PcepLspObject lspObj, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv,
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301352 float bandwidth, LspType lspType, String costType) {
1353
1354 Builder builder = DefaultAnnotations.builder();
1355
1356 /*
1357 * [RFC 5440] The absence of the METRIC object MUST be interpreted by the PCE as a path computation request
1358 * for which no constraints need be applied to any of the metrics.
1359 */
1360 if (costType != null) {
1361 builder.set(COST_TYPE, costType);
1362 }
1363
1364 SparseAnnotations annotations = builder
Priyanka B413fbe82016-05-26 11:44:45 +05301365 .set(BANDWIDTH, (new Float(bandwidth)).toString()).set(LSP_SIG_TYPE, lspType.name())
1366 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1367 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1368 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId()))
1369 .set(DELEGATE, String.valueOf(lspObj.getDFlag()))
1370 .build();
1371 return annotations;
1372 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301373
Priyanka B413fbe82016-05-26 11:44:45 +05301374 private LspType getLspType(PcepSrpObject srpObj) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301375 LspType lspType = WITH_SIGNALLING;
1376
1377 if (null != srpObj) {
1378 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1379 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1380
1381 while (listIterator.hasNext()) {
1382 PcepValueType tlv = listIterator.next();
1383
1384 switch (tlv.getType()) {
1385 case PathSetupTypeTlv.TYPE:
1386 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1387 break;
1388
1389 default:
1390 break;
1391 }
1392 }
1393 }
Priyanka B413fbe82016-05-26 11:44:45 +05301394 return lspType;
1395 }
1396
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301397 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId) {
Priyanka B413fbe82016-05-26 11:44:45 +05301398 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301399 String costType = null;
Priyanka B413fbe82016-05-26 11:44:45 +05301400 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1401 checkNotNull(msgPath);
1402 PcepEroObject eroObj = msgPath.getEroObject();
1403 if (eroObj == null) {
1404 log.error("ERO object is null in report message.");
1405 return;
1406 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301407
1408 PcepAttribute attributes = msgPath.getPcepAttribute();
1409 int cost = 0;
1410 if (attributes != null && attributes.getMetricObjectList() != null) {
1411 ListIterator<PcepMetricObject> iterator = attributes.getMetricObjectList().listIterator();
1412 PcepMetricObject metricObj = iterator.next();
1413
1414 while (metricObj != null) {
1415 if (metricObj.getBType() == IGP_METRIC) {
1416 costType = "COST";
1417 } else if (metricObj.getBType() == TE_METRIC) {
1418 costType = "TE_COST";
1419 }
1420
1421 if (costType != null) {
1422 cost = metricObj.getMetricVal();
1423 log.debug("Path cost {}", cost);
1424 break;
1425 }
1426 metricObj = iterator.next();
1427 }
1428 }
1429
1430 Path path = buildPathFromEroObj(eroObj, providerId, cost);
Priyanka B413fbe82016-05-26 11:44:45 +05301431
1432 float bandwidth = 0;
1433 if (msgPath.getBandwidthObject() != null) {
1434 bandwidth = msgPath.getBandwidthObject().getBandwidth();
1435 }
1436
1437 /*
1438 * To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1439 * from PCE.
1440 */
1441 PcepSrpObject srpObj = stateRpt.getSrpObject();
1442 LspType lspType = getLspType(srpObj);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301443
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301444 PcepLspObject lspObj = stateRpt.getLspObject();
1445 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1446 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1447 SymbolicPathNameTlv pathNameTlv = null;
1448
1449 while (listTlvIterator.hasNext()) {
1450 PcepValueType tlv = listTlvIterator.next();
1451 switch (tlv.getType()) {
1452 case StatefulIPv4LspIdentifiersTlv.TYPE:
1453 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1454 break;
1455
1456 case SymbolicPathNameTlv.TYPE:
1457 pathNameTlv = (SymbolicPathNameTlv) tlv;
1458 break;
1459
1460 default:
1461 break;
1462 }
1463 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301464 /*
1465 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1466 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1467 */
1468 if (ipv4LspIdenTlv == null) {
1469 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1470 return;
1471 }
1472
1473 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1474 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1475 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1476 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1477 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
1478
Priyanka B413fbe82016-05-26 11:44:45 +05301479 // Store delegation flag info and that LSP info because only delegated PCE sends update message
1480 // Storing if D flag is set, if not dont store. while checking whether delegation if annotation for D flag
1481 // not present then non-delegated , if present it is delegated.
1482 if (lspObj.getDFlag()) {
1483 pcepClientController.getClient(pccId).setLspAndDelegationInfo(
1484 new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()), lspObj.getDFlag());
1485 }
1486
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301487 Tunnel tunnel = null;
1488 // Asynchronous status change message from PCC for LSP reported earlier.
1489 for (Tunnel tunnelObj : tunnelQueryResult) {
1490 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1491 /*
1492 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1493 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1494 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1495 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1496 * session an LSP is "first" reported to a PCE.
1497 */
1498 if ((pathNameTlv != null)
1499 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1500 tunnel = tunnelObj;
1501 break;
1502 }
1503 continue;
1504 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301505 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId()) && (Integer
1506 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1507 tunnel = tunnelObj;
1508 break;
1509 }
1510 }
1511
1512 DefaultTunnelDescription td;
Priyanka B413fbe82016-05-26 11:44:45 +05301513 SparseAnnotations annotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301514 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
1515 if (tunnel == null) {
1516 if (lspObj.getRFlag()) {
1517 /*
1518 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1519 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1520 */
1521 return;
1522 }
1523
Priyanka B413fbe82016-05-26 11:44:45 +05301524 DeviceId deviceId = getDevice(pccId);
1525 if (deviceId == null) {
1526 log.error("Ingress deviceId not found");
1527 return;
1528 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301529 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType, costType);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301530
Priyanka B413fbe82016-05-26 11:44:45 +05301531 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
1532 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path,
1533 annotations);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301534
1535 // Do not support PCC initiated LSP after LSP DB sync is completed.
1536 if (!lspObj.getSFlag() && !lspObj.getCFlag()) {
1537 log.error("Received PCC initiated LSP while not in sync.");
1538 return;
1539 }
1540
Priyanka B413fbe82016-05-26 11:44:45 +05301541 /*
1542 * If ONOS instance is master for PCC then set delegated flag as annotation and add the tunnel to store.
1543 * Because all LSPs need not be delegated, hence mastership for the PCC is confirmed whereas not the
1544 * delegation set to all LSPs.If ONOS is not the master for that PCC then check if D flag is set, if yes
1545 * wait for 2 seconds [while master has added the tunnel to the store] then update the tunnel. Tunnel is
1546 * updated because in case of resilency only delegated LSPs are recomputed and only delegated PCE can
1547 * send update message to that client.
1548 *
1549 * 1)Master can 1st get the Rpt message
1550 * a)Master adds the tunnel into core.
1551 * b)If a non-master for ingress gets Rpt message with D flag set[as delegation owner]
1552 * after master, then runs timer then update the tunnel with D flag set.
1553 * 2)Non-Master can 1st get the Rpt message
1554 * a)Non-Master runs the timer check for the tunnel then updates the tunnel with D flag set
1555 * b)Master would have got the message while the non-master running timer, hence master adds
1556 * tunnel to core
1557 *
1558 * In general always master adds the tunnel to the core
1559 * while delegated owner [master or non-master with D flag set] always updates the tunnel running timer
1560 */
1561 if (mastershipService.isLocalMaster(deviceId)) {
1562 TunnelId tId = tunnelAdded(td, tunnelState);
1563 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1564 tunnelState, new DefaultGroupId(0), tId, TunnelName.tunnelName(String.valueOf(pathNameTlv
1565 .getValue())), path, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301566
Priyanka B413fbe82016-05-26 11:44:45 +05301567 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1568 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1569 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1570 } else if (!mastershipService.isLocalMaster(deviceId) && lspObj.getDFlag()) {
1571 //Start timer then update the tunnel with D flag
1572 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1573 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301574 return;
1575 }
1576
Priyanka B413fbe82016-05-26 11:44:45 +05301577 //delegated owner will update can be a master or non-master
1578 if (lspObj.getDFlag()) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301579 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType, costType);
Priyanka B413fbe82016-05-26 11:44:45 +05301580 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
1581 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path,
1582 annotations);
1583 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1584 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301585 removeOrUpdatetunnel(tunnel, pccId, lspObj, providerId, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +05301586 return;
1587 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301588
Priyanka B413fbe82016-05-26 11:44:45 +05301589 private void removeOrUpdatetunnel(Tunnel tunnel, PccId pccId, PcepLspObject lspObj, ProviderId providerId,
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301590 State tunnelState) {
Priyanka B413fbe82016-05-26 11:44:45 +05301591 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1592 tunnel.type(), tunnel.groupId(), providerId, tunnel.tunnelName(), tunnel.path(),
1593 (SparseAnnotations) tunnel.annotations());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301594 if (lspObj.getRFlag()) {
1595 tunnelRemoved(td);
1596 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301597 tunnelUpdated(td, tunnelState);
1598 }
Priyanka B413fbe82016-05-26 11:44:45 +05301599 }
1600
1601 private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
1602 DefaultTunnelDescription td, ProviderId providerId) {
1603 //Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
1604
1605 /*
1606 * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
1607 * master has added the tunnel to the store] then update the tunnel.
1608 */
1609 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1610
1611 // Thread is started after 2 seconds first time later periodically after 2 seconds to update the tunnel
1612 executor.scheduleAtFixedRate(new UpdateDelegation(td, providerId, annotations, pccId,
1613 executor), DELAY, DELAY, TimeUnit.SECONDS);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301614 }
1615
1616 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301617 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301618 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301619 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301620 * @param providerId provider id
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301621 * @param cost cost of path
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301622 * @return path object
1623 */
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301624 private Path buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId, int cost) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301625 checkNotNull(eroObj);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301626 List<Link> links = new ArrayList<Link>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301627 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301628 if (0 == llSubObj.size()) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301629 log.debug("ERO in report message does not have hop information");
1630 return null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301631 }
1632 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1633
1634 ConnectPoint src = null;
1635 ConnectPoint dst = null;
1636 boolean isSrcSet = false;
1637 while (tlvIterator.hasNext()) {
1638 PcepValueType subObj = tlvIterator.next();
1639 switch (subObj.getType()) {
1640
1641 case IPv4SubObject.TYPE:
1642
1643 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1644 if (!isSrcSet) {
1645 IpAddress srcIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1646 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1647 isSrcSet = true;
1648 } else {
1649 IpAddress dstIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1650 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
Ray Milkey2693bda2016-01-22 16:08:14 -08001651 Link link = DefaultLink.builder()
1652 .providerId(providerId)
1653 .src(src)
1654 .dst(dst)
1655 .type(Link.Type.DIRECT)
1656 .build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301657 links.add(link);
1658 src = dst;
1659 }
1660 break;
1661 default:
1662 // the other sub objects are not required
1663 }
1664 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301665
1666 return new DefaultPath(providerId, links, cost, EMPTY);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301667 }
1668
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301669 @Override
1670 public void clientConnected(PccId pccId) {
1671 // TODO
1672 }
1673
1674 @Override
1675 public void clientDisconnected(PccId pccId) {
1676 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001677 }
chengfan2fff70f2015-08-24 18:20:19 -05001678
chengfan2fff70f2015-08-24 18:20:19 -05001679 @Override
1680 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1681 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1682 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1683 tunnelStatisticsMap.put(id, tunnelStatistics);
1684 }
cheng fan48e832c2015-05-29 01:54:47 +08001685
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301686 @Override
1687 public void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301688
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301689 if (endOfSyncAction == SEND_UPDATE) {
1690 updateTunnel(tunnel, tunnel.path());
1691 return;
1692 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301693
1694 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1695 tunnel.src(), tunnel.dst(),
1696 tunnel.type(),
1697 tunnel.groupId(),
1698 tunnel.providerId(),
1699 tunnel.tunnelName(),
1700 tunnel.path(),
1701 (SparseAnnotations) tunnel.annotations());
1702
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301703
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301704 if (endOfSyncAction == PcepLspSyncAction.UNSTABLE) {
1705
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301706 // Send PCInit msg again after global reoptimization.
1707 tunnelUpdated(td, UNSTABLE);
1708
1709 // To remove the old tunnel from store whose PLSPID is not
1710 // recognized by ingress PCC.
1711 tunnelRemoved(td);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301712
1713 } else if (endOfSyncAction == REMOVE) {
1714 tunnelRemoved(td);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301715 }
1716 }
1717
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301718 @Override
1719 public void handleEndOfSyncAction(PccId pccId, PcepMessage msg, PcepLspSyncAction endOfSyncAction) {
1720 try {
1721 if ((msg instanceof PcepInitiateMsg) && (endOfSyncAction == SEND_DELETE)) {
1722 PcepClient pc = pcepClientController.getClient(pccId);
1723 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = ((PcepInitiateMsg) msg)
1724 .getPcInitiatedLspRequestList();
1725 PcInitiatedLspRequest pcInitMsg = llPcInitiatedLspRequestList.iterator().next();
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301726
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301727 if (pcInitMsg != null) {
1728 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(SrpIdGenerators.create())
1729 .setRFlag(true).build();
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301730
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301731 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest()
1732 .setLspObject(pcInitMsg.getLspObject()).setSrpObject(srpobj).build();
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301733
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301734 llPcInitiatedLspRequestList.remove(pcInitMsg);
1735 llPcInitiatedLspRequestList.add(releaseLspRequest);
1736
1737 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1738 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1739
1740 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1741 }
1742 }
1743 } catch (PcepParseException e) {
1744 log.error("Exception occured while sending initiate delete message {}", e.getMessage());
1745 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301746 }
1747 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301748 @Override
1749 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1750 return service.tunnelQueryById(tunnelId);
1751 }
1752
Priyanka B413fbe82016-05-26 11:44:45 +05301753
1754 private DeviceId getDevice(PccId pccId) {
1755 // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID.
1756 IpAddress lsrId = pccId.ipAddress();
1757 String lsrIdentifier = String.valueOf(lsrId);
1758
1759 // Find PCC deviceID from lsrId stored as annotations
1760 Iterable<Device> devices = deviceService.getAvailableDevices();
1761 for (Device dev : devices) {
1762 if (dev.annotations().value(AnnotationKeys.TYPE).equals("L3")
1763 && dev.annotations().value(LSRID).equals(lsrIdentifier)) {
1764 return dev.id();
1765 }
1766 }
1767 return null;
1768 }
1769
1770 /**
1771 * Updates the tunnel with updated tunnel annotation after a delay of two seconds and checks it till
1772 * tunnel is found.
1773 */
1774 private class UpdateDelegation implements Runnable {
1775 DefaultTunnelDescription td;
1776 ProviderId providerId;
1777 SparseAnnotations annotations;
1778 PccId pccId;
1779 ScheduledExecutorService executor;
1780
1781 /**
1782 * Creates an instance of UpdateDelegation.
1783 *
1784 * @param td tunnel description
1785 * @param providerId provider id
1786 * @param annotations tunnel annotations
1787 * @param pccId PCEP client id
1788 * @param executor service of delegated owner
1789 */
1790 public UpdateDelegation(DefaultTunnelDescription td, ProviderId providerId, SparseAnnotations annotations,
1791 PccId pccId, ScheduledExecutorService executor) {
1792 this.td = td;
1793 this.providerId = providerId;
1794 this.annotations = annotations;
1795 this.pccId = pccId;
1796 this.executor = executor;
1797 }
1798
1799 //Temporary using annotations later will use projection/network config service
1800 @Override
1801 public void run() {
1802 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(td.src(), td.dst());
1803 TunnelId tempTunnelId = null;
1804 for (Tunnel t : tunnelQueryResult) {
1805 if (t.annotations().value(LOCAL_LSP_ID) == null || t.annotations().value(PLSP_ID) == null) {
1806 continue;
1807 }
1808
1809 if (t.annotations().value(LOCAL_LSP_ID).equals(td.annotations().value(LOCAL_LSP_ID))
1810 && t.annotations().value(PLSP_ID).equals(td.annotations().value(PLSP_ID))
1811 && ((IpTunnelEndPoint) t.src()).ip().equals(pccId.id())) {
1812 tempTunnelId = t.tunnelId();
1813 break;
1814 }
1815 }
1816
1817 //If tunnel is found update the tunnel and shutdown the thread otherwise thread will be executing
1818 //periodically
1819 if (tempTunnelId != null) {
1820 Tunnel tunnel = new DefaultTunnel(providerId, td.src(), td.dst(), MPLS, new DefaultGroupId(0),
1821 tempTunnelId, td.tunnelName(), td.path(), annotations);
1822 tunnelUpdated(tunnel, td.path());
1823 executor.shutdown();
1824 try {
1825 executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
1826 } catch (InterruptedException e) {
1827 log.error("updating delegation failed");
1828 }
1829 }
1830 }
1831 }
cheng fan48e832c2015-05-29 01:54:47 +08001832}