From 00ee73f723560c825818eab41a4d3db9785627d4 Mon Sep 17 00:00:00 2001 From: "a.sreekumar" Date: Tue, 16 Apr 2019 09:40:48 +0000 Subject: Changes for RestServer support and some sonar fix 1) Changes to add RestServer to the PDP-A for healthcheck. 2) Adding Junit testcases. 3) Fixing some sonar issues. 4) Fixing checkstyle issues. Change-Id: I03530190c733432d219284dda9d9d4cc32ab0773 Issue-ID: POLICY-1632 Signed-off-by: a.sreekumar --- .../apex/services/onappf/ApexStarterActivator.java | 25 +++- .../apex/services/onappf/ApexStarterConstants.java | 4 + .../apex/services/onappf/ApexStarterMain.java | 12 +- .../services/onappf/comm/PdpStatusPublisher.java | 11 +- .../services/onappf/handler/ApexEngineHandler.java | 19 ++- .../services/onappf/handler/PdpMessageHandler.java | 12 +- .../handler/PdpStateChangeMessageHandler.java | 22 +-- .../onappf/handler/PdpUpdateMessageHandler.java | 49 +++---- .../parameters/ApexStarterParameterGroup.java | 2 +- .../onappf/parameters/RestServerParameters.java | 52 +++++++ .../services/onappf/rest/ApexStarterAafFilter.java | 39 ++++++ .../onappf/rest/ApexStarterRestServer.java | 150 +++++++++++++++++++++ .../services/onappf/rest/HealthCheckProvider.java | 58 ++++++++ .../onappf/rest/HealthCheckRestControllerV1.java | 86 ++++++++++++ 14 files changed, 483 insertions(+), 58 deletions(-) create mode 100644 services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/RestServerParameters.java create mode 100644 services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/ApexStarterAafFilter.java create mode 100644 services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/ApexStarterRestServer.java create mode 100644 services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/HealthCheckProvider.java create mode 100644 services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/HealthCheckRestControllerV1.java (limited to 'services/services-onappf/src/main/java/org') diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterActivator.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterActivator.java index 0da554d75..e08b5b344 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterActivator.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterActivator.java @@ -33,6 +33,7 @@ import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.apex.services.onappf.exception.ApexStarterRunTimeException; import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; +import org.onap.policy.apex.services.onappf.rest.ApexStarterRestServer; import org.onap.policy.common.endpoints.event.comm.TopicEndpoint; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.common.endpoints.event.comm.TopicSource; @@ -56,6 +57,7 @@ public class ApexStarterActivator { private List topicSinks;// topics to which apex-pdp sends pdp status private List topicSources; // topics to which apex-pdp listens to for messages from pap. private static final String[] MSG_TYPE_NAMES = { "messageName" }; + /** * Listens for messages on the topic, decodes them into a message, and then dispatches them. */ @@ -66,10 +68,21 @@ public class ApexStarterActivator { */ private ServiceManager manager; + /** + * The ApexStarter REST API server. + */ + private ApexStarterRestServer restServer; + @Getter @Setter(lombok.AccessLevel.PRIVATE) private volatile boolean alive = false; + /** + * Instantiate the activator for onappf PDP-A. + * + * @param apexStarterParameterGroup the parameters for the onappf PDP-A service + * @param topicProperties properties used to configure the topics + */ public ApexStarterActivator(final ApexStarterParameterGroup apexStarterParameterGroup, final Properties topicProperties) { @@ -118,7 +131,15 @@ public class ApexStarterActivator { () -> msgDispatcher.unregister(PdpMessageType.PDP_STATE_CHANGE.name())) .addAction("Message Dispatcher", () -> registerMsgDispatcher(), - () -> unregisterMsgDispatcher()); + () -> unregisterMsgDispatcher()) + .addAction("Create REST server", + () -> restServer = + new ApexStarterRestServer(apexStarterParameterGroup.getRestServerParameters()), + () -> restServer = null) + .addAction("Rest Server", + () -> restServer.start(), + () -> restServer.stop()); + // @formatter:on } @@ -177,7 +198,7 @@ public class ApexStarterActivator { /** * Get the parameters used by the activator. * - * @return the parameters of the activator + * @return apexStarterParameterGroup the parameters of the activator */ public ApexStarterParameterGroup getParameterGroup() { return apexStarterParameterGroup; diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterConstants.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterConstants.java index c6dbf5fe3..2481f4ef7 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterConstants.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterConstants.java @@ -31,4 +31,8 @@ public class ApexStarterConstants { public static final String REG_PDP_STATUS_PUBLISHER = "object:pdp/status/publisher"; public static final String REG_APEX_PDP_TOPIC_SINKS = "object:apex/pdp/topic/sinks"; public static final String REG_APEX_ENGINE_HANDLER = "object:engine/apex/handler"; + + private ApexStarterConstants() { + super(); + } } diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java index 22611c51b..4813eb356 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java @@ -38,6 +38,8 @@ import org.slf4j.LoggerFactory; */ public class ApexStarterMain { + private static final String APEX_STARTER_FAIL_MSG = "start of services-onappf failed"; + private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterMain.class); private ApexStarterActivator activator; @@ -48,7 +50,7 @@ public class ApexStarterMain { * * @param args the command line arguments */ - ApexStarterMain(final String[] args) { + public ApexStarterMain(final String[] args) { LOGGER.info("In ApexStarter with parameters " + Arrays.toString(args)); // Check the arguments @@ -63,7 +65,7 @@ public class ApexStarterMain { // Validate that the arguments are sane arguments.validate(); } catch (final ApexStarterException e) { - LOGGER.error("start of ApexStarter failed", e); + LOGGER.error(APEX_STARTER_FAIL_MSG, e); return; } @@ -71,7 +73,7 @@ public class ApexStarterMain { try { parameterGroup = new ApexStarterParameterHandler().getParameters(arguments); } catch (final Exception e) { - LOGGER.error("start of ApexStarter failed", e); + LOGGER.error(APEX_STARTER_FAIL_MSG, e); return; } @@ -83,7 +85,7 @@ public class ApexStarterMain { topicProperties.load(stream); } } catch (final Exception e) { - LOGGER.error("start of ApexStarter failed", e); + LOGGER.error(APEX_STARTER_FAIL_MSG, e); return; } @@ -108,7 +110,7 @@ public class ApexStarterMain { /** * Get the parameters specified in JSON. * - * @return the parameters + * @return parameterGroup the parameters */ public ApexStarterParameterGroup getParameters() { return parameterGroup; diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpStatusPublisher.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpStatusPublisher.java index 211fe1316..9a61084ca 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpStatusPublisher.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpStatusPublisher.java @@ -45,11 +45,10 @@ public class PdpStatusPublisher extends TimerTask { private long interval; /** - * Constructor for instantiating PdpStatusPublisher + * Constructor for instantiating PdpStatusPublisher. * - * @param pdpStatus - * @param topicSinks - * @param apexStarterParameterGroup + * @param topicSinks the topic sinks + * @param interval time interval to send pdp status */ public PdpStatusPublisher(final List topicSinks, final long interval) { this.topicSinkClient = new TopicSinkClient(topicSinks.get(0)); @@ -76,7 +75,7 @@ public class PdpStatusPublisher extends TimerTask { /** * Get the current time interval used by the timer task. * - * @return interval + * @return interval the current time interval */ public long getInterval() { return interval; @@ -84,6 +83,8 @@ public class PdpStatusPublisher extends TimerTask { /** * Method to send pdp status message to pap on demand. + * + * @param pdpStatus the pdp status */ public void send(final PdpStatus pdpStatus) { topicSinkClient.send(pdpStatus); diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java index 83e1b77c6..85d666c2b 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java @@ -49,9 +49,8 @@ public class ApexEngineHandler { /** * Constructs the object. Extracts the apex config and model files and instantiates the apex engine. * - * @param properties - * @return ApexEngineHandler - * @throws ApexStarterException + * @param properties the properties which contains the policies and configurations received from pap + * @throws ApexStarterException if the apex engine instantiation failed using the properties passed */ public ApexEngineHandler(final String properties) throws ApexStarterException { @@ -76,11 +75,11 @@ public class ApexEngineHandler { } /** - * Method to create the policy model file + * Method to create the policy model file. * - * @param policyModel - * @param modelFilePath - * @throws ApexStarterException + * @param fileContent the content of the file + * @param fileName the name of the file + * @throws ApexStarterException if the file creation failed */ private String createFile(final String fileContent, final String fileName) throws ApexStarterException { try { @@ -94,10 +93,16 @@ public class ApexEngineHandler { } } + /** + * Method to check whether the apex engine is running or not. + */ public boolean isApexEngineRunning() { return null != apexMain; } + /** + * Method to shut down the apex engine. + */ public void shutdown() throws ApexStarterException { try { LOGGER.debug("Shutting down apex engine."); diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java index e3f0c04a1..b72ef805f 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java @@ -44,12 +44,12 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifi public class PdpMessageHandler { /** - * Method to create PdpStatus message from the parameters which will be saved to the context + * Method to create PdpStatus message from the parameters which will be saved to the context. * * @param instanceId instance id of apex pdp * @param pdpStatusParameters pdp status parameters read from the configuration file * - * @return PdpStatus the pdp status message + * @return pdpStatus the pdp status message */ public PdpStatus createPdpStatusFromParameters(final String instanceId, final PdpStatusParameters pdpStatusParameters) { @@ -66,8 +66,8 @@ public class PdpMessageHandler { /** * Method to get supported policy types from the parameters. * - * @param pdpStatusParameters - * @return list of PolicyTypeIdent + * @param pdpStatusParameters pdp status parameters + * @return supportedPolicyTypes list of PolicyTypeIdent */ private List getSupportedPolicyTypesFromParameters( final PdpStatusParameters pdpStatusParameters) { @@ -104,9 +104,7 @@ public class PdpMessageHandler { /** * Method to get a final pdp status when the apex started is shutting down. * - * @param policies list of ToscaPolicy - * - * @return PdpStatus + * @return PdpStatus the pdp status message */ public PdpStatus getTerminatedPdpStatus() { final PdpStatus pdpStatusInContext = Registry.get(ApexStarterConstants.REG_PDP_STATUS_OBJECT, PdpStatus.class); diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java index 3a186aed6..d2c7786ca 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java @@ -32,6 +32,8 @@ import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.enums.PdpResponseStatus; import org.onap.policy.models.pdp.enums.PdpState; import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class supports the handling of pdp state change messages. @@ -40,6 +42,8 @@ import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy; */ public class PdpStateChangeMessageHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(PdpStateChangeMessageHandler.class); + /** * Method which handles a pdp state change event from PAP. * @@ -72,10 +76,10 @@ public class PdpStateChangeMessageHandler { /** * Method to handle when the new state from pap is active. * - * @param pdpStateChangeMsg - * @param pdpStatusContext - * @param pdpMessageHandler - * @return pdpResponseDetails + * @param pdpStateChangeMsg pdp state change message + * @param pdpStatusContext pdp status object in memory + * @param pdpMessageHandler the pdp message handler + * @return pdpResponseDetails pdp response */ private PdpResponseDetails handleActiveState(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) { @@ -98,6 +102,7 @@ public class PdpStateChangeMessageHandler { PdpResponseStatus.SUCCESS, "Apex engine started. State changed to active."); pdpStatusContext.setState(PdpState.ACTIVE); } catch (final ApexStarterException e) { + LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e); pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage()); } @@ -109,10 +114,10 @@ public class PdpStateChangeMessageHandler { /** * Method to handle when the new state from pap is passive. * - * @param pdpStateChangeMsg - * @param pdpStatusContext - * @param pdpMessageHandler - * @return pdpResponseDetails + * @param pdpStateChangeMsg pdp state change message + * @param pdpStatusContext pdp status object in memory + * @param pdpMessageHandler the pdp message handler + * @return pdpResponseDetails pdp response */ private PdpResponseDetails handlePassiveState(final PdpStateChange pdpStateChangeMsg, final PdpStatus pdpStatusContext, final PdpMessageHandler pdpMessageHandler) { @@ -128,6 +133,7 @@ public class PdpStateChangeMessageHandler { PdpResponseStatus.SUCCESS, "Apex pdp state changed from Active to Passive."); pdpStatusContext.setState(PdpState.PASSIVE); } catch (final Exception e) { + LOGGER.error("Stopping apex engine failed. State cannot be changed to Passive.", e); pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpStateChangeMsg.getRequestId(), PdpResponseStatus.FAIL, "Stopping apex engine failed. State cannot be changed to Passive." + e.getMessage()); diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java index 3ad5477cd..599de8625 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java @@ -32,6 +32,8 @@ import org.onap.policy.models.pdp.concepts.PdpStatus; import org.onap.policy.models.pdp.concepts.PdpUpdate; import org.onap.policy.models.pdp.enums.PdpResponseStatus; import org.onap.policy.models.pdp.enums.PdpState; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * This class supports the handling of pdp update messages. @@ -40,6 +42,8 @@ import org.onap.policy.models.pdp.enums.PdpState; */ public class PdpUpdateMessageHandler { + private static final Logger LOGGER = LoggerFactory.getLogger(PdpUpdateMessageHandler.class); + /** * Method which handles a pdp update event from PAP. * @@ -52,7 +56,7 @@ public class PdpUpdateMessageHandler { if (pdpUpdateMsg.appliesTo(pdpStatusContext.getName(), pdpStatusContext.getPdpGroup(), pdpStatusContext.getPdpSubgroup())) { final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER); - if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext, pdpStatusPublisher.getInterval())) { + if (checkIfAlreadyHandled(pdpUpdateMsg, pdpStatusContext)) { pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), PdpResponseStatus.SUCCESS, "Pdp already updated"); } else { @@ -65,8 +69,7 @@ public class PdpUpdateMessageHandler { pdpStatusContext .setPolicies(new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies())); if (pdpStatusContext.getState().equals(PdpState.ACTIVE)) { - pdpResponseDetails = - startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler, pdpStatusContext); + pdpResponseDetails = startOrStopApexEngineBasedOnPolicies(pdpUpdateMsg, pdpMessageHandler); } Registry.registerOrReplace(ApexStarterConstants.REG_APEX_TOSCA_POLICY_LIST, pdpUpdateMsg.getPolicies()); if (null == pdpResponseDetails) { @@ -88,13 +91,13 @@ public class PdpUpdateMessageHandler { * active, if PAP sends PdpUpdate with empty policies list, stop apex engine, or, if there is a change in policies, * stop the current running policies and the deploy the new ones. * - * @param pdpUpdateMsg - * @param pdpMessageHandler - * @param pdpStatusContext - * @return pdpResponseDetails + * @param pdpUpdateMsg the pdp update message from pap + * @param pdpMessageHandler pdp message handler + * @param pdpStatusContext the pdp status object in memory + * @return pdpResponseDetails the pdp response */ private PdpResponseDetails startOrStopApexEngineBasedOnPolicies(final PdpUpdate pdpUpdateMsg, - final PdpMessageHandler pdpMessageHandler, final PdpStatus pdpStatusContext) { + final PdpMessageHandler pdpMessageHandler) { PdpResponseDetails pdpResponseDetails = null; if (pdpUpdateMsg.getPolicies().isEmpty()) { final ApexEngineHandler apexEngineHandler = Registry.get(ApexStarterConstants.REG_APEX_ENGINE_HANDLER); @@ -102,6 +105,7 @@ public class PdpUpdateMessageHandler { try { apexEngineHandler.shutdown(); } catch (final ApexStarterException e) { + LOGGER.error("Pdp update failed as the policies couldn't be undeployed.", e); pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), PdpResponseStatus.FAIL, "Pdp update failed as the policies couldn't be undeployed."); } @@ -118,6 +122,7 @@ public class PdpUpdateMessageHandler { pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), PdpResponseStatus.SUCCESS, "Apex engine started and policies are running."); } catch (final ApexStarterException e) { + LOGGER.error("Apex engine service running failed. ", e); pdpResponseDetails = pdpMessageHandler.createPdpResonseDetails(pdpUpdateMsg.getRequestId(), PdpResponseStatus.FAIL, "Apex engine service running failed. " + e.getMessage()); } @@ -126,26 +131,24 @@ public class PdpUpdateMessageHandler { } /** - * Method checks of the Pdp update message is already handled by checking the values in the context. + * Method checks if the Pdp update message is already handled by checking the values in the context. * * @param pdpUpdateMsg pdp update message received from pap * @param pdpStatusContext values saved in context memory - * @param interval the current interval in which the pdp status publisher is sending the heartbeats * @return boolean flag which tells if the information is same or not */ - private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext, - final long interval) { - return null != pdpStatusContext.getPdpGroup() - && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup()) - && null != pdpStatusContext.getPdpSubgroup() - && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup()) - && null != pdpStatusContext.getPolicies() - && new PdpMessageHandler().getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()) - .equals(pdpStatusContext.getPolicies()) - && (null == pdpUpdateMsg.getPdpHeartbeatIntervalMs() - || (null != pdpUpdateMsg.getPdpHeartbeatIntervalMs() - && pdpUpdateMsg.getPdpHeartbeatIntervalMs() > 0 - && interval == pdpUpdateMsg.getPdpHeartbeatIntervalMs())); + private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) { + + if (null != pdpStatusContext.getPdpGroup() + && pdpStatusContext.getPdpGroup().equals(pdpUpdateMsg.getPdpGroup())) { + return true; + } else if (null != pdpStatusContext.getPdpSubgroup() + && pdpStatusContext.getPdpSubgroup().equals(pdpUpdateMsg.getPdpSubgroup())) { + return true; + } else { + return null != pdpStatusContext.getPolicies() && new PdpMessageHandler() + .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies()); + } } /** diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ApexStarterParameterGroup.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ApexStarterParameterGroup.java index 523e37337..db51803be 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ApexStarterParameterGroup.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ApexStarterParameterGroup.java @@ -35,7 +35,7 @@ import org.onap.policy.common.parameters.annotations.NotNull; @NotBlank @Getter public class ApexStarterParameterGroup extends ParameterGroupImpl { - + private RestServerParameters restServerParameters; private PdpStatusParameters pdpStatusParameters; /** diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/RestServerParameters.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/RestServerParameters.java new file mode 100644 index 000000000..fafca0c9e --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/RestServerParameters.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.apex.services.onappf.parameters; + +import lombok.Getter; + +import org.onap.policy.common.parameters.ParameterGroupImpl; +import org.onap.policy.common.parameters.annotations.Min; +import org.onap.policy.common.parameters.annotations.NotBlank; +import org.onap.policy.common.parameters.annotations.NotNull; + +/** + * Class to hold all parameters needed for services-onappf rest server. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +@NotNull +@NotBlank +@Getter +public class RestServerParameters extends ParameterGroupImpl { + private String host; + + @Min(value = 1) + private int port; + + private String userName; + private String password; + private boolean https; + private boolean aaf; + + public RestServerParameters() { + super("RestServerParameters"); + } +} diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/ApexStarterAafFilter.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/ApexStarterAafFilter.java new file mode 100644 index 000000000..2745a79f9 --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/ApexStarterAafFilter.java @@ -0,0 +1,39 @@ +/*- + * ============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.apex.services.onappf.rest; + +import org.onap.policy.common.endpoints.http.server.aaf.AafGranularAuthFilter; + +/** + * Class to manage aaf filters for services-onappf component. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class ApexStarterAafFilter extends AafGranularAuthFilter { + + public static final String AAF_NODETYPE = "policy-apex-pdp"; + public static final String AAF_ROOT_PERMISSION = DEFAULT_NAMESPACE + "." + AAF_NODETYPE; + + @Override + public String getPermissionTypeRoot() { + return AAF_ROOT_PERMISSION; + } +} diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/ApexStarterRestServer.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/ApexStarterRestServer.java new file mode 100644 index 000000000..02e1fcf70 --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/ApexStarterRestServer.java @@ -0,0 +1,150 @@ +/*- + * ============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.apex.services.onappf.rest; + +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.onap.policy.apex.services.onappf.parameters.RestServerParameters; +import org.onap.policy.common.capabilities.Startable; +import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to manage life cycle of services-onappf rest server. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class ApexStarterRestServer implements Startable { + private static final Logger LOGGER = LoggerFactory.getLogger(ApexStarterRestServer.class); + + private List servers = new ArrayList<>(); + + private RestServerParameters restServerParameters; + + /** + * Constructor for instantiating ApexStarterRestServer. + * + * @param restServerParameters the rest server parameters + */ + public ApexStarterRestServer(final RestServerParameters restServerParameters) { + this.restServerParameters = restServerParameters; + } + + /** + * {@inheritDoc}. + */ + @Override + public boolean start() { + try { + servers = HttpServletServer.factory.build(getServerProperties()); + for (final HttpServletServer server : servers) { + if (server.isAaf()) { + server.addFilterClass(null, ApexStarterAafFilter.class.getCanonicalName()); + } + server.start(); + } + } catch (final Exception exp) { + LOGGER.error("Failed to start services-onappf 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(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, restServerParameters.getName()); + + final String svcpfx = + PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + restServerParameters.getName(); + + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HOST_SUFFIX, restServerParameters.getHost()); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_PORT_SUFFIX, + Integer.toString(restServerParameters.getPort())); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_REST_CLASSES_SUFFIX, + HealthCheckRestControllerV1.class.getCanonicalName()); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false"); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "true"); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_USERNAME_SUFFIX, + restServerParameters.getUserName()); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_AUTH_PASSWORD_SUFFIX, + restServerParameters.getPassword()); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_HTTPS_SUFFIX, + String.valueOf(restServerParameters.isHttps())); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_AAF_SUFFIX, + String.valueOf(restServerParameters.isAaf())); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, + GsonMessageBodyHandler.class.getName()); + return props; + } + + /** + * {@inheritDoc}. + */ + @Override + public boolean stop() { + for (final HttpServletServer server : servers) { + try { + server.stop(); + } catch (final Exception exp) { + LOGGER.error("Failed to stop services-onappf 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("ApexStarterRestServer [servers="); + builder.append(servers); + builder.append("]"); + return builder.toString(); + } +} diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/HealthCheckProvider.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/HealthCheckProvider.java new file mode 100644 index 000000000..12a34ef57 --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/HealthCheckProvider.java @@ -0,0 +1,58 @@ +/*- + * ============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.apex.services.onappf.rest; + +import org.onap.policy.apex.services.onappf.ApexStarterActivator; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.onap.policy.common.endpoints.report.HealthCheckReport; +import org.onap.policy.common.utils.services.Registry; + +/** + * Class to fetch health check of ApexStarter service. + * + * @author Ajith Sreekumar (ajith.sreekumar@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 PDP-A"; + + /** + * 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); + + final boolean alive = + Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class).isAlive(); + + report.setHealthy(alive); + report.setCode(alive ? 200 : 500); + report.setMessage(alive ? ALIVE : NOT_ALIVE); + return report; + } +} diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/HealthCheckRestControllerV1.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/HealthCheckRestControllerV1.java new file mode 100644 index 000000000..53efa5940 --- /dev/null +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/rest/HealthCheckRestControllerV1.java @@ -0,0 +1,86 @@ +/*- + * ============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.apex.services.onappf.rest; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.Authorization; +import io.swagger.annotations.BasicAuthDefinition; +import io.swagger.annotations.Info; +import io.swagger.annotations.SecurityDefinition; +import io.swagger.annotations.SwaggerDefinition; +import io.swagger.annotations.Tag; + +import java.net.HttpURLConnection; + +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 services-onappf component health check. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +//@formatter:off +@Path("/policy/apex-pdp/v1") +@Api(value = "PDP-A API") +@Produces(MediaType.APPLICATION_JSON) +@SwaggerDefinition( + info = @Info(description = + "PDP-A is responsible for making policy decisions and for managing the administrative" + + " state of the PDPs as directed by PolicyAdministration", version = "v1.0", + title = "PDP-A"), + consumes = {MediaType.APPLICATION_JSON}, + produces = {MediaType.APPLICATION_JSON}, + schemes = {SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS}, + tags = {@Tag(name = "PDP-A", description = "PDP-A Operations")}, + securityDefinition = @SecurityDefinition(basicAuthDefinitions = {@BasicAuthDefinition(key = "basicAuth")})) +//@formatter:on +public class HealthCheckRestControllerV1 { + + public static final String AUTHORIZATION_TYPE = "basicAuth"; + + public static final int AUTHENTICATION_ERROR_CODE = HttpURLConnection.HTTP_UNAUTHORIZED; + public static final int AUTHORIZATION_ERROR_CODE = HttpURLConnection.HTTP_FORBIDDEN; + public static final int SERVER_ERROR_CODE = HttpURLConnection.HTTP_INTERNAL_ERROR; + + public static final String AUTHENTICATION_ERROR_MESSAGE = "Authentication Error"; + public static final String AUTHORIZATION_ERROR_MESSAGE = "Authorization Error"; + public static final String SERVER_ERROR_MESSAGE = "Internal Server Error"; + + @GET + @Path("healthcheck") + @ApiOperation(value = "Perform healthcheck", notes = "Returns healthy status of the PDP-A component", + response = HealthCheckReport.class, authorizations = @Authorization(value = AUTHORIZATION_TYPE)) + @ApiResponses(value = { @ApiResponse(code = AUTHENTICATION_ERROR_CODE, message = AUTHENTICATION_ERROR_MESSAGE), + @ApiResponse(code = AUTHORIZATION_ERROR_CODE, message = AUTHORIZATION_ERROR_MESSAGE), + @ApiResponse(code = SERVER_ERROR_CODE, message = SERVER_ERROR_MESSAGE) }) + public Response healthcheck() { + return Response.status(Response.Status.OK).entity(new HealthCheckProvider().performHealthCheck()).build(); + } +} -- cgit 1.2.3-korg