blob: ea95b5d436020dc77a14ec89e0ed3e46d4dcd2ca [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;
Ray Milkey95c50872015-04-14 14:32:31 -070026import org.onosproject.core.ApplicationId;
27import org.onosproject.core.CoreService;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.Link;
29import org.onosproject.net.flow.DefaultTrafficSelector;
Brian O'Connor4f5a98a2015-03-14 19:50:09 -070030import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
33import org.onosproject.net.intent.Constraint;
Ray Milkeyc24cde32015-03-10 18:20:18 -070034import org.onosproject.net.intent.Intent;
Ray Milkey5b3717e2015-02-05 11:44:08 -080035import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.intent.constraint.BandwidthConstraint;
37import org.onosproject.net.intent.constraint.LambdaConstraint;
38import org.onosproject.net.intent.constraint.LinkTypeConstraint;
39import org.onosproject.net.resource.Bandwidth;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080040import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070041import org.onlab.packet.MacAddress;
42
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070043/**
44 * Base class for command line operations for connectivity based intents.
45 */
46public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
47
Jonathan Hart2c25e362014-11-17 18:14:19 -080048 // Selectors
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070049 @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address",
50 required = false, multiValued = false)
51 private String srcMacString = null;
52
53 @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address",
54 required = false, multiValued = false)
55 private String dstMacString = null;
56
57 @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type",
58 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080059 private String ethTypeString = null;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070060
Jonathan Hart5f7097e2014-11-11 11:50:28 -080061 @Option(name = "--ipProto", description = "IP Protocol",
62 required = false, multiValued = false)
63 private String ipProtoString = null;
64
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -070065 @Option(name = "--ipSrc", description = "Source IP Prefix",
Jonathan Hart5f7097e2014-11-11 11:50:28 -080066 required = false, multiValued = false)
67 private String srcIpString = null;
68
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -070069 @Option(name = "--ipDst", description = "Destination IP Prefix",
Jonathan Hart5f7097e2014-11-11 11:50:28 -080070 required = false, multiValued = false)
71 private String dstIpString = null;
72
73 @Option(name = "--tcpSrc", description = "Source TCP Port",
74 required = false, multiValued = false)
75 private String srcTcpString = null;
76
77 @Option(name = "--tcpDst", description = "Destination TCP Port",
78 required = false, multiValued = false)
79 private String dstTcpString = null;
80
Ray Milkey460f4022014-11-05 15:41:43 -080081 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
82 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080083 private String bandwidthString = null;
Ray Milkey460f4022014-11-05 15:41:43 -080084
85 @Option(name = "-l", aliases = "--lambda", description = "Lambda",
86 required = false, multiValued = false)
87 private boolean lambda = false;
88
Ray Milkey95c50872015-04-14 14:32:31 -070089 @Option(name = "-a", aliases = "--appId", description = "Application Id",
90 required = false, multiValued = false)
91 private String appId = null;
92
Ray Milkey5b3717e2015-02-05 11:44:08 -080093 @Option(name = "-k", aliases = "--key", description = "Intent Key",
94 required = false, multiValued = false)
95 private String intentKey = null;
96
Jonathan Hart2c25e362014-11-17 18:14:19 -080097
98 // Treatments
99 @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address",
100 required = false, multiValued = false)
101 private String setEthSrcString = null;
102
103 @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address",
104 required = false, multiValued = false)
105 private String setEthDstString = null;
106
Ray Milkeyc24cde32015-03-10 18:20:18 -0700107 // Priorities
108 @Option(name = "-p", aliases = "--priority", description = "Priority",
109 required = false, multiValued = false)
110 private int priority = Intent.DEFAULT_INTENT_PRIORITY;
111
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700112 /**
113 * Constructs a traffic selector based on the command line arguments
114 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800115 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700116 */
117 protected TrafficSelector buildTrafficSelector() {
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -0700118 IpPrefix srcIpPrefix = null;
119 IpPrefix dstIpPrefix = null;
Ray Milkeycaa450b2014-10-29 15:54:24 -0700120
Pavlin Radoslavov6ba7efc2015-03-20 16:26:10 -0700121 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
122
123 if (!isNullOrEmpty(srcIpString)) {
124 srcIpPrefix = IpPrefix.valueOf(srcIpString);
125 if (srcIpPrefix.isIp4()) {
126 selectorBuilder.matchIPSrc(srcIpPrefix);
127 } else {
128 selectorBuilder.matchIPv6Src(srcIpPrefix);
129 }
130 }
131
132 if (!isNullOrEmpty(dstIpString)) {
133 dstIpPrefix = IpPrefix.valueOf(dstIpString);
134 if (dstIpPrefix.isIp4()) {
135 selectorBuilder.matchIPDst(dstIpPrefix);
136 } else {
137 selectorBuilder.matchIPv6Dst(dstIpPrefix);
138 }
139 }
140
141 if ((srcIpPrefix != null) && (dstIpPrefix != null) &&
142 (srcIpPrefix.version() != dstIpPrefix.version())) {
143 // ERROR: IP src/dst version mismatch
144 throw new IllegalArgumentException(
145 "IP source and destination version mismatch");
146 }
147
148 //
149 // Set the default EthType based on the IP version if the matching
150 // source or destination IP prefixes.
151 //
152 short ethType = EthType.IPV4.value();
153 if ((srcIpPrefix != null) && srcIpPrefix.isIp6()) {
154 ethType = EthType.IPV6.value();
155 }
156 if ((dstIpPrefix != null) && dstIpPrefix.isIp6()) {
157 ethType = EthType.IPV6.value();
158 }
Ray Milkey460f4022014-11-05 15:41:43 -0800159 if (!isNullOrEmpty(ethTypeString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800160 ethType = EthType.parseFromString(ethTypeString);
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700161 }
162 selectorBuilder.matchEthType(ethType);
163
Ray Milkey460f4022014-11-05 15:41:43 -0800164 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700165 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
166 }
167
Ray Milkey460f4022014-11-05 15:41:43 -0800168 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700169 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
170 }
171
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800172 if (!isNullOrEmpty(ipProtoString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800173 short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
174 selectorBuilder.matchIPProtocol((byte) ipProtoShort);
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800175 }
176
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800177 if (!isNullOrEmpty(srcTcpString)) {
178 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
179 }
180
181 if (!isNullOrEmpty(dstTcpString)) {
Jonathan Hart5dfa43f2014-11-17 15:35:54 -0800182 selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString));
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800183 }
184
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700185 return selectorBuilder.build();
186 }
187
Ray Milkey460f4022014-11-05 15:41:43 -0800188 /**
Jonathan Hart2c25e362014-11-17 18:14:19 -0800189 * Generates a traffic treatment for this intent based on command line
190 * arguments presented to the command.
191 *
192 * @return traffic treatment
193 */
194 protected TrafficTreatment buildTrafficTreatment() {
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700195 boolean hasEthSrc = !isNullOrEmpty(setEthSrcString);
196 boolean hasEthDst = !isNullOrEmpty(setEthDstString);
Jonathan Hart2c25e362014-11-17 18:14:19 -0800197
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700198 if (!hasEthSrc && !hasEthDst) {
199 return DefaultTrafficTreatment.emptyTreatment();
200 }
201
202 final TrafficTreatment.Builder builder = builder();
203 if (hasEthSrc) {
Jonathan Hart2c25e362014-11-17 18:14:19 -0800204 final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString);
205 builder.setEthSrc(setEthSrc);
206 }
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700207 if (hasEthDst) {
Jonathan Hart2c25e362014-11-17 18:14:19 -0800208 final MacAddress setEthDst = MacAddress.valueOf(setEthDstString);
209 builder.setEthDst(setEthDst);
210 }
211 return builder.build();
212 }
213
214 /**
Ray Milkey460f4022014-11-05 15:41:43 -0800215 * Builds the constraint list for this command based on the command line
216 * parameters.
217 *
218 * @return List of constraint objects describing the constraints requested
219 */
220 protected List<Constraint> buildConstraints() {
221 final List<Constraint> constraints = new LinkedList<>();
222
223 // Check for a bandwidth specification
224 if (!isNullOrEmpty(bandwidthString)) {
225 final double bandwidthValue = Double.parseDouble(bandwidthString);
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800226 constraints.add(new BandwidthConstraint(Bandwidth.bps(bandwidthValue)));
Ray Milkey460f4022014-11-05 15:41:43 -0800227 }
228
229 // Check for a lambda specification
230 if (lambda) {
231 constraints.add(new LambdaConstraint(null));
232 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800233 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
Ray Milkey460f4022014-11-05 15:41:43 -0800234
235 return constraints;
236 }
Ray Milkey5b3717e2015-02-05 11:44:08 -0800237
Ray Milkey95c50872015-04-14 14:32:31 -0700238 @Override
239 protected ApplicationId appId() {
240 ApplicationId appIdForIntent;
241 if (appId == null) {
242 appIdForIntent = super.appId();
243 } else {
244 CoreService service = get(CoreService.class);
245 appIdForIntent = service.getAppId(appId);
246 }
247 return appIdForIntent;
248 }
249
Ray Milkey5b3717e2015-02-05 11:44:08 -0800250 /**
251 * Creates a key for an intent based on command line arguments. If a key
252 * has been specified, it is returned. If no key is specified, null
253 * is returned.
254 *
255 * @return intent key if specified, null otherwise
256 */
257 protected Key key() {
258 Key key = null;
Ray Milkey95c50872015-04-14 14:32:31 -0700259 ApplicationId appIdForIntent;
260
Ray Milkey5b3717e2015-02-05 11:44:08 -0800261 if (intentKey != null) {
262 key = Key.of(intentKey, appId());
263 }
264 return key;
265 }
Ray Milkeyc24cde32015-03-10 18:20:18 -0700266
267 /**
268 * Gets the priority to use for the intent.
269 *
270 * @return priority
271 */
272 protected int priority() {
273 return priority;
274 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700275}