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.dcae.main.startstop;
23 import java.util.Arrays;
24 import javax.ws.rs.core.Response;
26 import org.onap.policy.clamp.controlloop.common.ControlLoopConstants;
27 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopException;
28 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
29 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameterHandler;
30 import org.onap.policy.clamp.controlloop.participant.dcae.main.parameters.ParticipantDcaeParameters;
31 import org.onap.policy.common.utils.resources.MessageConstants;
32 import org.onap.policy.common.utils.services.Registry;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * This class initiates ONAP Policy Framework Control Loop participant component.
41 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
43 private ParticipantDcaeActivator activator;
46 private ParticipantDcaeParameters parameterGroup;
49 * Instantiates the control loop participant service.
51 * @param args the command line arguments
53 public Main(final String[] args) {
54 final String argumentString = Arrays.toString(args);
55 LOGGER.info("Starting the control loop participant service with arguments - {}", argumentString);
57 // Check the arguments
58 final ParticipantDcaeCommandLineArguments arguments = new ParticipantDcaeCommandLineArguments();
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.info(argumentMessage);
66 // Validate that the arguments are sane
69 // Read the parameters
70 parameterGroup = new ParticipantDcaeParameterHandler().getParameters(arguments);
72 // Now, create the activator for the service
73 activator = new ParticipantDcaeActivator(parameterGroup);
74 Registry.register(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, activator);
76 // Start the activator
78 } catch (Exception exp) {
79 if (null != activator) {
80 Registry.unregister(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR);
82 throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST,
83 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP), exp);
86 // Add a shutdown hook to shut everything down in an orderly manner
87 Runtime.getRuntime().addShutdownHook(new ClRuntimeShutdownHookClass());
88 String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_CLAMP);
89 LOGGER.info(successMsg);
93 * Check if main is running.
95 public boolean isRunning() {
96 return activator != null && activator.isAlive();
100 * Shut down Execution.
102 * @throws ControlLoopException on shutdown errors
104 public void shutdown() throws ControlLoopException {
105 // clear the parameterGroup variable
106 parameterGroup = null;
108 // clear the cl participant activator
109 if (activator != null) {
115 * The Class ClRuntimeShutdownHookClass terminates the control loop participant service
116 * when its run method is called.
118 private class ClRuntimeShutdownHookClass extends Thread {
122 * @see java.lang.Runnable#run()
126 if (!activator.isAlive()) {
131 // Shutdown the control loop participant service and wait for everything to stop
133 } catch (final RuntimeException e) {
134 LOGGER.warn("error occured during shut down of the control loop participant service", e);
142 * @param args the arguments
144 public static void main(final String[] args) { // NOSONAR
146 * NOTE: arguments are validated by the constructor, thus sonar is disabled.