2 * Copyright (c) 2013, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.vpnservice.mdsalutil.packet;
11 import java.util.ArrayList;
12 import java.util.List;
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
19 * NOTE: Openflow 1.0 supports the IP Proto match only for ICMP, TCP and UDP
22 * http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
24 public enum IPProtocols {
36 BBNRCCMON("BBN-RCC-MON",10),
45 DCNMEAS("DCN-MEAS",19),
58 MERITINP("MERIT-INP",32),
64 IDPRCMTP("IDPR-CMTP",38),
65 TPPLUSPLUS("TP++",39),
69 IPV6Route("IPv6-Route",43),
70 IPV6Frag("IPv6-Frag",44),
84 IPV6ICMP("IPv6-ICMP", 58),
85 IPV6NoNxt("IPv6-NoNxt",59),
86 IPV6Opts("IPv6-Opts",60),
87 ANYHOST("ANY-HOST",61),
89 ANYNETWORK("ANY-NETWORK",63),
90 SATEXPAK("SAT-EXPAK",64),
91 KRYPTOLAN("KRYPTOLAN",65),
94 ANYDISTFS("ANY-DIST-FS",68),
102 BRSATMON("BR-SAT-MON",76),
105 WBEXPAK("WB-EXPAK",79),
108 SECUREVMTP("SECURE-VMTP",82),
112 NSFNETIGP("NSFNET-IGP",85),
116 OSPFIGP("OSPFIGP",89),
117 SPRITERPC("Sprite-RPC",90),
124 ETHERIP("ETHERIP",97),
126 ANYENC("ANY-ENC",99),
135 IPComp("IPComp",108),
137 COMPAQPEER("Compaq-Peer",110),
138 IPXINIP("IPX-in-IP",111),
141 ANY0HOP("ANY-0-HOP",114),
155 SSCOPMCE("SSCOPMCE",128),
161 RSVPE2EIGNORE("RSVP-E2E-IGNORE",134),
162 MOBILITYHEADER("Mobility Header",135),
163 UDPLITE("UDPLite",136),
164 MPLSINIP("MPLS-in-IP",137),
170 /*143-252 Unassigned by IANA*/
172 //Experimebtal protocol numbers (http://tools.ietf.org/html/rfc3692)
173 EXP1("Experimental1", 253),
174 EXP2("Experimental2", 254),
176 RESERVED("RESERVED",255);
178 private String protocolName;
179 private int protocolNumber;
181 private IPProtocols(String name, int number) {
183 protocolNumber = number;
186 public int intValue() {
187 return protocolNumber;
190 public short shortValue() {
191 return ((Integer) protocolNumber).shortValue();
194 public byte byteValue() {
195 return ((Integer) protocolNumber).byteValue();
199 public String toString() {
203 public static String getProtocolName(int number) {
204 return getProtocolNameInternal(number);
207 public static String getProtocolName(short number) {
208 return getProtocolNameInternal(number & 0xffff);
211 public static String getProtocolName(byte number) {
212 return getProtocolNameInternal(number & 0xff);
215 private static String getProtocolNameInternal(int number) {
216 for (IPProtocols proto : IPProtocols.values()) {
217 if (proto.protocolNumber == number) {
218 return proto.toString();
221 //TODO: this is for backwards compatibility
222 return "0x" + Integer.toHexString(number);
225 public static short getProtocolNumberShort(String name) {
226 IPProtocols p = fromString(name);
228 return p.shortValue();
230 //This method should be called after validation only
231 throw new IllegalArgumentException("Illegal IP protocol value: " + name);
234 public static int getProtocolNumberInt(String name) {
235 IPProtocols p = fromString(name);
239 //This method should be called after validation only
240 throw new IllegalArgumentException("Illegal IP protocol value: " + name);
243 public static byte getProtocolNumberByte(String name) {
244 IPProtocols p = fromString(name);
246 return p.byteValue();
248 //This method should be called after validation only
249 throw new IllegalArgumentException("Illegal IP protocol value: " + name);
252 public static List<String> getProtocolNameList() {
253 List<String> protoList = new ArrayList<String>();
254 for (IPProtocols proto : IPProtocols.values()) {
255 protoList.add(proto.toString());
261 * Method to parse an IPProtocol from a numeric string
264 * The IP protocol string to be parsed
265 * @return The IP protocol Enum, or null if invalid protocol string is passed
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;
273 // Try parsing numeric and find the related ENUM
275 int protoNum = Integer.decode(s);
276 for (IPProtocols protoEnum : IPProtocols.values()) {
277 if (protoEnum.protocolNumber == protoNum) {
281 // At this point it's an invalid number (i.e. out of range or not a valid proto num)
283 } catch (NumberFormatException nfe) {
284 // numeric failed try by NAME
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)) {
294 //couldn't parse, signifies an invalid proto field!