Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 17 | |
| 18 | import org.onlab.packet.Ethernet; |
| 19 | |
| 20 | /** |
| 21 | * Allowed values for Ethernet types. Used by the CLI completer for |
| 22 | * connectivity based intent L2 parameters. |
| 23 | */ |
| 24 | public enum EthType { |
| 25 | /** ARP. */ |
| 26 | ARP(Ethernet.TYPE_ARP), |
| 27 | /** RARP. */ |
| 28 | RARP(Ethernet.TYPE_RARP), |
| 29 | /** IPV4. */ |
| 30 | IPV4(Ethernet.TYPE_IPV4), |
Pavlin Radoslavov | c6809e4 | 2015-03-11 18:04:42 -0700 | [diff] [blame] | 31 | /** IPV6. */ |
| 32 | IPV6(Ethernet.TYPE_IPV6), |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 33 | /** LLDP. */ |
| 34 | LLDP(Ethernet.TYPE_LLDP), |
| 35 | /** BSN. */ |
| 36 | BSN(Ethernet.TYPE_BSN); |
| 37 | |
| 38 | private short value; |
| 39 | |
| 40 | /** |
| 41 | * Constructs an EthType with the given value. |
| 42 | * |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 43 | * @param value value to use when this EthType is seen |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 44 | */ |
| 45 | private EthType(short value) { |
| 46 | this.value = value; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Gets the value to use for this EthType. |
| 51 | * |
| 52 | * @return short value to use for this EthType |
| 53 | */ |
| 54 | public short value() { |
| 55 | return this.value; |
| 56 | } |
Jonathan Hart | 6e94828 | 2014-11-17 22:50:42 -0800 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Parse a string input that could contain an EthType value. The value |
| 60 | * may appear in the string either as a known protocol name (one of the |
| 61 | * values of this enum), or a numeric protocol value. |
| 62 | * |
| 63 | * @param input the input string to parse |
| 64 | * @return the numeric value of the parsed Ethernet type |
| 65 | * @throws IllegalArgumentException if the input string does not contain a |
| 66 | * value that can be parsed into an Ethernet type |
| 67 | */ |
| 68 | public static short parseFromString(String input) { |
| 69 | try { |
| 70 | return valueOf(input).value(); |
| 71 | } catch (IllegalArgumentException e) { |
| 72 | // The input is not a known Ethernet type name, let's see if it's an |
| 73 | // Ethernet type value (short). We parse with Integer to handle |
| 74 | // unsigned values correctly. |
| 75 | try { |
| 76 | return (short) Integer.parseInt(input); |
| 77 | } catch (NumberFormatException e1) { |
| 78 | throw new IllegalArgumentException( |
| 79 | "EthType value must be either a string protocol name" |
| 80 | + " or a 16-bit protocol value"); |
| 81 | } |
| 82 | } |
| 83 | } |
Ray Milkey | 9fdf1ea | 2014-10-21 09:48:02 -0700 | [diff] [blame] | 84 | } |