Ensure immutability of constants Collection

Change-Id: I9b26282ccabaef5e4ac81fb626abfa6e537d9333
diff --git a/utils/misc/src/main/java/org/onlab/packet/Ethernet.java b/utils/misc/src/main/java/org/onlab/packet/Ethernet.java
index 0b41457..01a4254 100644
--- a/utils/misc/src/main/java/org/onlab/packet/Ethernet.java
+++ b/utils/misc/src/main/java/org/onlab/packet/Ethernet.java
@@ -24,9 +24,10 @@
 import org.onlab.packet.ndp.RouterAdvertisement;
 import org.onlab.packet.ndp.RouterSolicitation;
 
+import com.google.common.collect.ImmutableMap;
+
 import java.nio.ByteBuffer;
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.Map;
 
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -60,15 +61,18 @@
 
     public static final short DATALAYER_ADDRESS_LENGTH = 6; // bytes
 
-    private static final Map<Short, Deserializer<? extends IPacket>> ETHERTYPE_DESERIALIZER_MAP =
-            new HashMap<>();
+    private static final Map<Short, Deserializer<? extends IPacket>> ETHERTYPE_DESERIALIZER_MAP;
 
     static {
+        ImmutableMap.Builder<Short, Deserializer<? extends IPacket>> builder =
+                ImmutableMap.builder();
+
        for (EthType.EtherType ethType : EthType.EtherType.values()) {
            if (ethType.deserializer() != null) {
-               ETHERTYPE_DESERIALIZER_MAP.put(ethType.ethType().toShort(), ethType.deserializer());
+               builder.put(ethType.ethType().toShort(), ethType.deserializer());
            }
        }
+       ETHERTYPE_DESERIALIZER_MAP = builder.build();
     }
 
     protected MacAddress destinationMACAddress;
diff --git a/utils/misc/src/main/java/org/onlab/packet/ICMP6.java b/utils/misc/src/main/java/org/onlab/packet/ICMP6.java
index 4e6139b..8b3f8c4 100644
--- a/utils/misc/src/main/java/org/onlab/packet/ICMP6.java
+++ b/utils/misc/src/main/java/org/onlab/packet/ICMP6.java
@@ -25,8 +25,9 @@
 import org.onlab.packet.ndp.RouterAdvertisement;
 import org.onlab.packet.ndp.RouterSolicitation;
 
+import com.google.common.collect.ImmutableMap;
+
 import java.nio.ByteBuffer;
-import java.util.HashMap;
 import java.util.Map;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
@@ -101,15 +102,13 @@
     public static final byte IPV6_OPT_ERR = (byte) 0x01;
 
     public static final Map<Byte, Deserializer<? extends IPacket>> TYPE_DESERIALIZER_MAP =
-            new HashMap<>();
-
-    static {
-        ICMP6.TYPE_DESERIALIZER_MAP.put(ICMP6.ROUTER_SOLICITATION, RouterSolicitation.deserializer());
-        ICMP6.TYPE_DESERIALIZER_MAP.put(ICMP6.ROUTER_ADVERTISEMENT, RouterAdvertisement.deserializer());
-        ICMP6.TYPE_DESERIALIZER_MAP.put(ICMP6.NEIGHBOR_SOLICITATION, NeighborSolicitation.deserializer());
-        ICMP6.TYPE_DESERIALIZER_MAP.put(ICMP6.NEIGHBOR_ADVERTISEMENT, NeighborAdvertisement.deserializer());
-        ICMP6.TYPE_DESERIALIZER_MAP.put(ICMP6.REDIRECT, Redirect.deserializer());
-    }
+            ImmutableMap.<Byte, Deserializer<? extends IPacket>>builder()
+                .put(ICMP6.ROUTER_SOLICITATION, RouterSolicitation.deserializer())
+                .put(ICMP6.ROUTER_ADVERTISEMENT, RouterAdvertisement.deserializer())
+                .put(ICMP6.NEIGHBOR_SOLICITATION, NeighborSolicitation.deserializer())
+                .put(ICMP6.NEIGHBOR_ADVERTISEMENT, NeighborAdvertisement.deserializer())
+                .put(ICMP6.REDIRECT, Redirect.deserializer())
+                .build();
 
     protected byte icmpType;
     protected byte icmpCode;
diff --git a/utils/misc/src/main/java/org/onlab/packet/IPv4.java b/utils/misc/src/main/java/org/onlab/packet/IPv4.java
index 95695dc..b79a947 100644
--- a/utils/misc/src/main/java/org/onlab/packet/IPv4.java
+++ b/utils/misc/src/main/java/org/onlab/packet/IPv4.java
@@ -19,9 +19,10 @@
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 import java.util.Collection;
-import java.util.HashMap;
 import java.util.Map;
 
+import com.google.common.collect.ImmutableMap;
+
 import static com.google.common.base.MoreObjects.toStringHelper;
 import static org.onlab.packet.PacketUtils.*;
 
@@ -35,15 +36,14 @@
     public static final byte PROTOCOL_UDP = 0x11;
     public static final byte PROTOCOL_PIM = 0x67;
     public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP =
-            new HashMap<>();
+            ImmutableMap.<Byte, Deserializer<? extends IPacket>>builder()
+                .put(IPv4.PROTOCOL_ICMP, ICMP.deserializer())
+                .put(IPv4.PROTOCOL_IGMP, IGMP.deserializer())
+                .put(IPv4.PROTOCOL_TCP, TCP.deserializer())
+                .put(IPv4.PROTOCOL_UDP, UDP.deserializer())
+                .put(IPv4.PROTOCOL_PIM, PIM.deserializer())
+                .build();
 
-    static {
-        IPv4.PROTOCOL_DESERIALIZER_MAP.put(IPv4.PROTOCOL_ICMP, ICMP.deserializer());
-        IPv4.PROTOCOL_DESERIALIZER_MAP.put(IPv4.PROTOCOL_IGMP, IGMP.deserializer());
-        IPv4.PROTOCOL_DESERIALIZER_MAP.put(IPv4.PROTOCOL_TCP, TCP.deserializer());
-        IPv4.PROTOCOL_DESERIALIZER_MAP.put(IPv4.PROTOCOL_UDP, UDP.deserializer());
-        IPv4.PROTOCOL_DESERIALIZER_MAP.put(IPv4.PROTOCOL_PIM, PIM.deserializer());
-    }
 
     private static final byte DSCP_MASK = 0x3f;
     private static final byte DSCP_OFFSET = 2;
diff --git a/utils/misc/src/main/java/org/onlab/packet/IPv6.java b/utils/misc/src/main/java/org/onlab/packet/IPv6.java
index d1eb4f0..8c83efd 100644
--- a/utils/misc/src/main/java/org/onlab/packet/IPv6.java
+++ b/utils/misc/src/main/java/org/onlab/packet/IPv6.java
@@ -24,9 +24,10 @@
 import org.onlab.packet.ipv6.IExtensionHeader;
 import org.onlab.packet.ipv6.Routing;
 
+import com.google.common.collect.ImmutableMap;
+
 import java.nio.ByteBuffer;
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.Map;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
@@ -53,19 +54,17 @@
     public static final byte LINK_LOCAL_1 = (byte) 0x80;
 
     public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP =
-            new HashMap<>();
-
-    static {
-        IPv6.PROTOCOL_DESERIALIZER_MAP.put(IPv6.PROTOCOL_ICMP6, ICMP6.deserializer());
-        IPv6.PROTOCOL_DESERIALIZER_MAP.put(IPv6.PROTOCOL_TCP, TCP.deserializer());
-        IPv6.PROTOCOL_DESERIALIZER_MAP.put(IPv6.PROTOCOL_UDP, UDP.deserializer());
-        IPv6.PROTOCOL_DESERIALIZER_MAP.put(IPv6.PROTOCOL_HOPOPT, HopByHopOptions.deserializer());
-        IPv6.PROTOCOL_DESERIALIZER_MAP.put(IPv6.PROTOCOL_ROUTING, Routing.deserializer());
-        IPv6.PROTOCOL_DESERIALIZER_MAP.put(IPv6.PROTOCOL_FRAG, Fragment.deserializer());
-        IPv6.PROTOCOL_DESERIALIZER_MAP.put(IPv6.PROTOCOL_ESP, EncapSecurityPayload.deserializer());
-        IPv6.PROTOCOL_DESERIALIZER_MAP.put(IPv6.PROTOCOL_AH, Authentication.deserializer());
-        IPv6.PROTOCOL_DESERIALIZER_MAP.put(IPv6.PROTOCOL_DSTOPT, DestinationOptions.deserializer());
-    }
+            ImmutableMap.<Byte, Deserializer<? extends IPacket>>builder()
+                .put(IPv6.PROTOCOL_ICMP6, ICMP6.deserializer())
+                .put(IPv6.PROTOCOL_TCP, TCP.deserializer())
+                .put(IPv6.PROTOCOL_UDP, UDP.deserializer())
+                .put(IPv6.PROTOCOL_HOPOPT, HopByHopOptions.deserializer())
+                .put(IPv6.PROTOCOL_ROUTING, Routing.deserializer())
+                .put(IPv6.PROTOCOL_FRAG, Fragment.deserializer())
+                .put(IPv6.PROTOCOL_ESP, EncapSecurityPayload.deserializer())
+                .put(IPv6.PROTOCOL_AH, Authentication.deserializer())
+                .put(IPv6.PROTOCOL_DSTOPT, DestinationOptions.deserializer())
+                .build();
 
     protected byte version;
     protected byte trafficClass;
diff --git a/utils/misc/src/main/java/org/onlab/packet/MPLS.java b/utils/misc/src/main/java/org/onlab/packet/MPLS.java
index f6ca734..3a6f71d 100644
--- a/utils/misc/src/main/java/org/onlab/packet/MPLS.java
+++ b/utils/misc/src/main/java/org/onlab/packet/MPLS.java
@@ -16,9 +16,10 @@
 package org.onlab.packet;
 
 import java.nio.ByteBuffer;
-import java.util.HashMap;
 import java.util.Map;
 
+import com.google.common.collect.ImmutableMap;
+
 import static com.google.common.base.MoreObjects.toStringHelper;
 import static org.onlab.packet.PacketUtils.checkInput;
 
@@ -30,13 +31,12 @@
 
     public static final byte PROTOCOL_IPV4 = 0x1;
     public static final byte PROTOCOL_MPLS = 0x6;
-    static Map<Byte, Deserializer<? extends IPacket>> protocolDeserializerMap
-            = new HashMap<>();
-
-    static {
-        protocolDeserializerMap.put(PROTOCOL_IPV4, IPv4.deserializer());
-        protocolDeserializerMap.put(PROTOCOL_MPLS, MPLS.deserializer());
-    }
+    // mutable for Testing
+    static Map<Byte, Deserializer<? extends IPacket>> protocolDeserializerMap =
+            ImmutableMap.<Byte, Deserializer<? extends IPacket>>builder()
+                .put(PROTOCOL_IPV4, IPv4.deserializer())
+                .put(PROTOCOL_MPLS, MPLS.deserializer())
+                .build();
 
     protected int label; //20bits
     protected byte bos; //1bit
diff --git a/utils/misc/src/main/java/org/onlab/packet/MacAddress.java b/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
index 90c2a8a..1027708 100644
--- a/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
+++ b/utils/misc/src/main/java/org/onlab/packet/MacAddress.java
@@ -15,8 +15,7 @@
  */
 package org.onlab.packet;
 
-import com.google.common.collect.Sets;
-
+import com.google.common.collect.ImmutableSet;
 import java.util.Arrays;
 import java.util.Set;
 
@@ -56,7 +55,7 @@
     /**
      * A set of LLDP MAC addresses.
      */
-    public static final Set<MacAddress> LLDP = Sets.newHashSet(
+    public static final Set<MacAddress> LLDP = ImmutableSet.of(
             MacAddress.valueOf("01:80:c2:00:00:00"),
             MacAddress.valueOf("01:80:c2:00:00:03"),
             MacAddress.valueOf("01:80:c2:00:00:0e"));
diff --git a/utils/misc/src/main/java/org/onlab/packet/PIM.java b/utils/misc/src/main/java/org/onlab/packet/PIM.java
index 94590df..5262ea4 100644
--- a/utils/misc/src/main/java/org/onlab/packet/PIM.java
+++ b/utils/misc/src/main/java/org/onlab/packet/PIM.java
@@ -18,8 +18,9 @@
 import org.onlab.packet.pim.PIMHello;
 import org.onlab.packet.pim.PIMJoinPrune;
 
+import com.google.common.collect.ImmutableMap;
+
 import java.nio.ByteBuffer;
-import java.util.HashMap;
 import java.util.Map;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
@@ -48,12 +49,10 @@
     public static final byte ADDRESS_FAMILY_IP6 = 0x2;
 
     public static final Map<Byte, Deserializer<? extends IPacket>> PROTOCOL_DESERIALIZER_MAP =
-            new HashMap<>();
-
-    static {
-        PIM.PROTOCOL_DESERIALIZER_MAP.put(PIM.TYPE_HELLO, PIMHello.deserializer());
-        PIM.PROTOCOL_DESERIALIZER_MAP.put(PIM.TYPE_JOIN_PRUNE_REQUEST, PIMJoinPrune.deserializer());
-    }
+            ImmutableMap.<Byte, Deserializer<? extends IPacket>>builder()
+                .put(PIM.TYPE_HELLO, PIMHello.deserializer())
+                .put(PIM.TYPE_JOIN_PRUNE_REQUEST, PIMJoinPrune.deserializer())
+                .build();
 
     /*
      * PIM Header fields
diff --git a/utils/misc/src/main/java/org/onlab/packet/UDP.java b/utils/misc/src/main/java/org/onlab/packet/UDP.java
index f62e177..edf99af 100644
--- a/utils/misc/src/main/java/org/onlab/packet/UDP.java
+++ b/utils/misc/src/main/java/org/onlab/packet/UDP.java
@@ -19,9 +19,10 @@
 package org.onlab.packet;
 
 import java.nio.ByteBuffer;
-import java.util.HashMap;
 import java.util.Map;
 
+import com.google.common.collect.ImmutableMap;
+
 import static com.google.common.base.MoreObjects.toStringHelper;
 import static org.onlab.packet.PacketUtils.*;
 
@@ -30,22 +31,16 @@
  */
 public class UDP extends BasePacket {
     public static final Map<Integer, Deserializer<? extends IPacket>> PORT_DESERIALIZER_MAP =
-            new HashMap<>();
+            ImmutableMap.<Integer, Deserializer<? extends IPacket>>builder()
+                .put(UDP.DHCP_SERVER_PORT, DHCP.deserializer())
+                .put(UDP.DHCP_CLIENT_PORT, DHCP.deserializer())
+                .build();
+
     public static final int DHCP_SERVER_PORT = 67;
     public static final int DHCP_CLIENT_PORT = 68;
 
     private static final short UDP_HEADER_LENGTH = 8;
 
-    static {
-        /*
-         * Disable DHCP until the deserialize code is hardened to deal with
-         * garbage input
-         */
-        UDP.PORT_DESERIALIZER_MAP.put(UDP.DHCP_SERVER_PORT, DHCP.deserializer());
-        UDP.PORT_DESERIALIZER_MAP.put(UDP.DHCP_CLIENT_PORT, DHCP.deserializer());
-
-    }
-
     protected int sourcePort;
     protected int destinationPort;
     protected short length;