blob: 253a166372204c4d936abd01ccb2199fb68c2db7 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070031import org.apache.karaf.shell.api.action.lifecycle.Service;
Mahesh Poojary S33536202016-05-30 07:22:36 +053032import org.onlab.util.DataRateUnit;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053033import org.onosproject.cli.AbstractShellCommand;
Priyanka B06420d92016-06-21 08:27:03 +053034import org.onosproject.incubator.net.tunnel.Tunnel;
35import org.onosproject.incubator.net.tunnel.TunnelService;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053036import org.onosproject.net.ConnectPoint;
37import org.onosproject.net.DefaultLink;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053038import org.onosproject.net.DeviceId;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053039import org.onosproject.net.NetworkResource;
40import org.onosproject.net.PortNumber;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053041import org.onosproject.net.intent.Constraint;
Priyanka Bbae0eeb12016-11-30 11:59:48 +053042import org.onosproject.net.provider.ProviderId;
43import org.onosproject.pce.pceservice.ExplicitPathInfo;
Mahesh Poojary S33536202016-05-30 07:22:36 +053044import org.onosproject.pce.pceservice.constraint.CostConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053045import org.onosproject.pce.pceservice.LspType;
46import org.onosproject.pce.pceservice.api.PceService;
Satish K2eb5d842017-04-04 16:28:37 +053047import org.onosproject.pce.pceservice.constraint.PceBandwidthConstraint;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053048import org.slf4j.Logger;
49
50/**
51 * Supports creating the pce path.
52 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070053@Service
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053054@Command(scope = "onos", name = "pce-setup-path", description = "Supports creating pce path.")
55public class PceSetupPathCommand extends AbstractShellCommand {
56 private final Logger log = getLogger(getClass());
Priyanka Bbae0eeb12016-11-30 11:59:48 +053057 public static final byte SUBTYPE_DEVICEID = 0;
58 public static final byte SUBTYPE_LINK = 1;
59 public static final byte SUBTYPE_INDEX = 1;
60 public static final byte TYPE_INDEX = 0;
61
62 public static final byte DEVICEID_INDEX = 2;
63 public static final byte SOURCE_DEVICEID_INDEX = 2;
64 public static final byte SOURCE_PORTNO_INDEX = 3;
65 public static final byte DESTINATION_DEVICEID_INDEX = 4;
66 public static final byte DESTINATION_PORTNO_INDEX = 5;
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053067
68 @Argument(index = 0, name = "src", description = "source device.", required = true, multiValued = false)
69 String src = null;
70
71 @Argument(index = 1, name = "dst", description = "destination device.", required = true, multiValued = false)
72 String dst = null;
73
74 @Argument(index = 2, name = "type", description = "LSP type:" + " It includes "
75 + "PCE tunnel with signalling in network (0), "
76 + "PCE tunnel without signalling in network with segment routing (1), "
77 + "PCE tunnel without signalling in network (2).",
78 required = true, multiValued = false)
79 int type = 0;
80
81 @Argument(index = 3, name = "name", description = "symbolic-path-name.", required = true, multiValued = false)
82 String name = null;
83
84 @Option(name = "-c", aliases = "--cost", description = "The cost attribute IGP cost(1) or TE cost(2)",
85 required = false, multiValued = false)
86 int cost = 2;
87
88 @Option(name = "-b", aliases = "--bandwidth", description = "The bandwidth attribute of path. "
89 + "Data rate unit is in BPS.", required = false, multiValued = false)
90 double bandwidth = 0.0;
91
Priyanka Bbae0eeb12016-11-30 11:59:48 +053092 @Option(name = "-e", aliases = "--explicitPathObjects", description = "List of strict and loose hopes"
93 + " explicitPathInfo format : Type/SubType/Value(DeviceId or Link info)\n" +
94 " If Value is Device : Type/SubType/deviceId\n" +
95 " If Value is Link : Type/SubType/SourceDeviceId/SourcePortNo/DestinationDeviceId/DestinationPortNo" +
96 "Type 0 - strict, 1 - loose \n" + "SubType 0 - deviceId, 1 - link \n",
97 required = false, multiValued = true)
98 String[] explicitPathInfoStrings;
99
Satish Kba1c9122017-04-05 15:27:23 +0530100 @Option(name = "-l", aliases = "--loadBalancing", description = "The load balancing option for user. ",
101 required = false, multiValued = false)
102 boolean loadBalancing = false;
103
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530104 //explicitPathInfo format : Type/SubType/Value(DeviceId or Link info)
105 //If Value is Device : Type/SubType/deviceId
106 //If Value is Link : Type/SubType/SourceDeviceId/SourcePortNo/DestinationDeviceId/DestinationPortNo
107 List<ExplicitPathInfo> explicitPathInfo = Lists.newLinkedList();
108
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530109 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -0700110 protected void doExecute() {
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530111 log.info("executing pce-setup-path");
112
113 PceService service = get(PceService.class);
Priyanka B06420d92016-06-21 08:27:03 +0530114 TunnelService tunnelService = get(TunnelService.class);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530115
116 DeviceId srcDevice = DeviceId.deviceId(src);
117 DeviceId dstDevice = DeviceId.deviceId(dst);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530118 List<Constraint> listConstrnt = new LinkedList<>();
119
Mahesh Poojary S33536202016-05-30 07:22:36 +0530120 // LSP type validation
121 if ((type < 0) || (type > 2)) {
122 error("The LSP type value can be PCE tunnel with signalling in network (0), " +
123 "PCE tunnel without signalling in network with segment routing (1), " +
124 "PCE tunnel without signalling in network (2).");
125 return;
126 }
127 LspType lspType = LspType.values()[type];
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530128
Priyanka B06420d92016-06-21 08:27:03 +0530129 //Validating tunnel name, duplicated tunnel names not allowed
130 Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
131 for (Tunnel t : existingTunnels) {
132 if (t.tunnelName().toString().equals(name)) {
133 error("Path creation failed, Tunnel name already exists");
134 return;
135 }
136 }
137
Mahesh Poojary S33536202016-05-30 07:22:36 +0530138 // Add bandwidth
Priyanka B4c3cef02016-06-14 20:27:53 +0530139 // bandwidth default data rate unit is in BPS
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530140 if (bandwidth != 0.0) {
Satish K2eb5d842017-04-04 16:28:37 +0530141 listConstrnt.add(PceBandwidthConstraint.of(bandwidth, DataRateUnit.valueOf("BPS")));
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530142 }
143
Mahesh Poojary S33536202016-05-30 07:22:36 +0530144 // Add cost
145 // Cost validation
146 if ((cost < 1) || (cost > 2)) {
147 error("The cost attribute value either IGP cost(1) or TE cost(2).");
148 return;
149 }
150 // Here 'cost - 1' indicates the index of enum
151 CostConstraint.Type costType = CostConstraint.Type.values()[cost - 1];
152 listConstrnt.add(CostConstraint.of(costType));
153
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530154 if (explicitPathInfoStrings != null) {
155 for (String str : explicitPathInfoStrings) {
156 String[] splitted = str.split("/");
157 DeviceId deviceId;
158 NetworkResource res = null;
159 PortNumber portNo;
160 int explicitPathType = Integer.parseInt(splitted[TYPE_INDEX]);
161 if ((explicitPathType < 0) || (explicitPathType > 1)) {
162 error("Explicit path validation failed");
163 return;
164 }
165
166 //subtype 0 = deviceId, 1 = link
167 //subtype is required to store either as deviceId or Link
168 if (splitted[DEVICEID_INDEX] != null && Integer.parseInt(splitted[SUBTYPE_INDEX]) == SUBTYPE_DEVICEID) {
169 res = DeviceId.deviceId(splitted[DEVICEID_INDEX]);
170 } else if (Integer.parseInt(splitted[SUBTYPE_INDEX]) == SUBTYPE_LINK
171 && splitted[SOURCE_DEVICEID_INDEX] != null
172 && splitted[SOURCE_PORTNO_INDEX] != null
173 && splitted[DESTINATION_DEVICEID_INDEX] != null
174 && splitted[DESTINATION_PORTNO_INDEX] != null) {
175
176 deviceId = DeviceId.deviceId(splitted[SOURCE_DEVICEID_INDEX]);
177 portNo = PortNumber.portNumber(splitted[SOURCE_PORTNO_INDEX]);
178 ConnectPoint cpSrc = new ConnectPoint(deviceId, portNo);
179 deviceId = DeviceId.deviceId(splitted[DESTINATION_DEVICEID_INDEX]);
180 portNo = PortNumber.portNumber(splitted[DESTINATION_PORTNO_INDEX]);
181 ConnectPoint cpDst = new ConnectPoint(deviceId, portNo);
182 res = DefaultLink.builder()
183 .providerId(ProviderId.NONE)
184 .src(cpSrc)
185 .dst(cpDst)
186 .type(DIRECT)
187 .state(ACTIVE)
188 .build();
189 } else {
190 error("Explicit path validation failed");
191 return;
192 }
193 ExplicitPathInfo obj = new ExplicitPathInfo(ExplicitPathInfo.Type.values()[explicitPathType], res);
194 explicitPathInfo.add(obj);
195 }
196 }
Satish Kba1c9122017-04-05 15:27:23 +0530197
198 //with load balancing option
199 if (loadBalancing) {
200 if (!service.setupPath(srcDevice, dstDevice, name, listConstrnt, lspType, loadBalancing)) {
201 error("Path creation failed.");
202 }
203 return;
204 }
205
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530206 if (!service.setupPath(srcDevice, dstDevice, name, listConstrnt, lspType, explicitPathInfo)) {
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530207 error("Path creation failed.");
208 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530209 }
210}