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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.api.main.startstop;
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;
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.
34 public class ApiActivator {
35 // The logger for this class
36 private static final Logger LOGGER = FlexLogger.getLogger(ApiActivator.class);
38 // The parameters of this policy api activator
39 private final ApiParameterGroup apiParameterGroup;
41 private static boolean alive = false;
43 private ApiRestServer restServer;
46 * Instantiate the activator for policy api as a complete service.
48 * @param apiParameterGroup the parameters for the api service
50 public ApiActivator(final ApiParameterGroup apiParameterGroup) {
51 this.apiParameterGroup = apiParameterGroup;
55 * Initialize api as a complete service.
57 * @throws PolicyApiException on errors in initializing the service
59 public void initialize() throws PolicyApiException {
60 LOGGER.debug("Policy api starting as a service . . .");
62 registerToParameterService(apiParameterGroup);
63 ApiActivator.setAlive(true);
64 LOGGER.debug("Policy api started as a service");
68 * Starts the api rest server using configuration parameters.
70 * @throws PolicyApiException if server start fails
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...");
82 * Terminate policy api.
84 * @throws PolicyApiException on termination errors
86 public void terminate() throws PolicyApiException {
88 deregisterToParameterService(apiParameterGroup);
89 ApiActivator.setAlive(false);
91 // Stop the api rest server
93 } catch (final Exception exp) {
94 LOGGER.error("Policy api service termination failed", exp);
95 throw new PolicyApiException(exp.getMessage(), exp);
100 * Get the parameters used by the activator.
102 * @return the parameters of the activator
104 public ApiParameterGroup getParameterGroup() {
105 return apiParameterGroup;
109 * Method to register the parameters to Common Parameter Service.
111 * @param apiParameterGroup the api parameter group
113 public void registerToParameterService(final ApiParameterGroup apiParameterGroup) {
114 ParameterService.register(apiParameterGroup);
118 * Method to deregister the parameters from Common Parameter Service.
120 * @param apiParameterGroup the api parameter group
122 public void deregisterToParameterService(final ApiParameterGroup apiParameterGroup) {
123 ParameterService.deregister(apiParameterGroup.getName());
127 * Returns the alive status of api service.
131 public static boolean isAlive() {
136 * Change the alive status of api service.
138 * @param status the status
140 public static void setAlive(final boolean status) {