Charles M.C. Chan | 2184de1 | 2015-04-26 02:24:53 +0800 | [diff] [blame] | 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.onosproject.cli.net; |
| 17 | |
| 18 | import org.onlab.packet.ICMP6; |
| 19 | |
| 20 | /** |
| 21 | * Known values for ICMPv6 type field that can be supplied to the CLI. |
| 22 | */ |
| 23 | public enum Icmp6Type { |
| 24 | /** Destination Unreachable. */ |
| 25 | DEST_UNREACH(ICMP6.DEST_UNREACH), |
| 26 | /** Packet Too Big. */ |
| 27 | PKT_TOO_BIG(ICMP6.PKT_TOO_BIG), |
| 28 | /** Time Exceeded. */ |
| 29 | TIME_EXCEED(ICMP6.TIME_EXCEED), |
| 30 | /** Parameter Problem. */ |
| 31 | PARAM_ERR(ICMP6.PARAM_ERR), |
| 32 | /** Echo Request. */ |
| 33 | ECHO_REQUEST(ICMP6.ECHO_REQUEST), |
| 34 | /** Echo Reply. */ |
| 35 | ECHO_REPLY(ICMP6.ECHO_REPLY), |
| 36 | /** Multicast Listener Query. */ |
| 37 | MCAST_QUERY(ICMP6.MCAST_QUERY), |
| 38 | /** Multicast Listener Report. */ |
| 39 | MCAST_REPORT(ICMP6.MCAST_REPORT), |
| 40 | /** Multicast Listener Done. */ |
| 41 | MCAST_DONE(ICMP6.MCAST_DONE), |
| 42 | /** Router Solicitation. */ |
| 43 | ROUTER_SOLICITATION(ICMP6.ROUTER_SOLICITATION), |
| 44 | /** Router Advertisement. */ |
| 45 | ROUTER_ADVERTISEMENT(ICMP6.ROUTER_ADVERTISEMENT), |
| 46 | /** Neighbor Solicitation. */ |
| 47 | NEIGHBOR_SOLICITATION(ICMP6.NEIGHBOR_SOLICITATION), |
| 48 | /** Neighbor Advertisement. */ |
| 49 | NEIGHBOR_ADVERTISEMENT(ICMP6.NEIGHBOR_ADVERTISEMENT), |
| 50 | /** Redirect Message. */ |
| 51 | REDIRECT(ICMP6.REDIRECT); |
| 52 | |
| 53 | |
| 54 | private byte value; |
| 55 | |
| 56 | /** |
| 57 | * Constructs an Icmp6Type with the given value. |
| 58 | * |
| 59 | * @param value value to use when this Icmp6Type is seen |
| 60 | */ |
| 61 | private Icmp6Type(byte value) { |
| 62 | this.value = value; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Gets the value to use for this Icmp6Type. |
| 67 | * |
| 68 | * @return short value to use for this Icmp6Type |
| 69 | */ |
| 70 | public byte value() { |
| 71 | return this.value; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Parse a string input that could contain an Icmp6Type value. The value |
| 76 | * may appear in the string either as a known type name (one of the |
| 77 | * values of this enum), or a numeric type value. |
| 78 | * |
| 79 | * @param input the input string to parse |
| 80 | * @return the numeric value of the parsed ICMPv6 type |
| 81 | * @throws IllegalArgumentException if the input string does not contain a |
| 82 | * value that can be parsed into an ICMPv6 type |
| 83 | */ |
| 84 | public static byte parseFromString(String input) { |
| 85 | try { |
| 86 | return valueOf(input).value(); |
| 87 | } catch (IllegalArgumentException e) { |
| 88 | // The input is not a known ICMPv6 type name, let's see if it's an ICMP6 |
| 89 | // type value (byte). We parse with Byte to handle unsigned values |
| 90 | // correctly. |
| 91 | try { |
| 92 | return Byte.parseByte(input); |
| 93 | } catch (NumberFormatException e1) { |
| 94 | throw new IllegalArgumentException( |
| 95 | "Icmp6Type value must be either a string type name" |
| 96 | + " or an 8-bit type value"); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |