Improves ping responder

Patch contains several bugfixes and improvements:
- Fixes sid retrieval when the destination leaf is down
- Fixes sid retrieval when ping goes through the spine
- Fixes MPLS deserializer
- Improves Ethernet toString
- Fixes ping to looback for dh host when bond sends to wrong leaf

Change-Id: I05963e74b2976e526826ffd377cadeb462ba0a8d
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 5fd07bf..fb249a9 100644
--- a/utils/misc/src/main/java/org/onlab/packet/Ethernet.java
+++ b/utils/misc/src/main/java/org/onlab/packet/Ethernet.java
@@ -514,13 +514,13 @@
      */
     @Override
     public String toString() {
-
         final StringBuilder sb = new StringBuilder("\n");
-
         final IPacket pkt = this.getPayload();
 
         if (pkt instanceof ARP) {
             sb.append("arp");
+        } else if (pkt instanceof MPLS) {
+            sb.append("mpls");
         } else if (pkt instanceof LLDP) {
             sb.append("lldp");
         } else if (pkt instanceof ICMP) {
@@ -530,11 +530,7 @@
         } else if (pkt instanceof DHCP) {
             sb.append("dhcp");
         } else {
-            /*
-             * When we don't know the protocol, we print using
-             * the well known hex format instead of a decimal
-             * value.
-             */
+            // Just print the ethertype
             sb.append(String.format(HEX_PROTO,
                                     Integer.toHexString(this.getEtherType() & 0xffff)));
         }
@@ -567,6 +563,12 @@
             sb.append("\nnw_dst: ");
             sb.append(IPv4.fromIPv4Address(IPv4.toIPv4Address(p
                     .getTargetProtocolAddress())));
+        } else if (pkt instanceof MPLS) {
+            final MPLS p = (MPLS) pkt;
+            sb.append("\nmpls: ");
+            sb.append(this.etherType == MPLS_UNICAST ? "unicast" : "multicast");
+            sb.append("\nmpls_label: ");
+            sb.append(p.label);
         } else if (pkt instanceof LLDP) {
             sb.append("lldp packet");
         } else if (pkt instanceof ICMP) {
@@ -593,7 +595,6 @@
                     sb.append(((TCP) payload).getSourcePort());
                     sb.append("\ntp_dst: ");
                     sb.append(((TCP) payload).getDestinationPort());
-
                 } else if (payload instanceof UDP) {
                     sb.append("\ntp_src: ");
                     sb.append(((UDP) payload).getSourcePort());
@@ -705,7 +706,6 @@
         } else {
             sb.append("\nunknown packet");
         }
-
         return sb.toString();
     }
 
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 948075b..07ead0c 100644
--- a/utils/misc/src/main/java/org/onlab/packet/MPLS.java
+++ b/utils/misc/src/main/java/org/onlab/packet/MPLS.java
@@ -30,13 +30,15 @@
     public static final int HEADER_LENGTH = 4;
 
     public static final byte PROTOCOL_IPV4 = 0x1;
+    public static final byte PROTOCOL_IPV6 = 0x2;
     public static final byte PROTOCOL_MPLS = 0x6;
     // 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();
+                    .put(PROTOCOL_IPV6, IPv6.deserializer())
+                    .put(PROTOCOL_IPV4, IPv4.deserializer())
+                    .put(PROTOCOL_MPLS, MPLS.deserializer())
+                    .build();
 
     protected int label; //20bits
     protected byte bos; //1bit
@@ -108,6 +110,22 @@
         this.ttl = ttl;
     }
 
+    @Override
+    public IPacket setPayload(final IPacket payload) {
+        // We implicitly assume that traffic can be only of these three types
+        if (payload instanceof MPLS) {
+            this.bos = 0;
+            this.protocol = PROTOCOL_MPLS;
+        } else if (payload instanceof IPv6) {
+            this.bos = 1;
+            this.protocol = PROTOCOL_IPV6;
+        } else {
+            this.bos = 1;
+            this.protocol = PROTOCOL_IPV4;
+        }
+        return super.setPayload(payload);
+    }
+
     /**
      * Deserializer function for MPLS packets.
      *
@@ -124,7 +142,11 @@
             mpls.label = ((mplsheader & 0xfffff000) >>> 12);
             mpls.bos = (byte) ((mplsheader & 0x00000100) >> 8);
             mpls.ttl = (byte) (mplsheader & 0x000000ff);
-            mpls.protocol = (mpls.bos == 1) ? PROTOCOL_IPV4 : PROTOCOL_MPLS;
+
+            ByteBuffer duplicate = bb.duplicate();
+            short protocol = (short) ((duplicate.get() & 0xf0) >> 4);
+            mpls.protocol = (mpls.bos == 1) ? protocol == 4 ?
+                    PROTOCOL_IPV4 : PROTOCOL_IPV6 : PROTOCOL_MPLS;
 
             Deserializer<? extends IPacket> deserializer;
             if (protocolDeserializerMap.containsKey(mpls.protocol)) {