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