d50c4f131380d60dda33aa35daab3436ec015abb
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / startstop / XacmlPdpActivator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.pdpx.main.startstop;
22
23 import org.onap.policy.common.parameters.ParameterService;
24 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
25 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
26 import org.onap.policy.pdpx.main.rest.XacmlPdpRestServer;
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
32  * all its xacml pdp and forwarding handlers.
33  */
34 public class XacmlPdpActivator {
35     // The logger for this class
36     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
37
38     // The parameters of this policy xacml pdp activator
39     private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
40
41     private static boolean alive = false;
42
43     private XacmlPdpRestServer restServer;
44
45     /**
46      * Instantiate the activator for policy xacml pdp as a complete service.
47      *
48      * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
49      */
50     public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
51         this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
52     }
53
54     /**
55      * Initialize xacml pdp as a complete service.
56      *
57      * @throws PolicyXacmlPdpException on errors in initializing the service
58      */
59     public void initialize() throws PolicyXacmlPdpException {
60         LOGGER.debug("Policy xacml pdp starting as a service . . .");
61         startXacmlPdpRestServer();
62         registerToParameterService(xacmlPdpParameterGroup);
63         XacmlPdpActivator.setAlive(true);
64         LOGGER.debug("Policy xacml pdp started as a service");
65     }
66
67     /**
68      * Starts the xacml pdp rest server using configuration parameters.
69      *
70      * @throws PolicyXacmlPdpException if server start fails
71      */
72     private void startXacmlPdpRestServer() throws PolicyXacmlPdpException {
73         xacmlPdpParameterGroup.getRestServerParameters().setName(xacmlPdpParameterGroup.getName());
74         restServer = new XacmlPdpRestServer(xacmlPdpParameterGroup.getRestServerParameters());
75         if (!restServer.start()) {
76             throw new PolicyXacmlPdpException("Failed to start xacml pdp rest server. Check log for more details...");
77         }
78     }
79
80     /**
81      * Terminate policy xacml pdp.
82      *
83      * @throws PolicyXacmlPdpException on termination errors
84      */
85     public void terminate() throws PolicyXacmlPdpException {
86         try {
87             deregisterToParameterService(xacmlPdpParameterGroup);
88             XacmlPdpActivator.setAlive(false);
89
90             // Stop the xacml pdp rest server
91             restServer.stop();
92         } catch (final Exception exp) {
93             LOGGER.error("Policy xacml pdp service termination failed", exp);
94             throw new PolicyXacmlPdpException(exp.getMessage(), exp);
95         }
96     }
97
98     /**
99      * Get the parameters used by the activator.
100      *
101      * @return the parameters of the activator
102      */
103     public XacmlPdpParameterGroup getParameterGroup() {
104         return xacmlPdpParameterGroup;
105     }
106
107     /**
108      * Method to register the parameters to Common Parameter Service.
109      *
110      * @param xacmlPdpParameterGroup the xacml pdp parameter group
111      */
112     public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
113         ParameterService.register(xacmlPdpParameterGroup);
114     }
115
116     /**
117      * Method to deregister the parameters from Common Parameter Service.
118      *
119      * @param xacmlPdpParameterGroup the xacml pdp parameter group
120      */
121     public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
122         ParameterService.deregister(xacmlPdpParameterGroup.getName());
123     }
124
125     /**
126      * Returns the alive status of xacml pdp service.
127      *
128      * @return the alive
129      */
130     public static boolean isAlive() {
131         return alive;
132     }
133
134     /**
135      * Change the alive status of xacml pdp service.
136      *
137      * @param status the status
138      */
139     private static void setAlive(final boolean status) {
140         alive = status;
141     }
142 }