2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2021 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.cmd.CommandLineException;
26 import org.onap.policy.common.utils.services.Registry;
27 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
28 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorRunTimeException;
29 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
30 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * This class initiates PdpSimulator.
37 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
39 public class PdpSimulatorMain {
41 private static final String PDP_SIMULATOR_FAIL_MSG = "start of pdp simulator failed";
43 private static final Logger LOGGER = LoggerFactory.getLogger(PdpSimulatorMain.class);
45 private PdpSimulatorActivator activator;
46 private PdpSimulatorParameterGroup parameterGroup;
49 * Instantiates the PdpSimulator.
51 * @param args the command line arguments
53 public PdpSimulatorMain(final String[] args) {
54 if (LOGGER.isInfoEnabled()) {
55 LOGGER.info("In PdpSimulator with parameters {}", Arrays.toString(args));
58 // Check the arguments
59 final var arguments = new PdpSimulatorCommandLineArguments();
61 // The arguments return a string if there is a message to print and we should exit
62 final String argumentMessage = arguments.parse(args);
63 if (argumentMessage != null) {
64 LOGGER.debug(argumentMessage);
67 // Validate that the arguments are sane
69 } catch (final PdpSimulatorRunTimeException | CommandLineException e) {
70 LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
74 // Read the parameters
76 parameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
77 } catch (final Exception e) {
78 LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
82 // create the activator
83 activator = new PdpSimulatorActivator(parameterGroup);
84 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
85 // Start the activator
87 activator.initialize();
88 } catch (final PdpSimulatorException e) {
89 LOGGER.error("start of PdpSimulator failed, used parameters are {}", Arrays.toString(args), e);
90 Registry.unregister(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR);
94 // Add a shutdown hook to shut everything down in an orderly manner
95 Runtime.getRuntime().addShutdownHook(new PdpSimulatorShutdownHookClass());
97 LOGGER.info("Started PdpSimulator service");
101 * Get the parameters specified in JSON.
103 * @return parameterGroup the parameters
105 public PdpSimulatorParameterGroup getParameters() {
106 return parameterGroup;
111 * Shut down Execution.
113 * @throws PdpSimulatorException on shutdown errors
115 public void shutdown() throws PdpSimulatorException {
116 // clear the parameterGroup variable
117 parameterGroup = null;
119 // clear the pdp simulator activator
120 if (activator != null && activator.isAlive()) {
121 activator.terminate();
126 * The Class PdpSimulatorShutdownHookClass terminates the pdp simulator when its run method is called.
128 private class PdpSimulatorShutdownHookClass extends Thread {
132 * @see java.lang.Runnable#run()
137 // Shutdown the pdp simulator service and wait for everything to stop
138 if (activator != null && activator.isAlive()) {
139 activator.terminate();
141 } catch (final PdpSimulatorException e) {
142 LOGGER.warn("error occured during shut down of the pdp simulator service", e);
148 * The main method. Arguments are validated in the constructor thus adding the NOSONAR.
150 * @param args the arguments
153 public static void main(final String[] args) { // NOSONAR
155 * The arguments are validated by the constructor, thus sonar is disabled.
158 new PdpSimulatorMain(args);