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