[ONOS-5942] Add LISP multicast LCAF address type

Change-Id: Ifb0e276f33ea9a6512b1a7037f0bbd30e24d276f
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispCanonicalAddressFormatEnum.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispCanonicalAddressFormatEnum.java
index 011a0dc..6a0492f 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispCanonicalAddressFormatEnum.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispCanonicalAddressFormatEnum.java
@@ -21,8 +21,8 @@
  * LCAF defines a canonical address format encoding used in LISP control message
  * and in the encoding of lookup keys for the LISP Mapping Database System.
  *
- * LCAF is defined in draft-ietf-lisp-lcaf-20
- * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-20
+ * LCAF is defined in draft-ietf-lisp-lcaf-22
+ * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22
  */
 public enum LispCanonicalAddressFormatEnum {
     LIST(1),                    // AFI LIST Type
@@ -31,9 +31,9 @@
     APPLICATION_DATA(4),        // Application Data Type
     NAT(7),                     // NAT Traversal Type
     MULTICAST(9),               // Multi-cast Info Type
+    TRAFFIC_ENGINEERING(10),    // Explicit Locator Path Type
     SECURITY(11),               // Security Key Type
     SOURCE_DEST(12),            // Source/Dest Key Type
-    TRAFFIC_ENGINEERING(10),    // Explicit Locator Path Type
     UNSPECIFIED(0),             // Unspecified Type
     UNKNOWN(-1);                // Unknown Type
 
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispLcafAddress.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispLcafAddress.java
index a6b5d46..5aff3b9 100644
--- a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispLcafAddress.java
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispLcafAddress.java
@@ -31,6 +31,7 @@
 import static com.google.common.base.MoreObjects.toStringHelper;
 import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.APPLICATION_DATA;
 import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.LIST;
+import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.MULTICAST;
 import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.NAT;
 import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.SEGMENT;
 import static org.onosproject.lisp.msg.types.lcaf.LispCanonicalAddressFormatEnum.SOURCE_DEST;
@@ -411,6 +412,10 @@
                 return new LispSegmentLcafAddress.SegmentLcafAddressReader().readFrom(byteBuf);
             }
 
+            if (lcafType == MULTICAST.getLispCode()) {
+                return new LispMulticastLcafAddress.MulticastLcafAddressReader().readFrom(byteBuf);
+            }
+
             if (lcafType == SOURCE_DEST.getLispCode()) {
                 return new LispSourceDestLcafAddress.SourceDestLcafAddressReader().readFrom(byteBuf);
             }
@@ -451,6 +456,10 @@
                     new LispSegmentLcafAddress.SegmentLcafAddressWriter().writeTo(byteBuf,
                             (LispSegmentLcafAddress) address);
                     break;
+                case MULTICAST:
+                    new LispMulticastLcafAddress.MulticastLcafAddressWriter().writeTo(byteBuf,
+                            (LispMulticastLcafAddress) address);
+                    break;
                 case SOURCE_DEST:
                     new LispSourceDestLcafAddress.SourceDestLcafAddressWriter().writeTo(byteBuf,
                             (LispSourceDestLcafAddress) address);
diff --git a/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispMulticastLcafAddress.java b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispMulticastLcafAddress.java
new file mode 100644
index 0000000..771b3ca
--- /dev/null
+++ b/protocols/lisp/msg/src/main/java/org/onosproject/lisp/msg/types/lcaf/LispMulticastLcafAddress.java
@@ -0,0 +1,311 @@
+/*
+ * Copyright 2017-present 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.lisp.msg.types.lcaf;
+
+import io.netty.buffer.ByteBuf;
+import org.onosproject.lisp.msg.exceptions.LispParseError;
+import org.onosproject.lisp.msg.exceptions.LispReaderException;
+import org.onosproject.lisp.msg.exceptions.LispWriterException;
+import org.onosproject.lisp.msg.types.LispAddressReader;
+import org.onosproject.lisp.msg.types.LispAddressWriter;
+import org.onosproject.lisp.msg.types.LispAfiAddress;
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Multicast group membership type LCAF address class.
+ * <p>
+ * Multicast group membership type is defined in draft-ietf-lisp-lcaf-22
+ * https://tools.ietf.org/html/draft-ietf-lisp-lcaf-22#page-15
+ * <p>
+ * <pre>
+ * {@literal
+ *  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
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |           AFI = 16387         |     Rsvd1     |     Flags     |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |   Type = 9    |     Rsvd2     |             Length            |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                         Instance-ID                           |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |            Reserved           | Source MaskLen| Group MaskLen |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |              AFI = x          |   Source/Subnet Address  ...  |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |              AFI = x          |       Group Address  ...      |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * }</pre>
+ */
+public final class LispMulticastLcafAddress extends LispLcafAddress {
+
+    private final int instanceId;
+    private final byte srcMaskLenth;
+    private final byte grpMaskLength;
+    private final LispAfiAddress srcAddress;
+    private final LispAfiAddress grpAddress;
+
+    /**
+     * Initializes multicast type LCAF address.
+     *
+     * @param instanceId    instance identifier
+     * @param srcMaskLenth  source mask length
+     * @param grpMaskLength group mask length
+     * @param srcAddress    source address
+     * @param grpAddress    group address
+     */
+    private LispMulticastLcafAddress(int instanceId, byte srcMaskLenth,
+                                     byte grpMaskLength, LispAfiAddress srcAddress,
+                                     LispAfiAddress grpAddress) {
+        super(LispCanonicalAddressFormatEnum.MULTICAST);
+        this.instanceId = instanceId;
+        this.srcMaskLenth = srcMaskLenth;
+        this.grpMaskLength = grpMaskLength;
+        this.srcAddress = srcAddress;
+        this.grpAddress = grpAddress;
+    }
+
+    /**
+     * Obtains instance identifier.
+     *
+     * @return instance identifier
+     */
+    public int getInstanceId() {
+        return instanceId;
+    }
+
+    /**
+     * Obtains source mask length.
+     *
+     * @return source mask length
+     */
+    public byte getSrcMaskLenth() {
+        return srcMaskLenth;
+    }
+
+    /**
+     * Obtains group mask length.
+     *
+     * @return group mask length
+     */
+    public byte getGrpMaskLength() {
+        return grpMaskLength;
+    }
+
+    /**
+     * Obtains source address.
+     *
+     * @return source address
+     */
+    public LispAfiAddress getSrcAddress() {
+        return srcAddress;
+    }
+
+    /**
+     * Obtains group address.
+     *
+     * @return group address
+     */
+    public LispAfiAddress getGrpAddress() {
+        return grpAddress;
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(instanceId, srcMaskLenth, grpMaskLength,
+                srcAddress, grpAddress);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+
+        if (obj instanceof LispMulticastLcafAddress) {
+            final LispMulticastLcafAddress other = (LispMulticastLcafAddress) obj;
+            return Objects.equals(this.instanceId, other.instanceId) &&
+                    Objects.equals(this.srcMaskLenth, other.srcMaskLenth) &&
+                    Objects.equals(this.grpMaskLength, other.grpMaskLength) &&
+                    Objects.equals(this.srcAddress, other.srcAddress) &&
+                    Objects.equals(this.grpAddress, other.grpAddress);
+
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("instance ID", instanceId)
+                .add("source mask length", srcMaskLenth)
+                .add("group mask length", grpMaskLength)
+                .add("source address", srcAddress)
+                .add("group address", grpAddress)
+                .toString();
+    }
+
+    public static final class MulticastAddressBuilder
+                        extends LcafAddressBuilder<MulticastAddressBuilder> {
+        private int instanceId;
+        private byte srcMaskLenth;
+        private byte grpMaskLength;
+        private LispAfiAddress srcAddress;
+        private LispAfiAddress grpAddress;
+
+        /**
+         * Sets instance identifier.
+         *
+         * @param instanceId instance identifier
+         * @return MulticastAddressBuilder object
+         */
+        public MulticastAddressBuilder withInstanceId(int instanceId) {
+            this.instanceId = instanceId;
+            return this;
+        }
+
+        /**
+         * Sets source mask length.
+         *
+         * @param srcMaskLenth source mask length
+         * @return MulticastAddressBuilder object
+         */
+        public MulticastAddressBuilder withSrcMaskLength(byte srcMaskLenth) {
+            this.srcMaskLenth = srcMaskLenth;
+            return this;
+        }
+
+        /**
+         * Sets group mask length.
+         *
+         * @param grpMaskLength group mask length
+         * @return MulticastAddressBuilder object
+         */
+        public MulticastAddressBuilder withGrpMaskLength(byte grpMaskLength) {
+            this.grpMaskLength = grpMaskLength;
+            return this;
+        }
+
+        /**
+         * Sets source address.
+         *
+         * @param srcAddress source address
+         * @return MulticastAddressBuilder object
+         */
+        public MulticastAddressBuilder withSrcAddress(LispAfiAddress srcAddress) {
+            this.srcAddress = srcAddress;
+            return this;
+        }
+
+        /**
+         * Sets group address.
+         *
+         * @param grpAddress group address
+         * @return MulticastAddressBuilder object
+         */
+        public MulticastAddressBuilder withGrpAddress(LispAfiAddress grpAddress) {
+            this.grpAddress = grpAddress;
+            return this;
+        }
+
+        /**
+         * Builds LispMulticastLcafAddress instance.
+         *
+         * @return LispMulticastLcafAddress instance
+         */
+        public LispMulticastLcafAddress build() {
+
+            checkNotNull(srcAddress, "Must specify a source address");
+            checkNotNull(grpAddress, "Must specify a group address");
+
+            return new LispMulticastLcafAddress(instanceId, srcMaskLenth,
+                                        grpMaskLength, srcAddress, grpAddress);
+        }
+    }
+
+    /**
+     * Multicast LCAF address reader class.
+     */
+    public static class MulticastLcafAddressReader
+                        implements LispAddressReader<LispMulticastLcafAddress> {
+
+        private static final int RESERVED_SKIP_LENGTH = 2;
+
+        @Override
+        public LispMulticastLcafAddress readFrom(ByteBuf byteBuf)
+                                        throws LispParseError, LispReaderException {
+
+            LispLcafAddress.deserializeCommon(byteBuf);
+
+            int instanceId = (int) byteBuf.readUnsignedInt();
+            byteBuf.skipBytes(RESERVED_SKIP_LENGTH);
+            byte srcMaskLength = (byte) byteBuf.readUnsignedByte();
+            byte grpMaskLength = (byte) byteBuf.readUnsignedByte();
+            LispAfiAddress srcAddress = new AfiAddressReader().readFrom(byteBuf);
+            LispAfiAddress grpAddress = new AfiAddressReader().readFrom(byteBuf);
+
+            return new MulticastAddressBuilder()
+                            .withInstanceId(instanceId)
+                            .withSrcMaskLength(srcMaskLength)
+                            .withGrpMaskLength(grpMaskLength)
+                            .withSrcAddress(srcAddress)
+                            .withGrpAddress(grpAddress)
+                            .build();
+        }
+    }
+
+    /**
+     * Multicast LCAF address writer class.
+     */
+    public static class MulticastLcafAddressWriter
+                        implements LispAddressWriter<LispMulticastLcafAddress> {
+
+        private static final int UNUSED_ZERO = 0;
+
+        @Override
+        public void writeTo(ByteBuf byteBuf, LispMulticastLcafAddress address)
+                                                    throws LispWriterException {
+            int lcafIndex = byteBuf.writerIndex();
+            LispLcafAddress.serializeCommon(byteBuf, address);
+
+            // instance identifier
+            byteBuf.writeInt(address.getInstanceId());
+
+            // reserved field
+            byteBuf.writeByte(UNUSED_ZERO);
+            byteBuf.writeByte(UNUSED_ZERO);
+
+            // source mask length
+            byteBuf.writeByte(address.getSrcMaskLenth());
+
+            // group mask length
+            byteBuf.writeByte(address.getGrpMaskLength());
+
+            // source address
+            AfiAddressWriter srcWriter = new AfiAddressWriter();
+            srcWriter.writeTo(byteBuf, address.getSrcAddress());
+
+            // group address
+            AfiAddressWriter grpWriter = new AfiAddressWriter();
+            grpWriter.writeTo(byteBuf, address.getGrpAddress());
+
+            LispLcafAddress.updateLength(lcafIndex, byteBuf);
+        }
+    }
+}
\ No newline at end of file
diff --git a/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/types/lcaf/LispMulticastLcafAddressTest.java b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/types/lcaf/LispMulticastLcafAddressTest.java
new file mode 100644
index 0000000..702a454
--- /dev/null
+++ b/protocols/lisp/msg/src/test/java/org/onosproject/lisp/msg/types/lcaf/LispMulticastLcafAddressTest.java
@@ -0,0 +1,121 @@
+/*
+ * Copyright 2017-present 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.lisp.msg.types.lcaf;
+
+import com.google.common.testing.EqualsTester;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.lisp.msg.exceptions.LispParseError;
+import org.onosproject.lisp.msg.exceptions.LispReaderException;
+import org.onosproject.lisp.msg.exceptions.LispWriterException;
+import org.onosproject.lisp.msg.types.LispIpv4Address;
+import org.onosproject.lisp.msg.types.lcaf.LispMulticastLcafAddress.MulticastAddressBuilder;
+import org.onosproject.lisp.msg.types.lcaf.LispMulticastLcafAddress.MulticastLcafAddressReader;
+import org.onosproject.lisp.msg.types.lcaf.LispMulticastLcafAddress.MulticastLcafAddressWriter;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+/**
+ * Unit tests for LispMulticastLcafAddress class.
+ */
+public class LispMulticastLcafAddressTest {
+
+    private static final String IP_ADDRESS_1 = "192.168.1.1";
+    private static final String IP_ADDRESS_2 = "192.168.1.2";
+
+    private LispMulticastLcafAddress address1;
+    private LispMulticastLcafAddress sameAsAddress1;
+    private LispMulticastLcafAddress address2;
+
+    @Before
+    public void setup() {
+
+        MulticastAddressBuilder builder1 = new MulticastAddressBuilder();
+
+        LispIpv4Address ipv4Address1 =
+                new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1));
+
+        address1 = builder1
+                .withInstanceId(1)
+                .withSrcMaskLength((byte) 0x24)
+                .withGrpMaskLength((byte) 0x24)
+                .withSrcAddress(ipv4Address1)
+                .withGrpAddress(ipv4Address1)
+                .build();
+
+        MulticastAddressBuilder builder2 = new MulticastAddressBuilder();
+
+        sameAsAddress1 = builder2
+                .withInstanceId(1)
+                .withSrcMaskLength((byte) 0x24)
+                .withGrpMaskLength((byte) 0x24)
+                .withSrcAddress(ipv4Address1)
+                .withGrpAddress(ipv4Address1)
+                .build();
+
+        MulticastAddressBuilder builder3 = new MulticastAddressBuilder();
+
+        LispIpv4Address ipv4Address2 =
+                new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_2));
+
+        address2 = builder3
+                .withInstanceId(2)
+                .withSrcMaskLength((byte) 0x24)
+                .withGrpMaskLength((byte) 0x24)
+                .withSrcAddress(ipv4Address2)
+                .withGrpAddress(ipv4Address2)
+                .build();
+    }
+
+    @Test
+    public void testEquality() {
+        new EqualsTester()
+                .addEqualityGroup(address1, sameAsAddress1)
+                .addEqualityGroup(address2).testEquals();
+    }
+
+    @Test
+    public void testConstruction() {
+        LispMulticastLcafAddress multicastLcafAddress = address1;
+
+        LispIpv4Address ipv4Address =
+                new LispIpv4Address(IpAddress.valueOf(IP_ADDRESS_1));
+
+        assertThat(multicastLcafAddress.getInstanceId(), is(1));
+        assertThat(multicastLcafAddress.getSrcMaskLenth(), is((byte) 0x24));
+        assertThat(multicastLcafAddress.getGrpMaskLength(), is((byte) 0x24));
+        assertThat(multicastLcafAddress.getSrcAddress(), is(ipv4Address));
+        assertThat(multicastLcafAddress.getGrpAddress(), is(ipv4Address));
+    }
+
+    @Test
+    public void testSerialization() throws LispWriterException, LispParseError,
+            LispReaderException {
+        ByteBuf byteBuf = Unpooled.buffer();
+
+        MulticastLcafAddressWriter writer = new MulticastLcafAddressWriter();
+        writer.writeTo(byteBuf, address1);
+
+        MulticastLcafAddressReader reader = new MulticastLcafAddressReader();
+        LispMulticastLcafAddress deserialized = reader.readFrom(byteBuf);
+
+        new EqualsTester().addEqualityGroup(address1, deserialized).testEquals();
+    }
+}