Add a set of codecs for kubevirt network with unit test cases
Change-Id: I0734a085258236f524d0d251ab71056eb5602dc4
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtNetwork.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtNetwork.java
index dc221ab..97808bb 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtNetwork.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/DefaultKubevirtNetwork.java
@@ -39,7 +39,7 @@
private final String segmentId;
private final IpAddress gatewayIp;
private final String cidr;
- private final Set<KubevirtHostRoute> hostRouts;
+ private final Set<KubevirtHostRoute> hostRoutes;
private final KubevirtIpPool ipPool;
/**
@@ -52,12 +52,12 @@
* @param segmentId segment identifier
* @param gatewayIp gateway IP address
* @param cidr CIDR of network
- * @param hostRouts a set of host routes
+ * @param hostRoutes a set of host routes
* @param ipPool IP pool
*/
public DefaultKubevirtNetwork(String networkId, Type type, String name,
Integer mtu, String segmentId, IpAddress gatewayIp,
- String cidr, Set<KubevirtHostRoute> hostRouts,
+ String cidr, Set<KubevirtHostRoute> hostRoutes,
KubevirtIpPool ipPool) {
this.networkId = networkId;
this.type = type;
@@ -66,7 +66,7 @@
this.segmentId = segmentId;
this.gatewayIp = gatewayIp;
this.cidr = cidr;
- this.hostRouts = hostRouts;
+ this.hostRoutes = hostRoutes;
this.ipPool = ipPool;
}
@@ -106,8 +106,8 @@
}
@Override
- public Set<KubevirtHostRoute> hostRouts() {
- return ImmutableSet.copyOf(hostRouts);
+ public Set<KubevirtHostRoute> hostRoutes() {
+ return ImmutableSet.copyOf(hostRoutes);
}
@Override
@@ -127,14 +127,14 @@
return networkId.equals(that.networkId) && type == that.type &&
name.equals(that.name) && mtu.equals(that.mtu) &&
gatewayIp.equals(that.gatewayIp) &&
- cidr.equals(that.cidr) && hostRouts.equals(that.hostRouts) &&
+ cidr.equals(that.cidr) && hostRoutes.equals(that.hostRoutes) &&
ipPool.equals(that.ipPool);
}
@Override
public int hashCode() {
return Objects.hash(networkId, type, name, mtu, segmentId, gatewayIp,
- cidr, hostRouts, ipPool);
+ cidr, hostRoutes, ipPool);
}
@Override
@@ -147,7 +147,7 @@
.add("segmentId", segmentId)
.add("gatewayIp", gatewayIp)
.add("cidr", cidr)
- .add("hostRouts", hostRouts)
+ .add("hostRouts", hostRoutes)
.add("ipPool", ipPool)
.toString();
}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtHostRoute.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtHostRoute.java
index 37cfc6b..b435cd6 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtHostRoute.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtHostRoute.java
@@ -44,7 +44,7 @@
*
* @return destination CIDR
*/
- public IpPrefix getDestination() {
+ public IpPrefix destination() {
return destination;
}
@@ -53,7 +53,7 @@
*
* @return nexthop IP address
*/
- public IpAddress getNexthop() {
+ public IpAddress nexthop() {
return nexthop;
}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtIpPool.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtIpPool.java
index 9b42b61..64874fd 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtIpPool.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtIpPool.java
@@ -44,7 +44,7 @@
*
* @return start address of IP pool
*/
- public IpAddress getStart() {
+ public IpAddress start() {
return start;
}
@@ -53,7 +53,7 @@
*
* @return end address of IP pool
*/
- public IpAddress getEnd() {
+ public IpAddress end() {
return end;
}
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtNetwork.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtNetwork.java
index 5492c9f..4293310 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtNetwork.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/KubevirtNetwork.java
@@ -104,7 +104,7 @@
*
* @return host routes
*/
- Set<KubevirtHostRoute> hostRouts();
+ Set<KubevirtHostRoute> hostRoutes();
/**
* Returns the IP pool.
diff --git a/apps/kubevirt-networking/app/BUILD b/apps/kubevirt-networking/app/BUILD
index 1d01e80..75b5b42 100644
--- a/apps/kubevirt-networking/app/BUILD
+++ b/apps/kubevirt-networking/app/BUILD
@@ -22,6 +22,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-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtHostRouteCodec.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtHostRouteCodec.java
new file mode 100644
index 0000000..2d73979
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtHostRouteCodec.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Kubevirt host route codec used for serializing and de-serializing JSON string.
+ */
+public final class KubevirtHostRouteCodec extends JsonCodec<KubevirtHostRoute> {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String DESTINATION = "destination";
+ private static final String NEXTHOP = "nexthop";
+
+ private static final String MISSING_MESSAGE = " is required in KubevirtHostRoute";
+
+ @Override
+ public ObjectNode encode(KubevirtHostRoute hostRoute, CodecContext context) {
+ checkNotNull(hostRoute, "Kubernetes network cannot be null");
+
+ ObjectNode result = context.mapper().createObjectNode()
+ .put(DESTINATION, hostRoute.destination().toString());
+
+ if (hostRoute.nexthop() != null) {
+ result.put(NEXTHOP, hostRoute.nexthop().toString());
+ }
+
+ return result;
+ }
+
+ @Override
+ public KubevirtHostRoute decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ String destination = nullIsIllegal(json.get(DESTINATION).asText(),
+ DESTINATION + MISSING_MESSAGE);
+ JsonNode nexthopJson = json.get(NEXTHOP);
+ IpAddress nexthop = null;
+
+ if (nexthopJson != null) {
+ nexthop = IpAddress.valueOf(nexthopJson.asText());
+ }
+
+ return new KubevirtHostRoute(IpPrefix.valueOf(destination), nexthop);
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtIpPoolCodec.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtIpPoolCodec.java
new file mode 100644
index 0000000..8f5522c
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtIpPoolCodec.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.codec;
+
+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.kubevirtnetworking.api.KubevirtIpPool;
+import org.slf4j.Logger;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Kubevirt IP pool codec used for serializing and de-serializing JSON string.
+ */
+public final class KubevirtIpPoolCodec extends JsonCodec<KubevirtIpPool> {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String START = "start";
+ private static final String END = "end";
+
+ private static final String MISSING_MESSAGE = " is required in KubevirtIpPool";
+
+ @Override
+ public ObjectNode encode(KubevirtIpPool ipPool, CodecContext context) {
+ checkNotNull(ipPool, "Kubevirt IP pool cannot be null");
+
+ return context.mapper().createObjectNode()
+ .put(START, ipPool.start().toString())
+ .put(END, ipPool.end().toString());
+ }
+
+ @Override
+ public KubevirtIpPool decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ String start = nullIsIllegal(json.get(START).asText(), START + MISSING_MESSAGE);
+ String end = nullIsIllegal(json.get(END).asText(), END + MISSING_MESSAGE);
+
+ return new KubevirtIpPool(IpAddress.valueOf(start), IpAddress.valueOf(end));
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkCodec.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkCodec.java
new file mode 100644
index 0000000..3416ba2
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkCodec.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.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.kubevirtnetworking.api.DefaultKubevirtNetwork;
+import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
+import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
+import org.slf4j.Logger;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.stream.IntStream;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onlab.util.Tools.nullIsIllegal;
+import static org.slf4j.LoggerFactory.getLogger;
+
+/**
+ * Kubevirt network codec used for serializing and de-serializing JSON string.
+ */
+public final class KubevirtNetworkCodec extends JsonCodec<KubevirtNetwork> {
+
+ private final Logger log = getLogger(getClass());
+
+ private static final String NETWORK_ID = "networkId";
+ private static final String TYPE = "type";
+ private static final String NAME = "name";
+ private static final String MTU = "mtu";
+ private static final String SEGMENT_ID = "segmentId";
+ private static final String GATEWAY_IP = "gatewayIp";
+ private static final String CIDR = "cidr";
+ private static final String HOST_ROUTES = "hostRoutes";
+ private static final String IP_POOL = "ipPool";
+
+ private static final String MISSING_MESSAGE = " is required in KubevirtNetwork";
+
+ @Override
+ public ObjectNode encode(KubevirtNetwork network, CodecContext context) {
+ checkNotNull(network, "Kubevirt network cannot be null");
+
+ ObjectNode result = context.mapper().createObjectNode()
+ .put(NETWORK_ID, network.networkId())
+ .put(TYPE, network.type().name())
+ .put(NAME, network.name())
+ .put(MTU, network.mtu())
+ .put(GATEWAY_IP, network.gatewayIp().toString())
+ .put(CIDR, network.cidr());
+
+ if (network.segmentId() != null) {
+ result.put(SEGMENT_ID, network.segmentId());
+ }
+
+ if (network.hostRoutes() != null && !network.hostRoutes().isEmpty()) {
+ ArrayNode hostRoutes = context.mapper().createArrayNode();
+ network.hostRoutes().forEach(hostRoute -> {
+ ObjectNode hostRouteJson =
+ context.codec(KubevirtHostRoute.class).encode(hostRoute, context);
+ hostRoutes.add(hostRouteJson);
+ });
+ result.set(HOST_ROUTES, hostRoutes);
+ }
+
+ if (network.ipPool() != null) {
+ ObjectNode ipPoolJson = context.codec(KubevirtIpPool.class).encode(network.ipPool(), context);
+ result.set(IP_POOL, ipPoolJson);
+ }
+
+ return result;
+ }
+
+ @Override
+ public KubevirtNetwork decode(ObjectNode json, CodecContext context) {
+ if (json == null || !json.isObject()) {
+ return null;
+ }
+
+ String networkId = nullIsIllegal(json.get(NETWORK_ID).asText(),
+ NETWORK_ID + MISSING_MESSAGE);
+ String type = nullIsIllegal(json.get(TYPE).asText(),
+ TYPE + MISSING_MESSAGE);
+ String name = nullIsIllegal(json.get(NAME).asText(),
+ NAME + MISSING_MESSAGE);
+ Integer mtu = nullIsIllegal(json.get(MTU).asInt(),
+ MTU + MISSING_MESSAGE);
+ String gatewayIp = nullIsIllegal(json.get(GATEWAY_IP).asText(),
+ GATEWAY_IP + MISSING_MESSAGE);
+ String cidr = nullIsIllegal(json.get(CIDR).asText(),
+ CIDR + MISSING_MESSAGE);
+
+ KubevirtNetwork.Builder networkBuilder = DefaultKubevirtNetwork.builder()
+ .networkId(networkId)
+ .type(KubevirtNetwork.Type.valueOf(type))
+ .name(name)
+ .mtu(mtu)
+ .gatewayIp(IpAddress.valueOf(gatewayIp))
+ .cidr(cidr);
+
+ JsonNode ipPoolJson = json.get(IP_POOL);
+ if (ipPoolJson != null) {
+ final JsonCodec<KubevirtIpPool>
+ ipPoolCodec = context.codec(KubevirtIpPool.class);
+ networkBuilder.ipPool(ipPoolCodec.decode(
+ (ObjectNode) ipPoolJson.deepCopy(), context));
+ }
+
+ // parse host routes
+ Set<KubevirtHostRoute> hostRoutes = new HashSet<>();
+ JsonNode hostRoutesJson = json.get(HOST_ROUTES);
+ if (hostRoutesJson != null) {
+ final JsonCodec<KubevirtHostRoute>
+ hostRouteCodec = context.codec(KubevirtHostRoute.class);
+
+ IntStream.range(0, hostRoutesJson.size()).forEach(i -> {
+ ObjectNode routeJson = get(hostRoutesJson, i);
+ hostRoutes.add(hostRouteCodec.decode(routeJson, context));
+ });
+ }
+ networkBuilder.hostRoutes(hostRoutes);
+
+ log.trace("Network is {}", networkBuilder.build().toString());
+
+ return networkBuilder.build();
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtHostRouteJsonMatcher.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtHostRouteJsonMatcher.java
new file mode 100644
index 0000000..4603980
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtHostRouteJsonMatcher.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
+
+/**
+ * Hamcrest matcher for kubevirt host route interface.
+ */
+public final class KubevirtHostRouteJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final KubevirtHostRoute hostRoute;
+
+ private static final String DESTINATION = "destination";
+ private static final String NEXTHOP = "nexthop";
+
+ private KubevirtHostRouteJsonMatcher(KubevirtHostRoute hostRoute) {
+ this.hostRoute = hostRoute;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+ // check destination
+ String jsonDestination = jsonNode.get(DESTINATION).asText();
+ String destination = hostRoute.destination().toString();
+ if (!jsonDestination.equals(destination)) {
+ description.appendText("destination was " + jsonDestination);
+ return false;
+ }
+
+ // check nexthop
+ JsonNode jsonNexthop = jsonNode.get(NEXTHOP);
+ if (jsonNexthop != null) {
+ String nexthop = hostRoute.nexthop().toString();
+ if (!jsonNexthop.asText().equals(nexthop)) {
+ description.appendText("nexthop was " + jsonNexthop);
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(description.toString());
+ }
+
+ /**
+ * Factory to allocate an kubevirt host route matcher.
+ *
+ * @param route kubevirt host route object we are looking for
+ * @return matcher
+ */
+ public static KubevirtHostRouteJsonMatcher matchesKubevirtHostRoute(KubevirtHostRoute route) {
+ return new KubevirtHostRouteJsonMatcher(route);
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtIpPoolJsonMatcher.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtIpPoolJsonMatcher.java
new file mode 100644
index 0000000..6217335
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtIpPoolJsonMatcher.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
+
+/**
+ * Hamcrest matcher for kubevirt IP pool interface.
+ */
+public final class KubevirtIpPoolJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final KubevirtIpPool ipPool;
+ private static final String START = "start";
+ private static final String END = "end";
+
+ private KubevirtIpPoolJsonMatcher(KubevirtIpPool ipPool) {
+ this.ipPool = ipPool;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+ // check start
+ String jsonStart = jsonNode.get(START).asText();
+ String start = ipPool.start().toString();
+ if (!jsonStart.equals(start)) {
+ description.appendText("start was " + jsonStart);
+ return false;
+ }
+
+ // check end
+ String jsonEnd = jsonNode.get(END).asText();
+ String end = ipPool.end().toString();
+ if (!jsonEnd.equals(end)) {
+ description.appendText("end was " + jsonEnd);
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(ipPool.toString());
+ }
+
+ /**
+ * Factory to allocate an kubevirt IP pool matcher.
+ *
+ * @param ipPool kubevirt IP pool object we are looking for
+ * @return matcher
+ */
+ public static KubevirtIpPoolJsonMatcher matchesKubevirtIpPool(KubevirtIpPool ipPool) {
+ return new KubevirtIpPoolJsonMatcher(ipPool);
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkCodecTest.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkCodecTest.java
new file mode 100644
index 0000000..285b190
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkCodecTest.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.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.ImmutableSet;
+import org.hamcrest.MatcherAssert;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.core.CoreService;
+import org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork;
+import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
+import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
+
+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.kubevirtnetworking.codec.KubevirtNetworkJsonMatcher.matchesKubevirtNetwork;
+import static org.onosproject.net.NetTestTools.APP_ID;
+
+/**
+ * Unit tests for KubevirtNetwork codec.
+ */
+public final class KubevirtNetworkCodecTest {
+
+ MockCodecContext context;
+
+ JsonCodec<KubevirtNetwork> kubevirtNetworkCodec;
+ JsonCodec<KubevirtHostRoute> kubevirtHostRouteCodec;
+ JsonCodec<KubevirtIpPool> kubevirtIpPoolCodec;
+
+ final CoreService mockCoreService = createMock(CoreService.class);
+ private static final String REST_APP_ID = "org.onosproject.rest";
+
+ @Before
+ public void setUp() {
+ context = new MockCodecContext();
+ kubevirtNetworkCodec = new KubevirtNetworkCodec();
+ kubevirtHostRouteCodec = new KubevirtHostRouteCodec();
+ kubevirtIpPoolCodec = new KubevirtIpPoolCodec();
+
+ assertThat(kubevirtNetworkCodec, notNullValue());
+ assertThat(kubevirtHostRouteCodec, notNullValue());
+ assertThat(kubevirtIpPoolCodec, notNullValue());
+
+ expect(mockCoreService.registerApplication(REST_APP_ID))
+ .andReturn(APP_ID).anyTimes();
+ replay(mockCoreService);
+ context.registerService(CoreService.class, mockCoreService);
+ }
+
+ /**
+ * Tests the kubevirt network encoding.
+ */
+ @Test
+ public void testKubevirtNetworkEncode() {
+ KubevirtHostRoute hostRoute1 = new KubevirtHostRoute(IpPrefix.valueOf("10.10.10.0/24"),
+ IpAddress.valueOf("20.20.20.1"));
+ KubevirtHostRoute hostRoute2 = new KubevirtHostRoute(IpPrefix.valueOf("20.20.20.0/24"),
+ IpAddress.valueOf("10.10.10.1"));
+
+ KubevirtIpPool ipPool = new KubevirtIpPool(IpAddress.valueOf("10.10.10.100"),
+ IpAddress.valueOf("10.10.10.200"));
+
+ KubevirtNetwork network = DefaultKubevirtNetwork.builder()
+ .networkId("net-1")
+ .name("net-1")
+ .type(KubevirtNetwork.Type.FLAT)
+ .gatewayIp(IpAddress.valueOf("10.10.10.1"))
+ .mtu(1500)
+ .cidr("10.10.10.0/24")
+ .hostRoutes(ImmutableSet.of(hostRoute1, hostRoute2))
+ .ipPool(ipPool)
+ .build();
+
+ ObjectNode networkJson = kubevirtNetworkCodec.encode(network, context);
+ assertThat(networkJson, matchesKubevirtNetwork(network));
+ }
+
+ /**
+ * Tests the kubevirt network decoding.
+ *
+ * @throws IOException io exception
+ */
+ @Test
+ public void testKubevirtNetworkDecode() throws IOException {
+ KubevirtNetwork network = getKubevirtNetwork("KubevirtNetwork.json");
+
+ assertThat(network.networkId(), is("network-1"));
+ assertThat(network.name(), is("network-1"));
+ assertThat(network.type().name(), is("FLAT"));
+ assertThat(network.cidr(), is("10.10.0.0/24"));
+ assertThat(network.gatewayIp().toString(), is("10.10.0.1"));
+ assertThat(network.ipPool().start().toString(), is("10.10.10.100"));
+ assertThat(network.ipPool().end().toString(), is("10.10.10.200"));
+ }
+
+ private KubevirtNetwork getKubevirtNetwork(String resourceName) throws IOException {
+ InputStream jsonStream = KubevirtNetworkCodecTest.class.getResourceAsStream(resourceName);
+ JsonNode json = context.mapper().readTree(jsonStream);
+ MatcherAssert.assertThat(json, notNullValue());
+ KubevirtNetwork network = kubevirtNetworkCodec.decode((ObjectNode) json, context);
+ assertThat(network, notNullValue());
+ return network;
+ }
+
+ 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;
+ }
+
+ @Override
+ public <T> JsonCodec<T> codec(Class<T> entityClass) {
+ if (entityClass == KubevirtHostRoute.class) {
+ return (JsonCodec<T>) kubevirtHostRouteCodec;
+ }
+
+ if (entityClass == KubevirtIpPool.class) {
+ return (JsonCodec<T>) kubevirtIpPoolCodec;
+ }
+
+ return manager.getCodec(entityClass);
+ }
+
+ @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-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkJsonArrayMatcher.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkJsonArrayMatcher.java
new file mode 100644
index 0000000..6701c7b
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkJsonArrayMatcher.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.codec;
+
+import com.eclipsesource.json.JsonArray;
+import com.eclipsesource.json.JsonObject;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeMatcher;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
+
+public final class KubevirtNetworkJsonArrayMatcher extends TypeSafeMatcher<JsonArray> {
+
+ private final KubevirtNetwork network;
+ private String reason = "";
+
+ public KubevirtNetworkJsonArrayMatcher(KubevirtNetwork network) {
+ this.network = network;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonArray json) {
+ boolean networkFound = false;
+ for (int jsonNetworkIndex = 0; jsonNetworkIndex < json.size(); jsonNetworkIndex++) {
+ final JsonObject jsonNode = json.get(jsonNetworkIndex).asObject();
+
+ final String networkId = network.networkId();
+ final String jsonNetworkId = jsonNode.get("networkId").asString();
+ if (jsonNetworkId.equals(networkId)) {
+ networkFound = true;
+ }
+ }
+
+ if (!networkFound) {
+ reason = "Network with networkId " + network.networkId() + " not found";
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(reason);
+ }
+
+ /**
+ * Factory to allocate a network array matcher.
+ *
+ * @param network network object we are looking for
+ * @return matcher
+ */
+ public static KubevirtNetworkJsonArrayMatcher hasNetwork(KubevirtNetwork network) {
+ return new KubevirtNetworkJsonArrayMatcher(network);
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkJsonMatcher.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkJsonMatcher.java
new file mode 100644
index 0000000..215e289
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/codec/KubevirtNetworkJsonMatcher.java
@@ -0,0 +1,163 @@
+/*
+ * Copyright 2021-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.kubevirtnetworking.codec;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import org.hamcrest.Description;
+import org.hamcrest.TypeSafeDiagnosingMatcher;
+import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
+import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
+
+/**
+ * Hamcrest matcher for kubevirt network.
+ */
+public final class KubevirtNetworkJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
+
+ private final KubevirtNetwork network;
+ private static final String NETWORK_ID = "networkId";
+ private static final String TYPE = "type";
+ private static final String NAME = "name";
+ private static final String MTU = "mtu";
+ private static final String SEGMENT_ID = "segmentId";
+ private static final String GATEWAY_IP = "gatewayIp";
+ private static final String CIDR = "cidr";
+ private static final String HOST_ROUTES = "hostRoutes";
+ private static final String IP_POOL = "ipPool";
+
+ private KubevirtNetworkJsonMatcher(KubevirtNetwork network) {
+ this.network = network;
+ }
+
+ @Override
+ protected boolean matchesSafely(JsonNode jsonNode, Description description) {
+ // check network ID
+ String jsonNetworkId = jsonNode.get(NETWORK_ID).asText();
+ String networkId = network.networkId();
+ if (!jsonNetworkId.equals(networkId)) {
+ description.appendText("network ID was " + jsonNetworkId);
+ return false;
+ }
+
+ // check type
+ String jsonType = jsonNode.get(TYPE).asText();
+ String type = network.type().name();
+ if (!jsonType.equals(type)) {
+ description.appendText("network type was " + jsonType);
+ return false;
+ }
+
+ // check name
+ String jsonName = jsonNode.get(NAME).asText();
+ String name = network.name();
+ if (!jsonName.equals(name)) {
+ description.appendText("network name was " + jsonName);
+ return false;
+ }
+
+ // check MTU
+ int jsonMtu = jsonNode.get(MTU).asInt();
+ int mtu = network.mtu();
+ if (jsonMtu != mtu) {
+ description.appendText("network MTU was " + jsonMtu);
+ return false;
+ }
+
+ // check gateway IP
+ String jsonGatewayIp = jsonNode.get(GATEWAY_IP).asText();
+ String gatewayIp = network.gatewayIp().toString();
+ if (!jsonGatewayIp.equals(gatewayIp)) {
+ description.appendText("gateway IP was " + jsonGatewayIp);
+ return false;
+ }
+
+ // check CIDR
+ String jsonCidr = jsonNode.get(CIDR).asText();
+ String cidr = network.cidr();
+ if (!jsonCidr.equals(cidr)) {
+ description.appendText("CIDR was " + jsonCidr);
+ return false;
+ }
+
+ // check segment ID
+ JsonNode jsonSegmentId = jsonNode.get(SEGMENT_ID);
+ if (jsonSegmentId != null) {
+ String segmentId = network.segmentId();
+ if (!jsonSegmentId.asText().equals(segmentId)) {
+ description.appendText("segment ID was " + jsonSegmentId.asText());
+ return false;
+ }
+ }
+
+ // check ip pool
+ JsonNode jsonIpPool = jsonNode.get(IP_POOL);
+ if (jsonIpPool != null) {
+ KubevirtIpPool ipPool = network.ipPool();
+ KubevirtIpPoolJsonMatcher ipPoolMatcher =
+ KubevirtIpPoolJsonMatcher.matchesKubevirtIpPool(ipPool);
+ if (ipPoolMatcher.matches(jsonIpPool)) {
+ return true;
+ } else {
+ description.appendText("IP pool was " + jsonIpPool.toString());
+ return false;
+ }
+ }
+
+ // check host routes
+ JsonNode jsonHostRoutes = jsonNode.get(HOST_ROUTES);
+ if (jsonHostRoutes != null) {
+ if (jsonHostRoutes.size() != network.hostRoutes().size()) {
+ description.appendText("host routes size was " + jsonHostRoutes.size());
+ return false;
+ }
+
+ for (KubevirtHostRoute hostRoute : network.hostRoutes()) {
+ boolean routeFound = false;
+ for (int routeIndex = 0; routeIndex < jsonHostRoutes.size(); routeIndex++) {
+ KubevirtHostRouteJsonMatcher routeMatcher =
+ KubevirtHostRouteJsonMatcher.matchesKubevirtHostRoute(hostRoute);
+ if (routeMatcher.matches(jsonHostRoutes.get(routeIndex))) {
+ routeFound = true;
+ break;
+ }
+ }
+
+ if (!routeFound) {
+ description.appendText("Host route not found " + hostRoute.toString());
+ return false;
+ }
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(network.toString());
+ }
+
+ /**
+ * Factory to allocate an kubevirt network matcher.
+ *
+ * @param network kubevirt network object we are looking for
+ * @return matcher
+ */
+ public static KubevirtNetworkJsonMatcher
+ matchesKubevirtNetwork(KubevirtNetwork network) {
+ return new KubevirtNetworkJsonMatcher(network);
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/resources/org/onosproject/kubevirtnetworking/codec/KubevirtNetwork.json b/apps/kubevirt-networking/app/src/test/resources/org/onosproject/kubevirtnetworking/codec/KubevirtNetwork.json
new file mode 100644
index 0000000..df048ba
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/resources/org/onosproject/kubevirtnetworking/codec/KubevirtNetwork.json
@@ -0,0 +1,12 @@
+{
+ "networkId": "network-1",
+ "name": "network-1",
+ "type": "FLAT",
+ "mtu": 1500,
+ "cidr": "10.10.0.0/24",
+ "gatewayIp": "10.10.0.1",
+ "ipPool": {
+ "start": "10.10.10.100",
+ "end": "10.10.10.200"
+ }
+}
\ No newline at end of file
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
index cecdc4a..b0c7dc2 100644
--- 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
@@ -60,7 +60,7 @@
}
/**
- * Factory to allocate an openstack physical interface matcher.
+ * Factory to allocate an kubevirt physical interface matcher.
*
* @param phyIntf kubevirt physical interface object we are looking for
* @return matcher