blob: e4fc5aa6022270d9535f9f6c5e0291361d69ff51 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070016package org.onlab.onos.cli.net;
17
Ray Milkey460f4022014-11-05 15:41:43 -080018import java.util.LinkedList;
19import java.util.List;
20
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070021import org.apache.karaf.shell.commands.Option;
22import org.onlab.onos.cli.AbstractShellCommand;
23import org.onlab.onos.net.flow.DefaultTrafficSelector;
24import org.onlab.onos.net.flow.TrafficSelector;
Ray Milkey460f4022014-11-05 15:41:43 -080025import org.onlab.onos.net.intent.Constraint;
26import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
27import org.onlab.onos.net.intent.constraint.LambdaConstraint;
28import org.onlab.onos.net.resource.Bandwidth;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070029import org.onlab.packet.Ethernet;
30import org.onlab.packet.MacAddress;
31
Ray Milkey460f4022014-11-05 15:41:43 -080032import static com.google.common.base.Strings.isNullOrEmpty;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070033
34/**
35 * Base class for command line operations for connectivity based intents.
36 */
37public 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 Milkey460f4022014-11-05 15:41:43 -080051 @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 Milkey9fdf1ea2014-10-21 09:48:02 -070059 /**
60 * Constructs a traffic selector based on the command line arguments
61 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080062 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070063 */
64 protected TrafficSelector buildTrafficSelector() {
65 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070066 Short ethType = Ethernet.TYPE_IPV4;
Ray Milkeycaa450b2014-10-29 15:54:24 -070067
Ray Milkey460f4022014-11-05 15:41:43 -080068 if (!isNullOrEmpty(ethTypeString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070069 EthType ethTypeParameter = EthType.valueOf(ethTypeString);
70 ethType = ethTypeParameter.value();
71 }
72 selectorBuilder.matchEthType(ethType);
73
Ray Milkey460f4022014-11-05 15:41:43 -080074 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070075 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
76 }
77
Ray Milkey460f4022014-11-05 15:41:43 -080078 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070079 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
80 }
81
82 return selectorBuilder.build();
83 }
84
Ray Milkey460f4022014-11-05 15:41:43 -080085 /**
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 Milkey9fdf1ea2014-10-21 09:48:02 -0700107}