diff options
author | 2019-02-05 10:28:22 +0000 | |
---|---|---|
committer | 2019-02-05 10:28:22 +0000 | |
commit | 370c227614be2b63c9383d4eb5a6c064d3d49835 (patch) | |
tree | 105047c7ec00b42f4793a99ade2f9d3bae89ef10 /main/src/main/java/org | |
parent | 07b7ba00a053a26a5eef39070bff5297c630c5ce (diff) |
Adding healthcheck endpoint to policy/pap
1) Adding healthcheck REST endpoint to policy/pap using the
policy-endpoints module in policy/common.
2) Added the related unit test cases.
Change-Id: I6a215cceccc9cd42494aef1dfcdd46f0f3fd7d13
Issue-ID: POLICY-1477
Signed-off-by: ramverma <ram.krishna.verma@est.tech>
Diffstat (limited to 'main/src/main/java/org')
8 files changed, 439 insertions, 13 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java index 01032c56..09f87049 100644 --- a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java +++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterGroup.java @@ -32,14 +32,17 @@ import org.onap.policy.common.utils.validation.ParameterValidationUtils; */ public class PapParameterGroup implements ParameterGroup { private String name; + private RestServerParameters restServerParameters; /** * Create the pap parameter group. * * @param name the parameter group name + * @param restServerParameters the rest server parameters */ - public PapParameterGroup(final String name) { + public PapParameterGroup(final String name, final RestServerParameters restServerParameters) { this.name = name; + this.restServerParameters = restServerParameters; } /** @@ -63,6 +66,15 @@ public class PapParameterGroup implements ParameterGroup { } /** + * Return the restServerParameters of this parameter group instance. + * + * @return the restServerParameters + */ + public RestServerParameters getRestServerParameters() { + return restServerParameters; + } + + /** * Validate the parameter group. * * @return the result of the validation @@ -73,6 +85,12 @@ public class PapParameterGroup implements ParameterGroup { if (!ParameterValidationUtils.validateStringParameter(name)) { validationResult.setResult("name", ValidationStatus.INVALID, "must be a non-blank string"); } + if (restServerParameters == null) { + validationResult.setResult("restServerParameters", ValidationStatus.INVALID, + "must have restServerParameters to configure pap rest server"); + } else { + validationResult.setResult("restServerParameters", restServerParameters.validate()); + } return validationResult; } } diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterHandler.java b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterHandler.java index f397daed..08124bb3 100644 --- a/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterHandler.java +++ b/main/src/main/java/org/onap/policy/pap/main/parameters/PapParameterHandler.java @@ -28,8 +28,8 @@ import java.io.FileReader; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.startstop.PapCommandLineArguments; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class handles reading, parsing and validating of policy pap parameters from JSON files. @@ -37,7 +37,8 @@ import org.slf4j.ext.XLoggerFactory; * @author Ram Krishna Verma (ram.krishna.verma@est.tech) */ public class PapParameterHandler { - private static final XLogger LOGGER = XLoggerFactory.getXLogger(PapParameterHandler.class); + + private static final Logger LOGGER = LoggerFactory.getLogger(PapParameterHandler.class); /** * Read the parameters from the parameter file. diff --git a/main/src/main/java/org/onap/policy/pap/main/parameters/RestServerParameters.java b/main/src/main/java/org/onap/policy/pap/main/parameters/RestServerParameters.java new file mode 100644 index 00000000..cfc8832b --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/parameters/RestServerParameters.java @@ -0,0 +1,138 @@ +/*- + * ============LICENSE_START======================================================= + * 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.pap.main.parameters; + +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ValidationStatus; +import org.onap.policy.common.utils.validation.ParameterValidationUtils; + +/** + * Class to hold all parameters needed for pap rest server. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class RestServerParameters implements ParameterGroup { + private String name; + private String host; + private int port; + private String userName; + private String password; + + /** + * Constructor for instantiating RestServerParameters. + * + * @param host the host name + * @param port the port + * @param userName the user name + * @param password the password + */ + public RestServerParameters(final String host, final int port, final String userName, final String password) { + super(); + this.host = host; + this.port = port; + this.userName = userName; + this.password = password; + } + + /** + * Return the name of this RestServerParameters instance. + * + * @return name the name of this RestServerParameters + */ + @Override + public String getName() { + return name; + } + + /** + * Return the host of this RestServerParameters instance. + * + * @return the host + */ + public String getHost() { + return host; + } + + /** + * Return the port of this RestServerParameters instance. + * + * @return the port + */ + public int getPort() { + return port; + } + + /** + * Return the user name of this RestServerParameters instance. + * + * @return the userName + */ + public String getUserName() { + return userName; + } + + /** + * Return the password of this RestServerParameters instance. + * + * @return the password + */ + public String getPassword() { + return password; + } + + /** + * Set the name of this RestServerParameters instance. + * + * @param name the name to set + */ + @Override + public void setName(final String name) { + this.name = name; + } + + /** + * Validate the rest server parameters. + * + * @return the result of the validation + */ + @Override + public GroupValidationResult validate() { + final GroupValidationResult validationResult = new GroupValidationResult(this); + if (!ParameterValidationUtils.validateStringParameter(host)) { + validationResult.setResult("host", ValidationStatus.INVALID, + "must be a non-blank string containing hostname/ipaddress of the pap rest server"); + } + if (!ParameterValidationUtils.validateStringParameter(userName)) { + validationResult.setResult("userName", ValidationStatus.INVALID, + "must be a non-blank string containing userName for pap rest server credentials"); + } + if (!ParameterValidationUtils.validateStringParameter(password)) { + validationResult.setResult("password", ValidationStatus.INVALID, + "must be a non-blank string containing password for pap rest server credentials"); + } + if (!ParameterValidationUtils.validateIntParameter(port)) { + validationResult.setResult("port", ValidationStatus.INVALID, + "must be a positive integer containing port of the pap rest server"); + } + return validationResult; + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java b/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java new file mode 100644 index 00000000..d67d0d5b --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/HealthCheckProvider.java @@ -0,0 +1,52 @@ +/*- + * ============LICENSE_START======================================================= + * 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.pap.main.rest; + +import org.onap.policy.common.endpoints.report.HealthCheckReport; +import org.onap.policy.pap.main.startstop.PapActivator; + +/** + * Class to fetch health check of PAP service. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class HealthCheckProvider { + + private static final String NOT_ALIVE = "not alive"; + private static final String ALIVE = "alive"; + private static final String URL = "self"; + private static final String NAME = "Policy PAP"; + + /** + * Performs the health check of PAP service. + * + * @return Report containing health check status + */ + public HealthCheckReport performHealthCheck() { + final HealthCheckReport report = new HealthCheckReport(); + report.setName(NAME); + report.setUrl(URL); + report.setHealthy(PapActivator.isAlive()); + report.setCode(PapActivator.isAlive() ? 200 : 500); + report.setMessage(PapActivator.isAlive() ? ALIVE : NOT_ALIVE); + return report; + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestController.java b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestController.java new file mode 100644 index 00000000..226dc356 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestController.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * 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.pap.main.rest; + +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; + +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.common.endpoints.report.HealthCheckReport; + +/** + * Class to provide REST endpoints for PAP component. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +@Path("/") +@Api +@Produces(MediaType.APPLICATION_JSON) +@SwaggerDefinition(info = @Info(description = "Policy Pap Service", version = "v1.0", title = "Policy Pap"), + consumes = { MediaType.APPLICATION_JSON }, produces = { MediaType.APPLICATION_JSON }, + schemes = { SwaggerDefinition.Scheme.HTTP }, + tags = { @Tag(name = "policy-pap", description = "Policy Pap Service Operations") }) +public class PapRestController { + + @GET + @Path("healthcheck") + @Produces(MediaType.APPLICATION_JSON) + @ApiOperation(value = "Perform a system healthcheck", notes = "Provides healthy status of the Policy Pap component", + response = HealthCheckReport.class) + public Response healthcheck() { + return Response.status(Response.Status.OK).entity(new HealthCheckProvider().performHealthCheck()).build(); + } +} diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java new file mode 100644 index 00000000..39f65f24 --- /dev/null +++ b/main/src/main/java/org/onap/policy/pap/main/rest/PapRestServer.java @@ -0,0 +1,141 @@ +/*- + * ============LICENSE_START======================================================= + * 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.pap.main.rest; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.onap.policy.common.capabilities.Startable; +import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.pap.main.parameters.RestServerParameters; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to manage life cycle of PAP rest server. + * + * @author Ram Krishna Verma (ram.krishna.verma@est.tech) + */ +public class PapRestServer implements Startable { + + private static final Logger LOGGER = LoggerFactory.getLogger(PapRestServer.class); + + private static final String SEPARATOR = "."; + private static final String HTTP_SERVER_SERVICES = "http.server.services"; + + private List<HttpServletServer> servers = new ArrayList<>(); + + private RestServerParameters restServerParameters; + + /** + * Constructor for instantiating PapRestServer. + * + * @param restServerParameters the rest server parameters + */ + public PapRestServer(final RestServerParameters restServerParameters) { + this.restServerParameters = restServerParameters; + } + + /** + * {@inheritDoc}. + */ + @Override + public boolean start() { + try { + servers = HttpServletServer.factory.build(getServerProperties()); + for (final HttpServletServer server : servers) { + server.start(); + } + } catch (final Exception exp) { + LOGGER.error("Failed to start pap http server", exp); + return false; + } + return true; + } + + /** + * Creates the server properties object using restServerParameters. + * + * @return the properties object + */ + private Properties getServerProperties() { + final Properties props = new Properties(); + props.setProperty(HTTP_SERVER_SERVICES, restServerParameters.getName()); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".host", + restServerParameters.getHost()); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".port", + Integer.toString(restServerParameters.getPort())); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".restClasses", + PapRestController.class.getCanonicalName()); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".managed", "false"); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".swagger", "true"); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".userName", + restServerParameters.getUserName()); + props.setProperty(HTTP_SERVER_SERVICES + SEPARATOR + restServerParameters.getName() + ".password", + restServerParameters.getPassword()); + return props; + } + + /** + * {@inheritDoc}. + */ + @Override + public boolean stop() { + for (final HttpServletServer server : servers) { + try { + server.stop(); + } catch (final Exception exp) { + LOGGER.error("Failed to stop pap http server", exp); + } + } + return true; + } + + /** + * {@inheritDoc}. + */ + @Override + public void shutdown() { + stop(); + } + + /** + * {@inheritDoc}. + */ + @Override + public boolean isAlive() { + return !servers.isEmpty(); + } + + /** + * {@inheritDoc}. + */ + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("PapRestServer [servers="); + builder.append(servers); + builder.append("]"); + return builder.toString(); + } + +} diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java index 63d41a02..602e04df 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java @@ -25,8 +25,8 @@ import java.util.Arrays; import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.parameters.PapParameterGroup; import org.onap.policy.pap.main.parameters.PapParameterHandler; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class initiates ONAP Policy Framework PAP component. @@ -34,7 +34,8 @@ import org.slf4j.ext.XLoggerFactory; * @author Ram Krishna Verma (ram.krishna.verma@est.tech) */ public class Main { - private static final XLogger LOGGER = XLoggerFactory.getXLogger(Main.class); + + private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); private PapActivator activator; private PapParameterGroup parameterGroup; diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java index 8aec2adf..b1674efb 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java @@ -23,8 +23,9 @@ package org.onap.policy.pap.main.startstop; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.pap.main.PolicyPapException; import org.onap.policy.pap.main.parameters.PapParameterGroup; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; +import org.onap.policy.pap.main.rest.PapRestServer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class wraps a distributor so that it can be activated as a complete service together with all its pap and @@ -33,13 +34,12 @@ import org.slf4j.ext.XLoggerFactory; * @author Ram Krishna Verma (ram.krishna.verma@est.tech) */ public class PapActivator { - // The logger for this class - private static final XLogger LOGGER = XLoggerFactory.getXLogger(PapActivator.class); - // The parameters of this policy pap activator - private final PapParameterGroup papParameterGroup; + private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class); + private final PapParameterGroup papParameterGroup; private static boolean alive = false; + private PapRestServer restServer; /** * Instantiate the activator for policy pap as a complete service. @@ -58,6 +58,7 @@ public class PapActivator { public void initialize() throws PolicyPapException { try { LOGGER.debug("Policy pap starting as a service . . ."); + startPapRestServer(); registerToParameterService(papParameterGroup); PapActivator.setAlive(true); LOGGER.debug("Policy pap started as a service"); @@ -77,6 +78,8 @@ public class PapActivator { deregisterToParameterService(papParameterGroup); PapActivator.setAlive(false); + // Stop the pap rest server + restServer.stop(); } catch (final Exception exp) { LOGGER.error("Policy pap service termination failed", exp); throw new PolicyPapException(exp.getMessage(), exp); @@ -127,4 +130,17 @@ public class PapActivator { public static void setAlive(final boolean status) { alive = status; } + + /** + * Starts the pap rest server using configuration parameters. + * + * @throws PolicyPapException if server start fails + */ + private void startPapRestServer() throws PolicyPapException { + papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName()); + restServer = new PapRestServer(papParameterGroup.getRestServerParameters()); + if (!restServer.start()) { + throw new PolicyPapException("Failed to start pap rest server. Check log for more details..."); + } + } } |