Adding code for db config & initial group creation
[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  * ================================================================================
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.startstop;
23
24 import java.io.FileInputStream;
25 import java.util.Arrays;
26 import java.util.Properties;
27
28 import org.onap.policy.common.utils.services.Registry;
29 import org.onap.policy.pap.main.PapConstants;
30 import org.onap.policy.pap.main.PolicyPapException;
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         } catch (final PolicyPapException e) {
69             LOGGER.error("start of policy pap service failed", e);
70             return;
71         }
72
73         // Read the parameters
74         try {
75             parameterGroup = new PapParameterHandler().getParameters(arguments);
76         } catch (final Exception e) {
77             LOGGER.error("start of policy pap service failed", e);
78             return;
79         }
80
81         // Read the properties
82         final Properties props = new Properties();
83         try {
84             final String propFile = arguments.getFullPropertyFilePath();
85             try (FileInputStream stream = new FileInputStream(propFile)) {
86                 props.load(stream);
87             }
88         } catch (final Exception e) {
89             LOGGER.error("start of policy pap service failed", e);
90             return;
91         }
92
93         // Initialize database
94         try {
95             new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters());
96         } catch (final PolicyPapException exp) {
97             LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), exp);
98             return;
99         }
100
101         // Now, create the activator for the policy pap service
102         activator = new PapActivator(parameterGroup, props);
103         Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator);
104
105         // Start the activator
106         try {
107             activator.start();
108         } catch (final RuntimeException e) {
109             LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), e);
110             Registry.unregister(PapConstants.REG_PAP_ACTIVATOR);
111             return;
112         }
113
114         // Add a shutdown hook to shut everything down in an orderly manner
115         Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass());
116         LOGGER.info("Started policy pap service");
117     }
118
119     /**
120      * Get the parameters specified in JSON.
121      *
122      * @return the parameters
123      */
124     public PapParameterGroup getParameters() {
125         return parameterGroup;
126     }
127
128     /**
129      * Shut down Execution.
130      *
131      * @throws PolicyPapException on shutdown errors
132      */
133     public void shutdown() throws PolicyPapException {
134         // clear the parameterGroup variable
135         parameterGroup = null;
136
137         // clear the pap activator
138         if (activator != null) {
139             activator.stop();
140         }
141     }
142
143     /**
144      * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called.
145      */
146     private class PolicyPapShutdownHookClass extends Thread {
147         /*
148          * (non-Javadoc)
149          *
150          * @see java.lang.Runnable#run()
151          */
152         @Override
153         public void run() {
154             try {
155                 // Shutdown the policy pap service and wait for everything to stop
156                 activator.stop();
157             } catch (final RuntimeException e) {
158                 LOGGER.warn("error occured during shut down of the policy pap service", e);
159             }
160         }
161     }
162
163     /**
164      * The main method.
165      *
166      * @param args the arguments
167      */
168     public static void main(final String[] args) {
169         new Main(args);
170     }
171 }