blob: 3b28bb060072140480833fe92588da2ed5fe6ab7 [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
Jonathan Hart6e948282014-11-17 22:50:42 -080018import static com.google.common.base.Strings.isNullOrEmpty;
19import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
20
Ray Milkey460f4022014-11-05 15:41:43 -080021import java.util.LinkedList;
22import java.util.List;
23
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070024import org.apache.karaf.shell.commands.Option;
25import org.onlab.onos.cli.AbstractShellCommand;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080026import org.onlab.onos.net.Link;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070027import org.onlab.onos.net.flow.DefaultTrafficSelector;
28import org.onlab.onos.net.flow.TrafficSelector;
Jonathan Hart2c25e362014-11-17 18:14:19 -080029import org.onlab.onos.net.flow.TrafficTreatment;
Ray Milkey460f4022014-11-05 15:41:43 -080030import org.onlab.onos.net.intent.Constraint;
31import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
32import org.onlab.onos.net.intent.constraint.LambdaConstraint;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080033import org.onlab.onos.net.intent.constraint.LinkTypeConstraint;
Ray Milkey460f4022014-11-05 15:41:43 -080034import org.onlab.onos.net.resource.Bandwidth;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080035import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070036import org.onlab.packet.MacAddress;
37
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070038/**
39 * Base class for command line operations for connectivity based intents.
40 */
41public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
42
Jonathan Hart2c25e362014-11-17 18:14:19 -080043 // Selectors
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070044 @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address",
45 required = false, multiValued = false)
46 private String srcMacString = null;
47
48 @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address",
49 required = false, multiValued = false)
50 private String dstMacString = null;
51
52 @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type",
53 required = false, multiValued = false)
54 private String ethTypeString = "";
55
Jonathan Hart5f7097e2014-11-11 11:50:28 -080056 @Option(name = "--ipProto", description = "IP Protocol",
57 required = false, multiValued = false)
58 private String ipProtoString = null;
59
60 @Option(name = "--ipSrc", description = "Source IP Address",
61 required = false, multiValued = false)
62 private String srcIpString = null;
63
64 @Option(name = "--ipDst", description = "Destination IP Address",
65 required = false, multiValued = false)
66 private String dstIpString = null;
67
68 @Option(name = "--tcpSrc", description = "Source TCP Port",
69 required = false, multiValued = false)
70 private String srcTcpString = null;
71
72 @Option(name = "--tcpDst", description = "Destination TCP Port",
73 required = false, multiValued = false)
74 private String dstTcpString = null;
75
Ray Milkey460f4022014-11-05 15:41:43 -080076 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
77 required = false, multiValued = false)
78 private String bandwidthString = "";
79
80 @Option(name = "-l", aliases = "--lambda", description = "Lambda",
81 required = false, multiValued = false)
82 private boolean lambda = false;
83
Jonathan Hart2c25e362014-11-17 18:14:19 -080084
85 // Treatments
86 @Option(name = "--setEthSrc", description = "Rewrite Source MAC Address",
87 required = false, multiValued = false)
88 private String setEthSrcString = null;
89
90 @Option(name = "--setEthDst", description = "Rewrite Destination MAC Address",
91 required = false, multiValued = false)
92 private String setEthDstString = null;
93
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070094 /**
95 * Constructs a traffic selector based on the command line arguments
96 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080097 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070098 */
99 protected TrafficSelector buildTrafficSelector() {
100 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Jonathan Hart6e948282014-11-17 22:50:42 -0800101 short ethType = EthType.IPV4.value();
Ray Milkeycaa450b2014-10-29 15:54:24 -0700102
Ray Milkey460f4022014-11-05 15:41:43 -0800103 if (!isNullOrEmpty(ethTypeString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800104 ethType = EthType.parseFromString(ethTypeString);
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700105 }
106 selectorBuilder.matchEthType(ethType);
107
Ray Milkey460f4022014-11-05 15:41:43 -0800108 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700109 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
110 }
111
Ray Milkey460f4022014-11-05 15:41:43 -0800112 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700113 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
114 }
115
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800116 if (!isNullOrEmpty(ipProtoString)) {
Jonathan Hart6e948282014-11-17 22:50:42 -0800117 short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
118 selectorBuilder.matchIPProtocol((byte) ipProtoShort);
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800119 }
120
121 if (!isNullOrEmpty(srcIpString)) {
122 selectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIpString));
123 }
124
125 if (!isNullOrEmpty(dstIpString)) {
126 selectorBuilder.matchIPDst(IpPrefix.valueOf(dstIpString));
127 }
128
129 if (!isNullOrEmpty(srcTcpString)) {
130 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
131 }
132
133 if (!isNullOrEmpty(dstTcpString)) {
Jonathan Hart5dfa43f2014-11-17 15:35:54 -0800134 selectorBuilder.matchTcpDst((short) Integer.parseInt(dstTcpString));
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800135 }
136
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700137 return selectorBuilder.build();
138 }
139
Ray Milkey460f4022014-11-05 15:41:43 -0800140 /**
Jonathan Hart2c25e362014-11-17 18:14:19 -0800141 * Generates a traffic treatment for this intent based on command line
142 * arguments presented to the command.
143 *
144 * @return traffic treatment
145 */
146 protected TrafficTreatment buildTrafficTreatment() {
147 final TrafficTreatment.Builder builder = builder();
148
149 if (!isNullOrEmpty(setEthSrcString)) {
150 final MacAddress setEthSrc = MacAddress.valueOf(setEthSrcString);
151 builder.setEthSrc(setEthSrc);
152 }
153 if (!isNullOrEmpty(setEthDstString)) {
154 final MacAddress setEthDst = MacAddress.valueOf(setEthDstString);
155 builder.setEthDst(setEthDst);
156 }
157 return builder.build();
158 }
159
160 /**
Ray Milkey460f4022014-11-05 15:41:43 -0800161 * Builds the constraint list for this command based on the command line
162 * parameters.
163 *
164 * @return List of constraint objects describing the constraints requested
165 */
166 protected List<Constraint> buildConstraints() {
167 final List<Constraint> constraints = new LinkedList<>();
168
169 // Check for a bandwidth specification
170 if (!isNullOrEmpty(bandwidthString)) {
171 final double bandwidthValue = Double.parseDouble(bandwidthString);
172 constraints.add(new BandwidthConstraint(Bandwidth.valueOf(bandwidthValue)));
173 }
174
175 // Check for a lambda specification
176 if (lambda) {
177 constraints.add(new LambdaConstraint(null));
178 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800179 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
Ray Milkey460f4022014-11-05 15:41:43 -0800180
181 return constraints;
182 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700183}