[DO NOT MERGE]
[controller.git] / opendaylight / ping / northbound / src / main / java / org / opendaylight / controller / ping / northbound / PingNorthbound.java
1 package org.opendaylight.controller.ping.northbound;
2
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;
7
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;
12
13 @Path("/")
14 public class PingNorthbound {
15     /**
16      * Ping test
17      */
18     @Path("/ping/{ipAddress}")
19     @PUT
20     @StatusCodes({
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);
27     }
28
29     @Path("/ping/async/start/{ipAddress}")
30     @PUT
31     @StatusCodes({
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);
38     }
39
40     @Path("/ping/async/get/{ipAddress}")
41     @PUT
42     @StatusCodes({
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);
49     }
50
51     @Path("/ping/async/stop/{ipAddress}")
52     @PUT
53     @StatusCodes({
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
60     }
61
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();
67         }
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();
75     }
76 }