Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 17 | |
| 18 | import org.onlab.packet.IPv4; |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 19 | import org.onlab.packet.IPv6; |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Known protocol values for IP protocol field that can be supplied to the CLI. |
| 23 | */ |
| 24 | public enum IpProtocol { |
| 25 | /** ICMP. **/ |
| 26 | ICMP(IPv4.PROTOCOL_ICMP), |
| 27 | /** TCP. **/ |
| 28 | TCP(IPv4.PROTOCOL_TCP), |
| 29 | /** UDP. **/ |
Charles M.C. Chan | e9c8bbc | 2015-04-24 05:03:02 +0800 | [diff] [blame] | 30 | UDP(IPv4.PROTOCOL_UDP), |
| 31 | /** ICMP6. **/ |
| 32 | ICMP6(IPv6.PROTOCOL_ICMP6); |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 33 | |
| 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 | } |