2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.drools.healthcheck;
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;
38 * REST Healthcheck JAX-RS.
41 @Produces({MediaType.APPLICATION_JSON, YamlMessageBodyHandler.APPLICATION_YAML})
42 public class RestHealthCheck implements HealthcheckApi {
45 * System healthcheck per configuration.
51 public Response healthcheck() {
52 var summary = getHealthcheckManager().healthCheck();
53 return getResponse(summary);
62 @Path("healthcheck/engine")
63 public Response engine() {
64 var summary = getHealthcheckManager().engineHealthcheck();
65 return getResponse(summary);
69 * Healthcheck on the controllers.
74 @Path("healthcheck/controllers")
75 public Response controllers() {
76 var summary = getHealthcheckManager().controllerHealthcheck();
77 return getResponse(summary);
81 * Healthcheck a controller.
86 @Path("healthcheck/controllers/{controllerName}")
87 public Response controllersName(@PathParam("controllerName") String controllerName) {
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();
100 * Healthcheck on the Http Clients per configuration.
105 @Path("healthcheck/clients")
106 public Response clients() {
107 var summary = getHealthcheckManager().clientHealthcheck();
108 return getResponse(summary);
112 * Healthcheck a on a Http Client.
117 @Path("healthcheck/clients/{clientName}")
118 public Response clientsName(@PathParam("clientName") String clientName) {
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();
128 protected Response getResponse(Reports summary) {
129 return Response.status(summary.isHealthy() ? Response.Status.OK : Response.Status.SERVICE_UNAVAILABLE)
130 .entity(summary).build();
133 protected HttpClientFactory getClientFactory() {
134 return HttpClientFactoryInstance.getClientFactory();
137 protected PolicyControllerFactory getControllerFactory() {
138 return PolicyControllerConstants.getFactory();
141 protected HealthCheck getHealthcheckManager() {
142 return HealthCheckConstants.getManager();