2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.startstop;
24 import org.onap.policy.common.parameters.ParameterService;
25 import org.onap.policy.pap.main.PolicyPapException;
26 import org.onap.policy.pap.main.parameters.PapParameterGroup;
27 import org.onap.policy.pap.main.rest.PapRestServer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * This class wraps a distributor so that it can be activated as a complete service together with all its pap and
33 * forwarding handlers.
35 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
37 public class PapActivator {
39 private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
41 private final PapParameterGroup papParameterGroup;
42 private static volatile boolean alive = false;
43 private PapRestServer restServer;
46 * Instantiate the activator for policy pap as a complete service.
48 * @param papParameterGroup the parameters for the pap service
50 public PapActivator(final PapParameterGroup papParameterGroup) {
51 this.papParameterGroup = papParameterGroup;
55 * Initialize pap as a complete service.
57 * @throws PolicyPapException on errors in initializing the service
59 public void initialize() throws PolicyPapException {
61 LOGGER.debug("Policy pap starting as a service . . .");
63 registerToParameterService(papParameterGroup);
64 PapActivator.setAlive(true);
65 LOGGER.debug("Policy pap started as a service");
66 } catch (final Exception exp) {
67 LOGGER.error("Policy pap service startup failed", exp);
68 throw new PolicyPapException(exp.getMessage(), exp);
73 * Terminate policy pap.
75 * @throws PolicyPapException on errors in terminating the service
77 public void terminate() throws PolicyPapException {
79 deregisterToParameterService(papParameterGroup);
80 PapActivator.setAlive(false);
82 // Stop the pap rest server
84 } catch (final Exception exp) {
85 LOGGER.error("Policy pap service termination failed", exp);
86 throw new PolicyPapException(exp.getMessage(), exp);
91 * Get the parameters used by the activator.
93 * @return the parameters of the activator
95 public PapParameterGroup getParameterGroup() {
96 return papParameterGroup;
100 * Method to register the parameters to Common Parameter Service.
102 * @param papParameterGroup the pap parameter group
104 public void registerToParameterService(final PapParameterGroup papParameterGroup) {
105 ParameterService.register(papParameterGroup);
109 * Method to deregister the parameters from Common Parameter Service.
111 * @param papParameterGroup the pap parameter group
113 public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
114 ParameterService.deregister(papParameterGroup.getName());
118 * Returns the alive status of pap service.
122 public static boolean isAlive() {
127 * Change the alive status of pap service.
129 * @param status the status
131 private static void setAlive(final boolean status) {
136 * Starts the pap rest server using configuration parameters.
138 * @throws PolicyPapException if server start fails
140 private void startPapRestServer() throws PolicyPapException {
141 papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
142 restServer = new PapRestServer(papParameterGroup.getRestServerParameters());
143 if (!restServer.start()) {
144 throw new PolicyPapException("Failed to start pap rest server. Check log for more details...");