diff options
Diffstat (limited to 'main/src/main/java/org/onap/policy/pap/main/startstop')
4 files changed, 61 insertions, 292 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java b/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java deleted file mode 100644 index 5918ed12..00000000 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/Main.java +++ /dev/null @@ -1,161 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019 Nordix Foundation. - * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. - * Modifications Copyright (C) 2020 Bell Canada. All rights reserved. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.startstop; - -import java.util.Arrays; -import org.onap.policy.common.utils.resources.MessageConstants; -import org.onap.policy.common.utils.services.Registry; -import org.onap.policy.pap.main.PapConstants; -import org.onap.policy.pap.main.PolicyPapRuntimeException; -import org.onap.policy.pap.main.parameters.PapParameterGroup; -import org.onap.policy.pap.main.parameters.PapParameterHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This class initiates ONAP Policy Framework PAP component. - * - * @author Ram Krishna Verma (ram.krishna.verma@est.tech) - */ -public class Main { - - private static final Logger LOGGER = LoggerFactory.getLogger(Main.class); - - private PapActivator activator; - private PapParameterGroup parameterGroup; - - /** - * Instantiates the policy pap service. - * - * @param args the command line arguments - */ - public Main(final String[] args) { - final var argumentString = Arrays.toString(args); - LOGGER.info("Starting policy pap service with arguments - {}", argumentString); - - // Check the arguments - final var arguments = new PapCommandLineArguments(); - try { - // The arguments return a string if there is a message to print and we should exit - final String argumentMessage = arguments.parse(args); - if (argumentMessage != null) { - LOGGER.info(argumentMessage); - return; - } - // Validate that the arguments are sane - arguments.validate(); - - // Read the parameters - parameterGroup = new PapParameterHandler().getParameters(arguments); - - // Initialize database - new PapDatabaseInitializer().initializePapDatabase( - parameterGroup.getDatabaseProviderParameters(), - arguments.getPdpGroupsConfiguration()); - - // Now, create the activator for the policy pap service - activator = new PapActivator(parameterGroup); - Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator); - - // Start the activator - activator.start(); - } catch (Exception exp) { // NOSONAR - /* - * Disabled sonar on the above line, because we want to capture the stack - * trace via the logger while still reporting the exception message on stdout - * when the JVM exits. - */ - LOGGER.error("failed to start Main", exp); - if (null != activator) { - Registry.unregister(PapConstants.REG_PAP_ACTIVATOR); - } - throw new PolicyPapRuntimeException( - String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP), exp); - } - - // Add a shutdown hook to shut everything down in an orderly manner - Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass()); - var successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_PAP); - LOGGER.info(successMsg); - } - - /** - * Get the parameters specified in JSON. - * - * @return the parameters - */ - public PapParameterGroup getParameters() { - return parameterGroup; - } - - /** - * Shut down Execution. - * - */ - public void shutdown() { - // clear the parameterGroup variable - parameterGroup = null; - - // clear the pap activator - if (activator != null) { - activator.stop(); - } - } - - /** - * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called. - */ - private class PolicyPapShutdownHookClass extends Thread { - /* - * (non-Javadoc) - * - * @see java.lang.Runnable#run() - */ - @Override - public void run() { - if (!activator.isAlive()) { - return; - } - - try { - // Shutdown the policy pap service and wait for everything to stop - activator.stop(); - } catch (final RuntimeException e) { - LOGGER.warn("error occurred during shut down of the policy pap service", e); - } - } - } - - /** - * The main method. - * - * @param args the arguments - */ - public static void main(final String[] args) { // NOSONAR - /* - * NOTE: arguments are validated by the constructor, thus sonar is disabled. - */ - - new Main(args); - } -} diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java index ad78fc7c..94943706 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapActivator.java @@ -30,8 +30,6 @@ import java.util.concurrent.atomic.AtomicReference; import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager; import org.onap.policy.common.endpoints.event.comm.TopicListener; import org.onap.policy.common.endpoints.event.comm.TopicSource; -import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; -import org.onap.policy.common.endpoints.http.server.RestServer; import org.onap.policy.common.endpoints.listeners.MessageTypeDispatcher; import org.onap.policy.common.endpoints.listeners.RequestIdDispatcher; import org.onap.policy.common.parameters.ParameterService; @@ -51,21 +49,12 @@ import org.onap.policy.pap.main.comm.TimerManager; import org.onap.policy.pap.main.notification.PolicyNotifier; import org.onap.policy.pap.main.parameters.PapParameterGroup; import org.onap.policy.pap.main.parameters.PdpModifyRequestMapParams; -import org.onap.policy.pap.main.rest.HealthCheckRestControllerV1; -import org.onap.policy.pap.main.rest.PapAafFilter; import org.onap.policy.pap.main.rest.PapStatisticsManager; -import org.onap.policy.pap.main.rest.PdpGroupCreateOrUpdateControllerV1; -import org.onap.policy.pap.main.rest.PdpGroupDeleteControllerV1; -import org.onap.policy.pap.main.rest.PdpGroupDeployControllerV1; -import org.onap.policy.pap.main.rest.PdpGroupHealthCheckControllerV1; -import org.onap.policy.pap.main.rest.PdpGroupQueryControllerV1; -import org.onap.policy.pap.main.rest.PdpGroupStateChangeControllerV1; -import org.onap.policy.pap.main.rest.PolicyAuditControllerV1; -import org.onap.policy.pap.main.rest.PolicyComponentsHealthCheckControllerV1; -import org.onap.policy.pap.main.rest.PolicyComponentsHealthCheckProvider; -import org.onap.policy.pap.main.rest.PolicyStatusControllerV1; import org.onap.policy.pap.main.rest.PolicyUndeployerImpl; -import org.onap.policy.pap.main.rest.StatisticsRestControllerV1; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; /** * This class activates Policy Administration (PAP) as a complete service together with all its controllers, listeners & @@ -73,6 +62,7 @@ import org.onap.policy.pap.main.rest.StatisticsRestControllerV1; * * @author Ram Krishna Verma (ram.krishna.verma@est.tech) */ +@Component public class PapActivator extends ServiceManagerContainer { private static final String[] MSG_TYPE_NAMES = { "messageName" }; private static final String[] REQ_ID_NAMES = { "response", "responseTo" }; @@ -82,7 +72,7 @@ public class PapActivator extends ServiceManagerContainer { */ private static final int MAX_MISSED_HEARTBEATS = 3; - private final PapParameterGroup papParameterGroup; + private PapParameterGroup papParameterGroup; /** * Listens for messages on the topic, decodes them into a {@link PdpStatus} message, and then dispatches them to @@ -108,13 +98,12 @@ public class PapActivator extends ServiceManagerContainer { * * @param papParameterGroup the parameters for the pap service */ - public PapActivator(final PapParameterGroup papParameterGroup) { + public PapActivator(PapParameterGroup papParameterGroup) { super("Policy PAP"); - + this.papParameterGroup = papParameterGroup; TopicEndpointManager.getManager().addTopics(papParameterGroup.getTopicParameterGroup()); try { - this.papParameterGroup = papParameterGroup; this.responseMsgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES); this.heartbeatMsgDispatcher = new MessageTypeDispatcher(MSG_TYPE_NAMES); this.responseReqIdDispatcher = new RequestIdDispatcher<>(PdpStatus.class, REQ_ID_NAMES); @@ -126,7 +115,6 @@ public class PapActivator extends ServiceManagerContainer { throw new PolicyPapRuntimeException(e); } - papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName()); final var pdpUpdateLock = new Object(); final var pdpParams = papParameterGroup.getPdpParameters(); @@ -137,7 +125,6 @@ public class PapActivator extends ServiceManagerContainer { final AtomicReference<ScheduledExecutorService> pdpExpirationTimer = new AtomicReference<>(); final AtomicReference<PolicyModelsProviderFactoryWrapper> daoFactory = new AtomicReference<>(); final AtomicReference<PdpModifyRequestMap> requestMap = new AtomicReference<>(); - final AtomicReference<RestServer> restServer = new AtomicReference<>(); final AtomicReference<PolicyNotifier> notifier = new AtomicReference<>(); // @formatter:off @@ -182,6 +169,10 @@ public class PapActivator extends ServiceManagerContainer { () -> Registry.register(PapConstants.REG_STATISTICS_MANAGER, new PapStatisticsManager()), () -> Registry.unregister(PapConstants.REG_STATISTICS_MANAGER)); + addAction("PAP Activator", + () -> Registry.register(PapConstants.REG_PAP_ACTIVATOR, this), + () -> Registry.unregister(PapConstants.REG_PAP_ACTIVATOR)); + addAction("PDP publisher", () -> { pdpPub.set(new Publisher<>(PapConstants.TOPIC_POLICY_PDP_PAP)); @@ -253,30 +244,6 @@ public class PapActivator extends ServiceManagerContainer { }, () -> pdpExpirationTimer.get().shutdown()); - addAction("PAP client executor", - () -> - PolicyComponentsHealthCheckProvider.initializeClientHealthCheckExecutorService(papParameterGroup, - HttpClientFactoryInstance.getClientFactory()), - PolicyComponentsHealthCheckProvider::cleanup); - - addAction("REST server", - () -> { - var server = new RestServer(papParameterGroup.getRestServerParameters(), PapAafFilter.class, - HealthCheckRestControllerV1.class, - StatisticsRestControllerV1.class, - PdpGroupCreateOrUpdateControllerV1.class, - PdpGroupDeployControllerV1.class, - PdpGroupDeleteControllerV1.class, - PdpGroupStateChangeControllerV1.class, - PdpGroupQueryControllerV1.class, - PdpGroupHealthCheckControllerV1.class, - PolicyStatusControllerV1.class, - PolicyComponentsHealthCheckControllerV1.class, - PolicyAuditControllerV1.class); - restServer.set(server); - restServer.get().start(); - }, - () -> restServer.get().stop()); // @formatter:on } @@ -322,4 +289,28 @@ public class PapActivator extends ServiceManagerContainer { source.unregister(dispatcher); } } + + /** + * Handle ContextRefreshEvent. + * + * @param ctxRefreshedEvent the ContextRefreshedEvent + */ + @EventListener + public void handleContextRefreshEvent(ContextRefreshedEvent ctxRefreshedEvent) { + if (!isAlive()) { + start(); + } + } + + /** + * Handle ContextClosedEvent. + * + * @param ctxClosedEvent the ContextClosedEvent + */ + @EventListener + public void handleContextClosedEvent(ContextClosedEvent ctxClosedEvent) { + if (isAlive()) { + stop(); + } + } } diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapCommandLineArguments.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapCommandLineArguments.java deleted file mode 100644 index afc8fbca..00000000 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapCommandLineArguments.java +++ /dev/null @@ -1,85 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2019-2021 Nordix Foundation. - * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.policy.pap.main.startstop; - -import org.apache.commons.cli.Option; -import org.onap.policy.common.utils.cmd.CommandLineArgumentsHandler; -import org.onap.policy.common.utils.cmd.CommandLineException; -import org.onap.policy.common.utils.resources.MessageConstants; -import org.onap.policy.pap.main.PolicyPapRuntimeException; - -/** - * This class reads and handles command line parameters for the policy pap service. - * - * @author Ram Krishna Verma (ram.krishna.verma@est.tech) - */ -public class PapCommandLineArguments extends CommandLineArgumentsHandler { - - protected static final String GROUP_FILE_OPTION = "g"; - protected static final String GROUP_FILE_LONG_OPTION = "groups-file"; - public static final String GROUP_FILE_ARG_NAME = "GROUP_FILE"; - - protected static final String DEFAULT_GROUP_RESOURCE = "PapDb.json"; - - /** - * Construct the options for the CLI editor. - */ - public PapCommandLineArguments() { - super(Main.class.getName(), MessageConstants.POLICY_PAP, customOptionG()); - } - - private static Option customOptionG() { - return Option.builder(GROUP_FILE_OPTION).longOpt(GROUP_FILE_LONG_OPTION) - .desc("the full path to the groups file to use, " - + "the groups file contains the group configuration added to the DB") - .hasArg().argName(GROUP_FILE_ARG_NAME).required(false).type(String.class).build(); - } - - protected String getPdpGroupsConfiguration() { - return this.getCommandLine() - .getOptionValue(GROUP_FILE_OPTION, DEFAULT_GROUP_RESOURCE); - } - - @Override - public void validate() throws CommandLineException { - super.validate(); - String groupConfig = getPdpGroupsConfiguration(); - if (!groupConfig.equals(DEFAULT_GROUP_RESOURCE)) { - validateReadableFile(MessageConstants.POLICY_PAP, groupConfig); - } - } - - /** - * Construct the options for the CLI editor and parse in the given arguments. - * - * @param args The command line arguments - */ - public PapCommandLineArguments(final String[] args) { - this(); - - try { - parse(args); - } catch (final CommandLineException e) { - throw new PolicyPapRuntimeException("parse error on policy pap parameters", e); - } - } -} diff --git a/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java b/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java index d180c934..617cdf36 100644 --- a/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java +++ b/main/src/main/java/org/onap/policy/pap/main/startstop/PapDatabaseInitializer.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2019 Nordix Foundation. * Modifications Copyright (C) 2019, 2021 AT&T Intellectual Property. + * Modifications Copyright (C) 2021 Bell Canada. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +23,7 @@ package org.onap.policy.pap.main.startstop; import java.util.List; +import javax.annotation.PostConstruct; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; @@ -32,14 +34,21 @@ import org.onap.policy.models.pdp.concepts.PdpGroups; import org.onap.policy.models.provider.PolicyModelsProviderFactory; import org.onap.policy.models.provider.PolicyModelsProviderParameters; import org.onap.policy.pap.main.PolicyPapException; +import org.onap.policy.pap.main.parameters.PapParameterGroup; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; /** * This class creates initial PdpGroup/SubGroup in the database. * * @author Ram Krishna Verma (ram.krishna.verma@est.tech) */ +@Component +@ConditionalOnProperty(value = "db.initialize", havingValue = "true", matchIfMissing = true) public class PapDatabaseInitializer { private static final Logger LOGGER = LoggerFactory.getLogger(PapDatabaseInitializer.class); @@ -47,6 +56,12 @@ public class PapDatabaseInitializer { private final StandardCoder standardCoder; private final PolicyModelsProviderFactory factory; + @Autowired + private PapParameterGroup papParameterGroup; + + @Value("${group-config-file:PapDb.json}") + private String groupConfigFile; + /** * Constructs the object. */ @@ -59,9 +74,10 @@ public class PapDatabaseInitializer { * Initializes database with group information. * * @param policyModelsProviderParameters the database parameters + * @param groupsJson the group file path * @throws PolicyPapException in case of errors. */ - public void initializePapDatabase( + private void initializePapDatabase( final PolicyModelsProviderParameters policyModelsProviderParameters, String groupsJson) throws PolicyPapException { @@ -86,4 +102,12 @@ public class PapDatabaseInitializer { throw new PolicyPapException(exp); } } + + /** + * Initializes database with group information. + */ + @PostConstruct + public void loadData() throws PolicyPapException { + initializePapDatabase(papParameterGroup.getDatabaseProviderParameters(), groupConfigFile); + } } |