diff options
Diffstat (limited to 'services/services-onappf')
41 files changed, 1091 insertions, 264 deletions
diff --git a/services/services-onappf/pom.xml b/services/services-onappf/pom.xml index ef23ae76f..92f07bfac 100644 --- a/services/services-onappf/pom.xml +++ b/services/services-onappf/pom.xml @@ -34,130 +34,10 @@ <dependencies> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - <dependency> <groupId>org.onap.policy.apex-pdp.services</groupId> <artifactId>services-engine</artifactId> <version>${project.version}</version> </dependency> - <!-- Plugins for Context Distribution --> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-context.plugins-context-distribution</groupId> - <artifactId>plugins-context-distribution-hazelcast</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-context.plugins-context-distribution</groupId> - <artifactId>plugins-context-distribution-infinispan</artifactId> - <version>${project.version}</version> - </dependency> - - <!-- Plugins for Context Locking --> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-context.plugins-context-locking</groupId> - <artifactId>plugins-context-locking-hazelcast</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-context.plugins-context-locking</groupId> - <artifactId>plugins-context-locking-curator</artifactId> - <version>${project.version}</version> - </dependency> - - <!-- Plugins for Context Schemas --> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-context.plugins-context-schema</groupId> - <artifactId>plugins-context-schema-avro</artifactId> - <version>${project.version}</version> - </dependency> - - <!-- Plugins for Executors --> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> - <artifactId>plugins-executor-java</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> - <artifactId>plugins-executor-javascript</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> - <artifactId>plugins-executor-jruby</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> - <artifactId>plugins-executor-jython</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-executor</groupId> - <artifactId>plugins-executor-mvel</artifactId> - <version>${project.version}</version> - </dependency> - - <!-- Plugins for Persistence of Models over JPA --> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-persistence.plugins-persistence-jpa</groupId> - <artifactId>plugins-persistence-jpa-eclipselink</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-persistence.plugins-persistence-jpa</groupId> - <artifactId>plugins-persistence-jpa-hibernate</artifactId> - <version>${project.version}</version> - </dependency> - - <!-- Plugins for event carrier technologies --> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-carrier</groupId> - <artifactId>plugins-event-carrier-kafka</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-carrier</groupId> - <artifactId>plugins-event-carrier-websocket</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-carrier</groupId> - <artifactId>plugins-event-carrier-jms</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-carrier</groupId> - <artifactId>plugins-event-carrier-restclient</artifactId> - <version>${project.version}</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.apex-pdp.plugins.plugins-event.plugins-event-carrier</groupId> - <artifactId>plugins-event-carrier-restrequestor</artifactId> - <version>${project.version}</version> - </dependency> - - <!-- Plugins for event protocols --> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-protocol</groupId> - <artifactId>plugins-event-protocol-xml</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.onap.policy.apex-pdp.plugins.plugins-event.plugins-event-protocol</groupId> - <artifactId>plugins-event-protocol-jms</artifactId> - <version>${project.version}</version> - </dependency> - - <dependency> <groupId>org.onap.policy.common</groupId> <artifactId>policy-endpoints</artifactId> @@ -182,16 +62,31 @@ <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> </exclusion> + <exclusion> + <groupId>org.glassfish.hk2</groupId> + <artifactId>hk2-api</artifactId> + </exclusion> + <exclusion> + <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> - <dependency> <groupId>org.glassfish.jersey.inject</groupId> <artifactId>jersey-hk2</artifactId> <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> @@ -208,6 +103,8 @@ </exclusions> <version>${version.policy.models}</version> </dependency> + + <!--test dependencies --> <dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> @@ -218,5 +115,10 @@ <artifactId>powermock-api-mockito</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> </dependencies> </project> diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterActivator.java index b33d792a0..e08b5b344 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterActivator.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterActivator.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter; +package org.onap.policy.apex.services.onappf; import java.util.List; import java.util.Properties; @@ -26,13 +26,14 @@ import java.util.Properties; import lombok.Getter; import lombok.Setter; -import org.onap.policy.apex.starter.comm.PdpStateChangeListener; -import org.onap.policy.apex.starter.comm.PdpStatusPublisher; -import org.onap.policy.apex.starter.comm.PdpUpdateListener; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.exception.ApexStarterRunTimeException; -import org.onap.policy.apex.starter.handler.PdpMessageHandler; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; +import org.onap.policy.apex.services.onappf.comm.PdpStateChangeListener; +import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher; +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.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 } @@ -128,8 +149,6 @@ public class ApexStarterActivator { private void stopAndRemovePdpStatusPublisher() { final PdpStatusPublisher pdpStatusPublisher = Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class); - // send a final heartbeat with terminated status - pdpStatusPublisher.send(new PdpMessageHandler().getTerminatedPdpStatus()); pdpStatusPublisher.terminate(); Registry.unregister(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER); } @@ -164,6 +183,10 @@ public class ApexStarterActivator { throw new IllegalStateException("activator is not running"); } try { + final PdpStatusPublisher pdpStatusPublisher = + Registry.get(ApexStarterConstants.REG_PDP_STATUS_PUBLISHER, PdpStatusPublisher.class); + // send a final heartbeat with terminated status + pdpStatusPublisher.send(new PdpMessageHandler().getTerminatedPdpStatus()); manager.stop(); Registry.unregister(ApexStarterConstants.REG_APEX_STARTER_ACTIVATOR); } catch (final ServiceManagerException exp) { @@ -175,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/starter/ApexStarterCommandLineArguments.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterCommandLineArguments.java index f1d082fcb..152182b52 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterCommandLineArguments.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterCommandLineArguments.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter; +package org.onap.policy.apex.services.onappf; import java.io.File; import java.io.PrintWriter; @@ -33,8 +33,8 @@ import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.log4j.chainsaw.Main; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.exception.ApexStarterRunTimeException; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.exception.ApexStarterRunTimeException; import org.onap.policy.common.utils.resources.ResourceUtils; /** diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterConstants.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterConstants.java index 6173872e0..2481f4ef7 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterConstants.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterConstants.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter; +package org.onap.policy.apex.services.onappf; /** * Names of various items contained in the Registry. @@ -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/starter/ApexStarterMain.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java index e4465ed92..4813eb356 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/ApexStarterMain.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/ApexStarterMain.java @@ -18,15 +18,15 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter; +package org.onap.policy.apex.services.onappf; import java.io.FileInputStream; import java.util.Arrays; import java.util.Properties; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler; +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; import org.onap.policy.common.utils.services.Registry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -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/starter/comm/PdpStateChangeListener.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpStateChangeListener.java index 0bd0d935f..404749051 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/comm/PdpStateChangeListener.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpStateChangeListener.java @@ -18,9 +18,9 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.comm; +package org.onap.policy.apex.services.onappf.comm; -import org.onap.policy.apex.starter.handler.PdpStateChangeMessageHandler; +import org.onap.policy.apex.services.onappf.handler.PdpStateChangeMessageHandler; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.listeners.ScoListener; import org.onap.policy.common.utils.coder.StandardCoderObject; diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/comm/PdpStatusPublisher.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpStatusPublisher.java index 69c9dd18d..9a61084ca 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/comm/PdpStatusPublisher.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpStatusPublisher.java @@ -18,13 +18,13 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.comm; +package org.onap.policy.apex.services.onappf.comm; import java.util.List; import java.util.Timer; import java.util.TimerTask; -import org.onap.policy.apex.starter.handler.PdpMessageHandler; +import org.onap.policy.apex.services.onappf.handler.PdpMessageHandler; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient; import org.onap.policy.models.pdp.concepts.PdpStatus; @@ -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/starter/comm/PdpUpdateListener.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpUpdateListener.java index 46ce30e57..5633d60fb 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/comm/PdpUpdateListener.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/comm/PdpUpdateListener.java @@ -18,9 +18,9 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.comm; +package org.onap.policy.apex.services.onappf.comm; -import org.onap.policy.apex.starter.handler.PdpUpdateMessageHandler; +import org.onap.policy.apex.services.onappf.handler.PdpUpdateMessageHandler; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.endpoints.listeners.ScoListener; import org.onap.policy.common.utils.coder.StandardCoderObject; diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/exception/ApexStarterException.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/exception/ApexStarterException.java index 7062e377b..461d758c9 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/exception/ApexStarterException.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/exception/ApexStarterException.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.exception; +package org.onap.policy.apex.services.onappf.exception; /** * This exception will be called if an error occurs in apex starter. diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/exception/ApexStarterRunTimeException.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/exception/ApexStarterRunTimeException.java index f15b55d33..a1c970a56 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/exception/ApexStarterRunTimeException.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/exception/ApexStarterRunTimeException.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.exception; +package org.onap.policy.apex.services.onappf.exception; /** * This runtime exception will be called if a runtime error occurs when using apex starter. diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/engine/ApexEngineHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java index e58b0db76..85d666c2b 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/engine/ApexEngineHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/ApexEngineHandler.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.engine; +package org.onap.policy.apex.services.onappf.handler; import com.google.gson.JsonObject; @@ -30,7 +30,7 @@ import java.nio.file.Path; import org.onap.policy.apex.model.basicmodel.concepts.ApexException; import org.onap.policy.apex.service.engine.main.ApexMain; -import org.onap.policy.apex.starter.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; import org.slf4j.Logger; @@ -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 { @@ -70,17 +69,17 @@ public class ApexEngineHandler { final String modelFilePath = createFile(policyModel, "modelFile"); final String apexConfigFilePath = createFile(apexConfig, "apexConfigFile"); - final String[] apexArgs = { "-rfr", "target/classes", "-c", apexConfigFilePath, "-m", modelFilePath }; + final String[] apexArgs = { "-c", apexConfigFilePath, "-m", modelFilePath }; LOGGER.debug("Starting apex engine."); apexMain = new ApexMain(apexArgs); } /** - * 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/starter/handler/PdpMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java index be76938f3..b72ef805f 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpMessageHandler.java @@ -18,14 +18,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.handler; +package org.onap.policy.apex.services.onappf.handler; import java.util.ArrayList; import java.util.List; -import org.onap.policy.apex.starter.ApexStarterConstants; -import org.onap.policy.apex.starter.parameters.PdpStatusParameters; -import org.onap.policy.apex.starter.parameters.ToscaPolicyTypeIdentifierParameters; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.onap.policy.apex.services.onappf.parameters.PdpStatusParameters; +import org.onap.policy.apex.services.onappf.parameters.ToscaPolicyTypeIdentifierParameters; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.pdp.concepts.PdpResponseDetails; import org.onap.policy.models.pdp.concepts.PdpStatus; @@ -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/starter/handler/PdpStateChangeMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java index 9f97fb24b..d2c7786ca 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpStateChangeMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpStateChangeMessageHandler.java @@ -18,14 +18,13 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.handler; +package org.onap.policy.apex.services.onappf.handler; import java.util.List; -import org.onap.policy.apex.starter.ApexStarterConstants; -import org.onap.policy.apex.starter.comm.PdpStatusPublisher; -import org.onap.policy.apex.starter.engine.ApexEngineHandler; -import org.onap.policy.apex.starter.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.pdp.concepts.PdpResponseDetails; import org.onap.policy.models.pdp.concepts.PdpStateChange; @@ -33,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. @@ -41,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. * @@ -73,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) { @@ -99,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()); } @@ -110,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) { @@ -129,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/starter/handler/PdpUpdateMessageHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java index 5198c229e..31963ddc0 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/handler/PdpUpdateMessageHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/handler/PdpUpdateMessageHandler.java @@ -18,14 +18,13 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.handler; +package org.onap.policy.apex.services.onappf.handler; import java.util.List; -import org.onap.policy.apex.starter.ApexStarterConstants; -import org.onap.policy.apex.starter.comm.PdpStatusPublisher; -import org.onap.policy.apex.starter.engine.ApexEngineHandler; -import org.onap.policy.apex.starter.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; +import org.onap.policy.apex.services.onappf.comm.PdpStatusPublisher; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.common.endpoints.event.comm.TopicSink; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.pdp.concepts.PdpResponseDetails; @@ -33,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. @@ -41,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. * @@ -53,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 { @@ -66,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) { @@ -89,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); @@ -103,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."); } @@ -119,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()); } @@ -127,26 +131,19 @@ 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) { + private boolean checkIfAlreadyHandled(final PdpUpdate pdpUpdateMsg, final PdpStatus pdpStatusContext) { 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())); + && null != pdpStatusContext.getPolicies() && new PdpMessageHandler() + .getToscaPolicyIdentifiers(pdpUpdateMsg.getPolicies()).equals(pdpStatusContext.getPolicies()); } /** diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/ApexStarterParameterGroup.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ApexStarterParameterGroup.java index 68299480e..db51803be 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/ApexStarterParameterGroup.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ApexStarterParameterGroup.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.parameters; +package org.onap.policy.apex.services.onappf.parameters; import lombok.Getter; @@ -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/starter/parameters/ApexStarterParameterHandler.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ApexStarterParameterHandler.java index dae4b8334..ed7c00f10 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/ApexStarterParameterHandler.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ApexStarterParameterHandler.java @@ -18,12 +18,12 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.parameters; +package org.onap.policy.apex.services.onappf.parameters; import java.io.File; -import org.onap.policy.apex.starter.ApexStarterCommandLineArguments; -import org.onap.policy.apex.starter.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; import org.onap.policy.common.parameters.GroupValidationResult; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; diff --git a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/PdpStatusParameters.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/PdpStatusParameters.java index fbc94193d..962a98e4c 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/PdpStatusParameters.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/PdpStatusParameters.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.parameters; +package org.onap.policy.apex.services.onappf.parameters; import java.util.List; 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/starter/parameters/ToscaPolicyTypeIdentifierParameters.java b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ToscaPolicyTypeIdentifierParameters.java index 37e27492e..a08b9347c 100644 --- a/services/services-onappf/src/main/java/org/onap/policy/apex/starter/parameters/ToscaPolicyTypeIdentifierParameters.java +++ b/services/services-onappf/src/main/java/org/onap/policy/apex/services/onappf/parameters/ToscaPolicyTypeIdentifierParameters.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.parameters; +package org.onap.policy.apex.services.onappf.parameters; import lombok.EqualsAndHashCode; import lombok.Getter; 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 new file mode 100644 index 000000000..3efaa8cff --- /dev/null +++ b/services/services-onappf/src/main/resources/config/OnapPfConfig.json @@ -0,0 +1,16 @@ +{ + "name":"OnapPfParameterGroup", + "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/main/resources/config/topic.properties b/services/services-onappf/src/main/resources/config/topic.properties new file mode 100644 index 000000000..b61626b2d --- /dev/null +++ b/services/services-onappf/src/main/resources/config/topic.properties @@ -0,0 +1,23 @@ +# ============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========================================================= + +dmaap.source.topics=POLICY-PDP-PAP +dmaap.sink.topics=POLICY-PDP-PAP + +dmaap.source.topics.POLICY-PDP-PAP.servers= message-router +dmaap.sink.topics.POLICY-PDP-PAP.servers= message-router
\ No newline at end of file diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java index 6da3a5eb6..238a89f66 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterActivator.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterActivator.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter; +package org.onap.policy.apex.services.onappf; import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.junit.Assert.assertEquals; @@ -33,10 +33,13 @@ import java.util.Properties; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler; -import org.onap.policy.apex.starter.parameters.CommonTestData; +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.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; +import org.onap.policy.apex.services.onappf.parameters.CommonTestData; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.pdp.concepts.PdpStatus; diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterConstants.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterConstants.java index 08e56cdf8..52ca94439 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterConstants.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterConstants.java @@ -18,9 +18,10 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter; +package org.onap.policy.apex.services.onappf; import org.junit.Test; +import org.onap.policy.apex.services.onappf.ApexStarterConstants; import org.powermock.reflect.Whitebox; /** diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java index 3005e320a..c5c0fc483 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/TestApexStarterMain.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/TestApexStarterMain.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter; +package org.onap.policy.apex.services.onappf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -27,8 +27,11 @@ import static org.junit.Assert.assertTrue; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.parameters.CommonTestData; +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.utils.services.Registry; /** diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java index d0a85f9f2..c6ed44556 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpStateChangeListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpStateChangeListener.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.comm; +package org.onap.policy.apex.services.onappf.comm; import static org.junit.Assert.assertEquals; @@ -37,12 +37,12 @@ import java.util.Properties; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.onap.policy.apex.starter.ApexStarterActivator; -import org.onap.policy.apex.starter.ApexStarterCommandLineArguments; -import org.onap.policy.apex.starter.ApexStarterConstants; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler; +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.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterGroup; +import org.onap.policy.apex.services.onappf.parameters.ApexStarterParameterHandler; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.pdp.concepts.PdpStateChange; @@ -63,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(); @@ -110,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/starter/comm/TestPdpUpdateListener.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java index 2ffed2cb6..ebe0fc957 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/comm/TestPdpUpdateListener.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/comm/TestPdpUpdateListener.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.comm; +package org.onap.policy.apex.services.onappf.comm; import static org.junit.Assert.assertEquals; @@ -37,13 +37,13 @@ import java.util.Properties; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.onap.policy.apex.starter.ApexStarterActivator; -import org.onap.policy.apex.starter.ApexStarterCommandLineArguments; -import org.onap.policy.apex.starter.ApexStarterConstants; -import org.onap.policy.apex.starter.exception.ApexStarterException; -import org.onap.policy.apex.starter.handler.PdpMessageHandler; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup; -import org.onap.policy.apex.starter.parameters.ApexStarterParameterHandler; +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.exception.ApexStarterException; +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.parameters.ApexStarterParameterHandler; import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure; import org.onap.policy.common.utils.services.Registry; import org.onap.policy.models.pdp.concepts.PdpStatus; @@ -61,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/starter/exception/TestExceptions.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/exception/TestExceptions.java index 63ebeb608..3a05e72b1 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/exception/TestExceptions.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/exception/TestExceptions.java @@ -18,9 +18,11 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.exception; +package org.onap.policy.apex.services.onappf.exception; import org.junit.Test; +import org.onap.policy.apex.services.onappf.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.exception.ApexStarterRunTimeException; import org.onap.policy.common.utils.test.ExceptionsTester; /** diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.java index d329d1f22..7e668e9b2 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/CommonTestData.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/CommonTestData.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.parameters; +package org.onap.policy.apex.services.onappf.parameters; import java.util.Arrays; import java.util.List; @@ -47,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(); @@ -89,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/starter/parameters/TestApexStarterParameterGroup.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.java index 8d5f1cef9..dd9e83f52 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterGroup.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterGroup.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.parameters; +package org.onap.policy.apex.services.onappf.parameters; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -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 @@ -100,7 +107,22 @@ public class TestApexStarterParameterGroup { final GroupValidationResult validationResult = apexStarterParameters.validate(); assertFalse(validationResult.isValid()); assertTrue(validationResult.getResult() - .contains("\"org.onap.policy.apex.starter.parameters.ApexStarterParameterGroup\" INVALID, " + .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/starter/parameters/TestApexStarterParameterHandler.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.java index baa91d23a..f91baeb45 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestApexStarterParameterHandler.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestApexStarterParameterHandler.java @@ -18,7 +18,7 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.parameters; +package org.onap.policy.apex.services.onappf.parameters; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -27,8 +27,10 @@ import static org.junit.Assert.fail; import java.io.FileNotFoundException; import org.junit.Test; -import org.onap.policy.apex.starter.ApexStarterCommandLineArguments; -import org.onap.policy.apex.starter.exception.ApexStarterException; +import org.onap.policy.apex.services.onappf.ApexStarterCommandLineArguments; +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; import org.onap.policy.common.utils.coder.CoderException; /** diff --git a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestPdpStatusParameters.java b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestPdpStatusParameters.java index 37cbbe021..e8a3f7ee0 100644 --- a/services/services-onappf/src/test/java/org/onap/policy/apex/starter/parameters/TestPdpStatusParameters.java +++ b/services/services-onappf/src/test/java/org/onap/policy/apex/services/onappf/parameters/TestPdpStatusParameters.java @@ -18,13 +18,14 @@ * ============LICENSE_END========================================================= */ -package org.onap.policy.apex.starter.parameters; +package org.onap.policy.apex.services.onappf.parameters; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import org.junit.Test; +import org.onap.policy.apex.services.onappf.parameters.PdpStatusParameters; import org.onap.policy.common.parameters.GroupValidationResult; /** 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 6b9df6014..d38031d41 100644 --- a/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json +++ b/services/services-onappf/src/test/resources/ApexStarterConfigParameters.json @@ -1,10 +1,16 @@ { "name":"ApexStarterParameterGroup", + "restServerParameters": { + "host": "0.0.0.0", + "port": 6969, + "userName": "healthcheck", + "password": "zb!XztG34", + "https": true + }, "pdpStatusParameters":{ - "timeIntervalMs": 2000, - "pdpName":"apex-pdp", + "timeIntervalMs": 120000, "pdpType":"apex", - "description":"Pdp status for HealthCheck", - "supportedPolicyTypes":[{"name":"policy1","version":"1.0"},{"name":"policy2","version":"1.0"}] + "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 diff --git a/services/services-onappf/src/test/resources/topic.properties b/services/services-onappf/src/test/resources/topic.properties index 87c6ebd54..9f3c21dff 100644 --- a/services/services-onappf/src/test/resources/topic.properties +++ b/services/services-onappf/src/test/resources/topic.properties @@ -1,3 +1,21 @@ +# ============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========================================================= + noop.sink.topics=POLICY-PDP-PAP noop.sink.topics.POLICY-PDP-PAP.servers=anyserver noop.source.topics=POLICY-PDP-PAP |