diff options
author | Guo Ruijing <ruijing.guo@intel.com> | 2017-07-28 08:23:01 +0000 |
---|---|---|
committer | Guo Ruijing <ruijing.guo@intel.com> | 2017-07-28 08:23:30 +0000 |
commit | 6abeb297254942c48722c2da0e7c355d523fe307 (patch) | |
tree | f2b006ec6ca8804633e2f74a6f1b40c90683f1ea /feature-healthcheck/src/main/java/org/onap | |
parent | d1d749ae390c276fc10c4985d0080f0a9ff7ff35 (diff) |
[POLICY-72] replace openecomp for drools-pdp
Change-Id: I8aa8e32d3ba10f7c655b50e97aaf6865514d4777
Signed-off-by: Guo Ruijing <ruijing.guo@intel.com>
Diffstat (limited to 'feature-healthcheck/src/main/java/org/onap')
3 files changed, 432 insertions, 0 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 new file mode 100644 index 00000000..0e4ed792 --- /dev/null +++ b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java @@ -0,0 +1,277 @@ +/*- + * ============LICENSE_START======================================================= + * feature-healthcheck + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.healthcheck; + +import java.util.ArrayList; +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.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; + +/** + * Healthcheck + */ +public interface HealthCheck extends Startable { + + /** + * Healthcheck Report + */ + public static class Report { + /** + * Named Entity in the report + */ + public String name; + + /** + * URL queried + */ + public String url; + + /** + * healthy? + */ + public boolean healthy; + + /** + * return code + */ + public int code; + + /** + * Message from remote entity + */ + 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(); + } + } + + /** + * Report aggregation + */ + public static class Reports { + public boolean healthy; + public ArrayList<Report> 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(); + } + } + + /** + * perform a healthcheck + * @return a report + */ + public Reports healthCheck(); + + /** + * Healthcheck Monitor + */ + public static final HealthCheck monitor = new HealthCheckMonitor(); +} + +/** + * Healthcheck Monitor + */ +class HealthCheckMonitor implements HealthCheck { + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(HealthCheckMonitor.class); + + /** + * attached http servers + */ + protected volatile ArrayList<HttpServletServer> servers = new ArrayList<>(); + + /** + * attached http clients + */ + protected volatile ArrayList<HttpClient> clients = new ArrayList<>(); + + /** + * healthcheck configuration + */ + protected volatile Properties healthCheckProperties = null; + + /** + * {@inheritDoc} + */ + 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) { + logger.warn("{}: cannot get body from http-client {}", this, client, e); + } + } catch (Exception e) { + report.healthy = false; + reports.healthy = false; + } + reports.details.add(report); + } + return reports; + } + + /** + * {@inheritDoc} + */ + @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) { + logger.warn("{}: cannot start http-server {}", this, server, e); + } + } + } catch (Exception e) { + return false; + } + + return true; + } + + /** + * {@inheritDoc} + */ + @Override + public boolean stop() throws IllegalStateException { + + 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() throws IllegalStateException { + this.stop(); + } + + /** + * {@inheritDoc} + */ + @Override + public synchronized boolean isAlive() { + return this.healthCheckProperties != null; + } + + /** + * @return list of attached Http Servers + */ + public ArrayList<HttpServletServer> getServers() { + return this.servers; + } + + /** + * @return list of attached Http Clients + */ + public ArrayList<HttpClient> 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/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java new file mode 100644 index 00000000..34c1fe3a --- /dev/null +++ b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheckFeature.java @@ -0,0 +1,78 @@ +/*- + * ============LICENSE_START======================================================= + * feature-healthcheck + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.healthcheck; + +import org.onap.policy.drools.features.PolicyEngineFeatureAPI; +import org.onap.policy.drools.system.PolicyEngine; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This feature provides healthcheck verification of remotely associated RESTful components + */ +public class HealthCheckFeature implements PolicyEngineFeatureAPI { + + /** + * Logger + */ + private static Logger logger = LoggerFactory.getLogger(HealthCheckFeature.class); + + /** + * Properties Configuration Name + */ + public static final String CONFIGURATION_PROPERTIES_NAME = "feature-healthcheck"; + + @Override + public int getSequenceNumber() { + return 1000; + } + + @Override + public boolean afterStart(PolicyEngine engine) { + try { + HealthCheck.monitor.start(); + } catch (IllegalStateException e) { + logger.error("Healthcheck Monitor cannot be started", e); + } + + return false; + } + + @Override + public boolean afterShutdown(PolicyEngine engine) { + try { + HealthCheck.monitor.stop(); + } catch (IllegalStateException e) { + logger.error("Healthcheck Monitor cannot be stopped", e); + } + + return false; + } + + /** + * gets the monitor + * @return the healthcheck monitor + */ + public HealthCheck getMonitor() { + return HealthCheck.monitor; + } + +} diff --git a/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/RestHealthCheck.java b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/RestHealthCheck.java new file mode 100644 index 00000000..3f51ba46 --- /dev/null +++ b/feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/RestHealthCheck.java @@ -0,0 +1,77 @@ +/*- + * ============LICENSE_START======================================================= + * feature-healthcheck + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.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 javax.ws.rs.core.Response; + +import org.onap.policy.drools.healthcheck.HealthCheck.Reports; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.Info; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; + +@Path("/") +@Api +@Produces(MediaType.APPLICATION_JSON) +@SwaggerDefinition( + info = @Info( + description = "PDP-D Healthcheck Service", + version = "v1.0", + title = "PDP-D Healthcheck" + ), + consumes = {MediaType.APPLICATION_JSON}, + produces = {MediaType.APPLICATION_JSON}, + schemes = {SwaggerDefinition.Scheme.HTTP}, + tags = { + @Tag(name = "pdp-d-healthcheck", description = "Drools PDP Healthcheck Operations") + } +) +public class RestHealthCheck { + + @GET + @Path("healthcheck") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + value="Perform a system healthcheck", + notes="Provides healthy status of the PDP-D plus the components defined in its configuration by using a REST interface", + response=Reports.class + ) + public Response healthcheck() { + return Response.status(Response.Status.OK).entity(HealthCheck.monitor.healthCheck()).build(); + } + + @GET + @Path("healthcheck/configuration") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation( + value="Configuration", + notes="Provides the Healthcheck server configuration and monitored REST clients", + response=HealthCheck.class + ) + public HealthCheck configuration() { + return HealthCheck.monitor; + } +} |