blob: 2e88c3ab4bc4c8dcb70c5484cf51b57849a13bce [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;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070027import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.cli.AbstractShellCommand;
Ray Milkey95c50872015-04-14 14:32:31 -070029import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.Link;
32import org.onosproject.net.flow.DefaultTrafficSelector;
Brian O'Connor11ff8702015-04-29 19:37:15 -070033import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.flow.TrafficSelector;
35import org.onosproject.net.flow.TrafficTreatment;
36import org.onosproject.net.intent.Constraint;
Ray Milkeyc24cde32015-03-10 18:20:18 -070037import org.onosproject.net.intent.Intent;
Ray Milkey5b3717e2015-02-05 11:44:08 -080038import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.constraint.BandwidthConstraint;
40import org.onosproject.net.intent.constraint.LambdaConstraint;
41import org.onosproject.net.intent.constraint.LinkTypeConstraint;
Brian O'Connor6de2e202015-05-21 14:30:41 -070042import org.onosproject.net.resource.link.BandwidthResource;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080043import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070044import org.onlab.packet.MacAddress;
45
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070046/**
47 * Base class for command line operations for connectivity based intents.
48 */
49public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
50
Jonathan Hart2c25e362014-11-17 18:14:19 -080051 // Selectors
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070052 @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address",
53 required = false, multiValued = false)
54 private String srcMacString = null;
55
56 @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address",
57 required = false, multiValued = false)
58 private String dstMacString = null;
59
60 @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type",
61 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080062 private String ethTypeString = null;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070063
Jonathan Hart5f7097e2014-11-11 11:50:28 -080064 @Option(name = "--ipProto", description = "IP Protocol",
65 required = false, multiValued = false)
66 private String ipProtoString = null;
67
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -070068 @Option(name = "--ipSrc", description = "Source IP Prefix",
Jonathan Hart5f7097e2014-11-11 11:50:28 -080069 required = false, multiValued = false)
70 private String srcIpString = null;
71
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -070072 @Option(name = "--ipDst", description = "Destination IP Prefix",
Jonathan Hart5f7097e2014-11-11 11:50:28 -080073 required = false, multiValued = false)
74 private String dstIpString = null;
75
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +080076 @Option(name = "--fLabel", description = "IPv6 Flow Label",
77 required = false, multiValued = false)
78 private String fLabelString = null;
79
80 @Option(name = "--icmp6Type", description = "ICMPv6 Type",
81 required = false, multiValued = false)
82 private String icmp6TypeString = null;
83
84 @Option(name = "--icmp6Code", description = "ICMPv6 Code",
85 required = false, multiValued = false)
86 private String icmp6CodeString = null;
87
88 @Option(name = "--ndTarget", description = "IPv6 Neighbor Discovery Target Address",
89 required = false, multiValued = false)
90 private String ndTargetString = null;
91
92 @Option(name = "--ndSLL", description = "IPv6 Neighbor Discovery Source Link-Layer",
93 required = false, multiValued = false)
94 private String ndSLLString = null;
95
96 @Option(name = "--ndTLL", description = "IPv6 Neighbor Discovery Target Link-Layer",
97 required = false, multiValued = false)
98 private String ndTLLString = null;
99
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800100 @Option(name = "--tcpSrc", description = "Source TCP Port",
101 required = false, multiValued = false)
102 private String srcTcpString = null;
103
104 @Option(name = "--tcpDst", description = "Destination TCP Port",
105 required = false, multiValued = false)
106 private String dstTcpString = null;
107
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800108 @Option(name = "--extHdr", description = "IPv6 Extension Header Pseudo-field",
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800109 required = false, multiValued = true)
110 private List<String> extHdrStringList = null;
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800111
Ray Milkey460f4022014-11-05 15:41:43 -0800112 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
113 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -0800114 private String bandwidthString = null;
Ray Milkey460f4022014-11-05 15:41:43 -0800115
116 @Option(name = "-l", aliases = "--lambda", description = "Lambda",
117 required = false, multiValued = false)
118 private boolean lambda = false;
119
Ray Milkey95c50872015-04-14 14:32:31 -0700120 @Option(name = "-a", aliases = "--appId", description = "Application Id",
121 required = false, multiValued = false)
122 private String appId = null;
123
Ray Milkey5b3717e2015-02-05 11:44:08 -0800124 @Option(name = "-k", aliases = "--key", description = "Intent Key",
125 required = false, multiValued = false)
126 private String intentKey = null;
127
Jonathan Hart2c25e362014-11-17 18:14:19 -0800128
129 // Treatments
130 @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address",
131 required = false, multiValued = false)
132 private String setEthSrcString = null;
133
134 @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address",
135 required = false, multiValued = false)
136 private String setEthDstString = null;
137
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800138 @Option(name = "--setIpSrc", description = "Rewrite Source IP Address",
139 required = false, multiValued = false)
140 private String setIpSrcString = null;
141
142 @Option(name = "--setIpDst", description = "Rewrite Destination IP Address",
143 required = false, multiValued = false)
144 private String setIpDstString = null;
145
146
Ray Milkeyc24cde32015-03-10 18:20:18 -0700147 // Priorities
148 @Option(name = "-p", aliases = "--priority", description = "Priority",
149 required = false, multiValued = false)
150 private int priority = Intent.DEFAULT_INTENT_PRIORITY;
151
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700152 /**
153 * Constructs a traffic selector based on the command line arguments
154 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800155 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700156 */
157 protected TrafficSelector buildTrafficSelector() {
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -0700158 IpPrefix srcIpPrefix = null;
159 IpPrefix dstIpPrefix = null;
Ray Milkeycaa450b2014-10-29 15:54:24 -0700160
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -0700161 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
162
163 if (!isNullOrEmpty(srcIpString)) {
164 srcIpPrefix = IpPrefix.valueOf(srcIpString);
165 if (srcIpPrefix.isIp4()) {
166 selectorBuilder.matchIPSrc(srcIpPrefix);
167 } else {
168 selectorBuilder.matchIPv6Src(srcIpPrefix);
169 }
170 }
171
172 if (!isNullOrEmpty(dstIpString)) {
173 dstIpPrefix = IpPrefix.valueOf(dstIpString);
174 if (dstIpPrefix.isIp4()) {
175 selectorBuilder.matchIPDst(dstIpPrefix);
176 } else {
177 selectorBuilder.matchIPv6Dst(dstIpPrefix);
178 }
179 }
180
181 if ((srcIpPrefix != null) && (dstIpPrefix != null) &&
182 (srcIpPrefix.version() != dstIpPrefix.version())) {
183 // ERROR: IP src/dst version mismatch
184 throw new IllegalArgumentException(
185 "IP source and destination version mismatch");
186 }
187
188 //
189 // Set the default EthType based on the IP version if the matching
190 // source or destination IP prefixes.
191 //
192 short ethType = EthType.IPV4.value();
193 if ((srcIpPrefix != null) && srcIpPrefix.isIp6()) {
194 ethType = EthType.IPV6.value();
195 }
196 if ((dstIpPrefix != null) && dstIpPrefix.isIp6()) {
197 ethType = EthType.IPV6.value();
198 }
Ray Milkey460f4022014-11-05 15:41:43 -0800199 if (!isNullOrEmpty(ethTypeString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800200 ethType = EthType.parseFromString(ethTypeString);
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700201 }
202 selectorBuilder.matchEthType(ethType);
203
Ray Milkey460f4022014-11-05 15:41:43 -0800204 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700205 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
206 }
207
Ray Milkey460f4022014-11-05 15:41:43 -0800208 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700209 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
210 }
211
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800212 if (!isNullOrEmpty(ipProtoString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800213 short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
214 selectorBuilder.matchIPProtocol((byte) ipProtoShort);
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800215 }
216
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800217 if (!isNullOrEmpty(fLabelString)) {
218 selectorBuilder.matchIPv6FlowLabel(Integer.parseInt(fLabelString));
219 }
220
221 if (!isNullOrEmpty(icmp6TypeString)) {
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800222 byte icmp6Type = Icmp6Type.parseFromString(icmp6TypeString);
223 selectorBuilder.matchIcmpv6Type(icmp6Type);
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800224 }
225
226 if (!isNullOrEmpty(icmp6CodeString)) {
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800227 byte icmp6Code = Icmp6Code.parseFromString(icmp6CodeString);
228 selectorBuilder.matchIcmpv6Code(icmp6Code);
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800229 }
230
231 if (!isNullOrEmpty(ndTargetString)) {
232 selectorBuilder.matchIPv6NDTargetAddress(Ip6Address.valueOf(ndTargetString));
233 }
234
235 if (!isNullOrEmpty(ndSLLString)) {
236 selectorBuilder.matchIPv6NDSourceLinkLayerAddress(MacAddress.valueOf(ndSLLString));
237 }
238
239 if (!isNullOrEmpty(ndTLLString)) {
240 selectorBuilder.matchIPv6NDTargetLinkLayerAddress(MacAddress.valueOf(ndTLLString));
241 }
242
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800243 if (!isNullOrEmpty(srcTcpString)) {
244 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
245 }
246
247 if (!isNullOrEmpty(dstTcpString)) {
Jonathan Hart5dfa43f2014-11-17 15:35:54 -0800248 selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString));
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800249 }
250
Charles M.C. Chan2184de12015-04-26 02:24:53 +0800251 if (extHdrStringList != null) {
252 short extHdr = 0;
253 for (String extHdrString : extHdrStringList) {
254 extHdr = (short) (extHdr | ExtHeader.parseFromString(extHdrString));
255 }
256 selectorBuilder.matchIPv6ExthdrFlags(extHdr);
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800257 }
258
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700259 return selectorBuilder.build();
260 }
261
Ray Milkey460f4022014-11-05 15:41:43 -0800262 /**
Jonathan Hart2c25e362014-11-17 18:14:19 -0800263 * Generates a traffic treatment for this intent based on command line
264 * arguments presented to the command.
265 *
266 * @return traffic treatment
267 */
268 protected TrafficTreatment buildTrafficTreatment() {
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800269 final TrafficTreatment.Builder treatmentBuilder = builder();
Brian O'Connor11ff8702015-04-29 19:37:15 -0700270 boolean emptyTreatment = true;
Jonathan Hart2c25e362014-11-17 18:14:19 -0800271
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800272 if (!isNullOrEmpty(setEthSrcString)) {
273 treatmentBuilder.setEthSrc(MacAddress.valueOf(setEthSrcString));
Brian O'Connor11ff8702015-04-29 19:37:15 -0700274 emptyTreatment = false;
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700275 }
276
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800277 if (!isNullOrEmpty(setEthDstString)) {
278 treatmentBuilder.setEthDst(MacAddress.valueOf(setEthDstString));
Brian O'Connor11ff8702015-04-29 19:37:15 -0700279 emptyTreatment = false;
Jonathan Hart2c25e362014-11-17 18:14:19 -0800280 }
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800281
282 if (!isNullOrEmpty(setIpSrcString)) {
283 treatmentBuilder.setIpSrc(IpAddress.valueOf(setIpSrcString));
Brian O'Connor11ff8702015-04-29 19:37:15 -0700284 emptyTreatment = false;
Jonathan Hart2c25e362014-11-17 18:14:19 -0800285 }
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800286
287 if (!isNullOrEmpty(setIpDstString)) {
288 treatmentBuilder.setIpSrc(IpAddress.valueOf(setIpDstString));
Brian O'Connor11ff8702015-04-29 19:37:15 -0700289 emptyTreatment = false;
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +0800290 }
291
Brian O'Connor11ff8702015-04-29 19:37:15 -0700292 if (emptyTreatment) {
293 return DefaultTrafficTreatment.emptyTreatment();
294 } else {
295 return treatmentBuilder.build();
296 }
Jonathan Hart2c25e362014-11-17 18:14:19 -0800297 }
298
299 /**
Ray Milkey460f4022014-11-05 15:41:43 -0800300 * Builds the constraint list for this command based on the command line
301 * parameters.
302 *
303 * @return List of constraint objects describing the constraints requested
304 */
305 protected List<Constraint> buildConstraints() {
306 final List<Constraint> constraints = new LinkedList<>();
307
308 // Check for a bandwidth specification
309 if (!isNullOrEmpty(bandwidthString)) {
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -0700310 final Bandwidth bandwidth = Bandwidth.bps(Double.parseDouble(bandwidthString));
311 constraints.add(new BandwidthConstraint(new BandwidthResource(bandwidth)));
Ray Milkey460f4022014-11-05 15:41:43 -0800312 }
313
314 // Check for a lambda specification
315 if (lambda) {
316 constraints.add(new LambdaConstraint(null));
317 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800318 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
Ray Milkey460f4022014-11-05 15:41:43 -0800319
320 return constraints;
321 }
Ray Milkey5b3717e2015-02-05 11:44:08 -0800322
Ray Milkey95c50872015-04-14 14:32:31 -0700323 @Override
324 protected ApplicationId appId() {
325 ApplicationId appIdForIntent;
326 if (appId == null) {
327 appIdForIntent = super.appId();
328 } else {
329 CoreService service = get(CoreService.class);
330 appIdForIntent = service.getAppId(appId);
331 }
332 return appIdForIntent;
333 }
334
Ray Milkey5b3717e2015-02-05 11:44:08 -0800335 /**
336 * Creates a key for an intent based on command line arguments. If a key
337 * has been specified, it is returned. If no key is specified, null
338 * is returned.
339 *
340 * @return intent key if specified, null otherwise
341 */
342 protected Key key() {
343 Key key = null;
Ray Milkey95c50872015-04-14 14:32:31 -0700344 ApplicationId appIdForIntent;
345
Ray Milkey5b3717e2015-02-05 11:44:08 -0800346 if (intentKey != null) {
347 key = Key.of(intentKey, appId());
348 }
349 return key;
350 }
Ray Milkeyc24cde32015-03-10 18:20:18 -0700351
352 /**
353 * Gets the priority to use for the intent.
354 *
355 * @return priority
356 */
357 protected int priority() {
358 return priority;
359 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700360}