Adding statistics endpoint to policy/pap
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.startstop;
22
23 import org.onap.policy.common.parameters.ParameterService;
24 import org.onap.policy.pap.main.PolicyPapException;
25 import org.onap.policy.pap.main.parameters.PapParameterGroup;
26 import org.onap.policy.pap.main.rest.PapRestServer;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * This class wraps a distributor so that it can be activated as a complete service together with all its pap and
32  * forwarding handlers.
33  *
34  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
35  */
36 public class PapActivator {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
39
40     private final PapParameterGroup papParameterGroup;
41     private static boolean alive = false;
42     private PapRestServer restServer;
43
44     /**
45      * Instantiate the activator for policy pap as a complete service.
46      *
47      * @param papParameterGroup the parameters for the pap service
48      */
49     public PapActivator(final PapParameterGroup papParameterGroup) {
50         this.papParameterGroup = papParameterGroup;
51     }
52
53     /**
54      * Initialize pap as a complete service.
55      *
56      * @throws PolicyPapException on errors in initializing the service
57      */
58     public void initialize() throws PolicyPapException {
59         try {
60             LOGGER.debug("Policy pap starting as a service . . .");
61             startPapRestServer();
62             registerToParameterService(papParameterGroup);
63             PapActivator.setAlive(true);
64             LOGGER.debug("Policy pap started as a service");
65         } catch (final Exception exp) {
66             LOGGER.error("Policy pap service startup failed", exp);
67             throw new PolicyPapException(exp.getMessage(), exp);
68         }
69     }
70
71     /**
72      * Terminate policy pap.
73      *
74      * @throws PolicyPapException on errors in terminating the service
75      */
76     public void terminate() throws PolicyPapException {
77         try {
78             deregisterToParameterService(papParameterGroup);
79             PapActivator.setAlive(false);
80
81             // Stop the pap rest server
82             restServer.stop();
83         } catch (final Exception exp) {
84             LOGGER.error("Policy pap service termination failed", exp);
85             throw new PolicyPapException(exp.getMessage(), exp);
86         }
87     }
88
89     /**
90      * Get the parameters used by the activator.
91      *
92      * @return the parameters of the activator
93      */
94     public PapParameterGroup getParameterGroup() {
95         return papParameterGroup;
96     }
97
98     /**
99      * Method to register the parameters to Common Parameter Service.
100      *
101      * @param papParameterGroup the pap parameter group
102      */
103     public void registerToParameterService(final PapParameterGroup papParameterGroup) {
104         ParameterService.register(papParameterGroup);
105     }
106
107     /**
108      * Method to deregister the parameters from Common Parameter Service.
109      *
110      * @param papParameterGroup the pap parameter group
111      */
112     public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
113         ParameterService.deregister(papParameterGroup.getName());
114     }
115
116     /**
117      * Returns the alive status of pap service.
118      *
119      * @return the alive
120      */
121     public static boolean isAlive() {
122         return alive;
123     }
124
125     /**
126      * Change the alive status of pap service.
127      *
128      * @param status the status
129      */
130     private static void setAlive(final boolean status) {
131         alive = status;
132     }
133
134     /**
135      * Starts the pap rest server using configuration parameters.
136      *
137      * @throws PolicyPapException if server start fails
138      */
139     private void startPapRestServer() throws PolicyPapException {
140         papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
141         restServer = new PapRestServer(papParameterGroup.getRestServerParameters());
142         if (!restServer.start()) {
143             throw new PolicyPapException("Failed to start pap rest server. Check log for more details...");
144         }
145     }
146 }