2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
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.pap.main.startstop;
23 import java.util.Arrays;
25 import org.onap.policy.pap.main.PolicyPapException;
26 import org.onap.policy.pap.main.parameters.PapParameterGroup;
27 import org.onap.policy.pap.main.parameters.PapParameterHandler;
28 import org.slf4j.ext.XLogger;
29 import org.slf4j.ext.XLoggerFactory;
32 * This class initiates ONAP Policy Framework PAP component.
34 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
37 private static final XLogger LOGGER = XLoggerFactory.getXLogger(Main.class);
39 private PapActivator activator;
40 private PapParameterGroup parameterGroup;
43 * Instantiates the policy pap service.
45 * @param args the command line arguments
47 public Main(final String[] args) {
48 final String argumentString = Arrays.toString(args);
49 LOGGER.info("Starting policy pap service with arguments - {}", argumentString);
51 // Check the arguments
52 final PapCommandLineArguments arguments = new PapCommandLineArguments();
54 // The arguments return a string if there is a message to print and we should exit
55 final String argumentMessage = arguments.parse(args);
56 if (argumentMessage != null) {
57 LOGGER.info(argumentMessage);
60 // Validate that the arguments are sane
62 } catch (final PolicyPapException e) {
63 LOGGER.error("start of policy pap service failed", e);
67 // Read the parameters
69 parameterGroup = new PapParameterHandler().getParameters(arguments);
70 } catch (final Exception e) {
71 LOGGER.error("start of policy pap service failed", e);
75 // Now, create the activator for the policy pap service
76 activator = new PapActivator(parameterGroup);
78 // Start the activator
80 activator.initialize();
81 } catch (final PolicyPapException e) {
82 LOGGER.error("start of policy pap service failed, used parameters are {}", Arrays.toString(args), e);
86 // Add a shutdown hook to shut everything down in an orderly manner
87 Runtime.getRuntime().addShutdownHook(new PolicyPapShutdownHookClass());
88 LOGGER.info("Started policy pap service");
92 * Get the parameters specified in JSON.
94 * @return the parameters
96 public PapParameterGroup getParameters() {
97 return parameterGroup;
101 * Shut down Execution.
103 * @throws PolicyPapException on shutdown errors
105 public void shutdown() throws PolicyPapException {
106 // clear the parameterGroup variable
107 parameterGroup = null;
109 // clear the pap activator
110 if (activator != null) {
111 activator.terminate();
116 * The Class PolicyPapShutdownHookClass terminates the policy pap service when its run method is called.
118 private class PolicyPapShutdownHookClass extends Thread {
122 * @see java.lang.Runnable#run()
127 // Shutdown the policy pap service and wait for everything to stop
128 activator.terminate();
129 } catch (final PolicyPapException e) {
130 LOGGER.warn("error occured during shut down of the policy pap service", e);
138 * @param args the arguments
140 public static void main(final String[] args) {