blob: 848b3a82193433fdf47ba6d3c901dd14cd1b3d4a [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;
28import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.intent.Constraint;
Ray Milkeyc24cde32015-03-10 18:20:18 -070031import org.onosproject.net.intent.Intent;
Ray Milkey5b3717e2015-02-05 11:44:08 -080032import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.intent.constraint.BandwidthConstraint;
34import org.onosproject.net.intent.constraint.LambdaConstraint;
35import org.onosproject.net.intent.constraint.LinkTypeConstraint;
36import org.onosproject.net.resource.Bandwidth;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080037import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070038import org.onlab.packet.MacAddress;
39
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070040/**
41 * Base class for command line operations for connectivity based intents.
42 */
43public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
44
Jonathan Hart2c25e362014-11-17 18:14:19 -080045 // Selectors
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070046 @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address",
47 required = false, multiValued = false)
48 private String srcMacString = null;
49
50 @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address",
51 required = false, multiValued = false)
52 private String dstMacString = null;
53
54 @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type",
55 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080056 private String ethTypeString = null;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070057
Jonathan Hart5f7097e2014-11-11 11:50:28 -080058 @Option(name = "--ipProto", description = "IP Protocol",
59 required = false, multiValued = false)
60 private String ipProtoString = null;
61
62 @Option(name = "--ipSrc", description = "Source IP Address",
63 required = false, multiValued = false)
64 private String srcIpString = null;
65
66 @Option(name = "--ipDst", description = "Destination IP Address",
67 required = false, multiValued = false)
68 private String dstIpString = null;
69
70 @Option(name = "--tcpSrc", description = "Source TCP Port",
71 required = false, multiValued = false)
72 private String srcTcpString = null;
73
74 @Option(name = "--tcpDst", description = "Destination TCP Port",
75 required = false, multiValued = false)
76 private String dstTcpString = null;
77
Ray Milkey460f4022014-11-05 15:41:43 -080078 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
79 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080080 private String bandwidthString = null;
Ray Milkey460f4022014-11-05 15:41:43 -080081
82 @Option(name = "-l", aliases = "--lambda", description = "Lambda",
83 required = false, multiValued = false)
84 private boolean lambda = false;
85
Ray Milkey5b3717e2015-02-05 11:44:08 -080086 @Option(name = "-k", aliases = "--key", description = "Intent Key",
87 required = false, multiValued = false)
88 private String intentKey = null;
89
Jonathan Hart2c25e362014-11-17 18:14:19 -080090
91 // Treatments
92 @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address",
93 required = false, multiValued = false)
94 private String setEthSrcString = null;
95
96 @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address",
97 required = false, multiValued = false)
98 private String setEthDstString = null;
99
Ray Milkeyc24cde32015-03-10 18:20:18 -0700100 // Priorities
101 @Option(name = "-p", aliases = "--priority", description = "Priority",
102 required = false, multiValued = false)
103 private int priority = Intent.DEFAULT_INTENT_PRIORITY;
104
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700105 /**
106 * Constructs a traffic selector based on the command line arguments
107 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800108 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700109 */
110 protected TrafficSelector buildTrafficSelector() {
111 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Jonathan Hart6e948282014-11-17 22:50:42 -0800112 short ethType = EthType.IPV4.value();
Ray Milkeycaa450b2014-10-29 15:54:24 -0700113
Ray Milkey460f4022014-11-05 15:41:43 -0800114 if (!isNullOrEmpty(ethTypeString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800115 ethType = EthType.parseFromString(ethTypeString);
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700116 }
117 selectorBuilder.matchEthType(ethType);
118
Ray Milkey460f4022014-11-05 15:41:43 -0800119 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700120 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
121 }
122
Ray Milkey460f4022014-11-05 15:41:43 -0800123 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700124 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
125 }
126
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800127 if (!isNullOrEmpty(ipProtoString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800128 short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
129 selectorBuilder.matchIPProtocol((byte) ipProtoShort);
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800130 }
131
132 if (!isNullOrEmpty(srcIpString)) {
133 selectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIpString));
134 }
135
136 if (!isNullOrEmpty(dstIpString)) {
137 selectorBuilder.matchIPDst(IpPrefix.valueOf(dstIpString));
138 }
139
140 if (!isNullOrEmpty(srcTcpString)) {
141 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
142 }
143
144 if (!isNullOrEmpty(dstTcpString)) {
Jonathan Hart5dfa43f2014-11-17 15:35:54 -0800145 selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString));
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800146 }
147
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700148 return selectorBuilder.build();
149 }
150
Ray Milkey460f4022014-11-05 15:41:43 -0800151 /**
Jonathan Hart2c25e362014-11-17 18:14:19 -0800152 * Generates a traffic treatment for this intent based on command line
153 * arguments presented to the command.
154 *
155 * @return traffic treatment
156 */
157 protected TrafficTreatment buildTrafficTreatment() {
158 final TrafficTreatment.Builder builder = builder();
159
160 if (!isNullOrEmpty(setEthSrcString)) {
161 final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString);
162 builder.setEthSrc(setEthSrc);
163 }
164 if (!isNullOrEmpty(setEthDstString)) {
165 final MacAddress setEthDst = MacAddress.valueOf(setEthDstString);
166 builder.setEthDst(setEthDst);
167 }
168 return builder.build();
169 }
170
171 /**
Ray Milkey460f4022014-11-05 15:41:43 -0800172 * Builds the constraint list for this command based on the command line
173 * parameters.
174 *
175 * @return List of constraint objects describing the constraints requested
176 */
177 protected List<Constraint> buildConstraints() {
178 final List<Constraint> constraints = new LinkedList<>();
179
180 // Check for a bandwidth specification
181 if (!isNullOrEmpty(bandwidthString)) {
182 final double bandwidthValue = Double.parseDouble(bandwidthString);
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800183 constraints.add(new BandwidthConstraint(Bandwidth.bps(bandwidthValue)));
Ray Milkey460f4022014-11-05 15:41:43 -0800184 }
185
186 // Check for a lambda specification
187 if (lambda) {
188 constraints.add(new LambdaConstraint(null));
189 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800190 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
Ray Milkey460f4022014-11-05 15:41:43 -0800191
192 return constraints;
193 }
Ray Milkey5b3717e2015-02-05 11:44:08 -0800194
195 /**
196 * Creates a key for an intent based on command line arguments. If a key
197 * has been specified, it is returned. If no key is specified, null
198 * is returned.
199 *
200 * @return intent key if specified, null otherwise
201 */
202 protected Key key() {
203 Key key = null;
204 if (intentKey != null) {
205 key = Key.of(intentKey, appId());
206 }
207 return key;
208 }
Ray Milkeyc24cde32015-03-10 18:20:18 -0700209
210 /**
211 * Gets the priority to use for the intent.
212 *
213 * @return priority
214 */
215 protected int priority() {
216 return priority;
217 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700218}