Added changes to make the list of preloaded policy types configurable
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / startstop / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API
4  * ================================================================================
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.startstop;
25
26 import java.util.Arrays;
27 import org.onap.policy.api.main.exception.PolicyApiException;
28 import org.onap.policy.api.main.parameters.ApiParameterGroup;
29 import org.onap.policy.api.main.parameters.ApiParameterHandler;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This class initiates ONAP Policy Framework policy api.
35  *
36  */
37 public class Main {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
40
41     // The policy api Activator that activates the policy api service
42     private ApiActivator activator;
43
44     // The parameters read in from JSON
45     private ApiParameterGroup parameterGroup;
46
47     /**
48      * Instantiates the policy api service.
49      *
50      * @param args the command line arguments
51      */
52     public Main(final String[] args) {
53         final String argumentString = Arrays.toString(args);
54         LOGGER.info("Starting policy api service with arguments - {}", argumentString);
55
56         // Check the arguments
57         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
58         try {
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);
63                 return;
64             }
65
66             // Validate that the arguments are sane
67             arguments.validate();
68         } catch (final PolicyApiException e) {
69             LOGGER.error("start of policy api service failed", e);
70             return;
71         }
72
73         // Read the parameters
74         try {
75             parameterGroup = new ApiParameterHandler().getParameters(arguments);
76         } catch (final Exception e) {
77             LOGGER.error("start of policy api service failed", e);
78             return;
79         }
80
81         // Initialize database
82         try {
83             new ApiDatabaseInitializer().initializeApiDatabase(parameterGroup);
84         } catch (final PolicyApiException e) {
85             LOGGER.error("Preloading policy types into DB failed", e);
86             return;
87         }
88
89         // Now, create the activator for the policy api service
90         activator = new ApiActivator(parameterGroup);
91
92         // Start the activator
93         try {
94             activator.initialize();
95         } catch (final PolicyApiException e) {
96             LOGGER.error("start of policy api service failed, used parameters are {} ", argumentString, e);
97             return;
98         }
99
100         // Add a shutdown hook to shut everything down in an orderly manner
101         Runtime.getRuntime().addShutdownHook(new PolicyApiShutdownHookClass());
102         LOGGER.info("Started policy api service");
103     }
104
105     /**
106      * Get the parameters specified in JSON.
107      *
108      * @return the parameters
109      */
110     public ApiParameterGroup getParameters() {
111         return parameterGroup;
112     }
113
114     /**
115      * Shut down Execution.
116      *
117      * @throws PolicyApiException on shutdown errors
118      */
119     public void shutdown() throws PolicyApiException {
120         // clear the parameterGroup variable
121         parameterGroup = null;
122
123         // clear the api activator
124         if (activator != null) {
125             activator.terminate();
126         }
127     }
128
129     /**
130      * The Class PolicyApiShutdownHookClass terminates the policy api service when its run method is
131      * called.
132      */
133     private class PolicyApiShutdownHookClass extends Thread {
134         /*
135          * (non-Javadoc)
136          *
137          * @see java.lang.Runnable#run()
138          */
139         @Override
140         public void run() {
141             try {
142                 // Shutdown the policy api service and wait for everything to stop
143                 activator.terminate();
144             } catch (final PolicyApiException e) {
145                 LOGGER.warn("error occured during shut down of the policy api service", e);
146             }
147         }
148     }
149
150     /**
151      * The main method.
152      *
153      * @param args the arguments
154      */
155     public static void main(final String[] args) {
156         new Main(args);
157     }
158 }