Refactor REST server tests
[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         if (isAlive()) {
75             throw new IllegalStateException("activator already initialized");
76         }
77
78         try {
79             LOGGER.debug("Policy pap starting as a service . . .");
80             startPapRestServer();
81             registerToParameterService(papParameterGroup);
82             setAlive(true);
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);
87         }
88     }
89
90     /**
91      * Terminate policy pap.
92      *
93      * @throws PolicyPapException on errors in terminating the service
94      */
95     public void terminate() throws PolicyPapException {
96         if (!isAlive()) {
97             throw new IllegalStateException("activator is not running");
98         }
99
100         try {
101             deregisterToParameterService(papParameterGroup);
102             setAlive(false);
103
104             // Stop the pap rest server
105             restServer.stop();
106         } catch (final Exception exp) {
107             LOGGER.error("Policy pap service termination failed", exp);
108             throw new PolicyPapException(exp.getMessage(), exp);
109         }
110     }
111
112     /**
113      * Get the parameters used by the activator.
114      *
115      * @return the parameters of the activator
116      */
117     public PapParameterGroup getParameterGroup() {
118         return papParameterGroup;
119     }
120
121     /**
122      * Method to register the parameters to Common Parameter Service.
123      *
124      * @param papParameterGroup the pap parameter group
125      */
126     public void registerToParameterService(final PapParameterGroup papParameterGroup) {
127         ParameterService.register(papParameterGroup);
128     }
129
130     /**
131      * Method to deregister the parameters from Common Parameter Service.
132      *
133      * @param papParameterGroup the pap parameter group
134      */
135     public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
136         ParameterService.deregister(papParameterGroup.getName());
137     }
138
139     /**
140      * Starts the pap rest server using configuration parameters.
141      *
142      * @throws PolicyPapException if server start fails
143      */
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...");
149         }
150     }
151 }