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 // Now, create the activator for the policy api service
82 activator = new ApiActivator(parameterGroup);
84 // Start the activator
86 activator.initialize();
87 } catch (final PolicyApiException e) {
88 LOGGER.error("start of policy api service failed, used parameters are {} ", argumentString, e);
92 // Add a shutdown hook to shut everything down in an orderly manner
93 Runtime.getRuntime().addShutdownHook(new PolicyApiShutdownHookClass());
94 LOGGER.info("Started policy api service");
98 * Get the parameters specified in JSON.
100 * @return the parameters
102 public ApiParameterGroup getParameters() {
103 return parameterGroup;
107 * Shut down Execution.
109 * @throws PolicyApiException on shutdown errors
111 public void shutdown() throws PolicyApiException {
112 // clear the parameterGroup variable
113 parameterGroup = null;
115 // clear the api activator
116 if (activator != null) {
117 activator.terminate();
122 * The Class PolicyApiShutdownHookClass terminates the policy api service when its run method is
125 private class PolicyApiShutdownHookClass extends Thread {
129 * @see java.lang.Runnable#run()
134 // Shutdown the policy api service and wait for everything to stop
135 activator.terminate();
136 } catch (final PolicyApiException e) {
137 LOGGER.warn("error occured during shut down of the policy api service", e);
145 * @param args the arguments
147 public static void main(final String[] args) {