blob: 4be169bd103e3b8b1f317822078602edc75960b3 [file] [log] [blame]
Jonathan Hart6e948282014-11-17 22:50:42 -08001/*
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 */
16package org.onlab.onos.cli.net;
17
18import org.onlab.packet.IPv4;
19
20/**
21 * Known protocol values for IP protocol field that can be supplied to the CLI.
22 */
23public 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}