Jonathan Hart

Added CLI completion for IP protocol types.

Also modified IpProto and EthType field parsing to allow the user to supply
either a string value (e.g. "ICMP", "ARP") or the protocol number.

Change-Id: I8f19bebe53c2a7dbdc7570fdc08f979b2c0851cb
...@@ -15,6 +15,9 @@ ...@@ -15,6 +15,9 @@
15 */ 15 */
16 package org.onlab.onos.cli.net; 16 package org.onlab.onos.cli.net;
17 17
18 +import static com.google.common.base.Strings.isNullOrEmpty;
19 +import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
20 +
18 import java.util.LinkedList; 21 import java.util.LinkedList;
19 import java.util.List; 22 import java.util.List;
20 23
...@@ -29,13 +32,9 @@ import org.onlab.onos.net.intent.constraint.BandwidthConstraint; ...@@ -29,13 +32,9 @@ import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
29 import org.onlab.onos.net.intent.constraint.LambdaConstraint; 32 import org.onlab.onos.net.intent.constraint.LambdaConstraint;
30 import org.onlab.onos.net.intent.constraint.LinkTypeConstraint; 33 import org.onlab.onos.net.intent.constraint.LinkTypeConstraint;
31 import org.onlab.onos.net.resource.Bandwidth; 34 import org.onlab.onos.net.resource.Bandwidth;
32 -import org.onlab.packet.Ethernet;
33 import org.onlab.packet.IpPrefix; 35 import org.onlab.packet.IpPrefix;
34 import org.onlab.packet.MacAddress; 36 import org.onlab.packet.MacAddress;
35 37
36 -import static com.google.common.base.Strings.isNullOrEmpty;
37 -import static org.onlab.onos.net.flow.DefaultTrafficTreatment.builder;
38 -
39 /** 38 /**
40 * Base class for command line operations for connectivity based intents. 39 * Base class for command line operations for connectivity based intents.
41 */ 40 */
...@@ -99,11 +98,10 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand { ...@@ -99,11 +98,10 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
99 */ 98 */
100 protected TrafficSelector buildTrafficSelector() { 99 protected TrafficSelector buildTrafficSelector() {
101 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); 100 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder();
102 - Short ethType = Ethernet.TYPE_IPV4; 101 + short ethType = EthType.IPV4.value();
103 102
104 if (!isNullOrEmpty(ethTypeString)) { 103 if (!isNullOrEmpty(ethTypeString)) {
105 - EthType ethTypeParameter = EthType.valueOf(ethTypeString); 104 + ethType = EthType.parseFromString(ethTypeString);
106 - ethType = ethTypeParameter.value();
107 } 105 }
108 selectorBuilder.matchEthType(ethType); 106 selectorBuilder.matchEthType(ethType);
109 107
...@@ -116,7 +114,8 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand { ...@@ -116,7 +114,8 @@ public abstract class ConnectivityIntentCommand extends AbstractShellCommand {
116 } 114 }
117 115
118 if (!isNullOrEmpty(ipProtoString)) { 116 if (!isNullOrEmpty(ipProtoString)) {
119 - selectorBuilder.matchIPProtocol((byte) Short.parseShort(ipProtoString)); 117 + short ipProtoShort = IpProtocol.parseFromString(ipProtoString);
118 + selectorBuilder.matchIPProtocol((byte) ipProtoShort);
120 } 119 }
121 120
122 if (!isNullOrEmpty(srcIpString)) { 121 if (!isNullOrEmpty(srcIpString)) {
......
...@@ -38,7 +38,7 @@ public enum EthType { ...@@ -38,7 +38,7 @@ public enum EthType {
38 /** 38 /**
39 * Constructs an EthType with the given value. 39 * Constructs an EthType with the given value.
40 * 40 *
41 - * @param value value to use when this EthType is seen. 41 + * @param value value to use when this EthType is seen
42 */ 42 */
43 private EthType(short value) { 43 private EthType(short value) {
44 this.value = value; 44 this.value = value;
...@@ -52,4 +52,31 @@ public enum EthType { ...@@ -52,4 +52,31 @@ public enum EthType {
52 public short value() { 52 public short value() {
53 return this.value; 53 return this.value;
54 } 54 }
55 +
56 + /**
57 + * Parse a string input that could contain an EthType value. The value
58 + * may appear in the string either as a known protocol name (one of the
59 + * values of this enum), or a numeric protocol value.
60 + *
61 + * @param input the input string to parse
62 + * @return the numeric value of the parsed Ethernet type
63 + * @throws IllegalArgumentException if the input string does not contain a
64 + * value that can be parsed into an Ethernet type
65 + */
66 + public static short parseFromString(String input) {
67 + try {
68 + return valueOf(input).value();
69 + } catch (IllegalArgumentException e) {
70 + // The input is not a known Ethernet type name, let's see if it's an
71 + // Ethernet type value (short). We parse with Integer to handle
72 + // unsigned values correctly.
73 + try {
74 + return (short) Integer.parseInt(input);
75 + } catch (NumberFormatException e1) {
76 + throw new IllegalArgumentException(
77 + "EthType value must be either a string protocol name"
78 + + " or a 16-bit protocol value");
79 + }
80 + }
81 + }
55 } 82 }
......
...@@ -30,11 +30,10 @@ public class EthTypeCompleter implements Completer { ...@@ -30,11 +30,10 @@ public class EthTypeCompleter implements Completer {
30 // Delegate string completer 30 // Delegate string completer
31 StringsCompleter delegate = new StringsCompleter(); 31 StringsCompleter delegate = new StringsCompleter();
32 SortedSet<String> strings = delegate.getStrings(); 32 SortedSet<String> strings = delegate.getStrings();
33 - strings.add(EthType.ARP.toString()); 33 +
34 - strings.add(EthType.BSN.toString()); 34 + for (EthType eth : EthType.values()) {
35 - strings.add(EthType.IPV4.toString()); 35 + strings.add(eth.toString());
36 - strings.add(EthType.LLDP.toString()); 36 + }
37 - strings.add(EthType.RARP.toString());
38 37
39 // Now let the completer do the work for figuring out what to offer. 38 // Now let the completer do the work for figuring out what to offer.
40 return delegate.complete(buffer, cursor, candidates); 39 return delegate.complete(buffer, cursor, candidates);
......
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * 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
7 + *
8 + * 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.
15 + */
16 +package org.onlab.onos.cli.net;
17 +
18 +import org.onlab.packet.IPv4;
19 +
20 +/**
21 + * Known protocol values for IP protocol field that can be supplied to the CLI.
22 + */
23 +public enum IpProtocol {
24 + /** ICMP. **/
25 + ICMP(IPv4.PROTOCOL_ICMP),
26 + /** TCP. **/
27 + TCP(IPv4.PROTOCOL_TCP),
28 + /** UDP. **/
29 + UDP(IPv4.PROTOCOL_UDP);
30 +
31 + private short value;
32 +
33 + /**
34 + * Constructs an IpProtocol with the given value.
35 + *
36 + * @param value value to use when this IpProtocol is seen
37 + */
38 + private IpProtocol(short value) {
39 + this.value = value;
40 + }
41 +
42 + /**
43 + * Gets the value to use for this IpProtocol.
44 + *
45 + * @return short value to use for this IpProtocol
46 + */
47 + public short value() {
48 + return this.value;
49 + }
50 +
51 + /**
52 + * Parse a string input that could contain an IpProtocol value. The value
53 + * may appear in the string either as a known protocol name (one of the
54 + * values of this enum), or a numeric protocol value.
55 + *
56 + * @param input the input string to parse
57 + * @return the numeric value of the parsed IP protocol
58 + * @throws IllegalArgumentException if the input string does not contain a
59 + * value that can be parsed into an IP protocol
60 + */
61 + public static short parseFromString(String input) {
62 + try {
63 + return valueOf(input).value();
64 + } catch (IllegalArgumentException e) {
65 + // The input is not a known IP protocol name, let's see if it's an IP
66 + // protocol value (byte). We parse with Short to handle unsigned values
67 + // correctly.
68 + try {
69 + return Short.parseShort(input);
70 + } catch (NumberFormatException e1) {
71 + throw new IllegalArgumentException(
72 + "IpProtocol value must be either a string protocol name"
73 + + " or an 8-bit protocol value");
74 + }
75 + }
76 + }
77 +}
1 +/*
2 + * Copyright 2014 Open Networking Laboratory
3 + *
4 + * 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
7 + *
8 + * 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.
15 + */
16 +package org.onlab.onos.cli.net;
17 +
18 +import java.util.List;
19 +import java.util.SortedSet;
20 +
21 +import org.apache.karaf.shell.console.Completer;
22 +import org.apache.karaf.shell.console.completer.StringsCompleter;
23 +
24 +/**
25 + * IP protocol completer.
26 + */
27 +public class IpProtocolCompleter implements Completer {
28 + @Override
29 + public int complete(String buffer, int cursor, List<String> candidates) {
30 + // Delegate string completer
31 + StringsCompleter delegate = new StringsCompleter();
32 + SortedSet<String> strings = delegate.getStrings();
33 +
34 + for (IpProtocol ip : IpProtocol.values()) {
35 + strings.add(ip.toString());
36 + }
37 +
38 + // Now let the completer do the work for figuring out what to offer.
39 + return delegate.complete(buffer, cursor, candidates);
40 + }
41 +
42 +}
...@@ -133,6 +133,7 @@ ...@@ -133,6 +133,7 @@
133 </completers> 133 </completers>
134 <optional-completers> 134 <optional-completers>
135 <entry key="-t" value-ref="ethTypeCompleter"/> 135 <entry key="-t" value-ref="ethTypeCompleter"/>
136 + <entry key="--ipProto" value-ref="ipProtocolCompleter"/>
136 </optional-completers> 137 </optional-completers>
137 </command> 138 </command>
138 <command> 139 <command>
...@@ -156,6 +157,7 @@ ...@@ -156,6 +157,7 @@
156 </completers> 157 </completers>
157 <optional-completers> 158 <optional-completers>
158 <entry key="-t" value-ref="ethTypeCompleter"/> 159 <entry key="-t" value-ref="ethTypeCompleter"/>
160 + <entry key="--ipProto" value-ref="ipProtocolCompleter"/>
159 </optional-completers> 161 </optional-completers>
160 </command> 162 </command>
161 <command> 163 <command>
...@@ -236,5 +238,6 @@ ...@@ -236,5 +238,6 @@
236 <bean id="connectPointCompleter" class="org.onlab.onos.cli.net.ConnectPointCompleter"/> 238 <bean id="connectPointCompleter" class="org.onlab.onos.cli.net.ConnectPointCompleter"/>
237 <bean id="nullCompleter" class="org.apache.karaf.shell.console.completer.NullCompleter"/> 239 <bean id="nullCompleter" class="org.apache.karaf.shell.console.completer.NullCompleter"/>
238 <bean id="ethTypeCompleter" class="org.onlab.onos.cli.net.EthTypeCompleter"/> 240 <bean id="ethTypeCompleter" class="org.onlab.onos.cli.net.EthTypeCompleter"/>
241 + <bean id="ipProtocolCompleter" class="org.onlab.onos.cli.net.IpProtocolCompleter"/>
239 242
240 </blueprint> 243 </blueprint>
......