Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 17 | |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Strings.isNullOrEmpty; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 19 | import static org.onosproject.net.flow.DefaultTrafficTreatment.builder; |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 20 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.Link; |
| 27 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Brian O'Connor | 4f5a98a | 2015-03-14 19:50:09 -0700 | [diff] [blame^] | 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.TrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficTreatment; |
| 31 | import org.onosproject.net.intent.Constraint; |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 32 | import org.onosproject.net.intent.Intent; |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 35 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
| 36 | import org.onosproject.net.intent.constraint.LinkTypeConstraint; |
| 37 | import org.onosproject.net.resource.Bandwidth; |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 38 | import org.onlab.packet.IpPrefix; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 39 | import org.onlab.packet.MacAddress; |
| 40 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 41 | /** |
| 42 | * Base class for command line operations for connectivity based intents. |
| 43 | */ |
| 44 | public abstract class ConnectivityIntentCommand extends AbstractShellCommand { |
| 45 | |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 46 | // Selectors |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 47 | @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address", |
| 48 | required = false, multiValued = false) |
| 49 | private String srcMacString = null; |
| 50 | |
| 51 | @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address", |
| 52 | required = false, multiValued = false) |
| 53 | private String dstMacString = null; |
| 54 | |
| 55 | @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type", |
| 56 | required = false, multiValued = false) |
Ray Milkey | 04c7832 | 2015-01-21 14:57:58 -0800 | [diff] [blame] | 57 | private String ethTypeString = null; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 58 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 59 | @Option(name = "--ipProto", description = "IP Protocol", |
| 60 | required = false, multiValued = false) |
| 61 | private String ipProtoString = null; |
| 62 | |
| 63 | @Option(name = "--ipSrc", description = "Source IP Address", |
| 64 | required = false, multiValued = false) |
| 65 | private String srcIpString = null; |
| 66 | |
| 67 | @Option(name = "--ipDst", description = "Destination IP Address", |
| 68 | required = false, multiValued = false) |
| 69 | private String dstIpString = null; |
| 70 | |
| 71 | @Option(name = "--tcpSrc", description = "Source TCP Port", |
| 72 | required = false, multiValued = false) |
| 73 | private String srcTcpString = null; |
| 74 | |
| 75 | @Option(name = "--tcpDst", description = "Destination TCP Port", |
| 76 | required = false, multiValued = false) |
| 77 | private String dstTcpString = null; |
| 78 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 79 | @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth", |
| 80 | required = false, multiValued = false) |
Ray Milkey | 04c7832 | 2015-01-21 14:57:58 -0800 | [diff] [blame] | 81 | private String bandwidthString = null; |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 82 | |
| 83 | @Option(name = "-l", aliases = "--lambda", description = "Lambda", |
| 84 | required = false, multiValued = false) |
| 85 | private boolean lambda = false; |
| 86 | |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 87 | @Option(name = "-k", aliases = "--key", description = "Intent Key", |
| 88 | required = false, multiValued = false) |
| 89 | private String intentKey = null; |
| 90 | |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 91 | |
| 92 | // Treatments |
| 93 | @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address", |
| 94 | required = false, multiValued = false) |
| 95 | private String setEthSrcString = null; |
| 96 | |
| 97 | @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address", |
| 98 | required = false, multiValued = false) |
| 99 | private String setEthDstString = null; |
| 100 | |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 101 | // Priorities |
| 102 | @Option(name = "-p", aliases = "--priority", description = "Priority", |
| 103 | required = false, multiValued = false) |
| 104 | private int priority = Intent.DEFAULT_INTENT_PRIORITY; |
| 105 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 106 | /** |
| 107 | * Constructs a traffic selector based on the command line arguments |
| 108 | * presented to the command. |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 109 | * @return traffic selector |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 110 | */ |
| 111 | protected TrafficSelector buildTrafficSelector() { |
| 112 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 113 | short ethType = EthType.IPV4.value(); |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 114 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 115 | if (!isNullOrEmpty(ethTypeString)) { |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 116 | ethType = EthType.parseFromString(ethTypeString); |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 117 | } |
| 118 | selectorBuilder.matchEthType(ethType); |
| 119 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 120 | if (!isNullOrEmpty(srcMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 121 | selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString)); |
| 122 | } |
| 123 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 124 | if (!isNullOrEmpty(dstMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 125 | selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString)); |
| 126 | } |
| 127 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 128 | if (!isNullOrEmpty(ipProtoString)) { |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 129 | short ipProtoShort = IpProtocol.parseFromString(ipProtoString); |
| 130 | selectorBuilder.matchIPProtocol((byte) ipProtoShort); |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (!isNullOrEmpty(srcIpString)) { |
| 134 | selectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIpString)); |
| 135 | } |
| 136 | |
| 137 | if (!isNullOrEmpty(dstIpString)) { |
| 138 | selectorBuilder.matchIPDst(IpPrefix.valueOf(dstIpString)); |
| 139 | } |
| 140 | |
| 141 | if (!isNullOrEmpty(srcTcpString)) { |
| 142 | selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString)); |
| 143 | } |
| 144 | |
| 145 | if (!isNullOrEmpty(dstTcpString)) { |
Jonathan Hart | 5dfa43f | 2014-11-17 15:35:54 -0800 | [diff] [blame] | 146 | selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString)); |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 149 | return selectorBuilder.build(); |
| 150 | } |
| 151 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 152 | /** |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 153 | * Generates a traffic treatment for this intent based on command line |
| 154 | * arguments presented to the command. |
| 155 | * |
| 156 | * @return traffic treatment |
| 157 | */ |
| 158 | protected TrafficTreatment buildTrafficTreatment() { |
Brian O'Connor | 4f5a98a | 2015-03-14 19:50:09 -0700 | [diff] [blame^] | 159 | boolean hasEthSrc = !isNullOrEmpty(setEthSrcString); |
| 160 | boolean hasEthDst = !isNullOrEmpty(setEthDstString); |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 161 | |
Brian O'Connor | 4f5a98a | 2015-03-14 19:50:09 -0700 | [diff] [blame^] | 162 | if (!hasEthSrc && !hasEthDst) { |
| 163 | return DefaultTrafficTreatment.emptyTreatment(); |
| 164 | } |
| 165 | |
| 166 | final TrafficTreatment.Builder builder = builder(); |
| 167 | if (hasEthSrc) { |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 168 | final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString); |
| 169 | builder.setEthSrc(setEthSrc); |
| 170 | } |
Brian O'Connor | 4f5a98a | 2015-03-14 19:50:09 -0700 | [diff] [blame^] | 171 | if (hasEthDst) { |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame] | 172 | final MacAddress setEthDst = MacAddress.valueOf(setEthDstString); |
| 173 | builder.setEthDst(setEthDst); |
| 174 | } |
| 175 | return builder.build(); |
| 176 | } |
| 177 | |
| 178 | /** |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 179 | * Builds the constraint list for this command based on the command line |
| 180 | * parameters. |
| 181 | * |
| 182 | * @return List of constraint objects describing the constraints requested |
| 183 | */ |
| 184 | protected List<Constraint> buildConstraints() { |
| 185 | final List<Constraint> constraints = new LinkedList<>(); |
| 186 | |
| 187 | // Check for a bandwidth specification |
| 188 | if (!isNullOrEmpty(bandwidthString)) { |
| 189 | final double bandwidthValue = Double.parseDouble(bandwidthString); |
Sho SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 190 | constraints.add(new BandwidthConstraint(Bandwidth.bps(bandwidthValue))); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Check for a lambda specification |
| 194 | if (lambda) { |
| 195 | constraints.add(new LambdaConstraint(null)); |
| 196 | } |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 197 | constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL)); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 198 | |
| 199 | return constraints; |
| 200 | } |
Ray Milkey | 5b3717e | 2015-02-05 11:44:08 -0800 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * Creates a key for an intent based on command line arguments. If a key |
| 204 | * has been specified, it is returned. If no key is specified, null |
| 205 | * is returned. |
| 206 | * |
| 207 | * @return intent key if specified, null otherwise |
| 208 | */ |
| 209 | protected Key key() { |
| 210 | Key key = null; |
| 211 | if (intentKey != null) { |
| 212 | key = Key.of(intentKey, appId()); |
| 213 | } |
| 214 | return key; |
| 215 | } |
Ray Milkey | c24cde3 | 2015-03-10 18:20:18 -0700 | [diff] [blame] | 216 | |
| 217 | /** |
| 218 | * Gets the priority to use for the intent. |
| 219 | * |
| 220 | * @return priority |
| 221 | */ |
| 222 | protected int priority() { |
| 223 | return priority; |
| 224 | } |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 225 | } |