blob: 5c513e3e7ee0af6e0ec7d4b4a89ed8c9e8053457 [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;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080030import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070031import org.onlab.packet.MacAddress;
32
Ray Milkey460f4022014-11-05 15:41:43 -080033import static com.google.common.base.Strings.isNullOrEmpty;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070034
35/**
36 * Base class for command line operations for connectivity based intents.
37 */
38public 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 Hart5f7097e2014-11-11 11:50:28 -080052 @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 Milkey460f4022014-11-05 15:41:43 -080072 @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 Milkey9fdf1ea2014-10-21 09:48:02 -070080 /**
81 * Constructs a traffic selector based on the command line arguments
82 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080083 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070084 */
85 protected TrafficSelector buildTrafficSelector() {
86 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070087 Short ethType = Ethernet.TYPE_IPV4;
Ray Milkeycaa450b2014-10-29 15:54:24 -070088
Ray Milkey460f4022014-11-05 15:41:43 -080089 if (!isNullOrEmpty(ethTypeString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070090 EthType ethTypeParameter = EthType.valueOf(ethTypeString);
91 ethType = ethTypeParameter.value();
92 }
93 selectorBuilder.matchEthType(ethType);
94
Ray Milkey460f4022014-11-05 15:41:43 -080095 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070096 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
97 }
98
Ray Milkey460f4022014-11-05 15:41:43 -080099 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700100 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
101 }
102
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800103 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 Milkey9fdf1ea2014-10-21 09:48:02 -0700123 return selectorBuilder.build();
124 }
125
Ray Milkey460f4022014-11-05 15:41:43 -0800126 /**
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 Milkey9fdf1ea2014-10-21 09:48:02 -0700148}