Support to check the existence of k8s network through REST API
Change-Id: I2da687f1abcc54a33b44b3e48fd2dd5487bcbe29
diff --git a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkWebResource.java b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkWebResource.java
index f582e4b..b6bf830 100644
--- a/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkWebResource.java
+++ b/apps/k8s-networking/app/src/main/java/org/onosproject/k8snetworking/web/K8sNetworkWebResource.java
@@ -25,6 +25,7 @@
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;
@@ -50,6 +51,8 @@
private static final String MESSAGE = "Received network %s request";
private static final String NETWORK_INVALID = "Invalid networkId in network update request";
+ private static final String RESULT = "result";
+
private final K8sNetworkAdminService adminService = get(K8sNetworkAdminService.class);
/**
@@ -126,4 +129,27 @@
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();
+ K8sNetwork network = adminService.network(id);
+
+ if (network == null) {
+ root.put(RESULT, false);
+ } else {
+ root.put(RESULT, true);
+ }
+
+ return Response.ok(root).build();
+ }
}
diff --git a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java
index 40299b2..9a4ca67 100644
--- a/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java
+++ b/apps/k8s-networking/app/src/test/java/org/onosproject/k8snetworking/web/K8sNetworkWebResourceTest.java
@@ -20,8 +20,10 @@
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.k8snetworking.api.DefaultK8sNetwork;
import org.onosproject.k8snetworking.api.K8sNetwork;
import org.onosproject.k8snetworking.api.K8sNetworkAdminService;
import org.onosproject.k8snetworking.codec.K8sNetworkCodec;
@@ -36,6 +38,7 @@
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;
@@ -71,6 +74,16 @@
.add(K8sNetworkAdminService.class, mockAdminService)
.add(CodecService.class, codecService);
setServiceDirectory(testDirectory);
+
+ k8sNetwork = DefaultK8sNetwork.builder()
+ .networkId("sona-network")
+ .name("sona-network")
+ .type(K8sNetwork.Type.VXLAN)
+ .segmentId("1")
+ .cidr("10.10.10.0/24")
+ .gatewayIp(IpAddress.valueOf("10.10.10.1"))
+ .mtu(1500)
+ .build();
}
/**
@@ -136,4 +149,42 @@
verify(mockAdminService);
}
+
+ /**
+ * Tests the results of checking network existence.
+ */
+ @Test
+ public void testHasNetworkWithValidNetwork() {
+ expect(mockAdminService.network(anyString())).andReturn(k8sNetwork);
+ replay(mockAdminService);
+
+ String location = PATH + "/exist/network-1";
+
+ 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-1";
+
+ final WebTarget wt = target();
+ String response = wt.path(location).request(
+ MediaType.APPLICATION_JSON_TYPE).get(String.class);
+
+ assertThat(response, is("{\"result\":false}"));
+
+ verify((mockAdminService));
+ }
}