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