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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.startstop;
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;
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.
34 public class XacmlPdpActivator {
35 // The logger for this class
36 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlPdpActivator.class);
38 // The parameters of this policy xacml pdp activator
39 private final XacmlPdpParameterGroup xacmlPdpParameterGroup;
41 private static boolean alive = false;
43 private XacmlPdpRestServer restServer;
46 * Instantiate the activator for policy xacml pdp as a complete service.
48 * @param xacmlPdpParameterGroup the parameters for the xacml pdp service
50 public XacmlPdpActivator(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
51 this.xacmlPdpParameterGroup = xacmlPdpParameterGroup;
55 * Initialize xacml pdp as a complete service.
57 * @throws PolicyXacmlPdpException on errors in initializing the service
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");
68 * Starts the xacml pdp rest server using configuration parameters.
70 * @throws PolicyXacmlPdpException if server start fails
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...");
82 * Terminate policy xacml pdp.
84 * @throws PolicyXacmlPdpException on termination errors
86 public void terminate() throws PolicyXacmlPdpException {
88 deregisterToParameterService(xacmlPdpParameterGroup);
89 XacmlPdpActivator.setAlive(false);
91 // Stop the xacml pdp rest server
93 } catch (final Exception exp) {
94 LOGGER.error("Policy xacml pdp service termination failed", exp);
95 throw new PolicyXacmlPdpException(exp.getMessage(), exp);
100 * Get the parameters used by the activator.
102 * @return the parameters of the activator
104 public XacmlPdpParameterGroup getParameterGroup() {
105 return xacmlPdpParameterGroup;
109 * Method to register the parameters to Common Parameter Service.
111 * @param xacmlPdpParameterGroup the xacml pdp parameter group
113 public void registerToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
114 ParameterService.register(xacmlPdpParameterGroup);
118 * Method to deregister the parameters from Common Parameter Service.
120 * @param xacmlPdpParameterGroup the xacml pdp parameter group
122 public void deregisterToParameterService(final XacmlPdpParameterGroup xacmlPdpParameterGroup) {
123 ParameterService.deregister(xacmlPdpParameterGroup.getName());
127 * Returns the alive status of xacml pdp service.
131 public static boolean isAlive() {
136 * Change the alive status of xacml pdp service.
138 * @param status the status
140 private static void setAlive(final boolean status) {