Add kubevirt node related codec with unit tests

Change-Id: I0c68d4c3d0c7626a4b6b66e5c783631cb9be50a8
diff --git a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/Constants.java b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/Constants.java
index c04c183..09451dd 100644
--- a/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/Constants.java
+++ b/apps/kubevirt-node/api/src/main/java/org/onosproject/kubevirtnode/api/Constants.java
@@ -23,6 +23,11 @@
     private Constants() {
     }
 
+    public static final String HOST_NAME = "hostname";
+    public static final String TYPE = "type";
+    public static final String MANAGEMENT_IP = "managementIp";
+    public static final String DATA_IP = "dataIp";
+
     public static final String VXLAN = "vxlan";
     public static final String GRE = "gre";
     public static final String GENEVE = "geneve";
diff --git a/apps/kubevirt-node/app/BUILD b/apps/kubevirt-node/app/BUILD
index 196953d..601bc8a 100644
--- a/apps/kubevirt-node/app/BUILD
+++ b/apps/kubevirt-node/app/BUILD
@@ -17,6 +17,7 @@
     "//core/api:onos-api-tests",
     "//core/common:onos-core-common-tests",
     "//web/api:onos-rest-tests",
+    "@minimal_json//jar",
 ]
 
 osgi_jar_with_tests(
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodec.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodec.java
new file mode 100644
index 0000000..17ed505
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodec.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.kubevirtnode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.kubevirtnode.api.DefaultKubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtNodeState;
+import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
+import org.onosproject.net.DeviceId;
+import org.slf4j.Logger;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.onosproject.kubevirtnode.api.Constants.DATA_IP;
+import static org.onosproject.kubevirtnode.api.Constants.HOST_NAME;
+import static org.onosproject.kubevirtnode.api.Constants.MANAGEMENT_IP;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Kubevirt node codec used for serializing and de-serializing JSON string.
+ */
+public final class KubevirtNodeCodec extends JsonCodec<KubevirtNode> {
+
+    private final Logger log = getLogger(getClass());
+
+    private static final String TYPE = "type";
+    private static final String INTEGRATION_BRIDGE = "integrationBridge";
+    private static final String STATE = "state";
+    private static final String PHYSICAL_INTERFACES = "phyIntfs";
+
+    private static final String MISSING_MESSAGE = " is required in OpenstackNode";
+
+    @Override
+    public ObjectNode encode(KubevirtNode node, CodecContext context) {
+        checkNotNull(node, "Kubevirt node cannot be null");
+
+        ObjectNode result = context.mapper().createObjectNode()
+                .put(HOST_NAME, node.hostname())
+                .put(TYPE, node.type().name())
+                .put(STATE, node.state().name())
+                .put(MANAGEMENT_IP, node.managementIp().toString());
+
+        // serialize integration bridge config
+        if (node.intgBridge() != null) {
+            result.put(INTEGRATION_BRIDGE, node.intgBridge().toString());
+        }
+
+        // serialize data IP only if it presents
+        if (node.dataIp() != null) {
+            result.put(DATA_IP, node.dataIp().toString());
+        }
+
+        // serialize physical interfaces, it is valid only if any of physical interface presents
+        if (node.phyIntfs() != null && !node.phyIntfs().isEmpty()) {
+            ArrayNode phyIntfs = context.mapper().createArrayNode();
+            node.phyIntfs().forEach(phyIntf -> {
+                ObjectNode phyIntfJson =
+                        context.codec(KubevirtPhyInterface.class).encode(phyIntf, context);
+                phyIntfs.add(phyIntfJson);
+            });
+            result.set(PHYSICAL_INTERFACES, phyIntfs);
+        }
+
+        return result;
+    }
+
+    @Override
+    public KubevirtNode decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        String hostname = nullIsIllegal(json.get(HOST_NAME).asText(),
+                HOST_NAME + MISSING_MESSAGE);
+        String type = nullIsIllegal(json.get(TYPE).asText(),
+                TYPE + MISSING_MESSAGE);
+        String mIp = nullIsIllegal(json.get(MANAGEMENT_IP).asText(),
+                MANAGEMENT_IP + MISSING_MESSAGE);
+
+        KubevirtNode.Builder nodeBuilder = DefaultKubevirtNode.builder()
+                .hostname(hostname)
+                .type(KubevirtNode.Type.valueOf(type))
+                .state(KubevirtNodeState.INIT)
+                .managementIp(IpAddress.valueOf(mIp));
+
+        if (json.get(DATA_IP) != null) {
+            nodeBuilder.dataIp(IpAddress.valueOf(json.get(DATA_IP).asText()));
+        }
+
+        JsonNode intBridgeJson = json.get(INTEGRATION_BRIDGE);
+        if (intBridgeJson != null) {
+            nodeBuilder.intgBridge(DeviceId.deviceId(intBridgeJson.asText()));
+        }
+
+        // parse physical interfaces
+        List<KubevirtPhyInterface> phyIntfs = new ArrayList<>();
+        JsonNode phyIntfsJson = json.get(PHYSICAL_INTERFACES);
+        if (phyIntfsJson != null) {
+            final JsonCodec<KubevirtPhyInterface>
+                    phyIntfCodec = context.codec(KubevirtPhyInterface.class);
+
+            IntStream.range(0, phyIntfsJson.size()).forEach(i -> {
+                ObjectNode intfJson = get(phyIntfsJson, i);
+                phyIntfs.add(phyIntfCodec.decode(intfJson, context));
+            });
+        }
+        nodeBuilder.phyIntfs(phyIntfs);
+
+        log.trace("node is {}", nodeBuilder.build().toString());
+
+        return nodeBuilder.build();
+    }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtPhyInterfaceCodec.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtPhyInterfaceCodec.java
new file mode 100644
index 0000000..3ba0164
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/codec/KubevirtPhyInterfaceCodec.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.kubevirtnode.codec;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface;
+import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+
+/**
+ * Kubevirt physical interface codec used for serializing and de-serializing JSON string.
+ */
+public final class KubevirtPhyInterfaceCodec extends JsonCodec<KubevirtPhyInterface> {
+
+    private static final String NETWORK = "network";
+    private static final String INTERFACE = "intf";
+
+    private static final String MISSING_MESSAGE = " is required in KubevirtPhyInterface";
+
+    @Override
+    public ObjectNode encode(KubevirtPhyInterface phyIntf, CodecContext context) {
+        checkNotNull(phyIntf, "Kubevirt physical interface cannot be null");
+
+        return context.mapper().createObjectNode()
+                .put(NETWORK, phyIntf.network())
+                .put(INTERFACE, phyIntf.intf());
+    }
+
+    @Override
+    public KubevirtPhyInterface decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        String network = nullIsIllegal(json.get(NETWORK).asText(),
+                NETWORK + MISSING_MESSAGE);
+        String intf = nullIsIllegal(json.get(INTERFACE).asText(),
+                INTERFACE + MISSING_MESSAGE);
+
+        return DefaultKubevirtPhyInterface.builder()
+                .network(network)
+                .intf(intf)
+                .build();
+    }
+}
diff --git a/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegister.java b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegister.java
new file mode 100644
index 0000000..efdf164
--- /dev/null
+++ b/apps/kubevirt-node/app/src/main/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegister.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.kubevirtnode.web;
+
+import org.onosproject.codec.CodecService;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
+import org.onosproject.kubevirtnode.codec.KubevirtNodeCodec;
+import org.onosproject.kubevirtnode.codec.KubevirtPhyInterfaceCodec;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.slf4j.Logger;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Implementation of the JSON codec brokering service for KubevirtNode.
+ */
+@Component(immediate = true)
+public class KubevirtNodeCodecRegister {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY)
+    protected CodecService codecService;
+
+    @Activate
+    protected void activate() {
+        codecService.registerCodec(KubevirtNode.class, new KubevirtNodeCodec());
+        codecService.registerCodec(KubevirtPhyInterface.class, new KubevirtPhyInterfaceCodec());
+
+        log.info("Started");
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        codecService.unregisterCodec(KubevirtNode.class);
+        codecService.unregisterCodec(KubevirtPhyInterface.class);
+
+        log.info("Stopped");
+    }
+}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodecTest.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodecTest.java
new file mode 100644
index 0000000..f8761bc
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeCodecTest.java
@@ -0,0 +1,184 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.kubevirtnode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableList;
+import org.hamcrest.MatcherAssert;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnode.api.DefaultKubevirtNode;
+import org.onosproject.kubevirtnode.api.DefaultKubevirtPhyInterface;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtNodeState;
+import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
+import org.onosproject.net.DeviceId;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onosproject.kubevirtnode.codec.KubevirtNodeJsonMatcher.matchesKubevirtNode;
+import static org.onosproject.net.NetTestTools.APP_ID;
+
+/**
+ * Unit tests for KubevirtNode codec.
+ */
+public class KubevirtNodeCodecTest {
+    MockCodecContext context;
+
+    JsonCodec<KubevirtNode> kubevirtNodeCodec;
+    JsonCodec<KubevirtPhyInterface> kubevirtPhyInterfaceJsonCodec;
+
+    final CoreService mockCoreService = createMock(CoreService.class);
+    private static final String REST_APP_ID = "org.onosproject.rest";
+
+    @Before
+    public void setUp() {
+        context = new MockCodecContext();
+        kubevirtNodeCodec = new KubevirtNodeCodec();
+        kubevirtPhyInterfaceJsonCodec = new KubevirtPhyInterfaceCodec();
+
+        assertThat(kubevirtNodeCodec, notNullValue());
+        assertThat(kubevirtPhyInterfaceJsonCodec, notNullValue());
+
+        expect(mockCoreService.registerApplication(REST_APP_ID))
+                .andReturn(APP_ID).anyTimes();
+        replay(mockCoreService);
+        context.registerService(CoreService.class, mockCoreService);
+    }
+
+    /**
+     * Tests the kubevirt compute node encoding.
+     */
+    @Test
+    public void testKubevirtComputeNodeEncode() {
+        KubevirtPhyInterface phyIntf1 = DefaultKubevirtPhyInterface.builder()
+                .network("mgmtnetwork")
+                .intf("eth3")
+                .build();
+
+        KubevirtPhyInterface phyIntf2 = DefaultKubevirtPhyInterface.builder()
+                .network("oamnetwork")
+                .intf("eth4")
+                .build();
+
+        KubevirtNode node = DefaultKubevirtNode.builder()
+                .hostname("worker")
+                .type(KubevirtNode.Type.WORKER)
+                .state(KubevirtNodeState.INIT)
+                .managementIp(IpAddress.valueOf("10.10.10.1"))
+                .intgBridge(DeviceId.deviceId("br-int"))
+                .dataIp(IpAddress.valueOf("20.20.20.2"))
+                .phyIntfs(ImmutableList.of(phyIntf1, phyIntf2))
+                .build();
+
+        ObjectNode nodeJson = kubevirtNodeCodec.encode(node, context);
+        assertThat(nodeJson, matchesKubevirtNode(node));
+    }
+
+    /**
+     * Tests the kubevirt compute node decoding.
+     *
+     * @throws IOException io exception
+     */
+    @Test
+    public void testKubevirtComputeNodeDecode() throws IOException {
+        KubevirtNode node = getKubevirtNode("KubevirtWorkerNode.json");
+
+        assertThat(node.hostname(), is("worker-01"));
+        assertThat(node.type().name(), is("WORKER"));
+        assertThat(node.managementIp().toString(), is("172.16.130.4"));
+        assertThat(node.dataIp().toString(), is("172.16.130.4"));
+        assertThat(node.intgBridge().toString(), is("of:00000000000000a1"));
+        assertThat(node.phyIntfs().size(), is(2));
+
+        node.phyIntfs().forEach(intf -> {
+            if (intf.network().equals("mgmtnetwork")) {
+                assertThat(intf.intf(), is("eth3"));
+            }
+            if (intf.network().equals("oamnetwork")) {
+                assertThat(intf.intf(), is("eth4"));
+            }
+        });
+    }
+
+    private KubevirtNode getKubevirtNode(String resourceName) throws IOException {
+        InputStream jsonStream = KubevirtNodeCodecTest.class.getResourceAsStream(resourceName);
+        JsonNode json = context.mapper().readTree(jsonStream);
+        MatcherAssert.assertThat(json, notNullValue());
+        KubevirtNode node = kubevirtNodeCodec.decode((ObjectNode) json, context);
+        assertThat(node, notNullValue());
+        return node;
+    }
+
+    /**
+     * Mock codec context for use in codec unit tests.
+     */
+    private class MockCodecContext implements CodecContext {
+
+        private final ObjectMapper mapper = new ObjectMapper();
+        private final CodecManager manager = new CodecManager();
+        private final Map<Class<?>, Object> services = new HashMap<>();
+
+        /**
+         * Constructs a new mock codec context.
+         */
+        public MockCodecContext() {
+            manager.activate();
+        }
+
+        @Override
+        public ObjectMapper mapper() {
+            return mapper;
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> JsonCodec<T> codec(Class<T> entityClass) {
+            if (entityClass == KubevirtPhyInterface.class) {
+                return (JsonCodec<T>) kubevirtPhyInterfaceJsonCodec;
+            }
+
+            return manager.getCodec(entityClass);
+        }
+
+        @SuppressWarnings("unchecked")
+        @Override
+        public <T> T getService(Class<T> serviceClass) {
+            return (T) services.get(serviceClass);
+        }
+
+        // for registering mock services
+        public <T> void registerService(Class<T> serviceClass, T impl) {
+            services.put(serviceClass, impl);
+        }
+    }
+}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeJsonArrayMatcher.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeJsonArrayMatcher.java
new file mode 100644
index 0000000..1e51616
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeJsonArrayMatcher.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.kubevirtnode.codec;
+
+import com.eclipsesource.json.JsonArray;
+import com.eclipsesource.json.JsonObject;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+
+/**
+ * Hamcrest matcher for kubevirt node array.
+ */
+public class KubevirtNodeJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
+
+    private final KubevirtNode node;
+    private String reason = "";
+
+    public KubevirtNodeJsonArrayMatcher(KubevirtNode node) {
+        this.node = node;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonArray json) {
+        boolean nodeFound = false;
+        for (int jsonNodeIndex = 0; jsonNodeIndex < json.size(); jsonNodeIndex++) {
+            final JsonObject jsonNode = json.get(jsonNodeIndex).asObject();
+
+            final String hostname = node.hostname();
+            final String jsonHostname = jsonNode.get("hostname").asString();
+            if (jsonHostname.equals(hostname)) {
+                nodeFound = true;
+            }
+        }
+
+        if (!nodeFound) {
+            reason = "Node with hostname " + node.hostname() + " not found";
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(reason);
+    }
+
+    /**
+     * Factory to allocate a node array matcher.
+     *
+     * @param node node object we are looking for
+     * @return matcher
+     */
+    public static KubevirtNodeJsonArrayMatcher hasNode(KubevirtNode node) {
+        return new KubevirtNodeJsonArrayMatcher(node);
+    }
+}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeJsonMatcher.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeJsonMatcher.java
new file mode 100644
index 0000000..92af688
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtNodeJsonMatcher.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.kubevirtnode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
+import org.onosproject.kubevirtnode.api.Constants;
+
+/**
+ * Hamcrest matcher for kubevirt node.
+ */
+public final class KubevirtNodeJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final KubevirtNode node;
+    private static final String INTEGRATION_BRIDGE = "integrationBridge";
+    private static final String STATE = "state";
+    private static final String PHYSICAL_INTERFACES = "phyIntfs";
+
+    private KubevirtNodeJsonMatcher(KubevirtNode node) {
+        this.node = node;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+        // check hostname
+        String jsonHostname = jsonNode.get(Constants.HOST_NAME).asText();
+        String hostname = node.hostname();
+        if (!jsonHostname.equals(hostname)) {
+            description.appendText("hostname was " + jsonHostname);
+            return false;
+        }
+
+        // check type
+        String jsonType = jsonNode.get(Constants.TYPE).asText();
+        String type = node.type().name();
+        if (!jsonType.equals(type)) {
+            description.appendText("type was " + jsonType);
+            return false;
+        }
+
+        // check management IP
+        String jsonMgmtIp = jsonNode.get(Constants.MANAGEMENT_IP).asText();
+        String mgmtIp = node.managementIp().toString();
+        if (!jsonMgmtIp.equals(mgmtIp)) {
+            description.appendText("management IP was " + jsonMgmtIp);
+            return false;
+        }
+
+        // check integration bridge
+        JsonNode jsonIntgBridge = jsonNode.get(INTEGRATION_BRIDGE);
+        if (jsonIntgBridge != null) {
+            String intgBridge = node.intgBridge().toString();
+            if (!jsonIntgBridge.asText().equals(intgBridge)) {
+                description.appendText("integration bridge was " + jsonIntgBridge);
+                return false;
+            }
+        }
+
+        // check state
+        String jsonState = jsonNode.get(STATE).asText();
+        String state = node.state().name();
+        if (!jsonState.equals(state)) {
+            description.appendText("state was " + jsonState);
+            return false;
+        }
+
+        // check data IP
+        JsonNode jsonDataIp = jsonNode.get(Constants.DATA_IP);
+        if (jsonDataIp != null) {
+            String dataIp = node.dataIp().toString();
+            if (!jsonDataIp.asText().equals(dataIp)) {
+                description.appendText("Data IP was " + jsonDataIp.asText());
+                return false;
+            }
+        }
+
+        // check physical interfaces
+        JsonNode jsonPhyIntfs = jsonNode.get(PHYSICAL_INTERFACES);
+        if (jsonPhyIntfs != null) {
+            if (jsonPhyIntfs.size() != node.phyIntfs().size()) {
+                description.appendText("physical interface size was " + jsonPhyIntfs.size());
+                return false;
+            }
+
+            for (KubevirtPhyInterface phyIntf : node.phyIntfs()) {
+                boolean intfFound = false;
+                for (int intfIndex = 0; intfIndex < jsonPhyIntfs.size(); intfIndex++) {
+                    KubevirtPhyInterfaceJsonMatcher intfMatcher =
+                            KubevirtPhyInterfaceJsonMatcher.matchesKubevirtPhyInterface(phyIntf);
+                    if (intfMatcher.matches(jsonPhyIntfs.get(intfIndex))) {
+                        intfFound = true;
+                        break;
+                    }
+                }
+
+                if (!intfFound) {
+                    description.appendText("PhyIntf not found " + phyIntf.toString());
+                    return false;
+                }
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(node.toString());
+    }
+
+    /**
+     * Factory to allocate an kubevirt node matcher.
+     *
+     * @param node kubevirt node object we are looking for
+     * @return matcher
+     */
+    public static KubevirtNodeJsonMatcher matchesKubevirtNode(KubevirtNode node) {
+        return new KubevirtNodeJsonMatcher(node);
+    }
+}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtPhyInterfaceJsonMatcher.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtPhyInterfaceJsonMatcher.java
new file mode 100644
index 0000000..cecdc4a
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/codec/KubevirtPhyInterfaceJsonMatcher.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.kubevirtnode.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
+
+/**
+ * Hamcrest matcher for kubevirt physical interface.
+ */
+public final class KubevirtPhyInterfaceJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+    private final KubevirtPhyInterface phyIntf;
+    private static final String NETWORK = "network";
+    private static final String INTERFACE = "intf";
+
+    private KubevirtPhyInterfaceJsonMatcher(KubevirtPhyInterface phyIntf) {
+        this.phyIntf = phyIntf;
+    }
+
+    @Override
+    protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+        // check network name
+        String jsonNetwork = jsonNode.get(NETWORK).asText();
+        String network = phyIntf.network();
+        if (!jsonNetwork.equals(network)) {
+            description.appendText("network name was " + jsonNetwork);
+            return false;
+        }
+
+        // check interface name
+        String jsonIntf = jsonNode.get(INTERFACE).asText();
+        String intf = phyIntf.intf();
+        if (!jsonIntf.equals(intf)) {
+            description.appendText("interface name was " + jsonIntf);
+            return false;
+        }
+
+        return true;
+    }
+
+    @Override
+    public void describeTo(Description description) {
+        description.appendText(phyIntf.toString());
+    }
+
+    /**
+     * Factory to allocate an openstack physical interface matcher.
+     *
+     * @param phyIntf kubevirt physical interface object we are looking for
+     * @return matcher
+     */
+    public static KubevirtPhyInterfaceJsonMatcher
+                    matchesKubevirtPhyInterface(KubevirtPhyInterface phyIntf) {
+        return new KubevirtPhyInterfaceJsonMatcher(phyIntf);
+    }
+}
diff --git a/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegisterTest.java b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegisterTest.java
new file mode 100644
index 0000000..9335153
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/java/org/onosproject/kubevirtnode/web/KubevirtNodeCodecRegisterTest.java
@@ -0,0 +1,88 @@
+/*
+ * Copyright 2020-present Open Networking Foundation
+ *
+ * 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.kubevirtnode.web;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
+import org.junit.Test;
+import org.onlab.junit.TestUtils;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.kubevirtnode.api.KubevirtNode;
+import org.onosproject.kubevirtnode.api.KubevirtPhyInterface;
+import org.onosproject.kubevirtnode.codec.KubevirtNodeCodec;
+import org.onosproject.kubevirtnode.codec.KubevirtPhyInterfaceCodec;
+
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+/**
+ * Unit test for kubevirt node codec register.
+ */
+public final class KubevirtNodeCodecRegisterTest {
+
+    private KubevirtNodeCodecRegister register;
+
+    /**
+     * Tests codec register activation and deactivation.
+     */
+    @Test
+    public void testActivateDeactivate() {
+        register = new KubevirtNodeCodecRegister();
+        CodecService codecService = new TestCodecService();
+
+        TestUtils.setField(register, "codecService", codecService);
+        register.activate();
+
+        assertEquals(KubevirtNodeCodec.class.getName(),
+                codecService.getCodec(KubevirtNode.class).getClass().getName());
+        assertEquals(KubevirtPhyInterfaceCodec.class.getName(),
+                codecService.getCodec(KubevirtPhyInterface.class).getClass().getName());
+
+        register.deactivate();
+
+        assertNull(codecService.getCodec(KubevirtNode.class));
+        assertNull(codecService.getCodec(KubevirtPhyInterface.class));
+    }
+
+    private static class TestCodecService implements CodecService {
+
+        private Map<String, JsonCodec> codecMap = Maps.newConcurrentMap();
+
+        @Override
+        public Set<Class<?>> getCodecs() {
+            return ImmutableSet.of();
+        }
+
+        @Override
+        public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
+            return codecMap.get(entityClass.getName());
+        }
+
+        @Override
+        public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
+            codecMap.put(entityClass.getName(), codec);
+        }
+
+        @Override
+        public void unregisterCodec(Class<?> entityClass) {
+            codecMap.remove(entityClass.getName());
+        }
+    }
+}
diff --git a/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtWorkerNode.json b/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtWorkerNode.json
new file mode 100644
index 0000000..70b449d
--- /dev/null
+++ b/apps/kubevirt-node/app/src/test/resources/org/onosproject/kubevirtnode/codec/KubevirtWorkerNode.json
@@ -0,0 +1,17 @@
+{
+  "hostname": "worker-01",
+  "type": "WORKER",
+  "managementIp": "172.16.130.4",
+  "dataIp": "172.16.130.4",
+  "integrationBridge": "of:00000000000000a1",
+  "phyIntfs": [
+    {
+      "network": "mgmtnetwork",
+      "intf": "eth3"
+    },
+    {
+      "network": "oamnetwork",
+      "intf": "eth4"
+    }
+  ]
+}
\ No newline at end of file