Tie XACML REST Decision
[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                 xacmlPdpParameterGroup.getApplicationPath());
76         if (!restServer.start()) {
77             throw new PolicyXacmlPdpException("Failed to start xacml pdp rest server. Check log for more details...");
78         }
79     }
80
81     /**
82      * Terminate policy xacml pdp.
83      *
84      * @throws PolicyXacmlPdpException on termination errors
85      */
86     public void terminate() throws PolicyXacmlPdpException {
87         try {
88             deregisterToParameterService(xacmlPdpParameterGroup);
89             XacmlPdpActivator.setAlive(false);
90
91             // Stop the xacml pdp rest server
92             restServer.stop();
93         } catch (final Exception exp) {
94             LOGGER.error("Policy xacml pdp service termination failed", exp);
95             throw new PolicyXacmlPdpException(exp.getMessage(), exp);
96         }
97     }
98
99     /**
100      * Get the parameters used by the activator.
101      *
102      * @return the parameters of the activator
103      */
104     public XacmlPdpParameterGroup getParameterGroup() {
105         return xacmlPdpParameterGroup;
106     }
107
108     /**
109      * Method to register the parameters to Common Parameter Service.
110      *
111      * @param xacmlPdpParameterGroup the xacml pdp parameter group
112      */
113     public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
114         ParameterService.register(xacmlPdpParameterGroup);
115     }
116
117     /**
118      * Method to deregister the parameters from Common Parameter Service.
119      *
120      * @param xacmlPdpParameterGroup the xacml pdp parameter group
121      */
122     public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
123         ParameterService.deregister(xacmlPdpParameterGroup.getName());
124     }
125
126     /**
127      * Returns the alive status of xacml pdp service.
128      *
129      * @return the alive
130      */
131     public static boolean isAlive() {
132         return alive;
133     }
134
135     /**
136      * Change the alive status of xacml pdp service.
137      *
138      * @param status the status
139      */
140     private static void setAlive(final boolean status) {
141         alive = status;
142     }
143 }