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;
26 import org.onap.policy.common.parameters.ParameterService;
27 import org.onap.policy.pap.main.PolicyPapException;
28 import org.onap.policy.pap.main.parameters.PapParameterGroup;
29 import org.onap.policy.pap.main.rest.PapRestServer;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * This class wraps a distributor so that it can be activated as a complete service together with all its pap and
35 * forwarding handlers.
37 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
39 public class PapActivator {
41 private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
43 private final PapParameterGroup papParameterGroup;
46 * The current activator. This is initialized to a dummy instance used until the real
47 * one has been configured.
51 private static volatile PapActivator current = new PapActivator(null);
54 @Setter(lombok.AccessLevel.PRIVATE)
55 private volatile boolean alive = false;
57 private PapRestServer restServer;
60 * Instantiate the activator for policy pap as a complete service.
62 * @param papParameterGroup the parameters for the pap service
64 public PapActivator(final PapParameterGroup papParameterGroup) {
65 this.papParameterGroup = papParameterGroup;
69 * Initialize pap as a complete service.
71 * @throws PolicyPapException on errors in initializing the service
73 public void initialize() throws PolicyPapException {
75 LOGGER.debug("Policy pap starting as a service . . .");
77 registerToParameterService(papParameterGroup);
79 LOGGER.debug("Policy pap started as a service");
80 } catch (final Exception exp) {
81 LOGGER.error("Policy pap service startup failed", exp);
82 throw new PolicyPapException(exp.getMessage(), exp);
87 * Terminate policy pap.
89 * @throws PolicyPapException on errors in terminating the service
91 public void terminate() throws PolicyPapException {
93 deregisterToParameterService(papParameterGroup);
96 // Stop the pap rest server
98 } catch (final Exception exp) {
99 LOGGER.error("Policy pap service termination failed", exp);
100 throw new PolicyPapException(exp.getMessage(), exp);
105 * Get the parameters used by the activator.
107 * @return the parameters of the activator
109 public PapParameterGroup getParameterGroup() {
110 return papParameterGroup;
114 * Method to register the parameters to Common Parameter Service.
116 * @param papParameterGroup the pap parameter group
118 public void registerToParameterService(final PapParameterGroup papParameterGroup) {
119 ParameterService.register(papParameterGroup);
123 * Method to deregister the parameters from Common Parameter Service.
125 * @param papParameterGroup the pap parameter group
127 public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
128 ParameterService.deregister(papParameterGroup.getName());
132 * Starts the pap rest server using configuration parameters.
134 * @throws PolicyPapException if server start fails
136 private void startPapRestServer() throws PolicyPapException {
137 papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
138 restServer = new PapRestServer(papParameterGroup.getRestServerParameters());
139 if (!restServer.start()) {
140 throw new PolicyPapException("Failed to start pap rest server. Check log for more details...");