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.runtime.main.startstop;
23 import java.util.Arrays;
24 import javax.ws.rs.core.Response;
25 import org.onap.policy.clamp.controlloop.common.ControlLoopConstants;
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.runtime.main.parameters.ClRuntimeParameterGroup;
29 import org.onap.policy.clamp.controlloop.runtime.main.parameters.ClRuntimeParameterHandler;
30 import org.onap.policy.common.utils.resources.MessageConstants;
31 import org.onap.policy.common.utils.services.Registry;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * This class initiates ONAP Policy Framework Control Loop runtime component.
40 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
42 private ClRuntimeActivator activator;
43 private ClRuntimeParameterGroup parameterGroup;
46 * Instantiates the control loop runtime service.
48 * @param args the command line arguments
49 * @throws ControlLoopRuntimeException if the CLAMP runtime fails to start
51 public Main(final String[] args) {
52 final var argumentString = Arrays.toString(args);
53 LOGGER.info("Starting the control loop runtime service with arguments - {}", argumentString);
55 // Check the arguments
56 final var arguments = new ClRuntimeCommandLineArguments();
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 ClRuntimeParameterHandler().getParameters(arguments);
70 // Now, create the activator for the service
71 activator = new ClRuntimeActivator(parameterGroup);
72 Registry.register(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, activator);
74 // Start the activator
76 } catch (Exception exp) {
77 if (null != activator) {
78 Registry.unregister(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR);
80 throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST,
81 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP), exp);
84 // Add a shutdown hook to shut everything down in an orderly manner
85 Runtime.getRuntime().addShutdownHook(new ClRuntimeShutdownHookClass());
86 var successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_CLAMP);
87 LOGGER.info(successMsg);
91 * Check if main is running.
93 * @return true if the CLAMP runtime is running
95 public boolean isRunning() {
96 return activator != null && activator.isAlive();
100 * Get the parameters specified in JSON.
102 * @return the parameters
104 public ClRuntimeParameterGroup getParameters() {
105 return parameterGroup;
109 * Shut down Execution.
111 * @throws ControlLoopException on shutdown errors
113 public void shutdown() throws ControlLoopException {
114 // clear the parameterGroup variable
115 parameterGroup = null;
117 // clear the cl runtime activator
118 if (activator != null) {
124 * The Class ClRuntimeShutdownHookClass terminates the control loop runtime service when its run method is called.
126 private class ClRuntimeShutdownHookClass extends Thread {
130 * @see java.lang.Runnable#run()
134 if (!activator.isAlive()) {
139 // Shutdown the control loop runtime service and wait for everything to stop
141 } catch (final RuntimeException e) {
142 LOGGER.warn("error occured during shut down of the control loop runtime service", e);
150 * @param args the arguments
152 public static void main(final String[] args) { // NOSONAR
154 * NOTE: arguments are validated by the constructor, thus sonar is disabled.