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