blob: deb85b4622540a1be3b13f5e1ce7cca50f1ccf29 [file] [log] [blame]
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05303 *
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.pce.cli;
17
Priyanka Bbae0eeb12016-11-30 11:59:48 +053018import static org.onosproject.net.Link.State.ACTIVE;
19import static org.onosproject.net.Link.Type.DIRECT;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053020import static org.slf4j.LoggerFactory.getLogger;
21
Priyanka B06420d92016-06-21 08:27:03 +053022import java.util.Collection;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053023import java.util.List;
24import java.util.LinkedList;
25
Priyanka Bbae0eeb12016-11-30 11:59:48 +053026import com.google.common.collect.Lists;
27
Ray Milkey86ad7bb2018-09-27 12:32:28 -070028import org.apache.karaf.shell.api.action.Argument;
29import org.apache.karaf.shell.api.action.Command;
30import org.apache.karaf.shell.api.action.Option;
Mahesh Poojary S33536202016-05-30 07:22:36 +053031import org.onlab.util.DataRateUnit;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053032import org.onosproject.cli.AbstractShellCommand;
Priyanka B06420d92016-06-21 08:27:03 +053033import org.onosproject.incubator.net.tunnel.Tunnel;
34import org.onosproject.incubator.net.tunnel.TunnelService;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053035import org.onosproject.net.ConnectPoint;
36import org.onosproject.net.DefaultLink;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053037import org.onosproject.net.DeviceId;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053038import org.onosproject.net.NetworkResource;
39import org.onosproject.net.PortNumber;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053040import org.onosproject.net.intent.Constraint;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053041import org.onosproject.net.provider.ProviderId;
42import org.onosproject.pce.pceservice.ExplicitPathInfo;
Mahesh Poojary S33536202016-05-30 07:22:36 +053043import org.onosproject.pce.pceservice.constraint.CostConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053044import org.onosproject.pce.pceservice.LspType;
45import org.onosproject.pce.pceservice.api.PceService;
Satish K2eb5d842017-04-04 16:28:37 +053046import org.onosproject.pce.pceservice.constraint.PceBandwidthConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053047import org.slf4j.Logger;
48
49/**
50 * Supports creating the pce path.
51 */
52@Command(scope = "onos", name = "pce-setup-path", description = "Supports creating pce path.")
53public class PceSetupPathCommand extends AbstractShellCommand {
54 private final Logger log = getLogger(getClass());
Priyanka Bbae0eeb12016-11-30 11:59:48 +053055 public static final byte SUBTYPE_DEVICEID = 0;
56 public static final byte SUBTYPE_LINK = 1;
57 public static final byte SUBTYPE_INDEX = 1;
58 public static final byte TYPE_INDEX = 0;
59
60 public static final byte DEVICEID_INDEX = 2;
61 public static final byte SOURCE_DEVICEID_INDEX = 2;
62 public static final byte SOURCE_PORTNO_INDEX = 3;
63 public static final byte DESTINATION_DEVICEID_INDEX = 4;
64 public static final byte DESTINATION_PORTNO_INDEX = 5;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053065
66 @Argument(index = 0, name = "src", description = "source device.", required = true, multiValued = false)
67 String src = null;
68
69 @Argument(index = 1, name = "dst", description = "destination device.", required = true, multiValued = false)
70 String dst = null;
71
72 @Argument(index = 2, name = "type", description = "LSP type:" + " It includes "
73 + "PCE tunnel with signalling in network (0), "
74 + "PCE tunnel without signalling in network with segment routing (1), "
75 + "PCE tunnel without signalling in network (2).",
76 required = true, multiValued = false)
77 int type = 0;
78
79 @Argument(index = 3, name = "name", description = "symbolic-path-name.", required = true, multiValued = false)
80 String name = null;
81
82 @Option(name = "-c", aliases = "--cost", description = "The cost attribute IGP cost(1) or TE cost(2)",
83 required = false, multiValued = false)
84 int cost = 2;
85
86 @Option(name = "-b", aliases = "--bandwidth", description = "The bandwidth attribute of path. "
87 + "Data rate unit is in BPS.", required = false, multiValued = false)
88 double bandwidth = 0.0;
89
Priyanka Bbae0eeb12016-11-30 11:59:48 +053090 @Option(name = "-e", aliases = "--explicitPathObjects", description = "List of strict and loose hopes"
91 + " explicitPathInfo format : Type/SubType/Value(DeviceId or Link info)\n" +
92 " If Value is Device : Type/SubType/deviceId\n" +
93 " If Value is Link : Type/SubType/SourceDeviceId/SourcePortNo/DestinationDeviceId/DestinationPortNo" +
94 "Type 0 - strict, 1 - loose \n" + "SubType 0 - deviceId, 1 - link \n",
95 required = false, multiValued = true)
96 String[] explicitPathInfoStrings;
97
Satish Kba1c9122017-04-05 15:27:23 +053098 @Option(name = "-l", aliases = "--loadBalancing", description = "The load balancing option for user. ",
99 required = false, multiValued = false)
100 boolean loadBalancing = false;
101
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530102 //explicitPathInfo format : Type/SubType/Value(DeviceId or Link info)
103 //If Value is Device : Type/SubType/deviceId
104 //If Value is Link : Type/SubType/SourceDeviceId/SourcePortNo/DestinationDeviceId/DestinationPortNo
105 List<ExplicitPathInfo> explicitPathInfo = Lists.newLinkedList();
106
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530107 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700108 protected void doExecute() {
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530109 log.info("executing pce-setup-path");
110
111 PceService service = get(PceService.class);
Priyanka B06420d92016-06-21 08:27:03 +0530112 TunnelService tunnelService = get(TunnelService.class);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530113
114 DeviceId srcDevice = DeviceId.deviceId(src);
115 DeviceId dstDevice = DeviceId.deviceId(dst);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530116 List<Constraint> listConstrnt = new LinkedList<>();
117
Mahesh Poojary S33536202016-05-30 07:22:36 +0530118 // LSP type validation
119 if ((type < 0) || (type > 2)) {
120 error("The LSP type value can be PCE tunnel with signalling in network (0), " +
121 "PCE tunnel without signalling in network with segment routing (1), " +
122 "PCE tunnel without signalling in network (2).");
123 return;
124 }
125 LspType lspType = LspType.values()[type];
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530126
Priyanka B06420d92016-06-21 08:27:03 +0530127 //Validating tunnel name, duplicated tunnel names not allowed
128 Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
129 for (Tunnel t : existingTunnels) {
130 if (t.tunnelName().toString().equals(name)) {
131 error("Path creation failed, Tunnel name already exists");
132 return;
133 }
134 }
135
Mahesh Poojary S33536202016-05-30 07:22:36 +0530136 // Add bandwidth
Priyanka B4c3cef02016-06-14 20:27:53 +0530137 // bandwidth default data rate unit is in BPS
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530138 if (bandwidth != 0.0) {
Satish K2eb5d842017-04-04 16:28:37 +0530139 listConstrnt.add(PceBandwidthConstraint.of(bandwidth, DataRateUnit.valueOf("BPS")));
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530140 }
141
Mahesh Poojary S33536202016-05-30 07:22:36 +0530142 // Add cost
143 // Cost validation
144 if ((cost < 1) || (cost > 2)) {
145 error("The cost attribute value either IGP cost(1) or TE cost(2).");
146 return;
147 }
148 // Here 'cost - 1' indicates the index of enum
149 CostConstraint.Type costType = CostConstraint.Type.values()[cost - 1];
150 listConstrnt.add(CostConstraint.of(costType));
151
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530152 if (explicitPathInfoStrings != null) {
153 for (String str : explicitPathInfoStrings) {
154 String[] splitted = str.split("/");
155 DeviceId deviceId;
156 NetworkResource res = null;
157 PortNumber portNo;
158 int explicitPathType = Integer.parseInt(splitted[TYPE_INDEX]);
159 if ((explicitPathType < 0) || (explicitPathType > 1)) {
160 error("Explicit path validation failed");
161 return;
162 }
163
164 //subtype 0 = deviceId, 1 = link
165 //subtype is required to store either as deviceId or Link
166 if (splitted[DEVICEID_INDEX] != null && Integer.parseInt(splitted[SUBTYPE_INDEX]) == SUBTYPE_DEVICEID) {
167 res = DeviceId.deviceId(splitted[DEVICEID_INDEX]);
168 } else if (Integer.parseInt(splitted[SUBTYPE_INDEX]) == SUBTYPE_LINK
169 && splitted[SOURCE_DEVICEID_INDEX] != null
170 && splitted[SOURCE_PORTNO_INDEX] != null
171 && splitted[DESTINATION_DEVICEID_INDEX] != null
172 && splitted[DESTINATION_PORTNO_INDEX] != null) {
173
174 deviceId = DeviceId.deviceId(splitted[SOURCE_DEVICEID_INDEX]);
175 portNo = PortNumber.portNumber(splitted[SOURCE_PORTNO_INDEX]);
176 ConnectPoint cpSrc = new ConnectPoint(deviceId, portNo);
177 deviceId = DeviceId.deviceId(splitted[DESTINATION_DEVICEID_INDEX]);
178 portNo = PortNumber.portNumber(splitted[DESTINATION_PORTNO_INDEX]);
179 ConnectPoint cpDst = new ConnectPoint(deviceId, portNo);
180 res = DefaultLink.builder()
181 .providerId(ProviderId.NONE)
182 .src(cpSrc)
183 .dst(cpDst)
184 .type(DIRECT)
185 .state(ACTIVE)
186 .build();
187 } else {
188 error("Explicit path validation failed");
189 return;
190 }
191 ExplicitPathInfo obj = new ExplicitPathInfo(ExplicitPathInfo.Type.values()[explicitPathType], res);
192 explicitPathInfo.add(obj);
193 }
194 }
Satish Kba1c9122017-04-05 15:27:23 +0530195
196 //with load balancing option
197 if (loadBalancing) {
198 if (!service.setupPath(srcDevice, dstDevice, name, listConstrnt, lspType, loadBalancing)) {
199 error("Path creation failed.");
200 }
201 return;
202 }
203
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530204 if (!service.setupPath(srcDevice, dstDevice, name, listConstrnt, lspType, explicitPathInfo)) {
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530205 error("Path creation failed.");
206 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530207 }
208}