diff options
author | Magnusen, Drew (dm741q) <dm741q@att.com> | 2017-12-05 14:56:32 -0600 |
---|---|---|
committer | Magnusen, Drew (dm741q) <dm741q@att.com> | 2017-12-05 14:56:47 -0600 |
commit | cdc0d1dcfbd41324fe4209caf4709d3e9ebc3cdf (patch) | |
tree | 14fdf2d5883c702fbf55cf2b555a64c9c6248c21 /feature-healthcheck/src/main/java | |
parent | 5108a6054ee3fc2bab9dfa0e457deb868c67407f (diff) |
feature-healthcheck technical debt
Made multiple changes to feature-healthcheck module to reduce technical
debt identified by sonar.
Issue-ID: POLICY-464
Change-Id: Ie168821611db5c0b171114e4fdd90411ce38a796
Signed-off-by: Magnusen, Drew (dm741q) <dm741q@att.com>
Diffstat (limited to 'feature-healthcheck/src/main/java')
-rw-r--r-- | feature-healthcheck/src/main/java/org/onap/policy/drools/healthcheck/HealthCheck.java | 158 |
1 files changed, 114 insertions, 44 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 cc6f02e0..bdb15a70 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 @@ -21,6 +21,7 @@ package org.onap.policy.drools.healthcheck; import java.util.ArrayList; +import java.util.List; import java.util.Properties; import javax.ws.rs.core.Response; @@ -45,63 +46,119 @@ public interface HealthCheck extends Startable { /** * Named Entity in the report */ - public String name; + private String name; /** * URL queried */ - public String url; + private String url; /** * healthy? */ - public boolean healthy; + private boolean healthy; /** * return code */ - public int code; + private int code; /** * Message from remote entity */ - public String message; + private String message; @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Report [name="); - builder.append(name); + builder.append(getName()); builder.append(", url="); - builder.append(url); + builder.append(getUrl()); builder.append(", healthy="); - builder.append(healthy); + builder.append(isHealthy()); builder.append(", code="); - builder.append(code); + builder.append(getCode()); builder.append(", message="); - builder.append(message); + builder.append(getMessage()); builder.append("]"); return builder.toString(); } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public boolean isHealthy() { + return healthy; + } + + public void setHealthy(boolean healthy) { + this.healthy = healthy; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } } /** * Report aggregation */ public static class Reports { - public boolean healthy; - public ArrayList<Report> details = new ArrayList<>(); + private boolean healthy; + private List<Report> details = new ArrayList<>(); @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Reports [healthy="); - builder.append(healthy); + builder.append(isHealthy()); builder.append(", details="); - builder.append(details); + builder.append(getDetails()); builder.append("]"); return builder.toString(); } + + public boolean isHealthy() { + return healthy; + } + + public void setHealthy(boolean healthy) { + this.healthy = healthy; + } + + public List<Report> getDetails() { + return details; + } + + public void setDetails(ArrayList<Report> details) { + this.details = details; + } } /** @@ -147,41 +204,37 @@ class HealthCheckMonitor implements HealthCheck { @Override public Reports healthCheck() { Reports reports = new Reports(); - reports.healthy = PolicyEngine.manager.isAlive(); + reports.setHealthy(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); + 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.name = client.getName(); - report.url = client.getBaseUrl(); - report.healthy = true; + report.setName(client.getName()); + report.setUrl(client.getBaseUrl()); + report.setHealthy(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.info("{}: cannot get body from http-client {}", this, client, e); + 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.healthy = false; - reports.healthy = false; + report.setHealthy(false); + reports.setHealthy(false); } - reports.details.add(report); + reports.getDetails().add(report); } return reports; } @@ -198,11 +251,7 @@ class HealthCheckMonitor implements HealthCheck { 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); - } + startServer(server); } } catch (Exception e) { logger.warn("{}: cannot start {}", this, e); @@ -256,16 +305,37 @@ class HealthCheckMonitor implements HealthCheck { /** * @return list of attached Http Servers */ - public ArrayList<HttpServletServer> getServers() { + public List<HttpServletServer> getServers() { return this.servers; } /** * @return list of attached Http Clients */ - public ArrayList<HttpClient> getClients() { + 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); + } + + return body; + } + + public void startServer(HttpServletServer server) { + try { + server.start(); + } catch (Exception e) { + logger.warn("{}: cannot start http-server {}", this, server, e); + } + } @Override public String toString() { |