[ONOS-6170] Implement codec for various MappingAddress primitives

Change-Id: I0f31408593d73c251d9ef84b4df5f6136d0f4b2f
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java
new file mode 100644
index 0000000..2e6878c
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingCodecRegistrator.java
@@ -0,0 +1,54 @@
+/*
+ * 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.mapping.web;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onosproject.codec.CodecService;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.web.codec.MappingAddressCodec;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of the JSON codec brokering service for mapping primitives.
+ */
+@Component(immediate = true)
+public class MappingCodecRegistrator {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    public CodecService codecService;
+
+    @Activate
+    public void activate() {
+        codecService.registerCodec(MappingAddress.class, new MappingAddressCodec());
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        codecService.unregisterCodec(MappingAddress.class);
+
+        log.info("Stopped");
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/DecodeMappingAddressCodecHelper.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/DecodeMappingAddressCodecHelper.java
new file mode 100644
index 0000000..aa0e644
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/DecodeMappingAddressCodecHelper.java
@@ -0,0 +1,148 @@
+/*
+ * 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.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.Maps;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+
+import java.util.Map;
+
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * Decode portion of the mapping address codec.
+ */
+public final class DecodeMappingAddressCodecHelper {
+
+    private final ObjectNode json;
+
+    protected static final String MISSING_MEMBER_MESSAGE =
+                                  " member is required in Mapping Address";
+
+    private final Map<String, MappingAddressDecoder> decoderMap;
+
+    /**
+     * Creates a decode mapping address codec object.
+     * Initializes the lookup map for mapping address subclass decoders.
+     *
+     * @param json JSON object to decode
+     */
+    public DecodeMappingAddressCodecHelper(ObjectNode json) {
+       this.json = json;
+       decoderMap = Maps.newHashMap();
+
+       decoderMap.put(MappingAddress.Type.IPV4.name(), new Ipv4TypeDecoder());
+       decoderMap.put(MappingAddress.Type.IPV6.name(), new Ipv6TypeDecoder());
+       decoderMap.put(MappingAddress.Type.AS.name(), new AsTypeDecoder());
+       decoderMap.put(MappingAddress.Type.DN.name(), new DnTypeDecoder());
+       decoderMap.put(MappingAddress.Type.ETH.name(), new EthTypeDecoder());
+    }
+
+    /**
+     * An interface of mapping address type decoder.
+     */
+    private interface MappingAddressDecoder {
+        MappingAddress decodeMappingAddress(ObjectNode json);
+    }
+
+    /**
+     * Implementation of IPv4 mapping address decoder.
+     */
+    private class Ipv4TypeDecoder implements MappingAddressDecoder {
+
+        @Override
+        public MappingAddress decodeMappingAddress(ObjectNode json) {
+            String ip = nullIsIllegal(json.get(MappingAddressCodec.IPV4),
+                    MappingAddressCodec.IPV4 + MISSING_MEMBER_MESSAGE).asText();
+            return MappingAddresses.ipv4MappingAddress(IpPrefix.valueOf(ip));
+        }
+    }
+
+    /**
+     * Implementation of IPv6 mapping address decoder.
+     */
+    private class Ipv6TypeDecoder implements MappingAddressDecoder {
+
+        @Override
+        public MappingAddress decodeMappingAddress(ObjectNode json) {
+            String ip = nullIsIllegal(json.get(MappingAddressCodec.IPV6),
+                    MappingAddressCodec.IPV6 + MISSING_MEMBER_MESSAGE).asText();
+            return MappingAddresses.ipv6MappingAddress(IpPrefix.valueOf(ip));
+        }
+    }
+
+    /**
+     * Implementation of AS mapping address decoder.
+     */
+    private class AsTypeDecoder implements MappingAddressDecoder {
+
+        @Override
+        public MappingAddress decodeMappingAddress(ObjectNode json) {
+            String as = nullIsIllegal(json.get(MappingAddressCodec.AS),
+                    MappingAddressCodec.AS + MISSING_MEMBER_MESSAGE).asText();
+            return MappingAddresses.asMappingAddress(as);
+        }
+    }
+
+    /**
+     * Implementation of DN mapping address decoder.
+     */
+    private class DnTypeDecoder implements MappingAddressDecoder {
+
+        @Override
+        public MappingAddress decodeMappingAddress(ObjectNode json) {
+            String dn = nullIsIllegal(json.get(MappingAddressCodec.DN),
+                    MappingAddressCodec.DN + MISSING_MEMBER_MESSAGE).asText();
+            return MappingAddresses.dnMappingAddress(dn);
+        }
+    }
+
+    /**
+     * Implementation of Ethernet mapping address decoder.
+     */
+    private class EthTypeDecoder implements MappingAddressDecoder {
+
+        @Override
+        public MappingAddress decodeMappingAddress(ObjectNode json) {
+            MacAddress mac = MacAddress.valueOf(nullIsIllegal(json.get(MappingAddressCodec.MAC),
+                    MappingAddressCodec.MAC + MISSING_MEMBER_MESSAGE).asText());
+            return MappingAddresses.ethMappingAddress(mac);
+        }
+    }
+
+    /**
+     * Decodes the JSON into a mapping address object.
+     *
+     * @return MappingAddress object
+     * @throws IllegalArgumentException if the JSON is invalid
+     */
+    public MappingAddress decode() {
+        String type =
+                nullIsIllegal(json.get(MappingAddressCodec.TYPE),
+                                    "Type not specified").asText();
+
+        MappingAddressDecoder decoder = decoderMap.get(type);
+        if (decoder != null) {
+            return decoder.decodeMappingAddress(json);
+        }
+
+        throw new IllegalArgumentException("Type " + type + " is unknown");
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/EncodeMappingAddressCodecHelper.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/EncodeMappingAddressCodecHelper.java
new file mode 100644
index 0000000..c3636fd
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/EncodeMappingAddressCodecHelper.java
@@ -0,0 +1,168 @@
+/*
+ * 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.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.mapping.addresses.ASMappingAddress;
+import org.onosproject.mapping.addresses.DNMappingAddress;
+import org.onosproject.mapping.addresses.EthMappingAddress;
+import org.onosproject.mapping.addresses.IPMappingAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+import java.util.EnumMap;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Encode portion of the mapping address codec.
+ */
+public final class EncodeMappingAddressCodecHelper {
+
+    private final MappingAddress address;
+    private final CodecContext context;
+
+    private final EnumMap<MappingAddress.Type, MappingAddressTypeFormatter> formatMap;
+
+    /**
+     * Creates an encoder object for a mapping address.
+     * Initializes the formatter lookup map for the mapping address subclasses.
+     *
+     * @param address MappingAddress to encode
+     * @param context context of the JSON encoding
+     */
+    public EncodeMappingAddressCodecHelper(MappingAddress address, CodecContext context) {
+        this.address = address;
+        this.context = context;
+
+        formatMap = new EnumMap<>(MappingAddress.Type.class);
+
+        formatMap.put(MappingAddress.Type.IPV4, new FormatIpv4());
+        formatMap.put(MappingAddress.Type.IPV6, new FormatIpv6());
+        formatMap.put(MappingAddress.Type.AS, new FormatAs());
+        formatMap.put(MappingAddress.Type.DN, new FormatDn());
+        formatMap.put(MappingAddress.Type.ETH, new FormatEth());
+
+        // TODO: not process extension mapping address for now
+        formatMap.put(MappingAddress.Type.EXTENSION, new FormatUnknown());
+    }
+
+    /**
+     * An interface of mapping address type formatter.
+     */
+    private interface MappingAddressTypeFormatter {
+        ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address);
+    }
+
+    /**
+     * Implementation of IPv4 mapping address type formatter.
+     */
+    private static class FormatIpv4 implements MappingAddressTypeFormatter {
+
+        @Override
+        public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+            final IPMappingAddress ipv4 = (IPMappingAddress) address;
+            return root.put(MappingAddressCodec.IPV4, ipv4.ip().toString());
+        }
+    }
+
+    /**
+     * Implementation of IPv6 mapping address type formatter.
+     */
+    private static class FormatIpv6 implements MappingAddressTypeFormatter {
+
+        @Override
+        public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+            final IPMappingAddress ipv6 = (IPMappingAddress) address;
+            return root.put(MappingAddressCodec.IPV6, ipv6.ip().toString());
+        }
+    }
+
+    /**
+     * Implementation of AS mapping address type formatter.
+     */
+    private static class FormatAs implements MappingAddressTypeFormatter {
+
+        @Override
+        public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+            final ASMappingAddress as = (ASMappingAddress) address;
+            return root.put(MappingAddressCodec.AS, as.asNumber());
+        }
+    }
+
+    /**
+     * Implementation of Distinguished Name mapping address type formatter.
+     */
+    private static class FormatDn implements MappingAddressTypeFormatter {
+
+        @Override
+        public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+            final DNMappingAddress dn = (DNMappingAddress) address;
+            return root.put(MappingAddressCodec.DN, dn.name());
+        }
+    }
+
+    /**
+     * Implementation of Ethernet mapping address type formatter.
+     */
+    private static class FormatEth implements MappingAddressTypeFormatter {
+
+        @Override
+        public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+            final EthMappingAddress eth = (EthMappingAddress) address;
+            return root.put(MappingAddressCodec.MAC, eth.mac().toString());
+        }
+    }
+
+    /**
+     * Implementation of Extension mapping address type formatter.
+     */
+    private static class FormatExtension implements MappingAddressTypeFormatter {
+
+        @Override
+        public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+            return null;
+        }
+    }
+
+    /**
+     * Implementation of Unknown mapping address type formatter.
+     */
+    private static class FormatUnknown implements MappingAddressTypeFormatter {
+
+        @Override
+        public ObjectNode encodeMappingAddress(ObjectNode root, MappingAddress address) {
+            return root;
+        }
+    }
+
+    /**
+     * Encodes a mapping address into a JSON node.
+     *
+     * @return encoded JSON object for the given mapping address
+     */
+    public ObjectNode encode() {
+        final ObjectNode result = context.mapper().createObjectNode()
+                    .put(MappingAddressCodec.TYPE, address.type().toString());
+
+        MappingAddressTypeFormatter formatter =
+                checkNotNull(formatMap.get(address.type()),
+                        "No formatter found for mapping address type "
+                        + address.type().toString());
+
+        return formatter.encodeMappingAddress(result, address);
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingAddressCodec.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingAddressCodec.java
new file mode 100644
index 0000000..0458cf5
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/MappingAddressCodec.java
@@ -0,0 +1,53 @@
+/*
+ * 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.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Mapping address codec.
+ */
+public final class MappingAddressCodec extends JsonCodec<MappingAddress> {
+
+    protected static final Logger log =
+                            LoggerFactory.getLogger(MappingAddressCodec.class);
+
+    protected static final String TYPE = "type";
+    protected static final String IPV4 = "ipv4";
+    protected static final String IPV6 = "ipv6";
+    protected static final String MAC = "mac";
+    protected static final String DN = "dn";
+    protected static final String AS = "as";
+
+    @Override
+    public ObjectNode encode(MappingAddress address, CodecContext context) {
+        EncodeMappingAddressCodecHelper encoder =
+                            new EncodeMappingAddressCodecHelper(address, context);
+        return encoder.encode();
+    }
+
+    @Override
+    public MappingAddress decode(ObjectNode json, CodecContext context) {
+        DecodeMappingAddressCodecHelper decoder =
+                            new DecodeMappingAddressCodecHelper(json);
+        return decoder.decode();
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/package-info.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/package-info.java
new file mode 100644
index 0000000..9ab4bf5
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/codec/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+/**
+ * Implementations of the codec broker and
+ * built-in entity JSON codecs for mapping address.
+ */
+package org.onosproject.mapping.web.codec;
\ No newline at end of file
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java
new file mode 100644
index 0000000..0d004b0
--- /dev/null
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressCodecTest.java
@@ -0,0 +1,152 @@
+/*
+ * 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.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.mapping.addresses.MappingAddress;
+import org.onosproject.mapping.addresses.MappingAddresses;
+import org.onosproject.mapping.web.MappingCodecRegistrator;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.mapping.web.codec.MappingAddressJsonMatcher.matchesMappingAddress;
+
+/**
+ * Unit tests for MappingAddressCodec.
+ */
+public class MappingAddressCodecTest {
+
+    private CodecContext context;
+    private JsonCodec<MappingAddress> addressCodec;
+    MappingCodecRegistrator registrator;
+    private static final IpPrefix IPV4_PREFIX = IpPrefix.valueOf("10.1.1.0/24");
+    private static final IpPrefix IPV6_PREFIX = IpPrefix.valueOf("fe80::/64");
+    private static final MacAddress MAC = MacAddress.valueOf("00:00:11:00:00:01");
+    private static final String DN = "onos";
+    private static final String AS = "AS1000";
+
+    /**
+     * Sets up for each test.
+     * Creates a context and fetches the mapping address codec.
+     */
+    @Before
+    public void setUp() {
+        CodecManager manager = new CodecManager();
+        registrator = new MappingCodecRegistrator();
+        registrator.codecService = manager;
+        registrator.activate();
+
+        context = new MappingTestContext(registrator.codecService);
+
+        addressCodec = context.codec(MappingAddress.class);
+        assertThat(addressCodec, notNullValue());
+    }
+
+    @After
+    public void tearDown() {
+        registrator.deactivate();
+    }
+
+    /**
+     * Tests AS mapping address.
+     */
+    @Test
+    public void asMappingAddressTest() {
+        MappingAddress address = MappingAddresses.asMappingAddress(AS);
+        ObjectNode result = addressCodec.encode(address, context);
+        assertThat(result, matchesMappingAddress(address));
+    }
+
+    /**
+     * Tests DN mapping address.
+     */
+    @Test
+    public void dnMappingAddressTest() {
+        MappingAddress address = MappingAddresses.dnMappingAddress(DN);
+        ObjectNode result = addressCodec.encode(address, context);
+        assertThat(result, matchesMappingAddress(address));
+    }
+
+    /**
+     * Tests IPv4 mapping address.
+     */
+    @Test
+    public void ipv4MappingAddressTest() {
+        MappingAddress address = MappingAddresses.ipv4MappingAddress(IPV4_PREFIX);
+        ObjectNode result = addressCodec.encode(address, context);
+        assertThat(result, matchesMappingAddress(address));
+    }
+
+    /**
+     * Tests IPv6 mapping address.
+     */
+    @Test
+    public void ipv6MappingAddressTest() {
+        MappingAddress address = MappingAddresses.ipv6MappingAddress(IPV6_PREFIX);
+        ObjectNode result = addressCodec.encode(address, context);
+        assertThat(result, matchesMappingAddress(address));
+    }
+
+    /**
+     * Tests Ethernet mapping address.
+     */
+    @Test
+    public void ethMappingAddressTest() {
+        MappingAddress address = MappingAddresses.ethMappingAddress(MAC);
+        ObjectNode result = addressCodec.encode(address, context);
+        assertThat(result, matchesMappingAddress(address));
+    }
+
+    /**
+     * Test mapping codec context.
+     */
+    private class MappingTestContext implements CodecContext {
+        private final ObjectMapper mapper = new ObjectMapper();
+        private final CodecService manager;
+
+        /**
+         * Constructs a new mock codec context.
+         */
+        public MappingTestContext(CodecService manager) {
+            this.manager = manager;
+        }
+
+        @Override
+        public ObjectMapper mapper() {
+            return mapper;
+        }
+
+        @Override
+        public <T> JsonCodec<T> codec(Class<T> entityClass) {
+            return manager.getCodec(entityClass);
+        }
+
+        @Override
+        public <T> T getService(Class<T> serviceClass) {
+            return null;
+        }
+    }
+}
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressJsonMatcher.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressJsonMatcher.java
new file mode 100644
index 0000000..ba496e9
--- /dev/null
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/codec/MappingAddressJsonMatcher.java
@@ -0,0 +1,164 @@
+/*
+ * 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.mapping.web.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.mapping.addresses.ASMappingAddress;
+import org.onosproject.mapping.addresses.DNMappingAddress;
+import org.onosproject.mapping.addresses.EthMappingAddress;
+import org.onosproject.mapping.addresses.IPMappingAddress;
+import org.onosproject.mapping.addresses.MappingAddress;
+
+/**
+ * Hamcrest matcher for mapping objects.
+ */
+public final class MappingAddressJsonMatcher extends
+                                            TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final MappingAddress address;
+    private Description description;
+    private JsonNode node;
+
+    /**
+     * Constructs a matcher object.
+     *
+     * @param address mapping address to match
+     */
+    private MappingAddressJsonMatcher(MappingAddress address) {
+        this.address = address;
+    }
+
+    /**
+     * Factory to allocate a mapping address matcher.
+     *
+     * @param address mapping address object we are looking for
+     * @return matcher
+     */
+    public static MappingAddressJsonMatcher matchesMappingAddress(MappingAddress address) {
+        return new MappingAddressJsonMatcher(address);
+    }
+
+    /**
+     * Matches an AS mapping address object,
+     *
+     * @param address mapping address to match
+     * @return true if the JSON matches the mapping address, false otherwise
+     */
+    private boolean matchMappingAddress(ASMappingAddress address) {
+        final String as = address.asNumber();
+        final String jsonAs = node.get(MappingAddressCodec.AS).textValue();
+        if (!as.equals(jsonAs)) {
+            description.appendText("AS was " + jsonAs);
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Matches a Distinguished Name mapping address object.
+     *
+     * @param address mapping address to match
+     * @return true if the JSON matches the mapping address, false otherwise
+     */
+    private boolean matchMappingAddress(DNMappingAddress address) {
+        final String dn = address.name();
+        final String jsonDn = node.get(MappingAddressCodec.DN).textValue();
+        if (!dn.equals(jsonDn)) {
+            description.appendText("Distinguished Name was " + jsonDn);
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Matches an IP mapping address object.
+     *
+     * @param address mapping address to match
+     * @return true if the JSON matches the mapping address, false otherwise
+     */
+    private boolean matchMappingAddress(IPMappingAddress address) {
+        final String ip = address.ip().toString();
+        String jsonIp = null;
+        if (address.type() == MappingAddress.Type.IPV4) {
+            jsonIp = node.get(MappingAddressCodec.IPV4).textValue();
+
+        } else if (address.type() == MappingAddress.Type.IPV6) {
+            jsonIp = node.get(MappingAddressCodec.IPV6).textValue();
+        }
+        if (!ip.equals(jsonIp)) {
+            description.appendText("IP was " + jsonIp);
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * Matches a MAC mapping address object.
+     *
+     * @param address mapping address to match
+     * @return true if the JSON matches the mapping address, false otherwise
+     */
+    private boolean matchMappingAddress(EthMappingAddress address) {
+        final String mac = address.mac().toString();
+        final String jsonMac = node.get(MappingAddressCodec.MAC).textValue();
+        if (!mac.equals(jsonMac)) {
+            description.appendText("MAC was " + jsonMac);
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+
+        this.description = description;
+        this.node = jsonNode;
+        final String type = address.type().name();
+        final String jsonType = jsonNode.get(MappingAddressCodec.TYPE).asText();
+        if (!type.equals(jsonType)) {
+            description.appendText("type was " + type);
+            return false;
+        }
+
+        switch (address.type()) {
+
+            case IPV4:
+            case IPV6:
+                return matchMappingAddress((IPMappingAddress) address);
+
+            case AS:
+                return matchMappingAddress((ASMappingAddress) address);
+
+            case DN:
+                return matchMappingAddress((DNMappingAddress) address);
+
+            case ETH:
+                return matchMappingAddress((EthMappingAddress) address);
+
+            default:
+                // Don't know how to format this type
+                description.appendText("unknown criterion type " + address.type());
+                return false;
+        }
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(address.toString());
+    }
+}