2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. 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.distribution.main.startstop;
23 import java.util.Arrays;
25 import org.onap.policy.common.logging.flexlogger.FlexLogger;
26 import org.onap.policy.common.logging.flexlogger.Logger;
27 import org.onap.policy.distribution.main.PolicyDistributionException;
28 import org.onap.policy.distribution.main.parameters.DistributionParameterGroup;
29 import org.onap.policy.distribution.main.parameters.DistributionParameterHandler;
32 * This class initiates ONAP Policy Framework policy distribution.
34 * @author Liam Fallon (liam.fallon@ericsson.com)
37 private static final Logger LOGGER = FlexLogger.getLogger(Main.class);
39 // The policy distribution Activator that activates the policy distribution service
40 private DistributionActivator activator;
42 // The parameters read in from JSON
43 private DistributionParameterGroup parameterGroup;
46 * Instantiates the policy distribution service.
48 * @param args the command line arguments
50 public Main(final String[] args) {
51 final String argumentString = Arrays.toString(args);
52 LOGGER.info("Starting policy distribution service with arguments - " + argumentString);
54 // Check the arguments
55 final DistributionCommandLineArguments arguments = new DistributionCommandLineArguments();
57 // The arguments return a string if there is a message to print and we should exit
58 final String argumentMessage = arguments.parse(args);
59 if (argumentMessage != null) {
60 LOGGER.info(argumentMessage);
64 // Validate that the arguments are sane
66 } catch (final PolicyDistributionException e) {
67 LOGGER.error("start of policy distribution service failed", e);
71 // Read the parameters
73 parameterGroup = new DistributionParameterHandler().getParameters(arguments);
74 } catch (final Exception e) {
75 LOGGER.error("start of policy distribution service failed", e);
79 // Now, create the activator for the policy distribution service
80 activator = new DistributionActivator(parameterGroup);
82 // Start the activator
84 activator.initialize();
85 } catch (final PolicyDistributionException e) {
86 LOGGER.error("start of policy distribution service failed, used parameters are " + Arrays.toString(args),
91 // Add a shutdown hook to shut everything down in an orderly manner
92 Runtime.getRuntime().addShutdownHook(new PolicyDistributionShutdownHookClass());
93 LOGGER.info("Started policy distribution service");
97 * Get the parameters specified in JSON.
99 * @return the parameters
101 public DistributionParameterGroup getParameters() {
102 return parameterGroup;
106 * Shut down Execution.
108 * @throws PolicyDistributionException on shutdown errors
110 public void shutdown() throws PolicyDistributionException {
111 // clear the parameterGroup variable
112 parameterGroup = null;
114 // clear the distribution activator
115 if (activator != null) {
116 activator.terminate();
121 * The Class PolicyDistributionShutdownHookClass terminates the policy distribution service when its run method is
124 private class PolicyDistributionShutdownHookClass extends Thread {
128 * @see java.lang.Runnable#run()
133 // Shutdown the policy distribution service and wait for everything to stop
134 activator.terminate();
135 } catch (final PolicyDistributionException e) {
136 LOGGER.warn("error occured during shut down of the policy distribution service", e);
144 * @param args the arguments
146 public static void main(final String[] args) {