5a02b70b5a3d1c8b37369bf4c4aaa872a97a45ad
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / mdsalutil / packet / IPProtocols.java
1 /*
2  * Copyright (c) 2013, 2015 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.vpnservice.mdsalutil.packet;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 /**
15  * Enum represents the most common IP protocols numbers It provides the binding
16  * between IP protocol names and numbers and provides APIs to read and parse
17  * them in either of the two forms
18  *
19  * NOTE: Openflow 1.0 supports the IP Proto match only for ICMP, TCP and UDP
20  *
21  * references:
22  * http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
23  */
24 public enum IPProtocols {
25      ANY("any", -1),
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      /*143-252 Unassigned by IANA*/
171
172      //Experimebtal protocol numbers (http://tools.ietf.org/html/rfc3692)
173      EXP1("Experimental1", 253),
174      EXP2("Experimental2", 254),
175
176      RESERVED("RESERVED",255);
177
178     private String protocolName;
179     private int protocolNumber;
180
181     private IPProtocols(String name, int number) {
182         protocolName = name;
183         protocolNumber = number;
184     }
185
186     public int intValue() {
187         return protocolNumber;
188     }
189
190     public short shortValue() {
191         return ((Integer) protocolNumber).shortValue();
192     }
193
194     public byte byteValue() {
195         return ((Integer) protocolNumber).byteValue();
196     }
197
198     @Override
199     public String toString() {
200         return protocolName;
201     }
202
203     public static String getProtocolName(int number) {
204         return getProtocolNameInternal(number);
205     }
206
207     public static String getProtocolName(short number) {
208         return getProtocolNameInternal(number & 0xffff);
209     }
210
211     public static String getProtocolName(byte number) {
212         return getProtocolNameInternal(number & 0xff);
213     }
214
215     private static String getProtocolNameInternal(int number) {
216         for (IPProtocols proto : IPProtocols.values()) {
217             if (proto.protocolNumber == number) {
218                 return proto.toString();
219             }
220         }
221         //TODO: this is for backwards compatibility
222         return "0x" + Integer.toHexString(number);
223     }
224
225     public static short getProtocolNumberShort(String name) {
226         IPProtocols p = fromString(name);
227         if (p != null) {
228             return p.shortValue();
229         }
230         //This method should be called after validation only
231         throw new IllegalArgumentException("Illegal IP protocol value: " + name);
232     }
233
234     public static int getProtocolNumberInt(String name) {
235         IPProtocols p = fromString(name);
236         if (p != null) {
237             return p.intValue();
238         }
239         //This method should be called after validation only
240         throw new IllegalArgumentException("Illegal IP protocol value: " + name);
241     }
242
243     public static byte getProtocolNumberByte(String name) {
244         IPProtocols p = fromString(name);
245         if (p != null) {
246             return p.byteValue();
247         }
248         //This method should be called after validation only
249         throw new IllegalArgumentException("Illegal IP protocol value: " + name);
250     }
251
252     public static List<String> getProtocolNameList() {
253         List<String> protoList = new ArrayList<String>();
254         for (IPProtocols proto : IPProtocols.values()) {
255             protoList.add(proto.toString());
256         }
257         return protoList;
258     }
259
260     /**
261      * Method to parse an IPProtocol from a numeric string
262      *
263      * @param s
264      *            The IP protocol string to be parsed
265      * @return The IP protocol Enum, or null if invalid protocol string is passed
266      */
267     public static IPProtocols fromString(String s) {
268         // null/empty/any/* evaluates to ANY
269         if (s == null || s.isEmpty() || s.equalsIgnoreCase("any") || s.equals("*")) {
270             return IPProtocols.ANY;
271         }
272
273         // Try parsing numeric and find the related ENUM
274         try {
275             int protoNum = Integer.decode(s);
276             for (IPProtocols protoEnum : IPProtocols.values()) {
277                 if (protoEnum.protocolNumber == protoNum) {
278                     return protoEnum;
279                 }
280             }
281             // At this point it's an invalid number (i.e. out of range or not a valid proto num)
282             return null;
283         } catch (NumberFormatException nfe) {
284             // numeric failed try by NAME
285             try {
286                 return valueOf(s);
287             } catch(IllegalArgumentException e) {
288                 // Neither numeric nor enum NAME, attempt human readable name
289                 for (IPProtocols protoEnum : IPProtocols.values()) {
290                     if (protoEnum.toString().equalsIgnoreCase(s)) {
291                         return protoEnum;
292                     }
293                 }
294                 //couldn't parse, signifies an invalid proto field!
295                 return null;
296             }
297
298         }
299     }
300 }