Add PDP Group Deploy and Delete REST API stubs
[policy/pap.git] / main / src / main / java / org / onap / policy / pap / main / startstop / PapActivator.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.startstop;
23
24 import java.util.Properties;
25 import lombok.Getter;
26 import lombok.Setter;
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;
36
37 /**
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.
40  *
41  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
42  */
43 public class PapActivator {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
46
47     private final PapParameterGroup papParameterGroup;
48
49     /**
50      * The current activator.
51      */
52     @Getter
53     private static volatile PapActivator current = null;
54
55     /**
56      * Used to stop the services.
57      */
58     private final ServiceManager manager;
59
60     @Getter
61     @Setter(lombok.AccessLevel.PRIVATE)
62     private volatile boolean alive = false;
63
64     private PapRestServer restServer;
65
66     /**
67      * Instantiate the activator for policy pap as a complete service.
68      *
69      * @param papParameterGroup the parameters for the pap service
70      * @param topicProperties properties used to configure the topics
71      */
72     public PapActivator(final PapParameterGroup papParameterGroup, Properties topicProperties) {
73         TopicEndpoint.manager.addTopicSinks(topicProperties);
74         TopicEndpoint.manager.addTopicSources(topicProperties);
75
76         this.papParameterGroup = papParameterGroup;
77
78         // @formatter:off
79         this.manager = new ServiceManager()
80                         .addAction("topics",
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",
90                             () -> setAlive(true),
91                             () -> setAlive(false));
92         // @formatter:on
93
94         current = this;
95     }
96
97     /**
98      * Initialize pap as a complete service.
99      *
100      * @throws PolicyPapException on errors in initializing the service
101      */
102     public void initialize() throws PolicyPapException {
103         if (isAlive()) {
104             throw new IllegalStateException("activator already initialized");
105         }
106
107         try {
108             LOGGER.debug("Policy pap starting as a service . . .");
109             manager.start();
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);
114         }
115     }
116
117     /**
118      * Terminate policy pap.
119      *
120      * @throws PolicyPapException on errors in terminating the service
121      */
122     public void terminate() throws PolicyPapException {
123         if (!isAlive()) {
124             throw new IllegalStateException("activator is not running");
125         }
126
127         try {
128             manager.stop();
129         } catch (final ServiceManagerException exp) {
130             LOGGER.error("Policy pap service termination failed");
131             throw new PolicyPapException(exp.getMessage(), exp);
132         }
133     }
134
135     /**
136      * Get the parameters used by the activator.
137      *
138      * @return the parameters of the activator
139      */
140     public PapParameterGroup getParameterGroup() {
141         return papParameterGroup;
142     }
143
144     /**
145      * Method to register the parameters to Common Parameter Service.
146      *
147      * @param papParameterGroup the pap parameter group
148      */
149     public void registerToParameterService(final PapParameterGroup papParameterGroup) {
150         ParameterService.register(papParameterGroup);
151     }
152
153     /**
154      * Method to deregister the parameters from Common Parameter Service.
155      *
156      * @param papParameterGroup the pap parameter group
157      */
158     public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
159         ParameterService.deregister(papParameterGroup.getName());
160     }
161
162     /**
163      * Starts the pap rest server using configuration parameters.
164      *
165      * @throws PolicyPapException if server start fails
166      */
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...");
172         }
173     }
174 }