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