blob: 308bbf9fee33091408957aade06ee29a391b5865 [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
63 @Option(name = "--ipSrc", description = "Source IP Address",
64 required = false, multiValued = false)
65 private String srcIpString = null;
66
67 @Option(name = "--ipDst", description = "Destination IP Address",
68 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() {
112 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Jonathan Hart6e948282014-11-17 22:50:42 -0800113 short ethType = EthType.IPV4.value();
Ray Milkeycaa450b2014-10-29 15:54:24 -0700114
Ray Milkey460f4022014-11-05 15:41:43 -0800115 if (!isNullOrEmpty(ethTypeString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800116 ethType = EthType.parseFromString(ethTypeString);
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700117 }
118 selectorBuilder.matchEthType(ethType);
119
Ray Milkey460f4022014-11-05 15:41:43 -0800120 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700121 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
122 }
123
Ray Milkey460f4022014-11-05 15:41:43 -0800124 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700125 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
126 }
127
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800128 if (!isNullOrEmpty(ipProtoString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800129 short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
130 selectorBuilder.matchIPProtocol((byte) ipProtoShort);
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800131 }
132
133 if (!isNullOrEmpty(srcIpString)) {
134 selectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIpString));
135 }
136
137 if (!isNullOrEmpty(dstIpString)) {
138 selectorBuilder.matchIPDst(IpPrefix.valueOf(dstIpString));
139 }
140
141 if (!isNullOrEmpty(srcTcpString)) {
142 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
143 }
144
145 if (!isNullOrEmpty(dstTcpString)) {
Jonathan Hart5dfa43f2014-11-17 15:35:54 -0800146 selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString));
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800147 }
148
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700149 return selectorBuilder.build();
150 }
151
Ray Milkey460f4022014-11-05 15:41:43 -0800152 /**
Jonathan Hart2c25e362014-11-17 18:14:19 -0800153 * Generates a traffic treatment for this intent based on command line
154 * arguments presented to the command.
155 *
156 * @return traffic treatment
157 */
158 protected TrafficTreatment buildTrafficTreatment() {
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700159 boolean hasEthSrc = !isNullOrEmpty(setEthSrcString);
160 boolean hasEthDst = !isNullOrEmpty(setEthDstString);
Jonathan Hart2c25e362014-11-17 18:14:19 -0800161
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700162 if (!hasEthSrc && !hasEthDst) {
163 return DefaultTrafficTreatment.emptyTreatment();
164 }
165
166 final TrafficTreatment.Builder builder = builder();
167 if (hasEthSrc) {
Jonathan Hart2c25e362014-11-17 18:14:19 -0800168 final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString);
169 builder.setEthSrc(setEthSrc);
170 }
Brian O'Connor4f5a98a2015-03-14 19:50:09 -0700171 if (hasEthDst) {
Jonathan Hart2c25e362014-11-17 18:14:19 -0800172 final MacAddress setEthDst = MacAddress.valueOf(setEthDstString);
173 builder.setEthDst(setEthDst);
174 }
175 return builder.build();
176 }
177
178 /**
Ray Milkey460f4022014-11-05 15:41:43 -0800179 * Builds the constraint list for this command based on the command line
180 * parameters.
181 *
182 * @return List of constraint objects describing the constraints requested
183 */
184 protected List<Constraint> buildConstraints() {
185 final List<Constraint> constraints = new LinkedList<>();
186
187 // Check for a bandwidth specification
188 if (!isNullOrEmpty(bandwidthString)) {
189 final double bandwidthValue = Double.parseDouble(bandwidthString);
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800190 constraints.add(new BandwidthConstraint(Bandwidth.bps(bandwidthValue)));
Ray Milkey460f4022014-11-05 15:41:43 -0800191 }
192
193 // Check for a lambda specification
194 if (lambda) {
195 constraints.add(new LambdaConstraint(null));
196 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800197 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
Ray Milkey460f4022014-11-05 15:41:43 -0800198
199 return constraints;
200 }
Ray Milkey5b3717e2015-02-05 11:44:08 -0800201
202 /**
203 * Creates a key for an intent based on command line arguments. If a key
204 * has been specified, it is returned. If no key is specified, null
205 * is returned.
206 *
207 * @return intent key if specified, null otherwise
208 */
209 protected Key key() {
210 Key key = null;
211 if (intentKey != null) {
212 key = Key.of(intentKey, appId());
213 }
214 return key;
215 }
Ray Milkeyc24cde32015-03-10 18:20:18 -0700216
217 /**
218 * Gets the priority to use for the intent.
219 *
220 * @return priority
221 */
222 protected int priority() {
223 return priority;
224 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700225}