Added validation for protocol field while adding flow
[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 regexDecimalString = "^[0-9]{3}$";
172     private static final String regexHexString = "^(0(x|X))[0-9a-fA-F]{2}$";
173     private String protocolName;
174     private int protocolNumber;
175
176     private IPProtocols(String name, int number) {
177         protocolName = name;
178         protocolNumber = number;
179     }
180
181     public int intValue() {
182         return protocolNumber;
183     }
184
185     public short shortValue() {
186         return ((Integer) protocolNumber).shortValue();
187     }
188
189     public byte byteValue() {
190         return ((Integer) protocolNumber).byteValue();
191     }
192
193     public String toString() {
194         return protocolName;
195     }
196
197     public static String getProtocolName(int number) {
198         return getProtocolNameInternal(number);
199     }
200
201     public static String getProtocolName(short number) {
202         return getProtocolNameInternal((int) number & 0xffff);
203     }
204
205     public static String getProtocolName(byte number) {
206         return getProtocolNameInternal((int) number & 0xff);
207     }
208
209     private static String getProtocolNameInternal(int number) {
210         for (IPProtocols proto : IPProtocols.values()) {
211             if (proto.protocolNumber == number) {
212                 return proto.toString();
213             }
214         }
215         return "0x" + Integer.toHexString(number);
216     }
217
218     public static short getProtocolNumberShort(String name) {
219         if (name.matches(regexHexString)) {
220             return Short.valueOf(Short.decode(name));
221         }
222         if (name.matches(regexDecimalString)) {
223             return Short.valueOf(name);
224         }
225         for (IPProtocols proto : IPProtocols.values()) {
226             if (proto.protocolName.equalsIgnoreCase(name)) {
227                 return proto.shortValue();
228             }
229         }
230         return 0;
231     }
232
233     public static int getProtocolNumberInt(String name) {
234         if (name.matches(regexHexString)) {
235             return Integer.valueOf(Integer.decode(name));
236         }
237         if (name.matches(regexDecimalString)) {
238             return Integer.valueOf(name);
239         }
240         for (IPProtocols proto : IPProtocols.values()) {
241             if (proto.protocolName.equalsIgnoreCase(name)) {
242                 return proto.intValue();
243             }
244         }
245         return 0;
246     }
247
248     public static byte getProtocolNumberByte(String name) {
249         if (name.matches(regexHexString)) {
250             return Integer.valueOf(Integer.decode(name)).byteValue();
251         }
252         if (name.matches(regexDecimalString)) {
253             return Integer.valueOf(name).byteValue();
254         }
255         for (IPProtocols proto : IPProtocols.values()) {
256             if (proto.protocolName.equalsIgnoreCase(name)) {
257                 return proto.byteValue();
258             }
259         }
260         return 0;
261     }
262
263     public static List<String> getProtocolNameList() {
264         List<String> protoList = new ArrayList<String>();
265         for (IPProtocols proto : IPProtocols.values()) {
266             protoList.add(proto.toString());
267         }
268         return protoList;
269     }
270 }