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