2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.api.main.startstop;
23 import java.util.Arrays;
24 import org.onap.policy.api.main.PolicyApiException;
25 import org.onap.policy.api.main.parameters.ApiParameterGroup;
26 import org.onap.policy.api.main.parameters.ApiParameterHandler;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
31 * This class initiates ONAP Policy Framework policy api.
35 private static final Logger LOGGER = FlexLogger.getLogger(Main.class);
37 // The policy api Activator that activates the policy api service
38 private ApiActivator activator;
40 // The parameters read in from JSON
41 private ApiParameterGroup parameterGroup;
44 * Instantiates the policy api service.
46 * @param args the command line arguments
48 public Main(final String[] args) {
49 final String argumentString = Arrays.toString(args);
50 LOGGER.info("Starting policy api service with arguments - " + argumentString);
52 // Check the arguments
53 final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
55 // The arguments return a string if there is a message to print and we should exit
56 final String argumentMessage = arguments.parse(args);
57 if (argumentMessage != null) {
58 LOGGER.info(argumentMessage);
62 // Validate that the arguments are sane
64 } catch (final PolicyApiException e) {
65 LOGGER.error("start of policy api service failed", e);
69 // Read the parameters
71 parameterGroup = new ApiParameterHandler().getParameters(arguments);
72 } catch (final Exception e) {
73 LOGGER.error("start of policy api service failed", e);
77 // Now, create the activator for the policy api service
78 activator = new ApiActivator(parameterGroup);
80 // Start the activator
82 activator.initialize();
83 } catch (final PolicyApiException e) {
84 LOGGER.error("start of policy api service failed, used parameters are " + Arrays.toString(args),
89 // Add a shutdown hook to shut everything down in an orderly manner
90 Runtime.getRuntime().addShutdownHook(new PolicyApiShutdownHookClass());
91 LOGGER.info("Started policy api service");
95 * Get the parameters specified in JSON.
97 * @return the parameters
99 public ApiParameterGroup getParameters() {
100 return parameterGroup;
104 * Shut down Execution.
106 * @throws PolicyApiException on shutdown errors
108 public void shutdown() throws PolicyApiException {
109 // clear the parameterGroup variable
110 parameterGroup = null;
112 // clear the api activator
113 if (activator != null) {
114 activator.terminate();
119 * The Class PolicyApiShutdownHookClass terminates the policy api service when its run method is
122 private class PolicyApiShutdownHookClass extends Thread {
126 * @see java.lang.Runnable#run()
131 // Shutdown the policy api service and wait for everything to stop
132 activator.terminate();
133 } catch (final PolicyApiException e) {
134 LOGGER.warn("error occured during shut down of the policy api service", e);
142 * @param args the arguments
144 public static void main(final String[] args) {