Clean up checkstyle declaration
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / startstop / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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.api.main.startstop;
22
23 import java.util.Arrays;
24 import org.onap.policy.api.main.PolicyApiException;
25 import org.onap.policy.api.main.parameters.ApiParameterGroup;
26 import org.onap.policy.api.main.parameters.ApiParameterHandler;
27 import org.onap.policy.common.logging.flexlogger.FlexLogger;
28 import org.onap.policy.common.logging.flexlogger.Logger;
29
30 /**
31  * This class initiates ONAP Policy Framework policy api.
32  *
33  */
34 public class Main {
35     private static final Logger LOGGER = FlexLogger.getLogger(Main.class);
36
37     // The policy api Activator that activates the policy api service
38     private ApiActivator activator;
39
40     // The parameters read in from JSON
41     private ApiParameterGroup parameterGroup;
42
43     /**
44      * Instantiates the policy api service.
45      *
46      * @param args the command line arguments
47      */
48     public Main(final String[] args) {
49         final String argumentString = Arrays.toString(args);
50         LOGGER.info("Starting policy api service with arguments - " + argumentString);
51
52         // Check the arguments
53         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
54         try {
55             // The arguments return a string if there is a message to print and we should exit
56             final String argumentMessage = arguments.parse(args);
57             if (argumentMessage != null) {
58                 LOGGER.info(argumentMessage);
59                 return;
60             }
61
62             // Validate that the arguments are sane
63             arguments.validate();
64         } catch (final PolicyApiException e) {
65             LOGGER.error("start of policy api service failed", e);
66             return;
67         }
68
69         // Read the parameters
70         try {
71             parameterGroup = new ApiParameterHandler().getParameters(arguments);
72         } catch (final Exception e) {
73             LOGGER.error("start of policy api service failed", e);
74             return;
75         }
76
77         // Now, create the activator for the policy api service
78         activator = new ApiActivator(parameterGroup);
79
80         // Start the activator
81         try {
82             activator.initialize();
83         } catch (final PolicyApiException e) {
84             LOGGER.error("start of policy api service failed, used parameters are " + Arrays.toString(args),
85                     e);
86             return;
87         }
88
89         // Add a shutdown hook to shut everything down in an orderly manner
90         Runtime.getRuntime().addShutdownHook(new PolicyApiShutdownHookClass());
91         LOGGER.info("Started policy api service");
92     }
93
94     /**
95      * Get the parameters specified in JSON.
96      *
97      * @return the parameters
98      */
99     public ApiParameterGroup getParameters() {
100         return parameterGroup;
101     }
102
103     /**
104      * Shut down Execution.
105      *
106      * @throws PolicyApiException on shutdown errors
107      */
108     public void shutdown() throws PolicyApiException {
109         // clear the parameterGroup variable
110         parameterGroup = null;
111
112         // clear the api activator
113         if (activator != null) {
114             activator.terminate();
115         }
116     }
117
118     /**
119      * The Class PolicyApiShutdownHookClass terminates the policy api service when its run method is
120      * called.
121      */
122     private class PolicyApiShutdownHookClass extends Thread {
123         /*
124          * (non-Javadoc)
125          *
126          * @see java.lang.Runnable#run()
127          */
128         @Override
129         public void run() {
130             try {
131                 // Shutdown the policy api service and wait for everything to stop
132                 activator.terminate();
133             } catch (final PolicyApiException e) {
134                 LOGGER.warn("error occured during shut down of the policy api 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) {
145         new Main(args);
146     }
147 }