From cd68fc9bae7d6258f77ff59c1431e4f925f61a4c Mon Sep 17 00:00:00 2001 From: liamfallon Date: Thu, 13 Sep 2018 23:48:50 +0100 Subject: Address sonar/Checkstyle Issues Sweep through Apex codebase to fix most ceheckstyle and straightforward sonar issues. Issue-ID: POLICY-1034 Change-Id: I149d9a94ad893affc93573e8de5e3304b6bdde2d Signed-off-by: liamfallon --- .../client/monitoring/rest/ApexMonitoringRest.java | 4 +- .../monitoring/rest/ApexMonitoringRestMain.java | 31 ++++--- .../rest/ApexMonitoringRestParameterParser.java | 2 +- .../rest/ApexMonitoringRestParameters.java | 14 ++- .../rest/ApexMonitoringRestResource.java | 103 ++++++++++++--------- 5 files changed, 89 insertions(+), 65 deletions(-) (limited to 'client/client-monitoring') diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRest.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRest.java index e7802ae3f..d600fb25c 100644 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRest.java +++ b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRest.java @@ -56,11 +56,11 @@ public class ApexMonitoringRest { // Create a resource configuration that scans for JAX-RS resources and providers // in org.onap.policy.apex.services.client.monitoring.rest package - final ResourceConfig rc = new ResourceConfig().packages(parameters.getRESTPackages()); + final ResourceConfig rc = new ResourceConfig().packages(parameters.getRestPackages()); // create and start a new instance of grizzly http server // exposing the Jersey application at BASE_URI - server = GrizzlyHttpServerFactory.createHttpServer(parameters.getBaseURI(), rc); + server = GrizzlyHttpServerFactory.createHttpServer(parameters.getBaseUri(), rc); // Add static content server.getServerConfiguration().addHttpHandler(new org.glassfish.grizzly.http.server.CLStaticHttpHandler( diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestMain.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestMain.java index bf2365f46..297ee97e9 100644 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestMain.java +++ b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestMain.java @@ -22,12 +22,21 @@ package org.onap.policy.apex.client.monitoring.rest; import java.io.PrintStream; +import org.slf4j.ext.XLogger; +import org.slf4j.ext.XLoggerFactory; + /** * The main class for Apex Restful Monitoring. * * @author Michael Watkins (michael.watkins@ericsson.com) */ public class ApexMonitoringRestMain { + // Logger for this class + private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexMonitoringRestMain.class); + + // Recurring string constants + private static final String REST_ENDPOINT_PREFIX = "Apex Services REST endpoint ("; + // Services state public enum ServicesState { STOPPED, READY, INITIALIZING, RUNNING @@ -54,7 +63,7 @@ public class ApexMonitoringRestMain { final ApexMonitoringRestMain restMain = new ApexMonitoringRestMain(args, System.out); restMain.init(); } catch (final Exception e) { - System.err.println(e.getMessage()); + LOGGER.error("start failed", e); } } @@ -76,7 +85,7 @@ public class ApexMonitoringRestMain { parameters = parser.parse(args); } catch (final ApexMonitoringRestParameterException e) { throw new ApexMonitoringRestParameterException( - "Apex Services REST endpoint (" + this.toString() + ") parameter error, " + e.getMessage() + '\n' + REST_ENDPOINT_PREFIX + this.toString() + ") parameter error, " + e.getMessage() + '\n' + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName())); } @@ -89,7 +98,7 @@ public class ApexMonitoringRestMain { final String validationMessage = parameters.validate(); if (validationMessage.length() > 0) { throw new ApexMonitoringRestParameterException( - "Apex Services REST endpoint (" + this.toString() + ") parameters invalid, " + validationMessage + REST_ENDPOINT_PREFIX + this.toString() + ") parameters invalid, " + validationMessage + '\n' + parser.getHelp(ApexMonitoringRestMain.class.getCanonicalName())); } @@ -100,8 +109,8 @@ public class ApexMonitoringRestMain { * Initialize the rest service. */ public void init() { - outStream.println("Apex Services REST endpoint (" + this.toString() + ") starting at " - + parameters.getBaseURI().toString() + " . . ."); + outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") starting at " + + parameters.getBaseUri().toString() + " . . ."); try { state = ServicesState.INITIALIZING; @@ -115,10 +124,10 @@ public class ApexMonitoringRestMain { state = ServicesState.RUNNING; if (parameters.getTimeToLive() == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE) { - outStream.println("Apex Services REST endpoint (" + this.toString() + ") started at " - + parameters.getBaseURI().toString()); + outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started at " + + parameters.getBaseUri().toString()); } else { - outStream.println("Apex Services REST endpoint (" + this.toString() + ") started"); + outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started"); } // Find out how long is left to wait @@ -134,7 +143,7 @@ public class ApexMonitoringRestMain { } } catch (final Exception e) { outStream.println( - "Apex Services REST endpoint (" + this.toString() + ") failed at with error: " + e.getMessage()); + REST_ENDPOINT_PREFIX + this.toString() + ") failed at with error: " + e.getMessage()); } finally { if (apexMonitoringRest != null) { apexMonitoringRest.shutdown(); @@ -167,11 +176,11 @@ public class ApexMonitoringRestMain { */ public void shutdown() { if (apexMonitoringRest != null) { - outStream.println("Apex Services REST endpoint (" + this.toString() + ") shutting down"); + outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down"); apexMonitoringRest.shutdown(); } state = ServicesState.STOPPED; - outStream.println("Apex Services REST endpoint (" + this.toString() + ") shut down"); + outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down"); } /** diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterParser.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterParser.java index af2ff6821..f5c8ad8c0 100644 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterParser.java +++ b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterParser.java @@ -81,7 +81,7 @@ public class ApexMonitoringRestParameterParser { } try { if (commandLine.hasOption('p')) { - parameters.setRESTPort(((Number) commandLine.getParsedOptionValue("port")).intValue()); + parameters.setRestPort(((Number) commandLine.getParsedOptionValue("port")).intValue()); } } catch (final ParseException e) { throw new ApexMonitoringRestParameterException("error parsing argument \"port\" :" + e.getMessage(), e); diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameters.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameters.java index fea7ae4a8..e4148a470 100644 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameters.java +++ b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameters.java @@ -44,6 +44,10 @@ public class ApexMonitoringRestParameters { private int restPort = DEFAULT_REST_PORT; private long timeToLive = INFINITY_TIME_TO_LIVE; + /** + * Validate the parameters. + * @return the result of the validation + */ public String validate() { String validationMessage = ""; validationMessage += validatePort(); @@ -52,11 +56,11 @@ public class ApexMonitoringRestParameters { return validationMessage; } - public URI getBaseURI() { + public URI getBaseUri() { return URI.create(DEFAULT_SERVER_URI_ROOT + restPort + DEFAULT_REST_PATH); } - public String[] getRESTPackages() { + public String[] getRestPackages() { return DEFAULT_PACKAGES; } @@ -88,11 +92,11 @@ public class ApexMonitoringRestParameters { this.helpSet = helpSet; } - public int getRESTPort() { + public int getRestPort() { return restPort; } - public void setRESTPort(final int restPort) { + public void setRestPort(final int restPort) { this.restPort = restPort; } @@ -107,7 +111,7 @@ public class ApexMonitoringRestParameters { @Override public String toString() { final StringBuilder ret = new StringBuilder(); - ret.append(this.getClass().getSimpleName()).append(": URI=").append(this.getBaseURI()).append(", TTL=") + ret.append(this.getClass().getSimpleName()).append(": URI=").append(this.getBaseUri()).append(", TTL=") .append(this.getTimeToLive()).append("sec"); return ret.toString(); } diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java index 85465a14b..f8cee7741 100644 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java +++ b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java @@ -48,9 +48,8 @@ import org.slf4j.ext.XLogger; import org.slf4j.ext.XLoggerFactory; /** - * The class represents the root resource exposed at the base URL
- * The url to access this resource would be in the form {@code /rest/....}
- * For example: a GET request to the following URL + * The class represents the root resource exposed at the base URL
The url to access this resource would be in the + * form {@code /rest/....}
For example: a GET request to the following URL * {@code http://localhost:18989/apexservices/rest/?hostName=localhost&port=12345} * * Note: An allocated {@code hostName} and {@code port} query parameter must be included in all requests. @@ -58,27 +57,26 @@ import org.slf4j.ext.XLoggerFactory; * */ @Path("monitoring/") -@Produces({ MediaType.APPLICATION_JSON }) -@Consumes({ MediaType.APPLICATION_JSON }) +@Produces( + { MediaType.APPLICATION_JSON }) +@Consumes( + { MediaType.APPLICATION_JSON }) public class ApexMonitoringRestResource { // Get a reference to the logger private static final XLogger LOGGER = XLoggerFactory.getXLogger(ApexMonitoringRestResource.class); + // Recurring string constants + private static final String ERROR_CONNECTING_PREFIX = "Error connecting to Apex Engine Service at "; + // Set the maximum number of stored data entries to be stored for each engine - private static final int maxCachedEntries = 50; + private static final int MAX_CACHED_ENTITIES = 50; // Set up a map separated by host and engine for the data - private static final HashMap>> cache = - new HashMap>>(); + private static final HashMap>> cache = new HashMap<>(); // Set up a map separated by host for storing the state of periodic events - private static final HashMap periodicEventsStateCache = new HashMap(); - - /** - * Constructor, a new resource director is created for each request. - */ - public ApexMonitoringRestResource() {} + private static final HashMap periodicEventsStateCache = new HashMap<>(); /** * Query the engine service for data. @@ -96,10 +94,10 @@ public class ApexMonitoringRestResource { try { engineServiceFacade.init(); } catch (final ApexDeploymentException e) { - final String errorMessage = "Error connecting to Apex Engine Service at " + host; + final String errorMessage = ERROR_CONNECTING_PREFIX + host; LOGGER.warn(errorMessage + "
", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage()) - .build(); + .build(); } final JsonObject responseObject = new JsonObject(); @@ -107,8 +105,8 @@ public class ApexMonitoringRestResource { // Engine Service data responseObject.addProperty("engine_id", engineServiceFacade.getKey().getId()); responseObject.addProperty("model_id", - engineServiceFacade.getApexModelKey() != null ? engineServiceFacade.getApexModelKey().getId() - : "Not Set"); + engineServiceFacade.getApexModelKey() != null ? engineServiceFacade.getApexModelKey().getId() + : "Not Set"); responseObject.addProperty("server", hostName); responseObject.addProperty("port", Integer.toString(port)); responseObject.addProperty("periodic_events", getPeriodicEventsState(host)); @@ -127,14 +125,16 @@ public class ApexMonitoringRestResource { engineStatusObject.addProperty("up_time", axEngineModel.getStats().getUpTime() / 1000L); engineStatusObject.addProperty("policy_executions", axEngineModel.getStats().getEventCount()); engineStatusObject.addProperty("last_policy_duration", - gson.toJson( - getValuesFromCache(host, engineKey.getId() + "_last_policy_duration", - axEngineModel.getTimestamp(), axEngineModel.getStats().getLastExecutionTime()), - List.class)); - engineStatusObject.addProperty("average_policy_duration", - gson.toJson(getValuesFromCache(host, engineKey.getId() + "_average_policy_duration", - axEngineModel.getTimestamp(), - (long) axEngineModel.getStats().getAverageExecutionTime()), List.class)); + gson.toJson(getValuesFromCache(host, engineKey.getId() + "_last_policy_duration", + axEngineModel.getTimestamp(), + axEngineModel.getStats().getLastExecutionTime()), List.class)); + engineStatusObject + .addProperty("average_policy_duration", gson.toJson( + getValuesFromCache(host, engineKey.getId() + "_average_policy_duration", + axEngineModel.getTimestamp(), + (long) axEngineModel.getStats() + .getAverageExecutionTime()), + List.class)); engineStatusList.add(engineStatusObject); } catch (final ApexException e) { LOGGER.warn("Error getting status of engine with ID " + engineKey.getId() + "
", e); @@ -174,23 +174,26 @@ public class ApexMonitoringRestResource { @GET @Path("startstop/") public Response startStop(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port, - @QueryParam("engineId") final String engineId, @QueryParam("startstop") final String startStop) { + @QueryParam("engineId") final String engineId, @QueryParam("startstop") final String startStop) { final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port); try { engineServiceFacade.init(); } catch (final ApexDeploymentException e) { - final String errorMessage = "Error connecting to Apex Engine Service at " + hostName + ":" + port; + final String errorMessage = ERROR_CONNECTING_PREFIX + hostName + ":" + port; LOGGER.warn(errorMessage + "
", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage()) - .build(); + .build(); } try { - final Map parameterMap = new HashMap(); - parameterMap.put("hostname", new String[] { hostName }); - parameterMap.put("port", new String[] { Integer.toString(port) }); - parameterMap.put("AxArtifactKey#" + engineId, new String[] { startStop }); + final Map parameterMap = new HashMap<>(); + parameterMap.put("hostname", new String[] + { hostName }); + parameterMap.put("port", new String[] + { Integer.toString(port) }); + parameterMap.put("AxArtifactKey#" + engineId, new String[] + { startStop }); final AxArtifactKey engineKey = ParameterCheck.getEngineKey(parameterMap); if (startStop.equals("Start")) { engineServiceFacade.startEngine(engineKey); @@ -203,7 +206,7 @@ public class ApexMonitoringRestResource { final StringWriter sw = new StringWriter(); e.printStackTrace(new PrintWriter(sw)); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + sw.toString()) - .build(); + .build(); } return Response.ok("{}").build(); @@ -222,17 +225,21 @@ public class ApexMonitoringRestResource { @GET @Path("periodiceventstartstop/") public Response periodiceventStartStop(@QueryParam("hostName") final String hostName, - @QueryParam("port") final int port, @QueryParam("engineId") final String engineId, - @QueryParam("startstop") final String startStop, @QueryParam("period") final long period) { + @QueryParam("port") final int port, @QueryParam("engineId") final String engineId, + @QueryParam("startstop") final String startStop, @QueryParam("period") final long period) { final EngineServiceFacade engineServiceFacade = new EngineServiceFacade(hostName, port); final String host = hostName + ":" + port; try { engineServiceFacade.init(); - final Map parameterMap = new HashMap(); - parameterMap.put("hostname", new String[] { hostName }); - parameterMap.put("port", new String[] { Integer.toString(port) }); - parameterMap.put("AxArtifactKey#" + engineId, new String[] { startStop }); - parameterMap.put("period", new String[] { Long.toString(period) }); + final Map parameterMap = new HashMap<>(); + parameterMap.put("hostname", new String[] + { hostName }); + parameterMap.put("port", new String[] + { Integer.toString(port) }); + parameterMap.put("AxArtifactKey#" + engineId, new String[] + { startStop }); + parameterMap.put("period", new String[] + { Long.toString(period) }); final AxArtifactKey engineKey = ParameterCheck.getEngineKey(parameterMap); if (startStop.equals("Start")) { engineServiceFacade.startPerioidicEvents(engineKey, period); @@ -242,10 +249,10 @@ public class ApexMonitoringRestResource { setPeriodicEventsState(host, false); } } catch (final ApexDeploymentException e) { - final String errorMessage = "Error connecting to Apex Engine Service at " + host; + final String errorMessage = ERROR_CONNECTING_PREFIX + host; LOGGER.warn(errorMessage + "
", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + e.getMessage()) - .build(); + .build(); } return Response.ok("{}").build(); @@ -258,7 +265,11 @@ public class ApexMonitoringRestResource { * @return a boolean stating if periodic events are running for a given host */ private Boolean getPeriodicEventsState(final String host) { - return periodicEventsStateCache.containsKey(host) ? periodicEventsStateCache.get(host) : false; + if (periodicEventsStateCache.containsKey(host)) { + return periodicEventsStateCache.get(host); + } else { + return false; + } } /** @@ -282,7 +293,7 @@ public class ApexMonitoringRestResource { * @return a list of {@code Counter} objects for that engine */ private List getValuesFromCache(final String host, final String id, final long timestamp, - final long latestValue) { + final long latestValue) { SlidingWindowList valueList; if (!cache.containsKey(host)) { @@ -292,7 +303,7 @@ public class ApexMonitoringRestResource { if (cache.get(host).containsKey(id)) { valueList = (SlidingWindowList) cache.get(host).get(id); } else { - valueList = new SlidingWindowList(maxCachedEntries); + valueList = new SlidingWindowList<>(MAX_CACHED_ENTITIES); } valueList.add(new Counter(timestamp, latestValue)); -- cgit 1.2.3-korg