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