[DO NOT MERGE]
[controller.git] / opendaylight / ping / northbound / src / main / java / org / opendaylight / controller / ping / northbound / PingNorthbound.java
index e1e21349bec7e7b7c989031d4ab3b6e774a44be6..62f794d5c87d640b29032b7cf2df2c0d8e510062 100644 (file)
@@ -18,22 +18,59 @@ public class PingNorthbound {
     @Path("/ping/{ipAddress}")
     @PUT
     @StatusCodes({
-        @ResponseCode(code = 200, condition = "Destination reachable"),
-        @ResponseCode(code = 503, condition = "Internal error"),
-        @ResponseCode(code = 503, condition = "Destination unreachable") })
+            @ResponseCode(code = 200, condition = "Destination reachable"),
+            // @ResponseCode(code = 206, condition = "Ping in progress"),
+            @ResponseCode(code = 503, condition = "Internal error"),
+            @ResponseCode(code = 503, condition = "Destination unreachable") })
     public Response ping(@PathParam(value = "ipAddress") String ipAddress) {
-        PingServiceAPI ping = (PingServiceAPI) ServiceHelper.getGlobalInstance(
-                PingServiceAPI.class, this);
-        if (ping == null) {
+        return pingCommon(ipAddress, true);
+    }
+
+    @Path("/ping/async/start/{ipAddress}")
+    @PUT
+    @StatusCodes({
+            @ResponseCode(code = 200, condition = "Destination reachable"),
+            @ResponseCode(code = 206, condition = "Ping in progress"),
+            @ResponseCode(code = 503, condition = "Internal error"),
+            @ResponseCode(code = 503, condition = "Destination unreachable") })
+    public Response pingAsyncStart(@PathParam(value = "ipAddress") String ipAddress) {
+        return pingCommon(ipAddress, false);
+    }
+
+    @Path("/ping/async/get/{ipAddress}")
+    @PUT
+    @StatusCodes({
+            @ResponseCode(code = 200, condition = "Destination reachable"),
+            @ResponseCode(code = 206, condition = "Ping in progress"),
+            @ResponseCode(code = 503, condition = "Internal error"),
+            @ResponseCode(code = 503, condition = "Destination unreachable") })
+    public Response pingAsyncGet(@PathParam(value = "ipAddress") String ipAddress) {
+        return pingCommon(ipAddress, false);
+    }
 
+    @Path("/ping/async/clear/{ipAddress}")
+    @PUT
+    @StatusCodes({
+            @ResponseCode(code = 200, condition = "Async ping removed"),
+            @ResponseCode(code = 503, condition = "Internal error")})
+    public Response pingAsyncClear(@PathParam(value = "ipAddress") String ipAddress) {
+        PingServiceAPI pingServiceAPI = (PingServiceAPI) ServiceHelper.getGlobalInstance(PingServiceAPI.class, this);
+        if (pingServiceAPI != null) { pingServiceAPI.pingAsyncClear(ipAddress); }
+        return Response.ok(new String(ipAddress + " - removed")).build();  // idem-potent
+    }
+
+    private Response pingCommon(String ipAddress, boolean isSync) {
+        PingServiceAPI pingServiceAPI = (PingServiceAPI) ServiceHelper.getGlobalInstance(PingServiceAPI.class, this);
+        if (pingServiceAPI == null) {
             /* Ping service not found. */
-            return Response.ok(new String("No ping service")).status(500)
-                    .build();
+            return Response.ok(new String("No ping service")).status(500).build();
         }
-        if (ping.pingDestination(ipAddress))
-            return Response.ok(new String(ipAddress + " - reachable")).build();
-
-        return Response.ok(new String(ipAddress + " - unreachable")).status(503)
-                .build();
+        PingServiceAPI.PingResult pingResult = isSync ?
+                pingServiceAPI.pingDestinationSync(ipAddress) : pingServiceAPI.pingDestinationAsync(ipAddress);
+        if (pingResult == PingServiceAPI.PingResult.InProgress)
+            return Response.ok(new String(ipAddress + " - " + pingResult)).status(206).build();
+        if (pingResult == PingServiceAPI.PingResult.GotResponse)
+            return Response.ok(new String(ipAddress + " - " + pingResult)).build();
+        return Response.ok(new String(ipAddress + " - " + pingResult)).status(503).build();
     }
 }