2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.clamp.controlloop.participant.simulator.main.startstop;
23 import java.util.Arrays;
24 import javax.ws.rs.core.Response;
26 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
27 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
28 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.ParticipantSimulatorParameterHandler;
29 import org.onap.policy.clamp.controlloop.participant.simulator.main.parameters.ParticipantSimulatorParameters;
30 import org.onap.policy.common.utils.resources.MessageConstants;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * This class initiates participant simulator.
39 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
41 private ParticipantSimulatorActivator activator;
44 private ParticipantSimulatorParameters parameterGroup;
47 * Instantiates the control loop participant service.
49 * @param args the command line arguments
51 public Main(final String[] args) {
52 final String argumentString = Arrays.toString(args);
53 LOGGER.info("Starting the participant service with arguments - {}", argumentString);
55 // Check the arguments
56 final ParticipantSimulatorCommandLineArguments arguments = new ParticipantSimulatorCommandLineArguments();
58 // The arguments return a string if there is a message to print and we should exit
59 final String argumentMessage = arguments.parse(args);
60 if (argumentMessage != null) {
61 LOGGER.info(argumentMessage);
64 // Validate that the arguments are sane
67 // Read the parameters
68 parameterGroup = new ParticipantSimulatorParameterHandler().getParameters(arguments);
70 // Now, create the activator for the service
71 activator = new ParticipantSimulatorActivator(parameterGroup);
73 // Start the activator
75 } catch (Exception exp) {
76 throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST,
77 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP), exp);
80 // Add a shutdown hook to shut everything down in an orderly manner
81 Runtime.getRuntime().addShutdownHook(new ClParticipantSimulatorShutdownHookClass());
82 String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_CLAMP);
83 LOGGER.info(successMsg);
87 * Check if main is running.
89 public boolean isRunning() {
90 return activator != null && activator.isAlive();
94 * Shut down Execution.
96 * @throws ControlLoopException on shutdown errors
98 public void shutdown() throws ControlLoopException {
99 // clear the parameterGroup variable
100 parameterGroup = null;
102 // clear the cl participant activator
103 if (activator != null) {
109 * The Class ClParticipantSimulatorShutdownHookClass terminates the control loop participant service
110 * when its run method is called.
112 private class ClParticipantSimulatorShutdownHookClass extends Thread {
116 * @see java.lang.Runnable#run()
121 // Shutdown the participant simulator and wait for everything to stop
123 } catch (final RuntimeException | ControlLoopException e) {
124 LOGGER.warn("error occured during shut down of the participant simulator", e);
132 * @param args the arguments
134 public static void main(final String[] args) { // NOSONAR
136 * NOTE: arguments are validated by the constructor, thus sonar is disabled.