BGP routepolicy distribution bug fixes.

Change-Id: I2176e3872391658263d6817ac8850a3390de00de
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java
index 4c3d4a1..7062a7d 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpUpdateMsgVer4.java
@@ -241,12 +241,11 @@
                         afi = mpUnReach.afi();
                         safi = mpUnReach.safi();
                     }
+                }
 
-                    if ((afi == Constants.AFI_FLOWSPEC_VALUE) && ((safi == Constants.SAFI_FLOWSPEC_VALUE)
-                            || (safi == Constants.VPN_SAFI_FLOWSPEC_VALUE))) {
-                        //unfeasible route length
-                        cb.writeShort(0);
-                    }
+                if ((afi == Constants.AFI_FLOWSPEC_VALUE) || (afi == Constants.AFI_VALUE)) {
+                    //unfeasible route length
+                    cb.writeShort(0);
                 }
 
             }
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 19f3126..d766f0d 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
@@ -171,15 +171,17 @@
         if ((as4pathSet != null) && (as4pathSeq != null)) {
             int iAsLenIndex = cb.writerIndex();
             cb.writeByte(0);
-            cb.writeByte(AsPath.ASPATH_SEQ_TYPE);
-            cb.writeByte(as4pathSeq.size());
+            if (as4pathSeq.size() != 0) {
+                cb.writeByte(AsPath.ASPATH_SEQ_TYPE);
+                cb.writeByte(as4pathSeq.size());
 
-            for (int j = 0; j < as4pathSeq.size(); j++) {
-                cb.writeInt(as4pathSeq.get(j));
+                for (int j = 0; j < as4pathSeq.size(); j++) {
+                    cb.writeInt(as4pathSeq.get(j));
+                }
+
+                int asLen = cb.writerIndex() - iAsLenIndex;
+                cb.setByte(iAsLenIndex, (byte) (asLen - 1));
             }
-
-            int asLen = cb.writerIndex() - iAsLenIndex;
-            cb.setByte(iAsLenIndex, (byte) (asLen - 1));
         } else {
             cb.writeByte(0);
         }
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 2c0f0b8..f6442d9 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
@@ -208,15 +208,16 @@
         if (isaspathSet()) {
             int iAsLenIndex = cb.writerIndex();
             cb.writeByte(0);
-            cb.writeByte(ASPATH_SEQ_TYPE);
-            cb.writeByte(aspathSeq.size());
+            if (aspathSeq.size() != 0) {
+                cb.writeByte(ASPATH_SEQ_TYPE);
+                cb.writeByte(aspathSeq.size());
 
-            for (int j = 0; j < aspathSeq.size(); j++) {
-                cb.writeShort(aspathSeq.get(j));
+                for (int j = 0; j < aspathSeq.size(); j++) {
+                    cb.writeShort(aspathSeq.get(j));
+                }
+                int asLen = cb.writerIndex() - iAsLenIndex;
+                cb.setByte(iAsLenIndex, (byte) (asLen - 1));
             }
-
-            int asLen = cb.writerIndex() - iAsLenIndex;
-            cb.setByte(iAsLenIndex, (byte) (asLen - 1));
         } else {
             cb.writeByte(0);
         }
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/WideCommunityAttrHeader.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/WideCommunityAttrHeader.java
index 47e1014..e49ef71 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/WideCommunityAttrHeader.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/WideCommunityAttrHeader.java
@@ -163,7 +163,6 @@
         c.writeShort(TYPE);
         c.writeByte(flag);
         c.writeByte(hopCount);
-        c.writeShort(length);
         return c.writerIndex() - iLenStartIndex;
     }
 
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/WideCommunityIpV4Neighbour.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/WideCommunityIpV4Neighbour.java
index 92464f4..5fc16cc 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/WideCommunityIpV4Neighbour.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/WideCommunityIpV4Neighbour.java
@@ -97,8 +97,8 @@
         while (listIterator.hasNext()) {
             IpV4Neighbour speaker = listIterator.next();
             if (speaker instanceof IpV4Neighbour) {
-                c.writeInt(Integer.valueOf(speaker.localSpeaker.toString()));
-                c.writeInt(Integer.valueOf(speaker.remoteSpeaker.toString()));
+                c.writeBytes(speaker.localSpeaker().toOctets());
+                c.writeBytes(speaker.remoteSpeaker().toOctets());
             }
         }
 
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/WideCommunity.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/WideCommunity.java
index 25afd95..1867959 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/WideCommunity.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/attr/WideCommunity.java
@@ -43,10 +43,10 @@
 public class WideCommunity implements BgpValueType {
 
     private static final Logger log = LoggerFactory.getLogger(WideCommunity.class);
-    public static final byte TYPE = (byte) 254; /* TODO: IANA Assigned */
+    public static final byte TYPE = (byte) 129;
     public static final short LENGTH = 4;
     public static final byte TYPE_LENGTH_SIZE = 3;
-    public static final byte FLAGS = (byte) 0x40;
+    public static final byte FLAGS = (byte) 0x90;
     private WideCommunityAttrHeader wideCommunityHeader;
     private int community;
     private int localAsn;
@@ -238,6 +238,9 @@
 
         wideCommunityHeader.write(c);
 
+        int iComLengthIndex = c.writerIndex();
+        c.writeShort(0);
+
         c.writeInt(community);
         c.writeInt(localAsn);
         c.writeInt(contextAsn);
@@ -275,6 +278,9 @@
             c.setShort(iTargetLenIndex, (short) (length - 2));
         }
 
+        length = c.writerIndex() - iComLengthIndex;
+        c.setShort(iComLengthIndex, (short) (length - 2));
+
         length = c.writerIndex() - iLengthIndex;
         c.setShort(iLengthIndex, (short) (length - 2));