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