diff options
Diffstat (limited to 'feature-healthcheck/src')
2 files changed, 350 insertions, 357 deletions
diff --git a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java index fb3da657..9c543412 100644 --- a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java +++ b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java @@ -26,10 +26,12 @@ import java.util.Properties; import javax.ws.rs.core.Response; -import org.onap.policy.drools.http.client.HttpClient; -import org.onap.policy.drools.http.server.HttpServletServer; +import org.onap.policy.common.capabilities.Startable; +import org.onap.policy.common.endpoints.http.client.HttpClient; +import org.onap.policy.common.endpoints.http.client.impl.IndexedHttpClientFactory; +import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.common.endpoints.http.server.impl.IndexedHttpServletServerFactory; import org.onap.policy.drools.persistence.SystemPersistence; -import org.onap.policy.drools.properties.Startable; import org.onap.policy.drools.system.PolicyEngine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,56 +41,56 @@ import org.slf4j.LoggerFactory; */ public interface HealthCheck extends Startable { - /** - * Healthcheck Monitor - */ - public static final HealthCheck monitor = new HealthCheckMonitor(); - - /** - * Healthcheck Report - */ - public static class Report { - /** - * Named Entity in the report - */ - private String name; - - /** - * URL queried - */ - private String url; - - /** - * healthy? - */ - private boolean healthy; - - /** - * return code - */ - private int code; - - /** - * Message from remote entity - */ - private String message; - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Report [name="); - builder.append(getName()); - builder.append(", url="); - builder.append(getUrl()); - builder.append(", healthy="); - builder.append(isHealthy()); - builder.append(", code="); - builder.append(getCode()); - builder.append(", message="); - builder.append(getMessage()); - builder.append("]"); - return builder.toString(); - } + /** + * Healthcheck Monitor + */ + public static final HealthCheck monitor = new HealthCheckMonitor(); + + /** + * Healthcheck Report + */ + public static class Report { + /** + * Named Entity in the report + */ + private String name; + + /** + * URL queried + */ + private String url; + + /** + * healthy? + */ + private boolean healthy; + + /** + * return code + */ + private int code; + + /** + * Message from remote entity + */ + private String message; + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Report [name="); + builder.append(getName()); + builder.append(", url="); + builder.append(getUrl()); + builder.append(", healthy="); + builder.append(isHealthy()); + builder.append(", code="); + builder.append(getCode()); + builder.append(", message="); + builder.append(getMessage()); + builder.append("]"); + return builder.toString(); + } public String getName() { return name; @@ -129,25 +131,25 @@ public interface HealthCheck extends Startable { public void setMessage(String message) { this.message = message; } - } - - /** - * Report aggregation - */ - public static class Reports { - private boolean healthy; - private List<Report> details = new ArrayList<>(); - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("Reports [healthy="); - builder.append(isHealthy()); - builder.append(", details="); - builder.append(getDetails()); - builder.append("]"); - return builder.toString(); - } + } + + /** + * Report aggregation + */ + public static class Reports { + private boolean healthy; + private List<Report> details = new ArrayList<>(); + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Reports [healthy="); + builder.append(isHealthy()); + builder.append(", details="); + builder.append(getDetails()); + builder.append("]"); + return builder.toString(); + } public boolean isHealthy() { return healthy; @@ -164,188 +166,190 @@ public interface HealthCheck extends Startable { public void setDetails(List<Report> details) { this.details = details; } - } - - /** - * perform a healthcheck - * @return a report - */ - public Reports healthCheck(); + } + + /** + * perform a healthcheck + * + * @return a report + */ + public Reports healthCheck(); } + /** * Healthcheck Monitor */ class HealthCheckMonitor implements HealthCheck { - /** - * Logger - */ - private static Logger logger = LoggerFactory.getLogger(HealthCheckMonitor.class); - - /** - * attached http servers - */ - protected volatile List<HttpServletServer> servers = new ArrayList<>(); - - /** - * attached http clients - */ - protected volatile List<HttpClient> clients = new ArrayList<>(); - - /** - * healthcheck configuration - */ - protected volatile Properties healthCheckProperties = null; - - /** - * {@inheritDoc} - */ - @Override - public Reports healthCheck() { - Reports reports = new Reports(); - reports.setHealthy(PolicyEngine.manager.isAlive()); - - HealthCheck.Report engineReport = new Report(); - engineReport.setHealthy(PolicyEngine.manager.isAlive()); - engineReport.setName("PDP-D"); - engineReport.setUrl("self"); - engineReport.setCode(PolicyEngine.manager.isAlive() ? 200 : 500); - engineReport.setMessage(PolicyEngine.manager.isAlive() ? "alive" : "not alive"); - reports.getDetails().add(engineReport); - - for (HttpClient client : clients) { - HealthCheck.Report report = new Report(); - report.setName(client.getName()); - report.setUrl(client.getBaseUrl()); - report.setHealthy(true); - try { - Response response = client.get(); - report.setCode(response.getStatus()); - if (report.getCode() != 200) { - report.setHealthy(false); - reports.setHealthy(false); - } - - report.setMessage(getHttpBody(response, client)); - } catch (Exception e) { - logger.warn("{}: cannot contact http-client {}", this, client, e); - - report.setHealthy(false); - reports.setHealthy(false); - } - reports.getDetails().add(report); - } - return reports; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean start() { - - try { - this.healthCheckProperties = SystemPersistence.manager.getProperties(HealthCheckFeature.CONFIGURATION_PROPERTIES_NAME); - this.servers = HttpServletServer.factory.build(healthCheckProperties); - this.clients = HttpClient.factory.build(healthCheckProperties); - - for (HttpServletServer server : servers) { - startServer(server); - } - } catch (Exception e) { - logger.warn("{}: cannot start {}", this, e); - return false; - } - - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public boolean stop() { - - for (HttpServletServer server : servers) { - try { - server.stop(); - } catch (Exception e) { - logger.warn("{}: cannot stop http-server {}", this, server, e); - } - } - - for (HttpClient client : clients) { - try { - client.stop(); - } catch (Exception e) { - logger.warn("{}: cannot stop http-client {}", this, client, e); - } - } - - return true; - } - - /** - * {@inheritDoc} - */ - @Override - public void shutdown() { - this.stop(); - } - - /** - * {@inheritDoc} - */ - @Override - public synchronized boolean isAlive() { - return this.healthCheckProperties != null; - } - - /** - * @return list of attached Http Servers - */ - public List<HttpServletServer> getServers() { - return this.servers; - } - - /** - * @return list of attached Http Clients - */ - public List<HttpClient> getClients() { - return this.clients; - } - - public String getHttpBody(Response response, HttpClient client) { - - String body = null; + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(HealthCheckMonitor.class); + + /** + * attached http servers + */ + protected volatile List<HttpServletServer> servers = new ArrayList<>(); + + /** + * attached http clients + */ + protected volatile List<HttpClient> clients = new ArrayList<>(); + + /** + * healthcheck configuration + */ + protected volatile Properties healthCheckProperties = null; + + /** + * {@inheritDoc} + */ + @Override + public Reports healthCheck() { + Reports reports = new Reports(); + reports.setHealthy(PolicyEngine.manager.isAlive()); + + HealthCheck.Report engineReport = new Report(); + engineReport.setHealthy(PolicyEngine.manager.isAlive()); + engineReport.setName("PDP-D"); + engineReport.setUrl("self"); + engineReport.setCode(PolicyEngine.manager.isAlive() ? 200 : 500); + engineReport.setMessage(PolicyEngine.manager.isAlive() ? "alive" : "not alive"); + reports.getDetails().add(engineReport); + + for (HttpClient client : clients) { + HealthCheck.Report report = new Report(); + report.setName(client.getName()); + report.setUrl(client.getBaseUrl()); + report.setHealthy(true); + try { + Response response = client.get(); + report.setCode(response.getStatus()); + if (report.getCode() != 200) { + report.setHealthy(false); + reports.setHealthy(false); + } + + report.setMessage(getHttpBody(response, client)); + } catch (Exception e) { + logger.warn("{}: cannot contact http-client {}", this, client, e); + + report.setHealthy(false); + reports.setHealthy(false); + } + reports.getDetails().add(report); + } + return reports; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean start() { + + try { + this.healthCheckProperties = + SystemPersistence.manager.getProperties(HealthCheckFeature.CONFIGURATION_PROPERTIES_NAME); + this.servers = IndexedHttpServletServerFactory.getInstance().build(healthCheckProperties); + this.clients = IndexedHttpClientFactory.getInstance().build(healthCheckProperties); + + for (HttpServletServer server : servers) { + startServer(server); + } + } catch (Exception e) { + logger.warn("{}: cannot start {}", this, e); + return false; + } + + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean stop() { + + for (HttpServletServer server : servers) { + try { + server.stop(); + } catch (Exception e) { + logger.warn("{}: cannot stop http-server {}", this, server, e); + } + } + + for (HttpClient client : clients) { + try { + client.stop(); + } catch (Exception e) { + logger.warn("{}: cannot stop http-client {}", this, client, e); + } + } + + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public void shutdown() { + this.stop(); + } + + /** + * {@inheritDoc} + */ + @Override + public synchronized boolean isAlive() { + return this.healthCheckProperties != null; + } + + /** + * @return list of attached Http Servers + */ + public List<HttpServletServer> getServers() { + return this.servers; + } + + /** + * @return list of attached Http Clients + */ + public List<HttpClient> getClients() { + return this.clients; + } + + public String getHttpBody(Response response, HttpClient client) { + + String body = null; try { body = HttpClient.getBody(response, String.class); } catch (Exception e) { - logger.info("{}: cannot get body from http-client {}", this, - client, e); + logger.info("{}: cannot get body from http-client {}", this, client, e); } - + return body; - } - - public void startServer(HttpServletServer server) { + } + + public void startServer(HttpServletServer server) { try { server.start(); } catch (Exception e) { logger.warn("{}: cannot start http-server {}", this, server, e); } - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("HealthCheckMonitor [servers="); - builder.append(servers); - builder.append(", clients="); - builder.append(clients); - builder.append("]"); - return builder.toString(); - } - + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("HealthCheckMonitor [servers="); + builder.append(servers); + builder.append(", clients="); + builder.append(clients); + builder.append("]"); + return builder.toString(); + } + } diff --git a/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java index a56483c4..578ce5d5 100644 --- a/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java +++ b/feature-healthcheck/src/test/java/org/onap/policy/drools/healthcheck/HealthCheckFeatureTest.java @@ -34,10 +34,10 @@ import java.util.Properties; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; import org.onap.policy.drools.healthcheck.HealthCheck.Report; import org.onap.policy.drools.healthcheck.HealthCheck.Reports; import org.onap.policy.drools.persistence.SystemPersistence; -import org.onap.policy.drools.properties.PolicyProperties; import org.onap.policy.drools.system.PolicyEngine; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -48,74 +48,63 @@ public class HealthCheckFeatureTest { * Healthcheck Configuration File */ private static final String HEALTH_CHECK_PROPERTIES_FILE = "feature-healthcheck.properties"; - - private static final Path healthCheckPropsPath = Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), - HEALTH_CHECK_PROPERTIES_FILE); - - private static final Path healthCheckPropsBackupPath = Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), - HEALTH_CHECK_PROPERTIES_FILE + ".bak"); - - + + private static final Path healthCheckPropsPath = + Paths.get(SystemPersistence.manager.getConfigurationPath().toString(), HEALTH_CHECK_PROPERTIES_FILE); + + private static final Path healthCheckPropsBackupPath = Paths + .get(SystemPersistence.manager.getConfigurationPath().toString(), HEALTH_CHECK_PROPERTIES_FILE + ".bak"); + + /** * logger */ private static Logger logger = LoggerFactory.getLogger(HealthCheckFeatureTest.class); private static Properties httpProperties = new Properties(); - - + + @BeforeClass - public static void setup(){ - - httpProperties.setProperty(PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES, "HEALTHCHECK"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX, - "localhost"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, - "7777"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, - "username"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, - "password"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, - org.onap.policy.drools.healthcheck.RestMockHealthCheck.class.getName()); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_MANAGED_SUFFIX, - "true"); - - - httpProperties.setProperty(PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES, "HEALTHCHECK"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_HOST_SUFFIX, - "localhost"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_PORT_SUFFIX, - "7777"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_URL_SUFFIX, - "healthcheck/test"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_HTTPS_SUFFIX, - "false"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, - "username"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, - "password"); - httpProperties.setProperty - (PolicyProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + PolicyProperties.PROPERTY_MANAGED_SUFFIX, - "true"); - + public static void setup() { + + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, "HEALTHCHECK"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "username"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "password"); + httpProperties.setProperty( + PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, + org.onap.policy.drools.healthcheck.RestMockHealthCheck.class.getName()); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); + + + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES, "HEALTHCHECK"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, "localhost"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, "7777"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_URL_SUFFIX, "healthcheck/test"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, "false"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, "username"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, "password"); + httpProperties.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_CLIENT_SERVICES + "." + "HEALTHCHECK" + + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); + configDirSetup(); - + } - + @AfterClass public static void tearDown() { logger.info("-- tearDown() --"); @@ -123,68 +112,68 @@ public class HealthCheckFeatureTest { configDirCleanup(); } - @Test - public void test() { - - HealthCheckFeature feature = new HealthCheckFeature(); - feature.afterStart(PolicyEngine.manager); - - Reports reports = HealthCheck.monitor.healthCheck(); - - for (Report rpt : reports.getDetails()) { - if (rpt.getName() == "HEALTHCHECK") { - assertTrue(rpt.isHealthy()); - assertEquals(200,rpt.getCode()); - assertEquals("All Alive", rpt.getMessage()); - break; - } - } - - feature.afterShutdown(PolicyEngine.manager); - - } - - - /** - * setup up config directory - */ - protected static void configDirSetup() { - - File origPropsFile = new File(healthCheckPropsPath.toString()); - File backupPropsFile = new File(healthCheckPropsBackupPath.toString()); - Path configDir = Paths.get(SystemPersistence.DEFAULT_CONFIGURATION_DIR); - - try { - - if (Files.notExists(configDir)) { - Files.createDirectories(configDir); - } - - Files.deleteIfExists(healthCheckPropsBackupPath); - origPropsFile.renameTo(backupPropsFile); - - FileWriter writer = new FileWriter(origPropsFile); - httpProperties.store(writer,"Machine created healthcheck-feature Properties"); - - } catch (final Exception e) { - logger.info("Problem cleaning {}", healthCheckPropsPath, e); - } - } - - /** - * cleanup up config directory - */ - protected static void configDirCleanup() { - + @Test + public void test() { + + HealthCheckFeature feature = new HealthCheckFeature(); + feature.afterStart(PolicyEngine.manager); + + Reports reports = HealthCheck.monitor.healthCheck(); + + for (Report rpt : reports.getDetails()) { + if (rpt.getName() == "HEALTHCHECK") { + assertTrue(rpt.isHealthy()); + assertEquals(200, rpt.getCode()); + assertEquals("All Alive", rpt.getMessage()); + break; + } + } + + feature.afterShutdown(PolicyEngine.manager); + + } + + + /** + * setup up config directory + */ + protected static void configDirSetup() { + + File origPropsFile = new File(healthCheckPropsPath.toString()); + File backupPropsFile = new File(healthCheckPropsBackupPath.toString()); + Path configDir = Paths.get(SystemPersistence.DEFAULT_CONFIGURATION_DIR); + + try { + + if (Files.notExists(configDir)) { + Files.createDirectories(configDir); + } + + Files.deleteIfExists(healthCheckPropsBackupPath); + origPropsFile.renameTo(backupPropsFile); + + FileWriter writer = new FileWriter(origPropsFile); + httpProperties.store(writer, "Machine created healthcheck-feature Properties"); + + } catch (final Exception e) { + logger.info("Problem cleaning {}", healthCheckPropsPath, e); + } + } + + /** + * cleanup up config directory + */ + protected static void configDirCleanup() { + File origPropsFile = new File(healthCheckPropsBackupPath.toString()); File backupPropsFile = new File(healthCheckPropsPath.toString()); - + try { - Files.deleteIfExists(healthCheckPropsPath); - origPropsFile.renameTo(backupPropsFile); - } catch (final Exception e) { - logger.info("Problem cleaning {}", healthCheckPropsPath, e); - } + Files.deleteIfExists(healthCheckPropsPath); + origPropsFile.renameTo(backupPropsFile); + } catch (final Exception e) { + logger.info("Problem cleaning {}", healthCheckPropsPath, e); } + } } |