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 throw new IllegalStateException("activator already initialized");
79 LOGGER.debug("Policy pap starting as a service . . .");
81 registerToParameterService(papParameterGroup);
83 LOGGER.debug("Policy pap started as a service");
84 } catch (final Exception exp) {
85 LOGGER.error("Policy pap service startup failed", exp);
86 throw new PolicyPapException(exp.getMessage(), exp);
91 * Terminate policy pap.
93 * @throws PolicyPapException on errors in terminating the service
95 public void terminate() throws PolicyPapException {
97 throw new IllegalStateException("activator is not running");
101 deregisterToParameterService(papParameterGroup);
104 // Stop the pap rest server
106 } catch (final Exception exp) {
107 LOGGER.error("Policy pap service termination failed", exp);
108 throw new PolicyPapException(exp.getMessage(), exp);
113 * Get the parameters used by the activator.
115 * @return the parameters of the activator
117 public PapParameterGroup getParameterGroup() {
118 return papParameterGroup;
122 * Method to register the parameters to Common Parameter Service.
124 * @param papParameterGroup the pap parameter group
126 public void registerToParameterService(final PapParameterGroup papParameterGroup) {
127 ParameterService.register(papParameterGroup);
131 * Method to deregister the parameters from Common Parameter Service.
133 * @param papParameterGroup the pap parameter group
135 public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
136 ParameterService.deregister(papParameterGroup.getName());
140 * Starts the pap rest server using configuration parameters.
142 * @throws PolicyPapException if server start fails
144 private void startPapRestServer() throws PolicyPapException {
145 papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
146 restServer = new PapRestServer(papParameterGroup.getRestServerParameters());
147 if (!restServer.start()) {
148 throw new PolicyPapException("Failed to start pap rest server. Check log for more details...");