ONOS-2711 Replaced short to TpPort for tcp/udp ports

Change-Id: Ibf0474b5369d11d377fd33cf5ab48083cbca3308
diff --git a/utils/misc/src/main/java/org/onlab/packet/TCP.java b/utils/misc/src/main/java/org/onlab/packet/TCP.java
index b13b53c..8d45079 100644
--- a/utils/misc/src/main/java/org/onlab/packet/TCP.java
+++ b/utils/misc/src/main/java/org/onlab/packet/TCP.java
@@ -30,8 +30,8 @@
 
     private static final short TCP_HEADER_LENGTH = 20;
 
-    protected short sourcePort;
-    protected short destinationPort;
+    protected int sourcePort;
+    protected int destinationPort;
     protected int sequence;
     protected int acknowledge;
     protected byte dataOffset;
@@ -46,17 +46,17 @@
      *
      * @return TCP source port
      */
-    public short getSourcePort() {
+    public int getSourcePort() {
         return this.sourcePort;
     }
 
     /**
      * Sets TCP source port.
      *
-     * @param sourcePort the sourcePort to set
+     * @param sourcePort the sourcePort to set (unsigned 16 bits integer)
      * @return this
      */
-    public TCP setSourcePort(final short sourcePort) {
+    public TCP setSourcePort(final int sourcePort) {
         this.sourcePort = sourcePort;
         return this;
     }
@@ -66,17 +66,17 @@
      *
      * @return the destinationPort
      */
-    public short getDestinationPort() {
+    public int getDestinationPort() {
         return this.destinationPort;
     }
 
     /**
      * Sets TCP destination port.
      *
-     * @param destinationPort the destinationPort to set
+     * @param destinationPort the destinationPort to set (unsigned 16 bits integer)
      * @return this
      */
-    public TCP setDestinationPort(final short destinationPort) {
+    public TCP setDestinationPort(final int destinationPort) {
         this.destinationPort = destinationPort;
         return this;
     }
@@ -270,8 +270,8 @@
         final byte[] data = new byte[length];
         final ByteBuffer bb = ByteBuffer.wrap(data);
 
-        bb.putShort(this.sourcePort);
-        bb.putShort(this.destinationPort);
+        bb.putShort((short) (this.sourcePort & 0xffff));
+        bb.putShort((short) (this.destinationPort & 0xffff));
         bb.putInt(this.sequence);
         bb.putInt(this.acknowledge);
         bb.putShort((short) (this.flags | this.dataOffset << 12));
@@ -348,8 +348,8 @@
     public IPacket deserialize(final byte[] data, final int offset,
                                final int length) {
         final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
-        this.sourcePort = bb.getShort();
-        this.destinationPort = bb.getShort();
+        this.sourcePort = (bb.getShort() & 0xffff);
+        this.destinationPort = (bb.getShort() & 0xffff);
         this.sequence = bb.getInt();
         this.acknowledge = bb.getInt();
         this.flags = bb.getShort();
@@ -435,8 +435,8 @@
             TCP tcp = new TCP();
 
             final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
-            tcp.sourcePort = bb.getShort();
-            tcp.destinationPort = bb.getShort();
+            tcp.sourcePort = (bb.getShort() & 0xffff);
+            tcp.destinationPort = (bb.getShort() & 0xffff);
             tcp.sequence = bb.getInt();
             tcp.acknowledge = bb.getInt();
             tcp.flags = bb.getShort();
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 32432e6..66534eb 100644
--- a/utils/misc/src/main/java/org/onlab/packet/UDP.java
+++ b/utils/misc/src/main/java/org/onlab/packet/UDP.java
@@ -46,24 +46,24 @@
 
     }
 
-    protected short sourcePort;
-    protected short destinationPort;
+    protected int sourcePort;
+    protected int destinationPort;
     protected short length;
     protected short checksum;
 
     /**
      * @return the sourcePort
      */
-    public short getSourcePort() {
+    public int getSourcePort() {
         return this.sourcePort;
     }
 
     /**
      * @param sourcePort
-     *            the sourcePort to set
+     *            the sourcePort to set (16 bits unsigned integer)
      * @return this
      */
-    public UDP setSourcePort(final short sourcePort) {
+    public UDP setSourcePort(final int sourcePort) {
         this.sourcePort = sourcePort;
         return this;
     }
@@ -71,16 +71,16 @@
     /**
      * @return the destinationPort
      */
-    public short getDestinationPort() {
+    public int getDestinationPort() {
         return this.destinationPort;
     }
 
     /**
      * @param destinationPort
-     *            the destinationPort to set
+     *            the destinationPort to set (16 bits unsigned integer)
      * @return this
      */
-    public UDP setDestinationPort(final short destinationPort) {
+    public UDP setDestinationPort(final int destinationPort) {
         this.destinationPort = destinationPort;
         return this;
     }
@@ -134,8 +134,8 @@
         final byte[] data = new byte[this.length];
         final ByteBuffer bb = ByteBuffer.wrap(data);
 
-        bb.putShort(this.sourcePort);
-        bb.putShort(this.destinationPort);
+        bb.putShort((short) (this.sourcePort & 0xffff));
+        bb.putShort((short) (this.destinationPort & 0xffff));
         bb.putShort(this.length);
         bb.putShort(this.checksum);
         if (payloadData != null) {
@@ -200,8 +200,8 @@
     public IPacket deserialize(final byte[] data, final int offset,
                                final int length) {
         final ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
-        this.sourcePort = bb.getShort();
-        this.destinationPort = bb.getShort();
+        this.sourcePort = (bb.getShort() & 0xffff);
+        this.destinationPort = (bb.getShort() & 0xffff);
         this.length = bb.getShort();
         this.checksum = bb.getShort();
 
@@ -284,8 +284,8 @@
             UDP udp = new UDP();
 
             ByteBuffer bb = ByteBuffer.wrap(data, offset, length);
-            udp.sourcePort = bb.getShort();
-            udp.destinationPort = bb.getShort();
+            udp.sourcePort = (bb.getShort() & 0xffff);
+            udp.destinationPort = (bb.getShort() & 0xffff);
             udp.length = bb.getShort();
             udp.checksum = bb.getShort();
 
diff --git a/utils/misc/src/test/java/org/onlab/packet/TCPTest.java b/utils/misc/src/test/java/org/onlab/packet/TCPTest.java
index 18f532e..faab5c2 100644
--- a/utils/misc/src/test/java/org/onlab/packet/TCPTest.java
+++ b/utils/misc/src/test/java/org/onlab/packet/TCPTest.java
@@ -88,8 +88,8 @@
     @Test
     public void testSerialize() {
         TCP tcp = new TCP();
-        tcp.setSourcePort((short) 0x50);
-        tcp.setDestinationPort((short) 0x60);
+        tcp.setSourcePort(0x50);
+        tcp.setDestinationPort(0x60);
         tcp.setSequence(0x10);
         tcp.setAcknowledge(0x20);
         tcp.setDataOffset((byte) 0x5);
@@ -121,8 +121,8 @@
     public void testDeserialize() throws Exception {
         TCP tcp = deserializer.deserialize(bytePacketTCP4, 0, bytePacketTCP4.length);
 
-        assertThat(tcp.getSourcePort(), is((short) 0x50));
-        assertThat(tcp.getDestinationPort(), is((short) 0x60));
+        assertThat(tcp.getSourcePort(), is(0x50));
+        assertThat(tcp.getDestinationPort(), is(0x60));
         assertThat(tcp.getSequence(), is(0x10));
         assertThat(tcp.getAcknowledge(), is(0x20));
         assertThat(tcp.getDataOffset(), is((byte) 0x5));
@@ -138,8 +138,8 @@
     @Test
     public void testEqual() {
         TCP tcp1 = new TCP();
-        tcp1.setSourcePort((short) 0x50);
-        tcp1.setDestinationPort((short) 0x60);
+        tcp1.setSourcePort(0x50);
+        tcp1.setDestinationPort(0x60);
         tcp1.setSequence(0x10);
         tcp1.setAcknowledge(0x20);
         tcp1.setDataOffset((byte) 0x5);
@@ -148,8 +148,8 @@
         tcp1.setUrgentPointer((short) 0x1);
 
         TCP tcp2 = new TCP();
-        tcp2.setSourcePort((short) 0x70);
-        tcp2.setDestinationPort((short) 0x60);
+        tcp2.setSourcePort(0x70);
+        tcp2.setDestinationPort(0x60);
         tcp2.setSequence(0x10);
         tcp2.setAcknowledge(0x20);
         tcp2.setDataOffset((byte) 0x5);
diff --git a/utils/misc/src/test/java/org/onlab/packet/UDPTest.java b/utils/misc/src/test/java/org/onlab/packet/UDPTest.java
index 86363fa..ba453f6 100644
--- a/utils/misc/src/test/java/org/onlab/packet/UDPTest.java
+++ b/utils/misc/src/test/java/org/onlab/packet/UDPTest.java
@@ -82,8 +82,8 @@
     @Test
     public void testSerialize() {
         UDP udp = new UDP();
-        udp.setSourcePort((short) 0x50);
-        udp.setDestinationPort((short) 0x60);
+        udp.setSourcePort(0x50);
+        udp.setDestinationPort(0x60);
 
         udp.setParent(ipv4);
         assertArrayEquals(bytePacketUDP4, udp.serialize());
@@ -109,8 +109,8 @@
     public void testDeserialize() throws Exception {
         UDP udp = deserializer.deserialize(bytePacketUDP4, 0, bytePacketUDP4.length);
 
-        assertThat(udp.getSourcePort(), is((short) 0x50));
-        assertThat(udp.getDestinationPort(), is((short) 0x60));
+        assertThat(udp.getSourcePort(), is(0x50));
+        assertThat(udp.getDestinationPort(), is(0x60));
         assertThat(udp.getLength(), is((short) 8));
         assertThat(udp.getChecksum(), is((short) 0x7bda));
     }
@@ -121,12 +121,12 @@
     @Test
     public void testEqual() {
         UDP udp1 = new UDP();
-        udp1.setSourcePort((short) 0x50);
-        udp1.setDestinationPort((short) 0x60);
+        udp1.setSourcePort(0x50);
+        udp1.setDestinationPort(0x60);
 
         UDP udp2 = new UDP();
-        udp2.setSourcePort((short) 0x70);
-        udp2.setDestinationPort((short) 0x60);
+        udp2.setSourcePort(0x70);
+        udp2.setDestinationPort(0x60);
 
         assertTrue(udp1.equals(udp1));
         assertFalse(udp1.equals(udp2));