2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2021-2022 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;
23 import java.util.Collections;
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.policy.common.utils.network.NetworkUtil;
26 import org.onap.policy.models.pdp.concepts.PdpMessage;
27 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
28 import org.onap.policy.models.pdp.concepts.PdpStateChange;
29 import org.onap.policy.models.pdp.concepts.PdpStatus;
30 import org.onap.policy.models.pdp.concepts.PdpUpdate;
31 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
32 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
33 import org.onap.policy.models.pdp.enums.PdpState;
34 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
35 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * Current state of this XACML PDP.
42 public class XacmlState {
43 // The logger for this class
44 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlState.class);
47 * Unique name for the xacml-pdp JVM, used in PdpStatus messages.
49 public static final String PDP_NAME = NetworkUtil.genUniqueName("xacml");
52 * The application manager.
54 private final XacmlPdpApplicationManager appManager;
57 * Records the current state of this PDP.
59 private final PdpStatus status;
62 * Constructs the object, initializing the state.
64 public XacmlState(XacmlPdpApplicationManager appManager, String pdpGroupName, String pdpType) {
65 this.appManager = appManager;
67 this.status = new PdpStatus();
68 this.status.setName(PDP_NAME);
69 this.status.setPdpType(pdpType);
70 this.status.setState(PdpState.PASSIVE);
71 this.status.setPolicies(Collections.emptyList());
72 this.status.setPdpGroup(pdpGroupName);
76 * Determines if this PDP should handle the given message.
78 * @param message message of interest
79 * @return {@code true} if this PDP should handle the message, {@code false} otherwise
81 public boolean shouldHandle(PdpMessage message) {
82 return message.appliesTo(status.getName(), status.getPdpGroup(), status.getPdpType());
86 * Generates a new heart beat message.
88 * @return a new heart beat message
90 public synchronized PdpStatus genHeartbeat() {
91 // first, update status fields
92 status.setHealthy(XacmlPdpActivator.getCurrent().isAlive() ? PdpHealthStatus.HEALTHY
93 : PdpHealthStatus.NOT_HEALTHY);
95 PdpStatus heartbeat = new PdpStatus(status);
100 * Updates the internal state based on the given message.
102 * @param message message from which to update the internal state
103 * @return a response to the message
105 public synchronized PdpStatus updateInternalState(PdpStateChange message) {
106 LOGGER.info("set state of {} to {}", this, message.getState());
107 status.setState(message.getState());
110 * NOTE: Do NOT update group & subgroup as state-change requests do not set those
111 * fields to indicate new values; they only set them to do broadcasts to all PDPs
112 * within a group/subgroup.
115 PdpStatus status2 = makeResponse(message, "");
117 // start/stop rest controller based on state change
118 handleXacmlRestController();
120 // these fields aren't needed in the response, so clear them out to avoid sending
121 status2.setPolicies(null);
127 * Updates the internal state based on the given message. Assumes that the policies
128 * have already been updated within the application manager.
130 * @param message message from which to update the internal state
131 * @return a response to the message
133 public synchronized PdpStatus updateInternalState(PdpUpdate message, String errMessage) {
134 status.setPdpSubgroup(message.getPdpSubgroup());
135 status.setPolicies(appManager.getToscaPolicyIdentifiers());
137 return makeResponse(message, errMessage);
141 * Updates the internal state to Terminated.
143 * @return the current PdpStatus with Terminated state
145 public synchronized PdpStatus terminatePdpMessage() {
146 LOGGER.info("set state of {} to {}", this, PdpState.TERMINATED);
147 status.setState(PdpState.TERMINATED);
148 return new PdpStatus(status);
152 * Makes a response to the given message, based on the current state.
154 * @param message message for which the response should be made
155 * @param errMessage the error message to be sent to PAP
156 * @return a new response
158 private PdpStatus makeResponse(PdpMessage message, String errMessage) {
159 var resp = new PdpResponseDetails();
161 if (StringUtils.isBlank(errMessage)) {
162 resp.setResponseStatus(PdpResponseStatus.SUCCESS);
164 resp.setResponseStatus(PdpResponseStatus.FAIL);
165 resp.setResponseMessage(errMessage);
167 resp.setResponseTo(message.getRequestId());
169 var status2 = new PdpStatus(status);
170 status2.setResponse(resp);
175 * Manages the Xacml-Pdp rest controller based on the Xacml-Pdp State.
176 * Current supported states:
177 * ACTIVE - rest service is running and handling requests
178 * PASSIVE - rest service is not running
180 private void handleXacmlRestController() {
181 if (status.getState() == PdpState.ACTIVE) {
182 LOGGER.info("State change: {} - Starting rest controller", status.getState());
183 XacmlPdpActivator.getCurrent().enableApi();
184 } else if (status.getState() == PdpState.PASSIVE) {
185 LOGGER.info("State change: {} - Stopping rest controller", status.getState());
186 XacmlPdpActivator.getCurrent().disableApi();
189 LOGGER.warn("Unsupported state: {}", status.getState());