From 0e16acf4d8579fd82349d6cd24e236275735a818 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Tue, 14 Feb 2017 19:45:48 -0500 Subject: Initial OpenECOMP policy/drools-pdp commit Change-Id: I0072ccab6f40ed32da39667f9f8523b6d6dad2e2 Signed-off-by: Pamela Dragosh --- policy-healthcheck/pom.xml | 157 ++++++++++++++++++ policy-healthcheck/src/assembly/assemble_zip.xml | 85 ++++++++++ .../policy/drools/healthcheck/HealthCheck.java | 178 +++++++++++++++++++++ .../drools/healthcheck/HealthCheckFeature.java | 92 +++++++++++ .../policy/drools/healthcheck/RestHealthCheck.java | 27 ++++ .../org.openecomp.policy.drools.core.FeatureAPI | 1 + 6 files changed, 540 insertions(+) create mode 100644 policy-healthcheck/pom.xml create mode 100644 policy-healthcheck/src/assembly/assemble_zip.xml create mode 100644 policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/HealthCheck.java create mode 100644 policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/HealthCheckFeature.java create mode 100644 policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/RestHealthCheck.java create mode 100644 policy-healthcheck/src/main/resources/META-INF/services/org.openecomp.policy.drools.core.FeatureAPI (limited to 'policy-healthcheck') diff --git a/policy-healthcheck/pom.xml b/policy-healthcheck/pom.xml new file mode 100644 index 00000000..c279cd1d --- /dev/null +++ b/policy-healthcheck/pom.xml @@ -0,0 +1,157 @@ + + + + + 4.0.0 + + + org.openecomp.policy.drools-pdp + drools-pdp + 1.0.0-SNAPSHOT + + + policy-healthcheck + + policy-healthcheck + Separately loadable module with healthcheck code + + + 1.8 + 1.8 + + + + + + maven-assembly-plugin + 2.6 + + + zipfile + + single + + package + + true + ${project.artifactId}-${project.version} + + src/assembly/assemble_zip.xml + + false + + + + + + org.apache.maven.plugins + maven-dependency-plugin + 2.8 + + + copy-dependencies + + copy-dependencies + + prepare-package + + false + ${project.build.directory}/assembly/lib + false + true + true + false + false + false + org.opendaylight,com.brocade.odl + provided + + + + + + maven-resources-plugin + 2.6 + + + copy-version + + copy-resources + + validate + + ${basedir}/target/versions + + + src/main/resources/versions + + version.properties + + true + + + + + + copy-resources + + copy-resources + + validate + + ${basedir}/target/etc/bvc-extensions + + + src/main/resources/etc/bvc-extensions + + feature_config_template.cfg + feature_custom.install + + true + + + + + + + + + + + + org.openecomp.policy.drools-pdp + policy-core + 1.0.0-SNAPSHOT + + + org.openecomp.policy.drools-pdp + policy-endpoints + 1.0.0-SNAPSHOT + + + org.openecomp.policy.drools-pdp + policy-management + 1.0.0-SNAPSHOT + + + + diff --git a/policy-healthcheck/src/assembly/assemble_zip.xml b/policy-healthcheck/src/assembly/assemble_zip.xml new file mode 100644 index 00000000..266e1d0a --- /dev/null +++ b/policy-healthcheck/src/assembly/assemble_zip.xml @@ -0,0 +1,85 @@ + + + + + + runtime + + zip + + + + false + + + + target + lib/opt + + policy-healthcheck-${project.version}.jar + + + + target/assembly/ + . + + + + + . + lib + + *.jar + + + + src/main/server-gen/bin + bin + 0744 + + + + + src/main/server/bin + bin + 0744 + + + + + src/main/server-gen/scripts + scripts + + + src/main/server/scripts + scripts + + + src/main/server/config + config + + + + diff --git a/policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/HealthCheck.java b/policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/HealthCheck.java new file mode 100644 index 00000000..2ed2e075 --- /dev/null +++ b/policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/HealthCheck.java @@ -0,0 +1,178 @@ +package org.openecomp.policy.drools.healthcheck; + +import java.util.ArrayList; +import java.util.Properties; + +import javax.ws.rs.core.Response; + +import org.openecomp.policy.drools.http.client.HttpClient; +import org.openecomp.policy.drools.http.server.HttpServletServer; +import org.openecomp.policy.drools.persistence.SystemPersistence; +import org.openecomp.policy.drools.properties.Startable; +import org.openecomp.policy.drools.system.PolicyEngine; + +public interface HealthCheck extends Startable { + + public static class Report { + public String name; + public String url; + public boolean healthy; + public int code; + public String message; + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Report [name="); + builder.append(name); + builder.append(", url="); + builder.append(url); + builder.append(", healthy="); + builder.append(healthy); + builder.append(", code="); + builder.append(code); + builder.append(", message="); + builder.append(message); + builder.append("]"); + return builder.toString(); + } + } + + public static class Reports { + public boolean healthy; + public ArrayList details = new ArrayList<>(); + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Reports [healthy="); + builder.append(healthy); + builder.append(", details="); + builder.append(details); + builder.append("]"); + return builder.toString(); + } + } + + public Reports healthCheck(); + + public static final HealthCheck monitor = new HealthCheckMonitor(); +} + +class HealthCheckMonitor implements HealthCheck { + + protected volatile ArrayList servers = new ArrayList<>(); + protected volatile ArrayList clients = new ArrayList<>(); + protected volatile Properties healthCheckProperties = null; + + public Reports healthCheck() { + Reports reports = new Reports(); + reports.healthy = PolicyEngine.manager.isAlive(); + + HealthCheck.Report engineReport = new Report(); + engineReport.healthy = PolicyEngine.manager.isAlive(); + engineReport.name = "PDP-D"; + engineReport.url = "self"; + engineReport.code = (PolicyEngine.manager.isAlive()) ? 200 : 500; + engineReport.message = (PolicyEngine.manager.isAlive()) ? "alive" : "not alive"; + reports.details.add(engineReport); + + for (HttpClient client : clients) { + HealthCheck.Report report = new Report(); + report.name = client.getName(); + report.url = client.getBaseUrl(); + report.healthy = true; + try { + Response response = client.get(); + report.code = response.getStatus(); + if (report.code != 200) { + report.healthy = false; + reports.healthy = false; + } + + try { + report.message = HttpClient.getBody(response, String.class); + } catch (Exception e) { + e.printStackTrace(); + } + } catch (Exception e) { + report.healthy = false; + reports.healthy = false; + } + reports.details.add(report); + } + return reports; + } + + @Override + public boolean start() throws IllegalStateException { + 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) { + try { + server.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + } catch (Exception e) { + return false; + } + + return true; + } + + @Override + public boolean stop() throws IllegalStateException { + for (HttpServletServer server : servers) { + try { + server.stop(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + for (HttpClient client : clients) { + try { + client.stop(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + return true; + } + + @Override + public void shutdown() throws IllegalStateException { + this.stop(); + } + + @Override + public synchronized boolean isAlive() { + return this.healthCheckProperties != null; + } + + public ArrayList getServers() { + return this.servers; + } + + public ArrayList getClients() { + return this.clients; + } + + @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/policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/HealthCheckFeature.java b/policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/HealthCheckFeature.java new file mode 100644 index 00000000..ce1dd90b --- /dev/null +++ b/policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/HealthCheckFeature.java @@ -0,0 +1,92 @@ +package org.openecomp.policy.drools.healthcheck; + +import java.util.Properties; + +import org.kie.api.runtime.KieSession; +import org.openecomp.policy.drools.core.FeatureAPI; +import org.openecomp.policy.drools.core.PolicyContainer; +import org.openecomp.policy.drools.core.PolicySession; + +public class HealthCheckFeature implements FeatureAPI { + + public static final String CONFIGURATION_PROPERTIES_NAME = "policy-healthcheck"; + + @Override + public int getSequenceNumber() { + return 2; + } + + @Override + public void globalInit(String[] args, String configDir) { + return; + } + + @Override + public KieSession activatePolicySession(PolicyContainer policyContainer, String name, String kieBaseName) { + return null; + } + + @Override + public void disposeKieSession(PolicySession policySession) { + return; + } + + @Override + public void destroyKieSession(PolicySession policySession) { + return; + } + + @Override + public void beforeStartEngine() throws IllegalStateException { + return; + } + + @Override + public void afterStartEngine() { + try { + HealthCheck.monitor.start(); + } catch (IllegalStateException e) { + e.printStackTrace(); + } + } + + @Override + public void beforeShutdownEngine() { + return; + } + + @Override + public void afterShutdownEngine() { + try { + HealthCheck.monitor.stop(); + } catch (IllegalStateException e) { + e.printStackTrace(); + } + } + + @Override + public void beforeCreateController(String name, Properties properties) { + return; + } + + @Override + public void afterCreateController(String name) { + return; + } + + @Override + public void beforeStartController(String name) { + return; + } + + @Override + public void afterStartController(String name) { + return; + } + + @Override + public boolean isPersistenceEnabled() { + return false; + } + +} diff --git a/policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/RestHealthCheck.java b/policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/RestHealthCheck.java new file mode 100644 index 00000000..e37e758a --- /dev/null +++ b/policy-healthcheck/src/main/java/org/openecomp/policy/drools/healthcheck/RestHealthCheck.java @@ -0,0 +1,27 @@ +package org.openecomp.policy.drools.healthcheck; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +import org.openecomp.policy.drools.healthcheck.HealthCheck.Reports; + +@Path("/") +public class RestHealthCheck { + + @GET + @Path("{a:healthcheck|test}") + @Produces(MediaType.APPLICATION_JSON) + public Reports healthcheck() { + Reports reports = HealthCheck.monitor.healthCheck(); + return reports; + } + + @GET + @Path("healthcheck/configuration") + @Produces(MediaType.APPLICATION_JSON) + public HealthCheck configuration() { + return HealthCheck.monitor; + } +} diff --git a/policy-healthcheck/src/main/resources/META-INF/services/org.openecomp.policy.drools.core.FeatureAPI b/policy-healthcheck/src/main/resources/META-INF/services/org.openecomp.policy.drools.core.FeatureAPI new file mode 100644 index 00000000..7e307d08 --- /dev/null +++ b/policy-healthcheck/src/main/resources/META-INF/services/org.openecomp.policy.drools.core.FeatureAPI @@ -0,0 +1 @@ +org.openecomp.policy.drools.healthcheck.HealthCheckFeature -- cgit 1.2.3-korg