8e60d68cf4ba82b50a54c142203f6ffd95c7a632
[policy/clamp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.clamp.controlloop.runtime.main.startstop;
22
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;
34
35 /**
36  * This class initiates ONAP Policy Framework Control Loop runtime component.
37  */
38 public class Main {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
41
42     private ClRuntimeActivator activator;
43     private ClRuntimeParameterGroup parameterGroup;
44
45     /**
46      * Instantiates the control loop runtime service.
47      *
48      * @param args the command line arguments
49      */
50     public Main(final String[] args) {
51         final String argumentString = Arrays.toString(args);
52         LOGGER.info("Starting the control loop runtime service with arguments - {}", argumentString);
53
54         // Check the arguments
55         final ClRuntimeCommandLineArguments arguments = new ClRuntimeCommandLineArguments();
56         try {
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);
61                 return;
62             }
63             // Validate that the arguments are sane
64             arguments.validate();
65
66             // Read the parameters
67             parameterGroup = new ClRuntimeParameterHandler().getParameters(arguments);
68
69             // Now, create the activator for the service
70             activator = new ClRuntimeActivator(parameterGroup);
71             Registry.register(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, activator);
72
73             // Start the activator
74             activator.start();
75         } catch (Exception exp) {
76             if (null != activator) {
77                 Registry.unregister(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR);
78             }
79             throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST,
80                 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP), exp);
81         }
82
83         // Add a shutdown hook to shut everything down in an orderly manner
84         Runtime.getRuntime().addShutdownHook(new ClRuntimeShutdownHookClass());
85         String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_CLAMP);
86         LOGGER.info(successMsg);
87     }
88
89     /**
90      * Check if main is running.
91      */
92     public boolean isRunning() {
93         return activator != null && activator.isAlive();
94     }
95
96     /**
97      * Get the parameters specified in JSON.
98      *
99      * @return the parameters
100      */
101     public ClRuntimeParameterGroup getParameters() {
102         return parameterGroup;
103     }
104
105     /**
106      * Shut down Execution.
107      *
108      * @throws ControlLoopException on shutdown errors
109      */
110     public void shutdown() throws ControlLoopException {
111         // clear the parameterGroup variable
112         parameterGroup = null;
113
114         // clear the cl runtime activator
115         if (activator != null) {
116             activator.stop();
117         }
118     }
119
120     /**
121      * The Class ClRuntimeShutdownHookClass terminates the control loop runtime service when its run method is called.
122      */
123     private class ClRuntimeShutdownHookClass extends Thread {
124         /*
125          * (non-Javadoc)
126          *
127          * @see java.lang.Runnable#run()
128          */
129         @Override
130         public void run() {
131             if (!activator.isAlive()) {
132                 return;
133             }
134
135             try {
136                 // Shutdown the control loop runtime service and wait for everything to stop
137                 activator.stop();
138             } catch (final RuntimeException e) {
139                 LOGGER.warn("error occured during shut down of the control loop runtime service", e);
140             }
141         }
142     }
143
144     /**
145      * The main method.
146      *
147      * @param args the arguments
148      */
149     public static void main(final String[] args) {      // NOSONAR
150         /*
151          * NOTE: arguments are validated by the constructor, thus sonar is disabled.
152          */
153
154         new Main(args);
155     }
156 }