Use constants for http property names
[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 org.onap.policy.common.parameters.ParameterService;
25 import org.onap.policy.pap.main.PolicyPapException;
26 import org.onap.policy.pap.main.parameters.PapParameterGroup;
27 import org.onap.policy.pap.main.rest.PapRestServer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * This class wraps a distributor so that it can be activated as a complete service together with all its pap and
33  * forwarding handlers.
34  *
35  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
36  */
37 public class PapActivator {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(PapActivator.class);
40
41     private final PapParameterGroup papParameterGroup;
42     private static volatile boolean alive = false;
43     private PapRestServer restServer;
44
45     /**
46      * Instantiate the activator for policy pap as a complete service.
47      *
48      * @param papParameterGroup the parameters for the pap service
49      */
50     public PapActivator(final PapParameterGroup papParameterGroup) {
51         this.papParameterGroup = papParameterGroup;
52     }
53
54     /**
55      * Initialize pap as a complete service.
56      *
57      * @throws PolicyPapException on errors in initializing the service
58      */
59     public void initialize() throws PolicyPapException {
60         try {
61             LOGGER.debug("Policy pap starting as a service . . .");
62             startPapRestServer();
63             registerToParameterService(papParameterGroup);
64             PapActivator.setAlive(true);
65             LOGGER.debug("Policy pap started as a service");
66         } catch (final Exception exp) {
67             LOGGER.error("Policy pap service startup failed", exp);
68             throw new PolicyPapException(exp.getMessage(), exp);
69         }
70     }
71
72     /**
73      * Terminate policy pap.
74      *
75      * @throws PolicyPapException on errors in terminating the service
76      */
77     public void terminate() throws PolicyPapException {
78         try {
79             deregisterToParameterService(papParameterGroup);
80             PapActivator.setAlive(false);
81
82             // Stop the pap rest server
83             restServer.stop();
84         } catch (final Exception exp) {
85             LOGGER.error("Policy pap service termination failed", exp);
86             throw new PolicyPapException(exp.getMessage(), exp);
87         }
88     }
89
90     /**
91      * Get the parameters used by the activator.
92      *
93      * @return the parameters of the activator
94      */
95     public PapParameterGroup getParameterGroup() {
96         return papParameterGroup;
97     }
98
99     /**
100      * Method to register the parameters to Common Parameter Service.
101      *
102      * @param papParameterGroup the pap parameter group
103      */
104     public void registerToParameterService(final PapParameterGroup papParameterGroup) {
105         ParameterService.register(papParameterGroup);
106     }
107
108     /**
109      * Method to deregister the parameters from Common Parameter Service.
110      *
111      * @param papParameterGroup the pap parameter group
112      */
113     public void deregisterToParameterService(final PapParameterGroup papParameterGroup) {
114         ParameterService.deregister(papParameterGroup.getName());
115     }
116
117     /**
118      * Returns the alive status of pap service.
119      *
120      * @return the alive
121      */
122     public static boolean isAlive() {
123         return alive;
124     }
125
126     /**
127      * Change the alive status of pap service.
128      *
129      * @param status the status
130      */
131     private static void setAlive(final boolean status) {
132         alive = status;
133     }
134
135     /**
136      * Starts the pap rest server using configuration parameters.
137      *
138      * @throws PolicyPapException if server start fails
139      */
140     private void startPapRestServer() throws PolicyPapException {
141         papParameterGroup.getRestServerParameters().setName(papParameterGroup.getName());
142         restServer = new PapRestServer(papParameterGroup.getRestServerParameters());
143         if (!restServer.start()) {
144             throw new PolicyPapException("Failed to start pap rest server. Check log for more details...");
145         }
146     }
147 }