blob: d969da07bdf7e1adf1687fbd18c45aeaff2a5a6c [file] [log] [blame]
Jonathan Hart6e948282014-11-17 22:50:42 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Jonathan Hart6e948282014-11-17 22:50:42 -08003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Jonathan Hart6e948282014-11-17 22:50:42 -080017
18import org.onlab.packet.IPv4;
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +080019import org.onlab.packet.IPv6;
Jonathan Hart6e948282014-11-17 22:50:42 -080020
21/**
22 * Known protocol values for IP protocol field that can be supplied to the CLI.
23 */
24public enum IpProtocol {
25 /** ICMP. **/
26 ICMP(IPv4.PROTOCOL_ICMP),
27 /** TCP. **/
28 TCP(IPv4.PROTOCOL_TCP),
29 /** UDP. **/
Charles M.C. Chane9c8bbc2015-04-24 05:03:02 +080030 UDP(IPv4.PROTOCOL_UDP),
31 /** ICMP6. **/
32 ICMP6(IPv6.PROTOCOL_ICMP6);
Jonathan Hart6e948282014-11-17 22:50:42 -080033
34 private short value;
35
36 /**
37 * Constructs an IpProtocol with the given value.
38 *
39 * @param value value to use when this IpProtocol is seen
40 */
41 private IpProtocol(short value) {
42 this.value = value;
43 }
44
45 /**
46 * Gets the value to use for this IpProtocol.
47 *
48 * @return short value to use for this IpProtocol
49 */
50 public short value() {
51 return this.value;
52 }
53
54 /**
55 * Parse a string input that could contain an IpProtocol value. The value
56 * may appear in the string either as a known protocol name (one of the
57 * values of this enum), or a numeric protocol value.
58 *
59 * @param input the input string to parse
60 * @return the numeric value of the parsed IP protocol
61 * @throws IllegalArgumentException if the input string does not contain a
62 * value that can be parsed into an IP protocol
63 */
64 public static short parseFromString(String input) {
65 try {
66 return valueOf(input).value();
67 } catch (IllegalArgumentException e) {
68 // The input is not a known IP protocol name, let's see if it's an IP
69 // protocol value (byte). We parse with Short to handle unsigned values
70 // correctly.
71 try {
72 return Short.parseShort(input);
73 } catch (NumberFormatException e1) {
74 throw new IllegalArgumentException(
75 "IpProtocol value must be either a string protocol name"
76 + " or an 8-bit protocol value");
77 }
78 }
79 }
80}