2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.healthcheck;
23 import java.util.ArrayList;
24 import java.util.Properties;
26 import javax.ws.rs.core.Response;
28 import org.onap.policy.drools.http.client.HttpClient;
29 import org.onap.policy.drools.http.server.HttpServletServer;
30 import org.onap.policy.drools.persistence.SystemPersistence;
31 import org.onap.policy.drools.properties.Startable;
32 import org.onap.policy.drools.system.PolicyEngine;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
39 public interface HealthCheck extends Startable {
44 public static class Report {
46 * Named Entity in the report
58 public boolean healthy;
66 * Message from remote entity
68 public String message;
71 public String toString() {
72 StringBuilder builder = new StringBuilder();
73 builder.append("Report [name=");
75 builder.append(", url=");
77 builder.append(", healthy=");
78 builder.append(healthy);
79 builder.append(", code=");
81 builder.append(", message=");
82 builder.append(message);
84 return builder.toString();
91 public static class Reports {
92 public boolean healthy;
93 public ArrayList<Report> details = new ArrayList<>();
96 public String toString() {
97 StringBuilder builder = new StringBuilder();
98 builder.append("Reports [healthy=");
99 builder.append(healthy);
100 builder.append(", details=");
101 builder.append(details);
103 return builder.toString();
108 * perform a healthcheck
111 public Reports healthCheck();
114 * Healthcheck Monitor
116 public static final HealthCheck monitor = new HealthCheckMonitor();
120 * Healthcheck Monitor
122 class HealthCheckMonitor implements HealthCheck {
127 private static Logger logger = LoggerFactory.getLogger(HealthCheckMonitor.class);
130 * attached http servers
132 protected volatile ArrayList<HttpServletServer> servers = new ArrayList<>();
135 * attached http clients
137 protected volatile ArrayList<HttpClient> clients = new ArrayList<>();
140 * healthcheck configuration
142 protected volatile Properties healthCheckProperties = null;
147 public Reports healthCheck() {
148 Reports reports = new Reports();
149 reports.healthy = PolicyEngine.manager.isAlive();
151 HealthCheck.Report engineReport = new Report();
152 engineReport.healthy = PolicyEngine.manager.isAlive();
153 engineReport.name = "PDP-D";
154 engineReport.url = "self";
155 engineReport.code = (PolicyEngine.manager.isAlive()) ? 200 : 500;
156 engineReport.message = (PolicyEngine.manager.isAlive()) ? "alive" : "not alive";
157 reports.details.add(engineReport);
159 for (HttpClient client : clients) {
160 HealthCheck.Report report = new Report();
161 report.name = client.getName();
162 report.url = client.getBaseUrl();
163 report.healthy = true;
165 Response response = client.get();
166 report.code = response.getStatus();
167 if (report.code != 200) {
168 report.healthy = false;
169 reports.healthy = false;
173 report.message = HttpClient.getBody(response, String.class);
174 } catch (Exception e) {
175 logger.warn("{}: cannot get body from http-client {}", this, client, e);
177 } catch (Exception e) {
178 report.healthy = false;
179 reports.healthy = false;
181 reports.details.add(report);
190 public boolean start() throws IllegalStateException {
193 this.healthCheckProperties = SystemPersistence.manager.getProperties(HealthCheckFeature.CONFIGURATION_PROPERTIES_NAME);
194 this.servers = HttpServletServer.factory.build(healthCheckProperties);
195 this.clients = HttpClient.factory.build(healthCheckProperties);
197 for (HttpServletServer server : servers) {
200 } catch (Exception e) {
201 logger.warn("{}: cannot start http-server {}", this, server, e);
204 } catch (Exception e) {
215 public boolean stop() throws IllegalStateException {
217 for (HttpServletServer server : servers) {
220 } catch (Exception e) {
221 logger.warn("{}: cannot stop http-server {}", this, server, e);
225 for (HttpClient client : clients) {
228 } catch (Exception e) {
229 logger.warn("{}: cannot stop http-client {}", this, client, e);
240 public void shutdown() throws IllegalStateException {
248 public synchronized boolean isAlive() {
249 return this.healthCheckProperties != null;
253 * @return list of attached Http Servers
255 public ArrayList<HttpServletServer> getServers() {
260 * @return list of attached Http Clients
262 public ArrayList<HttpClient> getClients() {
267 public String toString() {
268 StringBuilder builder = new StringBuilder();
269 builder.append("HealthCheckMonitor [servers=");
270 builder.append(servers);
271 builder.append(", clients=");
272 builder.append(clients);
274 return builder.toString();