blob: 713603bb3ea1dc867ad015e87f9553f69a305e8c [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.Link;
27import org.onosproject.net.flow.DefaultTrafficSelector;
Brian O'Connor4f5a98a2015-03-14 19:50:09 -070028import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.flow.TrafficSelector;
30import org.onosproject.net.flow.TrafficTreatment;
31import org.onosproject.net.intent.Constraint;
Ray Milkeyc24cde32015-03-10 18:20:18 -070032import org.onosproject.net.intent.Intent;
Ray Milkey5b3717e2015-02-05 11:44:08 -080033import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.intent.constraint.BandwidthConstraint;
35import org.onosproject.net.intent.constraint.LambdaConstraint;
36import org.onosproject.net.intent.constraint.LinkTypeConstraint;
37import org.onosproject.net.resource.Bandwidth;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080038import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070039import org.onlab.packet.MacAddress;
40
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070041/**
42 * Base class for command line operations for connectivity based intents.
43 */
44public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
45
Jonathan Hart2c25e362014-11-17 18:14:19 -080046 // Selectors
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070047 @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address",
48 required = false, multiValued = false)
49 private String srcMacString = null;
50
51 @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address",
52 required = false, multiValued = false)
53 private String dstMacString = null;
54
55 @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type",
56 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080057 private String ethTypeString = null;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070058
Jonathan Hart5f7097e2014-11-11 11:50:28 -080059 @Option(name = "--ipProto", description = "IP Protocol",
60 required = false, multiValued = false)
61 private String ipProtoString = null;
62
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -070063 @Option(name = "--ipSrc", description = "Source IP Prefix",
Jonathan Hart5f7097e2014-11-11 11:50:28 -080064 required = false, multiValued = false)
65 private String srcIpString = null;
66
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -070067 @Option(name = "--ipDst", description = "Destination IP Prefix",
Jonathan Hart5f7097e2014-11-11 11:50:28 -080068 required = false, multiValued = false)
69 private String dstIpString = null;
70
71 @Option(name = "--tcpSrc", description = "Source TCP Port",
72 required = false, multiValued = false)
73 private String srcTcpString = null;
74
75 @Option(name = "--tcpDst", description = "Destination TCP Port",
76 required = false, multiValued = false)
77 private String dstTcpString = null;
78
Ray Milkey460f4022014-11-05 15:41:43 -080079 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
80 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080081 private String bandwidthString = null;
Ray Milkey460f4022014-11-05 15:41:43 -080082
83 @Option(name = "-l", aliases = "--lambda", description = "Lambda",
84 required = false, multiValued = false)
85 private boolean lambda = false;
86
Ray Milkey5b3717e2015-02-05 11:44:08 -080087 @Option(name = "-k", aliases = "--key", description = "Intent Key",
88 required = false, multiValued = false)
89 private String intentKey = null;
90
Jonathan Hart2c25e362014-11-17 18:14:19 -080091
92 // Treatments
93 @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address",
94 required = false, multiValued = false)
95 private String setEthSrcString = null;
96
97 @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address",
98 required = false, multiValued = false)
99 private String setEthDstString = null;
100
Ray Milkeyc24cde32015-03-10 18:20:18 -0700101 // Priorities
102 @Option(name = "-p", aliases = "--priority", description = "Priority",
103 required = false, multiValued = false)
104 private int priority = Intent.DEFAULT_INTENT_PRIORITY;
105
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700106 /**
107 * Constructs a traffic selector based on the command line arguments
108 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800109 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700110 */
111 protected TrafficSelector buildTrafficSelector() {
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -0700112 IpPrefix srcIpPrefix = null;
113 IpPrefix dstIpPrefix = null;
Ray Milkeycaa450b2014-10-29 15:54:24 -0700114
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -0700115 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
116
117 if (!isNullOrEmpty(srcIpString)) {
118 srcIpPrefix = IpPrefix.valueOf(srcIpString);
119 if (srcIpPrefix.isIp4()) {
120 selectorBuilder.matchIPSrc(srcIpPrefix);
121 } else {
122 selectorBuilder.matchIPv6Src(srcIpPrefix);
123 }
124 }
125
126 if (!isNullOrEmpty(dstIpString)) {
127 dstIpPrefix = IpPrefix.valueOf(dstIpString);
128 if (dstIpPrefix.isIp4()) {
129 selectorBuilder.matchIPDst(dstIpPrefix);
130 } else {
131 selectorBuilder.matchIPv6Dst(dstIpPrefix);
132 }
133 }
134
135 if ((srcIpPrefix != null) && (dstIpPrefix != null) &&
136 (srcIpPrefix.version() != dstIpPrefix.version())) {
137 // ERROR: IP src/dst version mismatch
138 throw new IllegalArgumentException(
139 "IP source and destination version mismatch");
140 }
141
142 //
143 // Set the default EthType based on the IP version if the matching
144 // source or destination IP prefixes.
145 //
146 short ethType = EthType.IPV4.value();
147 if ((srcIpPrefix != null) && srcIpPrefix.isIp6()) {
148 ethType = EthType.IPV6.value();
149 }
150 if ((dstIpPrefix != null) && dstIpPrefix.isIp6()) {
151 ethType = EthType.IPV6.value();
152 }
Ray Milkey460f4022014-11-05 15:41:43 -0800153 if (!isNullOrEmpty(ethTypeString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800154 ethType = EthType.parseFromString(ethTypeString);
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700155 }
156 selectorBuilder.matchEthType(ethType);
157
Ray Milkey460f4022014-11-05 15:41:43 -0800158 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700159 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
160 }
161
Ray Milkey460f4022014-11-05 15:41:43 -0800162 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700163 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
164 }
165
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800166 if (!isNullOrEmpty(ipProtoString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800167 short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
168 selectorBuilder.matchIPProtocol((byte) ipProtoShort);
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800169 }
170
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800171 if (!isNullOrEmpty(srcTcpString)) {
172 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
173 }
174
175 if (!isNullOrEmpty(dstTcpString)) {
Jonathan Hart5dfa43f2014-11-17 15:35:54 -0800176 selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString));
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800177 }
178
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700179 return selectorBuilder.build();
180 }
181
Ray Milkey460f4022014-11-05 15:41:43 -0800182 /**
Jonathan Hart2c25e362014-11-17 18:14:19 -0800183 * Generates a traffic treatment for this intent based on command line
184 * arguments presented to the command.
185 *
186 * @return traffic treatment
187 */
188 protected TrafficTreatment buildTrafficTreatment() {
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700189 boolean hasEthSrc = !isNullOrEmpty(setEthSrcString);
190 boolean hasEthDst = !isNullOrEmpty(setEthDstString);
Jonathan Hart2c25e362014-11-17 18:14:19 -0800191
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700192 if (!hasEthSrc && !hasEthDst) {
193 return DefaultTrafficTreatment.emptyTreatment();
194 }
195
196 final TrafficTreatment.Builder builder = builder();
197 if (hasEthSrc) {
Jonathan Hart2c25e362014-11-17 18:14:19 -0800198 final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString);
199 builder.setEthSrc(setEthSrc);
200 }
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700201 if (hasEthDst) {
Jonathan Hart2c25e362014-11-17 18:14:19 -0800202 final MacAddress setEthDst = MacAddress.valueOf(setEthDstString);
203 builder.setEthDst(setEthDst);
204 }
205 return builder.build();
206 }
207
208 /**
Ray Milkey460f4022014-11-05 15:41:43 -0800209 * Builds the constraint list for this command based on the command line
210 * parameters.
211 *
212 * @return List of constraint objects describing the constraints requested
213 */
214 protected List<Constraint> buildConstraints() {
215 final List<Constraint> constraints = new LinkedList<>();
216
217 // Check for a bandwidth specification
218 if (!isNullOrEmpty(bandwidthString)) {
219 final double bandwidthValue = Double.parseDouble(bandwidthString);
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800220 constraints.add(new BandwidthConstraint(Bandwidth.bps(bandwidthValue)));
Ray Milkey460f4022014-11-05 15:41:43 -0800221 }
222
223 // Check for a lambda specification
224 if (lambda) {
225 constraints.add(new LambdaConstraint(null));
226 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800227 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
Ray Milkey460f4022014-11-05 15:41:43 -0800228
229 return constraints;
230 }
Ray Milkey5b3717e2015-02-05 11:44:08 -0800231
232 /**
233 * Creates a key for an intent based on command line arguments. If a key
234 * has been specified, it is returned. If no key is specified, null
235 * is returned.
236 *
237 * @return intent key if specified, null otherwise
238 */
239 protected Key key() {
240 Key key = null;
241 if (intentKey != null) {
242 key = Key.of(intentKey, appId());
243 }
244 return key;
245 }
Ray Milkeyc24cde32015-03-10 18:20:18 -0700246
247 /**
248 * Gets the priority to use for the intent.
249 *
250 * @return priority
251 */
252 protected int priority() {
253 return priority;
254 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700255}