blob: 18ea32ea18dd3821237476290ac4841dcc190fa8 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070017
18import 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 */
24public enum EthType {
25 /** ARP. */
26 ARP(Ethernet.TYPE_ARP),
27 /** RARP. */
28 RARP(Ethernet.TYPE_RARP),
29 /** IPV4. */
30 IPV4(Ethernet.TYPE_IPV4),
Pavlin Radoslavovc6809e42015-03-11 18:04:42 -070031 /** IPV6. */
32 IPV6(Ethernet.TYPE_IPV6),
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070033 /** 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 Hart6e948282014-11-17 22:50:42 -080043 * @param value value to use when this EthType is seen
Ray Milkey9fdf1ea2014-10-21 09:48:02 -070044 */
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 Hart6e948282014-11-17 22:50:42 -080057
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 Milkey9fdf1ea2014-10-21 09:48:02 -070084}