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;
26 import org.onap.policy.common.utils.cmd.CommandLineException;
27 import org.onap.policy.models.sim.dmaap.DmaapSimException;
28 import org.onap.policy.models.sim.dmaap.DmaapSimRuntimeException;
29 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
30 import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterHandler;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * This class initiates the DMaaP simulator component.
39 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
41 private DmaapSimActivator activator;
43 private DmaapSimParameterGroup parameters;
46 * Instantiates the DMaap Simulator service.
48 * @param args the command line arguments
50 public Main(final String[] args) {
51 final var argumentString = Arrays.toString(args);
52 LOGGER.info("Starting DMaaP simulator service with arguments - {}", argumentString);
54 // Check the arguments
55 final var arguments = new DmaapSimCommandLineArguments();
57 // The arguments return a string if there is a message to print and we should exit
58 final String argumentMessage = arguments.parse(args);
59 if (argumentMessage != null) {
60 LOGGER.info(argumentMessage);
63 // Validate that the arguments are sane
65 } catch (final DmaapSimRuntimeException | CommandLineException e) {
66 LOGGER.error("start of DMaaP simulator service failed", e);
70 // Read the parameters
72 parameters = new DmaapSimParameterHandler().getParameters(arguments);
73 } catch (final Exception e) {
74 LOGGER.error("start of DMaaP simulator service failed", e);
78 // Now, create the activator for the DMaaP Simulator service
79 activator = new DmaapSimActivator(parameters);
81 // Start the activator
84 } catch (final RuntimeException e) {
85 LOGGER.error("start of DMaaP simulator service failed, used parameters are {}", Arrays.toString(args), e);
89 // Add a shutdown hook to shut everything down in an orderly manner
90 Runtime.getRuntime().addShutdownHook(new DmaapSimShutdownHookClass());
91 LOGGER.info("Started DMaaP simulator service");
95 * Shut down Execution.
97 * @throws DmaapSimException on shutdown errors
99 public void shutdown() throws DmaapSimException {
100 // clear the parameterGroup variable
103 // clear the DMaaP simulator activator
104 if (activator != null && activator.isAlive()) {
110 * The Class DmaapSimShutdownHookClass terminates the DMaaP simulator service when its run method is called.
112 private class DmaapSimShutdownHookClass extends Thread {
116 * @see java.lang.Runnable#run()
121 // Shutdown the DMaaP simulator service and wait for everything to stop
124 } catch (final RuntimeException | DmaapSimException e) {
125 LOGGER.warn("error occured during shut down of the DMaaP simulator service", e);
133 * @param args the arguments
135 public static void main(final String[] args) { // NOSONAR
137 * The arguments are validated by the constructor, thus sonar is disabled.