Using standard success/failure messages in PAP
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / startstop / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property.
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.startstop;
24
25 import java.util.Arrays;
26 import org.onap.policy.common.utils.resources.MessageConstants;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.pap.main.PapConstants;
29 import org.onap.policy.pap.main.PolicyPapException;
30 import org.onap.policy.pap.main.PolicyPapRuntimeException;
31 import org.onap.policy.pap.main.parameters.PapParameterGroup;
32 import org.onap.policy.pap.main.parameters.PapParameterHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * This class initiates ONAP Policy Framework PAP component.
38  *
39  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
40  */
41 public class Main {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
44
45     private PapActivator activator;
46     private PapParameterGroup parameterGroup;
47
48     /**
49      * Instantiates the policy pap service.
50      *
51      * @param args the command line arguments
52      */
53     public Main(final String[] args) {
54         final String argumentString = Arrays.toString(args);
55         LOGGER.info("Starting policy pap service with arguments - {}", argumentString);
56
57         // Check the arguments
58         final PapCommandLineArguments arguments = new PapCommandLineArguments();
59         try {
60             // The arguments return a string if there is a message to print and we should exit
61             final String argumentMessage = arguments.parse(args);
62             if (argumentMessage != null) {
63                 LOGGER.info(argumentMessage);
64                 return;
65             }
66             // Validate that the arguments are sane
67             arguments.validate();
68
69             // Read the parameters
70             parameterGroup = new PapParameterHandler().getParameters(arguments);
71
72             // Initialize database
73             new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters());
74
75             // Now, create the activator for the policy pap service
76             activator = new PapActivator(parameterGroup);
77             Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator);
78
79             // Start the activator
80             activator.start();
81         } catch (Exception exp) {
82             if (null != activator) {
83                 Registry.unregister(PapConstants.REG_PAP_ACTIVATOR);
84             }
85             throw new PolicyPapRuntimeException(
86                 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_PAP), exp);
87         }
88
89         // Add a shutdown hook to shut everything down in an orderly manner
90         Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass());
91         String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_PAP);
92         LOGGER.info(successMsg);
93     }
94
95     /**
96      * Get the parameters specified in JSON.
97      *
98      * @return the parameters
99      */
100     public PapParameterGroup getParameters() {
101         return parameterGroup;
102     }
103
104     /**
105      * Shut down Execution.
106      *
107      * @throws PolicyPapException on shutdown errors
108      */
109     public void shutdown() throws PolicyPapException {
110         // clear the parameterGroup variable
111         parameterGroup = null;
112
113         // clear the pap activator
114         if (activator != null) {
115             activator.stop();
116         }
117     }
118
119     /**
120      * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called.
121      */
122     private class PolicyPapShutdownHookClass extends Thread {
123         /*
124          * (non-Javadoc)
125          *
126          * @see java.lang.Runnable#run()
127          */
128         @Override
129         public void run() {
130             if (!activator.isAlive()) {
131                 return;
132             }
133
134             try {
135                 // Shutdown the policy pap service and wait for everything to stop
136                 activator.stop();
137             } catch (final RuntimeException e) {
138                 LOGGER.warn("error occured during shut down of the policy pap service", e);
139             }
140         }
141     }
142
143     /**
144      * The main method.
145      *
146      * @param args the arguments
147      */
148     public static void main(final String[] args) {      // NOSONAR
149         /*
150          * NOTE: arguments are validated by the constructor, thus sonar is disabled.
151          */
152
153         new Main(args);
154     }
155 }