de1ce6f5d46d91523296ebe2bcfa93dad6aa572d
[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      * @throws ControlLoopRuntimeException if the CLAMP runtime fails to start
50      */
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);
54
55         // Check the arguments
56         final var arguments = new ClRuntimeCommandLineArguments();
57         try {
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);
62                 return;
63             }
64             // Validate that the arguments are sane
65             arguments.validate();
66
67             // Read the parameters
68             parameterGroup = new ClRuntimeParameterHandler().getParameters(arguments);
69
70             // Now, create the activator for the service
71             activator = new ClRuntimeActivator(parameterGroup);
72             Registry.register(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, activator);
73
74             // Start the activator
75             activator.start();
76         } catch (Exception exp) {
77             if (null != activator) {
78                 Registry.unregister(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR);
79             }
80             throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST,
81                 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP), exp);
82         }
83
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);
88     }
89
90     /**
91      * Check if main is running.
92      *
93      * @return true if the CLAMP runtime is running
94      */
95     public boolean isRunning() {
96         return activator != null && activator.isAlive();
97     }
98
99     /**
100      * Get the parameters specified in JSON.
101      *
102      * @return the parameters
103      */
104     public ClRuntimeParameterGroup getParameters() {
105         return parameterGroup;
106     }
107
108     /**
109      * Shut down Execution.
110      *
111      * @throws ControlLoopException on shutdown errors
112      */
113     public void shutdown() throws ControlLoopException {
114         // clear the parameterGroup variable
115         parameterGroup = null;
116
117         // clear the cl runtime activator
118         if (activator != null) {
119             activator.stop();
120         }
121     }
122
123     /**
124      * The Class ClRuntimeShutdownHookClass terminates the control loop runtime service when its run method is called.
125      */
126     private class ClRuntimeShutdownHookClass extends Thread {
127         /*
128          * (non-Javadoc)
129          *
130          * @see java.lang.Runnable#run()
131          */
132         @Override
133         public void run() {
134             if (!activator.isAlive()) {
135                 return;
136             }
137
138             try {
139                 // Shutdown the control loop runtime service and wait for everything to stop
140                 activator.stop();
141             } catch (final RuntimeException e) {
142                 LOGGER.warn("error occured during shut down of the control loop runtime service", e);
143             }
144         }
145     }
146
147     /**
148      * The main method.
149      *
150      * @param args the arguments
151      */
152     public static void main(final String[] args) {      // NOSONAR
153         /*
154          * NOTE: arguments are validated by the constructor, thus sonar is disabled.
155          */
156
157         new Main(args);
158     }
159 }