blob: 56d9de309d379e76a2fad055b701294e1d1dcc7c [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;
Ray Milkey460f4022014-11-05 15:41:43 -080026import org.onlab.onos.net.intent.Constraint;
27import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
28import org.onlab.onos.net.intent.constraint.LambdaConstraint;
Thomas Vachuskadea45ff2014-11-12 18:35:46 -080029import org.onlab.onos.net.intent.constraint.LinkTypeConstraint;
Ray Milkey460f4022014-11-05 15:41:43 -080030import org.onlab.onos.net.resource.Bandwidth;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070031import org.onlab.packet.Ethernet;
Jonathan Hart5f7097e2014-11-11 11:50:28 -080032import org.onlab.packet.IpPrefix;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070033import org.onlab.packet.MacAddress;
34
Ray Milkey460f4022014-11-05 15:41:43 -080035import static com.google.common.base.Strings.isNullOrEmpty;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070036
37/**
38 * Base class for command line operations for connectivity based intents.
39 */
40public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
41
42 @Option(name = "-s", aliases = "--ethSrc", description = "Source MAC Address",
43 required = false, multiValued = false)
44 private String srcMacString = null;
45
46 @Option(name = "-d", aliases = "--ethDst", description = "Destination MAC Address",
47 required = false, multiValued = false)
48 private String dstMacString = null;
49
50 @Option(name = "-t", aliases = "--ethType", description = "Ethernet Type",
51 required = false, multiValued = false)
52 private String ethTypeString = "";
53
Jonathan Hart5f7097e2014-11-11 11:50:28 -080054 @Option(name = "--ipProto", description = "IP Protocol",
55 required = false, multiValued = false)
56 private String ipProtoString = null;
57
58 @Option(name = "--ipSrc", description = "Source IP Address",
59 required = false, multiValued = false)
60 private String srcIpString = null;
61
62 @Option(name = "--ipDst", description = "Destination IP Address",
63 required = false, multiValued = false)
64 private String dstIpString = null;
65
66 @Option(name = "--tcpSrc", description = "Source TCP Port",
67 required = false, multiValued = false)
68 private String srcTcpString = null;
69
70 @Option(name = "--tcpDst", description = "Destination TCP Port",
71 required = false, multiValued = false)
72 private String dstTcpString = null;
73
Ray Milkey460f4022014-11-05 15:41:43 -080074 @Option(name = "-b", aliases = "--bandwidth", description = "Bandwidth",
75 required = false, multiValued = false)
76 private String bandwidthString = "";
77
78 @Option(name = "-l", aliases = "--lambda", description = "Lambda",
79 required = false, multiValued = false)
80 private boolean lambda = false;
81
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070082 /**
83 * Constructs a traffic selector based on the command line arguments
84 * presented to the command.
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080085 * @return traffic selector
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070086 */
87 protected TrafficSelector buildTrafficSelector() {
88 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070089 Short ethType = Ethernet.TYPE_IPV4;
Ray Milkeycaa450b2014-10-29 15:54:24 -070090
Ray Milkey460f4022014-11-05 15:41:43 -080091 if (!isNullOrEmpty(ethTypeString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070092 EthType ethTypeParameter = EthType.valueOf(ethTypeString);
93 ethType = ethTypeParameter.value();
94 }
95 selectorBuilder.matchEthType(ethType);
96
Ray Milkey460f4022014-11-05 15:41:43 -080097 if (!isNullOrEmpty(srcMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070098 selectorBuilder.matchEthSrc(MacAddress.valueOf(srcMacString));
99 }
100
Ray Milkey460f4022014-11-05 15:41:43 -0800101 if (!isNullOrEmpty(dstMacString)) {
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700102 selectorBuilder.matchEthDst(MacAddress.valueOf(dstMacString));
103 }
104
Jonathan Hart5f7097e2014-11-11 11:50:28 -0800105 if (!isNullOrEmpty(ipProtoString)) {
106 selectorBuilder.matchIPProtocol((byte) Short.parseShort(ipProtoString));
107 }
108
109 if (!isNullOrEmpty(srcIpString)) {
110 selectorBuilder.matchIPSrc(IpPrefix.valueOf(srcIpString));
111 }
112
113 if (!isNullOrEmpty(dstIpString)) {
114 selectorBuilder.matchIPDst(IpPrefix.valueOf(dstIpString));
115 }
116
117 if (!isNullOrEmpty(srcTcpString)) {
118 selectorBuilder.matchTcpSrc((short) Integer.parseInt(srcTcpString));
119 }
120
121 if (!isNullOrEmpty(dstTcpString)) {
122 selectorBuilder.matchTcpSrc((short) Integer.parseInt(dstTcpString));
123 }
124
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700125 return selectorBuilder.build();
126 }
127
Ray Milkey460f4022014-11-05 15:41:43 -0800128 /**
129 * Builds the constraint list for this command based on the command line
130 * parameters.
131 *
132 * @return List of constraint objects describing the constraints requested
133 */
134 protected List<Constraint> buildConstraints() {
135 final List<Constraint> constraints = new LinkedList<>();
136
137 // Check for a bandwidth specification
138 if (!isNullOrEmpty(bandwidthString)) {
139 final double bandwidthValue = Double.parseDouble(bandwidthString);
140 constraints.add(new BandwidthConstraint(Bandwidth.valueOf(bandwidthValue)));
141 }
142
143 // Check for a lambda specification
144 if (lambda) {
145 constraints.add(new LambdaConstraint(null));
146 }
Thomas Vachuskadea45ff2014-11-12 18:35:46 -0800147 constraints.add(new LinkTypeConstraint(lambda, Link.Type.OPTICAL));
Ray Milkey460f4022014-11-05 15:41:43 -0800148
149 return constraints;
150 }
Ray Milkey9fdf1ea2014-10-21 09:48:02 -0700151}