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