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 | /** |
| 19 | * Known values for IPv6 extension header field that can be supplied to the CLI. |
| 20 | */ |
| 21 | public enum ExtHeader { |
| 22 | /** No next header. */ |
| 23 | NOEXT((short) (1 << 0)), |
| 24 | /** Encapsulated Security Payload. */ |
| 25 | ESP((short) (1 << 1)), |
| 26 | /** Authentication header. */ |
| 27 | AUTH((short) (1 << 2)), |
| 28 | /** Destination header. */ |
| 29 | DEST((short) (1 << 3)), |
| 30 | /** Fragment header. */ |
| 31 | FRAG((short) (1 << 4)), |
| 32 | /** Router header. */ |
| 33 | ROUTE((short) (1 << 5)), |
| 34 | /** Hop-by-hop header. */ |
| 35 | HOP((short) (1 << 6)), |
| 36 | /** Unexpected repeats encountered. */ |
| 37 | UNREP((short) (1 << 7)), |
| 38 | /** Unexpected sequencing encountered. */ |
| 39 | UNSEQ((short) (1 << 8)); |
| 40 | |
| 41 | private short value; |
| 42 | |
| 43 | /** |
| 44 | * Constructs an ExtHeader with the given value. |
| 45 | * |
| 46 | * @param value value to use when this ExtHeader is seen |
| 47 | */ |
| 48 | private ExtHeader(short value) { |
| 49 | this.value = value; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Gets the value to use for this ExtHeader. |
| 54 | * |
| 55 | * @return short value to use for this ExtHeader |
| 56 | */ |
| 57 | public short value() { |
| 58 | return this.value; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Parse a string input that could contain an ExtHeader value. The value |
| 63 | * may appear in the string either as a known exntension header name (one of the |
| 64 | * values of this enum), or a numeric extension header value. |
| 65 | * |
| 66 | * @param input the input string to parse |
| 67 | * @return the numeric value of the parsed IPv6 extension header |
| 68 | * @throws IllegalArgumentException if the input string does not contain a |
| 69 | * value that can be parsed into an IPv6 extension header |
| 70 | */ |
| 71 | public static short parseFromString(String input) { |
| 72 | try { |
| 73 | return valueOf(input).value(); |
| 74 | } catch (IllegalArgumentException e) { |
| 75 | // The input is not a known IPv6 extension header name, let's see if |
| 76 | // it's an IPv6 extension header value (short). |
| 77 | // We parse with Short to handle unsigned values correctly. |
| 78 | try { |
| 79 | return Short.parseShort(input); |
| 80 | } catch (NumberFormatException e1) { |
| 81 | throw new IllegalArgumentException( |
| 82 | "ExtHeader value must be either a string extension header name" |
| 83 | + " or an 8-bit extension header value"); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |