blob: a8a28e24b5888f5f74036f50283263146e0ce38d [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 Milkey5b3717e2015-02-05 11:44:08 -080031import org.onosproject.net.intent.Key;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.constraint.BandwidthConstraint;
33import org.onosproject.net.intent.constraint.LambdaConstraint;
34import org.onosproject.net.intent.constraint.LinkTypeConstraint;
35import org.onosproject.net.resource.Bandwidth;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080036import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070037import org.onlab.packet.MacAddress;
38
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070039/**
40 * Base class for command line operations for connectivity based intents.
41 */
42public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
43
Jonathan Hart2c25e362014-11-17 18:14:19 -080044 // Selectors
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070045 @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address",
46 required = false, multiValued = false)
47 private String srcMacString = null;
48
49 @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address",
50 required = false, multiValued = false)
51 private String dstMacString = null;
52
53 @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type",
54 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080055 private String ethTypeString = null;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070056
Jonathan Hart5f7097e2014-11-11 11:50:28 -080057 @Option(name = "--ipProto", description = "IP Protocol",
58 required = false, multiValued = false)
59 private String ipProtoString = null;
60
61 @Option(name = "--ipSrc", description = "Source IP Address",
62 required = false, multiValued = false)
63 private String srcIpString = null;
64
65 @Option(name = "--ipDst", description = "Destination IP Address",
66 required = false, multiValued = false)
67 private String dstIpString = null;
68
69 @Option(name = "--tcpSrc", description = "Source TCP Port",
70 required = false, multiValued = false)
71 private String srcTcpString = null;
72
73 @Option(name = "--tcpDst", description = "Destination TCP Port",
74 required = false, multiValued = false)
75 private String dstTcpString = null;
76
Ray Milkey460f4022014-11-05 15:41:43 -080077 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
78 required = false, multiValued = false)
Ray Milkey04c78322015-01-21 14:57:58 -080079 private String bandwidthString = null;
Ray Milkey460f4022014-11-05 15:41:43 -080080
81 @Option(name = "-l", aliases = "--lambda", description = "Lambda",
82 required = false, multiValued = false)
83 private boolean lambda = false;
84
Ray Milkey5b3717e2015-02-05 11:44:08 -080085 @Option(name = "-k", aliases = "--key", description = "Intent Key",
86 required = false, multiValued = false)
87 private String intentKey = null;
88
Jonathan Hart2c25e362014-11-17 18:14:19 -080089
90 // Treatments
91 @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address",
92 required = false, multiValued = false)
93 private String setEthSrcString = null;
94
95 @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address",
96 required = false, multiValued = false)
97 private String setEthDstString = null;
98
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070099 /**
100 * Constructs a traffic selector based on the command line arguments
101 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800102 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700103 */
104 protected TrafficSelector buildTrafficSelector() {
105 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Jonathan Hart6e948282014-11-17 22:50:42 -0800106 short ethType = EthType.IPV4.value();
Ray Milkeycaa450b2014-10-29 15:54:24 -0700107
Ray Milkey460f4022014-11-05 15:41:43 -0800108 if (!isNullOrEmpty(ethTypeString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800109 ethType = EthType.parseFromString(ethTypeString);
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700110 }
111 selectorBuilder.matchEthType(ethType);
112
Ray Milkey460f4022014-11-05 15:41:43 -0800113 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700114 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
115 }
116
Ray Milkey460f4022014-11-05 15:41:43 -0800117 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700118 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
119 }
120
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800121 if (!isNullOrEmpty(ipProtoString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800122 short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
123 selectorBuilder.matchIPProtocol((byte) ipProtoShort);
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800124 }
125
126 if (!isNullOrEmpty(srcIpString)) {
127 selectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIpString));
128 }
129
130 if (!isNullOrEmpty(dstIpString)) {
131 selectorBuilder.matchIPDst(IpPrefix.valueOf(dstIpString));
132 }
133
134 if (!isNullOrEmpty(srcTcpString)) {
135 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
136 }
137
138 if (!isNullOrEmpty(dstTcpString)) {
Jonathan Hart5dfa43f2014-11-17 15:35:54 -0800139 selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString));
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800140 }
141
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700142 return selectorBuilder.build();
143 }
144
Ray Milkey460f4022014-11-05 15:41:43 -0800145 /**
Jonathan Hart2c25e362014-11-17 18:14:19 -0800146 * Generates a traffic treatment for this intent based on command line
147 * arguments presented to the command.
148 *
149 * @return traffic treatment
150 */
151 protected TrafficTreatment buildTrafficTreatment() {
152 final TrafficTreatment.Builder builder = builder();
153
154 if (!isNullOrEmpty(setEthSrcString)) {
155 final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString);
156 builder.setEthSrc(setEthSrc);
157 }
158 if (!isNullOrEmpty(setEthDstString)) {
159 final MacAddress setEthDst = MacAddress.valueOf(setEthDstString);
160 builder.setEthDst(setEthDst);
161 }
162 return builder.build();
163 }
164
165 /**
Ray Milkey460f4022014-11-05 15:41:43 -0800166 * Builds the constraint list for this command based on the command line
167 * parameters.
168 *
169 * @return List of constraint objects describing the constraints requested
170 */
171 protected List<Constraint> buildConstraints() {
172 final List<Constraint> constraints = new LinkedList<>();
173
174 // Check for a bandwidth specification
175 if (!isNullOrEmpty(bandwidthString)) {
176 final double bandwidthValue = Double.parseDouble(bandwidthString);
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800177 constraints.add(new BandwidthConstraint(Bandwidth.bps(bandwidthValue)));
Ray Milkey460f4022014-11-05 15:41:43 -0800178 }
179
180 // Check for a lambda specification
181 if (lambda) {
182 constraints.add(new LambdaConstraint(null));
183 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800184 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
Ray Milkey460f4022014-11-05 15:41:43 -0800185
186 return constraints;
187 }
Ray Milkey5b3717e2015-02-05 11:44:08 -0800188
189 /**
190 * Creates a key for an intent based on command line arguments. If a key
191 * has been specified, it is returned. If no key is specified, null
192 * is returned.
193 *
194 * @return intent key if specified, null otherwise
195 */
196 protected Key key() {
197 Key key = null;
198 if (intentKey != null) {
199 key = Key.of(intentKey, appId());
200 }
201 return key;
202 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700203}