Add REST API and CLI CURD support for kubevirt network
Change-Id: I5e01036c42157013ca3fb4665c67c8d47cf22c7b
diff --git a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
index ca0eecd..9f8201a 100644
--- a/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
+++ b/apps/kubevirt-networking/api/src/main/java/org/onosproject/kubevirtnetworking/api/Constants.java
@@ -53,4 +53,20 @@
// flow rule priority
public static final int PRIORITY_SWITCHING_RULE = 30000;
+
+ // CLI item length
+ public static final int CLI_ID_LENGTH = 30;
+ public static final int CLI_NAME_LENGTH = 30;
+ public static final int CLI_IP_ADDRESSES_LENGTH = 50;
+ public static final int CLI_IP_ADDRESS_LENGTH = 25;
+ public static final int CLI_MAC_ADDRESS_LENGTH = 25;
+ public static final int CLI_PORTS_LENGTH = 20;
+ public static final int CLI_NAMESPACE_LENGTH = 15;
+ public static final int CLI_PHASE_LENGTH = 15;
+ public static final int CLI_TYPE_LENGTH = 15;
+ public static final int CLI_TYPES_LENGTH = 30;
+ public static final int CLI_SEG_ID_LENGTH = 10;
+ public static final int CLI_LABELS_LENGTH = 30;
+ public static final int CLI_CONTAINERS_LENGTH = 30;
+ public static final int CLI_MARGIN_LENGTH = 2;
}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListNetworkCommand.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListNetworkCommand.java
new file mode 100644
index 0000000..dfaed15
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/cli/KubevirtListNetworkCommand.java
@@ -0,0 +1,84 @@
+/*
+ * 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.cli;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Lists;
+import org.apache.commons.lang.StringUtils;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetworkService;
+
+import java.util.Comparator;
+import java.util.List;
+
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_ID_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_IP_ADDRESS_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_MARGIN_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_NAME_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_SEG_ID_LENGTH;
+import static org.onosproject.kubevirtnetworking.api.Constants.CLI_TYPE_LENGTH;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.genFormatString;
+import static org.onosproject.kubevirtnetworking.util.KubevirtNetworkingUtil.prettyJson;
+
+/**
+ * Lists kubevirt networks.
+ */
+@Service
+@Command(scope = "onos", name = "kubevirt-networks",
+ description = "Lists all kubevirt networks")
+public class KubevirtListNetworkCommand extends AbstractShellCommand {
+ @Override
+ protected void doExecute() throws Exception {
+ KubevirtNetworkService service = get(KubevirtNetworkService.class);
+ List<KubevirtNetwork> networks = Lists.newArrayList(service.networks());
+ networks.sort(Comparator.comparing(KubevirtNetwork::name));
+
+ String format = genFormatString(ImmutableList.of(CLI_ID_LENGTH, CLI_NAME_LENGTH,
+ CLI_TYPE_LENGTH, CLI_SEG_ID_LENGTH, CLI_IP_ADDRESS_LENGTH));
+
+ if (outputJson()) {
+ print("%s", json(networks));
+ } else {
+ print(format, "ID", "Name", "Type", "SegId", "Gateway");
+
+ for (KubevirtNetwork net: networks) {
+ print(format,
+ StringUtils.substring(net.networkId(),
+ 0, CLI_ID_LENGTH - CLI_MARGIN_LENGTH),
+ StringUtils.substring(net.name(),
+ 0, CLI_NAME_LENGTH - CLI_MARGIN_LENGTH),
+ net.type().toString(),
+ net.segmentId() == null ? "N/A" : net.segmentId(),
+ net.gatewayIp() == null ? "" : net.gatewayIp().toString());
+ }
+ }
+ }
+
+ private String json(List<KubevirtNetwork> networks) {
+ ObjectMapper mapper = new ObjectMapper();
+ ArrayNode result = mapper.createArrayNode();
+
+ for (KubevirtNetwork network : networks) {
+ result.add(jsonForEntity(network, KubevirtNetwork.class));
+ }
+ return prettyJson(mapper, result.toString());
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
index 967c23b..e00eb52 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/util/KubevirtNetworkingUtil.java
@@ -15,11 +15,14 @@
*/
package org.onosproject.kubevirtnetworking.util;
+import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import org.onosproject.cfg.ConfigProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.io.IOException;
+import java.util.List;
import java.util.Optional;
import java.util.Set;
@@ -69,4 +72,37 @@
return portName;
}
+
+ /**
+ * Generates string format based on the given string length list.
+ *
+ * @param stringLengths a list of string lengths
+ * @return string format (e.g., %-28s%-15s%-24s%-20s%-15s)
+ */
+ public static String genFormatString(List<Integer> stringLengths) {
+ StringBuilder fsb = new StringBuilder();
+ stringLengths.forEach(length -> {
+ fsb.append("%-");
+ fsb.append(length);
+ fsb.append("s");
+ });
+ return fsb.toString();
+ }
+
+ /**
+ * Prints out the JSON string in pretty format.
+ *
+ * @param mapper Object mapper
+ * @param jsonString JSON string
+ * @return pretty formatted JSON string
+ */
+ public static String prettyJson(ObjectMapper mapper, String jsonString) {
+ try {
+ Object jsonObject = mapper.readValue(jsonString, Object.class);
+ return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
+ } catch (IOException e) {
+ log.debug("Json string parsing exception caused by {}", e);
+ }
+ return null;
+ }
}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkWebResource.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkWebResource.java
new file mode 100644
index 0000000..8ab531f
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkWebResource.java
@@ -0,0 +1,189 @@
+/*
+ * 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.kubevirtnetworking.web;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService;
+import org.onosproject.rest.AbstractWebResource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import static org.onlab.util.Tools.nullIsNotFound;
+import static org.onlab.util.Tools.readTreeFromStream;
+
+/**
+ * Handles REST API call from CNI plugin.
+ */
+@Path("network")
+public class KubevirtNetworkWebResource extends AbstractWebResource {
+
+ protected final Logger log = LoggerFactory.getLogger(getClass());
+
+ private static final String MESSAGE = "Received network %s request";
+ private static final String NETWORK_NOT_FOUND = "Network is not found for";
+ private static final String NETWORK_INVALID = "Invalid networkId in network update request";
+
+ private static final String RESULT = "result";
+
+ private final KubevirtNetworkAdminService adminService =
+ get(KubevirtNetworkAdminService.class);
+
+ /**
+ * Creates a network from the JSON input stream.
+ *
+ * @param input network JSON input stream
+ * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
+ * is invalid or duplicated network already exists
+ * @onos.rsModel KubevirtNetwork
+ */
+ @POST
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response createNetwork(InputStream input) {
+ log.trace(String.format(MESSAGE, "CREATE"));
+ URI location;
+
+ try {
+ ObjectNode jsonTree = readTreeFromStream(mapper(), input);
+ final KubevirtNetwork network =
+ codec(KubevirtNetwork.class).decode(jsonTree, this);
+ adminService.createNetwork(network);
+ location = new URI(network.networkId());
+ } catch (IOException | URISyntaxException e) {
+ throw new IllegalArgumentException(e);
+ }
+
+ return Response.created(location).build();
+ }
+
+ /**
+ * Updates the network with the specified identifier.
+ *
+ * @param id network identifier
+ * @param input network JSON input stream
+ * @return 200 OK with the updated network, 400 BAD_REQUEST if the requested
+ * network does not exist
+ * @onos.rsModel KubevirtNetwork
+ */
+ @PUT
+ @Path("{id}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response updateNetwork(@PathParam("id") String id, InputStream input) {
+ log.trace(String.format(MESSAGE, "UPDATED"));
+
+ try {
+ ObjectNode jsonTree = readTreeFromStream(mapper(), input);
+ JsonNode specifiedNetworkId = jsonTree.get("networkId");
+
+ if (specifiedNetworkId != null && !specifiedNetworkId.asText().equals(id)) {
+ throw new IllegalArgumentException(NETWORK_INVALID);
+ }
+
+ final KubevirtNetwork network =
+ codec(KubevirtNetwork.class).decode(jsonTree, this);
+ adminService.updateNetwork(network);
+ } catch (IOException e) {
+ throw new IllegalArgumentException(e);
+ }
+
+ return Response.ok().build();
+ }
+
+ /**
+ * Removes the network with the given id.
+ *
+ * @param id network identifier
+ * @return 204 NO_CONTENT, 400 BAD_REQUEST if the network does not exist
+ */
+ @DELETE
+ @Path("{id}")
+ public Response removeNetwork(@PathParam("id") String id) {
+ log.trace(String.format(MESSAGE, "DELETE " + id));
+
+ adminService.removeNetwork(id);
+ return Response.noContent().build();
+ }
+
+ /**
+ * Checks whether the network exists with given network id.
+ *
+ * @param id network identifier
+ * @return 200 OK with true/false result
+ */
+ @GET
+ @Path("exist/{id}")
+ public Response hasNetwork(@PathParam("id") String id) {
+ log.trace(String.format(MESSAGE, "QUERY " + id));
+
+ ObjectNode root = mapper().createObjectNode();
+ KubevirtNetwork network = adminService.network(id);
+
+ if (network == null) {
+ root.put(RESULT, false);
+ } else {
+ root.put(RESULT, true);
+ }
+
+ return Response.ok(root).build();
+ }
+
+ /**
+ * Returns set of all networks.
+ *
+ * @return 200 OK with set of all networks
+ * @onos.rsModel KubevirtNetworks
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response getNetworks() {
+ final Iterable<KubevirtNetwork> networks = adminService.networks();
+ return ok(encodeArray(KubevirtNetwork.class, "networks", networks)).build();
+ }
+
+ /**
+ * Returns the network with the specified identifier.
+ *
+ * @param id network identifier
+ * @return 200 OK with a network, 404 not found
+ * @onos.rsModel KubevirtNetwork
+ */
+ @GET
+ @Produces(MediaType.APPLICATION_JSON)
+ @Path("{id}")
+ public Response getNetworkById(@PathParam("id") String id) {
+ final KubevirtNetwork network = nullIsNotFound(adminService.network(id),
+ NETWORK_NOT_FOUND + id);
+ return ok(codec(KubevirtNetwork.class).encode(network, this)).build();
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegister.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegister.java
new file mode 100644
index 0000000..a9d2d20
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegister.java
@@ -0,0 +1,64 @@
+/*
+ * 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.web;
+
+import org.onosproject.codec.CodecService;
+import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute;
+import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
+import org.onosproject.kubevirtnetworking.codec.KubevirtHostRouteCodec;
+import org.onosproject.kubevirtnetworking.codec.KubevirtIpPoolCodec;
+import org.onosproject.kubevirtnetworking.codec.KubevirtNetworkCodec;
+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 KubevirtNetworking.
+ */
+@Component(immediate = true)
+public class KubevirtNetworkingCodecRegister {
+
+ private final Logger log = getLogger(getClass());
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY)
+ protected CodecService codecService;
+
+ @Activate
+ protected void activate() {
+
+ codecService.registerCodec(KubevirtHostRoute.class, new KubevirtHostRouteCodec());
+ codecService.registerCodec(KubevirtIpPool.class, new KubevirtIpPoolCodec());
+ codecService.registerCodec(KubevirtNetwork.class, new KubevirtNetworkCodec());
+
+ log.info("Started");
+ }
+
+ @Deactivate
+ protected void deactivate() {
+
+ codecService.unregisterCodec(KubevirtHostRoute.class);
+ codecService.unregisterCodec(KubevirtIpPool.class);
+ codecService.unregisterCodec(KubevirtNetwork.class);
+
+ log.info("Stopped");
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingWebApplication.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingWebApplication.java
index 5ae1453..71f3eb4 100644
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingWebApplication.java
+++ b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingWebApplication.java
@@ -25,6 +25,6 @@
public class KubevirtNetworkingWebApplication extends AbstractWebApplication {
@Override
public Set<Class<?>> getClasses() {
- return getClasses(KubevirtNetworkingWebResource.class);
+ return getClasses(KubevirtNetworkWebResource.class);
}
}
diff --git a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingWebResource.java b/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingWebResource.java
deleted file mode 100644
index 1f98048..0000000
--- a/apps/kubevirt-networking/app/src/main/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingWebResource.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * 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.kubevirtnetworking.web;
-
-import org.onosproject.rest.AbstractWebResource;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-import java.io.InputStream;
-
-@Path("network")
-public class KubevirtNetworkingWebResource extends AbstractWebResource {
-
- protected final Logger log = LoggerFactory.getLogger(getClass());
-
- /**
- * Creates a network from the JSON input stream.
- *
- * @param input network JSON input stream
- * @return 201 CREATED if the JSON is correct, 400 BAD_REQUEST if the JSON
- * is invalid or duplicated network already exists
- * @onos.rsModel KubevirtNetwork
- */
- @POST
- @Consumes(MediaType.APPLICATION_JSON)
- @Produces(MediaType.APPLICATION_JSON)
- public Response dummy(InputStream input) {
- return Response.ok().build();
- }
-}
diff --git a/apps/kubevirt-networking/app/src/main/resources/definitions/KubevirtNetworks.json b/apps/kubevirt-networking/app/src/main/resources/definitions/KubevirtNetworks.json
new file mode 100644
index 0000000..f838b60
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/main/resources/definitions/KubevirtNetworks.json
@@ -0,0 +1,66 @@
+{
+ "type": "object",
+ "title": "regions",
+ "required": [
+ "regions"
+ ],
+ "properties": {
+ "regions": {
+ "type": "array",
+ "xml": {
+ "name": "regions",
+ "wrapped": true
+ },
+ "items": {
+ "type": "object",
+ "description": "A network object.",
+ "required": [
+ "networkId",
+ "type",
+ "name",
+ "segmentId",
+ "gatewayIp",
+ "cidr",
+ "mtu"
+ ],
+ "properties": {
+ "networkId": {
+ "type": "string",
+ "example": "sona-network",
+ "description": "The ID of the attached network."
+ },
+ "type": {
+ "type": "string",
+ "example": "VXLAN",
+ "description": "Type of kubernetes network."
+ },
+ "name": {
+ "type": "string",
+ "example": "sona-network",
+ "description": "The name of network."
+ },
+ "segmentId": {
+ "type": "string",
+ "example": "1",
+ "description": "Segment ID of tenant network."
+ },
+ "gatewayIp": {
+ "type": "string",
+ "example": "10.10.10.1",
+ "description": "The IP address of the gateway."
+ },
+ "cidr": {
+ "type": "string",
+ "example": "10.10.10.0/24",
+ "description": "The CIDR of this network."
+ },
+ "mtu": {
+ "type": "integer",
+ "example": 1500,
+ "description": "The Maximum Transmission Unit of this network."
+ }
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkWebResourceTest.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkWebResourceTest.java
new file mode 100644
index 0000000..8a06018
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkWebResourceTest.java
@@ -0,0 +1,197 @@
+/*
+ * 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.web;
+
+import org.glassfish.jersey.server.ResourceConfig;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.osgi.ServiceDirectory;
+import org.onlab.osgi.TestServiceDirectory;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecService;
+import org.onosproject.codec.impl.CodecManager;
+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.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService;
+import org.onosproject.kubevirtnetworking.codec.KubevirtHostRouteCodec;
+import org.onosproject.kubevirtnetworking.codec.KubevirtIpPoolCodec;
+import org.onosproject.kubevirtnetworking.codec.KubevirtNetworkCodec;
+import org.onosproject.rest.resources.ResourceTest;
+
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.InputStream;
+
+import static org.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.anyString;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+/**
+ * Unit tests for kubevirt network REST API.
+ */
+public class KubevirtNetworkWebResourceTest extends ResourceTest {
+
+ final KubevirtNetworkAdminService mockAdminService = createMock(KubevirtNetworkAdminService.class);
+ private static final String PATH = "network";
+
+ private KubevirtNetwork network;
+
+ /**
+ * Constructs a kubernetes networking resource test instance.
+ */
+ public KubevirtNetworkWebResourceTest() {
+ super(ResourceConfig.forApplicationClass(KubevirtNetworkingWebApplication.class));
+ }
+
+ /**
+ * Sets up the global values for all the tests.
+ */
+ @Before
+ public void setUpTest() {
+ final CodecManager codecService = new CodecManager();
+ codecService.activate();
+ codecService.registerCodec(KubevirtHostRoute.class, new KubevirtHostRouteCodec());
+ codecService.registerCodec(KubevirtIpPool.class, new KubevirtIpPoolCodec());
+ codecService.registerCodec(KubevirtNetwork.class, new KubevirtNetworkCodec());
+ ServiceDirectory testDirectory =
+ new TestServiceDirectory()
+ .add(KubevirtNetworkAdminService.class, mockAdminService)
+ .add(CodecService.class, codecService);
+ setServiceDirectory(testDirectory);
+
+ network = DefaultKubevirtNetwork.builder()
+ .networkId("network")
+ .name("network")
+ .type(KubevirtNetwork.Type.FLAT)
+ .cidr("10.10.10.0/24")
+ .mtu(1500)
+ .gatewayIp(IpAddress.valueOf("10.10.10.1"))
+ .ipPool(new KubevirtIpPool(IpAddress.valueOf("10.10.10.100"),
+ IpAddress.valueOf("10.10.10.200")))
+ .build();
+ }
+
+ /**
+ * Tests the results of the REST API POST method with creating new network operation.
+ */
+ @Test
+ public void testCreateNetworkWithCreateOperation() {
+ mockAdminService.createNetwork(anyObject());
+ replay(mockAdminService);
+
+ final WebTarget wt = target();
+ InputStream jsonStream = KubevirtNetworkWebResourceTest.class
+ .getResourceAsStream("kubevirt-network.json");
+ Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE)
+ .post(Entity.json(jsonStream));
+ final int status = response.getStatus();
+
+ assertThat(status, is(201));
+
+ verify(mockAdminService);
+ }
+
+ /**
+ * Tests the results of the REST API PUT method with modifying the network.
+ */
+ @Test
+ public void testUpdateNetworkWithModifyOperation() {
+ mockAdminService.updateNetwork(anyObject());
+ replay(mockAdminService);
+
+ String location = PATH + "/network";
+
+ final WebTarget wt = target();
+ InputStream jsonStream = KubevirtNetworkWebResourceTest.class
+ .getResourceAsStream("kubevirt-network.json");
+ Response response = wt.path(location)
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .put(Entity.json(jsonStream));
+ final int status = response.getStatus();
+
+ assertThat(status, is(200));
+
+ verify(mockAdminService);
+ }
+
+ /**
+ * Tests the results of the REST API DELETE method with deleting the network.
+ */
+ @Test
+ public void testDeleteNetworkWithDeletionOperation() {
+ mockAdminService.removeNetwork(anyString());
+ replay(mockAdminService);
+
+ String location = PATH + "/network";
+
+ final WebTarget wt = target();
+ Response response = wt.path(location).request(
+ MediaType.APPLICATION_JSON_TYPE).delete();
+
+ final int status = response.getStatus();
+
+ assertThat(status, is(204));
+
+ verify(mockAdminService);
+ }
+
+ /**
+ * Tests the results of checking network existence.
+ */
+ @Test
+ public void testHasNetworkWithValidNetwork() {
+ expect(mockAdminService.network(anyString())).andReturn(network);
+ replay(mockAdminService);
+
+ String location = PATH + "/exist/network";
+
+ final WebTarget wt = target();
+ String response = wt.path(location).request(
+ MediaType.APPLICATION_JSON_TYPE).get(String.class);
+
+ assertThat(response, is("{\"result\":true}"));
+
+ verify((mockAdminService));
+ }
+
+ /**
+ * Tests the results of checking network existence.
+ */
+ @Test
+ public void testHasNetworkWithNullNetwork() {
+ expect(mockAdminService.network(anyString())).andReturn(null);
+ replay(mockAdminService);
+
+ String location = PATH + "/exist/network";
+
+ final WebTarget wt = target();
+ String response = wt.path(location).request(
+ MediaType.APPLICATION_JSON_TYPE).get(String.class);
+
+ assertThat(response, is("{\"result\":false}"));
+
+ verify((mockAdminService));
+ }
+}
diff --git a/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegisterTest.java b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegisterTest.java
new file mode 100644
index 0000000..c3784ba
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/java/org/onosproject/kubevirtnetworking/web/KubevirtNetworkingCodecRegisterTest.java
@@ -0,0 +1,86 @@
+/*
+ * 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.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.kubevirtnetworking.api.KubevirtHostRoute;
+import org.onosproject.kubevirtnetworking.api.KubevirtIpPool;
+import org.onosproject.kubevirtnetworking.api.KubevirtNetwork;
+import org.onosproject.kubevirtnetworking.codec.KubevirtHostRouteCodec;
+import org.onosproject.kubevirtnetworking.codec.KubevirtIpPoolCodec;
+import org.onosproject.kubevirtnetworking.codec.KubevirtNetworkCodec;
+
+import java.util.Map;
+import java.util.Set;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for kubevirt networking codec register.
+ */
+public class KubevirtNetworkingCodecRegisterTest {
+
+ /**
+ * Tests codec register activation and deactivation.
+ */
+ @Test
+ public void testActivateDeactivate() {
+ KubevirtNetworkingCodecRegister register = new KubevirtNetworkingCodecRegister();
+ CodecService codecService = new TestCodecService();
+
+ TestUtils.setField(register, "codecService", codecService);
+ register.activate();
+
+ assertEquals(KubevirtNetworkCodec.class.getName(),
+ codecService.getCodec(KubevirtNetwork.class).getClass().getName());
+ assertEquals(KubevirtHostRouteCodec.class.getName(),
+ codecService.getCodec(KubevirtHostRoute.class).getClass().getName());
+ assertEquals(KubevirtIpPoolCodec.class.getName(),
+ codecService.getCodec(KubevirtIpPool.class).getClass().getName());
+
+ register.deactivate();
+ }
+
+ 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-networking/app/src/test/resources/org/onosproject/kubevirtnetworking/web/kubevirt-network.json b/apps/kubevirt-networking/app/src/test/resources/org/onosproject/kubevirtnetworking/web/kubevirt-network.json
new file mode 100644
index 0000000..d8a0a1f
--- /dev/null
+++ b/apps/kubevirt-networking/app/src/test/resources/org/onosproject/kubevirtnetworking/web/kubevirt-network.json
@@ -0,0 +1,12 @@
+{
+ "networkId": "network",
+ "name": "network",
+ "type": "FLAT",
+ "mtu": 1500,
+ "cidr": "10.10.10.0/24",
+ "gatewayIp": "10.10.10.1",
+ "ipPool": {
+ "start": "10.10.10.100",
+ "end": "10.10.10.200"
+ }
+}
\ No newline at end of file