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 java.util.Properties;
27 import org.onap.policy.common.endpoints.event.comm.TopicEndpoint;
28 import org.onap.policy.common.parameters.ParameterService;
29 import org.onap.policy.common.utils.services.ServiceManager;
30 import org.onap.policy.common.utils.services.ServiceManagerException;
31 import org.onap.policy.pap.main.PolicyPapException;
32 import org.onap.policy.pap.main.parameters.PapParameterGroup;
33 import org.onap.policy.pap.main.rest.PapRestServer;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * This class wraps a distributor so that it can be activated as a complete service
39 * together with all its pap and forwarding handlers.
41 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
43 public class PapActivator {
45 private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
47 private final PapParameterGroup papParameterGroup;
50 * The current activator.
53 private static volatile PapActivator current = null;
56 * Used to stop the services.
58 private final ServiceManager manager;
61 @Setter(lombok.AccessLevel.PRIVATE)
62 private volatile boolean alive = false;
64 private PapRestServer restServer;
67 * Instantiate the activator for policy pap as a complete service.
69 * @param papParameterGroup the parameters for the pap service
70 * @param topicProperties properties used to configure the topics
72 public PapActivator(final PapParameterGroup papParameterGroup, Properties topicProperties) {
73 TopicEndpoint.manager.addTopicSinks(topicProperties);
74 TopicEndpoint.manager.addTopicSources(topicProperties);
76 this.papParameterGroup = papParameterGroup;
79 this.manager = new ServiceManager()
81 () -> TopicEndpoint.manager.start(),
82 () -> TopicEndpoint.manager.shutdown())
83 .addAction("register parameters",
84 () -> registerToParameterService(papParameterGroup),
85 () -> deregisterToParameterService(papParameterGroup))
86 .addAction("REST server",
87 () -> startPapRestServer(),
88 () -> restServer.stop())
89 .addAction("set alive",
91 () -> setAlive(false));
98 * Initialize pap as a complete service.
100 * @throws PolicyPapException on errors in initializing the service
102 public void initialize() throws PolicyPapException {
104 throw new IllegalStateException("activator already initialized");
108 LOGGER.debug("Policy pap starting as a service . . .");
110 LOGGER.debug("Policy pap started as a service");
111 } catch (final ServiceManagerException exp) {
112 LOGGER.error("Policy pap service startup failed");
113 throw new PolicyPapException(exp.getMessage(), exp);
118 * Terminate policy pap.
120 * @throws PolicyPapException on errors in terminating the service
122 public void terminate() throws PolicyPapException {
124 throw new IllegalStateException("activator is not running");
129 } catch (final ServiceManagerException exp) {
130 LOGGER.error("Policy pap service termination failed");
131 throw new PolicyPapException(exp.getMessage(), exp);
136 * Get the parameters used by the activator.
138 * @return the parameters of the activator
140 public PapParameterGroup getParameterGroup() {
141 return papParameterGroup;
145 * Method to register the parameters to Common Parameter Service.
147 * @param papParameterGroup the pap parameter group
149 public void registerToParameterService(final PapParameterGroup papParameterGroup) {
150 ParameterService.register(papParameterGroup);
154 * Method to deregister the parameters from Common Parameter Service.
156 * @param papParameterGroup the pap parameter group
158 public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
159 ParameterService.deregister(papParameterGroup.getName());
163 * Starts the pap rest server using configuration parameters.
165 * @throws PolicyPapException if server start fails
167 private void startPapRestServer() throws PolicyPapException {
168 papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
169 restServer = new PapRestServer(papParameterGroup.getRestServerParameters());
170 if (!restServer.start()) {
171 throw new PolicyPapException("Failed to start pap rest server. Check log for more details...");