Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Strings.isNullOrEmpty; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import static org.onosproject.net.flow.DefaultTrafficTreatment.builder; |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 20 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 95c5087 | 2015-04-14 14:32:31 -0700 | [diff] [blame^] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Brian O'Connor | 4f5a98a | 2015-03-14 19:50:09 -0700 | [diff] [blame] | 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.flow.TrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
| 33 | import org.onosproject.net.intent.Constraint; |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 34 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 37 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
| 38 | import org.onosproject.net.intent.constraint.LinkTypeConstraint; |
| 39 | import org.onosproject.net.resource.Bandwidth; |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 40 | import org.onlab.packet.IpPrefix; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 41 | import org.onlab.packet.MacAddress; |
| 42 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 43 | /** |
| 44 | * Base class for command line operations for connectivity based intents. |
| 45 | */ |
| 46 | public abstract class ConnectivityIntentCommand extends AbstractShellCommand { |
| 47 | |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 48 | // Selectors |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 49 | @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 Milkey | 04c7832 | 2015-01-21 14:57:58 -0800 | [diff] [blame] | 59 | private String ethTypeString = null; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 60 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 61 | @Option(name = "--ipProto", description = "IP Protocol", |
| 62 | required = false, multiValued = false) |
| 63 | private String ipProtoString = null; |
| 64 | |
Pavlin Radoslavov | 6ba7efc | 2015-03-20 16:26:10 -0700 | [diff] [blame] | 65 | @Option(name = "--ipSrc", description = "Source IP Prefix", |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 66 | required = false, multiValued = false) |
| 67 | private String srcIpString = null; |
| 68 | |
Pavlin Radoslavov | 6ba7efc | 2015-03-20 16:26:10 -0700 | [diff] [blame] | 69 | @Option(name = "--ipDst", description = "Destination IP Prefix", |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 70 | 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 Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 81 | @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth", |
| 82 | required = false, multiValued = false) |
Ray Milkey | 04c7832 | 2015-01-21 14:57:58 -0800 | [diff] [blame] | 83 | private String bandwidthString = null; |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 84 | |
| 85 | @Option(name = "-l", aliases = "--lambda", description = "Lambda", |
| 86 | required = false, multiValued = false) |
| 87 | private boolean lambda = false; |
| 88 | |
Ray Milkey | 95c5087 | 2015-04-14 14:32:31 -0700 | [diff] [blame^] | 89 | @Option(name = "-a", aliases = "--appId", description = "Application Id", |
| 90 | required = false, multiValued = false) |
| 91 | private String appId = null; |
| 92 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 93 | @Option(name = "-k", aliases = "--key", description = "Intent Key", |
| 94 | required = false, multiValued = false) |
| 95 | private String intentKey = null; |
| 96 | |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 97 | |
| 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 Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 107 | // Priorities |
| 108 | @Option(name = "-p", aliases = "--priority", description = "Priority", |
| 109 | required = false, multiValued = false) |
| 110 | private int priority = Intent.DEFAULT_INTENT_PRIORITY; |
| 111 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 112 | /** |
| 113 | * Constructs a traffic selector based on the command line arguments |
| 114 | * presented to the command. |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 115 | * @return traffic selector |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 116 | */ |
| 117 | protected TrafficSelector buildTrafficSelector() { |
Pavlin Radoslavov | 6ba7efc | 2015-03-20 16:26:10 -0700 | [diff] [blame] | 118 | IpPrefix srcIpPrefix = null; |
| 119 | IpPrefix dstIpPrefix = null; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 120 | |
Pavlin Radoslavov | 6ba7efc | 2015-03-20 16:26:10 -0700 | [diff] [blame] | 121 | 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 Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 159 | if (!isNullOrEmpty(ethTypeString)) { |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 160 | ethType = EthType.parseFromString(ethTypeString); |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 161 | } |
| 162 | selectorBuilder.matchEthType(ethType); |
| 163 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 164 | if (!isNullOrEmpty(srcMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 165 | selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString)); |
| 166 | } |
| 167 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 168 | if (!isNullOrEmpty(dstMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 169 | selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString)); |
| 170 | } |
| 171 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 172 | if (!isNullOrEmpty(ipProtoString)) { |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 173 | short ipProtoShort = IpProtocol.parseFromString(ipProtoString); |
| 174 | selectorBuilder.matchIPProtocol((byte) ipProtoShort); |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 177 | if (!isNullOrEmpty(srcTcpString)) { |
| 178 | selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString)); |
| 179 | } |
| 180 | |
| 181 | if (!isNullOrEmpty(dstTcpString)) { |
Jonathan Hart | 5dfa43f | 2014-11-17 15:35:54 -0800 | [diff] [blame] | 182 | selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString)); |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 185 | return selectorBuilder.build(); |
| 186 | } |
| 187 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 188 | /** |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 189 | * 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'Connor | 4f5a98a | 2015-03-14 19:50:09 -0700 | [diff] [blame] | 195 | boolean hasEthSrc = !isNullOrEmpty(setEthSrcString); |
| 196 | boolean hasEthDst = !isNullOrEmpty(setEthDstString); |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 197 | |
Brian O'Connor | 4f5a98a | 2015-03-14 19:50:09 -0700 | [diff] [blame] | 198 | if (!hasEthSrc && !hasEthDst) { |
| 199 | return DefaultTrafficTreatment.emptyTreatment(); |
| 200 | } |
| 201 | |
| 202 | final TrafficTreatment.Builder builder = builder(); |
| 203 | if (hasEthSrc) { |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 204 | final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString); |
| 205 | builder.setEthSrc(setEthSrc); |
| 206 | } |
Brian O'Connor | 4f5a98a | 2015-03-14 19:50:09 -0700 | [diff] [blame] | 207 | if (hasEthDst) { |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 208 | final MacAddress setEthDst = MacAddress.valueOf(setEthDstString); |
| 209 | builder.setEthDst(setEthDst); |
| 210 | } |
| 211 | return builder.build(); |
| 212 | } |
| 213 | |
| 214 | /** |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 215 | * 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 SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 226 | constraints.add(new BandwidthConstraint(Bandwidth.bps(bandwidthValue))); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | // Check for a lambda specification |
| 230 | if (lambda) { |
| 231 | constraints.add(new LambdaConstraint(null)); |
| 232 | } |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 233 | constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL)); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 234 | |
| 235 | return constraints; |
| 236 | } |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 237 | |
Ray Milkey | 95c5087 | 2015-04-14 14:32:31 -0700 | [diff] [blame^] | 238 | @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 Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 250 | /** |
| 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 Milkey | 95c5087 | 2015-04-14 14:32:31 -0700 | [diff] [blame^] | 259 | ApplicationId appIdForIntent; |
| 260 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 261 | if (intentKey != null) { |
| 262 | key = Key.of(intentKey, appId()); |
| 263 | } |
| 264 | return key; |
| 265 | } |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 266 | |
| 267 | /** |
| 268 | * Gets the priority to use for the intent. |
| 269 | * |
| 270 | * @return priority |
| 271 | */ |
| 272 | protected int priority() { |
| 273 | return priority; |
| 274 | } |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 275 | } |