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