3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.sal.utils;
12 import java.util.ArrayList;
13 import java.util.List;
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
20 * NOTE: Openflow 1.0 supports the IP Proto match only for ICMP, TCP and UDP
23 * http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
25 public enum IPProtocols {
37 BBNRCCMON("BBN-RCC-MON",10),
46 DCNMEAS("DCN-MEAS",19),
59 MERITINP("MERIT-INP",32),
65 IDPRCMTP("IDPR-CMTP",38),
66 TPPLUSPLUS("TP++",39),
70 IPV6Route("IPv6-Route",43),
71 IPV6Frag("IPv6-Frag",44),
85 IPV6ICMP("IPv6-ICMP", 58),
86 IPV6NoNxt("IPv6-NoNxt",59),
87 IPV6Opts("IPv6-Opts",60),
88 ANYHOST("ANY-HOST",61),
90 ANYNETWORK("ANY-NETWORK",63),
91 SATEXPAK("SAT-EXPAK",64),
92 KRYPTOLAN("KRYPTOLAN",65),
95 ANYDISTFS("ANY-DIST-FS",68),
103 BRSATMON("BR-SAT-MON",76),
106 WBEXPAK("WB-EXPAK",79),
109 SECUREVMTP("SECURE-VMTP",82),
113 NSFNETIGP("NSFNET-IGP",85),
117 OSPFIGP("OSPFIGP",89),
118 SPRITERPC("Sprite-RPC",90),
125 ETHERIP("ETHERIP",97),
127 ANYENC("ANY-ENC",99),
136 IPComp("IPComp",108),
138 COMPAQPEER("Compaq-Peer",110),
139 IPXINIP("IPX-in-IP",111),
142 ANY0HOP("ANY-0-HOP",114),
156 SSCOPMCE("SSCOPMCE",128),
162 RSVPE2EIGNORE("RSVP-E2E-IGNORE",134),
163 MOBILITYHEADER("Mobility Header",135),
164 UDPLITE("UDPLite",136),
165 MPLSINIP("MPLS-in-IP",137),
171 /*143-252 Unassigned by IANA*/
173 //Experimebtal protocol numbers (http://tools.ietf.org/html/rfc3692)
174 EXP1("Experimental1", 253),
175 EXP2("Experimental2", 254),
177 RESERVED("RESERVED",255);
179 private String protocolName;
180 private int protocolNumber;
182 private IPProtocols(String name, int number) {
184 protocolNumber = number;
187 public int intValue() {
188 return protocolNumber;
191 public short shortValue() {
192 return ((Integer) protocolNumber).shortValue();
195 public byte byteValue() {
196 return ((Integer) protocolNumber).byteValue();
200 public String toString() {
204 public static String getProtocolName(int number) {
205 return getProtocolNameInternal(number);
208 public static String getProtocolName(short number) {
209 return getProtocolNameInternal(number & 0xffff);
212 public static String getProtocolName(byte number) {
213 return getProtocolNameInternal(number & 0xff);
216 private static String getProtocolNameInternal(int number) {
217 for (IPProtocols proto : IPProtocols.values()) {
218 if (proto.protocolNumber == number) {
219 return proto.toString();
222 //TODO: this is for backwards compatibility
223 return "0x" + Integer.toHexString(number);
226 public static short getProtocolNumberShort(String name) {
227 IPProtocols p = fromString(name);
229 return p.shortValue();
231 //This method should be called after validation only
232 throw new IllegalArgumentException("Illegal IP protocol value: " + name);
235 public static int getProtocolNumberInt(String name) {
236 IPProtocols p = fromString(name);
240 //This method should be called after validation only
241 throw new IllegalArgumentException("Illegal IP protocol value: " + name);
244 public static byte getProtocolNumberByte(String name) {
245 IPProtocols p = fromString(name);
247 return p.byteValue();
249 //This method should be called after validation only
250 throw new IllegalArgumentException("Illegal IP protocol value: " + name);
253 public static List<String> getProtocolNameList() {
254 List<String> protoList = new ArrayList<String>();
255 for (IPProtocols proto : IPProtocols.values()) {
256 protoList.add(proto.toString());
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.
267 * The IP protocol string to be parsed
268 * @return The IP protocol Enum, or null if invalid protocol string is passed
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;
276 // Try parsing numeric and find the related ENUM
278 int protoNum = Integer.decode(s);
279 for (IPProtocols protoEnum : IPProtocols.values()) {
280 if (protoEnum.protocolNumber == protoNum) {
284 // At this point it's an invalid number (i.e. out of range or not a valid proto num)
286 } catch (NumberFormatException nfe) {
287 // numeric failed try by NAME
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)) {
297 //couldn't parse, signifies an invalid proto field!