2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.sim.pdp;
24 import java.util.Arrays;
25 import org.onap.policy.common.utils.services.Registry;
26 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
27 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
28 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * This class initiates PdpSimulator.
35 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
37 public class PdpSimulatorMain {
39 private static final String PDP_SIMULATOR_FAIL_MSG = "start of pdp simulator failed";
41 private static final Logger LOGGER = LoggerFactory.getLogger(PdpSimulatorMain.class);
43 private PdpSimulatorActivator activator;
44 private PdpSimulatorParameterGroup parameterGroup;
47 * Instantiates the PdpSimulator.
49 * @param args the command line arguments
51 public PdpSimulatorMain(final String[] args) {
52 if (LOGGER.isInfoEnabled()) {
53 LOGGER.info("In PdpSimulator with parameters {}", Arrays.toString(args));
56 // Check the arguments
57 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
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.debug(argumentMessage);
65 // Validate that the arguments are sane
67 } catch (final PdpSimulatorException e) {
68 LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
72 // Read the parameters
74 parameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
75 } catch (final Exception e) {
76 LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
80 // create the activator
81 activator = new PdpSimulatorActivator(parameterGroup);
82 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
83 // Start the activator
85 activator.initialize();
86 } catch (final PdpSimulatorException e) {
87 LOGGER.error("start of PdpSimulator failed, used parameters are {}", Arrays.toString(args), e);
88 Registry.unregister(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR);
92 // Add a shutdown hook to shut everything down in an orderly manner
93 Runtime.getRuntime().addShutdownHook(new PdpSimulatorShutdownHookClass());
95 LOGGER.info("Started PdpSimulator service");
99 * Get the parameters specified in JSON.
101 * @return parameterGroup the parameters
103 public PdpSimulatorParameterGroup getParameters() {
104 return parameterGroup;
109 * Shut down Execution.
111 * @throws PdpSimulatorException on shutdown errors
113 public void shutdown() throws PdpSimulatorException {
114 // clear the parameterGroup variable
115 parameterGroup = null;
117 // clear the pdp simulator activator
118 if (activator != null && activator.isAlive()) {
119 activator.terminate();
124 * The Class PdpSimulatorShutdownHookClass terminates the pdp simulator when its run method is called.
126 private class PdpSimulatorShutdownHookClass extends Thread {
130 * @see java.lang.Runnable#run()
135 // Shutdown the pdp simulator service and wait for everything to stop
136 if (activator != null && activator.isAlive()) {
137 activator.terminate();
139 } catch (final PdpSimulatorException e) {
140 LOGGER.warn("error occured during shut down of the pdp simulator service", e);
146 * The main method. Arguments are validated in the constructor thus adding the NOSONAR.
148 * @param args the arguments
151 public static void main(final String[] args) { // NOSONAR
153 * The arguments are validated by the constructor, thus sonar is disabled.
156 new PdpSimulatorMain(args);