2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021, 2023 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;
26 import org.onap.policy.common.utils.cmd.CommandLineException;
27 import org.onap.policy.common.utils.services.Registry;
28 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
29 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorRunTimeException;
30 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterGroup;
31 import org.onap.policy.models.sim.pdp.parameters.PdpSimulatorParameterHandler;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * This class initiates PdpSimulator.
38 * @author Ajith Sreekumar (ajith.sreekumar@est.tech)
40 public class PdpSimulatorMain {
42 private static final String PDP_SIMULATOR_FAIL_MSG = "start of pdp simulator failed";
44 private static final Logger LOGGER = LoggerFactory.getLogger(PdpSimulatorMain.class);
46 private PdpSimulatorActivator activator;
48 private PdpSimulatorParameterGroup parameters;
51 * Instantiates the PdpSimulator.
53 * @param args the command line arguments
55 public PdpSimulatorMain(final String[] args) {
56 if (LOGGER.isInfoEnabled()) {
57 LOGGER.info("In PdpSimulator with parameters {}", Arrays.toString(args));
60 // Check the arguments
61 final var arguments = new PdpSimulatorCommandLineArguments();
63 // The arguments return a string if there is a message to print and we should exit
64 final String argumentMessage = arguments.parse(args);
65 if (argumentMessage != null) {
66 LOGGER.debug(argumentMessage);
69 // Validate that the arguments are sane
71 } catch (final PdpSimulatorRunTimeException | CommandLineException e) {
72 LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
76 // Read the parameters
78 parameters = new PdpSimulatorParameterHandler().getParameters(arguments);
79 } catch (final Exception e) {
80 LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
84 // create the activator
85 activator = new PdpSimulatorActivator(parameters);
86 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
87 // Start the activator
89 activator.initialize();
90 } catch (final PdpSimulatorException e) {
91 LOGGER.error("start of PdpSimulator failed, used parameters are {}", Arrays.toString(args), e);
92 Registry.unregister(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR);
96 // Add a shutdown hook to shut everything down in an orderly manner
97 Runtime.getRuntime().addShutdownHook(new PdpSimulatorShutdownHookClass());
99 LOGGER.info("Started PdpSimulator service");
104 * Shut down Execution.
106 * @throws PdpSimulatorException on shutdown errors
108 public void shutdown() throws PdpSimulatorException {
109 // clear the parameterGroup variable
112 // clear the pdp simulator activator
113 if (activator != null && activator.isAlive()) {
114 activator.terminate();
119 * The Class PdpSimulatorShutdownHookClass terminates the pdp simulator when its run method is called.
121 private class PdpSimulatorShutdownHookClass extends Thread {
125 * @see java.lang.Runnable#run()
130 // Shutdown the pdp simulator service and wait for everything to stop
131 if (activator != null && activator.isAlive()) {
132 activator.terminate();
134 } catch (final PdpSimulatorException e) {
135 LOGGER.warn("error occurred during shut down of the pdp simulator service", e);
141 * The main method. Arguments are validated in the constructor thus adding the NOSONAR.
143 * @param args the arguments
146 public static void main(final String[] args) { // NOSONAR
148 * The arguments are validated by the constructor, thus sonar is disabled.
151 new PdpSimulatorMain(args);