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));
 
diff --git a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpLocalRibImpl.java b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpLocalRibImpl.java
index 4f2735f..aef8055 100755
--- a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpLocalRibImpl.java
+++ b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpLocalRibImpl.java
@@ -135,7 +135,7 @@
                 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
                 // Compare local RIB entry with the current attribute
                 decisionResult = selectionAlgo.compare(nodeTree.get(nodeLsIdentifier), detailsLocRib);
-                if (decisionResult < 0) {
+                if (decisionResult <= 0) {
                     for (BgpNodeListener l : bgpController.listener()) {
                         l.addNode((BgpNodeLSNlriVer4) nlri, details);
                     }
@@ -155,7 +155,7 @@
                 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
                 // Compare local RIB entry with the current attribute
                 decisionResult = selectionAlgo.compare(linkTree.get(linkLsIdentifier), detailsLocRib);
-                if (decisionResult < 0) {
+                if (decisionResult <= 0) {
                     linkTree.replace(linkLsIdentifier, detailsLocRib);
                     for (BgpLinkListener l : bgpController.linkListener()) {
                         l.addLink((BgpLinkLsNlriVer4) nlri, details);
@@ -175,7 +175,7 @@
                 BgpSelectionAlgo selectionAlgo = new BgpSelectionAlgo();
                 // Compare local RIB entry with the current attribute
                 decisionResult = selectionAlgo.compare(prefixTree.get(prefixIdentifier), detailsLocRib);
-                if (decisionResult < 0) {
+                if (decisionResult <= 0) {
                     prefixTree.replace(prefixIdentifier, detailsLocRib);
                     log.debug("Local RIB update prefix: {}", detailsLocRib.toString());
                 }
diff --git a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpPeerImpl.java b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpPeerImpl.java
index 6f992f4..02a4321 100644
--- a/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpPeerImpl.java
+++ b/protocols/bgp/ctl/src/main/java/org/onosproject/bgp/controller/impl/BgpPeerImpl.java
@@ -168,7 +168,7 @@
 
         List<BgpValueType> attributesList = new LinkedList<>();
         byte sessionType = sessionInfo.isIbgpSession() ? (byte) 0 : (byte) 1;
-        byte sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE;
+        byte sAfi = Constants.SAFI_FLOWSPEC_VALUE;
 
         boolean isFsCapabilitySet = isCapabilitySupported(MultiProtocolExtnCapabilityTlv.TYPE,
                                                         Constants.AFI_FLOWSPEC_VALUE,
@@ -191,10 +191,10 @@
             return;
         }
 
-        if ((wideCommunity != null) && (isVpnRpdCapabilitySet)) {
-            sAfi = Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE;
-        } else if (isVpnFsCapabilitySet) {
+        if (isVpnFsCapabilitySet) {
             sAfi = Constants.VPN_SAFI_FLOWSPEC_VALUE;
+        } else if (isVpnRpdCapabilitySet) {
+            sAfi = Constants.VPN_SAFI_FLOWSPEC_RDP_VALUE;
         }
         attributesList.add(new Origin((byte) 0));
 
@@ -223,7 +223,9 @@
         }
 
         attributesList.add(new BgpExtendedCommunity(flowSpec.fsActionTlv()));
-        attributesList.add(wideCommunity);
+        if (wideCommunity != null) {
+            attributesList.add(wideCommunity);
+        }
 
         if (operType == FlowSpecOperation.ADD) {
             attributesList.add(new MpReachNlri(flowSpec, Constants.AFI_FLOWSPEC_VALUE, sAfi));