[ONOS-6641] VXLAN packet support

Change-Id: I7d2086cded3edafa26219609e4f3458b0ea394c0
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 6849654..0d7ff37 100644
--- a/utils/misc/src/main/java/org/onlab/packet/UDP.java
+++ b/utils/misc/src/main/java/org/onlab/packet/UDP.java
@@ -34,12 +34,14 @@
                     .put(UDP.DHCP_CLIENT_PORT, DHCP.deserializer())
                     .put(UDP.DHCP_V6_SERVER_PORT, DHCP6.deserializer())
                     .put(UDP.DHCP_V6_CLIENT_PORT, DHCP6.deserializer())
+                    .put(UDP.VXLAN_UDP_PORT, VXLAN.deserializer())
                     .build();
 
     public static final int DHCP_SERVER_PORT = 67;
     public static final int DHCP_CLIENT_PORT = 68;
     public static final int DHCP_V6_SERVER_PORT = 547;
     public static final int DHCP_V6_CLIENT_PORT = 546;
+    public static final int VXLAN_UDP_PORT = 4789;
 
     private static final short UDP_HEADER_LENGTH = 8;
 
diff --git a/utils/misc/src/main/java/org/onlab/packet/VXLAN.java b/utils/misc/src/main/java/org/onlab/packet/VXLAN.java
new file mode 100644
index 0000000..1287a7f
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/packet/VXLAN.java
@@ -0,0 +1,196 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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 com.google.common.base.MoreObjects.toStringHelper;
+import static org.onlab.packet.PacketUtils.checkInput;
+
+/**
+ * Representation of a VXLAN(Virtual eXtensible Local Area Network) packet.
+ */
+public class VXLAN extends BasePacket {
+
+    private static final short VXLAN_HEADER_LENGTH = 8;
+    private static final int BYTE_SHIFT = 8;
+    private static final int BYTE_MASK = 0xff;
+
+    protected byte flags = 0;
+    protected byte[] rsvd1 = new byte[] {0, 0, 0}; // reserved filed
+    protected byte[] vni = new byte[] {0, 0, 0};
+    protected byte rsvd2 = 0; // reserved field
+
+    /**
+     * Serializes the packet.
+     */
+    @Override
+    public byte[] serialize() {
+
+        byte[] payloadData = null;
+        if (this.payload != null) {
+            this.payload.setParent(this);
+            payloadData = this.payload.serialize();
+        }
+
+        int length = VXLAN_HEADER_LENGTH + (payloadData == null ? 0 : payloadData.length);
+
+        final byte[] data = new byte[length];
+        final ByteBuffer bb = ByteBuffer.wrap(data);
+        bb.put(this.flags);
+        bb.put(this.rsvd1);
+        bb.put(this.vni);
+        bb.put(this.rsvd2);
+        if (payloadData != null) {
+            bb.put(payloadData);
+        }
+
+        return data;
+    }
+
+    @Override
+    public IPacket deserialize(byte[] data, int offset, int length) {
+
+        final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+
+        if (bb.remaining() < VXLAN_HEADER_LENGTH) {
+            return this;
+        }
+
+        this.flags = bb.get();
+        bb.get(this.rsvd1);
+        bb.get(this.vni);
+        this.rsvd2 = bb.get();
+
+        Deserializer<? extends IPacket> deserializer = Data.deserializer();
+
+        try {
+            this.payload = deserializer.deserialize(data, bb.position(),
+                    bb.limit() - bb.position());
+            this.payload.setParent(this);
+        } catch (DeserializationException e) {
+            return this;
+        }
+        return this;
+    }
+
+    /**
+     * Returns VNI(VXLAN Network Identifier).
+     *
+     * @return the VNI
+     */
+    public int getVni() {
+        return (vni[0] << (BYTE_SHIFT * 2)) + (vni[1] << BYTE_SHIFT) + vni[2];
+    }
+
+    /**
+     * Set VNI.
+     *
+     * @param vni the VNI to set( 24 bits )
+     * @return this
+     */
+    public VXLAN setVni(int vni) {
+        this.vni[0] = (byte) ((vni >> (BYTE_SHIFT * 2)) & BYTE_MASK);
+        this.vni[1] = (byte) ((vni >> BYTE_SHIFT) & BYTE_MASK);
+        this.vni[2] = (byte) (vni & BYTE_MASK);
+        return this;
+    }
+
+    /**
+     * Return flags.
+     *
+     * @return the flags
+     */
+    public byte getFlag() {
+        return this.flags;
+    }
+
+    /**
+     * Set flags.
+     *
+     * @param flags the flags to set( 8 bits )
+     * @return this
+     */
+    public VXLAN setFlag(byte flags) {
+        this.flags = flags;
+        return this;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 2521;
+        int result = super.hashCode();
+        result = prime * result + this.getVni();
+        return result;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (!(obj instanceof VXLAN)) {
+            return false;
+        }
+
+        final VXLAN other = (VXLAN) obj;
+        if (this.getVni() != other.getVni()) {
+            return false;
+        }
+        return true;
+    }
+
+
+    /**
+     * Returns the deserializer closure (used by upper layer deserializer).
+     *
+     * @return the deserializer closure
+     */
+    public static Deserializer<VXLAN> deserializer() {
+        return (data, offset, length) -> {
+            checkInput(data, offset, length, VXLAN_HEADER_LENGTH);
+
+            VXLAN vxlan = new VXLAN();
+
+            ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
+            vxlan.flags = bb.get();
+            bb.get(vxlan.rsvd1);
+            bb.get(vxlan.vni);
+            vxlan.rsvd2 = bb.get();
+
+            Deserializer<? extends IPacket> deserializer = Data.deserializer();
+
+            vxlan.payload = deserializer.deserialize(data, bb.position(),
+                    bb.limit() - bb.position());
+            vxlan.payload.setParent(vxlan);
+
+            return vxlan;
+        };
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(getClass())
+                .add("vni", Integer.toString(getVni()))
+                .add("flags", Byte.toString(getFlag()))
+                .toString();
+    }
+
+}
diff --git a/utils/misc/src/test/java/org/onlab/packet/VXLANTest.java b/utils/misc/src/test/java/org/onlab/packet/VXLANTest.java
new file mode 100644
index 0000000..e33ee4b
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/VXLANTest.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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 com.google.common.testing.EqualsTester;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Tests for class {@link VXLAN}.
+ */
+public class VXLANTest {
+
+    private static final byte[] BYTE_PACKET_VXLAN = {
+            (byte) 0x08, // flags (8 bits)
+            (byte) 0x00, (byte) 0x00, (byte) 0x00, // rsvd1 (24 bits)
+            (byte) 0x12, (byte) 0x34, (byte) 0x56, // vni (24 bits)
+            (byte) 0x00, // rsvd2 (8 bits)
+    };
+
+    private static Deserializer<VXLAN> deserializer;
+
+    private static final UDP UDP_HDR = new UDP();
+
+    private static final int TEST_UDP_SRCPORT = 0x50;
+    private static final int TEST_FLAGS = 0x08;
+    private static final int TEST_VNI1 = 0x123456;
+    private static final int TEST_VNI2 = 0x654321;
+
+
+    @BeforeClass
+    public static void setUpBeforeClass() throws Exception {
+        deserializer = VXLAN.deserializer();
+        UDP_HDR.setSourcePort(TEST_UDP_SRCPORT);
+        UDP_HDR.setDestinationPort(UDP.VXLAN_UDP_PORT);
+    }
+
+    /**
+     * Tests serialize and setters.
+     */
+    @Test
+    public void testSerialize() {
+        VXLAN vxlan = new VXLAN();
+        vxlan.setFlag((byte) TEST_FLAGS);
+        vxlan.setVni(TEST_VNI1);
+        vxlan.setParent(UDP_HDR);
+        assertArrayEquals("Serialized packet is not matched", BYTE_PACKET_VXLAN, vxlan.serialize());
+    }
+
+    /**
+     * Tests deserialize bad input.
+     */
+    @Test
+    public void testDeserializeBadInput() throws Exception {
+        PacketTestUtils.testDeserializeBadInput(deserializer);
+    }
+
+    /**
+     * Tests deserialize truncated.
+     */
+    @Test
+    public void testDeserializeTruncated() throws Exception {
+        PacketTestUtils.testDeserializeTruncated(deserializer, BYTE_PACKET_VXLAN);
+    }
+
+    /**
+     * Tests deserialize and getters.
+     */
+    @Test
+    public void testDeserialize() throws Exception {
+        VXLAN vxlan = deserializer.deserialize(BYTE_PACKET_VXLAN, 0, BYTE_PACKET_VXLAN.length);
+
+        assertThat(vxlan.getFlag(), is((byte) TEST_FLAGS));
+        assertThat(vxlan.getVni(), is(TEST_VNI1));
+    }
+
+    /**
+     * Tests comparator.
+     */
+    @Test
+    public void testEqual() {
+        VXLAN vxlan1 = new VXLAN();
+        vxlan1.setFlag((byte) TEST_FLAGS);
+        vxlan1.setVni(TEST_VNI1);
+
+        VXLAN vxlan2 = new VXLAN();
+        vxlan2.setFlag((byte) TEST_FLAGS);
+        vxlan2.setVni(TEST_VNI2);
+
+        new EqualsTester()
+                .addEqualityGroup(vxlan1, vxlan1)
+                .addEqualityGroup(vxlan2).testEquals();
+    }
+
+    /**
+     * Tests toString.
+     */
+    @Test
+    public void testToStringVXLAN() throws Exception {
+        VXLAN vxlan = deserializer.deserialize(BYTE_PACKET_VXLAN, 0, BYTE_PACKET_VXLAN.length);
+        String str = vxlan.toString();
+
+        assertTrue(StringUtils.contains(str, "flags=" + TEST_FLAGS));
+        assertTrue(StringUtils.contains(str, "vni=" + TEST_VNI1));
+    }
+}