2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
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.pap.main.startstop;
23 import org.onap.policy.common.parameters.ParameterService;
24 import org.onap.policy.pap.main.PolicyPapException;
25 import org.onap.policy.pap.main.parameters.PapParameterGroup;
26 import org.onap.policy.pap.main.rest.PapRestServer;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * This class wraps a distributor so that it can be activated as a complete service together with all its pap and
32 * forwarding handlers.
34 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
36 public class PapActivator {
38 private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
40 private final PapParameterGroup papParameterGroup;
41 private static boolean alive = false;
42 private PapRestServer restServer;
45 * Instantiate the activator for policy pap as a complete service.
47 * @param papParameterGroup the parameters for the pap service
49 public PapActivator(final PapParameterGroup papParameterGroup) {
50 this.papParameterGroup = papParameterGroup;
54 * Initialize pap as a complete service.
56 * @throws PolicyPapException on errors in initializing the service
58 public void initialize() throws PolicyPapException {
60 LOGGER.debug("Policy pap starting as a service . . .");
62 registerToParameterService(papParameterGroup);
63 PapActivator.setAlive(true);
64 LOGGER.debug("Policy pap started as a service");
65 } catch (final Exception exp) {
66 LOGGER.error("Policy pap service startup failed", exp);
67 throw new PolicyPapException(exp.getMessage(), exp);
72 * Terminate policy pap.
74 * @throws PolicyPapException on errors in terminating the service
76 public void terminate() throws PolicyPapException {
78 deregisterToParameterService(papParameterGroup);
79 PapActivator.setAlive(false);
81 // Stop the pap rest server
83 } catch (final Exception exp) {
84 LOGGER.error("Policy pap service termination failed", exp);
85 throw new PolicyPapException(exp.getMessage(), exp);
90 * Get the parameters used by the activator.
92 * @return the parameters of the activator
94 public PapParameterGroup getParameterGroup() {
95 return papParameterGroup;
99 * Method to register the parameters to Common Parameter Service.
101 * @param papParameterGroup the pap parameter group
103 public void registerToParameterService(final PapParameterGroup papParameterGroup) {
104 ParameterService.register(papParameterGroup);
108 * Method to deregister the parameters from Common Parameter Service.
110 * @param papParameterGroup the pap parameter group
112 public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
113 ParameterService.deregister(papParameterGroup.getName());
117 * Returns the alive status of pap service.
121 public static boolean isAlive() {
126 * Change the alive status of pap service.
128 * @param status the status
130 public static void setAlive(final boolean status) {
135 * Starts the pap rest server using configuration parameters.
137 * @throws PolicyPapException if server start fails
139 private void startPapRestServer() throws PolicyPapException {
140 papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
141 restServer = new PapRestServer(papParameterGroup.getRestServerParameters());
142 if (!restServer.start()) {
143 throw new PolicyPapException("Failed to start pap rest server. Check log for more details...");