Add changes to basic structure of api component
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / startstop / Main.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy API 
4  * ================================================================================ 
5  * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
6  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * SPDX-License-Identifier: Apache-2.0
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.api.main.startstop;
25
26 import java.util.Arrays;
27 import org.onap.policy.api.main.exception.PolicyApiException;
28 import org.onap.policy.api.main.parameters.ApiParameterGroup;
29 import org.onap.policy.api.main.parameters.ApiParameterHandler;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * This class initiates ONAP Policy Framework policy api.
35  *
36  */
37 public class Main {
38     
39     private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
40
41     // The policy api Activator that activates the policy api service
42     private ApiActivator activator;
43
44     // The parameters read in from JSON
45     private ApiParameterGroup parameterGroup;
46
47     /**
48      * Instantiates the policy api service.
49      *
50      * @param args the command line arguments
51      */
52     public Main(final String[] args) {
53         final String argumentString = Arrays.toString(args);
54         LOGGER.info("Starting policy api service with arguments - {}", argumentString);
55
56         // Check the arguments
57         final ApiCommandLineArguments arguments = new ApiCommandLineArguments();
58         try {
59             // The arguments return a string if there is a message to print and we should exit
60             final String argumentMessage = arguments.parse(args);
61             if (argumentMessage != null) {
62                 LOGGER.info(argumentMessage);
63                 return;
64             }
65
66             // Validate that the arguments are sane
67             arguments.validate();
68         } catch (final PolicyApiException e) {
69             LOGGER.error("start of policy api service failed", e);
70             return;
71         }
72
73         // Read the parameters
74         try {
75             parameterGroup = new ApiParameterHandler().getParameters(arguments);
76         } catch (final Exception e) {
77             LOGGER.error("start of policy api service failed", e);
78             return;
79         }
80
81         // Now, create the activator for the policy api service
82         activator = new ApiActivator(parameterGroup);
83
84         // Start the activator
85         try {
86             activator.initialize();
87         } catch (final PolicyApiException e) {
88             LOGGER.error("start of policy api service failed, used parameters are {} ", argumentString, e);
89             return;
90         }
91
92         // Add a shutdown hook to shut everything down in an orderly manner
93         Runtime.getRuntime().addShutdownHook(new PolicyApiShutdownHookClass());
94         LOGGER.info("Started policy api service");
95     }
96
97     /**
98      * Get the parameters specified in JSON.
99      *
100      * @return the parameters
101      */
102     public ApiParameterGroup getParameters() {
103         return parameterGroup;
104     }
105
106     /**
107      * Shut down Execution.
108      *
109      * @throws PolicyApiException on shutdown errors
110      */
111     public void shutdown() throws PolicyApiException {
112         // clear the parameterGroup variable
113         parameterGroup = null;
114
115         // clear the api activator
116         if (activator != null) {
117             activator.terminate();
118         }
119     }
120
121     /**
122      * The Class PolicyApiShutdownHookClass terminates the policy api service when its run method is
123      * called.
124      */
125     private class PolicyApiShutdownHookClass extends Thread {
126         /*
127          * (non-Javadoc)
128          *
129          * @see java.lang.Runnable#run()
130          */
131         @Override
132         public void run() {
133             try {
134                 // Shutdown the policy api service and wait for everything to stop
135                 activator.terminate();
136             } catch (final PolicyApiException e) {
137                 LOGGER.warn("error occured during shut down of the policy api service", e);
138             }
139         }
140     }
141
142     /**
143      * The main method.
144      *
145      * @param args the arguments
146      */
147     public static void main(final String[] args) {
148         new Main(args);
149     }
150 }