[ONOS-3856] BGP flow specification extended community path attribute parsing.

Change-Id: I9ea9db5565e91598328fdb703186c3d6577e2dc7
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
new file mode 100644
index 0000000..880b332
--- /dev/null
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpExtendedCommunity.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2016 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.bgpio.types;
+
+import com.google.common.base.MoreObjects;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.util.Constants;
+import org.onosproject.bgpio.util.Validation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.util.Objects;
+
+/**
+ * Provides implementation of extended community BGP Path Attribute.
+ */
+public class BgpExtendedCommunity implements BgpValueType {
+
+    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;
+
+    /**
+     * Constructor to initialize the value.
+     *
+     * @param fsActionTlv flow specification action type
+     */
+    public BgpExtendedCommunity(BgpValueType fsActionTlv) {
+        this.fsActionTlv = fsActionTlv;
+    }
+
+    /**
+     * Returns extended community type.
+     *
+     * @return extended community
+     */
+    public BgpValueType fsActionTlv() {
+        return this.fsActionTlv;
+    }
+
+    /**
+     * Reads from the channel buffer and parses extended community.
+     *
+     * @param cb ChannelBuffer
+     * @return object of BgpExtendedCommunity
+     * @throws BgpParseException while parsing extended community
+     */
+    public static BgpExtendedCommunity read(ChannelBuffer cb) throws BgpParseException {
+
+        ChannelBuffer tempCb = cb.copy();
+        Validation validation = Validation.parseAttributeHeader(cb);
+        BgpValueType fsActionTlv = null;
+
+        if (cb.readableBytes() < validation.getLength()) {
+            Validation.validateLen(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_LENGTH_ERROR,
+                    validation.getLength());
+        }
+        //if fourth bit is set, length is read as short otherwise as byte , len includes type, length and value
+        int len = validation.isShort() ? validation.getLength() + Constants.TYPE_AND_LEN_AS_SHORT : validation
+                .getLength() + Constants.TYPE_AND_LEN_AS_BYTE;
+        ChannelBuffer data = tempCb.readBytes(len);
+        if (validation.getFirstBit() && !validation.getSecondBit() && validation.getThirdBit()) {
+            throw new BgpParseException(BgpErrorType.UPDATE_MESSAGE_ERROR, BgpErrorType.ATTRIBUTE_FLAGS_ERROR, data);
+        }
+
+        ChannelBuffer tempBuf = cb.readBytes(validation.getLength());
+        if (tempBuf.readableBytes() > 0) {
+            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;
+            }
+        }
+        return new BgpExtendedCommunity(fsActionTlv);
+    }
+
+    @Override
+    public short getType() {
+        return TYPE;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(fsActionTlv);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof BgpExtendedCommunity) {
+            BgpExtendedCommunity other = (BgpExtendedCommunity) obj;
+            return Objects.equals(fsActionTlv, other.fsActionTlv);
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .omitNullValues()
+                .add("fsActionTlv", fsActionTlv)
+                .toString();
+    }
+
+    @Override
+    public int write(ChannelBuffer cb) {
+        int iLenStartIndex = cb.writerIndex();
+        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);
+        }
+
+        int fsActionLen = cb.writerIndex() - iActionLenIndex;
+        cb.setByte(iActionLenIndex, (byte) (fsActionLen - 1));
+
+        return cb.writerIndex() - iLenStartIndex;
+    }
+
+    @Override
+    public int compareTo(Object o) {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+}
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionReDirect.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionReDirect.java
index 650cce1..87f180d 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionReDirect.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionReDirect.java
@@ -77,12 +77,15 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec action redirect
      * @throws BgpParseException while parsing BgpFsActionReDirect
      */
-    public static BgpFsActionReDirect read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsActionReDirect read(ChannelBuffer cb) throws BgpParseException {
+        byte[] routeTarget;
+        ChannelBuffer tempCb = cb.copy();
+
+        routeTarget = tempCb.readBytes(tempCb.readableBytes()).array();
+        return new BgpFsActionReDirect(routeTarget);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficAction.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficAction.java
index 17856c6..4cedb6a 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficAction.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficAction.java
@@ -77,12 +77,15 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec action traffic rate
      * @throws BgpParseException while parsing BgpFsActionTrafficAction
      */
-    public static BgpFsActionTrafficAction read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsActionTrafficAction read(ChannelBuffer cb) throws BgpParseException {
+        byte[] bitMask;
+        ChannelBuffer tempCb = cb.copy();
+
+        bitMask = tempCb.readBytes(tempCb.readableBytes()).array();
+        return new BgpFsActionTrafficAction(bitMask);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficMarking.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficMarking.java
index cf192ec..6aee0f4 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficMarking.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficMarking.java
@@ -77,12 +77,15 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec action traffic marking
      * @throws BgpParseException while parsing BgpFsActionTrafficMarking
      */
-    public static BgpFsActionTrafficMarking read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsActionTrafficMarking read(ChannelBuffer cb) throws BgpParseException {
+        byte[] dscpValue;
+        ChannelBuffer tempCb = cb.copy();
+
+        dscpValue = tempCb.readBytes(tempCb.readableBytes()).array();
+        return new BgpFsActionTrafficMarking(dscpValue);
     }
 
     @Override
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficRate.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficRate.java
index e96642e..8f77ff0 100755
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficRate.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BgpFsActionTrafficRate.java
@@ -80,12 +80,17 @@
      * Reads the channel buffer and returns object.
      *
      * @param cb channelBuffer
-     * @param type address type
      * @return object of flow spec action traffic rate
      * @throws BgpParseException while parsing BgpFsActionTrafficRate
      */
-    public static BgpFsActionTrafficRate read(ChannelBuffer cb, short type) throws BgpParseException {
-        return null;
+    public static BgpFsActionTrafficRate read(ChannelBuffer cb) throws BgpParseException {
+        short asn;
+        float rate;
+        ChannelBuffer tempCb = cb.copy();
+
+        asn = tempCb.readShort();
+        rate = tempCb.readFloat();
+        return new BgpFsActionTrafficRate(asn, rate);
     }
 
     @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 33fdfc8..8d68742 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
@@ -48,6 +48,8 @@
     public static final int ENHANCED = 0x20;
     public static final int RESERVED = 0x40;
 
+    public static final byte BGP_EXTENDED_COMMUNITY = 0x10;
+
     public static final byte BGP_FLOWSPEC_DST_PREFIX = 0x01;
     public static final byte BGP_FLOWSPEC_SRC_PREFIX = 0x02;
     public static final byte BGP_FLOWSPEC_IP_PROTO = 0x03;