[ONOS-8082] and [ONOS-8083] Route refresh changes

- Added Route Refresh message
- Changes in BgpFactory
- Add parsing for Route Refresh Capability TLV
- Add Unit Test case for Route Refresh message

Change-Id: I68efe55be50c545b06c99a099c4bc315b2d5f566
(cherry picked from commit bd6fc428c37f6177fb7fe9f66f6a5e3919e4f30a)
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactory.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactory.java
index 645100e..95bdec8 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactory.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpFactory.java
@@ -52,6 +52,13 @@
     BgpUpdateMsg.Builder updateMessageBuilder();
 
     /**
+     * Gets the builder object for route refresh message.
+     *
+     * @return builder object for route refresh message
+     */
+    BgpRouteRefreshMsg.Builder routeRefreshMsgBuilder();
+
+    /**
      * Gets the BGP message reader.
      *
      * @return BGP message reader
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpRouteRefreshMsg.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpRouteRefreshMsg.java
new file mode 100644
index 0000000..d8d5370
--- /dev/null
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpRouteRefreshMsg.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.protocol;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.types.BgpHeader;
+
+/**
+ * Abstraction of an entity providing BGP Route Refresh Message.
+ */
+public interface BgpRouteRefreshMsg extends BgpMessage {
+
+    @Override
+    BgpVersion getVersion();
+
+    @Override
+    BgpType getType();
+
+    @Override
+    void writeTo(ChannelBuffer channelBuffer);
+
+    @Override
+    BgpHeader getHeader();
+
+    /**
+     * Builder interface with get and set functions to build RouteRefresh message.
+     */
+    interface Builder extends BgpMessage.Builder {
+        @Override
+        BgpRouteRefreshMsg build();
+
+        @Override
+        Builder setHeader(BgpHeader bgpMsgHeader);
+
+        /**
+         * Adds the current AFI-SAFI pair and reserved bytes to be transmitted.
+         * BGP Route Refresh message contains one pair of AFI-SAFI values, but routers may transmit more than
+         * one AFI-SAFI pair
+         *
+         * @param afi AFI value to be added to AFI-SAFI pairs
+         * @param reserved reserved byte to be added. By default, this should be 0x00
+         * @param safi SAFI value to be added to AFI-SAFI pairs
+         * @return builder by setting hold time
+         */
+        Builder addAfiSafiValue(short afi, byte reserved, byte safi);
+    }
+}
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpType.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpType.java
index d65caff..e96c99f 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpType.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BgpType.java
@@ -21,7 +21,7 @@
  */
 public enum BgpType {
 
-    NONE(0), OPEN(1), UPDATE(2), NOTIFICATION(3), KEEP_ALIVE(4);
+    NONE(0), OPEN(1), UPDATE(2), NOTIFICATION(3), KEEP_ALIVE(4), ROUTE_REFRESH(5);
 
     int value;
 
@@ -42,4 +42,4 @@
     public byte getType() {
         return (byte) value;
     }
-}
\ No newline at end of file
+}
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java
index babade2..96806f1 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpFactoryVer4.java
@@ -22,6 +22,7 @@
 import org.onosproject.bgpio.protocol.BgpMessageReader;
 import org.onosproject.bgpio.protocol.BgpNotificationMsg;
 import org.onosproject.bgpio.protocol.BgpOpenMsg;
+import org.onosproject.bgpio.protocol.BgpRouteRefreshMsg;
 import org.onosproject.bgpio.protocol.BgpUpdateMsg;
 import org.onosproject.bgpio.protocol.BgpVersion;
 
@@ -53,6 +54,11 @@
     }
 
     @Override
+    public BgpRouteRefreshMsg.Builder routeRefreshMsgBuilder() {
+        return new BgpRouteRefreshMsgVer4.Builder();
+    }
+
+    @Override
     public BgpMessageReader<BgpMessage> getReader() {
         return BgpMessageVer4.READER;
     }
@@ -61,4 +67,4 @@
     public BgpVersion getVersion() {
         return BgpVersion.BGP_4;
     }
-}
\ No newline at end of file
+}
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java
index 278d817..fd4669d 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpMessageVer4.java
@@ -36,6 +36,7 @@
 
     static final byte OPEN_MSG_TYPE = 0x1;
     static final byte KEEPALIVE_MSG_TYPE = 0x4;
+    static final byte ROUTEREFRESH_MSG_TYPE = 0x5;
     static final byte UPDATE_MSG_TYPE = 0x2;
     static final byte NOTIFICATION_MSG_TYPE = 0x3;
     static final int MINIMUM_COMMON_HEADER_LENGTH = 19;
@@ -98,6 +99,9 @@
                 case NOTIFICATION_MSG_TYPE:
                     log.debug("NOTIFICATION MESSAGE is received");
                     return BgpNotificationMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
+                case ROUTEREFRESH_MSG_TYPE:
+                    log.debug("ROUTEREFRESH MESSAGE is received");
+                    return BgpRouteRefreshMsgVer4.READER.readFrom(cb.readBytes(len), bgpHeader);
                 default:
                     Validation.validateType(BgpErrorType.MESSAGE_HEADER_ERROR, BgpErrorType.BAD_MESSAGE_TYPE, type);
                     return null;
@@ -108,4 +112,4 @@
             }
         }
     }
-}
\ No newline at end of file
+}
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java
index d7550ff..7c50998 100644
--- a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpOpenMsgVer4.java
@@ -30,6 +30,7 @@
 import org.onosproject.bgpio.types.BgpValueType;
 import org.onosproject.bgpio.types.FourOctetAsNumCapabilityTlv;
 import org.onosproject.bgpio.types.MultiProtocolExtnCapabilityTlv;
+import org.onosproject.bgpio.types.RouteRefreshCapabilityTlv;
 import org.onosproject.bgpio.util.Validation;
 import org.onosproject.bgpio.util.Constants;
 import org.onosproject.bgpio.types.RpdCapabilityTlv;
@@ -298,6 +299,15 @@
                 tlv = new MultiProtocolExtnCapabilityTlv(afi, res, safi);
 
                 break;
+            case RouteRefreshCapabilityTlv.TYPE:
+                log.debug("RouteRefreshCapabilityTlv");
+
+                if (RouteRefreshCapabilityTlv.LENGTH != length) {
+                    throw new BgpParseException("Invalid length received for RouteRefreshCapabilityTlv.");
+                }
+
+                tlv = new RouteRefreshCapabilityTlv(true);
+                break;
             default:
                 log.debug("Warning: Unsupported TLV: " + type);
                 cb.skipBytes(length);
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpRouteRefreshMsgVer4.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpRouteRefreshMsgVer4.java
new file mode 100644
index 0000000..9239176
--- /dev/null
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/ver4/BgpRouteRefreshMsgVer4.java
@@ -0,0 +1,213 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.protocol.ver4;
+
+import com.google.common.base.MoreObjects;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.protocol.BgpType;
+import org.onosproject.bgpio.types.BgpHeader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.onosproject.bgpio.protocol.BgpRouteRefreshMsg;
+import org.onosproject.bgpio.protocol.BgpMessageReader;
+import org.onosproject.bgpio.protocol.BgpMessageWriter;
+import org.onosproject.bgpio.protocol.BgpVersion;
+
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Provides BGP Route Refresh message.
+ */
+public class BgpRouteRefreshMsgVer4 implements BgpRouteRefreshMsg {
+
+    /*
+    <Route Refresh Message>::=
+
+    0                   1                   2                   3
+    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+    |                                                               |
+    +                                                               +
+    |                                                               |
+    +                                                               +
+    |                           Marker                              |
+    +                                                               +
+    |                                                               |
+    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+    |          Length               |      Type     |
+    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+    |           AFI                 |    Reserved   |     SAFI      |
+    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+
+    REFERENCE : RFC 2918
+    */
+
+    private static final Logger log = LoggerFactory.getLogger(BgpRouteRefreshMsgVer4.class);
+
+    public static final byte PACKET_VERSION = 4;
+    public static final int PACKET_LENGTH = 0x17;
+    public static final int MARKER_LENGTH = 16;
+    public static final BgpType MSG_TYPE = BgpType.ROUTE_REFRESH;
+    private static final byte[] MARKER = new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff};
+
+    private static final BgpHeader DEFAULT_ROUTEREFRESH_HEADER = new BgpHeader(MARKER,
+            (short) PACKET_LENGTH, BgpType.ROUTE_REFRESH.getType());
+
+    private BgpHeader bgpMsgHeader;
+    private byte version;
+    private List<AfiSafiValue> afiSafiValues;
+
+    public static final BgpRouteRefreshMsgVer4.Reader READER = new Reader();
+
+    /**
+     * Reader class for reading BGP route refresh message from channel buffer.
+     */
+    static class Reader implements BgpMessageReader<BgpRouteRefreshMsg> {
+
+        @Override
+        public BgpRouteRefreshMsgVer4 readFrom(ChannelBuffer cb, BgpHeader bgpHeader)
+                throws BgpParseException {
+
+            Short afi = cb.readShort();
+            log.debug("AFI read from Route Refresh message = {}", afi);
+
+            byte reserved = cb.readByte();
+            log.debug("Reserved read from Route Refresh message = {}", reserved);
+
+            byte safi = cb.readByte();
+            log.debug("SAFI read from Route Refresh message = {}", safi);
+
+            List<AfiSafiValue> afiSafiList = new LinkedList<>();
+
+            AfiSafiValue afisafiValue = new AfiSafiValue(afi, reserved, safi);
+            afiSafiList.add(afisafiValue);
+
+            return new BgpRouteRefreshMsgVer4(DEFAULT_ROUTEREFRESH_HEADER, PACKET_VERSION, afiSafiList);
+        }
+    }
+
+    /**
+     * Default constructor.
+     */
+    public BgpRouteRefreshMsgVer4() {
+        this.bgpMsgHeader = null;
+        this.version = 0;
+        this.afiSafiValues = null;
+    }
+
+    public BgpRouteRefreshMsgVer4(BgpHeader msgHeader, byte version, List<AfiSafiValue> afiSafiValues) {
+        this.bgpMsgHeader = msgHeader;
+        this.version = version;
+        this.afiSafiValues = afiSafiValues;
+    }
+
+    /**
+     * Builder class for BGP route refresh message.
+     */
+    static class Builder implements BgpRouteRefreshMsg.Builder {
+        BgpHeader bgpHeader = null;
+        boolean isHeaderSet = false;
+        List<AfiSafiValue> afiSafiList = null;
+
+        @Override
+        public Builder setHeader(BgpHeader bgpMsgHeader) {
+            this.bgpHeader = bgpMsgHeader;
+            this.isHeaderSet = true;
+            return this;
+        }
+
+        @Override
+        public BgpRouteRefreshMsg.Builder addAfiSafiValue(short afi, byte reserved, byte safi) {
+            if (afiSafiList == null) {
+                afiSafiList = new LinkedList<>();
+            }
+            AfiSafiValue currentElement = new AfiSafiValue(afi, reserved, safi);
+            afiSafiList.add(currentElement);
+            return this;
+        }
+
+        @Override
+        public BgpRouteRefreshMsg build() {
+            BgpHeader header = isHeaderSet ? bgpHeader : DEFAULT_ROUTEREFRESH_HEADER;
+            return new BgpRouteRefreshMsgVer4(header, PACKET_VERSION, afiSafiList);
+        }
+    }
+
+    @Override
+    public void writeTo(ChannelBuffer cb) {
+        WRITER.write(cb, this);
+    }
+
+    static final Writer WRITER = new Writer();
+
+    /**
+     * Writer class for writing the BGP route refresh message to channel buffer.
+     */
+    static class Writer implements BgpMessageWriter<BgpRouteRefreshMsgVer4> {
+
+        @Override
+        public void write(ChannelBuffer cb, BgpRouteRefreshMsgVer4 message) {
+            //Iterate over the added AFI-SAFI pair and write a new packet for each of those
+
+            for (AfiSafiValue element : message.afiSafiValues) {
+                cb.writeBytes(MARKER, 0, MARKER_LENGTH);
+                cb.writeShort(PACKET_LENGTH);
+                cb.writeByte(MSG_TYPE.getType());
+
+                cb.writeShort(element.afi);
+                cb.writeByte(element.reserved);
+                cb.writeByte(element.safi);
+            }
+        }
+    }
+
+    @Override
+    public BgpVersion getVersion() {
+        return BgpVersion.BGP_4;
+    }
+
+    @Override
+    public BgpType getType() {
+        return MSG_TYPE;
+    }
+
+    @Override
+    public BgpHeader getHeader() {
+        return this.bgpMsgHeader;
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass()).toString();
+    }
+}
+
+final class AfiSafiValue {
+    final short afi;
+    final byte reserved;
+    final byte safi;
+
+    public AfiSafiValue(short afi, byte reserved, byte safi) {
+        this.afi = afi;
+        this.reserved = reserved;
+        this.safi = safi;
+    }
+}
diff --git a/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/RouteRefreshCapabilityTlv.java b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/RouteRefreshCapabilityTlv.java
new file mode 100644
index 0000000..6d54a4e
--- /dev/null
+++ b/protocols/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/RouteRefreshCapabilityTlv.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Objects;
+
+/**
+ * Provides RouteRefreshCapabilityTlv.
+ */
+public class RouteRefreshCapabilityTlv implements BgpValueType {
+
+    /*
+       Capability Code = 2
+       Length = 0
+
+       REFERENCE : RFC 2918
+     */
+    private static final Logger log = LoggerFactory.getLogger(RouteRefreshCapabilityTlv.class);
+
+    public static final byte TYPE = 2;
+    public static final byte LENGTH = 0;
+
+    private boolean isSupported = false;
+
+    /**
+     * Constructor to initialize variables.
+     * @param isSupported Is Route Refresh supported
+     */
+    public RouteRefreshCapabilityTlv(boolean isSupported) {
+        this.isSupported = isSupported;
+    }
+
+    /**
+     * Returns object of RouteRefreshCapabilityTlv.
+     * @param isSupported Is Route Refresh supported
+     * @return object of RouteRefreshCapabilityTlv
+     */
+    public static RouteRefreshCapabilityTlv of(boolean isSupported) {
+        return new RouteRefreshCapabilityTlv(isSupported);
+    }
+
+    /**
+     * Returns isSupported value.
+     * @return isSupported value
+     */
+    public boolean isSupported() {
+        return isSupported;
+    }
+
+    @Override
+    public short getType() {
+        return TYPE;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(isSupported);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof RouteRefreshCapabilityTlv) {
+            RouteRefreshCapabilityTlv other = (RouteRefreshCapabilityTlv) obj;
+            return Objects.equals(this.isSupported, other.isSupported);
+        }
+        return false;
+    }
+
+    @Override
+    public int write(ChannelBuffer cb) {
+        int iLenStartIndex = cb.writerIndex();
+        cb.writeByte(TYPE);
+        cb.writeByte(LENGTH);
+
+        return cb.writerIndex() - iLenStartIndex;
+    }
+
+    /**
+     * Reads from channel buffer and returns object of RouteRefreshCapabilityTlv.
+     * @param cb of type channel buffer
+     * @return object of RouteRefreshCapabilityTlv
+     */
+    public static BgpValueType read(ChannelBuffer cb) {
+        //No need to read. If TLV is received, it means it is supported
+
+        return new RouteRefreshCapabilityTlv(true);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("Type", TYPE)
+                .add("Length", LENGTH).toString();
+    }
+
+    @Override
+    public int compareTo(Object o) {
+        return 0;
+    }
+}
diff --git a/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/protocol/BgpOpenMsgTest.java b/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/protocol/BgpOpenMsgTest.java
index a5d1b90..3fe02ee 100644
--- a/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/protocol/BgpOpenMsgTest.java
+++ b/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/protocol/BgpOpenMsgTest.java
@@ -297,7 +297,7 @@
         // OPEN Message with invalid message type.
         byte[] openMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
                                      (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
-                                     (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x1d, 0x05, 0X04,
+                                     (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x00, 0x1d, 0x06, 0X04,
                                      (byte) 0xfe, 0x09, 0x00, (byte) 0xb4, (byte) 0xc0, (byte) 0xa8, 0x00, 0x0f, 0x00 };
 
         ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
diff --git a/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/protocol/BgpRouteRefreshMsgTest.java b/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/protocol/BgpRouteRefreshMsgTest.java
new file mode 100644
index 0000000..408ba0e
--- /dev/null
+++ b/protocols/bgp/bgpio/src/test/java/org/onosproject/bgpio/protocol/BgpRouteRefreshMsgTest.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.protocol;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.buffer.ChannelBuffers;
+import org.junit.Test;
+import org.onosproject.bgpio.exceptions.BgpParseException;
+import org.onosproject.bgpio.types.BgpHeader;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.instanceOf;
+import static org.hamcrest.core.Is.is;
+
+/**
+ * Test case for BGP Route Refresh Message.
+ */
+public class BgpRouteRefreshMsgTest {
+
+    /**
+     * This test case checks BGP Route Refresh message.
+     */
+    @Test
+    public void routeRefreshMessageTest1() throws BgpParseException {
+
+        // BGP RouteRefresh Message
+        byte[] rrMsg = new byte[] {(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                                          (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                                          (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                                          (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
+                                          0x00, 0x17, 0x05, 0x00, 0x02, 0x00, 0x01};
+
+        byte[] testRouteRefreshMsg;
+        ChannelBuffer buffer = ChannelBuffers.dynamicBuffer();
+        buffer.writeBytes(rrMsg);
+
+        BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader();
+        BgpMessage message;
+        BgpHeader bgpHeader = new BgpHeader();
+
+        message = reader.readFrom(buffer, bgpHeader);
+
+        assertThat(message, instanceOf(BgpRouteRefreshMsg.class));
+
+        ChannelBuffer buf = ChannelBuffers.dynamicBuffer();
+        message.writeTo(buf);
+
+        int readLen = buf.writerIndex();
+        testRouteRefreshMsg = new byte[readLen];
+        buf.readBytes(testRouteRefreshMsg, 0, readLen);
+
+        assertThat(testRouteRefreshMsg, is(rrMsg));
+    }
+}