de5834faee6a7988e9296f8b51f353b8160a4d53
[controller.git] / opendaylight / commons / liblldp / src / main / java / org / opendaylight / controller / liblldp / EtherTypes.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.liblldp;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15  * The enum contains the most common 802.3 ethernet types and 802.2 + SNAP protocol ids
16  *
17  *
18  *
19  */
20 public enum EtherTypes {
21     PVSTP("PVSTP", 0x010B), // 802.2 + SNAP (Spanning Tree)
22     CDP("CDP", 0x2000), // 802.2 + SNAP
23     VTP("VTP", 0x2003), // 802.2 + SNAP
24     IPv4("IPv4", 0x800), ARP("ARP", 0x806), RARP("Reverse ARP", 0x8035), VLANTAGGED(
25             "VLAN Tagged", 0x8100), // 802.1Q
26     IPv6("IPv6", 0x86DD), MPLSUCAST("MPLS Unicast", 0x8847), MPLSMCAST(
27             "MPLS Multicast", 0x8848), QINQ("QINQ", 0x88A8), // Standard 802.1ad QinQ
28     LLDP("LLDP", 0x88CC), OLDQINQ("Old QINQ", 0x9100), // Old non-standard QinQ
29     CISCOQINQ("Cisco QINQ", 0x9200); // Cisco non-standard QinQ
30
31     private static final String regexNumberString = "^[0-9]+$";
32     private String description;
33     private int number;
34
35     EtherTypes(String description, int number) {
36         this.description = description;
37         this.number = number;
38     }
39
40     public String toString() {
41         return description;
42     }
43
44     public int intValue() {
45         return number;
46     }
47
48     public short shortValue() {
49         return ((Integer) number).shortValue();
50     }
51
52     public static String getEtherTypeName(int number) {
53         return getEtherTypeInternal(number);
54     }
55
56     public static String getEtherTypeName(short number) {
57         return getEtherTypeInternal(number & 0xffff);
58     }
59
60     public static String getEtherTypeName(byte number) {
61         return getEtherTypeInternal(number & 0xff);
62     }
63
64     private static String getEtherTypeInternal(int number) {
65         for (EtherTypes type : EtherTypes.values()) {
66             if (type.number == number) {
67                 return type.toString();
68             }
69         }
70         return "0x" + Integer.toHexString(number);
71     }
72
73     public static short getEtherTypeNumberShort(String name) {
74         if (name.matches(regexNumberString)) {
75             return Short.valueOf(name);
76         }
77         for (EtherTypes type : EtherTypes.values()) {
78             if (type.description.equalsIgnoreCase(name)) {
79                 return type.shortValue();
80             }
81         }
82         return 0;
83     }
84
85     public static int getEtherTypeNumberInt(String name) {
86         if (name.matches(regexNumberString)) {
87             return Integer.valueOf(name);
88         }
89         for (EtherTypes type : EtherTypes.values()) {
90             if (type.description.equalsIgnoreCase(name)) {
91                 return type.intValue();
92             }
93         }
94         return 0;
95     }
96
97     public static List<String> getEtherTypesNameList() {
98         List<String> ethertypesList = new ArrayList<>();
99         for (EtherTypes type : EtherTypes.values()) {
100             ethertypesList.add(type.toString());
101         }
102         return ethertypesList;
103     }
104
105     public static EtherTypes loadFromString(String string) {
106         int intType = Integer.parseInt(string);
107
108         for (EtherTypes type : EtherTypes.values()) {
109             if (type.number == intType) {
110                 return type;
111             }
112         }
113         return null;
114     }
115
116 }