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
26 public enum IPProtocols {
38 BBNRCCMON("BBN-RCC-MON",10),
47 DCNMEAS("DCN-MEAS",19),
60 MERITINP("MERIT-INP",32),
66 IDPRCMTP("IDPR-CMTP",38),
67 TPPLUSPLUS("TP++",39),
71 IPV6Route("IPv6-Route",43),
72 IPV6Frag("IPv6-Frag",44),
86 IPV6ICMP("IPv6-ICMP", 58),
87 IPV6NoNxt("IPv6-NoNxt",59),
88 IPV6Opts("IPv6-Opts",60),
89 ANYHOST("ANY-HOST",61),
91 ANYNETWORK("ANY-NETWORK",63),
92 SATEXPAK("SAT-EXPAK",64),
93 KRYPTOLAN("KRYPTOLAN",65),
96 ANYDISTFS("ANY-DIST-FS",68),
104 BRSATMON("BR-SAT-MON",76),
107 WBEXPAK("WB-EXPAK",79),
110 SECUREVMTP("SECURE-VMTP",82),
114 NSFNETIGP("NSFNET-IGP",85),
118 OSPFIGP("OSPFIGP",89),
119 SPRITERPC("Sprite-RPC",90),
126 ETHERIP("ETHERIP",97),
128 ANYENC("ANY-ENC",99),
137 IPComp("IPComp",108),
139 COMPAQPEER("Compaq-Peer",110),
140 IPXINIP("IPX-in-IP",111),
143 ANY0HOP("ANY-0-HOP",114),
157 SSCOPMCE("SSCOPMCE",128),
163 RSVPE2EIGNORE("RSVP-E2E-IGNORE",134),
164 MOBILITYHEADER("Mobility Header",135),
165 UDPLITE("UDPLite",136),
166 MPLSINIP("MPLS-in-IP",137),
172 /*143-252 Unassigned by IANA*/
174 //Experimebtal protocol numbers (http://tools.ietf.org/html/rfc3692)
175 EXP1("Experimental1", 253),
176 EXP2("Experimental2", 254),
178 RESERVED("RESERVED",255);
180 private String protocolName;
181 private int protocolNumber;
183 private IPProtocols(String name, int number) {
185 protocolNumber = number;
188 public int intValue() {
189 return protocolNumber;
192 public short shortValue() {
193 return ((Integer) protocolNumber).shortValue();
196 public byte byteValue() {
197 return ((Integer) protocolNumber).byteValue();
201 public String toString() {
205 public static String getProtocolName(int number) {
206 return getProtocolNameInternal(number);
209 public static String getProtocolName(short number) {
210 return getProtocolNameInternal(number & 0xffff);
213 public static String getProtocolName(byte number) {
214 return getProtocolNameInternal(number & 0xff);
217 private static String getProtocolNameInternal(int number) {
218 for (IPProtocols proto : IPProtocols.values()) {
219 if (proto.protocolNumber == number) {
220 return proto.toString();
223 //TODO: this is for backwards compatibility
224 return "0x" + Integer.toHexString(number);
227 public static short getProtocolNumberShort(String name) {
228 IPProtocols p = fromString(name);
230 return p.shortValue();
232 //This method should be called after validation only
233 throw new IllegalArgumentException("Illegal IP protocol value: " + name);
236 public static int getProtocolNumberInt(String name) {
237 IPProtocols p = fromString(name);
241 //This method should be called after validation only
242 throw new IllegalArgumentException("Illegal IP protocol value: " + name);
245 public static byte getProtocolNumberByte(String name) {
246 IPProtocols p = fromString(name);
248 return p.byteValue();
250 //This method should be called after validation only
251 throw new IllegalArgumentException("Illegal IP protocol value: " + name);
254 public static List<String> getProtocolNameList() {
255 List<String> protoList = new ArrayList<String>();
256 for (IPProtocols proto : IPProtocols.values()) {
257 protoList.add(proto.toString());
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.
268 * The IP protocol string to be parsed
269 * @return The IP protocol Enum, or null if invalid protocol string is passed
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;
277 // Try parsing numeric and find the related ENUM
279 int protoNum = Integer.decode(s);
280 for (IPProtocols protoEnum : IPProtocols.values()) {
281 if (protoEnum.protocolNumber == protoNum) {
285 // At this point it's an invalid number (i.e. out of range or not a valid proto num)
287 } catch (NumberFormatException nfe) {
288 // numeric failed try by NAME
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)) {
298 //couldn't parse, signifies an invalid proto field!