2b47a2c13e3312e2543c3c57247523130c9ee00e
[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.participant.dcae.main.startstop;
22
23 import java.util.Arrays;
24 import javax.ws.rs.core.Response;
25 import lombok.Getter;
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;
35
36 /**
37  * This class initiates ONAP Policy Framework Control Loop participant component.
38  */
39 public class Main {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
42
43     private ParticipantDcaeActivator activator;
44
45     @Getter
46     private ParticipantDcaeParameters parameterGroup;
47
48     /**
49      * Instantiates the control loop participant service.
50      *
51      * @param args the command line arguments
52      */
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);
56
57         // Check the arguments
58         final ParticipantDcaeCommandLineArguments arguments = new ParticipantDcaeCommandLineArguments();
59         try {
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);
64                 return;
65             }
66             // Validate that the arguments are sane
67             arguments.validate();
68
69             // Read the parameters
70             parameterGroup = new ParticipantDcaeParameterHandler().getParameters(arguments);
71
72             // Now, create the activator for the service
73             activator = new ParticipantDcaeActivator(parameterGroup);
74             Registry.register(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR, activator);
75
76             // Start the activator
77             activator.start();
78         } catch (Exception exp) {
79             if (null != activator) {
80                 Registry.unregister(ControlLoopConstants.REG_CLRUNTIME_ACTIVATOR);
81             }
82             throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST,
83                 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP), exp);
84         }
85
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);
90     }
91
92     /**
93      * Check if main is running.
94      */
95     public boolean isRunning() {
96         return activator != null && activator.isAlive();
97     }
98
99     /**
100      * Shut down Execution.
101      *
102      * @throws ControlLoopException on shutdown errors
103      */
104     public void shutdown() throws ControlLoopException {
105         // clear the parameterGroup variable
106         parameterGroup = null;
107
108         // clear the cl participant activator
109         if (activator != null) {
110             activator.stop();
111         }
112     }
113
114     /**
115      * The Class ClRuntimeShutdownHookClass terminates the control loop participant service
116      * when its run method is called.
117      */
118     private class ClRuntimeShutdownHookClass extends Thread {
119         /*
120          * (non-Javadoc)
121          *
122          * @see java.lang.Runnable#run()
123          */
124         @Override
125         public void run() {
126             if (!activator.isAlive()) {
127                 return;
128             }
129
130             try {
131                 // Shutdown the control loop participant service and wait for everything to stop
132                 activator.stop();
133             } catch (final RuntimeException e) {
134                 LOGGER.warn("error occured during shut down of the control loop participant service", e);
135             }
136         }
137     }
138
139     /**
140      * The main method.
141      *
142      * @param args the arguments
143      */
144     public static void main(final String[] args) {      // NOSONAR
145         /*
146          * NOTE: arguments are validated by the constructor, thus sonar is disabled.
147          */
148
149         new Main(args);
150     }
151 }