[Emu][ONOS-2593, ONOS-2594] BGP SBI controller and session handler

Change-Id: Ia95717ff173b2e3e1198bdd0fafef7cb0aa8f734
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/BGPParseException.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/BGPParseException.java
new file mode 100755
index 0000000..62427a4
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/exceptions/BGPParseException.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright 2015 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.exceptions;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+
+/**
+ * Custom Exception for BGP IO.
+ */
+public class BGPParseException extends Exception {
+
+    private static final long serialVersionUID = 1L;
+    private byte errorCode;
+    private byte errorSubCode;
+    private ChannelBuffer data;
+
+    /**
+     * Default constructor to create a new exception.
+     */
+    public BGPParseException() {
+        super();
+    }
+
+    /**
+     * Constructor to create exception from message and cause.
+     *
+     * @param message  the detail of exception in string
+     * @param cause underlying cause of the error
+     */
+    public BGPParseException(final String message, final Throwable cause) {
+        super(message, cause);
+    }
+
+    /**
+     * Constructor to create exception from message.
+     *
+     * @param message the detail of exception in string
+     */
+    public BGPParseException(final String message) {
+        super(message);
+    }
+
+    /**
+     * Constructor to create exception from cause.
+     *
+     * @param cause underlying cause of the error
+     */
+    public BGPParseException(final Throwable cause) {
+        super(cause);
+    }
+
+    /**
+     * Constructor to create exception from error code and error subcode.
+     *
+     * @param errorCode error code of BGP message
+     * @param errorSubCode error subcode of BGP message
+     * @param data error data of BGP message
+     */
+    public BGPParseException(final byte errorCode, final byte errorSubCode, final ChannelBuffer data) {
+        super();
+        this.errorCode = errorCode;
+        this.errorSubCode = errorSubCode;
+        this.data = data;
+    }
+
+    /**
+     * Returns errorcode for this exception.
+     *
+     * @return errorcode for this exception
+     */
+    public byte getErrorCode() {
+        return this.errorCode;
+    }
+
+    /**
+     * Returns error Subcode for this exception.
+     *
+     * @return error Subcode for this exception
+     */
+    public byte getErrorSubCode() {
+        return this.errorSubCode;
+    }
+
+    /**
+     * Returns error data for this exception.
+     *
+     * @return error data for this exception
+     */
+    public ChannelBuffer getData() {
+        return this.data;
+    }
+}
\ No newline at end of file
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessage.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessage.java
new file mode 100644
index 0000000..a5d8154
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessage.java
@@ -0,0 +1,92 @@
+/*
+ * Copyright 2015 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.protocol;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BGPParseException;
+import org.onosproject.bgpio.types.BGPHeader;
+
+/**
+ * Abstraction of an entity providing BGP Messages.
+ */
+public interface BGPMessage extends Writeable {
+    /**
+     * Returns BGP Header of BGP Message.
+     *
+     * @return BGP Header of BGP Message
+     */
+    BGPHeader getHeader();
+
+    /**
+     * Returns version of BGP Message.
+     *
+     * @return version of BGP Message
+     */
+    BGPVersion getVersion();
+
+    /**
+     * Returns BGP Type of BGP Message.
+     *
+     * @return BGP Type of BGP Message
+     */
+    BGPType getType();
+
+    @Override
+    void writeTo(ChannelBuffer cb) throws BGPParseException;
+
+    /**
+     * Builder interface with get and set functions to build BGP Message.
+     */
+    interface Builder {
+        /**
+         * Builds BGP Message.
+         *
+         * @return BGP Message
+         * @throws BGPParseException while building bgp message
+         */
+        BGPMessage build() throws BGPParseException;
+
+        /**
+         * Returns BGP Version of BGP Message.
+         *
+         * @return BGP Version of BGP Message
+         */
+        BGPVersion getVersion();
+
+        /**
+         * Returns BGP Type of BGP Message.
+         *
+         * @return BGP Type of BGP Message
+         */
+        BGPType getType();
+
+        /**
+         * Returns BGP Header of BGP Message.
+         *
+         * @return BGP Header of BGP Message
+         */
+        BGPHeader getHeader();
+
+        /**
+         * Sets BgpHeader and return its builder.
+         *
+         * @param bgpMsgHeader BGP Message Header
+         * @return builder by setting BGP message header
+         */
+        Builder setHeader(BGPHeader bgpMsgHeader);
+    }
+}
\ No newline at end of file
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageReader.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageReader.java
new file mode 100755
index 0000000..18b8f58
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPMessageReader.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2015 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.protocol;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BGPParseException;
+import org.onosproject.bgpio.types.BGPHeader;
+
+/**
+ * Abstraction of an entity providing BGP Message Reader.
+ */
+public interface BGPMessageReader<T> {
+
+    /**
+     * Reads the Objects in the BGP Message and Returns BGP Message.
+     *
+     * @param cb Channel Buffer
+     * @param bgpHeader BGP message header
+     * @return BGP Message
+     * @throws BGPParseException while parsing BGP message.
+     */
+    T readFrom(ChannelBuffer cb, BGPHeader bgpHeader) throws BGPParseException;
+}
\ No newline at end of file
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPType.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPType.java
new file mode 100755
index 0000000..d334915
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPType.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2015 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.protocol;
+
+/**
+ * Enum to Provide the Different types of BGP messages.
+ */
+public enum BGPType {
+
+    NONE(0), OPEN(1), UPDATE(2), NOTIFICATION(3), KEEP_ALIVE(4);
+
+    int value;
+
+    /**
+     * Assign value with the value val as the types of BGP message.
+     *
+     * @param val type of BGP message
+     */
+    BGPType(int val) {
+        value = val;
+    }
+
+    /**
+     * Returns value as type of BGP message.
+     *
+     * @return value type of BGP message
+     */
+    public byte getType() {
+        return (byte) value;
+    }
+}
\ No newline at end of file
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPVersion.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPVersion.java
new file mode 100755
index 0000000..97bc7dc
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/BGPVersion.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2015 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.protocol;
+
+/**
+ * Enum to provide BGP Message Version.
+ */
+public enum BGPVersion {
+
+    BGP_4(4);
+
+    public final int packetVersion;
+
+    /**
+     * Assign BGP PacketVersion with specified packetVersion.
+     *
+     * @param packetVersion version of BGP
+     */
+    BGPVersion(final int packetVersion) {
+        this.packetVersion = packetVersion;
+    }
+
+    /**
+     * Returns Packet version of BGP Message.
+     *
+     * @return packetVersion
+     */
+    public int getPacketVersion() {
+        return packetVersion;
+    }
+}
\ No newline at end of file
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/Writeable.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/Writeable.java
new file mode 100755
index 0000000..72df7f3
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/protocol/Writeable.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2015 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.protocol;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.bgpio.exceptions.BGPParseException;
+
+/**
+ * Abstraction of an entity providing functionality to write byte streams of
+ * Messages to channel buffer.
+ */
+public interface Writeable {
+
+    /**
+     * Writes byte streams of messages to channel buffer.
+     *
+     * @param cb channelBuffer
+     * @throws BGPParseException when error occurs while writing BGP message to channel buffer
+     */
+    void writeTo(ChannelBuffer cb) throws BGPParseException;
+}
\ No newline at end of file
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPHeader.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPHeader.java
new file mode 100755
index 0000000..6acda0d
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPHeader.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2015 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 org.jboss.netty.buffer.ChannelBuffer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provides BGP Message Header which is common for all the Messages.
+ */
+
+public class BGPHeader {
+
+    /*      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     |
+          +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+    */
+
+    protected static final Logger log = LoggerFactory.getLogger(BGPHeader.class);
+
+    public static final int MARKER_LENGTH = 16;
+    public static final short DEFAULT_HEADER_LENGTH = 19;
+
+    private byte[] marker;
+    private byte type;
+    private short length;
+
+    /**
+     * Reset fields.
+     */
+    public BGPHeader() {
+        this.marker = null;
+        this.length = 0;
+        this.type = 0;
+    }
+
+    /**
+     * Constructors to initialize parameters.
+     *
+     * @param marker field in BGP header
+     * @param length message length
+     * @param type message type
+     */
+    public BGPHeader(byte[] marker, short length, byte type) {
+        this.marker = marker;
+        this.length = length;
+        this.type = type;
+    }
+
+    /**
+     * Sets marker field.
+     *
+     * @param value marker field
+     */
+    public void setMarker(byte[] value) {
+        this.marker = value;
+    }
+
+    /**
+     * Sets message type.
+     *
+     * @param value message type
+     */
+    public void setType(byte value) {
+        this.type = value;
+    }
+
+    /**
+     * Sets message length.
+     *
+     * @param value message length
+     */
+    public void setLength(short value) {
+        this.length = value;
+    }
+
+    /**
+     * Returns message length.
+     *
+     * @return message length
+     */
+    public short getLength() {
+        return this.length;
+    }
+
+    /**
+     * Returns message marker.
+     *
+     * @return message marker
+     */
+    public byte[] getMarker() {
+        return this.marker;
+    }
+
+    /**
+     * Returns message type.
+     *
+     * @return message type
+     */
+    public byte getType() {
+        return this.type;
+    }
+
+    /**
+     * Writes Byte stream of BGP header to channel buffer.
+     *
+     * @param cb ChannelBuffer
+     * @return length index of message header
+     */
+    public int write(ChannelBuffer cb) {
+
+        cb.writeBytes(getMarker(), 0, MARKER_LENGTH);
+
+        int headerLenIndex = cb.writerIndex();
+        cb.writeShort((short) 0);
+        cb.writeByte(type);
+
+        return headerLenIndex;
+    }
+
+    /**
+     * Read from channel buffer and Returns BGP header.
+     *
+     * @param cb ChannelBuffer
+     * @return object of BGPHeader
+     */
+    public static BGPHeader read(ChannelBuffer cb) {
+
+        byte[] marker = new byte[MARKER_LENGTH];
+        byte type;
+        short length;
+        cb.readBytes(marker, 0, MARKER_LENGTH);
+        length = cb.readShort();
+        type = cb.readByte();
+        return new BGPHeader(marker, length, type);
+    }
+}
\ No newline at end of file
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPValueType.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPValueType.java
new file mode 100755
index 0000000..54a8b43
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/BGPValueType.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2015 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 org.jboss.netty.buffer.ChannelBuffer;
+
+/**
+ * Abstraction which Provides the BGP of TLV format.
+ */
+public interface BGPValueType {
+    /**
+     * Returns the Type of BGP Message.
+     *
+     * @return short value of type
+     */
+    short getType();
+
+    /**
+     * Writes the byte Stream of BGP Message to channel buffer.
+     *
+     * @param cb channel buffer
+     * @return length written to channel buffer
+     */
+    int write(ChannelBuffer cb);
+}
\ No newline at end of file