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 | */ |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 16 | package org.onlab.onos.cli.net; |
| 17 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 18 | import java.util.LinkedList; |
| 19 | import java.util.List; |
| 20 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.commands.Option; |
| 22 | import org.onlab.onos.cli.AbstractShellCommand; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 23 | import org.onlab.onos.net.Link; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 24 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 25 | import org.onlab.onos.net.flow.TrafficSelector; |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame^] | 26 | import org.onlab.onos.net.flow.TrafficTreatment; |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 27 | import org.onlab.onos.net.intent.Constraint; |
| 28 | import org.onlab.onos.net.intent.constraint.BandwidthConstraint; |
| 29 | import org.onlab.onos.net.intent.constraint.LambdaConstraint; |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 30 | import org.onlab.onos.net.intent.constraint.LinkTypeConstraint; |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 31 | import org.onlab.onos.net.resource.Bandwidth; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 32 | import org.onlab.packet.Ethernet; |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 33 | import org.onlab.packet.IpPrefix; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 34 | import org.onlab.packet.MacAddress; |
| 35 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 36 | import static com.google.common.base.Strings.isNullOrEmpty; |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame^] | 37 | import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 38 | |
| 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) |
| 55 | private String ethTypeString = ""; |
| 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) |
| 79 | private String bandwidthString = ""; |
| 80 | |
| 81 | @Option(name = "-l", aliases = "--lambda", description = "Lambda", |
| 82 | required = false, multiValued = false) |
| 83 | private boolean lambda = false; |
| 84 | |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame^] | 85 | |
| 86 | // Treatments |
| 87 | @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address", |
| 88 | required = false, multiValued = false) |
| 89 | private String setEthSrcString = null; |
| 90 | |
| 91 | @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address", |
| 92 | required = false, multiValued = false) |
| 93 | private String setEthDstString = null; |
| 94 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 95 | /** |
| 96 | * Constructs a traffic selector based on the command line arguments |
| 97 | * presented to the command. |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 98 | * @return traffic selector |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 99 | */ |
| 100 | protected TrafficSelector buildTrafficSelector() { |
| 101 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 102 | Short ethType = Ethernet.TYPE_IPV4; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 103 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 104 | if (!isNullOrEmpty(ethTypeString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 105 | EthType ethTypeParameter = EthType.valueOf(ethTypeString); |
| 106 | ethType = ethTypeParameter.value(); |
| 107 | } |
| 108 | selectorBuilder.matchEthType(ethType); |
| 109 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 110 | if (!isNullOrEmpty(srcMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 111 | selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString)); |
| 112 | } |
| 113 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 114 | if (!isNullOrEmpty(dstMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 115 | selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString)); |
| 116 | } |
| 117 | |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 118 | if (!isNullOrEmpty(ipProtoString)) { |
| 119 | selectorBuilder.matchIPProtocol((byte) Short.parseShort(ipProtoString)); |
| 120 | } |
| 121 | |
| 122 | if (!isNullOrEmpty(srcIpString)) { |
| 123 | selectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIpString)); |
| 124 | } |
| 125 | |
| 126 | if (!isNullOrEmpty(dstIpString)) { |
| 127 | selectorBuilder.matchIPDst(IpPrefix.valueOf(dstIpString)); |
| 128 | } |
| 129 | |
| 130 | if (!isNullOrEmpty(srcTcpString)) { |
| 131 | selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString)); |
| 132 | } |
| 133 | |
| 134 | if (!isNullOrEmpty(dstTcpString)) { |
Jonathan Hart | 5dfa43f | 2014-11-17 15:35:54 -0800 | [diff] [blame] | 135 | selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString)); |
Jonathan Hart | 5f7097e | 2014-11-11 11:50:28 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 138 | return selectorBuilder.build(); |
| 139 | } |
| 140 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 141 | /** |
Jonathan Hart | 2c25e36 | 2014-11-17 18:14:19 -0800 | [diff] [blame^] | 142 | * Generates a traffic treatment for this intent based on command line |
| 143 | * arguments presented to the command. |
| 144 | * |
| 145 | * @return traffic treatment |
| 146 | */ |
| 147 | protected TrafficTreatment buildTrafficTreatment() { |
| 148 | final TrafficTreatment.Builder builder = builder(); |
| 149 | |
| 150 | if (!isNullOrEmpty(setEthSrcString)) { |
| 151 | final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString); |
| 152 | builder.setEthSrc(setEthSrc); |
| 153 | } |
| 154 | if (!isNullOrEmpty(setEthDstString)) { |
| 155 | final MacAddress setEthDst = MacAddress.valueOf(setEthDstString); |
| 156 | builder.setEthDst(setEthDst); |
| 157 | } |
| 158 | return builder.build(); |
| 159 | } |
| 160 | |
| 161 | /** |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 162 | * Builds the constraint list for this command based on the command line |
| 163 | * parameters. |
| 164 | * |
| 165 | * @return List of constraint objects describing the constraints requested |
| 166 | */ |
| 167 | protected List<Constraint> buildConstraints() { |
| 168 | final List<Constraint> constraints = new LinkedList<>(); |
| 169 | |
| 170 | // Check for a bandwidth specification |
| 171 | if (!isNullOrEmpty(bandwidthString)) { |
| 172 | final double bandwidthValue = Double.parseDouble(bandwidthString); |
| 173 | constraints.add(new BandwidthConstraint(Bandwidth.valueOf(bandwidthValue))); |
| 174 | } |
| 175 | |
| 176 | // Check for a lambda specification |
| 177 | if (lambda) { |
| 178 | constraints.add(new LambdaConstraint(null)); |
| 179 | } |
Thomas Vachuska | dea45ff | 2014-11-12 18:35:46 -0800 | [diff] [blame] | 180 | constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL)); |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame] | 181 | |
| 182 | return constraints; |
| 183 | } |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 184 | } |