[ONOS-3857] BGP flow spec encoding for mandatory attributes

Change-Id: I58f19deca56464557214d02717a1562ac64d0407
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/As4Path.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/As4Path.java
index 51779cc..6e544ae 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/As4Path.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/As4Path.java
@@ -34,6 +34,7 @@
     private static final Logger log = LoggerFactory.getLogger(AsPath.class);
     public static final byte AS4PATH_TYPE = 17;
     public static final byte ASNUM_SIZE = 4;
+    public static final byte FLAGS = (byte) 0x40;
 
     private List<Integer> as4pathSet;
     private List<Integer> as4pathSeq;
@@ -162,8 +163,27 @@
 
     @Override
     public int write(ChannelBuffer cb) {
-        //Not required to Implement as of now
-        return 0;
+
+        int iLenStartIndex = cb.writerIndex();
+
+        cb.writeByte(FLAGS);
+        cb.writeByte(getType());
+        if ((as4pathSet != null) && (as4pathSeq != null)) {
+            int iAsLenIndex = cb.writerIndex();
+            cb.writeByte(0);
+            cb.writeByte(AsPath.ASPATH_SEQ_TYPE);
+            cb.writeByte(as4pathSeq.size());
+
+            for (int j = 0; j < as4pathSeq.size(); j++) {
+                cb.writeInt(as4pathSeq.get(j));
+            }
+
+            int asLen = cb.writerIndex() - iAsLenIndex;
+            cb.setByte(iAsLenIndex, (byte) (asLen - 1));
+        } else {
+            cb.writeByte(0);
+        }
+        return cb.writerIndex() - iLenStartIndex;
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AsPath.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AsPath.java
index 7879bd6..c4b9c71 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AsPath.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/AsPath.java
@@ -63,6 +63,7 @@
     public static final byte ASPATH_SET_TYPE = 1;
     public static final byte ASPATH_SEQ_TYPE = 2;
     public static final byte ASNUM_SIZE = 2;
+    public static final byte FLAGS = (byte) 0x40;
 
     private boolean isAsPath = false;
     private List<Short> aspathSet;
@@ -201,8 +202,25 @@
 
     @Override
     public int write(ChannelBuffer cb) {
-        //Not required to Implement as of now
-        return 0;
+        int iLenStartIndex = cb.writerIndex();
+        cb.writeByte(FLAGS);
+        cb.writeByte(getType());
+        if (isaspathSet()) {
+            int iAsLenIndex = cb.writerIndex();
+            cb.writeByte(0);
+            cb.writeByte(ASPATH_SEQ_TYPE);
+            cb.writeByte(aspathSeq.size());
+
+            for (int j = 0; j < aspathSeq.size(); j++) {
+                cb.writeShort(aspathSeq.get(j));
+            }
+
+            int asLen = cb.writerIndex() - iAsLenIndex;
+            cb.setByte(iAsLenIndex, (byte) (asLen - 1));
+        } else {
+            cb.writeByte(0);
+        }
+        return cb.writerIndex() - iLenStartIndex;
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/LocalPref.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/LocalPref.java
index 36793c1..b15c74c 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/LocalPref.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/LocalPref.java
@@ -30,6 +30,7 @@
 public class LocalPref implements BgpValueType {
     public static final byte LOCAL_PREF_TYPE = 5;
     public static final byte LOCAL_PREF_MAX_LEN = 4;
+    public static final byte FLAGS = (byte) 0x40;
 
     private int localPref;
 
@@ -109,8 +110,12 @@
 
     @Override
     public int write(ChannelBuffer cb) {
-        //Not to implement as of now
-        return 0;
+        int iLenStartIndex = cb.writerIndex();
+        cb.writeByte(FLAGS);
+        cb.writeByte(getType());
+        cb.writeByte(4);
+        cb.writeInt(localPref());
+        return cb.writerIndex() - iLenStartIndex;
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/Med.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/Med.java
index 7f1ab53..2fe4867 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/Med.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/Med.java
@@ -30,6 +30,7 @@
 public class Med implements BgpValueType {
     public static final byte MED_TYPE = 4;
     public static final byte MED_MAX_LEN = 4;
+    public static final byte FLAGS = (byte) 0x80;
 
     private int med;
 
@@ -109,8 +110,12 @@
 
     @Override
     public int write(ChannelBuffer cb) {
-        //Not to implement as of now
-        return 0;
+        int iLenStartIndex = cb.writerIndex();
+        cb.writeByte(FLAGS);
+        cb.writeByte(getType());
+        cb.writeByte(4);
+        cb.writeInt(med());
+        return cb.writerIndex() - iLenStartIndex;
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/Origin.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/Origin.java
index 3e0e06c..7999f82 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/Origin.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/Origin.java
@@ -55,6 +55,7 @@
 
     public static final byte ORIGIN_TYPE = 1;
     public static final byte ORIGIN_VALUE_LEN = 1;
+    public static final byte FLAGS = (byte) 0x40;
 
     private boolean isOrigin = false;
     private byte origin;
@@ -131,8 +132,12 @@
 
     @Override
     public int write(ChannelBuffer cb) {
-        //Not required to Implement as of now
-        return 0;
+        int iLenStartIndex = cb.writerIndex();
+        cb.writeByte(FLAGS);
+        cb.writeByte(ORIGIN_TYPE);
+        cb.writeByte(1);
+        cb.writeByte(origin().value);
+        return cb.writerIndex() - iLenStartIndex;
     }
 
     @Override