cdb4463c02acb2636a0991053f9e64951e3f3ba5
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / IPProtocols.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  * It represents the most common IP protocols numbers
17  * It provides the binding between IP protocol names and numbers
18  * and provides APIs to read and parse them in either of the two forms
19  *
20  *
21  *
22  */
23 // Openflow 1.0 supports the IP Proto match only for ICMP, TCP and UDP
24 public enum IPProtocols {
25     ANY("any", 0),
26     /*  HOPOPT("HOPOPT",0),
27      */ICMP("ICMP", 1),
28     /*  IGMP("IGMP",2),
29      GGP("GGP",3),
30      IPV4("IPv4",4),
31      ST("ST",5),
32      */TCP("TCP", 6),
33     /*  CBT("CBT",7),
34      EGP("EGP",8),
35      IGP("IGP",9),
36      BBNRCCMON("BBN-RCC-MON",10),
37      NVPII("NVP-II",11),
38      PUP("PUP",12),
39      ARGUS("ARGUS",13),
40      EMCON("EMCON",14),
41      XNET("XNET",15),
42      CHAOS("CHAOS",16),
43      */UDP("UDP", 17),
44     /*  MUX("MUX",18),
45      DCNMEAS("DCN-MEAS",19),
46      HMP("HMP",20),
47      PRM("PRM",21),
48      XNSIDP("XNS-IDP",22),
49      TRUNK1("TRUNK-1",23),
50      TRUNK2("TRUNK-2",24),
51      LEAF1("LEAF-1",25),
52      LEAF2("LEAF-2",26),
53      RDP("RDP",27),
54      IRTP("IRTP",28),
55      ISOTP4("ISO-TP4",29),
56      NETBLT("NETBLT",30),
57      MFENSP("MFE-NSP",31),
58      MERITINP("MERIT-INP",32),
59      DCCP("DCCP",33),
60      THREEPC("3PC",34),
61      IDPR("IDPR",35),
62      XTP("XTP",36),
63      DDP("DDP",37),
64      IDPRCMTP("IDPR-CMTP",38),
65      TPPLUSPLUS("TP++",39),
66      IL("IL",40),
67      IPV6("IPv6",41),
68      SDRP("SDRP",42),
69      IPV6Route("IPv6-Route",43),
70      IPV6Frag("IPv6-Frag",44),
71      IDRP("IDRP",45),
72      RSVP("RSVP",46),
73      GRE("GRE",47),
74      DSR("DSR",48),
75      BNA("BNA",49),
76      ESP("ESP",50),
77      AH("AH",51),
78      INLSP("I-NLSP",52),
79      SWIPE("SWIPE",53),
80      NARP("NARP",54),
81      MOBILE("MOBILE",55),
82      TLSP("TLSP",56),
83      SKIP("SKIP",57),
84      */IPV6ICMP("IPv6-ICMP", 58);
85     /*  IPV6NoNxt("IPv6-NoNxt",59),
86      IPV6Opts("IPv6-Opts",60),
87      ANYHOST("ANY-HOST",61),
88      CFTP("CFTP",62),
89      ANYNETWORK("ANY-NETWORK",63),
90      SATEXPAK("SAT-EXPAK",64),
91      KRYPTOLAN("KRYPTOLAN",65),
92      RVD("RVD",66),
93      IPPC("IPPC",67),
94      ANYDISTFS("ANY-DIST-FS",68),
95      SATMON("SAT-MON",69),
96      VISA("VISA",70),
97      IPCV("IPCV",71),
98      CPNX("CPNX",72),
99      CPHB("CPHB",73),
100      WSN("WSN",74),
101      PVP("PVP",75),
102      BRSATMON("BR-SAT-MON",76),
103      SUNND("SUN-ND",77),
104      WBMON("WB-MON",78),
105      WBEXPAK("WB-EXPAK",79),
106      ISOIP("ISO-IP",80),
107      VMTP("VMTP",81),
108      SECUREVMTP("SECURE-VMTP",82),
109      VINES("VINES",83),
110      TTP("TTP",84),
111      IPTM("IPTM",84),
112      NSFNETIGP("NSFNET-IGP",85),
113      DGP("DGP",86),
114      TCF("TCF",87),
115      EIGRP("EIGRP",88),
116      OSPFIGP("OSPFIGP",89),
117      SPRITERPC("Sprite-RPC",90),
118      LARP("LARP",91),
119      MTP("MTP",92),
120      AX25("AX.25",93),
121      IPIP("IPIP",94),
122      MICP("MICP",95),
123      SCCSP("SCC-SP",96),
124      ETHERIP("ETHERIP",97),
125      ENCAP("ENCAP",98),
126      ANYENC("ANY-ENC",99),
127      GMTP("GMTP",100),
128      IFMP("IFMP",101),
129      PNNI("PNNI",102),
130      PIM("PIM",103),
131      ARIS("ARIS",104),
132      SCPS("SCPS",105),
133      QNX("QNX",106),
134      AN("A/N",107),
135      IPComp("IPComp",108),
136      SNP("SNP",109),
137      COMPAQPEER("Compaq-Peer",110),
138      IPXINIP("IPX-in-IP",111),
139      VRRP("VRRP",112),
140      PGM("PGM",113),
141      ANY0HOP("ANY-0-HOP",114),
142      L2TP("L2TP",115),
143      DDX("DDX",116),
144      IATP("IATP",117),
145      STP("STP",118),
146      SRP("SRP",119),
147      UTI("UTI",120),
148      SMP("SMP",121),
149      SM("SM",122),
150      PTP("PTP",123),
151      ISIS("ISIS",124),
152      FIRE("FIRE",125),
153      CRTP("CRTP",126),
154      CRUDP("CRUDP",127),
155      SSCOPMCE("SSCOPMCE",128),
156      IPLT("IPLT",129),
157      SPS("SPS",130),
158      PIPE("PIPE",131),
159      SCTP("SCTP",132),
160      FC("FC",133),
161      RSVPE2EIGNORE("RSVP-E2E-IGNORE",134),
162      MOBILITYHEADER("Mobility Header",135),
163      UDPLITE("UDPLite",136),
164      MPLSINIP("MPLS-in-IP",137),
165      MANET("MANET",138),
166      HIP("HIP",139),
167      SHIM6("Shim6",140),
168      WESP("WESP",141),
169      ROHC("ROHC",142);
170      */
171     private static final String regexNumberString = "^[0-9]+$";
172     private String protocolName;
173     private int protocolNumber;
174
175     private IPProtocols(String name, int number) {
176         protocolName = name;
177         protocolNumber = number;
178     }
179
180     public int intValue() {
181         return protocolNumber;
182     }
183
184     public short shortValue() {
185         return ((Integer) protocolNumber).shortValue();
186     }
187
188     public byte byteValue() {
189         return ((Integer) protocolNumber).byteValue();
190     }
191
192     public String toString() {
193         return protocolName;
194     }
195
196     public static String getProtocolName(int number) {
197         return getProtocolNameInternal(number);
198     }
199
200     public static String getProtocolName(short number) {
201         return getProtocolNameInternal((int) number & 0xffff);
202     }
203
204     public static String getProtocolName(byte number) {
205         return getProtocolNameInternal((int) number & 0xff);
206     }
207
208     private static String getProtocolNameInternal(int number) {
209         for (IPProtocols proto : IPProtocols.values()) {
210             if (proto.protocolNumber == number) {
211                 return proto.toString();
212             }
213         }
214         return "0x" + Integer.toHexString(number);
215     }
216
217     public static short getProtocolNumberShort(String name) {
218         if (name.matches(regexNumberString)) {
219             return Short.valueOf(name);
220         }
221         for (IPProtocols proto : IPProtocols.values()) {
222             if (proto.protocolName.equalsIgnoreCase(name)) {
223                 return proto.shortValue();
224             }
225         }
226         return 0;
227     }
228
229     public static int getProtocolNumberInt(String name) {
230         if (name.matches(regexNumberString)) {
231             return Integer.valueOf(name);
232         }
233         for (IPProtocols proto : IPProtocols.values()) {
234             if (proto.protocolName.equalsIgnoreCase(name)) {
235                 return proto.intValue();
236             }
237         }
238         return 0;
239     }
240
241     public static byte getProtocolNumberByte(String name) {
242         if (name.matches(regexNumberString)) {
243             return Integer.valueOf(name).byteValue();
244         }
245         for (IPProtocols proto : IPProtocols.values()) {
246             if (proto.protocolName.equalsIgnoreCase(name)) {
247                 return proto.byteValue();
248             }
249         }
250         return 0;
251     }
252
253     public static List<String> getProtocolNameList() {
254         List<String> protoList = new ArrayList<String>();
255         for (IPProtocols proto : IPProtocols.values()) {
256             protoList.add(proto.toString());
257         }
258         return protoList;
259     }
260 }