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 * 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
23 // Openflow 1.0 supports the IP Proto match only for ICMP, TCP and UDP
24 public enum IPProtocols {
26 /* HOPOPT("HOPOPT",0),
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),
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;
176 private IPProtocols(String name, int number) {
178 protocolNumber = number;
181 public int intValue() {
182 return protocolNumber;
185 public short shortValue() {
186 return ((Integer) protocolNumber).shortValue();
189 public byte byteValue() {
190 return ((Integer) protocolNumber).byteValue();
193 public String toString() {
197 public static String getProtocolName(int number) {
198 return getProtocolNameInternal(number);
201 public static String getProtocolName(short number) {
202 return getProtocolNameInternal((int) number & 0xffff);
205 public static String getProtocolName(byte number) {
206 return getProtocolNameInternal((int) number & 0xff);
209 private static String getProtocolNameInternal(int number) {
210 for (IPProtocols proto : IPProtocols.values()) {
211 if (proto.protocolNumber == number) {
212 return proto.toString();
215 return "0x" + Integer.toHexString(number);
218 public static short getProtocolNumberShort(String name) {
219 if (name.matches(regexHexString)) {
220 return Short.valueOf(Short.decode(name));
222 if (name.matches(regexDecimalString)) {
223 return Short.valueOf(name);
225 for (IPProtocols proto : IPProtocols.values()) {
226 if (proto.protocolName.equalsIgnoreCase(name)) {
227 return proto.shortValue();
233 public static int getProtocolNumberInt(String name) {
234 if (name.matches(regexHexString)) {
235 return Integer.valueOf(Integer.decode(name));
237 if (name.matches(regexDecimalString)) {
238 return Integer.valueOf(name);
240 for (IPProtocols proto : IPProtocols.values()) {
241 if (proto.protocolName.equalsIgnoreCase(name)) {
242 return proto.intValue();
248 public static byte getProtocolNumberByte(String name) {
249 if (name.matches(regexHexString)) {
250 return Integer.valueOf(Integer.decode(name)).byteValue();
252 if (name.matches(regexDecimalString)) {
253 return Integer.valueOf(name).byteValue();
255 for (IPProtocols proto : IPProtocols.values()) {
256 if (proto.protocolName.equalsIgnoreCase(name)) {
257 return proto.byteValue();
263 public static List<String> getProtocolNameList() {
264 List<String> protoList = new ArrayList<String>();
265 for (IPProtocols proto : IPProtocols.values()) {
266 protoList.add(proto.toString());