[ONOS-6169] Implement codec for nat and nonce ext addresses

Change-Id: I81c8fecfcae9057e511ac49654cde0acdb3f6640
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 99f3988..05282fa 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
@@ -26,6 +26,8 @@
 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.drivers.lisp.extensions.codec.LispNatAddressCodec;
+import org.onosproject.drivers.lisp.extensions.codec.LispNonceAddressCodec;
 import org.onosproject.mapping.web.MappingCodecRegistrator;
 import org.slf4j.Logger;
 
@@ -56,6 +58,8 @@
         codecService.registerCodec(LispGcAddress.class, new LispGcAddressCodec());
         codecService.registerCodec(LispListAddress.class, new LispListAddressCodec());
         codecService.registerCodec(LispMulticastAddress.class, new LispMulticastAddressCodec());
+        codecService.registerCodec(LispNatAddress.class, new LispNatAddressCodec());
+        codecService.registerCodec(LispNonceAddress.class, new LispNonceAddressCodec());
 
         log.info("Started");
     }
@@ -67,6 +71,8 @@
         codecService.unregisterCodec(LispGcAddress.class);
         codecService.unregisterCodec(LispListAddress.class);
         codecService.unregisterCodec(LispMulticastAddress.class);
+        codecService.unregisterCodec(LispNatAddress.class);
+        codecService.unregisterCodec(LispNonceAddress.class);
 
         registrator.deactivate();
         registrator = null;
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispNatAddressCodec.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispNatAddressCodec.java
new file mode 100644
index 0000000..4de2d58
--- /dev/null
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispNatAddressCodec.java
@@ -0,0 +1,135 @@
+/*
+ * 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.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Lists;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.drivers.lisp.extensions.LispNatAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+import java.util.List;
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * LISP NAT address codec.
+ */
+public final class LispNatAddressCodec extends JsonCodec<LispNatAddress> {
+
+    protected static final String MS_UDP_PORT_NUMBER = "msUdpPortNumber";
+    protected static final String ETR_UDP_PORT_NUMBER = "etrUdpPortNumber";
+    protected static final String GLOBAL_ETR_RLOC_ADDRESS = "globalEtrRlocAddress";
+    protected static final String MS_RLOC_ADDRESS = "msRlocAddress";
+    protected static final String PRIVATE_ETR_RLOC_ADDRESS = "privateEtrRlocAddress";
+    protected static final String RTR_RLOC_ADDRESSES = "rtrRlocAddresses";
+
+    private static final String MISSING_MEMBER_MESSAGE =
+                                " member is required in LispListAddress";
+
+    @Override
+    public ObjectNode encode(LispNatAddress address, CodecContext context) {
+        checkNotNull(address, "LispListAddress cannot be null");
+
+        final ObjectNode result = context.mapper().createObjectNode()
+                .put(MS_UDP_PORT_NUMBER, address.getMsUdpPortNumber())
+                .put(ETR_UDP_PORT_NUMBER, address.getEtrUdpPortNumber());
+
+        final JsonCodec<MappingAddress> addressCodec =
+                context.codec(MappingAddress.class);
+
+        if (address.getGlobalEtrRlocAddress() != null) {
+            ObjectNode globalEtrRlocNode =
+                    addressCodec.encode(address.getGlobalEtrRlocAddress(), context);
+            result.set(GLOBAL_ETR_RLOC_ADDRESS, globalEtrRlocNode);
+        }
+
+        if (address.getMsRlocAddress() != null) {
+            ObjectNode msRlocNode =
+                    addressCodec.encode(address.getMsRlocAddress(), context);
+            result.set(MS_RLOC_ADDRESS, msRlocNode);
+        }
+
+        if (address.getPrivateEtrRlocAddress() != null) {
+            ObjectNode privateEtrRlocNode =
+                    addressCodec.encode(address.getPrivateEtrRlocAddress(), context);
+            result.set(PRIVATE_ETR_RLOC_ADDRESS, privateEtrRlocNode);
+        }
+
+        final ArrayNode jsonRtrRlocNodes = result.putArray(RTR_RLOC_ADDRESSES);
+
+        if (address.getRtrRlocAddresses() != null) {
+            for (final MappingAddress mappingAddress : address.getRtrRlocAddresses()) {
+                jsonRtrRlocNodes.add(addressCodec.encode(mappingAddress, context));
+            }
+        }
+
+        return result;
+    }
+
+    @Override
+    public LispNatAddress decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        final JsonCodec<MappingAddress> addressCodec =
+                context.codec(MappingAddress.class);
+
+        short msUdpPortNumber = (short) json.get(MS_UDP_PORT_NUMBER).asInt();
+        short etrUdpPortNumber = (short) json.get(ETR_UDP_PORT_NUMBER).asInt();
+
+        ObjectNode globalEtrRlocJson = get(json, GLOBAL_ETR_RLOC_ADDRESS);
+        ObjectNode msRlocJson = get(json, MS_RLOC_ADDRESS);
+        ObjectNode privateEtrRlocJson = get(json, PRIVATE_ETR_RLOC_ADDRESS);
+        JsonNode rtrRlocJson = json.get(RTR_RLOC_ADDRESSES);
+        MappingAddress globalEtrRlocAddress = null;
+        MappingAddress msRlocAddress = null;
+        MappingAddress privateEtrRlocAddress = null;
+        List<MappingAddress> rtrRlocAddresses = Lists.newArrayList();
+
+        if (globalEtrRlocJson != null) {
+            globalEtrRlocAddress = addressCodec.decode(globalEtrRlocJson, context);
+        }
+
+        if (msRlocJson != null) {
+            msRlocAddress = addressCodec.decode(msRlocJson, context);
+        }
+
+        if (privateEtrRlocJson != null) {
+            privateEtrRlocAddress = addressCodec.decode(privateEtrRlocJson, context);
+        }
+
+        if (rtrRlocJson != null) {
+            IntStream.range(0, rtrRlocJson.size())
+                    .forEach(i -> rtrRlocAddresses.add(
+                            addressCodec.decode(get(rtrRlocJson, i), context)));
+        }
+
+        return new LispNatAddress.Builder()
+                            .withMsUdpPortNumber(msUdpPortNumber)
+                            .withEtrUdpPortNumber(etrUdpPortNumber)
+                            .withGlobalEtrRlocAddress(globalEtrRlocAddress)
+                            .withMsRlocAddress(msRlocAddress)
+                            .withPrivateEtrRlocAddress(privateEtrRlocAddress)
+                            .withRtrRlocAddresses(rtrRlocAddresses)
+                            .build();
+    }
+}
diff --git a/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispNonceAddressCodec.java b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispNonceAddressCodec.java
new file mode 100644
index 0000000..ed63f9b
--- /dev/null
+++ b/drivers/lisp/src/main/java/org/onosproject/drivers/lisp/extensions/codec/LispNonceAddressCodec.java
@@ -0,0 +1,78 @@
+/*
+ * 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.LispNonceAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * LISP nonce address codec.
+ */
+public final class LispNonceAddressCodec extends JsonCodec<LispNonceAddress> {
+
+    protected static final String NONCE = "nonce";
+    protected static final String ADDRESS = "address";
+
+    private static final String MISSING_MEMBER_MESSAGE =
+                                " member is required in LispGcAddress";
+
+    @Override
+    public ObjectNode encode(LispNonceAddress address, CodecContext context) {
+        checkNotNull(address, "LispListAddress cannot be null");
+
+        final ObjectNode result = context.mapper().createObjectNode()
+                .put(NONCE, address.getNonce());
+
+        if (address.getAddress() != null) {
+            final JsonCodec<MappingAddress> addressCodec =
+                    context.codec(MappingAddress.class);
+            ObjectNode addressNode = addressCodec.encode(address.getAddress(), context);
+            result.set(ADDRESS, addressNode);
+        }
+
+        return result;
+    }
+
+    @Override
+    public LispNonceAddress decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        int nonce = nullIsIllegal(json.get(NONCE), NONCE +
+                MISSING_MEMBER_MESSAGE).asInt();
+
+        ObjectNode addressJson = get(json, ADDRESS);
+        MappingAddress mappingAddress = null;
+
+        if (addressJson != null) {
+            final JsonCodec<MappingAddress> addressCodec =
+                    context.codec(MappingAddress.class);
+            mappingAddress = addressCodec.decode(addressJson, context);
+        }
+
+        return new LispNonceAddress.Builder()
+                        .withNonce(nonce)
+                        .withAddress(mappingAddress)
+                        .build();
+    }
+}
diff --git a/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispNatAddressCodecTest.java b/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispNatAddressCodecTest.java
new file mode 100644
index 0000000..551ee44
--- /dev/null
+++ b/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispNatAddressCodecTest.java
@@ -0,0 +1,247 @@
+/*
+ * 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.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.drivers.lisp.extensions.LispMappingExtensionCodecRegistrator;
+import org.onosproject.drivers.lisp.extensions.LispNatAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+import org.onosproject.mapping.web.codec.MappingAddressJsonMatcher;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.List;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * Unit tests for LispNatAddressCodec.
+ */
+public class LispNatAddressCodecTest {
+
+    private static final short MS_UDP_PORT_NUMBER = (short) 1;
+    private static final short ETR_UDP_PORT_NUMBER = (short) 2;
+    private static final IpPrefix GLOBAL_ETR_RLOC_ADDRESS = IpPrefix.valueOf("10.1.1.1/24");
+    private static final IpPrefix MS_RLOC_ADDRESS = IpPrefix.valueOf("10.1.1.2/24");
+    private static final IpPrefix PRIVATE_ETR_RLOC_ADDRESS = IpPrefix.valueOf("10.1.1.3/24");
+
+    private CodecContext context;
+    private JsonCodec<LispNatAddress> natAddressCodec;
+    private LispMappingExtensionCodecRegistrator registrator;
+
+    /**
+     * Sets up for each test.
+     * Creates a context and fetches the LispNatAddress codec.
+     */
+    @Before
+    public void setUp() {
+        CodecManager manager = new CodecManager();
+        registrator = new LispMappingExtensionCodecRegistrator();
+        registrator.codecService = manager;
+        registrator.activate();
+
+        context = new LispMappingExtensionCodecContextAdapter(registrator.codecService);
+        natAddressCodec = context.codec(LispNatAddress.class);
+        assertThat("NAT address codec should not be null",
+                natAddressCodec, notNullValue());
+    }
+
+    /**
+     * Deactivates the codec registrator.
+     */
+    @After
+    public void tearDown() {
+        registrator.deactivate();
+    }
+
+    /**
+     * Tests encoding of a LispNatAddress object.
+     */
+    @Test
+    public void testLispNatAddressEncode() {
+
+        List<MappingAddress> rtrRlocs =
+                ImmutableList.of(MappingAddresses.ipv4MappingAddress(GLOBAL_ETR_RLOC_ADDRESS),
+                        MappingAddresses.ipv4MappingAddress(MS_RLOC_ADDRESS),
+                        MappingAddresses.ipv4MappingAddress(PRIVATE_ETR_RLOC_ADDRESS));
+
+
+        LispNatAddress address = new LispNatAddress.Builder()
+                .withMsUdpPortNumber(MS_UDP_PORT_NUMBER)
+                .withEtrUdpPortNumber(ETR_UDP_PORT_NUMBER)
+                .withGlobalEtrRlocAddress(MappingAddresses.ipv4MappingAddress(GLOBAL_ETR_RLOC_ADDRESS))
+                .withMsRlocAddress(MappingAddresses.ipv4MappingAddress(MS_RLOC_ADDRESS))
+                .withPrivateEtrRlocAddress(MappingAddresses.ipv4MappingAddress(PRIVATE_ETR_RLOC_ADDRESS))
+                .withRtrRlocAddresses(rtrRlocs)
+                .build();
+
+        ObjectNode addressJson = natAddressCodec.encode(address, context);
+        assertThat("errors in encoding NAT address JSON",
+                addressJson, LispNatAddressJsonMatcher.matchesNatAddress(address));
+    }
+
+    /**
+     * Tests decoding of a LispNatAddress JSON object.
+     */
+    @Test
+    public void testLispNatAddressDecode() throws IOException {
+        LispNatAddress address = getLispNatAddress("LispNatAddress.json");
+
+        assertThat("incorrect MS UDP port number",
+                address.getMsUdpPortNumber(), is(MS_UDP_PORT_NUMBER));
+        assertThat("incorrect ETR UDP port number",
+                address.getEtrUdpPortNumber(), is(ETR_UDP_PORT_NUMBER));
+        assertThat("incorrect global ETR RLOC address", address.getGlobalEtrRlocAddress(),
+                is(MappingAddresses.ipv4MappingAddress(GLOBAL_ETR_RLOC_ADDRESS)));
+        assertThat("incorrect MS RLOC address", address.getMsRlocAddress(),
+                is(MappingAddresses.ipv4MappingAddress(MS_RLOC_ADDRESS)));
+        assertThat("incorrect private ETR RLOC address", address.getPrivateEtrRlocAddress(),
+                is(MappingAddresses.ipv4MappingAddress(PRIVATE_ETR_RLOC_ADDRESS)));
+    }
+
+    /**
+     * Hamcrest matcher for LispNatAddress.
+     */
+    public static final class LispNatAddressJsonMatcher
+            extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+        private final LispNatAddress address;
+
+        /**
+         * Default constructor.
+         *
+         * @param address LispNatAddress object
+         */
+        private LispNatAddressJsonMatcher(LispNatAddress address) {
+            this.address = address;
+        }
+
+        private int filteredSize(JsonNode node) {
+            return node.size();
+        }
+
+        @Override
+        protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+            // check MS UDP port number
+            short jsonMsUdpPortNumber = (short)
+                    jsonNode.get(LispNatAddressCodec.MS_UDP_PORT_NUMBER).asInt();
+            short msUdpPortNumber = address.getMsUdpPortNumber();
+            if (jsonMsUdpPortNumber != msUdpPortNumber) {
+                description.appendText("MS UDP port number was " + jsonMsUdpPortNumber);
+                return false;
+            }
+
+            // check ETR UDP port number
+            short jsonEtrUdpPortNumber = (short)
+                    jsonNode.get(LispNatAddressCodec.ETR_UDP_PORT_NUMBER).asInt();
+            short etrUdpPortNumber = address.getEtrUdpPortNumber();
+            if (jsonEtrUdpPortNumber != etrUdpPortNumber) {
+                description.appendText("ETR UDP port number was " + jsonEtrUdpPortNumber);
+                return false;
+            }
+
+            // check RTR RLOC addresses
+            final JsonNode jsonRtrRlocs = jsonNode.get(LispNatAddressCodec.RTR_RLOC_ADDRESSES);
+
+            if (address.getRtrRlocAddresses().size() != filteredSize(jsonRtrRlocs)) {
+                description.appendText("addresses array size of " +
+                        Integer.toString(address.getRtrRlocAddresses().size()));
+                return false;
+            }
+
+            for (final MappingAddress address : address.getRtrRlocAddresses()) {
+                boolean addressFound = false;
+                for (int addressIndex = 0; addressIndex < jsonRtrRlocs.size(); addressIndex++) {
+                    final String jsonType =
+                            jsonRtrRlocs.get(addressIndex).get("type").asText();
+                    final String addressType = address.type().name();
+                    if (jsonType.equals(addressType)) {
+                        addressFound = true;
+                    }
+                }
+                if (!addressFound) {
+                    description.appendText("address " + address.toString());
+                    return false;
+                }
+            }
+
+            // check global ETR RLOC address
+            MappingAddressJsonMatcher globalEtrRlocMatcher =
+                    MappingAddressJsonMatcher.matchesMappingAddress(
+                            address.getGlobalEtrRlocAddress());
+
+            // check MS RLOC address
+            MappingAddressJsonMatcher msRlocMatcher =
+                    MappingAddressJsonMatcher.matchesMappingAddress(
+                            address.getMsRlocAddress());
+
+            // check private ETR RLOC address
+            MappingAddressJsonMatcher privateEtrRlocMatcher =
+                    MappingAddressJsonMatcher.matchesMappingAddress(
+                            address.getPrivateEtrRlocAddress());
+
+            return globalEtrRlocMatcher.matches(jsonNode.get(LispNatAddressCodec.GLOBAL_ETR_RLOC_ADDRESS)) ||
+                    msRlocMatcher.matches(jsonNode.get(LispNatAddressCodec.MS_RLOC_ADDRESS)) ||
+                    privateEtrRlocMatcher.matches(jsonNode.get(LispNatAddressCodec.PRIVATE_ETR_RLOC_ADDRESS));
+        }
+
+        @Override
+        public void describeTo(Description description) {
+            description.appendText(address.toString());
+        }
+
+        /**
+         * Factory to allocate a LispNatAddress matcher.
+         *
+         * @param address LispNatAddress object we are looking for
+         * @return matcher
+         */
+        public static LispNatAddressJsonMatcher matchesNatAddress(LispNatAddress address) {
+            return new LispNatAddressJsonMatcher(address);
+        }
+    }
+
+    /**
+     * Reads in a LispNatAddress from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the JSON for the rule
+     * @return decoded LispGcAddress
+     * @throws IOException if processing the resource fails
+     */
+    private LispNatAddress getLispNatAddress(String resourceName) throws IOException {
+        InputStream jsonStream = LispNatAddressCodecTest.class.getResourceAsStream(resourceName);
+        JsonNode json = context.mapper().readTree(jsonStream);
+        assertThat("JSON string should not be null", json, notNullValue());
+        LispNatAddress natAddress = natAddressCodec.decode((ObjectNode) json, context);
+        assertThat("decoded address should not be null", natAddress, notNullValue());
+        return natAddress;
+    }
+}
diff --git a/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispNonceAddressCodecTest.java b/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispNonceAddressCodecTest.java
new file mode 100644
index 0000000..881a906
--- /dev/null
+++ b/drivers/lisp/src/test/java/org/onosproject/drivers/lisp/extensions/codec/LispNonceAddressCodecTest.java
@@ -0,0 +1,170 @@
+/*
+ * 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.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.drivers.lisp.extensions.LispMappingExtensionCodecRegistrator;
+import org.onosproject.drivers.lisp.extensions.LispNonceAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+import org.onosproject.mapping.web.codec.MappingAddressJsonMatcher;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+
+/**
+ * Unit tests for LispNonceAddressCodec.
+ */
+public class LispNonceAddressCodecTest {
+
+    private static final int NONCE = 1;
+    private static final IpPrefix ADDRESS = IpPrefix.valueOf("10.1.1.0/24");
+
+    private CodecContext context;
+    private JsonCodec<LispNonceAddress> nonceAddressCodec;
+    private LispMappingExtensionCodecRegistrator registrator;
+
+    /**
+     * Sets up for each test.
+     * Creates a context and fetches the LispNonceAddress codec.
+     */
+    @Before
+    public void setUp() {
+        CodecManager manager = new CodecManager();
+        registrator = new LispMappingExtensionCodecRegistrator();
+        registrator.codecService = manager;
+        registrator.activate();
+
+        context = new LispMappingExtensionCodecContextAdapter(registrator.codecService);
+        nonceAddressCodec = context.codec(LispNonceAddress.class);
+        assertThat("nonce address codec should not be null",
+                nonceAddressCodec, notNullValue());
+    }
+
+    /**
+     * Deactivates the codec registrator.
+     */
+    @After
+    public void tearDown() {
+        registrator.deactivate();
+    }
+
+    /**
+     * Tests encoding of a LispNonceAddress object.
+     */
+    @Test
+    public void testLispNonceAddressEncode() {
+        LispNonceAddress address = new LispNonceAddress.Builder()
+                .withNonce(NONCE)
+                .withAddress(MappingAddresses.ipv4MappingAddress(ADDRESS))
+                .build();
+        ObjectNode addressJson = nonceAddressCodec.encode(address, context);
+        assertThat("errors in encoding nonce address JSON",
+                addressJson, LispNonceAddressJsonMatcher.matchesNonceAddress(address));
+    }
+
+    /**
+     * Tests decoding of a LispNonceAddress JSON object.
+     */
+    @Test
+    public void testLispNonceAddressDecode() throws IOException {
+        LispNonceAddress address = getLispNonceAddress("LispNonceAddress.json");
+
+        assertThat("incorrect nonce", address.getNonce(), is(NONCE));
+        assertThat("incorrect address", address.getAddress(),
+                is(MappingAddresses.ipv4MappingAddress(ADDRESS)));
+    }
+
+    /**
+     * Hamcrest matcher for LispNonceAddress.
+     */
+    public static final class LispNonceAddressJsonMatcher
+            extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+        private final LispNonceAddress address;
+
+        /**
+         * Default constructor.
+         *
+         * @param address LispNonceAddress object
+         */
+        private LispNonceAddressJsonMatcher(LispNonceAddress address) {
+            this.address = address;
+        }
+
+        @Override
+        protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+            // check nonce
+            int jsonNonce = jsonNode.get(LispNonceAddressCodec.NONCE).asInt();
+            int nonce = address.getNonce();
+            if (jsonNonce != nonce) {
+                description.appendText("Nonce was " + jsonNonce);
+                return false;
+            }
+
+            // check address
+            MappingAddressJsonMatcher addressMatcher =
+                    MappingAddressJsonMatcher.matchesMappingAddress(address.getAddress());
+
+            return addressMatcher.matches(jsonNode.get(LispNonceAddressCodec.ADDRESS));
+        }
+
+        @Override
+        public void describeTo(Description description) {
+            description.appendText(address.toString());
+        }
+
+        /**
+         * Factory to allocate a LispNonceAddress matcher.
+         *
+         * @param address LispNonceAddress object we are looking for
+         * @return matcher
+         */
+        public static LispNonceAddressJsonMatcher matchesNonceAddress(LispNonceAddress address) {
+            return new LispNonceAddressJsonMatcher(address);
+        }
+    }
+
+    /**
+     * Reads in a LispNonceAddress from the given resource and decodes it.
+     *
+     * @param resourceName resource to use to read the JSON for the rule
+     * @return decoded LispNonceAddress
+     * @throws IOException if processing the resource fails
+     */
+    private LispNonceAddress getLispNonceAddress(String resourceName) throws IOException {
+        InputStream jsonStream = LispNonceAddressCodecTest.class.getResourceAsStream(resourceName);
+        JsonNode json = context.mapper().readTree(jsonStream);
+        assertThat("JSON string should not be null", json, notNullValue());
+        LispNonceAddress nonceAddress = nonceAddressCodec.decode((ObjectNode) json, context);
+        assertThat("decoded address should not be null", nonceAddress, notNullValue());
+        return nonceAddress;
+    }
+}
\ No newline at end of file
diff --git a/drivers/lisp/src/test/resources/org/onosproject/drivers/lisp/extensions/codec/LispNatAddress.json b/drivers/lisp/src/test/resources/org/onosproject/drivers/lisp/extensions/codec/LispNatAddress.json
new file mode 100644
index 0000000..5942b75
--- /dev/null
+++ b/drivers/lisp/src/test/resources/org/onosproject/drivers/lisp/extensions/codec/LispNatAddress.json
@@ -0,0 +1,30 @@
+{
+  "msUdpPortNumber": 1,
+  "etrUdpPortNumber": 2,
+  "globalEtrRlocAddress": {
+    "type": "IPV4",
+    "ipv4": "10.1.1.1/24"
+  },
+  "msRlocAddress": {
+    "type": "IPV4",
+    "ipv4": "10.1.1.2/24"
+  },
+  "privateEtrRlocAddress": {
+    "type": "IPV4",
+    "ipv4": "10.1.1.3/24"
+  },
+  "rtrRlocAddresses": [
+    {
+      "type": "IPV4",
+      "ipv4": "10.1.1.1/24"
+    },
+    {
+      "type": "IPV4",
+      "ipv4": "10.1.1.2/24"
+    },
+    {
+      "type": "IPV4",
+      "ipv4": "10.1.1.3/24"
+    }
+  ]
+}
\ No newline at end of file
diff --git a/drivers/lisp/src/test/resources/org/onosproject/drivers/lisp/extensions/codec/LispNonceAddress.json b/drivers/lisp/src/test/resources/org/onosproject/drivers/lisp/extensions/codec/LispNonceAddress.json
new file mode 100644
index 0000000..32db0b8
--- /dev/null
+++ b/drivers/lisp/src/test/resources/org/onosproject/drivers/lisp/extensions/codec/LispNonceAddress.json
@@ -0,0 +1,7 @@
+{
+  "nonce": 1,
+  "address": {
+    "type": "IPV4",
+    "ipv4": "10.1.1.0/24"
+  }
+}
\ No newline at end of file