Removing Drools-pdp swagger annotations
[policy/drools-pdp.git] / feature-healthcheck / src / main / java / org / onap / policy / drools / healthcheck / RestHealthCheck.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2017-2019, 2022 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.healthcheck;
23
24 import javax.ws.rs.GET;
25 import javax.ws.rs.Path;
26 import javax.ws.rs.PathParam;
27 import javax.ws.rs.Produces;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
31 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
32 import org.onap.policy.common.endpoints.http.server.YamlMessageBodyHandler;
33 import org.onap.policy.drools.healthcheck.HealthCheck.Reports;
34 import org.onap.policy.drools.system.PolicyControllerConstants;
35 import org.onap.policy.drools.system.PolicyControllerFactory;
36
37 /**
38  * REST Healthcheck JAX-RS.
39  */
40 @Path("/")
41 @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
42 public class RestHealthCheck implements HealthcheckApi {
43
44     /**
45      * System healthcheck per configuration.
46      */
47
48     @Override
49     @GET
50     @Path("healthcheck")
51     public Response healthcheck() {
52         var summary = getHealthcheckManager().healthCheck();
53         return getResponse(summary);
54     }
55
56     /**
57      * Engine Healthcheck.
58      */
59
60     @Override
61     @GET
62     @Path("healthcheck/engine")
63     public Response engine() {
64         var summary = getHealthcheckManager().engineHealthcheck();
65         return getResponse(summary);
66     }
67
68     /**
69      * Healthcheck on the controllers.
70      */
71
72     @Override
73     @GET
74     @Path("healthcheck/controllers")
75     public Response controllers() {
76         var summary = getHealthcheckManager().controllerHealthcheck();
77         return getResponse(summary);
78     }
79
80     /**
81      * Healthcheck a controller.
82      */
83
84     @Override
85     @GET
86     @Path("healthcheck/controllers/{controllerName}")
87     public Response controllersName(@PathParam("controllerName") String controllerName) {
88         try {
89             var controller = getControllerFactory().get(controllerName);
90             var summary = getHealthcheckManager().controllerHealthcheck(controller);
91             return getResponse(summary);
92         } catch (final IllegalArgumentException e) {
93             return Response.status(Response.Status.NOT_FOUND).build();
94         } catch (final IllegalStateException e) {
95             return Response.status(Response.Status.NOT_ACCEPTABLE).build();
96         }
97     }
98
99     /**
100      * Healthcheck on the Http Clients per configuration.
101      */
102
103     @Override
104     @GET
105     @Path("healthcheck/clients")
106     public Response clients() {
107         var summary = getHealthcheckManager().clientHealthcheck();
108         return getResponse(summary);
109     }
110
111     /**
112      * Healthcheck a on a Http Client.
113      */
114
115     @Override
116     @GET
117     @Path("healthcheck/clients/{clientName}")
118     public Response clientsName(@PathParam("clientName") String clientName) {
119         try {
120             var client = getClientFactory().get(clientName);
121             var summary = getHealthcheckManager().clientHealthcheck(client);
122             return getResponse(summary);
123         } catch (final IllegalArgumentException e) {
124             return Response.status(Response.Status.NOT_FOUND).build();
125         }
126     }
127
128     protected Response getResponse(Reports summary) {
129         return Response.status(summary.isHealthy() ? Response.Status.OK : Response.Status.SERVICE_UNAVAILABLE)
130                 .entity(summary).build();
131     }
132
133     protected HttpClientFactory getClientFactory() {
134         return HttpClientFactoryInstance.getClientFactory();
135     }
136
137     protected PolicyControllerFactory getControllerFactory() {
138         return PolicyControllerConstants.getFactory();
139     }
140
141     protected HealthCheck getHealthcheckManager() {
142         return HealthCheckConstants.getManager();
143     }
144
145 }