From a75526349c558da203503fe9ee4a6e4432352523 Mon Sep 17 00:00:00 2001
From: Ryan Izard <rizard@g.clemson.edu>
Date: Wed, 27 Aug 2014 13:15:58 -0400
Subject: [PATCH] Refactor IPv4 and UDP payload's protocol class map the proper
 way.

---
 .../net/floodlightcontroller/packet/IPv4.java | 17 +++++++----------
 .../net/floodlightcontroller/packet/UDP.java  | 19 +++++++++----------
 .../floodlightcontroller/packet/DHCPTest.java |  4 ++--
 3 files changed, 18 insertions(+), 22 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/packet/IPv4.java b/src/main/java/net/floodlightcontroller/packet/IPv4.java
index 54effbc64..83cc54263 100644
--- a/src/main/java/net/floodlightcontroller/packet/IPv4.java
+++ b/src/main/java/net/floodlightcontroller/packet/IPv4.java
@@ -35,16 +35,13 @@ import org.projectfloodlight.openflow.types.U8;
  *
  */
 public class IPv4 extends BasePacket {
-    public static final byte PROTOCOL_ICMP = 0x1;
-    public static final byte PROTOCOL_TCP = 0x6;
-    public static final byte PROTOCOL_UDP = 0x11;
-    public static Map<Byte, Class<? extends IPacket>> protocolClassMap;
+    public static Map<IpProtocol, Class<? extends IPacket>> protocolClassMap;
 
     static {
-        protocolClassMap = new HashMap<Byte, Class<? extends IPacket>>();
-        protocolClassMap.put(PROTOCOL_ICMP, ICMP.class);
-        protocolClassMap.put(PROTOCOL_TCP, TCP.class);
-        protocolClassMap.put(PROTOCOL_UDP, UDP.class);
+        protocolClassMap = new HashMap<IpProtocol, Class<? extends IPacket>>();
+        protocolClassMap.put(IpProtocol.ICMP, ICMP.class);
+        protocolClassMap.put(IpProtocol.TCP, TCP.class);
+        protocolClassMap.put(IpProtocol.UDP, UDP.class);
     }
 
     public static final byte IPV4_FLAGS_MOREFRAG = 0x1;
@@ -411,8 +408,8 @@ public class IPv4 extends BasePacket {
         isFragment = ((this.flags & IPV4_FLAGS_DONTFRAG) == 0) &&
                 ((this.flags & IPV4_FLAGS_MOREFRAG) != 0 ||
                 this.fragmentOffset != 0);
-        if (!isFragment && IPv4.protocolClassMap.containsKey((byte)this.protocol.getIpProtocolNumber())) {
-            Class<? extends IPacket> clazz = IPv4.protocolClassMap.get((byte)this.protocol.getIpProtocolNumber());
+        if (!isFragment && IPv4.protocolClassMap.containsKey(this.protocol)) {
+            Class<? extends IPacket> clazz = IPv4.protocolClassMap.get(this.protocol);
             try {
                 payload = clazz.newInstance();
             } catch (Exception e) {
diff --git a/src/main/java/net/floodlightcontroller/packet/UDP.java b/src/main/java/net/floodlightcontroller/packet/UDP.java
index 8639cb92f..a5ee35f2c 100644
--- a/src/main/java/net/floodlightcontroller/packet/UDP.java
+++ b/src/main/java/net/floodlightcontroller/packet/UDP.java
@@ -29,17 +29,16 @@ import org.projectfloodlight.openflow.types.TransportPort;
  * @author David Erickson (daviderickson@cs.stanford.edu)
  */
 public class UDP extends BasePacket {
-    public static Map<Short, Class<? extends IPacket>> decodeMap;
-    public static short DHCP_SERVER_PORT = (short)67;
-    public static short DHCP_CLIENT_PORT = (short)68;
-
+    public static Map<TransportPort, Class<? extends IPacket>> decodeMap;
+    public static final TransportPort DHCP_CLIENT_PORT = TransportPort.of(68);
+    public static final TransportPort DHCP_SERVER_PORT = TransportPort.of(67);
     static {
-        decodeMap = new HashMap<Short, Class<? extends IPacket>>();
+        decodeMap = new HashMap<TransportPort, Class<? extends IPacket>>();
         /*
          * Disable DHCP until the deserialize code is hardened to deal with garbage input
          */
-        UDP.decodeMap.put(DHCP_SERVER_PORT, DHCP.class);
         UDP.decodeMap.put(DHCP_CLIENT_PORT, DHCP.class);
+        UDP.decodeMap.put(DHCP_SERVER_PORT, DHCP.class);
         
     }
 
@@ -229,15 +228,15 @@ public class UDP extends BasePacket {
         this.length = bb.getShort();
         this.checksum = bb.getShort();
 
-        if (UDP.decodeMap.containsKey((short)this.destinationPort.getPort())) {
+        if (UDP.decodeMap.containsKey(this.destinationPort)) {
             try {
-                this.payload = UDP.decodeMap.get((short)this.destinationPort.getPort()).getConstructor().newInstance();
+                this.payload = UDP.decodeMap.get(this.destinationPort).getConstructor().newInstance();
             } catch (Exception e) {
                 throw new RuntimeException("Failure instantiating class", e);
             }
-        } else if (UDP.decodeMap.containsKey((short)this.sourcePort.getPort())) {
+        } else if (UDP.decodeMap.containsKey(this.sourcePort)) {
             try {
-                this.payload = UDP.decodeMap.get((short)this.sourcePort.getPort()).getConstructor().newInstance();
+                this.payload = UDP.decodeMap.get(this.sourcePort).getConstructor().newInstance();
             } catch (Exception e) {
                 throw new RuntimeException("Failure instantiating class", e);
             }
diff --git a/src/test/java/net/floodlightcontroller/packet/DHCPTest.java b/src/test/java/net/floodlightcontroller/packet/DHCPTest.java
index cecb7b1e9..b83ffa816 100644
--- a/src/test/java/net/floodlightcontroller/packet/DHCPTest.java
+++ b/src/test/java/net/floodlightcontroller/packet/DHCPTest.java
@@ -586,8 +586,8 @@ public class DHCPTest extends TestCase {
         assertTrue(udp.getPayload() instanceof DHCP);
         DHCP dhcp = (DHCP) udp.getPayload();
 
-        assertEquals(UDP.DHCP_CLIENT_PORT, udp.getSourcePort().getPort());
-        assertEquals(UDP.DHCP_SERVER_PORT, udp.getDestinationPort().getPort());
+        assertEquals(UDP.DHCP_CLIENT_PORT, udp.getSourcePort());
+        assertEquals(UDP.DHCP_SERVER_PORT, udp.getDestinationPort());
         
         // should get invalid opCode of 0
         assertEquals(0, dhcp.getOpCode());
-- 
GitLab