blob: d9eedb411f4576e47a35a7cb7c04c45cd9830fdf [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;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053081import org.onosproject.pcep.controller.PccId;
82import org.onosproject.pcep.controller.PcepClient;
83import org.onosproject.pcep.controller.PcepClientController;
84import org.onosproject.pcep.controller.PcepClientListener;
85import org.onosproject.pcep.controller.PcepEventListener;
Priyanka B259847d2016-06-03 21:28:35 +053086import org.onosproject.pcep.controller.PcepLspStatus;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053087import org.onosproject.pcep.controller.PcepLspSyncAction;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +053088import org.onosproject.pcep.controller.SrpIdGenerators;
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +053089import org.onosproject.pcep.controller.PcepSyncStatus;
Phanendra Manda37b97fb2015-08-15 02:04:24 +053090import org.onosproject.pcepio.exceptions.PcepParseException;
91import org.onosproject.pcepio.protocol.PcInitiatedLspRequest;
92import org.onosproject.pcepio.protocol.PcepAttribute;
93import org.onosproject.pcepio.protocol.PcepBandwidthObject;
94import org.onosproject.pcepio.protocol.PcepEndPointsObject;
95import org.onosproject.pcepio.protocol.PcepEroObject;
96import org.onosproject.pcepio.protocol.PcepInitiateMsg;
97import org.onosproject.pcepio.protocol.PcepLspObject;
98import org.onosproject.pcepio.protocol.PcepMessage;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +053099import org.onosproject.pcepio.protocol.PcepMetricObject;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530100import org.onosproject.pcepio.protocol.PcepMsgPath;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530101import org.onosproject.pcepio.protocol.PcepNai;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530102import org.onosproject.pcepio.protocol.PcepReportMsg;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530103import org.onosproject.pcepio.protocol.PcepSrpObject;
104import org.onosproject.pcepio.protocol.PcepStateReport;
105import org.onosproject.pcepio.protocol.PcepUpdateMsg;
106import org.onosproject.pcepio.protocol.PcepUpdateRequest;
107import org.onosproject.pcepio.types.IPv4SubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530108import org.onosproject.pcepio.types.PathSetupTypeTlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530109import org.onosproject.pcepio.types.PcepNaiIpv4Adjacency;
110import org.onosproject.pcepio.types.PcepNaiIpv4NodeId;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530111import org.onosproject.pcepio.types.PcepValueType;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530112import org.onosproject.pcepio.types.SrEroSubObject;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530113import org.onosproject.pcepio.types.StatefulIPv4LspIdentifiersTlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530114import org.onosproject.pcepio.types.SymbolicPathNameTlv;
chengfan2fff70f2015-08-24 18:20:19 -0500115import org.osgi.service.component.ComponentContext;
Jonathan Hart51539b82015-10-29 09:53:04 -0700116import org.osgi.service.component.annotations.Modified;
117import org.slf4j.Logger;
118
119import java.util.ArrayList;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530120import java.util.Arrays;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530121import java.util.Collection;
Jonathan Hart51539b82015-10-29 09:53:04 -0700122import java.util.Collections;
123import java.util.Dictionary;
124import java.util.HashMap;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530125import java.util.Iterator;
Jonathan Hart51539b82015-10-29 09:53:04 -0700126import java.util.LinkedList;
127import java.util.List;
128import java.util.ListIterator;
129import java.util.Optional;
Priyanka B413fbe82016-05-26 11:44:45 +0530130import java.util.concurrent.Executors;
131import java.util.concurrent.ScheduledExecutorService;
132import java.util.concurrent.TimeUnit;
Jonathan Hart51539b82015-10-29 09:53:04 -0700133
134import static com.google.common.base.Preconditions.checkNotNull;
135import static com.google.common.base.Strings.isNullOrEmpty;
136import static org.onlab.util.Tools.get;
Priyanka B4c3cef02016-06-14 20:27:53 +0530137import static org.onosproject.incubator.net.tunnel.Tunnel.State.INIT;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530138import static org.onosproject.incubator.net.tunnel.Tunnel.Type.MPLS;
Jonathan Hart51539b82015-10-29 09:53:04 -0700139import static org.onosproject.net.DefaultAnnotations.EMPTY;
140import static org.onosproject.net.DeviceId.deviceId;
141import static org.onosproject.net.PortNumber.portNumber;
142import static org.onosproject.pcep.api.PcepDpid.uri;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530143import static org.onosproject.provider.pcep.tunnel.impl.LspType.WITH_SIGNALLING;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530144import static org.onosproject.provider.pcep.tunnel.impl.LspType.SR_WITHOUT_SIGNALLING;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530145import static org.onosproject.pcep.controller.PcepAnnotationKeys.BANDWIDTH;
146import static org.onosproject.pcep.controller.PcepAnnotationKeys.LOCAL_LSP_ID;
147import static org.onosproject.pcep.controller.PcepAnnotationKeys.LSP_SIG_TYPE;
148import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCC_TUNNEL_ID;
Priyanka B4c3cef02016-06-14 20:27:53 +0530149import static org.onosproject.pcep.controller.PcepAnnotationKeys.PCE_INIT;
Avantika-Huaweifc10dca2016-06-10 16:13:55 +0530150import static org.onosproject.pcep.controller.PcepAnnotationKeys.PLSP_ID;
151import static org.onosproject.pcep.controller.PcepAnnotationKeys.DELEGATE;
152import static org.onosproject.pcep.controller.PcepAnnotationKeys.COST_TYPE;
Avantika-Huawei56c11842016-04-28 00:56:56 +0530153import static org.onosproject.provider.pcep.tunnel.impl.RequestType.CREATE;
154import static org.onosproject.provider.pcep.tunnel.impl.RequestType.DELETE;
155import static org.onosproject.provider.pcep.tunnel.impl.RequestType.LSP_STATE_RPT;
156import static org.onosproject.provider.pcep.tunnel.impl.RequestType.UPDATE;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +0530157import static org.onosproject.incubator.net.tunnel.Tunnel.State.UNSTABLE;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +0530158import static org.onosproject.pcep.controller.PcepLspSyncAction.REMOVE;
159import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_UPDATE;
160import static org.onosproject.pcep.controller.PcepLspSyncAction.SEND_DELETE;
161import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.IGP_METRIC;
162import static org.onosproject.pcepio.protocol.ver1.PcepMetricObjectVer1.TE_METRIC;
Jonathan Hart51539b82015-10-29 09:53:04 -0700163import static org.slf4j.LoggerFactory.getLogger;
cheng fan48e832c2015-05-29 01:54:47 +0800164
165/**
166 * Provider which uses an PCEP controller to detect, update, create network
167 * tunnels.
168 */
169@Component(immediate = true)
170@Service
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530171public class PcepTunnelProvider extends AbstractProvider implements TunnelProvider {
cheng fan48e832c2015-05-29 01:54:47 +0800172
173 private static final Logger log = getLogger(PcepTunnelProvider.class);
174 private static final long MAX_BANDWIDTH = 99999744;
175 private static final long MIN_BANDWIDTH = 64;
cheng fan7716ec92015-05-31 01:53:19 +0800176 private static final String BANDWIDTH_UINT = "kbps";
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530177 static final String PROVIDER_ID = "org.onosproject.provider.tunnel.pcep";
Priyanka B413fbe82016-05-26 11:44:45 +0530178 public static final long IDENTIFIER_SET = 0x100000000L;
179 public static final long SET = 0xFFFFFFFFL;
180 private static final int DELAY = 2;
181 private static final int WAIT_TIME = 5;
182 public static final String LSRID = "lsrId";
cheng fan48e832c2015-05-29 01:54:47 +0800183
chengfan2fff70f2015-08-24 18:20:19 -0500184 static final int POLL_INTERVAL = 10;
185 @Property(name = "tunnelStatsPollFrequency", intValue = POLL_INTERVAL,
186 label = "Frequency (in seconds) for polling tunnel statistics")
187 private int tunnelStatsPollFrequency = POLL_INTERVAL;
188
cheng fan48e832c2015-05-29 01:54:47 +0800189 private static final String TUNNLE_NOT_NULL = "Create failed,The given port may be wrong or has been occupied.";
190
191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
192 protected TunnelProviderRegistry tunnelProviderRegistry;
193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
195 protected PcepController controller;
196
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
198 protected PcepClientController pcepClientController;
chengfan2fff70f2015-08-24 18:20:19 -0500199
200 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
201 protected TunnelService tunnelService;
202
203 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
204 protected ComponentConfigService cfgService;
205
Priyanka B413fbe82016-05-26 11:44:45 +0530206 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
207 protected TunnelAdminService tunnelAdminService;
208
209 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
210 protected MastershipService mastershipService;
211
212 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
213 protected DeviceService deviceService;
214
cheng fan48e832c2015-05-29 01:54:47 +0800215 TunnelProviderService service;
216
217 HashMap<String, TunnelId> tunnelMap = new HashMap<String, TunnelId>();
chengfan2fff70f2015-08-24 18:20:19 -0500218 HashMap<TunnelId, TunnelStatistics> tunnelStatisticsMap = new HashMap<>();
Brian Stanke9a108972016-04-11 15:25:17 -0400219 private HashMap<String, TunnelStatsCollector> collectors = Maps.newHashMap();
cheng fan48e832c2015-05-29 01:54:47 +0800220
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530221 private InnerTunnelProvider listener = new InnerTunnelProvider();
222
Jonathan Hart51539b82015-10-29 09:53:04 -0700223 protected PcepTunnelApiMapper pcepTunnelApiMapper = new PcepTunnelApiMapper();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530224 private static final int DEFAULT_BANDWIDTH_VALUE = 10;
cheng fan48e832c2015-05-29 01:54:47 +0800225
226 /**
227 * Creates a Tunnel provider.
228 */
229 public PcepTunnelProvider() {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530230 super(new ProviderId("pcep", PROVIDER_ID));
cheng fan48e832c2015-05-29 01:54:47 +0800231 }
232
233 @Activate
234 public void activate() {
chengfan2fff70f2015-08-24 18:20:19 -0500235 cfgService.registerProperties(getClass());
cheng fan48e832c2015-05-29 01:54:47 +0800236 service = tunnelProviderRegistry.register(this);
237 controller.addTunnelListener(listener);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530238 pcepClientController.addListener(listener);
239 pcepClientController.addEventListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500240 tunnelService.queryAllTunnels().forEach(tunnel -> {
Jonathan Hart51539b82015-10-29 09:53:04 -0700241 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
chengfan2fff70f2015-08-24 18:20:19 -0500242 TunnelStatsCollector tsc = new TunnelStatsCollector(pcepTunnelId, tunnelStatsPollFrequency);
243 tsc.start();
244 collectors.put(tunnel.tunnelId().id(), tsc);
245
246 });
247
cheng fan48e832c2015-05-29 01:54:47 +0800248 log.info("Started");
249 }
250
251 @Deactivate
252 public void deactivate() {
253 tunnelProviderRegistry.unregister(this);
254 controller.removeTunnelListener(listener);
chengfan2fff70f2015-08-24 18:20:19 -0500255 collectors.values().forEach(TunnelStatsCollector::stop);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530256 pcepClientController.removeListener(listener);
cheng fan48e832c2015-05-29 01:54:47 +0800257 log.info("Stopped");
258 }
259
chengfan2fff70f2015-08-24 18:20:19 -0500260 @Modified
261 public void modified(ComponentContext context) {
262 Dictionary<?, ?> properties = context.getProperties();
263 int newTunnelStatsPollFrequency;
264 try {
265 String s = get(properties, "tunnelStatsPollFrequency");
266 newTunnelStatsPollFrequency = isNullOrEmpty(s) ? tunnelStatsPollFrequency : Integer.parseInt(s.trim());
267
268 } catch (NumberFormatException | ClassCastException e) {
269 newTunnelStatsPollFrequency = tunnelStatsPollFrequency;
270 }
271
272 if (newTunnelStatsPollFrequency != tunnelStatsPollFrequency) {
273 tunnelStatsPollFrequency = newTunnelStatsPollFrequency;
274 collectors.values().forEach(tsc -> tsc.adjustPollInterval(tunnelStatsPollFrequency));
275 log.info("New setting: tunnelStatsPollFrequency={}", tunnelStatsPollFrequency);
276 }
277
278 }
279
cheng fan48e832c2015-05-29 01:54:47 +0800280 @Override
281 public void setupTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530282 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530283 log.error("Tunnel Type MPLS is only supported");
284 return;
285 }
cheng fan48e832c2015-05-29 01:54:47 +0800286
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530287 // check for tunnel end points
288 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
289 log.error("Tunnel source or destination is not valid");
290 return;
291 }
292
293 // Get the pcc client
294 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
295
296 if (!(pc instanceof PcepClient)) {
297 log.error("There is no PCC connected with ip addresss {}"
chengfan2fff70f2015-08-24 18:20:19 -0500298 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530299 return;
300 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530301
302 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
Priyanka B413fbe82016-05-26 11:44:45 +0530303 //Only master will initiate setup tunnel
304 if (pc.capability().pcInstantiationCapability() && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530305 pcepSetupTunnel(tunnel, path, pc);
306 }
cheng fan48e832c2015-05-29 01:54:47 +0800307 }
308
309 @Override
310 public void setupTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800311
Priyanka B4c3cef02016-06-14 20:27:53 +0530312 //TODO: tunnel which is passed doesn't have tunnelID
Avantika-Huawei56c11842016-04-28 00:56:56 +0530313 if (tunnel.annotations().value(PLSP_ID) != null) {
314 updateTunnel(tunnel, path);
315 return;
316 }
317
318 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530319 log.error("Tunnel Type MPLS is only supported");
320 return;
321 }
322
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530323 // check for tunnel end points
324 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
325 log.error("Tunnel source or destination is not valid");
326 return;
327 }
328
Priyanka Bcdf9b102016-06-07 20:01:38 +0530329 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530330
331 if (!(pc instanceof PcepClient)) {
Priyanka B4c3cef02016-06-14 20:27:53 +0530332 log.error("There is no PCC connected with this device {}"
333 + srcElement.toString());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530334 return;
335 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530336
Priyanka B413fbe82016-05-26 11:44:45 +0530337 //If stateful and PC Initiation capability is not supported by client not sending Initiate msg
338 //Only master will initiate setup tunnel
339 if (pc.capability().pcInstantiationCapability()
340 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530341 pcepSetupTunnel(tunnel, path, pc);
342 }
cheng fan48e832c2015-05-29 01:54:47 +0800343 }
344
345 @Override
346 public void releaseTunnel(Tunnel tunnel) {
cheng fan48e832c2015-05-29 01:54:47 +0800347
Avantika-Huawei56c11842016-04-28 00:56:56 +0530348 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530349 log.error("Tunnel Type MPLS is only supported");
350 return;
351 }
352
353 // check for tunnel end points
354 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
355 log.error("Tunnel source or destination is not valid");
356 return;
357 }
358
359 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
360
361 if (!(pc instanceof PcepClient)) {
362 log.error("There is no PCC connected with ip addresss {}"
363 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
364 return;
365 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530366
Priyanka B413fbe82016-05-26 11:44:45 +0530367 //Only master will release tunnel
368 if (pc.capability().pcInstantiationCapability()
369 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530370 pcepReleaseTunnel(tunnel, pc);
371 }
cheng fan48e832c2015-05-29 01:54:47 +0800372 }
373
374 @Override
375 public void releaseTunnel(ElementId srcElement, Tunnel tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530376 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530377 log.error("Tunnel Type MPLS is only supported");
378 return;
379 }
cheng fan48e832c2015-05-29 01:54:47 +0800380
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530381 if (!(srcElement instanceof IpElementId)) {
382 log.error("Element id is not valid");
383 return;
384 }
385
386 // check for tunnel end points
387 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
388 log.error("Tunnel source or destination is not valid");
389 return;
390 }
391
392 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
393
394 if (!(pc instanceof PcepClient)) {
395 log.error("There is no PCC connected with ip addresss {}"
396 + ((IpElementId) srcElement).ipAddress().toString());
397 return;
398 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530399
Priyanka B413fbe82016-05-26 11:44:45 +0530400 //Only master will release tunnel
401 if (pc.capability().pcInstantiationCapability()
402 && mastershipService.isLocalMaster(getDevice(pc.getPccId()))) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530403 pcepReleaseTunnel(tunnel, pc);
404 }
cheng fan48e832c2015-05-29 01:54:47 +0800405 }
406
407 @Override
408 public void updateTunnel(Tunnel tunnel, Path path) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530409 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530410 log.error("Tunnel Type MPLS is only supported");
411 return;
412 }
cheng fan48e832c2015-05-29 01:54:47 +0800413
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530414 // check for tunnel end points
415 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
416 log.error("Tunnel source or destination is not valid");
417 return;
418 }
419
Priyanka B4c3cef02016-06-14 20:27:53 +0530420 //To get new tunnel ID (modified tunnel ID)
421 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
422 for (Tunnel t : tunnels) {
423 if (t.state().equals(INIT) && t.tunnelName().equals(tunnel.tunnelName())) {
424 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
425 tunnel.dst(), tunnel.type(),
426 t.state(), tunnel.groupId(),
427 t.tunnelId(),
428 tunnel.tunnelName(),
429 tunnel.path(),
430 tunnel.resource(),
431 tunnel.annotations());
432 break;
433 }
434 }
435
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530436 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpTunnelEndPoint) tunnel.src()).ip()));
437
438 if (!(pc instanceof PcepClient)) {
439 log.error("There is no PCC connected with ip addresss {}"
440 + ((IpTunnelEndPoint) tunnel.src()).ip().toString());
441 return;
442 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530443
Priyanka B413fbe82016-05-26 11:44:45 +0530444 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
445 // LSP].If annotation is null D flag is not set else it is set.
Priyanka B4c3cef02016-06-14 20:27:53 +0530446 Short localLspId = 0;
447 for (Tunnel t : tunnels) {
448 if (!t.tunnelId().equals(tunnel.tunnelId()) && t.tunnelName().equals(tunnel.tunnelName())) {
449 localLspId = Short.valueOf(t.annotations().value(LOCAL_LSP_ID));
450 }
451 }
452
453 if (localLspId == 0) {
454 log.error("Local LSP ID for old tunnel not found");
455 return;
456 }
457
458 //PCInitiate tunnels are always have D flag set, else check for tunnels who are delegated via LspKey
459 if (pc.capability().statefulPceCapability()) {
460 if (tunnel.annotations().value(PCE_INIT) != null && tunnel.annotations().value(PCE_INIT).equals("true")) {
461 pcepUpdateTunnel(tunnel, path, pc);
462 } else if (pc.delegationInfo(
463 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)),
464 localLspId.shortValue())) != null) {
465 pcepUpdateTunnel(tunnel, path, pc);
466 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530467 }
cheng fan48e832c2015-05-29 01:54:47 +0800468 }
469
470 @Override
471 public void updateTunnel(ElementId srcElement, Tunnel tunnel, Path path) {
cheng fan48e832c2015-05-29 01:54:47 +0800472
Avantika-Huawei56c11842016-04-28 00:56:56 +0530473 if (tunnel.type() != MPLS) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530474 log.error("Tunnel Type MPLS is only supported");
475 return;
476 }
477
478 if (!(srcElement instanceof IpElementId)) {
479 log.error("Element id is not valid");
480 return;
481 }
482
483 // check for tunnel end points
484 if (!(tunnel.src() instanceof IpTunnelEndPoint) || !(tunnel.dst() instanceof IpTunnelEndPoint)) {
485 log.error("Tunnel source or destination is not valid");
486 return;
487 }
488
489 PcepClient pc = pcepClientController.getClient(PccId.pccId(((IpElementId) srcElement).ipAddress()));
490
491 if (!(pc instanceof PcepClient)) {
492 log.error("There is no PCC connected with ip addresss {}"
493 + ((IpElementId) srcElement).ipAddress().toString());
494 return;
495 }
Priyanka Bd2b28882016-04-04 16:57:04 +0530496
Priyanka B413fbe82016-05-26 11:44:45 +0530497 // If delegation flag is set then only send update message[means delegated PCE can send update msg for that
498 // LSP].If annotation is null D flag is not set else it is set.
499 if (pc.capability().statefulPceCapability()
500 && pc.delegationInfo(
501 new LspKey(Integer.valueOf(tunnel.annotations().value(PLSP_ID)), Short.valueOf(tunnel
502 .annotations().value(LOCAL_LSP_ID)))) != null) {
Priyanka Bd2b28882016-04-04 16:57:04 +0530503 pcepUpdateTunnel(tunnel, path, pc);
504 }
cheng fan48e832c2015-05-29 01:54:47 +0800505 }
506
507 @Override
508 public TunnelId tunnelAdded(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530509 return handleTunnelAdded(tunnel, null);
510 }
511
512 public TunnelId tunnelAdded(TunnelDescription tunnel, State tunnelState) {
513 return handleTunnelAdded(tunnel, tunnelState);
514 }
515
516 private TunnelId handleTunnelAdded(TunnelDescription tunnel, State tunnelState) {
517
518 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700519 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530520
521 if (tunnelState == null) {
522 return service.tunnelAdded(tunnel);
523 } else {
524 return service.tunnelAdded(tunnel, tunnelState);
525 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530526 }
cheng fan48e832c2015-05-29 01:54:47 +0800527
Avantika-Huawei56c11842016-04-28 00:56:56 +0530528 long bandwidth = Long.parseLong(tunnel.annotations().value(BANDWIDTH));
cheng fan48e832c2015-05-29 01:54:47 +0800529
530 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800531 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800532 return null;
533 }
534
535 // endpoints
536 OpticalTunnelEndPoint src = (org.onosproject.incubator.net.tunnel.OpticalTunnelEndPoint) tunnel
537 .src();
538 OpticalTunnelEndPoint dst = (OpticalTunnelEndPoint) tunnel.dst();
539 // devices
540 DeviceId srcId = (DeviceId) src.elementId().get();
541 DeviceId dstId = (DeviceId) dst.elementId().get();
542
543 // ports
544 long srcPort = src.portNumber().get().toLong();
545 long dstPort = dst.portNumber().get().toLong();
546
547 // type
548 if (tunnel.type() != Tunnel.Type.VLAN) {
cheng fan7716ec92015-05-31 01:53:19 +0800549 error("Illegal tunnel type. Only support VLAN tunnel creation.");
cheng fan48e832c2015-05-29 01:54:47 +0800550 return null;
551 }
552
553 PcepTunnel pcepTunnel = controller.applyTunnel(srcId, dstId, srcPort,
554 dstPort, bandwidth,
555 tunnel.tunnelName()
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530556 .value());
cheng fan48e832c2015-05-29 01:54:47 +0800557
558 checkNotNull(pcepTunnel, TUNNLE_NOT_NULL);
559 TunnelDescription tunnelAdded = buildOpticalTunnel(pcepTunnel, null);
560 TunnelId tunnelId = service.tunnelAdded(tunnelAdded);
561
562 tunnelMap.put(String.valueOf(pcepTunnel.id()), tunnelId);
563 return tunnelId;
564 }
565
Priyanka B413fbe82016-05-26 11:44:45 +0530566 private void tunnelUpdated(Tunnel tunnel, Path path) {
567 handleTunnelUpdate(tunnel, path);
568 }
569
570 //Handles tunnel updated using tunnel admin service[specially to update annotations].
571 private void handleTunnelUpdate(Tunnel tunnel, Path path) {
572
573 if (tunnel.type() == MPLS) {
574 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.tunnelId());
575
576 tunnelAdminService.updateTunnel(tunnel, path);
577
578 return;
579 }
580
581 Tunnel tunnelOld = tunnelQueryById(tunnel.tunnelId());
582 if (tunnelOld.type() != Tunnel.Type.VLAN) {
583 error("Illegal tunnel type. Only support VLAN tunnel update.");
584 return;
585 }
586
587 long bandwidth = Long
588 .parseLong(tunnel.annotations().value("bandwidth"));
589 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
590 error("Update failed, invalid bandwidth.");
591 return;
592 }
593 String pcepTunnelId = getPcepTunnelKey(tunnel.tunnelId());
594
595 checkNotNull(pcepTunnelId, "Invalid tunnel id");
596 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
597 error("Update failed,maybe invalid bandwidth.");
598 return;
599 }
600 tunnelAdminService.updateTunnel(tunnel, path);
601 }
602
cheng fan48e832c2015-05-29 01:54:47 +0800603 @Override
604 public void tunnelRemoved(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530605 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700606 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530607 service.tunnelRemoved(tunnel);
608 }
609
cheng fan48e832c2015-05-29 01:54:47 +0800610 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
611 checkNotNull(tunnelOld, "The tunnel id is not exsited.");
612 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800613 error("Illegal tunnel type. Only support VLAN tunnel deletion.");
cheng fan48e832c2015-05-29 01:54:47 +0800614 return;
615 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700616 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800617 checkNotNull(pcepTunnelId, "The tunnel id is not exsited.");
cheng fan7716ec92015-05-31 01:53:19 +0800618 if (!controller.deleteTunnel(pcepTunnelId)) {
619 error("Delete tunnel failed, Maybe some devices have been disconnected.");
620 return;
cheng fan48e832c2015-05-29 01:54:47 +0800621 }
622 tunnelMap.remove(pcepTunnelId);
623 service.tunnelRemoved(tunnel);
cheng fan48e832c2015-05-29 01:54:47 +0800624 }
625
626 @Override
627 public void tunnelUpdated(TunnelDescription tunnel) {
Avantika-Huawei56c11842016-04-28 00:56:56 +0530628 handleTunnelUpdate(tunnel, null);
629 }
630
631 public void tunnelUpdated(TunnelDescription tunnel, State tunnelState) {
632 handleTunnelUpdate(tunnel, tunnelState);
633 }
634
635 private void handleTunnelUpdate(TunnelDescription tunnel, State tunnelState) {
636 if (tunnel.type() == MPLS) {
Jonathan Hart51539b82015-10-29 09:53:04 -0700637 pcepTunnelApiMapper.removeFromCoreTunnelRequestQueue(tunnel.id());
Avantika-Huawei56c11842016-04-28 00:56:56 +0530638
639 if (tunnelState == null) {
640 service.tunnelUpdated(tunnel);
641 } else {
642 service.tunnelUpdated(tunnel, tunnelState);
643 }
644 return;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530645 }
cheng fan48e832c2015-05-29 01:54:47 +0800646
647 Tunnel tunnelOld = tunnelQueryById(tunnel.id());
648 if (tunnelOld.type() != Tunnel.Type.VLAN) {
cheng fan93258c72015-06-02 23:42:32 +0800649 error("Illegal tunnel type. Only support VLAN tunnel update.");
cheng fan48e832c2015-05-29 01:54:47 +0800650 return;
651 }
cheng fan7716ec92015-05-31 01:53:19 +0800652 long bandwidth = Long
653 .parseLong(tunnel.annotations().value("bandwidth"));
cheng fan48e832c2015-05-29 01:54:47 +0800654 if (bandwidth < MIN_BANDWIDTH || bandwidth > MAX_BANDWIDTH) {
cheng fan7716ec92015-05-31 01:53:19 +0800655 error("Update failed, invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800656 return;
657 }
Jonathan Hart51539b82015-10-29 09:53:04 -0700658 String pcepTunnelId = getPcepTunnelKey(tunnel.id());
cheng fan48e832c2015-05-29 01:54:47 +0800659
660 checkNotNull(pcepTunnelId, "Invalid tunnel id");
661 if (!controller.updateTunnelBandwidth(pcepTunnelId, bandwidth)) {
662
cheng fan7716ec92015-05-31 01:53:19 +0800663 error("Update failed,maybe invalid bandwidth.");
cheng fan48e832c2015-05-29 01:54:47 +0800664 return;
665
666 }
667 service.tunnelUpdated(tunnel);
668 }
669
cheng fan7716ec92015-05-31 01:53:19 +0800670 private void error(String info) {
671 System.err.println(info);
672 }
673
cheng fan48e832c2015-05-29 01:54:47 +0800674 // Short-hand for creating a connection point.
675 private ConnectPoint connectPoint(PcepDpid id, long port) {
676 return new ConnectPoint(deviceId(uri(id)), portNumber(port));
677 }
678
679 // Short-hand for creating a link.
680 private Link link(PcepDpid src, long sp, PcepDpid dst, long dp) {
Ray Milkey2693bda2016-01-22 16:08:14 -0800681 return DefaultLink.builder()
682 .providerId(id())
683 .src(connectPoint(src, sp))
684 .dst(connectPoint(dst, dp))
685 .type(Link.Type.TUNNEL)
686 .build();
cheng fan48e832c2015-05-29 01:54:47 +0800687 }
688
689 // Creates a path that leads through the given devices.
690 private Path createPath(List<PcepHopNodeDescription> hopList,
Jonathan Hart51539b82015-10-29 09:53:04 -0700691 PathType pathtype, PathState pathState) {
cheng fan48e832c2015-05-29 01:54:47 +0800692 if (hopList == null || hopList.size() == 0) {
693 return null;
694 }
695 List<Link> links = new ArrayList<>();
696 for (int i = 1; i < hopList.size() - 1; i = i + 2) {
697 links.add(link(hopList.get(i).getDeviceId(), hopList.get(i)
698 .getPortNum(), hopList.get(i + 1).getDeviceId(), hopList
699 .get(i + 1).getPortNum()));
700 }
701
702 int hopNum = hopList.size() - 2;
703 DefaultAnnotations extendAnnotations = DefaultAnnotations.builder()
704 .set("pathNum", String.valueOf(hopNum))
cheng fan93258c72015-06-02 23:42:32 +0800705 .set("pathState", String.valueOf(pathState))
cheng fan48e832c2015-05-29 01:54:47 +0800706 .set("pathType", String.valueOf(pathtype)).build();
707 return new DefaultPath(id(), links, hopNum, extendAnnotations);
708 }
709
710 // convert the path description to a string.
711 public String pathToString(List<Link> links) {
712 StringBuilder builder = new StringBuilder();
713 builder.append("{");
714 for (Link link : links) {
715 builder.append("(Device:" + link.src().deviceId() + " Port:"
716 + link.src().port().toLong());
717 builder.append(" Device:" + link.dst().deviceId() + " Port:"
718 + link.dst().port().toLong());
719 builder.append(")");
720 }
721 builder.append("}");
722 return builder.toString();
723 }
724
725 // build a TunnelDescription.
726 private TunnelDescription buildOpticalTunnel(PcepTunnel pcepTunnel,
727 TunnelId tunnelId) {
728 TunnelEndPoint srcPoint = null;
729 TunnelEndPoint dstPoint = null;
730 Tunnel.Type tunnelType = null;
731 TunnelName name = TunnelName.tunnelName(pcepTunnel.name());
732
733 // add path after codes of tunnel's path merged
734 Path path = createPath(pcepTunnel.getHopList(),
cheng fan93258c72015-06-02 23:42:32 +0800735 pcepTunnel.getPathType(),
736 pcepTunnel.getPathState());
cheng fan48e832c2015-05-29 01:54:47 +0800737
738 OpticalTunnelEndPoint.Type endPointType = null;
739 switch (pcepTunnel.type()) {
740 case OCH:
741 tunnelType = Tunnel.Type.OCH;
742 endPointType = OpticalTunnelEndPoint.Type.LAMBDA;
743 break;
744
745 case OTN:
746 tunnelType = Tunnel.Type.ODUK;
747 endPointType = OpticalTunnelEndPoint.Type.TIMESLOT;
748 break;
749
750 case UNI:
751 tunnelType = Tunnel.Type.VLAN;
752 endPointType = null;
753 break;
754
755 default:
756 break;
757 }
758 DeviceId srcDid = deviceId(uri(pcepTunnel.srcDeviceID()));
759 DeviceId dstDid = deviceId(uri(pcepTunnel.dstDeviceId()));
760 PortNumber srcPort = PortNumber.portNumber(pcepTunnel.srcPort());
761 PortNumber dstPort = PortNumber.portNumber(pcepTunnel.dstPort());
762
763 srcPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(srcDid),
764 Optional.of(srcPort), null,
765 endPointType,
766 OpticalLogicId.logicId(0),
767 true);
768 dstPoint = new DefaultOpticalTunnelEndPoint(id(), Optional.of(dstDid),
769 Optional.of(dstPort), null,
770 endPointType,
771 OpticalLogicId.logicId(0),
772 true);
773
774 // basic annotations
cheng fan7716ec92015-05-31 01:53:19 +0800775 DefaultAnnotations annotations = DefaultAnnotations
776 .builder()
cheng fan48e832c2015-05-29 01:54:47 +0800777 .set("SLA", String.valueOf(pcepTunnel.getSla()))
cheng fan7716ec92015-05-31 01:53:19 +0800778 .set("bandwidth",
779 String.valueOf(pcepTunnel.bandWidth()) + BANDWIDTH_UINT)
cheng fan48e832c2015-05-29 01:54:47 +0800780 .set("index", String.valueOf(pcepTunnel.id())).build();
781
cheng fan48e832c2015-05-29 01:54:47 +0800782 // a VLAN tunnel always carry OCH tunnel, this annotation is the index
783 // of a OCH tunnel.
cheng fan93258c72015-06-02 23:42:32 +0800784 if (pcepTunnel.underlayTunnelId() != 0) {
cheng fan48e832c2015-05-29 01:54:47 +0800785 DefaultAnnotations extendAnnotations = DefaultAnnotations
786 .builder()
787 .set("underLayTunnelIndex",
cheng fan93258c72015-06-02 23:42:32 +0800788 String.valueOf(pcepTunnel.underlayTunnelId())).build();
cheng fan48e832c2015-05-29 01:54:47 +0800789 annotations = DefaultAnnotations.merge(annotations,
790 extendAnnotations);
791
792 }
793 TunnelDescription tunnel = new DefaultTunnelDescription(
794 tunnelId,
795 srcPoint,
796 dstPoint,
797 tunnelType,
798 new DefaultGroupId(
799 0),
800 id(), name,
801 path,
802 annotations);
803 return tunnel;
cheng fan48e832c2015-05-29 01:54:47 +0800804 }
805
806 /**
807 * Get the tunnelID according to the tunnel key.
808 *
809 * @param tunnelKey tunnel key
810 * @return corresponding tunnel id of the a tunnel key.
811 */
812 private TunnelId getTunnelId(String tunnelKey) {
cheng fan48e832c2015-05-29 01:54:47 +0800813 for (String key : tunnelMap.keySet()) {
814 if (key.equals(tunnelKey)) {
815 return tunnelMap.get(key);
816 }
817 }
818 return null;
819 }
820
821 /**
822 * Get the tunnel key according to the tunnelID.
823 *
824 * @param tunnelId tunnel id
825 * @return corresponding a tunnel key of the tunnel id.
826 */
Jonathan Hart51539b82015-10-29 09:53:04 -0700827 private String getPcepTunnelKey(TunnelId tunnelId) {
cheng fan48e832c2015-05-29 01:54:47 +0800828 for (String key : tunnelMap.keySet()) {
829 if (tunnelMap.get(key).id() == tunnelId.id()) {
830 return key;
831 }
832 }
833 return null;
834
835 }
836
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530837 /**
chengfan2fff70f2015-08-24 18:20:19 -0500838 * Build a DefaultTunnelStatistics from a PcepTunnelStatistics.
839 *
840 * @param statistics statistics data from a PCEP tunnel
841 * @return TunnelStatistics
842 */
843 private TunnelStatistics buildTunnelStatistics(PcepTunnelStatistics statistics) {
844 DefaultTunnelStatistics.Builder builder = new DefaultTunnelStatistics.Builder();
845 DefaultTunnelStatistics tunnelStatistics = builder.setBwUtilization(statistics.bandwidthUtilization())
846 .setPacketLossRatio(statistics.packetLossRate())
847 .setFlowDelay(statistics.flowDelay())
848 .setAlarms(statistics.alarms())
849 .build();
850 return tunnelStatistics;
851 }
852 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530853 * Creates list of hops for ERO object from Path.
854 *
855 * @param path network path
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530856 * @return list of ERO subobjects
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530857 */
858 private LinkedList<PcepValueType> createPcepPath(Path path) {
859 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
860 List<Link> listLink = path.links();
861 ConnectPoint source = null;
862 ConnectPoint destination = null;
863 IpAddress ipDstAddress = null;
864 IpAddress ipSrcAddress = null;
865 PcepValueType subObj = null;
Priyanka Bcdf9b102016-06-07 20:01:38 +0530866 long portNo;
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530867
868 for (Link link : listLink) {
869 source = link.src();
870 if (!(source.equals(destination))) {
871 //set IPv4SubObject for ERO object
Priyanka Bcdf9b102016-06-07 20:01:38 +0530872 portNo = source.port().toLong();
873 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
874 ipSrcAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530875 subObj = new IPv4SubObject(ipSrcAddress.getIp4Address().toInt());
876 llSubObjects.add(subObj);
877 }
878
879 destination = link.dst();
Priyanka Bcdf9b102016-06-07 20:01:38 +0530880 portNo = destination.port().toLong();
881 portNo = ((portNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? portNo & SET : portNo;
882 ipDstAddress = Ip4Address.valueOf((int) portNo);
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530883 subObj = new IPv4SubObject(ipDstAddress.getIp4Address().toInt());
884 llSubObjects.add(subObj);
885 }
Priyanka Bcdf9b102016-06-07 20:01:38 +0530886
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530887 return llSubObjects;
888 }
889
890 /**
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530891 * Creates label stack for ERO object from network resource.
892 *
893 * @param labelStack
894 * @param path (hop list)
895 * @return list of ERO subobjects
896 */
897 private LinkedList<PcepValueType> createPcepLabelStack(DefaultLabelStack labelStack, Path path) {
898 checkNotNull(labelStack);
899
900 LinkedList<PcepValueType> llSubObjects = new LinkedList<PcepValueType>();
901 Iterator<Link> links = path.links().iterator();
902 LabelResourceId label = null;
903 Link link = null;
904 PcepValueType subObj = null;
905 PcepNai nai = null;
906 Device dstNode = null;
907 long srcPortNo, dstPortNo;
908
909 ListIterator<LabelResourceId> labelListIterator = labelStack.labelResources().listIterator();
910 while (labelListIterator.hasNext()) {
911 label = labelListIterator.next();
912 link = links.next();
913
914 srcPortNo = link.src().port().toLong();
915 srcPortNo = ((srcPortNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? srcPortNo & SET : srcPortNo;
916
917 dstPortNo = link.dst().port().toLong();
918 dstPortNo = ((dstPortNo & IDENTIFIER_SET) == IDENTIFIER_SET) ? dstPortNo & SET : dstPortNo;
919
920 nai = new PcepNaiIpv4Adjacency((int) srcPortNo, (int) dstPortNo);
921 subObj = new SrEroSubObject(PcepNaiIpv4Adjacency.ST_TYPE, false, false, false, true, (int) label.labelId(),
922 nai);
923 llSubObjects.add(subObj);
924
925 dstNode = deviceService.getDevice(link.dst().deviceId());
926 nai = new PcepNaiIpv4NodeId(Ip4Address.valueOf(dstNode.annotations().value(LSRID)).toInt());
927
928 if (!labelListIterator.hasNext()) {
929 log.error("Malformed label stack.");
930 }
931 label = labelListIterator.next();
932 subObj = new SrEroSubObject(PcepNaiIpv4NodeId.ST_TYPE, false, false, false, true, (int) label.labelId(),
933 nai);
934 llSubObjects.add(subObj);
935 }
936 return llSubObjects;
937 }
938
939 /**
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530940 * Creates PcInitiated lsp request list for setup tunnel.
941 *
942 * @param tunnel mpls tunnel
943 * @param path network path
944 * @param pc pcep client
945 * @param srpId unique id for pcep message
946 * @return list of PcInitiatedLspRequest
947 * @throws PcepParseException while building pcep objects fails
948 */
949 LinkedList<PcInitiatedLspRequest> createPcInitiatedLspReqList(Tunnel tunnel, Path path,
950 PcepClient pc, int srpId)
951 throws PcepParseException {
952 PcepValueType tlv;
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530953 LinkedList<PcepValueType> llSubObjects = null;
954
955 NetworkResource labelStack = tunnel.resource();
956 if (labelStack != null && labelStack instanceof DefaultLabelStack) {
957 llSubObjects = createPcepLabelStack((DefaultLabelStack) labelStack, path);
958 } else {
959 llSubObjects = createPcepPath(path);
960 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530961
Sho SHIMIZUde09fa02015-09-03 09:39:52 -0700962 if (llSubObjects == null || llSubObjects.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530963 log.error("There is no link information to create tunnel");
964 return null;
965 }
966
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530967 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530968
969 // set PathSetupTypeTlv of SRP object
970 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)).type());
971 llOptionalTlv.add(tlv);
972
973 // build SRP object
974 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
975 .setOptionalTlv(llOptionalTlv).build();
976
977 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530978 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
Avantika-Huawei56c11842016-04-28 00:56:56 +0530979
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530980 // set LSP identifiers TLV
Avantika-Huawei56c11842016-04-28 00:56:56 +0530981 short localLspId = 0;
982 if (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) != WITH_SIGNALLING) {
983 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
984 if (localLspIdString != null) {
985 localLspId = Short.valueOf(localLspIdString);
986 }
987 }
988
Priyanka B4c3cef02016-06-14 20:27:53 +0530989 tunnel.annotations().value(LSP_SIG_TYPE);
Avantika-Huawei56c11842016-04-28 00:56:56 +0530990 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt()),
991 localLspId, (short) 0, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
992 .getIp4Address().toInt()));
Phanendra Manda37b97fb2015-08-15 02:04:24 +0530993 llOptionalTlv.add(tlv);
994 //set SymbolicPathNameTlv of LSP object
995 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
996 llOptionalTlv.add(tlv);
997
998 //build LSP object
Avantika-Huaweif849aab2016-06-21 22:29:15 +0530999 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true).setDFlag(true).setOFlag((byte) 0)
1000 .setPlspId(0).setOptionalTlv(llOptionalTlv).build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301001
1002 //build ENDPOINTS object
1003 PcepEndPointsObject endpointsobj = pc.factory().buildEndPointsObject()
1004 .setSourceIpAddress(((IpTunnelEndPoint) tunnel.src()).ip().getIp4Address().toInt())
1005 .setDestIpAddress(((IpTunnelEndPoint) tunnel.dst()).ip().getIp4Address().toInt())
1006 .setPFlag(true).build();
1007
1008 //build ERO object
1009 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
1010
Priyanka B4c3cef02016-06-14 20:27:53 +05301011 int iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301012 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301013 iBandwidth = Float.floatToIntBits(Float.parseFloat(tunnel.annotations().value(BANDWIDTH)));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301014 }
1015 // build bandwidth object
1016 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1017 // build pcep attribute
1018 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1019
1020 PcInitiatedLspRequest initiateLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1021 .setLspObject(lspobj).setEndPointsObject(endpointsobj).setEroObject(eroobj)
1022 .setPcepAttribute(pcepAttribute).build();
1023 llPcInitiatedLspRequestList.add(initiateLspRequest);
1024 return llPcInitiatedLspRequestList;
1025 }
1026
1027 /**
1028 * To send initiate tunnel message to pcc.
1029 *
1030 * @param tunnel mpls tunnel info
1031 * @param path explicit route for the tunnel
1032 * @param pc pcep client to send message
1033 */
1034 private void pcepSetupTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1035 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301036 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1037 log.error("Setup tunnel has failed as LSP DB sync is not finished");
1038 return;
1039 }
1040
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301041 int srpId = SrpIdGenerators.create();
Priyanka B4c3cef02016-06-14 20:27:53 +05301042 Collection<Tunnel> tunnels = tunnelService.queryTunnel(tunnel.src(), tunnel.dst());
1043 for (Tunnel t : tunnels) {
1044 if (t.tunnelName().equals(tunnel.tunnelName())) {
1045 tunnel = new DefaultTunnel(tunnel.providerId(), tunnel.src(),
1046 tunnel.dst(), tunnel.type(),
1047 t.state(), tunnel.groupId(),
1048 t.tunnelId(),
1049 tunnel.tunnelName(),
1050 tunnel.path(),
1051 tunnel.resource(),
1052 tunnel.annotations());
1053 break;
1054 }
1055 }
1056
1057 if (tunnel.tunnelId() == null) {
1058 log.error("Tunnel ID not found");
1059 return;
1060 }
1061
Avantika-Huawei56c11842016-04-28 00:56:56 +05301062 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, CREATE);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301063
Jonathan Hart51539b82015-10-29 09:53:04 -07001064 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301065
1066 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = createPcInitiatedLspReqList(tunnel, path,
1067 pc, srpId);
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001068 if (llPcInitiatedLspRequestList == null || llPcInitiatedLspRequestList.size() == 0) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301069 log.error("Failed to create PcInitiatedLspRequestList");
1070 return;
1071 }
1072
1073 //build PCInitiate message
1074 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1075 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList)
1076 .build();
1077
1078 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1079
Jonathan Hart51539b82015-10-29 09:53:04 -07001080 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301081 } catch (PcepParseException e) {
1082 log.error("PcepParseException occurred while processing setup tunnel {}", e.getMessage());
1083 }
1084 }
1085
1086 /**
1087 * To send Release tunnel message to pcc.
1088 *
1089 * @param tunnel mpls tunnel info
1090 * @param pc pcep client to send message
1091 */
1092 private void pcepReleaseTunnel(Tunnel tunnel, PcepClient pc) {
1093 try {
MaheshRaju-Huaweibb591072016-06-17 17:47:16 +05301094 if (!(pc.lspDbSyncStatus().equals(PcepSyncStatus.SYNCED))) {
1095 log.error("Release tunnel has failed as LSP DB sync is not finished");
1096 return;
1097 }
1098
Avantika-Huawei56c11842016-04-28 00:56:56 +05301099 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, DELETE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001100 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301101 int srpId = SrpIdGenerators.create();
1102 TunnelId tunnelId = tunnel.tunnelId();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301103
1104 PcepValueType tlv;
1105 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301106
1107 // set PathSetupTypeTlv of SRP object
1108 tlv = new PathSetupTypeTlv(LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE))
1109 .type());
1110 llOptionalTlv.add(tlv);
1111
1112 // build SRP object
1113 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(true)
1114 .setOptionalTlv(llOptionalTlv).build();
1115
1116 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301117 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = new LinkedList<PcInitiatedLspRequest>();
1118
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301119 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1120 llOptionalTlv.add(tlv);
Priyanka B4c3cef02016-06-14 20:27:53 +05301121
1122 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1123 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301124 String pLspIdString = tunnel.annotations().value(PLSP_ID);
1125
Priyanka B4c3cef02016-06-14 20:27:53 +05301126 short localLspId = 0;
1127 short pccTunnelId = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301128 int plspId = 0;
Priyanka B4c3cef02016-06-14 20:27:53 +05301129
1130 if (localLspIdString != null) {
1131 localLspId = Short.valueOf(localLspIdString);
1132 }
1133
1134 if (pccTunnelIdString != null) {
1135 pccTunnelId = Short.valueOf(pccTunnelIdString);
1136 }
1137
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301138 if (pLspIdString != null) {
1139 plspId = Integer.valueOf(pLspIdString);
1140 }
1141
Priyanka B4c3cef02016-06-14 20:27:53 +05301142 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1143 .ip().getIp4Address().toInt()),
1144 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1145 .getIp4Address().toInt()));
1146 llOptionalTlv.add(tlv);
1147
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301148 // build lsp object, set r flag as false to delete the tunnel
1149 PcepLspObject lspobj = pc.factory().buildLspObject().setRFlag(false).setPlspId(plspId)
1150 .setOptionalTlv(llOptionalTlv).build();
1151
1152 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest().setSrpObject(srpobj)
1153 .setLspObject(lspobj).build();
1154
1155 llPcInitiatedLspRequestList.add(releaseLspRequest);
1156
1157 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1158 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1159
1160 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1161
Jonathan Hart51539b82015-10-29 09:53:04 -07001162 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301163 } catch (PcepParseException e) {
1164 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1165 }
1166 }
1167
1168 /**
1169 * To send Update tunnel request message to pcc.
1170 *
1171 * @param tunnel mpls tunnel info
1172 * @param path explicit route for the tunnel
1173 * @param pc pcep client to send message
1174 */
1175 private void pcepUpdateTunnel(Tunnel tunnel, Path path, PcepClient pc) {
1176 try {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301177 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnel, path, UPDATE);
Jonathan Hart51539b82015-10-29 09:53:04 -07001178 pcepTunnelApiMapper.addToCoreTunnelRequestQueue(pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301179 int srpId = SrpIdGenerators.create();
1180 TunnelId tunnelId = tunnel.tunnelId();
1181 PcepValueType tlv;
1182 int plspId = 0;
1183
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301184 LinkedList<PcepValueType> llSubObjects = null;
1185 NetworkResource labelStack = tunnel.resource();
1186 if (labelStack != null && labelStack instanceof DefaultLabelStack) {
1187 llSubObjects = createPcepLabelStack((DefaultLabelStack) labelStack, path);
1188 } else {
1189 llSubObjects = createPcepPath(path);
1190 }
1191
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301192 LinkedList<PcepValueType> llOptionalTlv = new LinkedList<PcepValueType>();
1193 LinkedList<PcepUpdateRequest> llUpdateRequestList = new LinkedList<PcepUpdateRequest>();
1194
Avantika-Huawei56c11842016-04-28 00:56:56 +05301195 // set PathSetupTypeTlv of SRP object
1196 LspType lspSigType = LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE));
1197 tlv = new PathSetupTypeTlv(lspSigType.type());
1198 llOptionalTlv.add(tlv);
1199
1200 // build SRP object
1201 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(srpId).setRFlag(false)
1202 .setOptionalTlv(llOptionalTlv).build();
1203
1204 llOptionalTlv = new LinkedList<PcepValueType>();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301205
Avantika-Huawei56c11842016-04-28 00:56:56 +05301206 if (lspSigType != WITH_SIGNALLING) {
1207 String localLspIdString = tunnel.annotations().value(LOCAL_LSP_ID);
1208 String pccTunnelIdString = tunnel.annotations().value(PCC_TUNNEL_ID);
1209 short localLspId = 0;
1210 short pccTunnelId = 0;
1211
1212 if (localLspIdString != null) {
1213 localLspId = Short.valueOf(localLspIdString);
1214 }
1215
1216 if (pccTunnelIdString != null) {
1217 pccTunnelId = Short.valueOf(pccTunnelIdString);
1218 }
1219
1220 tlv = new StatefulIPv4LspIdentifiersTlv((((IpTunnelEndPoint) tunnel.src())
1221 .ip().getIp4Address().toInt()),
1222 localLspId, pccTunnelId, 0, (((IpTunnelEndPoint) tunnel.dst()).ip()
1223 .getIp4Address().toInt()));
1224 llOptionalTlv.add(tlv);
1225 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301226
1227 if (tunnel.tunnelName().value() != null) {
1228 tlv = new SymbolicPathNameTlv(tunnel.tunnelName().value().getBytes());
1229 llOptionalTlv.add(tlv);
1230 }
1231
1232 // build lsp object
Priyanka B4c3cef02016-06-14 20:27:53 +05301233 PcepLspObject lspobj = pc.factory().buildLspObject().setAFlag(true)
1234 .setPlspId(Integer.valueOf(tunnel.annotations().value(PLSP_ID)))
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301235 .setOptionalTlv(llOptionalTlv).build();
1236 // build ero object
1237 PcepEroObject eroobj = pc.factory().buildEroObject().setSubObjects(llSubObjects).build();
1238
Priyanka B4c3cef02016-06-14 20:27:53 +05301239 float iBandwidth = DEFAULT_BANDWIDTH_VALUE;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301240 if (tunnel.annotations().value(BANDWIDTH) != null) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301241 iBandwidth = Float.parseFloat(tunnel.annotations().value(BANDWIDTH));
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301242 }
1243 // build bandwidth object
1244 PcepBandwidthObject bandwidthObject = pc.factory().buildBandwidthObject().setBandwidth(iBandwidth).build();
1245 // build pcep attribute
1246 PcepAttribute pcepAttribute = pc.factory().buildPcepAttribute().setBandwidthObject(bandwidthObject).build();
1247 // build pcep msg path
1248 PcepMsgPath msgPath = pc.factory().buildPcepMsgPath().setEroObject(eroobj).setPcepAttribute(pcepAttribute)
1249 .build();
1250
1251 PcepUpdateRequest updateRequest = pc.factory().buildPcepUpdateRequest().setSrpObject(srpobj)
1252 .setLspObject(lspobj).setMsgPath(msgPath).build();
1253
1254 llUpdateRequestList.add(updateRequest);
1255
1256 PcepUpdateMsg pcUpdateMsg = pc.factory().buildUpdateMsg().setUpdateRequestList(llUpdateRequestList).build();
1257
1258 pc.sendMessage(Collections.singletonList(pcUpdateMsg));
Jonathan Hart51539b82015-10-29 09:53:04 -07001259 pcepTunnelApiMapper.addToTunnelRequestQueue(srpId, pcepTunnelData);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301260 } catch (PcepParseException e) {
1261 log.error("PcepParseException occurred while processing release tunnel {}", e.getMessage());
1262 }
1263 }
1264
chengfan2fff70f2015-08-24 18:20:19 -05001265
1266
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301267 private class InnerTunnelProvider implements PcepTunnelListener, PcepEventListener, PcepClientListener {
cheng fan48e832c2015-05-29 01:54:47 +08001268
1269 @Override
Jonathan Hart51539b82015-10-29 09:53:04 -07001270 public void handlePcepTunnel(PcepTunnel pcepTunnel) {
cheng fan48e832c2015-05-29 01:54:47 +08001271 TunnelDescription tunnel = null;
1272 // instance and id identify a tunnel together
1273 String tunnelKey = String.valueOf(pcepTunnel.getInstance())
1274 + String.valueOf(pcepTunnel.id());
1275
1276 if (tunnelKey == null || "".equals(tunnelKey)) {
1277 log.error("Invalid PCEP tunnel");
1278 return;
1279 }
1280
1281 TunnelId tunnelId = getTunnelId(tunnelKey);
1282
1283 tunnel = buildOpticalTunnel(pcepTunnel, tunnelId);
1284
1285 OperationType operType = pcepTunnel.getOperationType();
1286 switch (operType) {
1287 case ADD:
1288 tunnelId = service.tunnelAdded(tunnel);
1289 tunnelMap.put(tunnelKey, tunnelId);
1290 break;
1291
1292 case UPDATE:
1293 service.tunnelUpdated(tunnel);
1294 break;
1295
1296 case DELETE:
1297 service.tunnelRemoved(tunnel);
1298 tunnelMap.remove(tunnelKey);
1299 break;
1300
1301 default:
1302 log.error("Invalid tunnel operation");
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301303 }
1304 }
cheng fan48e832c2015-05-29 01:54:47 +08001305
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301306 @Override
1307 public void handleMessage(PccId pccId, PcepMessage msg) {
1308 try {
1309 log.debug("tunnel provider handle message {}", msg.getType().toString());
1310 switch (msg.getType()) {
1311 case REPORT:
1312 int srpId = 0;
1313 LinkedList<PcepStateReport> llStateReportList = null;
1314 llStateReportList = ((PcepReportMsg) msg).getStateReportList();
1315 ListIterator<PcepStateReport> listIterator = llStateReportList.listIterator();
1316 PcepSrpObject srpObj = null;
1317 PcepLspObject lspObj = null;
1318 while (listIterator.hasNext()) {
1319 PcepStateReport stateRpt = listIterator.next();
1320 srpObj = stateRpt.getSrpObject();
1321 lspObj = stateRpt.getLspObject();
1322
1323 if (srpObj instanceof PcepSrpObject) {
1324 srpId = srpObj.getSrpID();
1325 }
1326
1327 log.debug("Plsp ID in handle message " + lspObj.getPlspId());
1328 log.debug("SRP ID in handle message " + srpId);
1329
Jonathan Hart51539b82015-10-29 09:53:04 -07001330 if (!(pcepTunnelApiMapper.checkFromTunnelRequestQueue(srpId))) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301331 // For PCRpt without matching SRP id.
1332 handleRptWithoutSrpId(stateRpt, pccId);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301333 continue;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301334 }
1335
Avantika-Huawei56c11842016-04-28 00:56:56 +05301336 handleReportMessage(srpId, lspObj, stateRpt);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301337 }
1338 break;
1339
1340 default:
1341 log.debug("Received unsupported message type {}", msg.getType().toString());
1342 }
1343 } catch (Exception e) {
1344 log.error("Exception occured while processing report message {}", e.getMessage());
1345 }
1346 }
1347
1348 /**
1349 * Handles report message for setup/update/delete tunnel request.
1350 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301351 * @param srpId unique identifier for PCEP message
1352 * @param lspObj LSP object
1353 * @param stateRpt parsed PCEP report msg.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301354 */
Avantika-Huawei56c11842016-04-28 00:56:56 +05301355 private void handleReportMessage(int srpId, PcepLspObject lspObj, PcepStateReport stateRpt) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301356 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Jonathan Hart51539b82015-10-29 09:53:04 -07001357 PcepTunnelData pcepTunnelData = pcepTunnelApiMapper.getDataFromTunnelRequestQueue(srpId);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301358
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301359 // store the values required from report message
1360 pcepTunnelData.setPlspId(lspObj.getPlspId());
1361 pcepTunnelData.setLspAFlag(lspObj.getAFlag());
1362 pcepTunnelData.setLspOFlag(lspObj.getOFlag());
1363 pcepTunnelData.setLspDFlag(lspObj.getDFlag());
1364
Avantika-Huawei56c11842016-04-28 00:56:56 +05301365 StatefulIPv4LspIdentifiersTlv ipv4LspTlv = null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301366 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1367 while (listTlvIterator.hasNext()) {
1368 PcepValueType tlv = listTlvIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301369 if (tlv.getType() == StatefulIPv4LspIdentifiersTlv.TYPE) {
1370 ipv4LspTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301371 break;
1372 }
1373 }
Sho SHIMIZUde09fa02015-09-03 09:39:52 -07001374 if (ipv4LspTlv != null) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301375 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspTlv);
cheng fan48e832c2015-05-29 01:54:47 +08001376 }
1377
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301378 Path path = pcepTunnelData.path();
1379 Tunnel tunnel = pcepTunnelData.tunnel();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301380 Builder annotationBuilder = DefaultAnnotations.builder();
1381 annotationBuilder.putAll(pcepTunnelData.tunnel().annotations());
1382
1383 // PCRpt in response to PCInitate msg will carry PLSP id allocated by PCC.
1384 if (tunnel.annotations().value(PLSP_ID) == null) {
1385 annotationBuilder.set(PLSP_ID, String.valueOf(lspObj.getPlspId()));
1386 }
1387
1388 // Signalled LSPs will carry local LSP id allocated by signalling protocol(PCC).
1389 if (tunnel.annotations().value(LOCAL_LSP_ID) == null) {
1390 annotationBuilder.set(LOCAL_LSP_ID, String.valueOf(ipv4LspTlv.getLspId()));
1391 }
1392
Priyanka B4c3cef02016-06-14 20:27:53 +05301393 if (tunnel.annotations().value(PCC_TUNNEL_ID) == null) {
1394 annotationBuilder.set(PCC_TUNNEL_ID, String.valueOf(ipv4LspTlv.getTunnelId()));
1395 }
1396
Avantika-Huawei56c11842016-04-28 00:56:56 +05301397 SparseAnnotations annotations = annotationBuilder.build();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301398 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(),
1399 tunnel.dst(), tunnel.type(), tunnel.groupId(),
1400 providerId, tunnel.tunnelName(), path,
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301401 tunnel.resource(), annotations);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301402
Avantika-Huawei56c11842016-04-28 00:56:56 +05301403 if (CREATE == pcepTunnelData.requestType()) {
Priyanka B4c3cef02016-06-14 20:27:53 +05301404 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
Jonathan Hart51539b82015-10-29 09:53:04 -07001405 pcepTunnelApiMapper.handleCreateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301406 } else if (DELETE == pcepTunnelData.requestType()) {
Jonathan Hart51539b82015-10-29 09:53:04 -07001407 pcepTunnelApiMapper.handleRemoveFromTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301408 } else if (UPDATE == pcepTunnelData.requestType()) {
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301409 pcepTunnelData.setRptFlag(true);
Jonathan Hart51539b82015-10-29 09:53:04 -07001410 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1411 pcepTunnelApiMapper.handleUpdateTunnelRequestQueue(srpId, pcepTunnelData);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301412 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301413
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301414 PcepLspStatus pcepLspStatus = PcepLspStatus.values()[lspObj.getOFlag()];
1415
Avantika-Huawei56c11842016-04-28 00:56:56 +05301416 if (lspObj.getRFlag()) {
1417 tunnelRemoved(td);
1418 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301419 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(pcepLspStatus);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301420 tunnelUpdated(td, tunnelState);
1421 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301422
1423 // SR-TE also needs PCUpd msg after receiving PCRpt with status GOING-UP even
1424 // though there are no labels to download for SR-TE.
1425 if ((pcepLspStatus == PcepLspStatus.GOING_UP)
1426 && (LspType.valueOf(tunnel.annotations().value(LSP_SIG_TYPE)) == SR_WITHOUT_SIGNALLING)) {
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301427 // Query again to get latest tunnel updated with protocol values from PCRpt msg.
1428 updateTunnel(service.tunnelQueryById(tunnel.tunnelId()), tunnel.path());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301429 }
Avantika-Huawei56c11842016-04-28 00:56:56 +05301430 }
1431
Priyanka B413fbe82016-05-26 11:44:45 +05301432 private SparseAnnotations getAnnotations(PcepLspObject lspObj, StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv,
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301433 float bandwidth, LspType lspType, String costType) {
1434
1435 Builder builder = DefaultAnnotations.builder();
1436
1437 /*
1438 * [RFC 5440] The absence of the METRIC object MUST be interpreted by the PCE as a path computation request
1439 * for which no constraints need be applied to any of the metrics.
1440 */
1441 if (costType != null) {
1442 builder.set(COST_TYPE, costType);
1443 }
1444
1445 SparseAnnotations annotations = builder
Priyanka B413fbe82016-05-26 11:44:45 +05301446 .set(BANDWIDTH, (new Float(bandwidth)).toString()).set(LSP_SIG_TYPE, lspType.name())
1447 .set(PCC_TUNNEL_ID, String.valueOf(ipv4LspIdenTlv.getTunnelId()))
1448 .set(PLSP_ID, String.valueOf(lspObj.getPlspId()))
1449 .set(LOCAL_LSP_ID, String.valueOf(ipv4LspIdenTlv.getLspId()))
1450 .set(DELEGATE, String.valueOf(lspObj.getDFlag()))
1451 .build();
1452 return annotations;
1453 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301454
Priyanka B413fbe82016-05-26 11:44:45 +05301455 private LspType getLspType(PcepSrpObject srpObj) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301456 LspType lspType = WITH_SIGNALLING;
1457
1458 if (null != srpObj) {
1459 LinkedList<PcepValueType> llOptionalTlv = srpObj.getOptionalTlv();
1460 ListIterator<PcepValueType> listIterator = llOptionalTlv.listIterator();
1461
1462 while (listIterator.hasNext()) {
1463 PcepValueType tlv = listIterator.next();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301464 switch (tlv.getType()) {
1465 case PathSetupTypeTlv.TYPE:
1466 lspType = LspType.values()[Integer.valueOf(((PathSetupTypeTlv) tlv).getPst())];
1467 break;
Avantika-Huawei56c11842016-04-28 00:56:56 +05301468 default:
1469 break;
1470 }
1471 }
1472 }
Priyanka B413fbe82016-05-26 11:44:45 +05301473 return lspType;
1474 }
1475
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301476 private void handleRptWithoutSrpId(PcepStateReport stateRpt, PccId pccId) {
Priyanka B413fbe82016-05-26 11:44:45 +05301477 ProviderId providerId = new ProviderId("pcep", PROVIDER_ID);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301478 String costType = null;
Priyanka B413fbe82016-05-26 11:44:45 +05301479 PcepStateReport.PcepMsgPath msgPath = stateRpt.getMsgPath();
1480 checkNotNull(msgPath);
1481 PcepEroObject eroObj = msgPath.getEroObject();
1482 if (eroObj == null) {
1483 log.error("ERO object is null in report message.");
1484 return;
1485 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301486
1487 PcepAttribute attributes = msgPath.getPcepAttribute();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301488 float bandwidth = 0;
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301489 int cost = 0;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301490 if (attributes != null) {
1491 if (attributes.getMetricObjectList() != null) {
1492 ListIterator<PcepMetricObject> iterator = attributes.getMetricObjectList().listIterator();
1493 PcepMetricObject metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301494
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301495 while (metricObj != null) {
1496 if (metricObj.getBType() == IGP_METRIC) {
1497 costType = "COST";
1498 } else if (metricObj.getBType() == TE_METRIC) {
1499 costType = "TE_COST";
1500 }
1501 if (costType != null) {
1502 cost = metricObj.getMetricVal();
1503 log.debug("Path cost {}", cost);
1504 break;
1505 }
1506 metricObj = iterator.next();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301507 }
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301508 }
1509 if (attributes.getBandwidthObject() != null) {
1510 bandwidth = attributes.getBandwidthObject().getBandwidth();
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301511 }
1512 }
1513
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301514 List<Object> eroSubObjList = buildPathFromEroObj(eroObj, providerId);
1515 List<Link> links = new ArrayList<>();
1516 List<LabelResourceId> labels = new ArrayList<>();
1517 for (Object linkOrLabel : eroSubObjList) {
1518 if (linkOrLabel instanceof Link) {
1519 links.add((Link) linkOrLabel);
1520 } else if (linkOrLabel instanceof Integer) {
1521 labels.add(LabelResourceId.labelResourceId(((Integer) linkOrLabel).longValue()));
1522 }
Priyanka B413fbe82016-05-26 11:44:45 +05301523 }
1524
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301525 Path path = new DefaultPath(providerId, links, cost, EMPTY);
1526 NetworkResource labelStack = new DefaultLabelStack(labels);
1527
1528 // To carry PST TLV, SRP object can be present with value 0 even when PCRpt is not in response to any action
1529 // from PCE.
Priyanka B413fbe82016-05-26 11:44:45 +05301530 PcepSrpObject srpObj = stateRpt.getSrpObject();
1531 LspType lspType = getLspType(srpObj);
Avantika-Huawei56c11842016-04-28 00:56:56 +05301532
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301533 PcepLspObject lspObj = stateRpt.getLspObject();
1534 ListIterator<PcepValueType> listTlvIterator = lspObj.getOptionalTlv().listIterator();
1535 StatefulIPv4LspIdentifiersTlv ipv4LspIdenTlv = null;
1536 SymbolicPathNameTlv pathNameTlv = null;
1537
1538 while (listTlvIterator.hasNext()) {
1539 PcepValueType tlv = listTlvIterator.next();
1540 switch (tlv.getType()) {
1541 case StatefulIPv4LspIdentifiersTlv.TYPE:
1542 ipv4LspIdenTlv = (StatefulIPv4LspIdentifiersTlv) tlv;
1543 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301544 case SymbolicPathNameTlv.TYPE:
1545 pathNameTlv = (SymbolicPathNameTlv) tlv;
1546 break;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301547 default:
1548 break;
1549 }
1550 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301551 /*
1552 * Draft says: The LSP-IDENTIFIERS TLV MUST be included in the LSP object in PCRpt messages for
1553 * RSVP-signaled LSPs. For ONOS PCECC implementation, it is mandatory.
1554 */
1555 if (ipv4LspIdenTlv == null) {
1556 log.error("Stateful IPv4 identifier TLV is null in PCRpt msg.");
1557 return;
1558 }
1559
1560 IpTunnelEndPoint tunnelEndPointSrc = IpTunnelEndPoint
1561 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4IngressAddress()));
1562 IpTunnelEndPoint tunnelEndPointDst = IpTunnelEndPoint
1563 .ipTunnelPoint(IpAddress.valueOf(ipv4LspIdenTlv.getIpv4EgressAddress()));
1564 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(tunnelEndPointSrc, tunnelEndPointDst);
1565
Priyanka B413fbe82016-05-26 11:44:45 +05301566 // Store delegation flag info and that LSP info because only delegated PCE sends update message
1567 // Storing if D flag is set, if not dont store. while checking whether delegation if annotation for D flag
1568 // not present then non-delegated , if present it is delegated.
1569 if (lspObj.getDFlag()) {
1570 pcepClientController.getClient(pccId).setLspAndDelegationInfo(
1571 new LspKey(lspObj.getPlspId(), ipv4LspIdenTlv.getLspId()), lspObj.getDFlag());
1572 }
1573
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301574 Tunnel tunnel = null;
1575 // Asynchronous status change message from PCC for LSP reported earlier.
1576 for (Tunnel tunnelObj : tunnelQueryResult) {
1577 if (tunnelObj.annotations().value(PLSP_ID) == null) {
1578 /*
1579 * PLSP_ID is null while Tunnel is created at PCE and PCInit msg carries it as 0. It is allocated by
1580 * PCC and in that case it becomes the first PCRpt msg from PCC for this LSP, and hence symbolic
1581 * path name must be carried in the PCRpt msg. Draft says: The SYMBOLIC-PATH-NAME TLV "MUST" be
1582 * included in the LSP object in the LSP State Report (PCRpt) message when during a given PCEP
1583 * session an LSP is "first" reported to a PCE.
1584 */
1585 if ((pathNameTlv != null)
1586 && Arrays.equals(tunnelObj.tunnelName().value().getBytes(), pathNameTlv.getValue())) {
1587 tunnel = tunnelObj;
1588 break;
1589 }
1590 continue;
1591 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301592 if ((Integer.valueOf(tunnelObj.annotations().value(PLSP_ID)) == lspObj.getPlspId()) && (Integer
1593 .valueOf(tunnelObj.annotations().value(LOCAL_LSP_ID)) == ipv4LspIdenTlv.getLspId())) {
1594 tunnel = tunnelObj;
1595 break;
1596 }
1597 }
1598
1599 DefaultTunnelDescription td;
Priyanka B413fbe82016-05-26 11:44:45 +05301600 SparseAnnotations annotations = null;
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301601 State tunnelState = PcepLspStatus.getTunnelStatusFromLspStatus(PcepLspStatus.values()[lspObj.getOFlag()]);
1602 if (tunnel == null) {
1603 if (lspObj.getRFlag()) {
1604 /*
1605 * If PCC sends remove message and for any reason PCE does not have that entry, simply discard the
1606 * message. Or if PCRpt for initiated LSP received and PCE doesn't know, then too discard.
1607 */
1608 return;
1609 }
1610
Priyanka B413fbe82016-05-26 11:44:45 +05301611 DeviceId deviceId = getDevice(pccId);
1612 if (deviceId == null) {
1613 log.error("Ingress deviceId not found");
1614 return;
1615 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301616 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType, costType);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301617
Priyanka B413fbe82016-05-26 11:44:45 +05301618 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301619 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path, labelStack,
Priyanka B413fbe82016-05-26 11:44:45 +05301620 annotations);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301621
1622 // Do not support PCC initiated LSP after LSP DB sync is completed.
1623 if (!lspObj.getSFlag() && !lspObj.getCFlag()) {
1624 log.error("Received PCC initiated LSP while not in sync.");
1625 return;
1626 }
1627
Priyanka B413fbe82016-05-26 11:44:45 +05301628 /*
1629 * If ONOS instance is master for PCC then set delegated flag as annotation and add the tunnel to store.
1630 * Because all LSPs need not be delegated, hence mastership for the PCC is confirmed whereas not the
1631 * delegation set to all LSPs.If ONOS is not the master for that PCC then check if D flag is set, if yes
1632 * wait for 2 seconds [while master has added the tunnel to the store] then update the tunnel. Tunnel is
1633 * updated because in case of resilency only delegated LSPs are recomputed and only delegated PCE can
1634 * send update message to that client.
1635 *
1636 * 1)Master can 1st get the Rpt message
1637 * a)Master adds the tunnel into core.
1638 * b)If a non-master for ingress gets Rpt message with D flag set[as delegation owner]
1639 * after master, then runs timer then update the tunnel with D flag set.
1640 * 2)Non-Master can 1st get the Rpt message
1641 * a)Non-Master runs the timer check for the tunnel then updates the tunnel with D flag set
1642 * b)Master would have got the message while the non-master running timer, hence master adds
1643 * tunnel to core
1644 *
1645 * In general always master adds the tunnel to the core
1646 * while delegated owner [master or non-master with D flag set] always updates the tunnel running timer
1647 */
1648 if (mastershipService.isLocalMaster(deviceId)) {
1649 TunnelId tId = tunnelAdded(td, tunnelState);
1650 Tunnel tunnelInserted = new DefaultTunnel(providerId, tunnelEndPointSrc, tunnelEndPointDst, MPLS,
1651 tunnelState, new DefaultGroupId(0), tId, TunnelName.tunnelName(String.valueOf(pathNameTlv
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301652 .getValue())), path, labelStack, annotations);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301653
Priyanka B413fbe82016-05-26 11:44:45 +05301654 PcepTunnelData pcepTunnelData = new PcepTunnelData(tunnelInserted, path, LSP_STATE_RPT);
1655 pcepTunnelData.setStatefulIpv4IndentifierTlv(ipv4LspIdenTlv);
1656 pcepTunnelApiMapper.addToTunnelIdMap(pcepTunnelData);
1657 } else if (!mastershipService.isLocalMaster(deviceId) && lspObj.getDFlag()) {
1658 //Start timer then update the tunnel with D flag
1659 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1660 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301661 return;
1662 }
1663
Priyanka B413fbe82016-05-26 11:44:45 +05301664 //delegated owner will update can be a master or non-master
1665 if (lspObj.getDFlag()) {
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301666 annotations = getAnnotations(lspObj, ipv4LspIdenTlv, bandwidth, lspType, costType);
Priyanka B413fbe82016-05-26 11:44:45 +05301667 td = new DefaultTunnelDescription(null, tunnelEndPointSrc, tunnelEndPointDst, MPLS, new DefaultGroupId(
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301668 0), providerId, TunnelName.tunnelName(new String(pathNameTlv.getValue())), path, labelStack,
Priyanka B413fbe82016-05-26 11:44:45 +05301669 annotations);
1670 tunnelUpdateInDelegatedCase(pccId, annotations, td, providerId);
1671 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301672 removeOrUpdatetunnel(tunnel, pccId, lspObj, providerId, tunnelState);
Priyanka B413fbe82016-05-26 11:44:45 +05301673 return;
1674 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301675
Priyanka B413fbe82016-05-26 11:44:45 +05301676 private void removeOrUpdatetunnel(Tunnel tunnel, PccId pccId, PcepLspObject lspObj, ProviderId providerId,
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301677 State tunnelState) {
Priyanka B413fbe82016-05-26 11:44:45 +05301678 DefaultTunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(), tunnel.src(), tunnel.dst(),
1679 tunnel.type(), tunnel.groupId(), providerId, tunnel.tunnelName(), tunnel.path(),
1680 (SparseAnnotations) tunnel.annotations());
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301681 if (lspObj.getRFlag()) {
1682 tunnelRemoved(td);
1683 } else {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301684 tunnelUpdated(td, tunnelState);
1685 }
Priyanka B413fbe82016-05-26 11:44:45 +05301686 }
1687
1688 private void tunnelUpdateInDelegatedCase(PccId pccId, SparseAnnotations annotations,
1689 DefaultTunnelDescription td, ProviderId providerId) {
1690 //Wait for 2sec then query tunnel based on ingress PLSP-ID and local LSP-ID
1691
1692 /*
1693 * If ONOS is not the master for that PCC then check if D flag is set, if yes wait [while
1694 * master has added the tunnel to the store] then update the tunnel.
1695 */
1696 ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
1697
1698 // Thread is started after 2 seconds first time later periodically after 2 seconds to update the tunnel
1699 executor.scheduleAtFixedRate(new UpdateDelegation(td, providerId, annotations, pccId,
1700 executor), DELAY, DELAY, TimeUnit.SECONDS);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301701 }
1702
1703 /**
Avantika-Huawei56c11842016-04-28 00:56:56 +05301704 * To build Path in network from ERO object.
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301705 *
Avantika-Huawei56c11842016-04-28 00:56:56 +05301706 * @param eroObj ERO object
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301707 * @param providerId provider id
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301708 * @return list of links and labels
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301709 */
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301710 private List<Object> buildPathFromEroObj(PcepEroObject eroObj, ProviderId providerId) {
Avantika-Huawei56c11842016-04-28 00:56:56 +05301711 checkNotNull(eroObj);
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301712 List<Object> subObjList = new ArrayList<>();
Avantika-Huawei56c11842016-04-28 00:56:56 +05301713 LinkedList<PcepValueType> llSubObj = eroObj.getSubObjects();
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301714 if (0 == llSubObj.size()) {
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301715 log.error("ERO in report message does not have hop information");
Priyanka B4c3cef02016-06-14 20:27:53 +05301716 return null;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301717 }
1718 ListIterator<PcepValueType> tlvIterator = llSubObj.listIterator();
1719
1720 ConnectPoint src = null;
1721 ConnectPoint dst = null;
1722 boolean isSrcSet = false;
1723 while (tlvIterator.hasNext()) {
1724 PcepValueType subObj = tlvIterator.next();
1725 switch (subObj.getType()) {
1726
1727 case IPv4SubObject.TYPE:
1728
1729 IPv4SubObject ipv4SubObj = (IPv4SubObject) subObj;
1730 if (!isSrcSet) {
1731 IpAddress srcIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1732 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1733 isSrcSet = true;
1734 } else {
1735 IpAddress dstIp = IpAddress.valueOf(ipv4SubObj.getIpAddress());
1736 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
Ray Milkey2693bda2016-01-22 16:08:14 -08001737 Link link = DefaultLink.builder()
1738 .providerId(providerId)
1739 .src(src)
1740 .dst(dst)
1741 .type(Link.Type.DIRECT)
1742 .build();
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301743 subObjList.add(link);
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301744 src = dst;
1745 }
1746 break;
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301747 case SrEroSubObject.TYPE:
1748 SrEroSubObject srEroSubObj = (SrEroSubObject) subObj;
1749 subObjList.add(srEroSubObj.getSid());
1750
1751 if (srEroSubObj.getSt() == PcepNaiIpv4Adjacency.ST_TYPE) {
1752 PcepNaiIpv4Adjacency nai = (PcepNaiIpv4Adjacency) (srEroSubObj.getNai());
1753 IpAddress srcIp = IpAddress.valueOf(nai.getLocalIpv4Addr());
1754 src = new ConnectPoint(IpElementId.ipElement(srcIp), PortNumber.portNumber(0));
1755 IpAddress dstIp = IpAddress.valueOf(nai.getRemoteIpv4Addr());
1756 dst = new ConnectPoint(IpElementId.ipElement(dstIp), PortNumber.portNumber(0));
1757 Link link = DefaultLink.builder()
1758 .providerId(providerId)
1759 .src(src)
1760 .dst(dst)
1761 .type(Link.Type.DIRECT)
1762 .build();
1763 subObjList.add(link);
1764 }
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301765 default:
1766 // the other sub objects are not required
1767 }
1768 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301769
Avantika-Huaweif849aab2016-06-21 22:29:15 +05301770 return subObjList;
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301771 }
1772
Phanendra Manda37b97fb2015-08-15 02:04:24 +05301773 @Override
1774 public void clientConnected(PccId pccId) {
1775 // TODO
1776 }
1777
1778 @Override
1779 public void clientDisconnected(PccId pccId) {
1780 // TODO
cheng fan48e832c2015-05-29 01:54:47 +08001781 }
chengfan2fff70f2015-08-24 18:20:19 -05001782
chengfan2fff70f2015-08-24 18:20:19 -05001783 @Override
1784 public void handlePcepTunnelStatistics(PcepTunnelStatistics pcepTunnelStatistics) {
1785 TunnelId id = getTunnelId(String.valueOf(pcepTunnelStatistics.id()));
1786 TunnelStatistics tunnelStatistics = buildTunnelStatistics(pcepTunnelStatistics);
1787 tunnelStatisticsMap.put(id, tunnelStatistics);
1788 }
cheng fan48e832c2015-05-29 01:54:47 +08001789
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301790 @Override
1791 public void handleEndOfSyncAction(Tunnel tunnel, PcepLspSyncAction endOfSyncAction) {
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301792
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301793 if (endOfSyncAction == SEND_UPDATE) {
1794 updateTunnel(tunnel, tunnel.path());
1795 return;
1796 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301797
1798 TunnelDescription td = new DefaultTunnelDescription(tunnel.tunnelId(),
1799 tunnel.src(), tunnel.dst(),
1800 tunnel.type(),
1801 tunnel.groupId(),
1802 tunnel.providerId(),
1803 tunnel.tunnelName(),
1804 tunnel.path(),
1805 (SparseAnnotations) tunnel.annotations());
1806
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301807
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301808 if (endOfSyncAction == PcepLspSyncAction.UNSTABLE) {
1809
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301810 // Send PCInit msg again after global reoptimization.
1811 tunnelUpdated(td, UNSTABLE);
1812
1813 // To remove the old tunnel from store whose PLSPID is not
1814 // recognized by ingress PCC.
1815 tunnelRemoved(td);
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301816
1817 } else if (endOfSyncAction == REMOVE) {
1818 tunnelRemoved(td);
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301819 }
1820 }
1821
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301822 @Override
1823 public void handleEndOfSyncAction(PccId pccId, PcepMessage msg, PcepLspSyncAction endOfSyncAction) {
1824 try {
1825 if ((msg instanceof PcepInitiateMsg) && (endOfSyncAction == SEND_DELETE)) {
1826 PcepClient pc = pcepClientController.getClient(pccId);
1827 LinkedList<PcInitiatedLspRequest> llPcInitiatedLspRequestList = ((PcepInitiateMsg) msg)
1828 .getPcInitiatedLspRequestList();
1829 PcInitiatedLspRequest pcInitMsg = llPcInitiatedLspRequestList.iterator().next();
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301830
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301831 if (pcInitMsg != null) {
1832 PcepSrpObject srpobj = pc.factory().buildSrpObject().setSrpID(SrpIdGenerators.create())
1833 .setRFlag(true).build();
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301834
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301835 PcInitiatedLspRequest releaseLspRequest = pc.factory().buildPcInitiatedLspRequest()
1836 .setLspObject(pcInitMsg.getLspObject()).setSrpObject(srpobj).build();
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301837
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301838 llPcInitiatedLspRequestList.remove(pcInitMsg);
1839 llPcInitiatedLspRequestList.add(releaseLspRequest);
1840
1841 PcepInitiateMsg pcInitiateMsg = pc.factory().buildPcepInitiateMsg()
1842 .setPcInitiatedLspRequestList(llPcInitiatedLspRequestList).build();
1843
1844 pc.sendMessage(Collections.singletonList(pcInitiateMsg));
1845 }
1846 }
1847 } catch (PcepParseException e) {
1848 log.error("Exception occured while sending initiate delete message {}", e.getMessage());
1849 }
Avantika-Huawei7f7376a2016-05-11 17:07:50 +05301850 }
1851 }
Avantika-Huaweid1e36bd2016-05-26 12:47:16 +05301852 @Override
1853 public Tunnel tunnelQueryById(TunnelId tunnelId) {
1854 return service.tunnelQueryById(tunnelId);
1855 }
1856
Priyanka B413fbe82016-05-26 11:44:45 +05301857
1858 private DeviceId getDevice(PccId pccId) {
1859 // Get lsrId of the PCEP client from the PCC ID. Session info is based on lsrID.
1860 IpAddress lsrId = pccId.ipAddress();
1861 String lsrIdentifier = String.valueOf(lsrId);
1862
1863 // Find PCC deviceID from lsrId stored as annotations
1864 Iterable<Device> devices = deviceService.getAvailableDevices();
1865 for (Device dev : devices) {
1866 if (dev.annotations().value(AnnotationKeys.TYPE).equals("L3")
1867 && dev.annotations().value(LSRID).equals(lsrIdentifier)) {
1868 return dev.id();
1869 }
1870 }
1871 return null;
1872 }
1873
1874 /**
1875 * Updates the tunnel with updated tunnel annotation after a delay of two seconds and checks it till
1876 * tunnel is found.
1877 */
1878 private class UpdateDelegation implements Runnable {
1879 DefaultTunnelDescription td;
1880 ProviderId providerId;
1881 SparseAnnotations annotations;
1882 PccId pccId;
1883 ScheduledExecutorService executor;
1884
1885 /**
1886 * Creates an instance of UpdateDelegation.
1887 *
1888 * @param td tunnel description
1889 * @param providerId provider id
1890 * @param annotations tunnel annotations
1891 * @param pccId PCEP client id
1892 * @param executor service of delegated owner
1893 */
1894 public UpdateDelegation(DefaultTunnelDescription td, ProviderId providerId, SparseAnnotations annotations,
1895 PccId pccId, ScheduledExecutorService executor) {
1896 this.td = td;
1897 this.providerId = providerId;
1898 this.annotations = annotations;
1899 this.pccId = pccId;
1900 this.executor = executor;
1901 }
1902
1903 //Temporary using annotations later will use projection/network config service
1904 @Override
1905 public void run() {
1906 Collection<Tunnel> tunnelQueryResult = tunnelService.queryTunnel(td.src(), td.dst());
1907 TunnelId tempTunnelId = null;
1908 for (Tunnel t : tunnelQueryResult) {
1909 if (t.annotations().value(LOCAL_LSP_ID) == null || t.annotations().value(PLSP_ID) == null) {
1910 continue;
1911 }
1912
1913 if (t.annotations().value(LOCAL_LSP_ID).equals(td.annotations().value(LOCAL_LSP_ID))
1914 && t.annotations().value(PLSP_ID).equals(td.annotations().value(PLSP_ID))
1915 && ((IpTunnelEndPoint) t.src()).ip().equals(pccId.id())) {
1916 tempTunnelId = t.tunnelId();
1917 break;
1918 }
1919 }
1920
1921 //If tunnel is found update the tunnel and shutdown the thread otherwise thread will be executing
1922 //periodically
1923 if (tempTunnelId != null) {
1924 Tunnel tunnel = new DefaultTunnel(providerId, td.src(), td.dst(), MPLS, new DefaultGroupId(0),
1925 tempTunnelId, td.tunnelName(), td.path(), annotations);
1926 tunnelUpdated(tunnel, td.path());
1927 executor.shutdown();
1928 try {
1929 executor.awaitTermination(WAIT_TIME, TimeUnit.SECONDS);
1930 } catch (InterruptedException e) {
1931 log.error("updating delegation failed");
1932 }
1933 }
1934 }
1935 }
cheng fan48e832c2015-05-29 01:54:47 +08001936}