BGP encode multiple flow spec action issue fix.

Change-Id: Ieb173be9edb86bc717bfe1e55271873645e4a5b1
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecDetails.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecDetails.java
index cc6384c..994f656 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecDetails.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/flowspec/BgpFlowSpecDetails.java
@@ -27,7 +27,7 @@
  */
 public class BgpFlowSpecDetails {
     private List<BgpValueType> flowSpecComponents;
-    private BgpValueType fsActionTlv;
+    private List<BgpValueType> fsActionTlv;
     private RouteDistinguisher routeDistinguisher;
 
     /**
@@ -44,7 +44,7 @@
      *
      * @return flow specification action tlv
      */
-    public BgpValueType fsActionTlv() {
+    public List<BgpValueType> fsActionTlv() {
         return this.fsActionTlv;
     }
 
@@ -53,7 +53,7 @@
      *
      * @param fsActionTlv flow specification action tlv
      */
-    public void setFsActionTlv(BgpValueType fsActionTlv) {
+    public void setFsActionTlv(List<BgpValueType> fsActionTlv) {
         this.fsActionTlv = fsActionTlv;
     }
 
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 880b332..31a85da 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
@@ -23,6 +23,10 @@
 import org.onosproject.bgpio.util.Validation;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.ListIterator;
 import java.util.Objects;
 
 /**
@@ -33,14 +37,14 @@
     private static final Logger log = LoggerFactory.getLogger(BgpExtendedCommunity.class);
     public static final short TYPE = Constants.BGP_EXTENDED_COMMUNITY;
     public static final byte FLAGS = (byte) 0xC0;
-    private BgpValueType fsActionTlv;
+    private List<BgpValueType> fsActionTlv;
 
     /**
      * Constructor to initialize the value.
      *
      * @param fsActionTlv flow specification action type
      */
-    public BgpExtendedCommunity(BgpValueType fsActionTlv) {
+    public BgpExtendedCommunity(List<BgpValueType> fsActionTlv) {
         this.fsActionTlv = fsActionTlv;
     }
 
@@ -49,7 +53,7 @@
      *
      * @return extended community
      */
-    public BgpValueType fsActionTlv() {
+    public List<BgpValueType> fsActionTlv() {
         return this.fsActionTlv;
     }
 
@@ -64,7 +68,7 @@
 
         ChannelBuffer tempCb = cb.copy();
         Validation validation = Validation.parseAttributeHeader(cb);
-        BgpValueType fsActionTlv = null;
+        List<BgpValueType> fsActionTlvs = new LinkedList<>();
 
         if (cb.readableBytes() < validation.getLength()) {
             Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
@@ -80,27 +84,33 @@
 
         ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
         if (tempBuf.readableBytes() > 0) {
+            BgpValueType fsActionTlv = null;
             ChannelBuffer actionBuf = tempBuf.readBytes(validation.getLength());
-            short actionType = actionBuf.readShort();
-            short length = (short) validation.getLength();
-            switch (actionType) {
-                case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION:
-                    fsActionTlv = BgpFsActionTrafficAction.read(actionBuf);
-                    break;
-                case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING:
-                    fsActionTlv = BgpFsActionTrafficMarking.read(actionBuf);
-                    break;
-                case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE:
-                    fsActionTlv = BgpFsActionTrafficRate.read(actionBuf);
-                    break;
-                case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT:
-                    fsActionTlv = BgpFsActionReDirect.read(actionBuf);
-                    break;
-                default: log.debug("Other type Not Supported:" + actionType);
-                    break;
+
+            while (actionBuf.readableBytes() > 0) {
+                short actionType = actionBuf.readShort();
+                switch (actionType) {
+                    case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION:
+                        fsActionTlv = BgpFsActionTrafficAction.read(actionBuf);
+                        break;
+                    case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING:
+                        fsActionTlv = BgpFsActionTrafficMarking.read(actionBuf);
+                        break;
+                    case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE:
+                        fsActionTlv = BgpFsActionTrafficRate.read(actionBuf);
+                        break;
+                    case Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT:
+                        fsActionTlv = BgpFsActionReDirect.read(actionBuf);
+                        break;
+                    default: log.debug("Other type Not Supported:" + actionType);
+                        break;
+                }
+            }
+            if (fsActionTlv != null) {
+                fsActionTlvs.add(fsActionTlv);
             }
         }
-        return new BgpExtendedCommunity(fsActionTlv);
+        return new BgpExtendedCommunity(fsActionTlvs);
     }
 
     @Override
@@ -136,24 +146,29 @@
     @Override
     public int write(ChannelBuffer cb) {
         int iLenStartIndex = cb.writerIndex();
+        ListIterator<BgpValueType> listIterator = fsActionTlv().listIterator();
+
         cb.writeByte(FLAGS);
         cb.writeByte(getType());
 
         int iActionLenIndex = cb.writerIndex();
         cb.writeByte(0);
 
-        if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION) {
-            BgpFsActionTrafficAction trafficAction = (BgpFsActionTrafficAction) fsActionTlv;
-            trafficAction.write(cb);
-        } else if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING) {
-            BgpFsActionTrafficMarking trafficMarking = (BgpFsActionTrafficMarking) fsActionTlv;
-            trafficMarking.write(cb);
-        } else if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE) {
-            BgpFsActionTrafficRate trafficRate = (BgpFsActionTrafficRate) fsActionTlv;
-            trafficRate.write(cb);
-        } else if (fsActionTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT) {
-            BgpFsActionReDirect trafficRedirect = (BgpFsActionReDirect) fsActionTlv;
-            trafficRedirect.write(cb);
+        while (listIterator.hasNext()) {
+            BgpValueType fsTlv = listIterator.next();
+            if (fsTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_ACTION) {
+                BgpFsActionTrafficAction trafficAction = (BgpFsActionTrafficAction) fsTlv;
+                trafficAction.write(cb);
+            } else if (fsTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_MARKING) {
+                BgpFsActionTrafficMarking trafficMarking = (BgpFsActionTrafficMarking) fsTlv;
+                trafficMarking.write(cb);
+            } else if (fsTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_RATE) {
+                BgpFsActionTrafficRate trafficRate = (BgpFsActionTrafficRate) fsTlv;
+                trafficRate.write(cb);
+            } else if (fsTlv.getType() == Constants.BGP_FLOWSPEC_ACTION_TRAFFIC_REDIRECT) {
+                BgpFsActionReDirect trafficRedirect = (BgpFsActionReDirect) fsTlv;
+                trafficRedirect.write(cb);
+            }
         }
 
         int fsActionLen = cb.writerIndex() - iActionLenIndex;