blob: 42c26625421d936cd024e2d860601582a2a54611 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070017
Jonathan Hart6e948282014-11-17 22:50:42 -080018import static com.google.common.base.Strings.isNullOrEmpty;
Brian O'Connorabafb502014-12-02 22:26:20 -080019import static org.onosproject.net.flow.DefaultTrafficTreatment.builder;
Jonathan Hart6e948282014-11-17 22:50:42 -080020
Ray Milkey460f4022014-11-05 15:41:43 -080021import java.util.LinkedList;
22import java.util.List;
23
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070024import org.apache.karaf.shell.commands.Option;
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +080025import org.onlab.packet.Ip6Address;
26import org.onlab.packet.IpAddress;
Dusan Pajin6b887c92015-07-01 18:32:49 +020027import org.onlab.packet.VlanId;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070028import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.cli.AbstractShellCommand;
Ray Milkey95c50872015-04-14 14:32:31 -070030import org.onosproject.core.ApplicationId;
31import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.Link;
33import org.onosproject.net.flow.DefaultTrafficSelector;
Brian O'Connor11ff8702015-04-29 19:37:15 -070034import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.flow.TrafficSelector;
36import org.onosproject.net.flow.TrafficTreatment;
37import org.onosproject.net.intent.Constraint;
Ray Milkeyc24cde32015-03-10 18:20:18 -070038import org.onosproject.net.intent.Intent;
Ray Milkey5b3717e2015-02-05 11:44:08 -080039import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import org.onosproject.net.intent.constraint.BandwidthConstraint;
41import org.onosproject.net.intent.constraint.LambdaConstraint;
42import org.onosproject.net.intent.constraint.LinkTypeConstraint;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070043import org.onosproject.net.intent.constraint.PartialFailureConstraint;
Brian O'Connor6de2e202015-05-21 14:30:41 -070044import org.onosproject.net.resource.link.BandwidthResource;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080045import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070046import org.onlab.packet.MacAddress;
47
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070048/**
49 * Base class for command line operations for connectivity based intents.
50 */
51public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
52
Jonathan Hart2c25e362014-11-17 18:14:19 -080053 // Selectors
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070054 @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address",
55 required = false, multiValued = false)
56 private String srcMacString = null;
57
58 @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address",
59 required = false, multiValued = false)
60 private String dstMacString = null;
61
62 @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type",
63 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080064 private String ethTypeString = null;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070065
Dusan Pajin6b887c92015-07-01 18:32:49 +020066 @Option(name = "-v", aliases = "--vlan", description = "VLAN ID",
67 required = false, multiValued = false)
68 private String vlanString = null;
69
Jonathan Hart5f7097e2014-11-11 11:50:28 -080070 @Option(name = "--ipProto", description = "IP Protocol",
71 required = false, multiValued = false)
72 private String ipProtoString = null;
73
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -070074 @Option(name = "--ipSrc", description = "Source IP Prefix",
Jonathan Hart5f7097e2014-11-11 11:50:28 -080075 required = false, multiValued = false)
76 private String srcIpString = null;
77
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -070078 @Option(name = "--ipDst", description = "Destination IP Prefix",
Jonathan Hart5f7097e2014-11-11 11:50:28 -080079 required = false, multiValued = false)
80 private String dstIpString = null;
81
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +080082 @Option(name = "--fLabel", description = "IPv6 Flow Label",
83 required = false, multiValued = false)
84 private String fLabelString = null;
85
86 @Option(name = "--icmp6Type", description = "ICMPv6 Type",
87 required = false, multiValued = false)
88 private String icmp6TypeString = null;
89
90 @Option(name = "--icmp6Code", description = "ICMPv6 Code",
91 required = false, multiValued = false)
92 private String icmp6CodeString = null;
93
94 @Option(name = "--ndTarget", description = "IPv6 Neighbor Discovery Target Address",
95 required = false, multiValued = false)
96 private String ndTargetString = null;
97
98 @Option(name = "--ndSLL", description = "IPv6 Neighbor Discovery Source Link-Layer",
99 required = false, multiValued = false)
100 private String ndSLLString = null;
101
102 @Option(name = "--ndTLL", description = "IPv6 Neighbor Discovery Target Link-Layer",
103 required = false, multiValued = false)
104 private String ndTLLString = null;
105
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800106 @Option(name = "--tcpSrc", description = "Source TCP Port",
107 required = false, multiValued = false)
108 private String srcTcpString = null;
109
110 @Option(name = "--tcpDst", description = "Destination TCP Port",
111 required = false, multiValued = false)
112 private String dstTcpString = null;
113
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800114 @Option(name = "--extHdr", description = "IPv6 Extension Header Pseudo-field",
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800115 required = false, multiValued = true)
116 private List<String> extHdrStringList = null;
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800117
Ray Milkey460f4022014-11-05 15:41:43 -0800118 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
119 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -0800120 private String bandwidthString = null;
Ray Milkey460f4022014-11-05 15:41:43 -0800121
122 @Option(name = "-l", aliases = "--lambda", description = "Lambda",
123 required = false, multiValued = false)
Sho SHIMIZU792c26b2015-06-08 19:08:32 -0700124 private boolean lambda = false;
Ray Milkey460f4022014-11-05 15:41:43 -0800125
Ray Milkey95c50872015-04-14 14:32:31 -0700126 @Option(name = "-a", aliases = "--appId", description = "Application Id",
127 required = false, multiValued = false)
128 private String appId = null;
129
Ray Milkey5b3717e2015-02-05 11:44:08 -0800130 @Option(name = "-k", aliases = "--key", description = "Intent Key",
131 required = false, multiValued = false)
132 private String intentKey = null;
133
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700134 @Option(name = "--partial", description = "Allow partial installation",
135 required = false, multiValued = false)
136 private boolean partial = false;
137
Jonathan Hart2c25e362014-11-17 18:14:19 -0800138
139 // Treatments
140 @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address",
141 required = false, multiValued = false)
142 private String setEthSrcString = null;
143
144 @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address",
145 required = false, multiValued = false)
146 private String setEthDstString = null;
147
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800148 @Option(name = "--setIpSrc", description = "Rewrite Source IP Address",
149 required = false, multiValued = false)
150 private String setIpSrcString = null;
151
152 @Option(name = "--setIpDst", description = "Rewrite Destination IP Address",
153 required = false, multiValued = false)
154 private String setIpDstString = null;
155
Dusan Pajin6b887c92015-07-01 18:32:49 +0200156 @Option(name = "--setVlan", description = "Rewrite VLAN ID",
157 required = false, multiValued = false)
158 private String setVlan = null;
159
160 @Option(name = "--popVlan", description = "Pop VLAN Tag",
161 required = false, multiValued = false)
162 private boolean popVlan = false;
163
164 @Option(name = "--pushVlan", description = "Push VLAN ID",
165 required = false, multiValued = false)
166 private String pushVlan = null;
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800167
Ray Milkeyc24cde32015-03-10 18:20:18 -0700168 // Priorities
169 @Option(name = "-p", aliases = "--priority", description = "Priority",
170 required = false, multiValued = false)
171 private int priority = Intent.DEFAULT_INTENT_PRIORITY;
172
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700173 /**
174 * Constructs a traffic selector based on the command line arguments
175 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800176 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700177 */
178 protected TrafficSelector buildTrafficSelector() {
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -0700179 IpPrefix srcIpPrefix = null;
180 IpPrefix dstIpPrefix = null;
Ray Milkeycaa450b2014-10-29 15:54:24 -0700181
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -0700182 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
183
184 if (!isNullOrEmpty(srcIpString)) {
185 srcIpPrefix = IpPrefix.valueOf(srcIpString);
186 if (srcIpPrefix.isIp4()) {
187 selectorBuilder.matchIPSrc(srcIpPrefix);
188 } else {
189 selectorBuilder.matchIPv6Src(srcIpPrefix);
190 }
191 }
192
193 if (!isNullOrEmpty(dstIpString)) {
194 dstIpPrefix = IpPrefix.valueOf(dstIpString);
195 if (dstIpPrefix.isIp4()) {
196 selectorBuilder.matchIPDst(dstIpPrefix);
197 } else {
198 selectorBuilder.matchIPv6Dst(dstIpPrefix);
199 }
200 }
201
202 if ((srcIpPrefix != null) && (dstIpPrefix != null) &&
203 (srcIpPrefix.version() != dstIpPrefix.version())) {
204 // ERROR: IP src/dst version mismatch
205 throw new IllegalArgumentException(
206 "IP source and destination version mismatch");
207 }
208
209 //
210 // Set the default EthType based on the IP version if the matching
211 // source or destination IP prefixes.
212 //
Ayaka Koshibe8117f362015-06-02 13:44:01 -0700213 Short ethType = null;
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -0700214 if ((srcIpPrefix != null) && srcIpPrefix.isIp6()) {
215 ethType = EthType.IPV6.value();
216 }
217 if ((dstIpPrefix != null) && dstIpPrefix.isIp6()) {
218 ethType = EthType.IPV6.value();
219 }
Ray Milkey460f4022014-11-05 15:41:43 -0800220 if (!isNullOrEmpty(ethTypeString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800221 ethType = EthType.parseFromString(ethTypeString);
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700222 }
Ayaka Koshibe8117f362015-06-02 13:44:01 -0700223 if (ethType != null) {
224 selectorBuilder.matchEthType(ethType);
225 }
Dusan Pajin6b887c92015-07-01 18:32:49 +0200226 if (!isNullOrEmpty(vlanString)) {
227 selectorBuilder.matchVlanId(VlanId.vlanId(Short.parseShort(vlanString)));
228 }
Ray Milkey460f4022014-11-05 15:41:43 -0800229 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700230 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
231 }
232
Ray Milkey460f4022014-11-05 15:41:43 -0800233 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700234 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
235 }
236
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800237 if (!isNullOrEmpty(ipProtoString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800238 short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
239 selectorBuilder.matchIPProtocol((byte) ipProtoShort);
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800240 }
241
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800242 if (!isNullOrEmpty(fLabelString)) {
243 selectorBuilder.matchIPv6FlowLabel(Integer.parseInt(fLabelString));
244 }
245
246 if (!isNullOrEmpty(icmp6TypeString)) {
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800247 byte icmp6Type = Icmp6Type.parseFromString(icmp6TypeString);
248 selectorBuilder.matchIcmpv6Type(icmp6Type);
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800249 }
250
251 if (!isNullOrEmpty(icmp6CodeString)) {
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800252 byte icmp6Code = Icmp6Code.parseFromString(icmp6CodeString);
253 selectorBuilder.matchIcmpv6Code(icmp6Code);
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800254 }
255
256 if (!isNullOrEmpty(ndTargetString)) {
257 selectorBuilder.matchIPv6NDTargetAddress(Ip6Address.valueOf(ndTargetString));
258 }
259
260 if (!isNullOrEmpty(ndSLLString)) {
261 selectorBuilder.matchIPv6NDSourceLinkLayerAddress(MacAddress.valueOf(ndSLLString));
262 }
263
264 if (!isNullOrEmpty(ndTLLString)) {
265 selectorBuilder.matchIPv6NDTargetLinkLayerAddress(MacAddress.valueOf(ndTLLString));
266 }
267
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800268 if (!isNullOrEmpty(srcTcpString)) {
269 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
270 }
271
272 if (!isNullOrEmpty(dstTcpString)) {
Jonathan Hart5dfa43f2014-11-17 15:35:54 -0800273 selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString));
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800274 }
275
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800276 if (extHdrStringList != null) {
277 short extHdr = 0;
278 for (String extHdrString : extHdrStringList) {
279 extHdr = (short) (extHdr | ExtHeader.parseFromString(extHdrString));
280 }
281 selectorBuilder.matchIPv6ExthdrFlags(extHdr);
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800282 }
283
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700284 return selectorBuilder.build();
285 }
286
Ray Milkey460f4022014-11-05 15:41:43 -0800287 /**
Jonathan Hart2c25e362014-11-17 18:14:19 -0800288 * Generates a traffic treatment for this intent based on command line
289 * arguments presented to the command.
290 *
291 * @return traffic treatment
292 */
293 protected TrafficTreatment buildTrafficTreatment() {
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800294 final TrafficTreatment.Builder treatmentBuilder = builder();
Brian O'Connor11ff8702015-04-29 19:37:15 -0700295 boolean emptyTreatment = true;
Jonathan Hart2c25e362014-11-17 18:14:19 -0800296
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800297 if (!isNullOrEmpty(setEthSrcString)) {
298 treatmentBuilder.setEthSrc(MacAddress.valueOf(setEthSrcString));
Brian O'Connor11ff8702015-04-29 19:37:15 -0700299 emptyTreatment = false;
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700300 }
301
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800302 if (!isNullOrEmpty(setEthDstString)) {
303 treatmentBuilder.setEthDst(MacAddress.valueOf(setEthDstString));
Brian O'Connor11ff8702015-04-29 19:37:15 -0700304 emptyTreatment = false;
Jonathan Hart2c25e362014-11-17 18:14:19 -0800305 }
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800306
307 if (!isNullOrEmpty(setIpSrcString)) {
308 treatmentBuilder.setIpSrc(IpAddress.valueOf(setIpSrcString));
Brian O'Connor11ff8702015-04-29 19:37:15 -0700309 emptyTreatment = false;
Jonathan Hart2c25e362014-11-17 18:14:19 -0800310 }
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800311
312 if (!isNullOrEmpty(setIpDstString)) {
313 treatmentBuilder.setIpSrc(IpAddress.valueOf(setIpDstString));
Brian O'Connor11ff8702015-04-29 19:37:15 -0700314 emptyTreatment = false;
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800315 }
Dusan Pajin6b887c92015-07-01 18:32:49 +0200316 if (!isNullOrEmpty(setVlan)) {
317 treatmentBuilder.setVlanId(VlanId.vlanId(Short.parseShort(setVlan)));
318 emptyTreatment = false;
319 }
320 if (popVlan) {
321 treatmentBuilder.popVlan();
322 emptyTreatment = false;
323 }
324 if (!isNullOrEmpty(pushVlan)) {
325 treatmentBuilder.pushVlan();
326 treatmentBuilder.setVlanId(VlanId.vlanId(Short.parseShort(pushVlan)));
327 emptyTreatment = false;
328 }
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800329
Brian O'Connor11ff8702015-04-29 19:37:15 -0700330 if (emptyTreatment) {
331 return DefaultTrafficTreatment.emptyTreatment();
332 } else {
333 return treatmentBuilder.build();
334 }
Jonathan Hart2c25e362014-11-17 18:14:19 -0800335 }
336
337 /**
Ray Milkey460f4022014-11-05 15:41:43 -0800338 * Builds the constraint list for this command based on the command line
339 * parameters.
340 *
341 * @return List of constraint objects describing the constraints requested
342 */
343 protected List<Constraint> buildConstraints() {
344 final List<Constraint> constraints = new LinkedList<>();
345
346 // Check for a bandwidth specification
347 if (!isNullOrEmpty(bandwidthString)) {
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700348 final Bandwidth bandwidth = Bandwidth.bps(Double.parseDouble(bandwidthString));
349 constraints.add(new BandwidthConstraint(new BandwidthResource(bandwidth)));
Ray Milkey460f4022014-11-05 15:41:43 -0800350 }
351
352 // Check for a lambda specification
Sho SHIMIZU792c26b2015-06-08 19:08:32 -0700353 if (lambda) {
354 constraints.add(new LambdaConstraint(null));
Ray Milkey460f4022014-11-05 15:41:43 -0800355 }
Sho SHIMIZU792c26b2015-06-08 19:08:32 -0700356 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
357
Jonathan Hart96c5a4a2015-07-31 14:23:33 -0700358 if (partial) {
359 constraints.add(new PartialFailureConstraint());
360 }
361
Ray Milkey460f4022014-11-05 15:41:43 -0800362 return constraints;
363 }
Ray Milkey5b3717e2015-02-05 11:44:08 -0800364
Ray Milkey95c50872015-04-14 14:32:31 -0700365 @Override
366 protected ApplicationId appId() {
367 ApplicationId appIdForIntent;
368 if (appId == null) {
369 appIdForIntent = super.appId();
370 } else {
371 CoreService service = get(CoreService.class);
372 appIdForIntent = service.getAppId(appId);
373 }
374 return appIdForIntent;
375 }
376
Ray Milkey5b3717e2015-02-05 11:44:08 -0800377 /**
378 * Creates a key for an intent based on command line arguments. If a key
379 * has been specified, it is returned. If no key is specified, null
380 * is returned.
381 *
382 * @return intent key if specified, null otherwise
383 */
384 protected Key key() {
385 Key key = null;
Ray Milkey95c50872015-04-14 14:32:31 -0700386 ApplicationId appIdForIntent;
387
Ray Milkey5b3717e2015-02-05 11:44:08 -0800388 if (intentKey != null) {
389 key = Key.of(intentKey, appId());
390 }
391 return key;
392 }
Ray Milkeyc24cde32015-03-10 18:20:18 -0700393
394 /**
395 * Gets the priority to use for the intent.
396 *
397 * @return priority
398 */
399 protected int priority() {
400 return priority;
401 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700402}