Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present 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 | |
| 18 | import org.apache.karaf.shell.commands.Option; |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 19 | import org.onlab.packet.Ip6Address; |
| 20 | import org.onlab.packet.IpAddress; |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 21 | import org.onlab.packet.IpPrefix; |
| 22 | import org.onlab.packet.MacAddress; |
| 23 | import org.onlab.packet.TpPort; |
Dusan Pajin | 6b887c9 | 2015-07-01 18:32:49 +0200 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 25 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.cli.AbstractShellCommand; |
Ray Milkey | 95c5087 | 2015-04-14 14:32:31 -0700 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Michele Santuari | 05b882c | 2015-11-25 09:40:46 +0100 | [diff] [blame] | 29 | import org.onosproject.net.EncapsulationType; |
Steffen Gebert | ba2d3b7 | 2015-10-22 11:14:31 +0200 | [diff] [blame] | 30 | import org.onosproject.net.PortNumber; |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 31 | import org.onosproject.net.ResourceGroup; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Brian O'Connor | 11ff870 | 2015-04-29 19:37:15 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.TrafficSelector; |
| 35 | import org.onosproject.net.flow.TrafficTreatment; |
| 36 | import org.onosproject.net.intent.Constraint; |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 37 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame] | 40 | import org.onosproject.net.intent.constraint.DomainConstraint; |
Michele Santuari | 05b882c | 2015-11-25 09:40:46 +0100 | [diff] [blame] | 41 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
jaegonkim | 7e87663 | 2017-01-25 06:01:49 +0900 | [diff] [blame] | 42 | import org.onosproject.net.intent.constraint.HashedPathSelectionConstraint; |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 43 | import org.onosproject.net.intent.constraint.PartialFailureConstraint; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 44 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 45 | import java.util.LinkedList; |
| 46 | import java.util.List; |
| 47 | |
| 48 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 49 | import static org.onosproject.net.flow.DefaultTrafficTreatment.builder; |
| 50 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 51 | /** |
| 52 | * Base class for command line operations for connectivity based intents. |
| 53 | */ |
| 54 | public abstract class ConnectivityIntentCommand extends AbstractShellCommand { |
| 55 | |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 56 | // Selectors |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 57 | @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address", |
| 58 | required = false, multiValued = false) |
| 59 | private String srcMacString = null; |
| 60 | |
| 61 | @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address", |
| 62 | required = false, multiValued = false) |
| 63 | private String dstMacString = null; |
| 64 | |
| 65 | @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type", |
| 66 | required = false, multiValued = false) |
Ray Milkey | 04c7832 | 2015-01-21 14:57:58 -0800 | [diff] [blame] | 67 | private String ethTypeString = null; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 68 | |
Dusan Pajin | 6b887c9 | 2015-07-01 18:32:49 +0200 | [diff] [blame] | 69 | @Option(name = "-v", aliases = "--vlan", description = "VLAN ID", |
| 70 | required = false, multiValued = false) |
| 71 | private String vlanString = null; |
| 72 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 73 | @Option(name = "--ipProto", description = "IP Protocol", |
| 74 | required = false, multiValued = false) |
| 75 | private String ipProtoString = null; |
| 76 | |
Pavlin Radoslavov | 6ba7efc | 2015-03-20 16:26:10 -0700 | [diff] [blame] | 77 | @Option(name = "--ipSrc", description = "Source IP Prefix", |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 78 | required = false, multiValued = false) |
| 79 | private String srcIpString = null; |
| 80 | |
Pavlin Radoslavov | 6ba7efc | 2015-03-20 16:26:10 -0700 | [diff] [blame] | 81 | @Option(name = "--ipDst", description = "Destination IP Prefix", |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 82 | required = false, multiValued = false) |
| 83 | private String dstIpString = null; |
| 84 | |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 85 | @Option(name = "--fLabel", description = "IPv6 Flow Label", |
| 86 | required = false, multiValued = false) |
| 87 | private String fLabelString = null; |
| 88 | |
| 89 | @Option(name = "--icmp6Type", description = "ICMPv6 Type", |
| 90 | required = false, multiValued = false) |
| 91 | private String icmp6TypeString = null; |
| 92 | |
| 93 | @Option(name = "--icmp6Code", description = "ICMPv6 Code", |
| 94 | required = false, multiValued = false) |
| 95 | private String icmp6CodeString = null; |
| 96 | |
| 97 | @Option(name = "--ndTarget", description = "IPv6 Neighbor Discovery Target Address", |
| 98 | required = false, multiValued = false) |
| 99 | private String ndTargetString = null; |
| 100 | |
| 101 | @Option(name = "--ndSLL", description = "IPv6 Neighbor Discovery Source Link-Layer", |
| 102 | required = false, multiValued = false) |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 103 | private String ndSllString = null; |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 104 | |
| 105 | @Option(name = "--ndTLL", description = "IPv6 Neighbor Discovery Target Link-Layer", |
| 106 | required = false, multiValued = false) |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 107 | private String ndTllString = null; |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 108 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 109 | @Option(name = "--tcpSrc", description = "Source TCP Port", |
| 110 | required = false, multiValued = false) |
| 111 | private String srcTcpString = null; |
| 112 | |
| 113 | @Option(name = "--tcpDst", description = "Destination TCP Port", |
| 114 | required = false, multiValued = false) |
| 115 | private String dstTcpString = null; |
| 116 | |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 117 | @Option(name = "--extHdr", description = "IPv6 Extension Header Pseudo-field", |
Charles M.C. Chan | 2184de1 | 2015-04-26 02:24:53 +0800 | [diff] [blame] | 118 | required = false, multiValued = true) |
| 119 | private List<String> extHdrStringList = null; |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 120 | |
Ray Milkey | 95c5087 | 2015-04-14 14:32:31 -0700 | [diff] [blame] | 121 | @Option(name = "-a", aliases = "--appId", description = "Application Id", |
| 122 | required = false, multiValued = false) |
| 123 | private String appId = null; |
| 124 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 125 | @Option(name = "-k", aliases = "--key", description = "Intent Key", |
| 126 | required = false, multiValued = false) |
| 127 | private String intentKey = null; |
| 128 | |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 129 | |
| 130 | // Treatments |
| 131 | @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address", |
| 132 | required = false, multiValued = false) |
| 133 | private String setEthSrcString = null; |
| 134 | |
| 135 | @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address", |
| 136 | required = false, multiValued = false) |
| 137 | private String setEthDstString = null; |
| 138 | |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 139 | @Option(name = "--setIpSrc", description = "Rewrite Source IP Address", |
| 140 | required = false, multiValued = false) |
| 141 | private String setIpSrcString = null; |
| 142 | |
| 143 | @Option(name = "--setIpDst", description = "Rewrite Destination IP Address", |
| 144 | required = false, multiValued = false) |
| 145 | private String setIpDstString = null; |
| 146 | |
Dusan Pajin | 6b887c9 | 2015-07-01 18:32:49 +0200 | [diff] [blame] | 147 | @Option(name = "--setVlan", description = "Rewrite VLAN ID", |
| 148 | required = false, multiValued = false) |
| 149 | private String setVlan = null; |
| 150 | |
| 151 | @Option(name = "--popVlan", description = "Pop VLAN Tag", |
| 152 | required = false, multiValued = false) |
| 153 | private boolean popVlan = false; |
| 154 | |
| 155 | @Option(name = "--pushVlan", description = "Push VLAN ID", |
| 156 | required = false, multiValued = false) |
| 157 | private String pushVlan = null; |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 158 | |
Steffen Gebert | ba2d3b7 | 2015-10-22 11:14:31 +0200 | [diff] [blame] | 159 | @Option(name = "--setQueue", description = "Set Queue ID (for OpenFlow 1.0, " + |
| 160 | "also the port has to be specified, i.e., <port>/<queue>", |
Steffen Gebert | bbfdaaa | 2015-09-29 11:01:46 +0200 | [diff] [blame] | 161 | required = false, multiValued = false) |
| 162 | private String setQueue = null; |
| 163 | |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 164 | // Priorities |
| 165 | @Option(name = "-p", aliases = "--priority", description = "Priority", |
| 166 | required = false, multiValued = false) |
| 167 | private int priority = Intent.DEFAULT_INTENT_PRIORITY; |
| 168 | |
Michele Santuari | 05b882c | 2015-11-25 09:40:46 +0100 | [diff] [blame] | 169 | // Constraints |
| 170 | @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth", |
| 171 | required = false, multiValued = false) |
| 172 | private String bandwidthString = null; |
| 173 | |
Michele Santuari | 05b882c | 2015-11-25 09:40:46 +0100 | [diff] [blame] | 174 | @Option(name = "--partial", description = "Allow partial installation", |
| 175 | required = false, multiValued = false) |
| 176 | private boolean partial = false; |
| 177 | |
| 178 | @Option(name = "-e", aliases = "--encapsulation", description = "Encapsulation type", |
| 179 | required = false, multiValued = false) |
| 180 | private String encapsulationString = null; |
| 181 | |
jaegonkim | 7e87663 | 2017-01-25 06:01:49 +0900 | [diff] [blame] | 182 | @Option(name = "--hashed", description = "Hashed path selection", |
| 183 | required = false, multiValued = false) |
| 184 | private boolean hashedPathSelection = false; |
| 185 | |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame] | 186 | @Option(name = "--domains", description = "Allow domain delegation", |
| 187 | required = false, multiValued = false) |
| 188 | private boolean domains = false; |
| 189 | |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 190 | // Resource Group |
| 191 | @Option(name = "-r", aliases = "--resourceGroup", description = "Resource Group Id", |
| 192 | required = false, multiValued = false) |
| 193 | private String resourceGroupId = null; |
| 194 | |
Michele Santuari | 05b882c | 2015-11-25 09:40:46 +0100 | [diff] [blame] | 195 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Constructs a traffic selector based on the command line arguments |
| 198 | * presented to the command. |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 199 | * @return traffic selector |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 200 | */ |
| 201 | protected TrafficSelector buildTrafficSelector() { |
Pavlin Radoslavov | 6ba7efc | 2015-03-20 16:26:10 -0700 | [diff] [blame] | 202 | IpPrefix srcIpPrefix = null; |
| 203 | IpPrefix dstIpPrefix = null; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 204 | |
Pavlin Radoslavov | 6ba7efc | 2015-03-20 16:26:10 -0700 | [diff] [blame] | 205 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 206 | |
| 207 | if (!isNullOrEmpty(srcIpString)) { |
| 208 | srcIpPrefix = IpPrefix.valueOf(srcIpString); |
| 209 | if (srcIpPrefix.isIp4()) { |
| 210 | selectorBuilder.matchIPSrc(srcIpPrefix); |
| 211 | } else { |
| 212 | selectorBuilder.matchIPv6Src(srcIpPrefix); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (!isNullOrEmpty(dstIpString)) { |
| 217 | dstIpPrefix = IpPrefix.valueOf(dstIpString); |
| 218 | if (dstIpPrefix.isIp4()) { |
| 219 | selectorBuilder.matchIPDst(dstIpPrefix); |
| 220 | } else { |
| 221 | selectorBuilder.matchIPv6Dst(dstIpPrefix); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | if ((srcIpPrefix != null) && (dstIpPrefix != null) && |
| 226 | (srcIpPrefix.version() != dstIpPrefix.version())) { |
| 227 | // ERROR: IP src/dst version mismatch |
| 228 | throw new IllegalArgumentException( |
| 229 | "IP source and destination version mismatch"); |
| 230 | } |
| 231 | |
| 232 | // |
| 233 | // Set the default EthType based on the IP version if the matching |
| 234 | // source or destination IP prefixes. |
| 235 | // |
Ayaka Koshibe | 8117f36 | 2015-06-02 13:44:01 -0700 | [diff] [blame] | 236 | Short ethType = null; |
Pavlin Radoslavov | 6ba7efc | 2015-03-20 16:26:10 -0700 | [diff] [blame] | 237 | if ((srcIpPrefix != null) && srcIpPrefix.isIp6()) { |
| 238 | ethType = EthType.IPV6.value(); |
| 239 | } |
| 240 | if ((dstIpPrefix != null) && dstIpPrefix.isIp6()) { |
| 241 | ethType = EthType.IPV6.value(); |
| 242 | } |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 243 | if (!isNullOrEmpty(ethTypeString)) { |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 244 | ethType = EthType.parseFromString(ethTypeString); |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 245 | } |
Ayaka Koshibe | 8117f36 | 2015-06-02 13:44:01 -0700 | [diff] [blame] | 246 | if (ethType != null) { |
| 247 | selectorBuilder.matchEthType(ethType); |
| 248 | } |
Dusan Pajin | 6b887c9 | 2015-07-01 18:32:49 +0200 | [diff] [blame] | 249 | if (!isNullOrEmpty(vlanString)) { |
| 250 | selectorBuilder.matchVlanId(VlanId.vlanId(Short.parseShort(vlanString))); |
| 251 | } |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 252 | if (!isNullOrEmpty(srcMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 253 | selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString)); |
| 254 | } |
| 255 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 256 | if (!isNullOrEmpty(dstMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 257 | selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString)); |
| 258 | } |
| 259 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 260 | if (!isNullOrEmpty(ipProtoString)) { |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 261 | short ipProtoShort = IpProtocol.parseFromString(ipProtoString); |
| 262 | selectorBuilder.matchIPProtocol((byte) ipProtoShort); |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 265 | if (!isNullOrEmpty(fLabelString)) { |
| 266 | selectorBuilder.matchIPv6FlowLabel(Integer.parseInt(fLabelString)); |
| 267 | } |
| 268 | |
| 269 | if (!isNullOrEmpty(icmp6TypeString)) { |
Charles M.C. Chan | 2184de1 | 2015-04-26 02:24:53 +0800 | [diff] [blame] | 270 | byte icmp6Type = Icmp6Type.parseFromString(icmp6TypeString); |
| 271 | selectorBuilder.matchIcmpv6Type(icmp6Type); |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | if (!isNullOrEmpty(icmp6CodeString)) { |
Charles M.C. Chan | 2184de1 | 2015-04-26 02:24:53 +0800 | [diff] [blame] | 275 | byte icmp6Code = Icmp6Code.parseFromString(icmp6CodeString); |
| 276 | selectorBuilder.matchIcmpv6Code(icmp6Code); |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (!isNullOrEmpty(ndTargetString)) { |
| 280 | selectorBuilder.matchIPv6NDTargetAddress(Ip6Address.valueOf(ndTargetString)); |
| 281 | } |
| 282 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 283 | if (!isNullOrEmpty(ndSllString)) { |
| 284 | selectorBuilder.matchIPv6NDSourceLinkLayerAddress(MacAddress.valueOf(ndSllString)); |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 285 | } |
| 286 | |
Jonathan Hart | d9df7bd | 2015-11-10 17:10:25 -0800 | [diff] [blame] | 287 | if (!isNullOrEmpty(ndTllString)) { |
| 288 | selectorBuilder.matchIPv6NDTargetLinkLayerAddress(MacAddress.valueOf(ndTllString)); |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 289 | } |
| 290 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 291 | if (!isNullOrEmpty(srcTcpString)) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 292 | selectorBuilder.matchTcpSrc(TpPort.tpPort(Integer.parseInt(srcTcpString))); |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | if (!isNullOrEmpty(dstTcpString)) { |
Hyunsun Moon | cf732fb | 2015-08-22 21:04:23 -0700 | [diff] [blame] | 296 | selectorBuilder.matchTcpDst(TpPort.tpPort(Integer.parseInt(dstTcpString))); |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Charles M.C. Chan | 2184de1 | 2015-04-26 02:24:53 +0800 | [diff] [blame] | 299 | if (extHdrStringList != null) { |
| 300 | short extHdr = 0; |
| 301 | for (String extHdrString : extHdrStringList) { |
| 302 | extHdr = (short) (extHdr | ExtHeader.parseFromString(extHdrString)); |
| 303 | } |
| 304 | selectorBuilder.matchIPv6ExthdrFlags(extHdr); |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 305 | } |
| 306 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 307 | return selectorBuilder.build(); |
| 308 | } |
| 309 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 310 | /** |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 311 | * Generates a traffic treatment for this intent based on command line |
| 312 | * arguments presented to the command. |
| 313 | * |
| 314 | * @return traffic treatment |
| 315 | */ |
| 316 | protected TrafficTreatment buildTrafficTreatment() { |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 317 | final TrafficTreatment.Builder treatmentBuilder = builder(); |
Brian O'Connor | 11ff870 | 2015-04-29 19:37:15 -0700 | [diff] [blame] | 318 | boolean emptyTreatment = true; |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 319 | |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 320 | if (!isNullOrEmpty(setEthSrcString)) { |
| 321 | treatmentBuilder.setEthSrc(MacAddress.valueOf(setEthSrcString)); |
Brian O'Connor | 11ff870 | 2015-04-29 19:37:15 -0700 | [diff] [blame] | 322 | emptyTreatment = false; |
Brian O'Connor | 4f5a98a | 2015-03-14 19:50:09 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 325 | if (!isNullOrEmpty(setEthDstString)) { |
| 326 | treatmentBuilder.setEthDst(MacAddress.valueOf(setEthDstString)); |
Brian O'Connor | 11ff870 | 2015-04-29 19:37:15 -0700 | [diff] [blame] | 327 | emptyTreatment = false; |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 328 | } |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 329 | |
| 330 | if (!isNullOrEmpty(setIpSrcString)) { |
| 331 | treatmentBuilder.setIpSrc(IpAddress.valueOf(setIpSrcString)); |
Brian O'Connor | 11ff870 | 2015-04-29 19:37:15 -0700 | [diff] [blame] | 332 | emptyTreatment = false; |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 333 | } |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 334 | |
| 335 | if (!isNullOrEmpty(setIpDstString)) { |
Charles Chan | d448988 | 2016-04-05 11:38:09 -0700 | [diff] [blame] | 336 | treatmentBuilder.setIpDst(IpAddress.valueOf(setIpDstString)); |
Brian O'Connor | 11ff870 | 2015-04-29 19:37:15 -0700 | [diff] [blame] | 337 | emptyTreatment = false; |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 338 | } |
Dusan Pajin | 6b887c9 | 2015-07-01 18:32:49 +0200 | [diff] [blame] | 339 | if (!isNullOrEmpty(setVlan)) { |
| 340 | treatmentBuilder.setVlanId(VlanId.vlanId(Short.parseShort(setVlan))); |
| 341 | emptyTreatment = false; |
| 342 | } |
| 343 | if (popVlan) { |
| 344 | treatmentBuilder.popVlan(); |
| 345 | emptyTreatment = false; |
| 346 | } |
| 347 | if (!isNullOrEmpty(pushVlan)) { |
| 348 | treatmentBuilder.pushVlan(); |
| 349 | treatmentBuilder.setVlanId(VlanId.vlanId(Short.parseShort(pushVlan))); |
| 350 | emptyTreatment = false; |
| 351 | } |
Steffen Gebert | bbfdaaa | 2015-09-29 11:01:46 +0200 | [diff] [blame] | 352 | if (!isNullOrEmpty(setQueue)) { |
Steffen Gebert | ba2d3b7 | 2015-10-22 11:14:31 +0200 | [diff] [blame] | 353 | // OpenFlow 1.0 notation (for ENQUEUE): <port>/<queue> |
| 354 | if (setQueue.contains("/")) { |
| 355 | String[] queueConfig = setQueue.split("/"); |
| 356 | PortNumber port = PortNumber.portNumber(Long.parseLong(queueConfig[0])); |
| 357 | long queueId = Long.parseLong(queueConfig[1]); |
| 358 | treatmentBuilder.setQueue(queueId, port); |
| 359 | } else { |
| 360 | treatmentBuilder.setQueue(Long.parseLong(setQueue)); |
| 361 | } |
Steffen Gebert | bbfdaaa | 2015-09-29 11:01:46 +0200 | [diff] [blame] | 362 | emptyTreatment = false; |
| 363 | } |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 364 | |
Brian O'Connor | 11ff870 | 2015-04-29 19:37:15 -0700 | [diff] [blame] | 365 | if (emptyTreatment) { |
| 366 | return DefaultTrafficTreatment.emptyTreatment(); |
| 367 | } else { |
| 368 | return treatmentBuilder.build(); |
| 369 | } |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /** |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 373 | * Builds the constraint list for this command based on the command line |
| 374 | * parameters. |
| 375 | * |
| 376 | * @return List of constraint objects describing the constraints requested |
| 377 | */ |
| 378 | protected List<Constraint> buildConstraints() { |
| 379 | final List<Constraint> constraints = new LinkedList<>(); |
| 380 | |
| 381 | // Check for a bandwidth specification |
| 382 | if (!isNullOrEmpty(bandwidthString)) { |
Sho SHIMIZU | 31f37ed | 2016-01-08 18:45:54 -0800 | [diff] [blame] | 383 | Bandwidth bandwidth; |
| 384 | try { |
| 385 | bandwidth = Bandwidth.bps(Long.parseLong(bandwidthString)); |
| 386 | // when the string can't be parsed as long, then try to parse as double |
| 387 | } catch (NumberFormatException e) { |
| 388 | bandwidth = Bandwidth.bps(Double.parseDouble(bandwidthString)); |
| 389 | } |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 390 | constraints.add(new BandwidthConstraint(bandwidth)); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Michele Santuari | 05b882c | 2015-11-25 09:40:46 +0100 | [diff] [blame] | 393 | // Check for partial failure specification |
Jonathan Hart | 96c5a4a | 2015-07-31 14:23:33 -0700 | [diff] [blame] | 394 | if (partial) { |
| 395 | constraints.add(new PartialFailureConstraint()); |
| 396 | } |
| 397 | |
Michele Santuari | 05b882c | 2015-11-25 09:40:46 +0100 | [diff] [blame] | 398 | // Check for encapsulation specification |
| 399 | if (!isNullOrEmpty(encapsulationString)) { |
| 400 | final EncapsulationType encapType = EncapsulationType.valueOf(encapsulationString); |
| 401 | constraints.add(new EncapsulationConstraint(encapType)); |
| 402 | } |
| 403 | |
jaegonkim | 7e87663 | 2017-01-25 06:01:49 +0900 | [diff] [blame] | 404 | // Check for hashed path selection |
| 405 | if (hashedPathSelection) { |
| 406 | constraints.add(new HashedPathSelectionConstraint()); |
| 407 | } |
Thomas Szyrkowiec | 770093f | 2017-05-05 13:06:53 +0200 | [diff] [blame] | 408 | |
| 409 | // Check for domain processing |
| 410 | if (domains) { |
| 411 | constraints.add(DomainConstraint.domain()); |
| 412 | } |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 413 | return constraints; |
| 414 | } |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 415 | |
Ray Milkey | 95c5087 | 2015-04-14 14:32:31 -0700 | [diff] [blame] | 416 | @Override |
| 417 | protected ApplicationId appId() { |
| 418 | ApplicationId appIdForIntent; |
| 419 | if (appId == null) { |
| 420 | appIdForIntent = super.appId(); |
| 421 | } else { |
| 422 | CoreService service = get(CoreService.class); |
| 423 | appIdForIntent = service.getAppId(appId); |
| 424 | } |
| 425 | return appIdForIntent; |
| 426 | } |
| 427 | |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 428 | protected ResourceGroup resourceGroup() { |
| 429 | if (resourceGroupId != null) { |
| 430 | if (resourceGroupId.toLowerCase().startsWith("0x")) { |
| 431 | return ResourceGroup.of(Long.parseUnsignedLong(resourceGroupId.substring(2), 16)); |
| 432 | } else { |
| 433 | return ResourceGroup.of(Long.parseUnsignedLong(resourceGroupId)); |
| 434 | } |
| 435 | } else { |
| 436 | return null; |
| 437 | } |
| 438 | } |
| 439 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 440 | /** |
| 441 | * Creates a key for an intent based on command line arguments. If a key |
| 442 | * has been specified, it is returned. If no key is specified, null |
| 443 | * is returned. |
| 444 | * |
| 445 | * @return intent key if specified, null otherwise |
| 446 | */ |
| 447 | protected Key key() { |
| 448 | Key key = null; |
Ray Milkey | 95c5087 | 2015-04-14 14:32:31 -0700 | [diff] [blame] | 449 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 450 | if (intentKey != null) { |
| 451 | key = Key.of(intentKey, appId()); |
| 452 | } |
| 453 | return key; |
| 454 | } |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 455 | |
| 456 | /** |
| 457 | * Gets the priority to use for the intent. |
| 458 | * |
| 459 | * @return priority |
| 460 | */ |
| 461 | protected int priority() { |
| 462 | return priority; |
| 463 | } |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 464 | } |