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