Charles M.C. Chan | 2184de1 | 2015-04-26 02:24:53 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Charles M.C. Chan | 2184de1 | 2015-04-26 02:24:53 +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 | */ |
| 16 | package org.onosproject.cli.net; |
| 17 | |
| 18 | import org.onlab.packet.ICMP6; |
| 19 | |
| 20 | /** |
| 21 | * Known values for ICMPv6 code field that can be supplied to the CLI. |
| 22 | */ |
| 23 | public enum Icmp6Code { |
| 24 | // Code for DEST_UNREACH |
| 25 | /** No route to destination. */ |
| 26 | NO_ROUTE(ICMP6.NO_ROUTE), |
| 27 | /** Communication with destination administratively prohibited. */ |
| 28 | COMM_PROHIBIT(ICMP6.COMM_PROHIBIT), |
| 29 | /** Beyond scope of source address. */ |
| 30 | BEYOND_SCOPE(ICMP6.BEYOND_SCOPE), |
| 31 | /** Address unreachable. */ |
| 32 | ADDR_UNREACH(ICMP6.ADDR_UNREACH), |
| 33 | /** Port unreachable. */ |
| 34 | PORT_UNREACH(ICMP6.PORT_UNREACH), |
| 35 | /** Source address failed ingress/egress policy. */ |
| 36 | FAIL_POLICY(ICMP6.FAIL_POLICY), |
| 37 | /** Reject route to destination. */ |
| 38 | REJECT_ROUTE(ICMP6.REJECT_ROUTE), |
| 39 | /** Error in Source Routing Header. */ |
| 40 | SRC_ROUTING_HEADER_ERR(ICMP6.SRC_ROUTING_HEADER_ERR), |
| 41 | |
| 42 | // Code for TIME_EXCEED |
| 43 | /** Hop limit exceeded in transit. */ |
| 44 | HOP_LIMIT_EXCEED(ICMP6.HOP_LIMIT_EXCEED), |
| 45 | /** Fragment reassembly time exceeded. */ |
| 46 | DEFRAG_TIME_EXCEED(ICMP6.DEFRAG_TIME_EXCEED), |
| 47 | |
| 48 | // Code for PARAM_ERR |
| 49 | /** Erroneous header field encountered. */ |
| 50 | HDR_FIELD_ERR(ICMP6.HDR_FIELD_ERR), |
| 51 | /** Unrecognized Next Header type encountered. */ |
| 52 | NEXT_HEADER_ERR(ICMP6.NEXT_HEADER_ERR), |
| 53 | /** Unrecognized IPv6 option encountered. */ |
| 54 | IPV6_OPT_ERR(ICMP6.IPV6_OPT_ERR); |
| 55 | |
| 56 | private byte value; |
| 57 | |
| 58 | /** |
| 59 | * Constructs an Icmp6Code with the given value. |
| 60 | * |
| 61 | * @param value value to use when this Icmp6Code is seen |
| 62 | */ |
| 63 | private Icmp6Code(byte value) { |
| 64 | this.value = value; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Gets the value to use for this Icmp6Code. |
| 69 | * |
| 70 | * @return short value to use for this Icmp6Code |
| 71 | */ |
| 72 | public byte value() { |
| 73 | return this.value; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Parse a string input that could contain an Icmp6Code value. The value |
| 78 | * may appear in the string either as a known code name (one of the |
| 79 | * values of this enum), or a numeric code value. |
| 80 | * |
| 81 | * @param input the input string to parse |
| 82 | * @return the numeric value of the parsed ICMPv6 code |
| 83 | * @throws IllegalArgumentException if the input string does not contain a |
| 84 | * value that can be parsed into an ICMPv6 code |
| 85 | */ |
| 86 | public static byte parseFromString(String input) { |
| 87 | try { |
| 88 | return valueOf(input).value(); |
| 89 | } catch (IllegalArgumentException e) { |
| 90 | // The input is not a known ICMPv6 code name, let's see if it's an ICMP6 |
| 91 | // code value (byte). We parse with Byte to handle unsigned values |
| 92 | // correctly. |
| 93 | try { |
| 94 | return Byte.parseByte(input); |
| 95 | } catch (NumberFormatException e1) { |
| 96 | throw new IllegalArgumentException( |
| 97 | "Icmp6Code value must be either a string code name" |
| 98 | + " or an 8-bit code value"); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |