[ONOS-6169] Implement codec for multicast and list ext addresses

Change-Id: I784a893552fdc4eb798f4ccac38512a5d081b5ea
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/LispMappingExtensionCodecRegistrator.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/LispMappingExtensionCodecRegistrator.java
index d41fd0c..99f3988 100644
--- a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/LispMappingExtensionCodecRegistrator.java
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/LispMappingExtensionCodecRegistrator.java
@@ -24,6 +24,8 @@
 import org.onosproject.drivers.lisp.extensions.codec.LispAppDataAddressCodec;
 import org.onosproject.drivers.lisp.extensions.codec.LispAsAddressCodec;
 import org.onosproject.drivers.lisp.extensions.codec.LispGcAddressCodec;
+import org.onosproject.drivers.lisp.extensions.codec.LispListAddressCodec;
+import org.onosproject.drivers.lisp.extensions.codec.LispMulticastAddressCodec;
 import org.onosproject.mapping.web.MappingCodecRegistrator;
 import org.slf4j.Logger;
 
@@ -52,6 +54,8 @@
         codecService.registerCodec(LispAppDataAddress.class, new LispAppDataAddressCodec());
         codecService.registerCodec(LispAsAddress.class, new LispAsAddressCodec());
         codecService.registerCodec(LispGcAddress.class, new LispGcAddressCodec());
+        codecService.registerCodec(LispListAddress.class, new LispListAddressCodec());
+        codecService.registerCodec(LispMulticastAddress.class, new LispMulticastAddressCodec());
 
         log.info("Started");
     }
@@ -61,6 +65,8 @@
         codecService.unregisterCodec(LispAppDataAddress.class);
         codecService.unregisterCodec(LispAsAddress.class);
         codecService.unregisterCodec(LispGcAddress.class);
+        codecService.unregisterCodec(LispListAddress.class);
+        codecService.unregisterCodec(LispMulticastAddress.class);
 
         registrator.deactivate();
         registrator = null;
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispListAddressCodec.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispListAddressCodec.java
new file mode 100644
index 0000000..4e5c197
--- /dev/null
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispListAddressCodec.java
@@ -0,0 +1,97 @@
+/*
+ * 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.drivers.lisp.extensions.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.drivers.lisp.extensions.LispListAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * LISP list address codec.
+ */
+public final class LispListAddressCodec extends JsonCodec<LispListAddress> {
+
+    private final Logger log = getLogger(getClass());
+
+    protected static final String IPV4 = "ipv4";
+    protected static final String IPV6 = "ipv6";
+
+    private static final String MISSING_MEMBER_MESSAGE =
+                                " member is required in LispListAddress";
+
+    @Override
+    public ObjectNode encode(LispListAddress address, CodecContext context) {
+        checkNotNull(address, "LispListAddress cannot be null");
+
+        final ObjectNode result = context.mapper().createObjectNode();
+
+        final JsonCodec<MappingAddress> addressCodec =
+                context.codec(MappingAddress.class);
+
+        if (address.getIpv4() != null) {
+            ObjectNode ipv4Node = addressCodec.encode(address.getIpv4(), context);
+            result.set(IPV4, ipv4Node);
+        }
+
+        if (address.getIpv6() != null) {
+            ObjectNode ipv6Node = addressCodec.encode(address.getIpv6(), context);
+            result.set(IPV6, ipv6Node);
+        }
+
+        if (address.getIpv4() == null && address.getIpv6() == null) {
+            log.error("Either IPv4 or IPv6 address should be specified.");
+        }
+
+        return result;
+    }
+
+    @Override
+    public LispListAddress decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        final JsonCodec<MappingAddress> addressCodec =
+                context.codec(MappingAddress.class);
+        ObjectNode ipv4Json = get(json, IPV4);
+        ObjectNode ipv6Json = get(json, IPV6);
+        MappingAddress ipv4Address = null;
+        MappingAddress ipv6Address = null;
+
+        if (ipv4Json != null) {
+            ipv4Address = addressCodec.decode(ipv4Json, context);
+        }
+
+        if (ipv6Json != null) {
+            ipv6Address = addressCodec.decode(ipv6Json, context);
+        }
+
+        if (ipv4Json == null && ipv6Json == null) {
+            log.error("Either IPv4 or IPv6 address should be specified.");
+        }
+
+        return new LispListAddress.Builder()
+                                .withIpv4(ipv4Address)
+                                .withIpv6(ipv6Address)
+                                .build();
+    }
+}
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispMulticastAddressCodec.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispMulticastAddressCodec.java
new file mode 100644
index 0000000..f5a1efa
--- /dev/null
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispMulticastAddressCodec.java
@@ -0,0 +1,103 @@
+/*
+ * 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.drivers.lisp.extensions.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.drivers.lisp.extensions.LispMulticastAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * LISP multicast address codec.
+ */
+public final class LispMulticastAddressCodec extends JsonCodec<LispMulticastAddress> {
+
+    protected static final String INSTANCE_ID = "instanceId";
+    protected static final String SRC_MASK_LENGTH = "srcMaskLength";
+    protected static final String GRP_MASK_LENGTH = "grpMaskLength";
+    protected static final String SRC_ADDRESS = "srcAddress";
+    protected static final String GRP_ADDRESS = "grpAddress";
+
+    private static final String MISSING_MEMBER_MESSAGE =
+                                " member is required in LispMulticastAddress";
+
+    @Override
+    public ObjectNode encode(LispMulticastAddress address, CodecContext context) {
+        checkNotNull(address, "LispMulticastAddress cannot be null");
+
+        final ObjectNode result = context.mapper().createObjectNode()
+                .put(INSTANCE_ID, address.getInstanceId())
+                .put(SRC_MASK_LENGTH, address.getSrcMaskLength())
+                .put(GRP_MASK_LENGTH, address.getGrpMaskLength());
+
+        final JsonCodec<MappingAddress> addressCodec =
+                context.codec(MappingAddress.class);
+
+        if (address.getSrcAddress() != null) {
+            ObjectNode srcAddressNode = addressCodec.encode(address.getSrcAddress(), context);
+            result.set(SRC_ADDRESS, srcAddressNode);
+        }
+
+        if (address.getGrpAddress() != null) {
+            ObjectNode grpAddressNode = addressCodec.encode(address.getGrpAddress(), context);
+            result.set(GRP_ADDRESS, grpAddressNode);
+        }
+
+        return result;
+    }
+
+    @Override
+    public LispMulticastAddress decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        int instanceId = nullIsIllegal(json.get(INSTANCE_ID),
+                INSTANCE_ID + MISSING_MEMBER_MESSAGE).asInt();
+        byte srcMaskLength = (byte) nullIsIllegal(json.get(SRC_MASK_LENGTH),
+                SRC_MASK_LENGTH + MISSING_MEMBER_MESSAGE).asInt();
+        byte grpMaskLength = (byte) nullIsIllegal(json.get(GRP_MASK_LENGTH),
+                GRP_MASK_LENGTH + MISSING_MEMBER_MESSAGE).asInt();
+
+        final JsonCodec<MappingAddress> addressCodec =
+                context.codec(MappingAddress.class);
+        ObjectNode srcAddressJson = get(json, SRC_ADDRESS);
+        MappingAddress srcAddress = null;
+
+        if (srcAddressJson != null) {
+            srcAddress = addressCodec.decode(srcAddressJson, context);
+        }
+
+        ObjectNode grpAddressJson = get(json, GRP_ADDRESS);
+        MappingAddress grpAddress = null;
+
+        if (grpAddressJson != null) {
+            grpAddress = addressCodec.decode(grpAddressJson, context);
+        }
+
+        return new LispMulticastAddress.Builder()
+                        .withInstanceId(instanceId)
+                        .withSrcMaskLength(srcMaskLength)
+                        .withGrpMaskLength(grpMaskLength)
+                        .withSrcAddress(srcAddress)
+                        .withGrpAddress(grpAddress)
+                        .build();
+    }
+}