2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.startstop;
24 import java.util.Arrays;
25 import org.onap.policy.common.utils.services.Registry;
26 import org.onap.policy.pap.main.PapConstants;
27 import org.onap.policy.pap.main.PolicyPapException;
28 import org.onap.policy.pap.main.parameters.PapParameterGroup;
29 import org.onap.policy.pap.main.parameters.PapParameterHandler;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * This class initiates ONAP Policy Framework PAP component.
36 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
40 private static final String START_FAILED = "start of policy pap service failed";
42 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
44 private PapActivator activator;
45 private PapParameterGroup parameterGroup;
48 * Instantiates the policy pap service.
50 * @param args the command line arguments
52 public Main(final String[] args) {
53 final String argumentString = Arrays.toString(args);
54 LOGGER.info("Starting policy pap service with arguments - {}", argumentString);
56 // Check the arguments
57 final PapCommandLineArguments arguments = new PapCommandLineArguments();
59 // The arguments return a string if there is a message to print and we should exit
60 final String argumentMessage = arguments.parse(args);
61 if (argumentMessage != null) {
62 LOGGER.info(argumentMessage);
65 // Validate that the arguments are sane
67 } catch (final PolicyPapException e) {
68 LOGGER.error(START_FAILED, e);
72 // Read the parameters
74 parameterGroup = new PapParameterHandler().getParameters(arguments);
75 } catch (final Exception e) {
76 LOGGER.error(START_FAILED, e);
80 // Initialize database
82 new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters());
83 } catch (final PolicyPapException exp) {
84 LOGGER.error(START_FAILED + ", used parameters are {}", Arrays.toString(args), exp);
88 // Now, create the activator for the policy pap service
89 activator = new PapActivator(parameterGroup);
90 Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator);
92 // Start the activator
95 } catch (final RuntimeException e) {
96 LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), e);
97 Registry.unregister(PapConstants.REG_PAP_ACTIVATOR);
101 // Add a shutdown hook to shut everything down in an orderly manner
102 Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass());
103 LOGGER.info("Started policy pap service");
107 * Get the parameters specified in JSON.
109 * @return the parameters
111 public PapParameterGroup getParameters() {
112 return parameterGroup;
116 * Shut down Execution.
118 * @throws PolicyPapException on shutdown errors
120 public void shutdown() throws PolicyPapException {
121 // clear the parameterGroup variable
122 parameterGroup = null;
124 // clear the pap activator
125 if (activator != null) {
131 * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called.
133 private class PolicyPapShutdownHookClass extends Thread {
137 * @see java.lang.Runnable#run()
141 if (!activator.isAlive()) {
146 // Shutdown the policy pap service and wait for everything to stop
148 } catch (final RuntimeException e) {
149 LOGGER.warn("error occured during shut down of the policy pap service", e);
157 * @param args the arguments
159 public static void main(final String[] args) { // NOSONAR
161 * NOTE: arguments are validated by the constructor, thus sonar is disabled.