diff options
Diffstat (limited to 'services')
26 files changed, 939 insertions, 71 deletions
diff --git a/services/services-engine/pom.xml b/services/services-engine/pom.xml index 2b68f2731..24872d536 100644 --- a/services/services-engine/pom.xml +++ b/services/services-engine/pom.xml @@ -55,12 +55,6 @@ <version>${project.version}</version> </dependency> <dependency> - <groupId>org.onap.policy.apex-pdp.context</groupId> - <artifactId>context-test-utils</artifactId> - <version>${project.version}</version> - <scope>test</scope> - </dependency> - <dependency> <groupId>commons-cli</groupId> <artifactId>commons-cli</artifactId> </dependency> diff --git a/services/services-onappf/pom.xml b/services/services-onappf/pom.xml index 81df89483..92f07bfac 100644 --- a/services/services-onappf/pom.xml +++ b/services/services-onappf/pom.xml @@ -70,6 +70,10 @@ <groupId>org.glassfish.hk2.external</groupId> <artifactId>javax.inject</artifactId> </exclusion> + <exclusion> + <groupId>org.glassfish.jersey.core</groupId> + <artifactId>jersey-client</artifactId> + </exclusion> </exclusions> <version>${version.policy.common}</version> </dependency> @@ -79,6 +83,11 @@ <version>${version.jersey}</version> </dependency> <dependency> + <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-carrier</groupId> + <artifactId>plugins-event-carrier-restserver</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> <groupId>org.onap.policy.common</groupId> <artifactId>utils-test</artifactId> <version>${version.policy.common}</version> 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<TopicSink> topicSinks;// topics to which apex-pdp sends pdp status private List<TopicSource> 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<TopicSink> 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<ToscaPolicyTypeIdentifier> 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<HttpServletServer> 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(); + } +} diff --git a/services/services-onappf/src/main/resources/config/OnapPfConfig.json b/services/services-onappf/src/main/resources/config/OnapPfConfig.json index 5684f1d4e..3efaa8cff 100644 --- a/services/services-onappf/src/main/resources/config/OnapPfConfig.json +++ b/services/services-onappf/src/main/resources/config/OnapPfConfig.json @@ -1,5 +1,12 @@ { "name":"OnapPfParameterGroup", + "restServerParameters": { + "host": "0.0.0.0", + "port": 6969, + "userName": "healthcheck", + "password": "zb!XztG34", + "https": true + }, "pdpStatusParameters":{ "timeIntervalMs": 120000, "pdpType":"apex", diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java index 34025a1ad..c6ed44556 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java @@ -40,8 +40,6 @@ import org.junit.Test; import org.onap.policy.apex.services.onappf.ApexStarterActivator; import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; import org.onap.policy.apex.services.onappf.ApexStarterConstants; -import org.onap.policy.apex.services.onappf.comm.PdpStateChangeListener; -import org.onap.policy.apex.services.onappf.comm.PdpUpdateListener; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; @@ -65,6 +63,13 @@ public class TestPdpStateChangeListener { private static final String TOPIC = "my-topic"; private ApexStarterActivator activator; + /** + * Method for setup before each test. + * + * @throws ApexStarterException if some error occurs while starting up the apex starter + * @throws FileNotFoundException if the file is missing + * @throws IOException if IO exception occurs + */ @Before public void setUp() throws ApexStarterException, FileNotFoundException, IOException { pdpUpdateMessageListener = new PdpUpdateListener(); @@ -112,8 +117,10 @@ public class TestPdpStateChangeListener { } /** - * @param instance - * @return + * Method to initiate a PdpUpdate. + * + * @param instance the instance id + * @return PdpUpdate the pdp update message */ private PdpUpdate performPdpUpdate(final String instance) { final PdpUpdate pdpUpdateMsg = new PdpUpdate(); diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java index 921cfd283..ebe0fc957 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java @@ -40,7 +40,6 @@ import org.junit.Test; import org.onap.policy.apex.services.onappf.ApexStarterActivator; import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; import org.onap.policy.apex.services.onappf.ApexStarterConstants; -import org.onap.policy.apex.services.onappf.comm.PdpUpdateListener; import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler; import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; @@ -62,6 +61,13 @@ public class TestPdpUpdateListener { private static final String TOPIC = "my-topic"; private ApexStarterActivator activator; + /** + * Method for setup before each test. + * + * @throws ApexStarterException if some error occurs while starting up the apex starter + * @throws FileNotFoundException if the file is missing + * @throws IOException if IO exception occurs + */ @Before public void setUp() throws ApexStarterException, FileNotFoundException, IOException { Registry.newRegistry(); diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.java index e4946dd2b..7e668e9b2 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.java @@ -25,7 +25,6 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; -import org.onap.policy.apex.services.onappf.parameters.ToscaPolicyTypeIdentifierParameters; import org.onap.policy.common.parameters.ParameterGroup; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; @@ -48,6 +47,12 @@ public class CommonTestData { public static final String POLICY_VERSION = "0.0.1"; public static final List<ToscaPolicyTypeIdentifierParameters> SUPPORTED_POLICY_TYPES = Arrays.asList(getSupportedPolicyTypes(POLICY_NAME, POLICY_VERSION)); + private static final String REST_SERVER_PASSWORD = "zb!XztG34"; + private static final String REST_SERVER_USER = "healthcheck"; + private static final int REST_SERVER_PORT = 6969; + private static final String REST_SERVER_HOST = "0.0.0.0"; + private static final boolean REST_SERVER_HTTPS = true; + private static final boolean REST_SERVER_AAF = false; public static final Coder coder = new StandardCoder(); @@ -90,12 +95,34 @@ public class CommonTestData { final Map<String, Object> map = new TreeMap<>(); map.put("name", name); + map.put("restServerParameters", getRestServerParametersMap(false)); map.put("pdpStatusParameters", getPdpStatusParametersMap(false)); return map; } /** + * Returns a property map for a RestServerParameters map for test cases. + * + * @param isEmpty boolean value to represent that object created should be empty or not + * @return a property map suitable for constructing an object + */ + public Map<String, Object> getRestServerParametersMap(final boolean isEmpty) { + final Map<String, Object> map = new TreeMap<>(); + map.put("https", REST_SERVER_HTTPS); + map.put("aaf", REST_SERVER_AAF); + + if (!isEmpty) { + map.put("host", REST_SERVER_HOST); + map.put("port", REST_SERVER_PORT); + map.put("userName", REST_SERVER_USER); + map.put("password", REST_SERVER_PASSWORD); + } + + return map; + } + + /** * Returns a property map for a PdpStatusParameters map for test cases. * * @param isEmpty boolean value to represent that object created should be empty or not diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.java index c843c6787..dd9e83f52 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.java @@ -48,6 +48,7 @@ public class TestApexStarterParameterGroup { final ApexStarterParameterGroup apexStarterParameters = commonTestData.toObject( commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME), ApexStarterParameterGroup.class); + final RestServerParameters restServerParameters = apexStarterParameters.getRestServerParameters(); final PdpStatusParameters pdpStatusParameters = apexStarterParameters.getPdpStatusParameters(); final GroupValidationResult validationResult = apexStarterParameters.validate(); assertTrue(validationResult.isValid()); @@ -56,6 +57,12 @@ public class TestApexStarterParameterGroup { assertEquals(CommonTestData.PDP_TYPE, pdpStatusParameters.getPdpType()); assertEquals(CommonTestData.DESCRIPTION, pdpStatusParameters.getDescription()); assertEquals(CommonTestData.SUPPORTED_POLICY_TYPES, pdpStatusParameters.getSupportedPolicyTypes()); + assertEquals(restServerParameters.getHost(), apexStarterParameters.getRestServerParameters().getHost()); + assertEquals(restServerParameters.getPort(), apexStarterParameters.getRestServerParameters().getPort()); + assertEquals(restServerParameters.getUserName(), apexStarterParameters.getRestServerParameters().getUserName()); + assertEquals(restServerParameters.getPassword(), apexStarterParameters.getRestServerParameters().getPassword()); + assertTrue(apexStarterParameters.getRestServerParameters().isHttps()); + assertFalse(apexStarterParameters.getRestServerParameters().isAaf()); } @Test @@ -103,4 +110,19 @@ public class TestApexStarterParameterGroup { .contains("\"org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup\" INVALID, " + "parameter group has status INVALID")); } + + @Test + public void testApexStarterParameterGroupp_EmptyRestServerParameters() { + final Map<String, Object> map = + commonTestData.getApexStarterParameterGroupMap(CommonTestData.APEX_STARTER_GROUP_NAME); + map.put("restServerParameters", commonTestData.getRestServerParametersMap(true)); + + final ApexStarterParameterGroup apexStarterParameters = + commonTestData.toObject(map, ApexStarterParameterGroup.class); + final GroupValidationResult validationResult = apexStarterParameters.validate(); + assertFalse(validationResult.isValid()); + assertTrue(validationResult.getResult() + .contains("\"org.onap.policy.apex.services.onappf.parameters.RestServerParameters\" INVALID, " + + "parameter group has status INVALID")); + } } diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/CommonApexStarterRestServer.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/CommonApexStarterRestServer.java new file mode 100644 index 000000000..72db6e091 --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/CommonApexStarterRestServer.java @@ -0,0 +1,286 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.security.SecureRandom; +import java.util.Map; +import java.util.Properties; +import java.util.function.Function; + +import javax.net.ssl.SSLContext; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.glassfish.jersey.client.ClientProperties; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.onap.policy.apex.services.onappf.ApexStarterActivator; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.onap.policy.apex.services.onappf.ApexStarterMain; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.parameters.CommonTestData; +import org.onap.policy.common.gson.GsonMessageBodyHandler; +import org.onap.policy.common.utils.coder.Coder; +import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.network.NetworkUtil; +import org.onap.policy.common.utils.services.Registry; +import org.powermock.reflect.Whitebox; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Class to perform unit test of {@link ApexStarterRestServer}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class CommonApexStarterRestServer { + + private static final Logger LOGGER = LoggerFactory.getLogger(CommonApexStarterRestServer.class); + + private static String KEYSTORE = System.getProperty("user.dir") + "/src/test/resources/ssl/policy-keystore"; + + private static Coder coder = new StandardCoder(); + + public static final String NOT_ALIVE = "not alive"; + public static final String ALIVE = "alive"; + public static final String SELF = "self"; + public static final String NAME = "Policy PDP-A"; + public static final String ENDPOINT_PREFIX = "policy/apex-pdp/v1/"; + + private static int port; + protected static String httpsPrefix; + + private static ApexStarterMain main; + + private boolean activatorWasAlive; + + /** + * Allocates a port for the server, writes a config file, and then starts Main. + * + * @throws Exception if an error occurs + */ + @BeforeClass + public static void setUpBeforeClass() throws Exception { + port = NetworkUtil.allocPort(); + + httpsPrefix = "https://localhost:" + port + "/"; + + makeConfigFile(); + + startMain(); + } + + /** + * Stops Main. + */ + @AfterClass + public static void teardownAfterClass() { + try { + stopMain(); + + } catch (final ApexStarterException exp) { + LOGGER.error("cannot stop main", exp); + } + } + + /** + * Set up. + * + * @throws Exception if an error occurs + */ + @Before + public void setUp() throws Exception { + // restart, if not currently running + if (main == null) { + startMain(); + } + + activatorWasAlive = + Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class).isAlive(); + } + + /** + * Restores the activator's "alive" state. + */ + @After + public void tearDown() { + markActivator(activatorWasAlive); + } + + /** + * Verifies that an endpoint appears within the swagger response. + * + * @param endpoint the endpoint of interest + * @throws Exception if an error occurs + */ + protected void testSwagger(final String endpoint) throws Exception { + final Invocation.Builder invocationBuilder = sendFqeRequest(httpsPrefix + "swagger.yaml", true); + final String resp = invocationBuilder.get(String.class); + + assertTrue(resp.contains(ENDPOINT_PREFIX + endpoint + ":")); + } + + /** + * Makes a parameter configuration file. + * + * @throws Exception if an error occurs + */ + private static void makeConfigFile() throws Exception { + final Map<String, Object> config = + new CommonTestData().getApexStarterParameterGroupMap("ApexStarterParameterGroup"); + + @SuppressWarnings("unchecked") + final Map<String, Object> restParams = (Map<String, Object>) config.get("restServerParameters"); + restParams.put("port", port); + + final File file = new File("src/test/resources/TestConfigParams.json"); + file.deleteOnExit(); + + coder.encode(file, config); + } + + /** + * Starts the "Main". + * + * @throws Exception if an error occurs + */ + private static void startMain() throws Exception { + Registry.newRegistry(); + + // make sure port is available + if (NetworkUtil.isTcpPortOpen("localhost", port, 1, 1L)) { + throw new IllegalStateException("port " + port + " is still in use"); + } + + final Properties systemProps = System.getProperties(); + systemProps.put("javax.net.ssl.keyStore", KEYSTORE); + systemProps.put("javax.net.ssl.keyStorePassword", "Pol1cy_0nap"); + System.setProperties(systemProps); + + // @formatter:off + final String[] apexStarterConfigParameters = { + "-c", "src/test/resources/TestConfigParams.json", + "-p", "src/test/resources/topic.properties" + }; + // @formatter:on + + main = new ApexStarterMain(apexStarterConfigParameters); + + if (!NetworkUtil.isTcpPortOpen("localhost", port, 6, 10000L)) { + throw new IllegalStateException("server is not listening on port " + port); + } + } + + /** + * Stops the "Main". + * + * @throws Exception if an error occurs + */ + private static void stopMain() throws ApexStarterException { + if (main != null) { + final ApexStarterMain main2 = main; + main = null; + + main2.shutdown(); + } + } + + private void markActivator(final boolean wasAlive) { + final Object manager = Whitebox.getInternalState( + Registry.get(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR, ApexStarterActivator.class), "manager"); + Whitebox.setInternalState(manager, "running", wasAlive); + } + + /** + * Verifies that unauthorized requests fail. + * + * @param endpoint the target end point + * @param sender function that sends the requests to the target + * @throws Exception if an error occurs + */ + protected void checkUnauthRequest(final String endpoint, final Function<Invocation.Builder, Response> sender) + throws Exception { + assertEquals(Response.Status.UNAUTHORIZED.getStatusCode(), + sender.apply(sendNoAuthRequest(endpoint)).getStatus()); + } + + /** + * Sends a request to an endpoint. + * + * @param endpoint the target endpoint + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendRequest(final String endpoint) throws Exception { + return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, true); + } + + /** + * Sends a request to an endpoint, without any authorization header. + * + * @param endpoint the target endpoint + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendNoAuthRequest(final String endpoint) throws Exception { + return sendFqeRequest(httpsPrefix + ENDPOINT_PREFIX + endpoint, false); + } + + /** + * Sends a request to a fully qualified endpoint. + * + * @param fullyQualifiedEndpoint the fully qualified target endpoint + * @param includeAuth if authorization header should be included + * @return a request builder + * @throws Exception if an error occurs + */ + protected Invocation.Builder sendFqeRequest(final String fullyQualifiedEndpoint, final boolean includeAuth) + throws Exception { + final SSLContext sc = SSLContext.getInstance("TLSv1.2"); + sc.init(null, NetworkUtil.getAlwaysTrustingManager(), new SecureRandom()); + final ClientBuilder clientBuilder = + ClientBuilder.newBuilder().sslContext(sc).hostnameVerifier((host, session) -> true); + final Client client = clientBuilder.build(); + + client.property(ClientProperties.METAINF_SERVICES_LOOKUP_DISABLE, "true"); + client.register(GsonMessageBodyHandler.class); + + if (includeAuth) { + final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34"); + client.register(feature); + } + + final WebTarget webTarget = client.target(fullyQualifiedEndpoint); + + return webTarget.request(MediaType.APPLICATION_JSON); + } +} diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/TestHealthCheckRestControllerV1.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/TestHealthCheckRestControllerV1.java new file mode 100644 index 000000000..cd625c8dd --- /dev/null +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/rest/TestHealthCheckRestControllerV1.java @@ -0,0 +1,62 @@ +/*- + * ============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 static org.junit.Assert.assertEquals; + +import javax.ws.rs.client.Invocation; + +import org.junit.Test; +import org.onap.policy.common.endpoints.report.HealthCheckReport; + +/** + * Class to perform unit test of {@link ApexStarterRestServer}. + * + * @author Ajith Sreekumar (ajith.sreekumar@est.tech) + */ +public class TestHealthCheckRestControllerV1 extends CommonApexStarterRestServer { + + private static final String HEALTHCHECK_ENDPOINT = "healthcheck"; + + @Test + public void testSwagger() throws Exception { + super.testSwagger(HEALTHCHECK_ENDPOINT); + } + + @Test + public void testHealthCheckSuccess() throws Exception { + final Invocation.Builder invocationBuilder = sendRequest(HEALTHCHECK_ENDPOINT); + final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class); + validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report); + + // verify it fails when no authorization info is included + checkUnauthRequest(HEALTHCHECK_ENDPOINT, req -> req.get()); + } + + private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code, + final String message, final HealthCheckReport report) { + assertEquals(name, report.getName()); + assertEquals(url, report.getUrl()); + assertEquals(healthy, report.isHealthy()); + assertEquals(code, report.getCode()); + assertEquals(message, report.getMessage()); + } +} diff --git a/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json b/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json index b1ea14de7..d38031d41 100644 --- a/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json +++ b/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json @@ -1,9 +1,16 @@ { "name":"ApexStarterParameterGroup", + "restServerParameters": { + "host": "0.0.0.0", + "port": 6969, + "userName": "healthcheck", + "password": "zb!XztG34", + "https": true + }, "pdpStatusParameters":{ "timeIntervalMs": 120000, "pdpType":"apex", - "description":"Pdp status for HealthCheck", + "description":"Pdp Heartbeat", "supportedPolicyTypes":[{"name":"onap.policies.controlloop.Operational","version":"1.0.0"}] } }
\ No newline at end of file diff --git a/services/services-onappf/src/test/resources/TestConfigParameters.json b/services/services-onappf/src/test/resources/TestConfigParameters.json new file mode 100644 index 000000000..d9f095f21 --- /dev/null +++ b/services/services-onappf/src/test/resources/TestConfigParameters.json @@ -0,0 +1,16 @@ +{ + "name":"ApexStarterParameterGroup", + "restServerParameters": { + "host": "0.0.0.0", + "port": 6969, + "userName": "healthcheck", + "password": "zb!XztG34", + "https": true + }, + "pdpStatusParameters":{ + "timeIntervalMs": 120000, + "pdpType":"apex", + "description":"Pdp heartbeat", + "supportedPolicyTypes":[{"name":"onap.policies.controlloop.Operational","version":"1.0.0"}] + } +}
\ No newline at end of file diff --git a/services/services-onappf/src/test/resources/ssl/policy-keystore b/services/services-onappf/src/test/resources/ssl/policy-keystore Binary files differnew file mode 100644 index 000000000..7d2b1ecce --- /dev/null +++ b/services/services-onappf/src/test/resources/ssl/policy-keystore |