Clean up checkstyle declaration
[policy/api.git] / main / src / main / java / org / onap / policy / api / main / startstop / ApiActivator.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.api.main.parameters.ApiParameterGroup;
25 import org.onap.policy.api.main.rest.ApiRestServer;
26 import org.onap.policy.common.logging.flexlogger.FlexLogger;
27 import org.onap.policy.common.logging.flexlogger.Logger;
28 import org.onap.policy.common.parameters.ParameterService;
29
30 /**
31  * This class wraps a distributor so that it can be activated as a complete service together with all its api
32  * and forwarding handlers.
33  */
34 public class ApiActivator {
35     // The logger for this class
36     private static final Logger LOGGER = FlexLogger.getLogger(ApiActivator.class);
37
38     // The parameters of this policy api activator
39     private final ApiParameterGroup apiParameterGroup;
40
41     private static boolean alive = false;
42
43     private ApiRestServer restServer;
44
45     /**
46      * Instantiate the activator for policy api as a complete service.
47      *
48      * @param apiParameterGroup the parameters for the api service
49      */
50     public ApiActivator(final ApiParameterGroup apiParameterGroup) {
51         this.apiParameterGroup = apiParameterGroup;
52     }
53
54     /**
55      * Initialize api as a complete service.
56      *
57      * @throws PolicyApiException on errors in initializing the service
58      */
59     public void initialize() throws PolicyApiException {
60         LOGGER.debug("Policy api starting as a service . . .");
61         startApiRestServer();
62         registerToParameterService(apiParameterGroup);
63         ApiActivator.setAlive(true);
64         LOGGER.debug("Policy api started as a service");
65     }
66
67     /**
68      * Starts the api rest server using configuration parameters.
69      *
70      * @throws PolicyApiException if server start fails
71      */
72     private void startApiRestServer() throws PolicyApiException {
73         apiParameterGroup.getRestServerParameters().setName(apiParameterGroup.getName());
74         restServer = new ApiRestServer(apiParameterGroup.getRestServerParameters());
75         if (!restServer.start()) {
76             throw new PolicyApiException(
77                     "Failed to start api rest server. Check log for more details...");
78         }
79     }
80
81     /**
82      * Terminate policy api.
83      *
84      * @throws PolicyApiException on termination errors
85      */
86     public void terminate() throws PolicyApiException {
87         try {
88             deregisterToParameterService(apiParameterGroup);
89             ApiActivator.setAlive(false);
90
91             // Stop the api rest server
92             restServer.stop();
93         } catch (final Exception exp) {
94             LOGGER.error("Policy api service termination failed", exp);
95             throw new PolicyApiException(exp.getMessage(), exp);
96         }
97     }
98
99     /**
100      * Get the parameters used by the activator.
101      *
102      * @return the parameters of the activator
103      */
104     public ApiParameterGroup getParameterGroup() {
105         return apiParameterGroup;
106     }
107
108     /**
109      * Method to register the parameters to Common Parameter Service.
110      *
111      * @param apiParameterGroup the api parameter group
112      */
113     public void registerToParameterService(final ApiParameterGroup apiParameterGroup) {
114         ParameterService.register(apiParameterGroup);
115     }
116
117     /**
118      * Method to deregister the parameters from Common Parameter Service.
119      *
120      * @param apiParameterGroup the api parameter group
121      */
122     public void deregisterToParameterService(final ApiParameterGroup apiParameterGroup) {
123         ParameterService.deregister(apiParameterGroup.getName());
124     }
125
126     /**
127      * Returns the alive status of api service.
128      *
129      * @return the alive
130      */
131     public static boolean isAlive() {
132         return alive;
133     }
134
135     /**
136      * Change the alive status of api service.
137      *
138      * @param status the status
139      */
140     public static void setAlive(final boolean status) {
141         alive = status;
142     }
143 }