Merge "Handle null policy lists"
[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 String START_FAILED = "start of policy pap service failed";
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
46
47     private PapActivator activator;
48     private PapParameterGroup parameterGroup;
49
50     /**
51      * Instantiates the policy pap service.
52      *
53      * @param args the command line arguments
54      */
55     public Main(final String[] args) {
56         final String argumentString = Arrays.toString(args);
57         LOGGER.info("Starting policy pap service with arguments - {}", argumentString);
58
59         // Check the arguments
60         final PapCommandLineArguments arguments = new PapCommandLineArguments();
61         try {
62             // The arguments return a string if there is a message to print and we should exit
63             final String argumentMessage = arguments.parse(args);
64             if (argumentMessage != null) {
65                 LOGGER.info(argumentMessage);
66                 return;
67             }
68             // Validate that the arguments are sane
69             arguments.validate();
70         } catch (final PolicyPapException e) {
71             LOGGER.error(START_FAILED, e);
72             return;
73         }
74
75         // Read the parameters
76         try {
77             parameterGroup = new PapParameterHandler().getParameters(arguments);
78         } catch (final Exception e) {
79             LOGGER.error(START_FAILED, e);
80             return;
81         }
82
83         // Read the properties
84         final Properties props = new Properties();
85         try {
86             final String propFile = arguments.getFullPropertyFilePath();
87             try (FileInputStream stream = new FileInputStream(propFile)) {
88                 props.load(stream);
89             }
90         } catch (final Exception e) {
91             LOGGER.error(START_FAILED, e);
92             return;
93         }
94
95         // Initialize database
96         try {
97             new PapDatabaseInitializer().initializePapDatabase(parameterGroup.getDatabaseProviderParameters());
98         } catch (final PolicyPapException exp) {
99             LOGGER.error(START_FAILED + ", used parameters are {}", Arrays.toString(args), exp);
100             return;
101         }
102
103         // Now, create the activator for the policy pap service
104         activator = new PapActivator(parameterGroup, props);
105         Registry.register(PapConstants.REG_PAP_ACTIVATOR, activator);
106
107         // Start the activator
108         try {
109             activator.start();
110         } catch (final RuntimeException e) {
111             LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), e);
112             Registry.unregister(PapConstants.REG_PAP_ACTIVATOR);
113             return;
114         }
115
116         // Add a shutdown hook to shut everything down in an orderly manner
117         Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass());
118         LOGGER.info("Started policy pap service");
119     }
120
121     /**
122      * Get the parameters specified in JSON.
123      *
124      * @return the parameters
125      */
126     public PapParameterGroup getParameters() {
127         return parameterGroup;
128     }
129
130     /**
131      * Shut down Execution.
132      *
133      * @throws PolicyPapException on shutdown errors
134      */
135     public void shutdown() throws PolicyPapException {
136         // clear the parameterGroup variable
137         parameterGroup = null;
138
139         // clear the pap activator
140         if (activator != null) {
141             activator.stop();
142         }
143     }
144
145     /**
146      * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called.
147      */
148     private class PolicyPapShutdownHookClass extends Thread {
149         /*
150          * (non-Javadoc)
151          *
152          * @see java.lang.Runnable#run()
153          */
154         @Override
155         public void run() {
156             if (!activator.isAlive()) {
157                 return;
158             }
159
160             try {
161                 // Shutdown the policy pap service and wait for everything to stop
162                 activator.stop();
163             } catch (final RuntimeException e) {
164                 LOGGER.warn("error occured during shut down of the policy pap service", e);
165             }
166         }
167     }
168
169     /**
170      * The main method.
171      *
172      * @param args the arguments
173      */
174     public static void main(final String[] args) {
175         new Main(args);
176     }
177 }