Replace static methods with single getInstance
[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 lombok.Getter;
25 import lombok.Setter;
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;
32
33 /**
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.
36  *
37  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
38  */
39 public class PapActivator {
40
41     private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
42
43     private final PapParameterGroup papParameterGroup;
44
45     /**
46      * The current activator. This is initialized to a dummy instance used until the real
47      * one has been configured.
48      */
49     @Getter
50     @Setter
51     private static volatile PapActivator current = new PapActivator(null);
52
53     @Getter
54     @Setter(lombok.AccessLevel.PRIVATE)
55     private volatile boolean alive = false;
56
57     private PapRestServer restServer;
58
59     /**
60      * Instantiate the activator for policy pap as a complete service.
61      *
62      * @param papParameterGroup the parameters for the pap service
63      */
64     public PapActivator(final PapParameterGroup papParameterGroup) {
65         this.papParameterGroup = papParameterGroup;
66     }
67
68     /**
69      * Initialize pap as a complete service.
70      *
71      * @throws PolicyPapException on errors in initializing the service
72      */
73     public void initialize() throws PolicyPapException {
74         try {
75             LOGGER.debug("Policy pap starting as a service . . .");
76             startPapRestServer();
77             registerToParameterService(papParameterGroup);
78             setAlive(true);
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);
83         }
84     }
85
86     /**
87      * Terminate policy pap.
88      *
89      * @throws PolicyPapException on errors in terminating the service
90      */
91     public void terminate() throws PolicyPapException {
92         try {
93             deregisterToParameterService(papParameterGroup);
94             setAlive(false);
95
96             // Stop the pap rest server
97             restServer.stop();
98         } catch (final Exception exp) {
99             LOGGER.error("Policy pap service termination failed", exp);
100             throw new PolicyPapException(exp.getMessage(), exp);
101         }
102     }
103
104     /**
105      * Get the parameters used by the activator.
106      *
107      * @return the parameters of the activator
108      */
109     public PapParameterGroup getParameterGroup() {
110         return papParameterGroup;
111     }
112
113     /**
114      * Method to register the parameters to Common Parameter Service.
115      *
116      * @param papParameterGroup the pap parameter group
117      */
118     public void registerToParameterService(final PapParameterGroup papParameterGroup) {
119         ParameterService.register(papParameterGroup);
120     }
121
122     /**
123      * Method to deregister the parameters from Common Parameter Service.
124      *
125      * @param papParameterGroup the pap parameter group
126      */
127     public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
128         ParameterService.deregister(papParameterGroup.getName());
129     }
130
131     /**
132      * Starts the pap rest server using configuration parameters.
133      *
134      * @throws PolicyPapException if server start fails
135      */
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...");
141         }
142     }
143 }