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