BGP flow spec system test update message parsing issues fix.

Change-Id: Ic2d615e1f449e93d937802ce3f9f33e468d42423
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecPrefix.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecPrefix.java
index cbf4269..a394839 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecPrefix.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecPrefix.java
@@ -59,7 +59,12 @@
         if (obj instanceof BgpFlowSpecPrefix) {
              BgpFlowSpecPrefix other = (BgpFlowSpecPrefix) obj;
 
-             if (this.destinationPrefix.equals(other.destinationPrefix)) {
+             if ((this.destinationPrefix != null) && (this.sourcePrefix != null)
+                 && (this.destinationPrefix.equals(other.destinationPrefix))) {
+                 return this.sourcePrefix.equals(other.sourcePrefix);
+             } else if (this.destinationPrefix != null) {
+                 return this.destinationPrefix.equals(other.destinationPrefix);
+             } else if (this.sourcePrefix != null) {
                  return this.sourcePrefix.equals(other.sourcePrefix);
              }
              return false;
@@ -106,33 +111,36 @@
 
         if (o instanceof BgpFlowSpecPrefix) {
             BgpFlowSpecPrefix that = (BgpFlowSpecPrefix) o;
-
-            if (this.destinationPrefix().prefixLength() == that.destinationPrefix().prefixLength()) {
-                ByteBuffer value1 = ByteBuffer.wrap(this.destinationPrefix().address().toOctets());
-                ByteBuffer value2 = ByteBuffer.wrap(that.destinationPrefix().address().toOctets());
-                int cmpVal = value1.compareTo(value2);
-                if (cmpVal != 0) {
-                    return cmpVal;
+            if (this.destinationPrefix() != null) {
+                if (this.destinationPrefix().prefixLength() == that.destinationPrefix().prefixLength()) {
+                    ByteBuffer value1 = ByteBuffer.wrap(this.destinationPrefix().address().toOctets());
+                    ByteBuffer value2 = ByteBuffer.wrap(that.destinationPrefix().address().toOctets());
+                    int cmpVal = value1.compareTo(value2);
+                    if (cmpVal != 0) {
+                        return cmpVal;
+                    }
+                } else {
+                    if (this.destinationPrefix().prefixLength() > that.destinationPrefix().prefixLength()) {
+                        return 1;
+                    } else if (this.destinationPrefix().prefixLength() < that.destinationPrefix().prefixLength()) {
+                        return -1;
+                    }
                 }
-            } else {
-                if (this.destinationPrefix().prefixLength() > that.destinationPrefix().prefixLength()) {
+            }
+            if (this.sourcePrefix() != null) {
+                if (this.sourcePrefix().prefixLength() == that.sourcePrefix().prefixLength()) {
+                    ByteBuffer value1 = ByteBuffer.wrap(this.sourcePrefix().address().toOctets());
+                    ByteBuffer value2 = ByteBuffer.wrap(that.sourcePrefix().address().toOctets());
+                    return value1.compareTo(value2);
+                }
+
+                if (this.sourcePrefix().prefixLength() > that.sourcePrefix().prefixLength()) {
                     return 1;
-                } else if (this.destinationPrefix().prefixLength() < that.destinationPrefix().prefixLength()) {
+                } else if (this.sourcePrefix().prefixLength() < that.sourcePrefix().prefixLength()) {
                     return -1;
                 }
             }
-
-            if (this.sourcePrefix().prefixLength() == that.sourcePrefix().prefixLength()) {
-                ByteBuffer value1 = ByteBuffer.wrap(this.sourcePrefix().address().toOctets());
-                ByteBuffer value2 = ByteBuffer.wrap(that.sourcePrefix().address().toOctets());
-                return value1.compareTo(value2);
-            }
-
-            if (this.sourcePrefix().prefixLength() > that.sourcePrefix().prefixLength()) {
-                return 1;
-            } else if (this.sourcePrefix().prefixLength() < that.sourcePrefix().prefixLength()) {
-                return -1;
-            }
+            return 0;
         }
         return 1;
     }
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpExtendedCommunity.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpExtendedCommunity.java
index 31a85da..7dee922 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpExtendedCommunity.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpExtendedCommunity.java
@@ -105,9 +105,9 @@
                     default: log.debug("Other type Not Supported:" + actionType);
                         break;
                 }
-            }
-            if (fsActionTlv != null) {
-                fsActionTlvs.add(fsActionTlv);
+                if (fsActionTlv != null) {
+                    fsActionTlvs.add(fsActionTlv);
+                }
             }
         }
         return new BgpExtendedCommunity(fsActionTlvs);
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionReDirect.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionReDirect.java
index 87f180d..cc15992 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionReDirect.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionReDirect.java
@@ -30,6 +30,7 @@
 
     public static final short TYPE = Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT;
     private byte[] routeTarget;
+    public static final byte ROUTE_TARGET_LEN = 6;
 
     /**
      * Constructor to initialize the value.
@@ -82,9 +83,8 @@
      */
     public static BgpFsActionReDirect read(ChannelBuffer cb) throws BgpParseException {
         byte[] routeTarget;
-        ChannelBuffer tempCb = cb.copy();
 
-        routeTarget = tempCb.readBytes(tempCb.readableBytes()).array();
+        routeTarget = cb.readBytes(ROUTE_TARGET_LEN).array();
         return new BgpFsActionReDirect(routeTarget);
     }
 
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficAction.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficAction.java
index 4cedb6a..eb6280b 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficAction.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficAction.java
@@ -30,6 +30,7 @@
 
     public static final short TYPE = Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION;
     private byte[] bitMask;
+    public static final byte BIT_MASK_LEN = 6;
 
     /**
      * Constructor to initialize the value.
@@ -82,9 +83,8 @@
      */
     public static BgpFsActionTrafficAction read(ChannelBuffer cb) throws BgpParseException {
         byte[] bitMask;
-        ChannelBuffer tempCb = cb.copy();
 
-        bitMask = tempCb.readBytes(tempCb.readableBytes()).array();
+        bitMask = cb.readBytes(BIT_MASK_LEN).array();
         return new BgpFsActionTrafficAction(bitMask);
     }
 
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficMarking.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficMarking.java
index 6aee0f4..dbc399c 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficMarking.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficMarking.java
@@ -30,6 +30,7 @@
 
     public static final short TYPE = Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING;
     private byte[] dscpValue;
+    public static final byte DSCP_LEN = 6;
 
     /**
      * Constructor to initialize the value.
@@ -82,9 +83,8 @@
      */
     public static BgpFsActionTrafficMarking read(ChannelBuffer cb) throws BgpParseException {
         byte[] dscpValue;
-        ChannelBuffer tempCb = cb.copy();
 
-        dscpValue = tempCb.readBytes(tempCb.readableBytes()).array();
+        dscpValue = cb.readBytes(DSCP_LEN).array();
         return new BgpFsActionTrafficMarking(dscpValue);
     }
 
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficRate.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficRate.java
index 8f77ff0..93d024b 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficRate.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficRate.java
@@ -86,10 +86,9 @@
     public static BgpFsActionTrafficRate read(ChannelBuffer cb) throws BgpParseException {
         short asn;
         float rate;
-        ChannelBuffer tempCb = cb.copy();
 
-        asn = tempCb.readShort();
-        rate = tempCb.readFloat();
+        asn = cb.readShort();
+        rate = cb.readFloat();
         return new BgpFsActionTrafficRate(asn, rate);
     }
 
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpReachNlri.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpReachNlri.java
index 89f2f3d..a93725e 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpReachNlri.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/MpReachNlri.java
@@ -239,7 +239,7 @@
                             flowSpecComponent = BgpFsIcmpType.read(tempBuf);
                             break;
                         case Constants.BGP_FLOWSPEC_ICMP_CD:
-                            flowSpecComponent = BgpFsIcmpType.read(tempBuf);
+                            flowSpecComponent = BgpFsIcmpCode.read(tempBuf);
                             break;
                         case Constants.BGP_FLOWSPEC_TCP_FLAGS:
                             flowSpecComponent = BgpFsTcpFlags.read(tempBuf);
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/Constants.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/Constants.java
index e904313..81f4d62 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/Constants.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/util/Constants.java
@@ -59,9 +59,9 @@
     public static final byte BGP_FLOWSPEC_ICMP_TP = 0x07;
     public static final byte BGP_FLOWSPEC_ICMP_CD = 0x08;
     public static final byte BGP_FLOWSPEC_TCP_FLAGS = 0x09;
-    public static final byte BGP_FLOWSPEC_PCK_LEN = 0x10;
-    public static final byte BGP_FLOWSPEC_DSCP = 0x11;
-    public static final byte BGP_FLOWSPEC_FRAGMENT = 0x12;
+    public static final byte BGP_FLOWSPEC_PCK_LEN = 0x0a;
+    public static final byte BGP_FLOWSPEC_DSCP = 0x0b;
+    public static final byte BGP_FLOWSPEC_FRAGMENT = 0x0c;
 
     public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_RATE = (short) 0x8006;
     public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION = (short) 0x8007;
diff --git a/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/types/MpReachNlriTest.java b/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/types/MpReachNlriTest.java
index 2b90b91..b2b176b 100644
--- a/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/types/MpReachNlriTest.java
+++ b/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/types/MpReachNlriTest.java
@@ -39,19 +39,107 @@
     @Test
     public void mpReachNlriTest1() throws BgpParseException {
 
-        // BGP flow spec  Message
-        byte[] flowSpecMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff,
+        // BGP flow spec Message
+        byte[] flowSpecMsg = new byte[] {(byte) 0xff, (byte) 0xff,
                 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
                 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
                 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
-                (byte) 0xff, 0x00, 0x4a, 0x02, 0x00, 0x00, 0x00,
-                0x33, 0x40, 0x01, 0x01, 0x00, 0x40, 0x02, 0x04,
-                0x02, 0x01, 0x00, 0x64, (byte) 0x80, 0x04, 0x04, 0x00,
-                0x00, 0x00, 0x00, (byte) 0xc0, 0x10, 0x08, (byte) 0x80, 0x06,
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x90, 0x0e,
-                0x00, 0x12, 0x00, 0x01, (byte) 0x85, 0x00, 0x00, 0x0c,
-                0x02, 0x20, (byte) 0xc0, (byte) 0xa8, 0x07, 0x36, 0x03, (byte) 0x81,
-                0x67, 0x04, (byte) 0x81, 0x01};
+                (byte) 0xff, (byte) 0xff, 0x00, 0x4a, 0x02, 0x00, 0x00, 0x00,
+                0x33, 0x40, 0x01, 0x01, 0x00, 0x40, 0x02, 0x04, 0x02, 0x01,
+                0x00, 0x64, (byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00,
+                (byte) 0xc0, 0x10, 0x08, (byte) 0x80, 0x06, 0x00, 0x00, 0x00,
+                0x00, 0x00, 0x00, (byte) 0x90, 0x0e, 0x00, 0x12, 0x00, 0x01,
+                (byte) 0x85, 0x00, 0x00, 0x0c, 0x02, 0x20, (byte) 0xc0,
+                (byte) 0xa8, 0x07, 0x36, 0x03, (byte) 0x81, 0x67, 0x04,
+                (byte) 0x81, 0x01 };
+
+        byte[] testFsMsg;
+        ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
+        buffer.writeBytes(flowSpecMsg);
+
+        BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
+        BgpMessage message;
+        BgpHeader bgpHeader = new BgpHeader();
+
+        message = reader.readFrom(buffer, bgpHeader);
+
+        assertThat(message, instanceOf(BgpUpdateMsgVer4.class));
+        ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
+        message.writeTo(buf);
+
+        int readLen = buf.writerIndex();
+        testFsMsg = new byte[readLen];
+        buf.readBytes(testFsMsg, 0, readLen);
+
+        assertThat(testFsMsg, is(flowSpecMsg));
+    }
+
+    /**
+     * This testcase checks BGP update message.
+     */
+    @Test
+    public void mpReachNlriTest2() throws BgpParseException {
+
+        // BGP flow spec Message
+        byte[] flowSpecMsg = new byte[] {(byte) 0xff, (byte) 0xff,
+                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                (byte) 0xff, (byte) 0xff, 0x00, 0x52, 0x02, 0x00, 0x00, 0x00,
+                0x3b, 0x40, 0x01, 0x01, 0x01, 0x40, 0x02, 0x04, 0x02, 0x01,
+                0x00, (byte) 0xc8, (byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00,
+                0x00, (byte) 0xc0, 0x10, 0x10, (byte) 0x80, 0x06, 0x00, 0x7b,
+                0x40, 0x60, 0x00, 0x00, (byte) 0x80, 0x09, 0x00, 0x00, 0x00,
+                0x00, 0x00, 0x0f, (byte) 0x90, 0x0e, 0x00, 0x12, 0x00, 0x01,
+                (byte) 0x85, 0x00, 0x00, 0x0c, 0x01, 0x1e, (byte) 0xc0,
+                (byte) 0xa8, 0x02, 0x00, 0x02, 0x1e, (byte) 0xc0, (byte) 0xa8,
+                0x01, 0x00 };
+
+        byte[] testFsMsg;
+        ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
+        buffer.writeBytes(flowSpecMsg);
+
+        BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
+        BgpMessage message;
+        BgpHeader bgpHeader = new BgpHeader();
+
+        message = reader.readFrom(buffer, bgpHeader);
+
+        assertThat(message, instanceOf(BgpUpdateMsgVer4.class));
+        ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
+        message.writeTo(buf);
+
+        int readLen = buf.writerIndex();
+        testFsMsg = new byte[readLen];
+        buf.readBytes(testFsMsg, 0, readLen);
+
+        assertThat(testFsMsg, is(flowSpecMsg));
+    }
+
+    /**
+     * This testcase checks BGP update message.
+     */
+    @Test
+    public void mpReachNlriTest3() throws BgpParseException {
+
+        // BGP flow spec Message
+        byte[] flowSpecMsg = new byte[] {(byte) 0xff, (byte) 0xff,
+                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                (byte) 0xff, (byte) 0xff, 0x00, 0x71, 0x02, 0x00, 0x00, 0x00,
+                0x5a, 0x40, 0x01, 0x01, 0x01, 0x40, 0x02, 0x04, 0x02, 0x01,
+                0x00, (byte) 0xc8, (byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00,
+                0x00, (byte) 0xc0, 0x10, 0x10, (byte) 0x80, 0x06, 0x00, 0x7b,
+                0x40, 0x60, 0x00, 0x00, (byte) 0x80, 0x09, 0x00, 0x00, 0x00,
+                0x00, 0x00, 0x0f, (byte) 0x90, 0x0e, 0x00, 0x31, 0x00, 0x01,
+                (byte) 0x85, 0x00, 0x00, 0x2b, 0x01, 0x1e, (byte) 0xc0,
+                (byte) 0xa8, 0x02, 0x00, 0x02, 0x1e, (byte) 0xc0, (byte) 0xa8,
+                0x01, 0x00, 0x03, (byte) 0x80, 0x04, 0x04, (byte) 0x80,
+                (byte) 0xb3, 0x05, (byte) 0x80, (byte) 0xc8, 0x06, (byte) 0x80,
+                0x64, 0x07, (byte) 0x80, 0x7b, 0x08, (byte) 0x80, (byte) 0xea,
+                0x09, (byte) 0x80, 0x7b, 0x0a, (byte) 0x90, 0x03, (byte) 0xe8,
+                0x0b, (byte) 0x80, 0x7b, 0x0c, (byte) 0x80, 0x02 };
 
         byte[] testFsMsg;
         ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();