Implements ICMPEcho packet type.
- For echo request and reply purpose

Change-Id: I5797bf41ad6f4afd2c7327b9bfa193aa90198ce4
diff --git a/utils/misc/src/main/java/org/onlab/packet/ICMP.java b/utils/misc/src/main/java/org/onlab/packet/ICMP.java
index 68fddcb..7a5364f 100644
--- a/utils/misc/src/main/java/org/onlab/packet/ICMP.java
+++ b/utils/misc/src/main/java/org/onlab/packet/ICMP.java
@@ -32,8 +32,14 @@
 
     public static final byte TYPE_ECHO_REQUEST = 0x08;
     public static final byte TYPE_ECHO_REPLY = 0x00;
+
+    //Uses CODE_ECHO_REPLY instead.
+    @Deprecated
     public static final byte SUBTYPE_ECHO_REPLY = 0x00;
 
+    public static final byte CODE_ECHO_REPLY = 0x00;
+    public static final byte CODE_ECHO_REQEUST = 0x00;
+
     public static final short ICMP_HEADER_LENGTH = 4;
 
     /**
@@ -194,10 +200,20 @@
             icmp.icmpCode = bb.get();
             icmp.checksum = bb.getShort();
 
-            icmp.payload = Data.deserializer()
-                    .deserialize(data, bb.position(), bb.limit()
-                            - bb.position());
+            switch (icmp.icmpType) {
+                case ICMP.TYPE_ECHO_REQUEST:
+                case ICMP.TYPE_ECHO_REPLY:
+                    Deserializer<ICMPEcho> deserializer = ICMPEcho.deserializer();
+                    icmp.payload = deserializer.deserialize(data, bb.position(), ICMPEcho.ICMP_ECHO_HEADER_LENGTH);
+                    break;
+                default:
+                    icmp.payload = Data.deserializer()
+                            .deserialize(data, bb.position(), bb.limit()
+                                    - bb.position());
+            }
+
             icmp.payload.setParent(icmp);
+
             return icmp;
         };
     }
diff --git a/utils/misc/src/main/java/org/onlab/packet/ICMPEcho.java b/utils/misc/src/main/java/org/onlab/packet/ICMPEcho.java
new file mode 100644
index 0000000..aa06cc6
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/packet/ICMPEcho.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import java.nio.ByteBuffer;
+
+import static org.onlab.packet.PacketUtils.checkInput;
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * ICMP packet class for echo purpose.
+ */
+public class ICMPEcho extends BasePacket {
+    private short identifier;
+    private short sequenceNum;
+
+    public static final short ICMP_ECHO_HEADER_LENGTH = 4;
+
+    /**
+     * Sets the identifier.
+     *
+     * @param identifier identifier
+     * @return this
+     */
+    public ICMPEcho setIdentifier(final short identifier) {
+        this.identifier = identifier;
+        return this;
+    }
+
+    /**
+     * Gets the identifier.
+     *
+     * @return identifier
+     */
+    public short getIdentifier() {
+        return this.identifier;
+    }
+
+    /**
+     * Sets the sequencer number.
+     *
+     * @param sequenceNum sequence number
+     * @return this
+     */
+    public ICMPEcho setSequenceNum(final short sequenceNum) {
+        this.sequenceNum = sequenceNum;
+        return this;
+    }
+
+    /**
+     * Gets the sequence number.
+     *
+     * @return sequence number
+     */
+    public short getSequenceNum() {
+        return this.sequenceNum;
+    }
+
+    /**
+     * Serializes the packet. Will compute and set the following fields if they
+     * are set to specific values at the time serialize is called: -checksum : 0
+     * -length : 0
+     */
+    @Override
+    public byte[] serialize() {
+        checkNotNull(this.identifier);
+        checkNotNull(this.sequenceNum);
+
+        int length = ICMP_ECHO_HEADER_LENGTH;
+        byte[] payloadData = null;
+        if (this.payload != null) {
+            this.payload.setParent(this);
+            payloadData = this.payload.serialize();
+            length += payloadData.length;
+        }
+
+        final byte[] data = new byte[length];
+        final ByteBuffer bb = ByteBuffer.wrap(data);
+
+        bb.putShort(this.identifier);
+        bb.putShort(this.sequenceNum);
+        if (payloadData != null) {
+            bb.put(payloadData);
+        }
+        return data;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 5807;
+        int result = super.hashCode();
+        result = prime * result + this.identifier;
+        result = prime * result + this.sequenceNum;
+        return result;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (!(obj instanceof ICMPEcho)) {
+            return false;
+        }
+        final ICMPEcho other = (ICMPEcho) obj;
+
+        if (this.identifier != other.identifier) {
+            return false;
+        }
+        if (this.sequenceNum != other.sequenceNum) {
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Deserializer function for ICMPEcho packets.
+     *
+     * @return deserializer function
+     */
+    public static Deserializer<ICMPEcho> deserializer() {
+        return (data, offset, length) -> {
+            checkInput(data, offset, length, ICMP_ECHO_HEADER_LENGTH);
+
+            ICMPEcho icmp = new ICMPEcho();
+
+            final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+
+            icmp.identifier = bb.getShort();
+            icmp.sequenceNum = bb.getShort();
+
+            return icmp;
+        };
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(getClass())
+                .add("ICMP echo identifier", Short.toString(identifier))
+                .add("ICMP echo sequenceNumber", Short.toString(sequenceNum))
+                .toString();
+    }
+}
\ No newline at end of file
diff --git a/utils/misc/src/test/java/org/onlab/packet/ICMPEchoTest.java b/utils/misc/src/test/java/org/onlab/packet/ICMPEchoTest.java
new file mode 100644
index 0000000..b85f4c5
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/ICMPEchoTest.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onlab.packet;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.packet.Ethernet.TYPE_IPV4;
+
+/**
+ * Unit tests for the ICMPEcho class.
+ */
+public class ICMPEchoTest {
+    private Deserializer<ICMPEcho> deserializer;
+
+    protected byte icmpType = ICMP.TYPE_ECHO_REQUEST;
+    protected byte icmpCode = ICMP.CODE_ECHO_REQEUST;
+    protected short checksum = 870;
+    private short identifier = 1;
+    private short sequenceNum = 2;
+    private MacAddress srcMac = MacAddress.valueOf("11:22:33:44:55:66");
+    private IpAddress srcIp = IpAddress.valueOf("10.0.0.3");
+    private MacAddress dstMac = MacAddress.valueOf("66:55:44:33:22:11");
+    private IpAddress dstIp = IpAddress.valueOf("10.0.0.1");
+
+    private byte[] headerBytes;
+
+    /**
+     * Initial setup for this unit test.
+     */
+    @Before
+    public void setUp() throws Exception {
+        deserializer = ICMPEcho.deserializer();
+
+        ByteBuffer bb = ByteBuffer.allocate(ICMPEcho.ICMP_ECHO_HEADER_LENGTH);
+
+        bb.putShort(identifier);
+        bb.putShort(sequenceNum);
+
+        headerBytes = bb.array();
+    }
+
+
+    /**
+     * Tests the deserializeBadInput.
+     *
+     * @throws Exception exception
+     */
+    @Test
+    public void testDeserializeBadInput() throws Exception {
+        PacketTestUtils.testDeserializeBadInput(deserializer);
+    }
+
+    /**
+     * Tests the deserialzeTruncates.
+     *
+     * @throws Exception exception
+     */
+    @Test
+    public void testDeserializeTruncated() throws Exception {
+        PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes);
+    }
+
+    /**
+     * Tests deserialize, serialize, getters and setters.
+     *
+     * @throws Exception exception
+     */
+    @Test
+    public void testIcmpEchoPacket() throws Exception {
+        ICMPEcho icmpEcho = new ICMPEcho();
+
+        icmpEcho.setIdentifier(identifier);
+        icmpEcho.setSequenceNum(sequenceNum);
+
+        ByteBuffer bb = ByteBuffer.wrap(icmpEcho.serialize());
+
+        ICMPEcho deserializedIcmpEcho = deserializer.deserialize(bb.array(), 0, bb.array().length);
+
+        assertTrue(deserializedIcmpEcho.equals(icmpEcho));
+
+        assertEquals(deserializedIcmpEcho.getIdentifier(), icmpEcho.getIdentifier());
+        assertEquals(deserializedIcmpEcho.getSequenceNum(), icmpEcho.getSequenceNum());
+    }
+
+    @Test
+    public void testDeserializeFromIpPacket() throws Exception {
+        ICMPEcho icmpEcho = new ICMPEcho();
+        icmpEcho.setIdentifier(identifier)
+                .setSequenceNum(sequenceNum);
+        ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize());
+
+        ICMP icmp = new ICMP();
+        icmp.setIcmpType(icmpType)
+                .setIcmpCode(icmpCode)
+                .setChecksum(checksum);
+
+        icmp.setPayload(ICMPEcho.deserializer().deserialize(byteBufferIcmpEcho.array(),
+                0, ICMPEcho.ICMP_ECHO_HEADER_LENGTH));
+        ByteBuffer byteBufferIcmp = ByteBuffer.wrap(icmp.serialize());
+
+
+        IPv4 iPacket = new IPv4();
+        iPacket.setDestinationAddress(dstIp.toString());
+        iPacket.setSourceAddress(srcIp.toString());
+        iPacket.setTtl((byte) 64);
+        iPacket.setChecksum((short) 0);
+        iPacket.setDiffServ((byte) 0);
+        iPacket.setProtocol(IPv4.PROTOCOL_ICMP);
+
+        iPacket.setPayload(ICMP.deserializer().deserialize(byteBufferIcmp.array(),
+                0,
+                byteBufferIcmp.array().length));
+
+        Ethernet ethPacket = new Ethernet();
+
+        ethPacket.setEtherType(TYPE_IPV4);
+        ethPacket.setSourceMACAddress(srcMac);
+        ethPacket.setDestinationMACAddress(dstMac);
+        ethPacket.setPayload(iPacket);
+
+        validatePacket(ethPacket);
+    }
+
+    private void validatePacket(Ethernet ethPacket) {
+        ICMP icmp = (ICMP) ethPacket.getPayload().getPayload();
+        ICMPEcho icmpEcho = (ICMPEcho) icmp.getPayload();
+
+        assertEquals(icmp.getIcmpType(), icmpType);
+        assertEquals(icmp.getIcmpCode(), icmpCode);
+        assertEquals(icmp.getChecksum(), checksum);
+        assertEquals(icmpEcho.getIdentifier(), identifier);
+        assertEquals(icmpEcho.getSequenceNum(), sequenceNum);
+    }
+
+}
diff --git a/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java b/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java
index 4d215b4..24e9b19 100644
--- a/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java
+++ b/utils/misc/src/test/java/org/onlab/packet/ICMPTest.java
@@ -40,13 +40,21 @@
 
     @Before
     public void setUp() throws Exception {
+        ICMPEcho icmpEcho = new ICMPEcho();
+        icmpEcho.setIdentifier((short) 0)
+                .setSequenceNum((short) 0);
+
+        ByteBuffer byteBufferIcmpEcho = ByteBuffer.wrap(icmpEcho.serialize());
+
         deserializer = ICMP.deserializer();
 
-        ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH);
+        ByteBuffer bb = ByteBuffer.allocate(ICMP.ICMP_HEADER_LENGTH + ICMPEcho.ICMP_ECHO_HEADER_LENGTH);
 
         bb.put(icmpType);
         bb.put(icmpCode);
         bb.putShort(checksum);
+        bb.put(byteBufferIcmpEcho);
+
 
         headerBytes = bb.array();
     }