2 * ============LICENSE_START=======================================================
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * SPDX-License-Identifier: Apache-2.0
23 * ============LICENSE_END=========================================================
26 package org.onap.policy.api.main.startstop;
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;
39 * This class initiates ONAP Policy Framework policy api.
44 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
46 // The policy api Activator that activates the policy api service
47 private ApiActivator activator;
49 // The parameters read in from JSON
50 private ApiParameterGroup parameterGroup;
53 * Instantiates the policy api service.
55 * @param args the command line arguments
57 public Main(final String[] args) {
58 final var argumentString = Arrays.toString(args);
59 LOGGER.info("Starting policy api service with arguments - {}", argumentString);
61 // Check the arguments
62 final var arguments = new ApiCommandLineArguments();
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);
71 // Validate that the arguments are sane
74 // Read the parameters
75 parameterGroup = new ApiParameterHandler().getParameters(arguments);
76 // Initialize database
77 new ApiDatabaseInitializer().initializeApiDatabase(parameterGroup);
79 // Now, create the activator for the policy api service
80 activator = new ApiActivator(parameterGroup);
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);
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);
96 * Get the parameters specified in JSON.
98 * @return the parameters
100 public ApiParameterGroup getParameters() {
101 return parameterGroup;
105 * Shut down Execution.
107 * @throws PolicyApiException on shutdown errors
109 public void shutdown() throws PolicyApiException {
110 // clear the parameterGroup variable
111 parameterGroup = null;
113 // clear the api activator
114 if (activator != null) {
115 activator.terminate();
120 * The Class PolicyApiShutdownHookClass terminates the policy api service when its run method is
123 private class PolicyApiShutdownHookClass extends Thread {
127 * @see java.lang.Runnable#run()
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);
143 * @param args the arguments
145 public static void main(final String[] args) {