BUG 2302 : odl-clustering-test-app should not be part of the odl-restconf-all feature set
[controller.git] / opendaylight / adsal / 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  * Enum represents the most common IP protocols numbers It provides the binding
17  * between IP protocol names and numbers and provides APIs to read and parse
18  * them in either of the two forms
19  *
20  * NOTE: Openflow 1.0 supports the IP Proto match only for ICMP, TCP and UDP
21  *
22  * references:
23  * http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
24  */
25 public enum IPProtocols {
26      ANY("any", -1),
27      HOPOPT("HOPOPT",0),
28      ICMP("ICMP", 1),
29      IGMP("IGMP",2),
30      GGP("GGP",3),
31      IPV4("IPv4",4),
32      ST("ST",5),
33      TCP("TCP", 6),
34      CBT("CBT",7),
35      EGP("EGP",8),
36      IGP("IGP",9),
37      BBNRCCMON("BBN-RCC-MON",10),
38      NVPII("NVP-II",11),
39      PUP("PUP",12),
40      ARGUS("ARGUS",13),
41      EMCON("EMCON",14),
42      XNET("XNET",15),
43      CHAOS("CHAOS",16),
44      UDP("UDP", 17),
45      MUX("MUX",18),
46      DCNMEAS("DCN-MEAS",19),
47      HMP("HMP",20),
48      PRM("PRM",21),
49      XNSIDP("XNS-IDP",22),
50      TRUNK1("TRUNK-1",23),
51      TRUNK2("TRUNK-2",24),
52      LEAF1("LEAF-1",25),
53      LEAF2("LEAF-2",26),
54      RDP("RDP",27),
55      IRTP("IRTP",28),
56      ISOTP4("ISO-TP4",29),
57      NETBLT("NETBLT",30),
58      MFENSP("MFE-NSP",31),
59      MERITINP("MERIT-INP",32),
60      DCCP("DCCP",33),
61      THREEPC("3PC",34),
62      IDPR("IDPR",35),
63      XTP("XTP",36),
64      DDP("DDP",37),
65      IDPRCMTP("IDPR-CMTP",38),
66      TPPLUSPLUS("TP++",39),
67      IL("IL",40),
68      IPV6("IPv6",41),
69      SDRP("SDRP",42),
70      IPV6Route("IPv6-Route",43),
71      IPV6Frag("IPv6-Frag",44),
72      IDRP("IDRP",45),
73      RSVP("RSVP",46),
74      GRE("GRE",47),
75      DSR("DSR",48),
76      BNA("BNA",49),
77      ESP("ESP",50),
78      AH("AH",51),
79      INLSP("I-NLSP",52),
80      SWIPE("SWIPE",53),
81      NARP("NARP",54),
82      MOBILE("MOBILE",55),
83      TLSP("TLSP",56),
84      SKIP("SKIP",57),
85      IPV6ICMP("IPv6-ICMP", 58),
86      IPV6NoNxt("IPv6-NoNxt",59),
87      IPV6Opts("IPv6-Opts",60),
88      ANYHOST("ANY-HOST",61),
89      CFTP("CFTP",62),
90      ANYNETWORK("ANY-NETWORK",63),
91      SATEXPAK("SAT-EXPAK",64),
92      KRYPTOLAN("KRYPTOLAN",65),
93      RVD("RVD",66),
94      IPPC("IPPC",67),
95      ANYDISTFS("ANY-DIST-FS",68),
96      SATMON("SAT-MON",69),
97      VISA("VISA",70),
98      IPCV("IPCV",71),
99      CPNX("CPNX",72),
100      CPHB("CPHB",73),
101      WSN("WSN",74),
102      PVP("PVP",75),
103      BRSATMON("BR-SAT-MON",76),
104      SUNND("SUN-ND",77),
105      WBMON("WB-MON",78),
106      WBEXPAK("WB-EXPAK",79),
107      ISOIP("ISO-IP",80),
108      VMTP("VMTP",81),
109      SECUREVMTP("SECURE-VMTP",82),
110      VINES("VINES",83),
111      TTP("TTP",84),
112      IPTM("IPTM",84),
113      NSFNETIGP("NSFNET-IGP",85),
114      DGP("DGP",86),
115      TCF("TCF",87),
116      EIGRP("EIGRP",88),
117      OSPFIGP("OSPFIGP",89),
118      SPRITERPC("Sprite-RPC",90),
119      LARP("LARP",91),
120      MTP("MTP",92),
121      AX25("AX.25",93),
122      IPIP("IPIP",94),
123      MICP("MICP",95),
124      SCCSP("SCC-SP",96),
125      ETHERIP("ETHERIP",97),
126      ENCAP("ENCAP",98),
127      ANYENC("ANY-ENC",99),
128      GMTP("GMTP",100),
129      IFMP("IFMP",101),
130      PNNI("PNNI",102),
131      PIM("PIM",103),
132      ARIS("ARIS",104),
133      SCPS("SCPS",105),
134      QNX("QNX",106),
135      AN("A/N",107),
136      IPComp("IPComp",108),
137      SNP("SNP",109),
138      COMPAQPEER("Compaq-Peer",110),
139      IPXINIP("IPX-in-IP",111),
140      VRRP("VRRP",112),
141      PGM("PGM",113),
142      ANY0HOP("ANY-0-HOP",114),
143      L2TP("L2TP",115),
144      DDX("DDX",116),
145      IATP("IATP",117),
146      STP("STP",118),
147      SRP("SRP",119),
148      UTI("UTI",120),
149      SMP("SMP",121),
150      SM("SM",122),
151      PTP("PTP",123),
152      ISIS("ISIS",124),
153      FIRE("FIRE",125),
154      CRTP("CRTP",126),
155      CRUDP("CRUDP",127),
156      SSCOPMCE("SSCOPMCE",128),
157      IPLT("IPLT",129),
158      SPS("SPS",130),
159      PIPE("PIPE",131),
160      SCTP("SCTP",132),
161      FC("FC",133),
162      RSVPE2EIGNORE("RSVP-E2E-IGNORE",134),
163      MOBILITYHEADER("Mobility Header",135),
164      UDPLITE("UDPLite",136),
165      MPLSINIP("MPLS-in-IP",137),
166      MANET("MANET",138),
167      HIP("HIP",139),
168      SHIM6("Shim6",140),
169      WESP("WESP",141),
170      ROHC("ROHC",142),
171      /*143-252 Unassigned by IANA*/
172
173      //Experimebtal protocol numbers (http://tools.ietf.org/html/rfc3692)
174      EXP1("Experimental1", 253),
175      EXP2("Experimental2", 254),
176
177      RESERVED("RESERVED",255);
178
179     private String protocolName;
180     private int protocolNumber;
181
182     private IPProtocols(String name, int number) {
183         protocolName = name;
184         protocolNumber = number;
185     }
186
187     public int intValue() {
188         return protocolNumber;
189     }
190
191     public short shortValue() {
192         return ((Integer) protocolNumber).shortValue();
193     }
194
195     public byte byteValue() {
196         return ((Integer) protocolNumber).byteValue();
197     }
198
199     @Override
200     public String toString() {
201         return protocolName;
202     }
203
204     public static String getProtocolName(int number) {
205         return getProtocolNameInternal(number);
206     }
207
208     public static String getProtocolName(short number) {
209         return getProtocolNameInternal(number & 0xffff);
210     }
211
212     public static String getProtocolName(byte number) {
213         return getProtocolNameInternal(number & 0xff);
214     }
215
216     private static String getProtocolNameInternal(int number) {
217         for (IPProtocols proto : IPProtocols.values()) {
218             if (proto.protocolNumber == number) {
219                 return proto.toString();
220             }
221         }
222         //TODO: this is for backwards compatibility
223         return "0x" + Integer.toHexString(number);
224     }
225
226     public static short getProtocolNumberShort(String name) {
227         IPProtocols p = fromString(name);
228         if (p != null) {
229             return p.shortValue();
230         }
231         //This method should be called after validation only
232         throw new IllegalArgumentException("Illegal IP protocol value: " + name);
233     }
234
235     public static int getProtocolNumberInt(String name) {
236         IPProtocols p = fromString(name);
237         if (p != null) {
238             return p.intValue();
239         }
240         //This method should be called after validation only
241         throw new IllegalArgumentException("Illegal IP protocol value: " + name);
242     }
243
244     public static byte getProtocolNumberByte(String name) {
245         IPProtocols p = fromString(name);
246         if (p != null) {
247             return p.byteValue();
248         }
249         //This method should be called after validation only
250         throw new IllegalArgumentException("Illegal IP protocol value: " + name);
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
261     /**
262      * Method to parse an IPProtocol from a numeric string
263      * (see: {@link Java.Lang.Integer.decode(java.lang.String)} for parsable strings),
264      * or this enum's name string.
265      *
266      * @param s
267      *            The IP protocol string to be parsed
268      * @return The IP protocol Enum, or null if invalid protocol string is passed
269      */
270     public static IPProtocols fromString(String s) {
271         // null/empty/any/* evaluates to ANY
272         if (s == null || s.isEmpty() || s.equalsIgnoreCase("any") || s.equals("*")) {
273             return IPProtocols.ANY;
274         }
275
276         // Try parsing numeric and find the related ENUM
277         try {
278             int protoNum = Integer.decode(s);
279             for (IPProtocols protoEnum : IPProtocols.values()) {
280                 if (protoEnum.protocolNumber == protoNum) {
281                     return protoEnum;
282                 }
283             }
284             // At this point it's an invalid number (i.e. out of range or not a valid proto num)
285             return null;
286         } catch (NumberFormatException nfe) {
287             // numeric failed try by NAME
288             try {
289                 return valueOf(s);
290             } catch(IllegalArgumentException e) {
291                 // Neither numeric nor enum NAME, attempt human readable name
292                 for (IPProtocols protoEnum : IPProtocols.values()) {
293                     if (protoEnum.toString().equalsIgnoreCase(s)) {
294                         return protoEnum;
295                     }
296                 }
297                 //couldn't parse, signifies an invalid proto field!
298                 return null;
299             }
300
301         }
302     }
303 }