Changes to make PAP container crash with non zero exitCode
[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.services.Registry;
27 import org.onap.policy.pap.main.PapConstants;
28 import org.onap.policy.pap.main.PolicyPapException;
29 import org.onap.policy.pap.main.PolicyPapRuntimeException;
30 import org.onap.policy.pap.main.parameters.PapParameterGroup;
31 import org.onap.policy.pap.main.parameters.PapParameterHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /**
36  * This class initiates ONAP Policy Framework PAP component.
37  *
38  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39  */
40 public class Main {
41
42     private static final String START_FAILED = "start of policy pap service failed";
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
45
46     private PapActivator activator;
47     private PapParameterGroup parameterGroup;
48
49     /**
50      * Instantiates the policy pap service.
51      *
52      * @param args the command line arguments
53      */
54     public Main(final String[] args) {
55         final String argumentString = Arrays.toString(args);
56         LOGGER.info("Starting policy pap service with arguments - {}", argumentString);
57
58         // Check the arguments
59         final PapCommandLineArguments arguments = new PapCommandLineArguments();
60         try {
61             // The arguments return a string if there is a message to print and we should exit
62             final String argumentMessage = arguments.parse(args);
63             if (argumentMessage != null) {
64                 LOGGER.info(argumentMessage);
65                 return;
66             }
67             // Validate that the arguments are sane
68             arguments.validate();
69
70             // Read the parameters
71             parameterGroup = new PapParameterHandler().getParameters(arguments);
72
73             // Initialize database
74             new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters());
75
76             // Now, create the activator for the policy pap service
77             activator = new PapActivator(parameterGroup);
78             Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator);
79
80             // Start the activator
81             activator.start();
82         } catch (Exception exp) {
83             if (null != activator) {
84                 Registry.unregister(PapConstants.REG_PAP_ACTIVATOR);
85             }
86             throw new PolicyPapRuntimeException(START_FAILED + ", used parameters are " + Arrays.toString(args), exp);
87         }
88
89         // Add a shutdown hook to shut everything down in an orderly manner
90         Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass());
91         LOGGER.info("Started policy pap service");
92     }
93
94     /**
95      * Get the parameters specified in JSON.
96      *
97      * @return the parameters
98      */
99     public PapParameterGroup getParameters() {
100         return parameterGroup;
101     }
102
103     /**
104      * Shut down Execution.
105      *
106      * @throws PolicyPapException on shutdown errors
107      */
108     public void shutdown() throws PolicyPapException {
109         // clear the parameterGroup variable
110         parameterGroup = null;
111
112         // clear the pap activator
113         if (activator != null) {
114             activator.stop();
115         }
116     }
117
118     /**
119      * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called.
120      */
121     private class PolicyPapShutdownHookClass extends Thread {
122         /*
123          * (non-Javadoc)
124          *
125          * @see java.lang.Runnable#run()
126          */
127         @Override
128         public void run() {
129             if (!activator.isAlive()) {
130                 return;
131             }
132
133             try {
134                 // Shutdown the policy pap service and wait for everything to stop
135                 activator.stop();
136             } catch (final RuntimeException e) {
137                 LOGGER.warn("error occured during shut down of the policy pap service", e);
138             }
139         }
140     }
141
142     /**
143      * The main method.
144      *
145      * @param args the arguments
146      */
147     public static void main(final String[] args) {      // NOSONAR
148         /*
149          * NOTE: arguments are validated by the constructor, thus sonar is disabled.
150          */
151
152         new Main(args);
153     }
154 }