[Emu] [ONOS-2602]Implement BGP Update Protocol Message Mpreach and MPunreach parse and decode(Node, Link and Prefix)

Change-Id: Idba57626625cd1207b2b0fbddd39214faca337b6
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPv4AddressTlv.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPv4AddressTlv.java
new file mode 100644
index 0000000..d5f0326
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPv4AddressTlv.java
@@ -0,0 +1,128 @@
+/*
+ * 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 java.net.InetAddress;
+import java.util.Objects;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.bgpio.exceptions.BGPParseException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+
+/**
+ * Provides Implementation of IPv4AddressTlv.
+ */
+public class IPv4AddressTlv implements BGPValueType {
+    private static final Logger log = LoggerFactory.getLogger(IPv4AddressTlv.class);
+    private static final int LENGTH = 4;
+
+    private Ip4Address address;
+    private short type;
+
+    /**
+     * Constructor to initialize parameters.
+     *
+     * @param address Ipv4 address of interface/neighbor
+     * @param type address type
+     */
+    public IPv4AddressTlv(Ip4Address address, short type) {
+        this.address = Preconditions.checkNotNull(address);
+        this.type = type;
+    }
+
+    /**
+     * Returns Ipv4 address of interface/neighbor.
+     *
+     * @return Ipv4 address of interface/neighbor
+     */
+    public Ip4Address getValue() {
+        return address;
+    }
+
+    @Override
+    public short getType() {
+        return this.type;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(address);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof IPv4AddressTlv) {
+            IPv4AddressTlv other = (IPv4AddressTlv) obj;
+            return Objects.equals(this.address, other.address) && Objects.equals(this.type, other.type);
+        }
+        return false;
+    }
+
+    @Override
+    public int write(ChannelBuffer cb) {
+        int iLenStartIndex = cb.writerIndex();
+        cb.writeShort(type);
+        cb.writeShort(LENGTH);
+        cb.writeInt(address.toInt());
+        return cb.writerIndex() - iLenStartIndex;
+    }
+
+    /**
+     * Reads the channel buffer and returns object of IPv4AddressTlv.
+     *
+     * @param cb channelBuffer
+     * @param type address type
+     * @return object of IPv4AddressTlv
+     * @throws BGPParseException while parsing IPv4AddressTlv
+     */
+    public static IPv4AddressTlv read(ChannelBuffer cb, short type) throws BGPParseException {
+        //TODO: use Validation.toInetAddress once Validation is merged
+        InetAddress ipAddress = (InetAddress) cb.readBytes(LENGTH);
+        if (ipAddress.isMulticastAddress()) {
+            throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
+        }
+        Ip4Address address = Ip4Address.valueOf(ipAddress);
+        return IPv4AddressTlv.of(address, type);
+    }
+
+    /**
+     * Returns object of this class with specified values.
+     *
+     * @param address Ipv4 interface/neighbor Address
+     * @param type says Ipv4 address of interface/neighbor tlv type
+     * @return object of this class
+     */
+    public static IPv4AddressTlv of(final Ip4Address address , final short type) {
+        return new IPv4AddressTlv(address, type);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("type", type)
+                .add("LENGTH", LENGTH)
+                .add("address", address)
+                .toString();
+    }
+}
\ No newline at end of file
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPv6AddressTlv.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPv6AddressTlv.java
new file mode 100644
index 0000000..65b7c16
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/IPv6AddressTlv.java
@@ -0,0 +1,128 @@
+/*
+ * 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 java.net.InetAddress;
+import java.util.Objects;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onlab.packet.Ip6Address;
+import org.onosproject.bgpio.exceptions.BGPParseException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.base.Preconditions;
+
+/**
+ * Provides Implementation of IPv6AddressTlv.
+ */
+public class IPv6AddressTlv implements BGPValueType {
+    private static final Logger log = LoggerFactory.getLogger(IPv6AddressTlv.class);
+    private static final int LENGTH = 16;
+
+    private final Ip6Address address;
+    private short type;
+
+    /**
+     * Constructor to initialize parameters.
+     *
+     * @param address Ipv6 address of interface/neighbor
+     * @param type address type
+     */
+    public IPv6AddressTlv(Ip6Address address, short type) {
+        this.address = Preconditions.checkNotNull(address);
+        this.type = type;
+    }
+
+    /**
+     * Returns Ipv6 address of interface/neighbor.
+     *
+     * @return Ipv6 address of interface/neighbor
+     */
+    public Ip6Address getValue() {
+        return address;
+    }
+
+    @Override
+    public short getType() {
+        return type;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(address);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof IPv6AddressTlv) {
+            IPv6AddressTlv other = (IPv6AddressTlv) obj;
+            return Objects.equals(this.address, other.address) && Objects.equals(this.type, other.type);
+        }
+        return false;
+    }
+
+    @Override
+    public int write(ChannelBuffer cb) {
+        int iLenStartIndex = cb.writerIndex();
+        cb.writeShort(type);
+        cb.writeShort(LENGTH);
+        cb.writeBytes(address.toOctets());
+        return cb.writerIndex() - iLenStartIndex;
+    }
+
+    /**
+     * Reads the channel buffer and returns object of IPv6AddressTlv.
+     *
+     * @param cb channelBuffer
+     * @param type address type
+     * @return object of IPv6AddressTlv
+     * @throws BGPParseException while parsing IPv6AddressTlv
+     */
+    public static IPv6AddressTlv read(ChannelBuffer cb, short type) throws BGPParseException {
+        //TODO: use Validation.toInetAddress once Validation is merged
+        InetAddress ipAddress = (InetAddress) cb.readBytes(LENGTH);
+        if (ipAddress.isMulticastAddress()) {
+            throw new BGPParseException(BGPErrorType.UPDATE_MESSAGE_ERROR, (byte) 0, null);
+        }
+        Ip6Address address = Ip6Address.valueOf(ipAddress);
+        return IPv6AddressTlv.of(address, type);
+    }
+
+    /**
+     * Returns object of this class with specified values.
+     *
+     * @param address Ipv6 interface/neighbor address
+     * @param type says Ipv6 address of interface/neighbor tlv type
+     * @return object of this class
+     */
+    public static IPv6AddressTlv of(final Ip6Address address , final short type) {
+        return new IPv6AddressTlv(address, type);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("type", type)
+                .add("LENGTH", LENGTH)
+                .add("address", address)
+                .toString();
+    }
+}
\ No newline at end of file
diff --git a/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/LinkLocalRemoteIdentifiersTlv.java b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/LinkLocalRemoteIdentifiersTlv.java
new file mode 100644
index 0000000..988925f
--- /dev/null
+++ b/bgp/bgpio/src/main/java/org/onosproject/bgpio/types/LinkLocalRemoteIdentifiersTlv.java
@@ -0,0 +1,131 @@
+/*
+ * 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 java.util.Objects;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.MoreObjects;
+
+/**
+ * Provides Implementation of Link Local/Remote IdentifiersTlv.
+ */
+public class LinkLocalRemoteIdentifiersTlv implements BGPValueType {
+    private static final Logger log = LoggerFactory.getLogger(LinkLocalRemoteIdentifiersTlv.class);
+    public static final short TYPE = 258;
+    private static final int LENGTH = 8;
+
+    private final int linkLocalIdentifer;
+    private final int linkRemoteIdentifer;
+
+    /**
+     * Constructor to initialize parameters.
+     *
+     * @param linkLocalIdentifer link local Identifer
+     * @param linkRemoteIdentifer link remote Identifer
+     */
+    public LinkLocalRemoteIdentifiersTlv(int linkLocalIdentifer, int linkRemoteIdentifer) {
+        this.linkLocalIdentifer = linkLocalIdentifer;
+        this.linkRemoteIdentifer = linkRemoteIdentifer;
+    }
+
+    /**
+     * Returns link remote Identifer.
+     *
+     * @return link remote Identifer
+     */
+    public int getLinkRemoteIdentifier() {
+        return linkRemoteIdentifer;
+    }
+
+    /**
+     * Returns link local Identifer.
+     *
+     * @return link local Identifer
+     */
+    public int getLinkLocalIdentifier() {
+        return linkLocalIdentifer;
+    }
+
+    @Override
+    public short getType() {
+        return TYPE;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(linkLocalIdentifer, linkRemoteIdentifer);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj instanceof LinkLocalRemoteIdentifiersTlv) {
+            LinkLocalRemoteIdentifiersTlv other = (LinkLocalRemoteIdentifiersTlv) obj;
+            return Objects.equals(this.linkLocalIdentifer, other.linkLocalIdentifer)
+                    && Objects.equals(this.linkRemoteIdentifer, other.linkRemoteIdentifer);
+        }
+        return false;
+    }
+
+    @Override
+    public int write(ChannelBuffer cb) {
+        int iLenStartIndex = cb.writerIndex();
+        cb.writeShort(TYPE);
+        cb.writeShort(LENGTH);
+        cb.writeInt(linkLocalIdentifer);
+        cb.writeInt(linkRemoteIdentifer);
+        return cb.writerIndex() - iLenStartIndex;
+    }
+
+    /**
+     * Reads the channel buffer and returns object of LinkLocalRemoteIdentifiersTlv.
+     *
+     * @param cb channelBuffer
+     * @return object of LinkLocalRemoteIdentifiersTlv
+     */
+    public static LinkLocalRemoteIdentifiersTlv read(ChannelBuffer cb) {
+        int linkLocalIdentifer = cb.readInt();
+        int linkRemoteIdentifer = cb.readInt();
+        return LinkLocalRemoteIdentifiersTlv.of(linkLocalIdentifer, linkRemoteIdentifer);
+    }
+
+    /**
+     * Returns object of this class with specified link local identifer and link remote identifer.
+     *
+     * @param linkLocalIdentifer link local identifier
+     * @param linkRemoteIdentifer link remote identifier
+     * @return object of LinkLocalRemoteIdentifiersTlv
+     */
+    public static LinkLocalRemoteIdentifiersTlv of(final int linkLocalIdentifer, final int linkRemoteIdentifer) {
+        return new LinkLocalRemoteIdentifiersTlv(linkLocalIdentifer, linkRemoteIdentifer);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .add("TYPE", TYPE)
+                .add("LENGTH", LENGTH)
+                .add("linkLocalIdentifer", linkLocalIdentifer)
+                .add("linkRemoteIdentifer", linkRemoteIdentifer)
+                .toString();
+    }
+}
\ No newline at end of file