1 package org.opendaylight.controller.ping.northbound;
3 import javax.ws.rs.PUT;
4 import javax.ws.rs.Path;
5 import javax.ws.rs.PathParam;
6 import javax.ws.rs.core.Response;
8 import org.codehaus.enunciate.jaxrs.ResponseCode;
9 import org.codehaus.enunciate.jaxrs.StatusCodes;
10 import org.opendaylight.controller.ping.service.api.PingServiceAPI;
11 import org.opendaylight.controller.sal.utils.ServiceHelper;
14 public class PingNorthbound {
18 @Path("/ping/{ipAddress}")
21 @ResponseCode(code = 200, condition = "Destination reachable"),
22 // @ResponseCode(code = 206, condition = "Ping in progress"),
23 @ResponseCode(code = 503, condition = "Internal error"),
24 @ResponseCode(code = 503, condition = "Destination unreachable") })
25 public Response ping(@PathParam(value = "ipAddress") String ipAddress) {
26 return pingCommon(ipAddress, true);
29 @Path("/ping/async/start/{ipAddress}")
32 @ResponseCode(code = 200, condition = "Destination reachable"),
33 @ResponseCode(code = 206, condition = "Ping in progress"),
34 @ResponseCode(code = 503, condition = "Internal error"),
35 @ResponseCode(code = 503, condition = "Destination unreachable") })
36 public Response pingAsyncStart(@PathParam(value = "ipAddress") String ipAddress) {
37 return pingCommon(ipAddress, false);
40 @Path("/ping/async/get/{ipAddress}")
43 @ResponseCode(code = 200, condition = "Destination reachable"),
44 @ResponseCode(code = 206, condition = "Ping in progress"),
45 @ResponseCode(code = 503, condition = "Internal error"),
46 @ResponseCode(code = 503, condition = "Destination unreachable") })
47 public Response pingAsyncGet(@PathParam(value = "ipAddress") String ipAddress) {
48 return pingCommon(ipAddress, false);
51 @Path("/ping/async/stop/{ipAddress}")
54 @ResponseCode(code = 200, condition = "Ping stopped"),
55 @ResponseCode(code = 503, condition = "Internal error")})
56 public Response pingAsyncStop(@PathParam(value = "ipAddress") String ipAddress) {
57 PingServiceAPI pingServiceAPI = (PingServiceAPI) ServiceHelper.getGlobalInstance(PingServiceAPI.class, this);
58 if (pingServiceAPI != null) { pingServiceAPI.pingAsyncStop(ipAddress); }
59 return Response.ok(new String(ipAddress + " - stopped")).build(); // idem-potent
62 private Response pingCommon(String ipAddress, boolean isSync) {
63 PingServiceAPI pingServiceAPI = (PingServiceAPI) ServiceHelper.getGlobalInstance(PingServiceAPI.class, this);
64 if (pingServiceAPI == null) {
65 /* Ping service not found. */
66 return Response.ok(new String("No ping service")).status(500).build();
68 PingServiceAPI.PingResult pingResult = isSync ?
69 pingServiceAPI.pingDestinationSync(ipAddress) : pingServiceAPI.pingDestinationAsync(ipAddress);
70 if (pingResult == PingServiceAPI.PingResult.InProgress)
71 return Response.ok(new String(ipAddress + " - " + pingResult)).status(206).build();
72 if (pingResult == PingServiceAPI.PingResult.GotResponse)
73 return Response.ok(new String(ipAddress + " - " + pingResult)).build();
74 return Response.ok(new String(ipAddress + " - " + pingResult)).status(503).build();