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.models.sim.pdp;
23 import java.io.FileInputStream;
24 import java.util.Arrays;
25 import java.util.Properties;
28 import org.onap.policy.common.utils.services.Registry;
29 import org.onap.policy.models.sim.pdp.exception.PdpSimulatorException;
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;
47 private PdpSimulatorParameterGroup parameterGroup;
50 * Instantiates the PdpSimulator.
52 * @param args the command line arguments
54 public PdpSimulatorMain(final String[] args) {
55 LOGGER.info("In PdpSimulator with parameters ", Arrays.toString(args));
57 // Check the arguments
58 final PdpSimulatorCommandLineArguments arguments = new PdpSimulatorCommandLineArguments();
60 // The arguments return a string if there is a message to print and we should exit
61 final String argumentMessage = arguments.parse(args);
62 if (argumentMessage != null) {
63 LOGGER.debug(argumentMessage);
66 // Validate that the arguments are sane
68 } catch (final PdpSimulatorException e) {
69 LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
73 // Read the parameters
75 parameterGroup = new PdpSimulatorParameterHandler().getParameters(arguments);
76 } catch (final Exception e) {
77 LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
81 // Read the properties
82 final Properties topicProperties = new Properties();
84 final String propFile = arguments.getFullPropertyFilePath();
85 try (FileInputStream stream = new FileInputStream(propFile)) {
86 topicProperties.load(stream);
88 } catch (final Exception e) {
89 LOGGER.error(PDP_SIMULATOR_FAIL_MSG, e);
93 // create the activator
94 activator = new PdpSimulatorActivator(parameterGroup, topicProperties);
95 Registry.register(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR, activator);
96 // Start the activator
98 activator.initialize();
99 } catch (final PdpSimulatorException e) {
100 LOGGER.error("start of PdpSimulator failed, used parameters are {}", Arrays.toString(args), e);
101 Registry.unregister(PdpSimulatorConstants.REG_PDP_SIMULATOR_ACTIVATOR);
105 // Add a shutdown hook to shut everything down in an orderly manner
106 Runtime.getRuntime().addShutdownHook(new PdpSimulatorShutdownHookClass());
108 LOGGER.info("Started PdpSimulator service");
112 * Get the parameters specified in JSON.
114 * @return parameterGroup the parameters
116 public PdpSimulatorParameterGroup getParameters() {
117 return parameterGroup;
122 * Shut down Execution.
124 * @throws PdpSimulatorException on shutdown errors
126 public void shutdown() throws PdpSimulatorException {
127 // clear the parameterGroup variable
128 parameterGroup = null;
130 // clear the pdp simulator activator
131 if (activator != null && activator.isAlive()) {
132 activator.terminate();
137 * The Class PdpSimulatorShutdownHookClass terminates the pdp simulator when its run method is called.
139 private class PdpSimulatorShutdownHookClass extends Thread {
143 * @see java.lang.Runnable#run()
148 // Shutdown the pdp simulator service and wait for everything to stop
149 if (activator != null && activator.isAlive()) {
150 activator.terminate();
152 } catch (final PdpSimulatorException e) {
153 LOGGER.warn("error occured during shut down of the pdp simulator service", e);
161 * @param args the arguments
164 public static void main(final String[] args) {
165 new PdpSimulatorMain(args);