9a6bfdf7da6a3503c62047d1513dbbdbe5dd54d1
[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.policy.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.exception.ControlLoopException;
27 import org.onap.policy.clamp.controlloop.common.exception.ControlLoopRuntimeException;
28 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameterHandler;
29 import org.onap.policy.clamp.controlloop.participant.policy.main.parameters.ParticipantPolicyParameters;
30 import org.onap.policy.common.utils.resources.MessageConstants;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * This class initiates Policy Participant.
36  */
37 public class Main {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
40
41     private ParticipantPolicyActivator activator;
42
43     @Getter
44     private ParticipantPolicyParameters parameterGroup;
45
46     /**
47      * Instantiates policy participant.
48      *
49      * @param args the command line arguments
50      */
51     public Main(final String[] args) {
52         final String argumentString = Arrays.toString(args);
53         LOGGER.info("Starting the control loop participant service with arguments - {}", argumentString);
54
55         // Check the arguments
56         final ParticipantPolicyCommandLineArguments arguments = new ParticipantPolicyCommandLineArguments();
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 ParticipantPolicyParameterHandler().getParameters(arguments);
69
70             // Now, create the activator for the service
71             activator = new ParticipantPolicyActivator(parameterGroup);
72
73             // Start the activator
74             activator.start();
75         } catch (Exception exp) {
76             throw new ControlLoopRuntimeException(Response.Status.BAD_REQUEST,
77                 String.format(MessageConstants.START_FAILURE_MSG, MessageConstants.POLICY_CLAMP), exp);
78         }
79
80         // Add a shutdown hook to shut everything down in an orderly manner
81         Runtime.getRuntime().addShutdownHook(new ClRuntimeShutdownHookClass());
82         String successMsg = String.format(MessageConstants.START_SUCCESS_MSG, MessageConstants.POLICY_CLAMP);
83         LOGGER.info(successMsg);
84     }
85
86     /**
87      * Check if main is running.
88      */
89     public boolean isRunning() {
90         return activator != null && activator.isAlive();
91     }
92
93     /**
94      * Shut down Execution.
95      *
96      * @throws ControlLoopException on shutdown errors
97      */
98     public void shutdown() throws ControlLoopException {
99         // clear the parameterGroup variable
100         parameterGroup = null;
101
102         // clear the cl participant activator
103         if (activator != null) {
104             activator.stop();
105         }
106     }
107
108     /**
109      * The Class ClRuntimeShutdownHookClass terminates the policy participant
110      * when its run method is called.
111      */
112     private class ClRuntimeShutdownHookClass extends Thread {
113         /*
114          * (non-Javadoc)
115          *
116          * @see java.lang.Runnable#run()
117          */
118         @Override
119         public void run() {
120             try {
121                 // Shutdown the control loop participant service and wait for everything to stop
122                 shutdown();
123             } catch (final RuntimeException | ControlLoopException e) {
124                 LOGGER.warn("error occured during shut down of the policy participant", e);
125             }
126         }
127     }
128
129     /**
130      * The main method.
131      *
132      * @param args the arguments
133      */
134     public static void main(final String[] args) {      // NOSONAR
135         /*
136          * NOTE: arguments are validated by the constructor, thus sonar is disabled.
137          */
138
139         new Main(args);
140     }
141 }