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