Add REST API and CLI CURD support for kubevirt network
Change-Id: I5e01036c42157013ca3fb4665c67c8d47cf22c7b
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();
- }
-}