2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.policy.api.main.startstop;
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;
34 * This class initiates ONAP Policy Framework policy api.
39 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
41 // The policy api Activator that activates the policy api service
42 private ApiActivator activator;
44 // The parameters read in from JSON
45 private ApiParameterGroup parameterGroup;
48 * Instantiates the policy api service.
50 * @param args the command line arguments
52 public Main(final String[] args) {
53 final String argumentString = Arrays.toString(args);
54 LOGGER.info("Starting policy api service with arguments - {}", argumentString);
56 // Check the arguments
57 final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
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);
66 // Validate that the arguments are sane
68 } catch (final PolicyApiException e) {
69 LOGGER.error("start of policy api service failed", e);
73 // Read the parameters
75 parameterGroup = new ApiParameterHandler().getParameters(arguments);
76 } catch (final Exception e) {
77 LOGGER.error("start of policy api service failed", e);
81 // Initialize database
83 new ApiDatabaseInitializer().initializeApiDatabase(parameterGroup.getDatabaseProviderParameters());
84 } catch (final PolicyApiException e) {
85 LOGGER.error("Preloading policy types into DB failed", e);
89 // Now, create the activator for the policy api service
90 activator = new ApiActivator(parameterGroup);
92 // Start the activator
94 activator.initialize();
95 } catch (final PolicyApiException e) {
96 LOGGER.error("start of policy api service failed, used parameters are {} ", argumentString, e);
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");
106 * Get the parameters specified in JSON.
108 * @return the parameters
110 public ApiParameterGroup getParameters() {
111 return parameterGroup;
115 * Shut down Execution.
117 * @throws PolicyApiException on shutdown errors
119 public void shutdown() throws PolicyApiException {
120 // clear the parameterGroup variable
121 parameterGroup = null;
123 // clear the api activator
124 if (activator != null) {
125 activator.terminate();
130 * The Class PolicyApiShutdownHookClass terminates the policy api service when its run method is
133 private class PolicyApiShutdownHookClass extends Thread {
137 * @see java.lang.Runnable#run()
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);
153 * @param args the arguments
155 public static void main(final String[] args) {