From 25e3f7a0d6cd5e364e4fd69eef310fcdb8a58b55 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Fri, 4 Sep 2020 11:03:24 +0100 Subject: Remove client code from apex-pdp Removal of the deployment and monitoring client from the apex-pdp as the functionality is replaced by the Policy Framework deployment using PAP and the Policy Framework monitoring GUI. Code in the engine proper that was used to administer apex-pdp over the Web Socket port is also removed, as this code was only used by the apex-pdp legacy GUIs. Issue-ID: POLICY-2621 Change-Id: Ib9fc4a667a4bc79377bb7b82fae6a2bd520a3e3d Signed-off-by: liamfallon --- .../client/monitoring/rest/ApexMonitoringRest.java | 81 ----- .../monitoring/rest/ApexMonitoringRestMain.java | 204 ----------- .../rest/ApexMonitoringRestParameterException.java | 49 --- .../rest/ApexMonitoringRestParameterParser.java | 115 ------ .../rest/ApexMonitoringRestParameters.java | 118 ------ .../rest/ApexMonitoringRestResource.java | 402 --------------------- .../client/monitoring/rest/ParameterCheck.java | 255 ------------- .../apex/client/monitoring/rest/package-info.java | 27 -- 8 files changed, 1251 deletions(-) delete mode 100644 client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRest.java delete mode 100644 client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestMain.java delete mode 100644 client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterException.java delete mode 100644 client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterParser.java delete mode 100644 client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameters.java delete mode 100644 client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java delete mode 100644 client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheck.java delete mode 100644 client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/package-info.java (limited to 'client/client-monitoring/src/main/java/org') 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 deleted file mode 100644 index e327f1b11..000000000 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRest.java +++ /dev/null @@ -1,81 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.client.monitoring.rest; - -import org.glassfish.grizzly.http.server.HttpServer; -import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; -import org.glassfish.jersey.server.ResourceConfig; -import org.onap.policy.common.utils.validation.Assertions; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * This class is used to launch the services. It creates a Grizzly embedded web server and runs the services. - */ -public class ApexMonitoringRest { - // Logger for this class - private static final XLogger logger = XLoggerFactory.getXLogger(ApexMonitoringRest.class); - - // The HTTP server exposing JAX-RS resources defined in this application. - private HttpServer server; - - /** - * Starts the HTTP server for the Apex services client on the default base URI and with the default REST packages. - */ - public ApexMonitoringRest() { - this(new ApexMonitoringRestParameters()); - } - - /** - * Starts the HTTP server for the Apex services client. - * - * @param parameters The Apex parameters to use to start the server. - */ - public ApexMonitoringRest(final ApexMonitoringRestParameters parameters) { - Assertions.argumentNotNull(parameters, "parameters may not be null"); - - logger.debug("Apex services RESTful client starting . . ."); - - // 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()); - - // create and start a new instance of grizzly http server - // exposing the Jersey application at BASE_URI - server = GrizzlyHttpServerFactory.createHttpServer(parameters.getBaseUri(), rc); - - // Add static content - server.getServerConfiguration().addHttpHandler(new org.glassfish.grizzly.http.server.CLStaticHttpHandler( - ApexMonitoringRestMain.class.getClassLoader(), "/webapp/"), parameters.getStaticPath()); - - logger.debug("Apex services RESTful client started"); - } - - /** - * Shut down the web server. - */ - public void shutdown() { - logger.debug("Apex services RESTful client shutting down . . ."); - server.shutdown(); - logger.debug("Apex services RESTful client shut down"); - } -} 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 deleted file mode 100644 index 6b9531462..000000000 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestMain.java +++ /dev/null @@ -1,204 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -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 - } - - private ServicesState state = ServicesState.STOPPED; - - // The parameters for the client - private ApexMonitoringRestParameters parameters = null; - - // Output and error streams for messages - private final PrintStream outStream; - - // The Apex services client this class is running - private ApexMonitoringRest apexMonitoringRest = null; - - /** - * Constructor, kicks off the rest service. - * - * @param args The command line arguments for the RESTful service - * @param outStream The stream for output messages - */ - public ApexMonitoringRestMain(final String[] args, final PrintStream outStream) { - // Save the streams for output and error - this.outStream = outStream; - - // Client parameter parsing - final ApexMonitoringRestParameterParser parser = new ApexMonitoringRestParameterParser(); - - try { - // Get and check the parameters - parameters = parser.parse(args); - } catch (final ApexMonitoringRestParameterException e) { - throw new ApexMonitoringRestParameterException( - REST_ENDPOINT_PREFIX + this.toString() + ") parameter error, " + e.getMessage() + '\n' - + parser.getHelp(ApexMonitoringRestMain.class.getName()), - e); - } - - if (parameters.isHelpSet()) { - throw new ApexMonitoringRestParameterException(parser.getHelp(ApexMonitoringRestMain.class.getName())); - } - - // Validate the parameters - final String validationMessage = parameters.validate(); - if (validationMessage.length() > 0) { - throw new ApexMonitoringRestParameterException( - REST_ENDPOINT_PREFIX + this.toString() + ") parameters invalid, " + validationMessage + '\n' - + parser.getHelp(ApexMonitoringRestMain.class.getName())); - } - - state = ServicesState.READY; - } - - /** - * Initialize the rest service. - */ - public void init() { - outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") starting at " + parameters.getBaseUri().toString() - + " . . ."); - - try { - state = ServicesState.INITIALIZING; - - // Start the REST service - apexMonitoringRest = new ApexMonitoringRest(parameters); - - // Add a shutdown hook to shut down the rest services when the process is exiting - Runtime.getRuntime().addShutdownHook(new Thread(new ApexServicesShutdownHook())); - - state = ServicesState.RUNNING; - - if (parameters.getTimeToLive() == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE) { - outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started at " - + parameters.getBaseUri().toString()); - } else { - outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") started"); - } - - // Find out how long is left to wait - long timeRemaining = parameters.getTimeToLive(); - while (timeRemaining == ApexMonitoringRestParameters.INFINITY_TIME_TO_LIVE || timeRemaining > 0) { - // decrement the time to live in the non-infinity case - if (timeRemaining > 0) { - timeRemaining--; - } - - // Wait for a second - Thread.sleep(1000); - } - } catch (final Exception e) { - String message = REST_ENDPOINT_PREFIX + this.toString() + ") failed at with error: " + e.getMessage(); - outStream.println(message); - LOGGER.warn(message, e); - } finally { - if (apexMonitoringRest != null) { - apexMonitoringRest.shutdown(); - apexMonitoringRest = null; - } - state = ServicesState.STOPPED; - } - - } - - /** - * Get services state. - * - * @return the service state - */ - public ServicesState getState() { - return state; - } - - @Override - public String toString() { - final StringBuilder ret = new StringBuilder(); - ret.append(this.getClass().getSimpleName()).append(": Config=[").append(this.parameters).append("], State=") - .append(this.getState()); - return ret.toString(); - } - - /** - * Explicitly shut down the services. - */ - public void shutdown() { - if (apexMonitoringRest != null) { - outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shutting down"); - apexMonitoringRest.shutdown(); - } - state = ServicesState.STOPPED; - outStream.println(REST_ENDPOINT_PREFIX + this.toString() + ") shut down"); - } - - /** - * This class is a shutdown hook for the Apex services command. - */ - private class ApexServicesShutdownHook implements Runnable { - /** - * {@inheritDoc}. - */ - @Override - public void run() { - if (apexMonitoringRest != null) { - apexMonitoringRest.shutdown(); - } - } - } - - /** - * Main method, main entry point for command. - * - * @param args The command line arguments for the client - */ - public static void main(final String[] args) { - try { - final ApexMonitoringRestMain restMain = new ApexMonitoringRestMain(args, System.out); - restMain.init(); - } catch (final Exception e) { - LOGGER.error("start failed", e); - } - } -} diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterException.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterException.java deleted file mode 100644 index 9ca309f13..000000000 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterException.java +++ /dev/null @@ -1,49 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.client.monitoring.rest; - -/** - * A run time exception used to report parsing and parameter input errors. - * - * @author Michael Watkins (michael.watkins@ericsson.com) - */ -public class ApexMonitoringRestParameterException extends IllegalArgumentException { - private static final long serialVersionUID = 6520231162404452427L; - - /** - * Create an ApexServicesRestParameterException with a message. - * - * @param message the message - */ - public ApexMonitoringRestParameterException(final String message) { - super(message); - } - - /** - * Create an ApexServicesRestParameterException with a message and an exception. - * - * @param message the message - * @param throwable The exception that caused the exception - */ - public ApexMonitoringRestParameterException(final String message, final Throwable throwable) { - super(message, throwable); - } -} 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 deleted file mode 100644 index 5902ef0ce..000000000 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameterParser.java +++ /dev/null @@ -1,115 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.client.monitoring.rest; - -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.Arrays; -import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.DefaultParser; -import org.apache.commons.cli.HelpFormatter; -import org.apache.commons.cli.Option; -import org.apache.commons.cli.Options; -import org.apache.commons.cli.ParseException; - -/** - * This class reads and handles command line parameters to the Apex RESTful services. - * - * @author Michael Watkins (michael.watkins@ericsson.com) - */ -public class ApexMonitoringRestParameterParser { - // Apache Commons CLI options - private Options options; - - /** - * Construct the options for the CLI RESTful services. - */ - public ApexMonitoringRestParameterParser() { - options = new Options(); - options.addOption("h", "help", false, "outputs the usage of this command"); - options.addOption(Option.builder("p").longOpt("port").desc("port to use for the Apex Services REST calls") - .hasArg().argName("PORT").required(false).type(Number.class).build()); - options.addOption(Option.builder("t").longOpt("time-to-live") - .desc("the amount of time in seconds that the server will run for before terminating").hasArg() - .argName("TIME_TO_LIVE").required(false).type(Number.class).build()); - } - - /** - * Parse the command line options. - * - * @param args the arguments - * @return parsed parameters - */ - public ApexMonitoringRestParameters parse(final String[] args) { - CommandLine commandLine = null; - try { - commandLine = new DefaultParser().parse(options, args); - } catch (final ParseException e) { - throw new ApexMonitoringRestParameterException( - "invalid command line arguments specified : " + e.getMessage()); - } - - final ApexMonitoringRestParameters parameters = new ApexMonitoringRestParameters(); - final String[] remainingArgs = commandLine.getArgs(); - - if (commandLine.getArgs().length > 0) { - throw new ApexMonitoringRestParameterException( - "too many command line arguments specified : " + Arrays.toString(remainingArgs)); - } - - if (commandLine.hasOption('h')) { - parameters.setHelp(true); - } - try { - if (commandLine.hasOption('p')) { - parameters.setRestPort(((Number) commandLine.getParsedOptionValue("port")).intValue()); - } - } catch (final ParseException e) { - throw new ApexMonitoringRestParameterException("error parsing argument \"port\" :" + e.getMessage(), e); - } - try { - if (commandLine.hasOption('t')) { - parameters.setTimeToLive(((Number) commandLine.getParsedOptionValue("time-to-live")).longValue()); - } - } catch (final ParseException e) { - throw new ApexMonitoringRestParameterException("error parsing argument \"time-to-live\" :" + e.getMessage(), - e); - } - - return parameters; - } - - /** - * Get help information. - * - * @param mainClassName the main class name for the help output - * @return help string - */ - public String getHelp(final String mainClassName) { - final StringWriter stringWriter = new StringWriter(); - final PrintWriter stringPrintWriter = new PrintWriter(stringWriter); - - final HelpFormatter helpFormatter = new HelpFormatter(); - helpFormatter.printHelp(stringPrintWriter, 120, mainClassName + " [options...] ", "", options, 0, 0, ""); - - return stringWriter.toString(); - } -} 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 deleted file mode 100644 index e4148a470..000000000 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestParameters.java +++ /dev/null @@ -1,118 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.client.monitoring.rest; - -import java.net.URI; - -/** - * This class reads and handles command line parameters to the Apex RESTful services. - * - * @author Michael Watkins (michael.watkins@ericsson.com) - */ -public class ApexMonitoringRestParameters { - public static final int DEFAULT_REST_PORT = 18989; - public static final int INFINITY_TIME_TO_LIVE = -1; - - // Base URI the HTTP server will listen on - private static final String DEFAULT_SERVER_URI_ROOT = "http://localhost:"; - private static final String DEFAULT_REST_PATH = "/apexservices/"; - private static final String DEFAULT_STATIC_PATH = "/"; - - // Package that will field REST requests - private static final String[] DEFAULT_PACKAGES = new String[] {"org.onap.policy.apex.client.monitoring.rest"}; - - // The services parameters - private boolean helpSet = false; - 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(); - validationMessage += validateTimeToLive(); - - return validationMessage; - } - - public URI getBaseUri() { - return URI.create(DEFAULT_SERVER_URI_ROOT + restPort + DEFAULT_REST_PATH); - } - - public String[] getRestPackages() { - return DEFAULT_PACKAGES; - } - - public String getStaticPath() { - return DEFAULT_STATIC_PATH; - } - - private String validatePort() { - if (restPort < 1024 || restPort > 65535) { - return "port must be greater than 1023 and less than 65536\n"; - } else { - return ""; - } - } - - private String validateTimeToLive() { - if (timeToLive < -1) { - return "time to live must be greater than -1 (set to -1 to wait forever)\n"; - } else { - return ""; - } - } - - public boolean isHelpSet() { - return helpSet; - } - - public void setHelp(final boolean helpSet) { - this.helpSet = helpSet; - } - - public int getRestPort() { - return restPort; - } - - public void setRestPort(final int restPort) { - this.restPort = restPort; - } - - public long getTimeToLive() { - return timeToLive; - } - - public void setTimeToLive(final long timeToLive) { - this.timeToLive = timeToLive; - } - - @Override - public String toString() { - final StringBuilder ret = new StringBuilder(); - 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 deleted file mode 100644 index f31f47cd4..000000000 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ApexMonitoringRestResource.java +++ /dev/null @@ -1,402 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * ================================================================================ - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.client.monitoring.rest; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; -import java.io.PrintWriter; -import java.io.StringWriter; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import javax.ws.rs.Consumes; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import org.onap.policy.apex.core.deployment.ApexDeploymentException; -import org.onap.policy.apex.core.deployment.EngineServiceFacade; -import org.onap.policy.apex.model.basicmodel.concepts.ApexException; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.onap.policy.apex.model.enginemodel.concepts.AxEngineModel; -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 - * {@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. - * Datasets for different {@code hostName} are completely isolated from one another. - * - */ -@Path("monitoring/") -@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 MAX_CACHED_ENTITIES = 50; - - // Set up a map separated by host and engine for the data - 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<>(); - - /** - * Query the engine service for data. - * - * @param hostName the host name of the engine service to connect to. - * @param port the port number of the engine service to connect to. - * @return a Response object containing the engines service, status and context data in JSON - */ - @GET - public Response createSession(@QueryParam("hostName") final String hostName, @QueryParam("port") final int port) { - final Gson gson = new Gson(); - final String host = hostName + ":" + port; - final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(hostName, port); - - try { - engineServiceFacade.init(); - } catch (final ApexDeploymentException e) { - 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(); - } - - final JsonObject responseObject = new JsonObject(); - - // Engine Service data - responseObject.addProperty("engine_id", engineServiceFacade.getKey().getId()); - responseObject.addProperty("model_id", - engineServiceFacade.getApexModelKey() != null ? engineServiceFacade.getApexModelKey().getId() - : "Not Set"); - responseObject.addProperty("server", hostName); - responseObject.addProperty("port", Integer.toString(port)); - responseObject.addProperty("periodic_events", getPeriodicEventsState(host)); - - // Engine Status data - final JsonArray engineStatusList = new JsonArray(); - - for (final AxArtifactKey engineKey : engineServiceFacade.getEngineKeyArray()) { - try { - final JsonObject engineStatusObject = new JsonObject(); - final AxEngineModel axEngineModel = engineServiceFacade.getEngineStatus(engineKey); - engineStatusObject.addProperty("timestamp", axEngineModel.getTimeStampString()); - engineStatusObject.addProperty("id", engineKey.getId()); - engineStatusObject.addProperty("status", axEngineModel.getState().toString()); - engineStatusObject.addProperty("last_message", axEngineModel.getStats().getTimeStampString()); - 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)); - engineStatusList.add(engineStatusObject); - } catch (final ApexException e) { - LOGGER.warn("Error getting status of engine with ID " + engineKey.getId() + "
", e); - } - } - responseObject.add("status", engineStatusList); - - // Engine context data - final JsonArray engineContextList = new JsonArray(); - for (final AxArtifactKey engineKey : engineServiceFacade.getEngineKeyArray()) { - try { - final String engineInfo = engineServiceFacade.getEngineInfo(engineKey); - if (engineInfo != null && !engineInfo.trim().isEmpty()) { - final JsonObject engineContextObject = new JsonObject(); - engineContextObject.addProperty("id", engineKey.getId()); - engineContextObject.addProperty("engine_info", engineInfo); - engineContextList.add(engineContextObject); - } - } catch (final ApexException e) { - LOGGER.warn("Error getting runtime information of engine with ID " + engineKey.getId() + "
", e); - } - } - responseObject.add("context", engineContextList); - - return Response.ok(responseObject.toString(), MediaType.APPLICATION_JSON).build(); - } - - /** - * Start/Stop and Apex engine. - * - * @param hostName the host name of the engine service to connect to. - * @param port the port number of the engine service to connect to. - * @param engineId the id of the engine to be started/stopped. - * @param startStop the parameter to start/stop the engine. Expects either "Start" or "Stop" - * @return a Response object of type 200 - */ - @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) { - final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(hostName, port); - - try { - engineServiceFacade.init(); - } catch (final ApexDeploymentException e) { - 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(); - } - - 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 AxArtifactKey engineKey = ParameterCheck.getEngineKey(parameterMap); - if ("Start".equals(startStop)) { - engineServiceFacade.startEngine(engineKey); - } else if ("Stop".equals(startStop)) { - engineServiceFacade.stopEngine(engineKey); - } - } catch (final Exception e) { - final String errorMessage = "Error calling " + startStop + " on Apex Engine: " + engineId; - LOGGER.warn(errorMessage + "
", e); - final StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw)); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage + "\n" + sw.toString()) - .build(); - } - - return Response.ok("{}").build(); - } - - /** - * Start/Stop Apex engine Periodic Events. - * - * @param hostName the host name of the engine service to connect to. - * @param port the port number of the engine service to connect to. - * @param engineId the id of the engine to be started/stopped. - * @param startStop the parameter to start/stop the engine. Expects either "Start" or "Stop" - * @param period the time between each event in milliseconds - * @return a Response object of type 200 - */ - @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) { - final EngineServiceFacade engineServiceFacade = getEngineServiceFacade(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 AxArtifactKey engineKey = ParameterCheck.getEngineKey(parameterMap); - if ("Start".equals(startStop)) { - engineServiceFacade.startPerioidicEvents(engineKey, period); - setPeriodicEventsState(host, true); - } else if ("Stop".equals(startStop)) { - engineServiceFacade.stopPerioidicEvents(engineKey); - setPeriodicEventsState(host, false); - } - } catch (final ApexDeploymentException e) { - 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(); - } - - return Response.ok("{}").build(); - } - - /** - * Check if periodic events are running. - * - * @param host the engine's host url - * @return a boolean stating if periodic events are running for a given host - */ - private Boolean getPeriodicEventsState(final String host) { - if (periodicEventsStateCache.containsKey(host)) { - return periodicEventsStateCache.get(host); - } else { - return false; - } - } - - /** - * Sets the state of periodic events for a host. - * - * @param host the engine's host url - * @param boolean that states if periodic events have been started or stopped - */ - private void setPeriodicEventsState(final String host, final Boolean isRunning) { - periodicEventsStateCache.put(host, isRunning); - } - - /** - * This method takes in the latest data entry for an engine, adds it to an existing data set and returns the full - * map for that host and engine. - * - * @param host the engine's host url - * @param id the engines id - * @param timestamp the timestamp of the latest data entry - * @param latestValue the value of the latest data entry - * @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) { - SlidingWindowList valueList; - - if (!cache.containsKey(host)) { - cache.put(host, new HashMap<>()); - } - - if (cache.get(host).containsKey(id)) { - valueList = (SlidingWindowList) cache.get(host).get(id); - } else { - valueList = new SlidingWindowList<>(MAX_CACHED_ENTITIES); - } - valueList.add(new Counter(timestamp, latestValue)); - - cache.get(host).put(id, valueList); - - return valueList; - } - - - /** - * Get an engine service facade for sending REST requests. This method is package because it is used by unit test. - * - * @param hostName the host name of the Apex engine - * @param port the port of the Apex engine - * @return the engine service facade - */ - protected EngineServiceFacade getEngineServiceFacade(final String hostName, final int port) { - return new EngineServiceFacade(hostName, port); - } - - /** - * A list of values that uses a FIFO sliding window of a fixed size. - */ - public class SlidingWindowList extends LinkedList { - private static final long serialVersionUID = -7187277916025957447L; - - private final int maxEntries; - - public SlidingWindowList(final int maxEntries) { - this.maxEntries = maxEntries; - } - - @Override - public boolean add(final V elm) { - if (this.size() > (maxEntries - 1)) { - this.removeFirst(); - } - return super.add(elm); - } - - private ApexMonitoringRestResource getOuterType() { - return ApexMonitoringRestResource.this; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + getOuterType().hashCode(); - result = prime * result + maxEntries; - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - - if (!super.equals(obj)) { - return false; - } - - if (getClass() != obj.getClass()) { - return false; - } - - @SuppressWarnings("unchecked") - SlidingWindowList other = (SlidingWindowList) obj; - if (!getOuterType().equals(other.getOuterType())) { - return false; - } - - return maxEntries == other.maxEntries; - } - } - - /** - * A class used to storing a single data entry for an engine. - */ - public class Counter { - private long timestamp; - private long value; - - public Counter(final long timestamp, final long value) { - this.timestamp = timestamp; - this.value = value; - } - - public long getTimestamp() { - return timestamp; - } - - public long getValue() { - return value; - } - } -} diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheck.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheck.java deleted file mode 100644 index 703731333..000000000 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/ParameterCheck.java +++ /dev/null @@ -1,255 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.apex.client.monitoring.rest; - -import java.util.Map; -import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * The Class ParameterCheck is used to check parameters passed to the servlet. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public final class ParameterCheck { - private static final XLogger LOGGER = XLoggerFactory.getXLogger(ParameterCheck.class); - - private static final String HOSTNAME_PAR = "hostname"; - private static final String PORT_PAR = "port"; - private static final String AXARTIFACTKEY_PAR = "AxArtifactKey"; - - // Recurring string constants - private static final String PARAMETER = "parameter \""; - private static final String NOT_FOUND = "\" not found"; - - private static final int MAX_PORT = 65535; - - /** - * private constructor to prevent subclassing of this utility class. - */ - private ParameterCheck() { - } - - /** - * The Enum StartStop is used to hold . - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ - public enum StartStop { - /** Start of an Apex engine has been ordered. */ - START, - /** Stop of an Apex engine has been ordered. */ - STOP - } - - /** - * Gets the host name. - * - * @param parameterMap the parameter map - * @return the host name - */ - public static String getHostName(final Map parameterMap) { - if (parameterMap == null) { - return null; - } - - if (!parameterMap.containsKey(HOSTNAME_PAR)) { - LOGGER.warn(PARAMETER + HOSTNAME_PAR + NOT_FOUND); - return null; - } - - final String[] hostNameValue = parameterMap.get(HOSTNAME_PAR); - - if (hostNameValue == null) { - return null; - } - - if (hostNameValue.length == 0 || hostNameValue[0].trim().length() == 0) { - LOGGER.warn("value of parameter \"" + HOSTNAME_PAR + NOT_FOUND); - return null; - } - - return hostNameValue[0]; - } - - /** - * Gets the port. - * - * @param parameterMap the parameter map - * @return the port - */ - public static int getPort(final Map parameterMap) { - if (parameterMap == null) { - return -1; - } - - if (!parameterMap.containsKey(PORT_PAR)) { - LOGGER.warn(PARAMETER + PORT_PAR + NOT_FOUND); - return -1; - } - - final String[] portValue = parameterMap.get(PORT_PAR); - - if (portValue == null) { - return -1; - } - - if (portValue.length == 0 || portValue[0].trim().length() == 0) { - LOGGER.warn("value of parameter \"" + PORT_PAR + NOT_FOUND); - return -1; - } - - int port = -1; - try { - port = Integer.parseInt(portValue[0]); - } catch (final Exception e) { - LOGGER.warn("value \"{}\"of parameter \"" + PORT_PAR + "\" not a valid integer", portValue[0], e); - return -1; - } - - if (port <= 0 || port > MAX_PORT) { - LOGGER.warn("value \"{}\"of parameter \"" + PORT_PAR + "\" not a valid port between 0 and 65535", - portValue[0]); - return -1; - } - - return port; - } - - /** - * Gets the engine key. - * - * @param parameterMap the parameter map - * @return the engine key - */ - public static AxArtifactKey getEngineKey(final Map parameterMap) { - if (parameterMap == null) { - return null; - } - - String artifactKeyParameter = null; - for (final String parameter : parameterMap.keySet()) { - // Check for an AxArtifactKey parameter - if (parameter.startsWith(AXARTIFACTKEY_PAR)) { - artifactKeyParameter = parameter; - break; - } - } - if (artifactKeyParameter == null) { - LOGGER.warn(PARAMETER + AXARTIFACTKEY_PAR + NOT_FOUND); - return null; - } - - final String[] axArtifactKeyArray = artifactKeyParameter.split("#"); - - if (axArtifactKeyArray.length != 2) { - LOGGER.warn("value \"{}\" of parameter \"" + AXARTIFACTKEY_PAR + "\" not valid", artifactKeyParameter); - return null; - } - - try { - return new AxArtifactKey(axArtifactKeyArray[1]); - } catch (Exception apEx) { - LOGGER.trace("invalid artifact key ID {}", axArtifactKeyArray[1], apEx); - return null; - } - } - - /** - * Gets the start stop. - * - * @param parameterMap the parameter map - * @param engineKey the engine key - * @return the start stop - */ - public static ParameterCheck.StartStop getStartStop(final Map parameterMap, - final AxArtifactKey engineKey) { - if (parameterMap == null || engineKey == null) { - return null; - } - - final String startStopPar = AXARTIFACTKEY_PAR + '#' + engineKey.getId(); - if (!parameterMap.containsKey(startStopPar)) { - LOGGER.warn("parameter \"{}\" not found", startStopPar); - return null; - } - - final String[] startStopValue = parameterMap.get(startStopPar); - - if (startStopValue == null) { - return null; - } - - if (startStopValue.length == 0 || startStopValue[0].trim().length() == 0) { - LOGGER.warn("value of parameter \"{}\" not found", startStopPar); - return null; - } - - ParameterCheck.StartStop startStop; - if ("start".equalsIgnoreCase(startStopValue[0])) { - startStop = ParameterCheck.StartStop.START; - } else if ("stop".equalsIgnoreCase(startStopValue[0])) { - startStop = ParameterCheck.StartStop.STOP; - } else { - LOGGER.warn("value \"{}\"of parameter \"{}\" not \"start\" or \"stop\"", startStopValue[0], startStopPar); - return null; - } - - return startStop; - } - - /** - * Find and return a long value with the given name. - * - * @param parameterMap The parameter map containing the value - * @param longName The name of the long parameter - * @return The long value - */ - public static long getLong(final Map parameterMap, final String longName) { - if (parameterMap == null || longName == null) { - return -1; - } - - if (!parameterMap.containsKey(longName)) { - LOGGER.warn("parameter \"{}\" not found", longName); - return -1; - } - - final String[] longValue = parameterMap.get(longName); - - if (longValue == null) { - return -1; - } - - if (longValue.length == 0 || longValue[0].trim().length() == 0) { - LOGGER.warn("value of parameter \"{}\" not found", longName); - return -1; - } - - try { - return Long.parseLong(longValue[0]); - } catch (final Exception e) { - LOGGER.warn("value \"{}\"of parameter \"{}\" not a valid long", longValue[0], longName, e); - return -1; - } - } -} diff --git a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/package-info.java b/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/package-info.java deleted file mode 100644 index d6c8d3c5e..000000000 --- a/client/client-monitoring/src/main/java/org/onap/policy/apex/client/monitoring/rest/package-info.java +++ /dev/null @@ -1,27 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -/** - * Implements the RESTful monitoring client. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ - -package org.onap.policy.apex.client.monitoring.rest; -- cgit 1.2.3-korg