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