blob: 6139004dd9b7cf4591fba7ec37b96bd702385c9a [file] [log] [blame]
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +05301/*
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +05302 * Copyright 2016-present Open Networking Laboratory
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
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +053028import org.apache.karaf.shell.commands.Argument;
29import org.apache.karaf.shell.commands.Command;
30import org.apache.karaf.shell.commands.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 S33536202016-05-30 07:22:36 +053040import org.onosproject.net.intent.constraint.BandwidthConstraint;
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;
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
98 //explicitPathInfo format : Type/SubType/Value(DeviceId or Link info)
99 //If Value is Device : Type/SubType/deviceId
100 //If Value is Link : Type/SubType/SourceDeviceId/SourcePortNo/DestinationDeviceId/DestinationPortNo
101 List<ExplicitPathInfo> explicitPathInfo = Lists.newLinkedList();
102
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530103 @Override
104 protected void execute() {
105 log.info("executing pce-setup-path");
106
107 PceService service = get(PceService.class);
Priyanka B06420d92016-06-21 08:27:03 +0530108 TunnelService tunnelService = get(TunnelService.class);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530109
110 DeviceId srcDevice = DeviceId.deviceId(src);
111 DeviceId dstDevice = DeviceId.deviceId(dst);
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530112 List<Constraint> listConstrnt = new LinkedList<>();
113
Mahesh Poojary S33536202016-05-30 07:22:36 +0530114 // LSP type validation
115 if ((type < 0) || (type > 2)) {
116 error("The LSP type value can be PCE tunnel with signalling in network (0), " +
117 "PCE tunnel without signalling in network with segment routing (1), " +
118 "PCE tunnel without signalling in network (2).");
119 return;
120 }
121 LspType lspType = LspType.values()[type];
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530122
Priyanka B06420d92016-06-21 08:27:03 +0530123 //Validating tunnel name, duplicated tunnel names not allowed
124 Collection<Tunnel> existingTunnels = tunnelService.queryTunnel(Tunnel.Type.MPLS);
125 for (Tunnel t : existingTunnels) {
126 if (t.tunnelName().toString().equals(name)) {
127 error("Path creation failed, Tunnel name already exists");
128 return;
129 }
130 }
131
Mahesh Poojary S33536202016-05-30 07:22:36 +0530132 // Add bandwidth
Priyanka B4c3cef02016-06-14 20:27:53 +0530133 // bandwidth default data rate unit is in BPS
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530134 if (bandwidth != 0.0) {
Priyanka B4c3cef02016-06-14 20:27:53 +0530135 listConstrnt.add(BandwidthConstraint.of(bandwidth, DataRateUnit.valueOf("BPS")));
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530136 }
137
Mahesh Poojary S33536202016-05-30 07:22:36 +0530138 // Add cost
139 // Cost validation
140 if ((cost < 1) || (cost > 2)) {
141 error("The cost attribute value either IGP cost(1) or TE cost(2).");
142 return;
143 }
144 // Here 'cost - 1' indicates the index of enum
145 CostConstraint.Type costType = CostConstraint.Type.values()[cost - 1];
146 listConstrnt.add(CostConstraint.of(costType));
147
Priyanka Bbae0eeb12016-11-30 11:59:48 +0530148 if (explicitPathInfoStrings != null) {
149 for (String str : explicitPathInfoStrings) {
150 String[] splitted = str.split("/");
151 DeviceId deviceId;
152 NetworkResource res = null;
153 PortNumber portNo;
154 int explicitPathType = Integer.parseInt(splitted[TYPE_INDEX]);
155 if ((explicitPathType < 0) || (explicitPathType > 1)) {
156 error("Explicit path validation failed");
157 return;
158 }
159
160 //subtype 0 = deviceId, 1 = link
161 //subtype is required to store either as deviceId or Link
162 if (splitted[DEVICEID_INDEX] != null && Integer.parseInt(splitted[SUBTYPE_INDEX]) == SUBTYPE_DEVICEID) {
163 res = DeviceId.deviceId(splitted[DEVICEID_INDEX]);
164 } else if (Integer.parseInt(splitted[SUBTYPE_INDEX]) == SUBTYPE_LINK
165 && splitted[SOURCE_DEVICEID_INDEX] != null
166 && splitted[SOURCE_PORTNO_INDEX] != null
167 && splitted[DESTINATION_DEVICEID_INDEX] != null
168 && splitted[DESTINATION_PORTNO_INDEX] != null) {
169
170 deviceId = DeviceId.deviceId(splitted[SOURCE_DEVICEID_INDEX]);
171 portNo = PortNumber.portNumber(splitted[SOURCE_PORTNO_INDEX]);
172 ConnectPoint cpSrc = new ConnectPoint(deviceId, portNo);
173 deviceId = DeviceId.deviceId(splitted[DESTINATION_DEVICEID_INDEX]);
174 portNo = PortNumber.portNumber(splitted[DESTINATION_PORTNO_INDEX]);
175 ConnectPoint cpDst = new ConnectPoint(deviceId, portNo);
176 res = DefaultLink.builder()
177 .providerId(ProviderId.NONE)
178 .src(cpSrc)
179 .dst(cpDst)
180 .type(DIRECT)
181 .state(ACTIVE)
182 .build();
183 } else {
184 error("Explicit path validation failed");
185 return;
186 }
187 ExplicitPathInfo obj = new ExplicitPathInfo(ExplicitPathInfo.Type.values()[explicitPathType], res);
188 explicitPathInfo.add(obj);
189 }
190 }
191 if (!service.setupPath(srcDevice, dstDevice, name, listConstrnt, lspType, explicitPathInfo)) {
Mahesh Poojary S8e1670e2016-05-19 22:15:34 +0530192 error("Path creation failed.");
193 }
Mahesh Poojary Sd7a36922016-04-01 16:11:14 +0530194 }
195}