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.dmaap.startstop;
24 import java.util.Arrays;
25 import org.onap.policy.common.utils.cmd.CommandLineException;
26 import org.onap.policy.models.sim.dmaap.DmaapSimException;
27 import org.onap.policy.models.sim.dmaap.DmaapSimRuntimeException;
28 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
29 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterHandler;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * This class initiates the DMaaP simulator component.
38 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
40 private DmaapSimActivator activator;
41 private DmaapSimParameterGroup parameterGroup;
44 * Instantiates the DMaap Simulator service.
46 * @param args the command line arguments
48 public Main(final String[] args) {
49 final var argumentString = Arrays.toString(args);
50 LOGGER.info("Starting DMaaP simulator service with arguments - {}", argumentString);
52 // Check the arguments
53 final var arguments = new DmaapSimCommandLineArguments();
55 // The arguments return a string if there is a message to print and we should exit
56 final String argumentMessage = arguments.parse(args);
57 if (argumentMessage != null) {
58 LOGGER.info(argumentMessage);
61 // Validate that the arguments are sane
63 } catch (final DmaapSimRuntimeException | CommandLineException e) {
64 LOGGER.error("start of DMaaP simulator service failed", e);
68 // Read the parameters
70 parameterGroup = new DmaapSimParameterHandler().getParameters(arguments);
71 } catch (final Exception e) {
72 LOGGER.error("start of DMaaP simulator service failed", e);
76 // Now, create the activator for the DMaaP Simulator service
77 activator = new DmaapSimActivator(parameterGroup);
79 // Start the activator
82 } catch (final RuntimeException e) {
83 LOGGER.error("start of DMaaP simulator service failed, used parameters are {}", Arrays.toString(args), e);
87 // Add a shutdown hook to shut everything down in an orderly manner
88 Runtime.getRuntime().addShutdownHook(new DmaapSimShutdownHookClass());
89 LOGGER.info("Started DMaaP simulator service");
93 * Get the parameters specified in JSON.
95 * @return the parameters
97 public DmaapSimParameterGroup getParameters() {
98 return parameterGroup;
102 * Shut down Execution.
104 * @throws DmaapSimException on shutdown errors
106 public void shutdown() throws DmaapSimException {
107 // clear the parameterGroup variable
108 parameterGroup = null;
110 // clear the DMaaP simulator activator
111 if (activator != null && activator.isAlive()) {
117 * The Class DmaapSimShutdownHookClass terminates the DMaaP simulator service when its run method is called.
119 private class DmaapSimShutdownHookClass extends Thread {
123 * @see java.lang.Runnable#run()
128 // Shutdown the DMaaP simulator service and wait for everything to stop
131 } catch (final RuntimeException | DmaapSimException e) {
132 LOGGER.warn("error occured during shut down of the DMaaP simulator service", e);
140 * @param args the arguments
142 public static void main(final String[] args) { // NOSONAR
144 * The arguments are validated by the constructor, thus sonar is disabled.