Add kubevirt node related codec with unit tests
Change-Id: I0c68d4c3d0c7626a4b6b66e5c783631cb9be50a8
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