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