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; |
| 30 | import org.onlab.packet.MacAddress; |
| 31 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 32 | import static com.google.common.base.Strings.isNullOrEmpty; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Base class for command line operations for connectivity based intents. |
| 36 | */ |
| 37 | public abstract class ConnectivityIntentCommand extends AbstractShellCommand { |
| 38 | |
| 39 | @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address", |
| 40 | required = false, multiValued = false) |
| 41 | private String srcMacString = null; |
| 42 | |
| 43 | @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address", |
| 44 | required = false, multiValued = false) |
| 45 | private String dstMacString = null; |
| 46 | |
| 47 | @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type", |
| 48 | required = false, multiValued = false) |
| 49 | private String ethTypeString = ""; |
| 50 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 51 | @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth", |
| 52 | required = false, multiValued = false) |
| 53 | private String bandwidthString = ""; |
| 54 | |
| 55 | @Option(name = "-l", aliases = "--lambda", description = "Lambda", |
| 56 | required = false, multiValued = false) |
| 57 | private boolean lambda = false; |
| 58 | |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 59 | /** |
| 60 | * Constructs a traffic selector based on the command line arguments |
| 61 | * presented to the command. |
Yuta HIGUCHI | 5c94727 | 2014-11-03 21:39:21 -0800 | [diff] [blame] | 62 | * @return traffic selector |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 63 | */ |
| 64 | protected TrafficSelector buildTrafficSelector() { |
| 65 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 66 | Short ethType = Ethernet.TYPE_IPV4; |
Ray Milkey | caa450b | 2014-10-29 15:54:24 -0700 | [diff] [blame] | 67 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 68 | if (!isNullOrEmpty(ethTypeString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 69 | EthType ethTypeParameter = EthType.valueOf(ethTypeString); |
| 70 | ethType = ethTypeParameter.value(); |
| 71 | } |
| 72 | selectorBuilder.matchEthType(ethType); |
| 73 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 74 | if (!isNullOrEmpty(srcMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 75 | selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString)); |
| 76 | } |
| 77 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 78 | if (!isNullOrEmpty(dstMacString)) { |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 79 | selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString)); |
| 80 | } |
| 81 | |
| 82 | return selectorBuilder.build(); |
| 83 | } |
| 84 | |
Ray Milkey | 460f402 | 2014-11-05 15:41:43 -0800 | [diff] [blame^] | 85 | /** |
| 86 | * Builds the constraint list for this command based on the command line |
| 87 | * parameters. |
| 88 | * |
| 89 | * @return List of constraint objects describing the constraints requested |
| 90 | */ |
| 91 | protected List<Constraint> buildConstraints() { |
| 92 | final List<Constraint> constraints = new LinkedList<>(); |
| 93 | |
| 94 | // Check for a bandwidth specification |
| 95 | if (!isNullOrEmpty(bandwidthString)) { |
| 96 | final double bandwidthValue = Double.parseDouble(bandwidthString); |
| 97 | constraints.add(new BandwidthConstraint(Bandwidth.valueOf(bandwidthValue))); |
| 98 | } |
| 99 | |
| 100 | // Check for a lambda specification |
| 101 | if (lambda) { |
| 102 | constraints.add(new LambdaConstraint(null)); |
| 103 | } |
| 104 | |
| 105 | return constraints; |
| 106 | } |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 107 | } |