blob: add9cd364316c64f6d65ede8fc95c32a5e4288d4 [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;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080023import org.onlab.onos.net.Link;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070024import org.onlab.onos.net.flow.DefaultTrafficSelector;
25import org.onlab.onos.net.flow.TrafficSelector;
Jonathan Hart2c25e362014-11-17 18:14:19 -080026import org.onlab.onos.net.flow.TrafficTreatment;
Ray Milkey460f4022014-11-05 15:41:43 -080027import org.onlab.onos.net.intent.Constraint;
28import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
29import org.onlab.onos.net.intent.constraint.LambdaConstraint;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080030import org.onlab.onos.net.intent.constraint.LinkTypeConstraint;
Ray Milkey460f4022014-11-05 15:41:43 -080031import org.onlab.onos.net.resource.Bandwidth;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070032import org.onlab.packet.Ethernet;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080033import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070034import org.onlab.packet.MacAddress;
35
Ray Milkey460f4022014-11-05 15:41:43 -080036import static com.google.common.base.Strings.isNullOrEmpty;
Jonathan Hart2c25e362014-11-17 18:14:19 -080037import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070038
39/**
40 * Base class for command line operations for connectivity based intents.
41 */
42public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
43
Jonathan Hart2c25e362014-11-17 18:14:19 -080044 // Selectors
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070045 @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address",
46 required = false, multiValued = false)
47 private String srcMacString = null;
48
49 @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address",
50 required = false, multiValued = false)
51 private String dstMacString = null;
52
53 @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type",
54 required = false, multiValued = false)
55 private String ethTypeString = "";
56
Jonathan Hart5f7097e2014-11-11 11:50:28 -080057 @Option(name = "--ipProto", description = "IP Protocol",
58 required = false, multiValued = false)
59 private String ipProtoString = null;
60
61 @Option(name = "--ipSrc", description = "Source IP Address",
62 required = false, multiValued = false)
63 private String srcIpString = null;
64
65 @Option(name = "--ipDst", description = "Destination IP Address",
66 required = false, multiValued = false)
67 private String dstIpString = null;
68
69 @Option(name = "--tcpSrc", description = "Source TCP Port",
70 required = false, multiValued = false)
71 private String srcTcpString = null;
72
73 @Option(name = "--tcpDst", description = "Destination TCP Port",
74 required = false, multiValued = false)
75 private String dstTcpString = null;
76
Ray Milkey460f4022014-11-05 15:41:43 -080077 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
78 required = false, multiValued = false)
79 private String bandwidthString = "";
80
81 @Option(name = "-l", aliases = "--lambda", description = "Lambda",
82 required = false, multiValued = false)
83 private boolean lambda = false;
84
Jonathan Hart2c25e362014-11-17 18:14:19 -080085
86 // Treatments
87 @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address",
88 required = false, multiValued = false)
89 private String setEthSrcString = null;
90
91 @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address",
92 required = false, multiValued = false)
93 private String setEthDstString = null;
94
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070095 /**
96 * Constructs a traffic selector based on the command line arguments
97 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080098 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070099 */
100 protected TrafficSelector buildTrafficSelector() {
101 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700102 Short ethType = Ethernet.TYPE_IPV4;
Ray Milkeycaa450b2014-10-29 15:54:24 -0700103
Ray Milkey460f4022014-11-05 15:41:43 -0800104 if (!isNullOrEmpty(ethTypeString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700105 EthType ethTypeParameter = EthType.valueOf(ethTypeString);
106 ethType = ethTypeParameter.value();
107 }
108 selectorBuilder.matchEthType(ethType);
109
Ray Milkey460f4022014-11-05 15:41:43 -0800110 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700111 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
112 }
113
Ray Milkey460f4022014-11-05 15:41:43 -0800114 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700115 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
116 }
117
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800118 if (!isNullOrEmpty(ipProtoString)) {
119 selectorBuilder.matchIPProtocol((byte) Short.parseShort(ipProtoString));
120 }
121
122 if (!isNullOrEmpty(srcIpString)) {
123 selectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIpString));
124 }
125
126 if (!isNullOrEmpty(dstIpString)) {
127 selectorBuilder.matchIPDst(IpPrefix.valueOf(dstIpString));
128 }
129
130 if (!isNullOrEmpty(srcTcpString)) {
131 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
132 }
133
134 if (!isNullOrEmpty(dstTcpString)) {
Jonathan Hart5dfa43f2014-11-17 15:35:54 -0800135 selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString));
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800136 }
137
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700138 return selectorBuilder.build();
139 }
140
Ray Milkey460f4022014-11-05 15:41:43 -0800141 /**
Jonathan Hart2c25e362014-11-17 18:14:19 -0800142 * Generates a traffic treatment for this intent based on command line
143 * arguments presented to the command.
144 *
145 * @return traffic treatment
146 */
147 protected TrafficTreatment buildTrafficTreatment() {
148 final TrafficTreatment.Builder builder = builder();
149
150 if (!isNullOrEmpty(setEthSrcString)) {
151 final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString);
152 builder.setEthSrc(setEthSrc);
153 }
154 if (!isNullOrEmpty(setEthDstString)) {
155 final MacAddress setEthDst = MacAddress.valueOf(setEthDstString);
156 builder.setEthDst(setEthDst);
157 }
158 return builder.build();
159 }
160
161 /**
Ray Milkey460f4022014-11-05 15:41:43 -0800162 * Builds the constraint list for this command based on the command line
163 * parameters.
164 *
165 * @return List of constraint objects describing the constraints requested
166 */
167 protected List<Constraint> buildConstraints() {
168 final List<Constraint> constraints = new LinkedList<>();
169
170 // Check for a bandwidth specification
171 if (!isNullOrEmpty(bandwidthString)) {
172 final double bandwidthValue = Double.parseDouble(bandwidthString);
173 constraints.add(new BandwidthConstraint(Bandwidth.valueOf(bandwidthValue)));
174 }
175
176 // Check for a lambda specification
177 if (lambda) {
178 constraints.add(new LambdaConstraint(null));
179 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800180 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
Ray Milkey460f4022014-11-05 15:41:43 -0800181
182 return constraints;
183 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700184}