[ONOS-3857] BGP update message flow specification components parsing.

Change-Id: I9b58cdf2c47cb736a8119433cbc7911d871a83d1
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDestinationPortNum.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDestinationPortNum.java
index b9020c8..3ad87a3 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDestinationPortNum.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDestinationPortNum.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.Objects;
 import java.util.List;
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -78,12 +79,34 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec destination port number
      * @throws BgpParseException while parsing BgpFsDestinationPortNum
      */
-    public static BgpFsDestinationPortNum read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsDestinationPortNum read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        short port;
+
+        /*
+        0   1   2   3   4   5   6   7
+        +---+---+---+---+---+---+---+----+
+        | e | a |   len  | 0 |lt |gt |eq |
+        +---+---+---+---+---+---+---+----+
+        */
+
+        do {
+            option = (byte) cb.readByte();
+            int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
+            if ((1 << len) == 1) {
+                port = cb.readByte();
+                operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port}));
+            } else {
+                port = cb.readShort();
+                operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port}));
+            }
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+
+        return new BgpFsDestinationPortNum(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDestinationPrefix.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDestinationPrefix.java
index 63a6898..6a6a28b 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDestinationPrefix.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDestinationPrefix.java
@@ -22,6 +22,8 @@
 import org.onlab.packet.IpPrefix;
 import org.onosproject.bgpio.exceptions.BgpParseException;
 import org.onosproject.bgpio.util.Constants;
+import org.onosproject.bgpio.util.Validation;
+
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
 
@@ -33,6 +35,7 @@
     public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_DST_PREFIX;
     private byte length;
     private IpPrefix ipPrefix;
+    public static final int BYTE_IN_BITS = 8;
 
     /**
      * Constructor to initialize parameters.
@@ -89,12 +92,32 @@
      * Reads the channel buffer and returns object of IPv4AddressTlv.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec destination prefix
      * @throws BgpParseException while parsing BgpFsDestinationPrefix
      */
-    public static BgpFsDestinationPrefix read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsDestinationPrefix read(ChannelBuffer cb) throws BgpParseException {
+        IpPrefix ipPrefix;
+
+        int length = cb.readByte();
+        if (length == 0) {
+            byte[] prefix = new byte[] {0};
+            ipPrefix = Validation.bytesToPrefix(prefix, length);
+            return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix);
+        }
+        int len = length / BYTE_IN_BITS;
+        int reminder = length % BYTE_IN_BITS;
+        if (reminder > 0) {
+            len = len + 1;
+        }
+        if (cb.readableBytes() < len) {
+            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
+                    BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes());
+        }
+        byte[] prefix = new byte[len];
+        cb.readBytes(prefix, 0, len);
+        ipPrefix = Validation.bytesToPrefix(prefix, length);
+
+        return new BgpFsDestinationPrefix((byte) ipPrefix.prefixLength(), ipPrefix);
     }
 
     /**
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDscpValue.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDscpValue.java
index e383399..6165a50 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDscpValue.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsDscpValue.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.Objects;
 import java.util.List;
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -79,12 +80,20 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec DSCP value
      * @throws BgpParseException while parsing BgpFsDscpValue
      */
-    public static BgpFsDscpValue read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsDscpValue read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        byte dscpValue;
+
+        do {
+            option = (byte) cb.readByte();
+            dscpValue = cb.readByte();
+            operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) dscpValue}));
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+        return new BgpFsDscpValue(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsFragment.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsFragment.java
index 4c6bf79..933b7db 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsFragment.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsFragment.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.Objects;
 import java.util.List;
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -79,12 +80,21 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec fragment
      * @throws BgpParseException while parsing BgpFsFragment
      */
-    public static BgpFsFragment read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsFragment read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        byte fragment;
+
+        do {
+            option = (byte) cb.readByte();
+            fragment = cb.readByte();
+            operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) fragment}));
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+
+        return new BgpFsFragment(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIcmpCode.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIcmpCode.java
index 7ae2501..dadc9a1 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIcmpCode.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIcmpCode.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.Objects;
 
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -79,12 +80,21 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec ICMP code
      * @throws BgpParseException while parsing BgpFsIcmpCode
      */
-    public static BgpFsIcmpCode read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsIcmpCode read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        byte icmpCode;
+
+        do {
+            option = (byte) cb.readByte();
+            icmpCode = cb.readByte();
+            operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) icmpCode}));
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+
+        return new BgpFsIcmpCode(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIcmpType.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIcmpType.java
index d3d0329..22682b9 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIcmpType.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIcmpType.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.Objects;
 
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -78,12 +79,21 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec ICMP type
      * @throws BgpParseException while parsing BgpFsIcmpType
      */
-    public static BgpFsIcmpType read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsIcmpType read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        byte icmpType;
+
+        do {
+            option = (byte) cb.readByte();
+            icmpType = cb.readByte();
+            operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) icmpType}));
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+
+        return new BgpFsIcmpType(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIpProtocol.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIpProtocol.java
index 725d4ad..6da7e54 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIpProtocol.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsIpProtocol.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Objects;
 
@@ -89,12 +90,21 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec IP protocol
      * @throws BgpParseException while parsing BgpFsIpProtocol
      */
-    public static BgpFsIpProtocol read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsIpProtocol read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        byte proto;
+
+        do {
+            option = (byte) cb.readByte();
+            proto = cb.readByte();
+            operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) proto}));
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+
+        return new BgpFsIpProtocol(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsPacketLength.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsPacketLength.java
index 5e2a83b..215da13 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsPacketLength.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsPacketLength.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.Objects;
 import java.util.List;
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -79,12 +80,28 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec packet length
      * @throws BgpParseException while parsing BgpFsPacketLength
      */
-    public static BgpFsPacketLength read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsPacketLength read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        short packetLen;
+
+        do {
+            option = (byte) cb.readByte();
+            int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
+            if ((1 << len) == 1) {
+                packetLen = cb.readByte();
+                operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) packetLen}));
+            } else {
+                packetLen = cb.readShort();
+                operatorValue.add(new BgpFsOperatorValue(option,
+                                  new byte[] {(byte) (packetLen >> 8), (byte) packetLen}));
+            }
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+
+        return new BgpFsPacketLength(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsPortNum.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsPortNum.java
index 95872523..9aef15e 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsPortNum.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsPortNum.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.Objects;
 import java.util.List;
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -78,12 +79,29 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec port number
      * @throws BgpParseException while parsing BgpFsPortNum
      */
-    public static BgpFsPortNum read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsPortNum read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        short port;
+
+        do {
+            option = (byte) cb.readByte();
+            int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
+            if ((1 << len) == 1) {
+                port = cb.readByte();
+                operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port}));
+            } else {
+                port = cb.readShort();
+                operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port}));
+            }
+
+
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+
+        return new BgpFsPortNum(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsSourcePortNum.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsSourcePortNum.java
index 52f3f3c..25ea179 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsSourcePortNum.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsSourcePortNum.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.Objects;
 import java.util.List;
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -79,12 +80,27 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec source port number
      * @throws BgpParseException while parsing BgpFsSourcePortNum
      */
-    public static BgpFsSourcePortNum read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsSourcePortNum read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        short port;
+
+        do {
+            option = (byte) cb.readByte();
+            int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
+            if ((1 << len) == 1) {
+                port = cb.readByte();
+                operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) port}));
+            } else {
+                port = cb.readShort();
+                operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (port >> 8), (byte) port}));
+            }
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+
+        return new BgpFsSourcePortNum(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsSourcePrefix.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsSourcePrefix.java
index 5b75684..babf71f 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsSourcePrefix.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsSourcePrefix.java
@@ -22,6 +22,7 @@
 import org.onlab.packet.IpPrefix;
 import org.onosproject.bgpio.exceptions.BgpParseException;
 import org.onosproject.bgpio.util.Constants;
+import org.onosproject.bgpio.util.Validation;
 
 import com.google.common.base.MoreObjects;
 import com.google.common.base.Preconditions;
@@ -34,7 +35,7 @@
     public static final byte FLOW_SPEC_TYPE = Constants.BGP_FLOWSPEC_SRC_PREFIX;
     private byte length;
     private IpPrefix ipPrefix;
-
+    public static final int BYTE_IN_BITS = 8;
     /**
      * Constructor to initialize parameters.
      *
@@ -90,12 +91,33 @@
      * Reads the channel buffer and returns object of IPv4AddressTlv.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec source prefix
      * @throws BgpParseException while parsing BgpFsSourcePrefix
      */
-    public static BgpFsSourcePrefix read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsSourcePrefix read(ChannelBuffer cb) throws BgpParseException {
+        IpPrefix ipPrefix;
+
+        int length = cb.readByte();
+        if (length == 0) {
+            byte[] prefix = new byte[] {0};
+            ipPrefix = Validation.bytesToPrefix(prefix, length);
+            return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix);
+        }
+
+        int len = length / BYTE_IN_BITS;
+        int reminder = length % BYTE_IN_BITS;
+        if (reminder > 0) {
+            len = len + 1;
+        }
+        if (cb.readableBytes() < len) {
+            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR,
+                    BgpErrorType.MALFORMED_ATTRIBUTE_LIST, cb.readableBytes());
+        }
+        byte[] prefix = new byte[len];
+        cb.readBytes(prefix, 0, len);
+        ipPrefix = Validation.bytesToPrefix(prefix, length);
+
+        return new BgpFsSourcePrefix((byte) ipPrefix.prefixLength(), ipPrefix);
     }
 
     /**
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsTcpFlags.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsTcpFlags.java
index fef2d3f..7af2350 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsTcpFlags.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsTcpFlags.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.bgpio.types;
 
+import java.util.LinkedList;
 import java.util.Objects;
 import java.util.List;
 import org.jboss.netty.buffer.ChannelBuffer;
@@ -78,12 +79,28 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec TCP flags
      * @throws BgpParseException while parsing BgpFsTcpFlags
      */
-    public static BgpFsTcpFlags read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsTcpFlags read(ChannelBuffer cb) throws BgpParseException {
+        List<BgpFsOperatorValue> operatorValue = new LinkedList<>();
+        byte option;
+        short tcpFlag;
+
+        do {
+            option = (byte) cb.readByte();
+            int len = (option & Constants.BGP_FLOW_SPEC_LEN_MASK) >> 4;
+            if ((1 << len) == 1) {
+                tcpFlag = cb.readByte();
+                operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) tcpFlag}));
+            } else {
+                tcpFlag = cb.readShort();
+                operatorValue.add(new BgpFsOperatorValue(option, new byte[] {(byte) (tcpFlag >> 8), (byte) tcpFlag}));
+            }
+
+        } while ((option & Constants.BGP_FLOW_SPEC_END_OF_LIST_MASK) == 0);
+
+        return new BgpFsTcpFlags(operatorValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/NextHop.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/NextHop.java
index 806efe5..7e65dc0 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/NextHop.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/NextHop.java
@@ -32,7 +32,7 @@
  */
 public class NextHop implements BgpValueType {
     public static final byte NEXTHOP_TYPE = 3;
-
+    public static final byte FLAGS = (byte) 0x40;
     private boolean isNextHop = false;
     private Ip4Address nextHop;
 
@@ -47,6 +47,14 @@
     }
 
     /**
+     * Constructor to initialize default parameters.
+     *
+     */
+    public NextHop() {
+        this.nextHop = null;
+    }
+
+    /**
      * Returns whether next hop is present.
      *
      * @return whether next hop is present
@@ -103,8 +111,16 @@
 
     @Override
     public int write(ChannelBuffer cb) {
-        //Not required to be implemented now
-        return 0;
+        int iLenStartIndex = cb.writerIndex();
+        cb.writeByte(FLAGS);
+        cb.writeByte(getType());
+        if (!isNextHopSet()) {
+            cb.writeByte(0);
+        } else {
+            cb.writeInt(nextHop.toInt());
+        }
+
+        return cb.writerIndex() - iLenStartIndex;
     }
 
     @Override
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 8d68742..e904313 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
@@ -68,4 +68,6 @@
     public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT = (short) 0x8008;
     public static final short BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING = (short) 0x8009;
 
+    public static final byte BGP_FLOW_SPEC_LEN_MASK = 0x30;
+    public static final byte BGP_FLOW_SPEC_END_OF_LIST_MASK = (byte) 0x80;
 }
\ No newline at end of file