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.time.Instant;
24 import java.util.Collections;
25 import org.apache.commons.lang3.StringUtils;
26 import org.onap.policy.common.utils.network.NetworkUtil;
27 import org.onap.policy.models.pdp.concepts.PdpMessage;
28 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
29 import org.onap.policy.models.pdp.concepts.PdpStateChange;
30 import org.onap.policy.models.pdp.concepts.PdpStatistics;
31 import org.onap.policy.models.pdp.concepts.PdpStatus;
32 import org.onap.policy.models.pdp.concepts.PdpUpdate;
33 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
34 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
35 import org.onap.policy.models.pdp.enums.PdpState;
36 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
37 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
38 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Current state of this XACML PDP.
45 public class XacmlState {
46 // The logger for this class
47 private static final Logger LOGGER = LoggerFactory.getLogger(XacmlState.class);
50 * Unique name for the xacml-pdp JVM, used in PdpStatus messages.
52 public static final String PDP_NAME = NetworkUtil.genUniqueName("xacml");
55 * The application manager.
57 private final XacmlPdpApplicationManager appManager;
60 * Records the current state of this PDP.
62 private final PdpStatus status;
65 * Constructs the object, initializing the state.
67 public XacmlState(XacmlPdpApplicationManager appManager, String pdpGroupName, String pdpType) {
68 this.appManager = appManager;
70 this.status = new PdpStatus();
71 this.status.setName(PDP_NAME);
72 this.status.setPdpType(pdpType);
73 this.status.setState(PdpState.PASSIVE);
74 this.status.setPolicies(Collections.emptyList());
75 this.status.setPdpGroup(pdpGroupName);
79 * Determines if this PDP should handle the given message.
81 * @param message message of interest
82 * @return {@code true} if this PDP should handle the message, {@code false} otherwise
84 public boolean shouldHandle(PdpMessage message) {
85 return message.appliesTo(status.getName(), status.getPdpGroup(), status.getPdpType());
89 * Generates a new heart beat message.
91 * @return a new heart beat message
93 public synchronized PdpStatus genHeartbeat() {
94 // first, update status fields
95 status.setHealthy(XacmlPdpActivator.getCurrent().isAlive() ? PdpHealthStatus.HEALTHY
96 : PdpHealthStatus.NOT_HEALTHY);
98 PdpStatus heartbeat = new PdpStatus(status);
99 heartbeat.setStatistics(getStatistics());
104 * Generates statistics to be used in a heart beat message.
106 * @return statistics for heart beat message
108 protected PdpStatistics getStatistics() {
109 XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
111 LOGGER.warn("XacmlPdpStatisticsManager is null");
114 stats.setTotalPolicyCount(appManager.getPolicyCount());
116 PdpStatistics pdpStats = new PdpStatistics();
117 pdpStats.setPdpGroupName(this.status.getPdpGroup());
118 pdpStats.setPdpSubGroupName(this.status.getPdpSubgroup());
119 pdpStats.setTimeStamp(Instant.ofEpochSecond(this.status.getTimestampMs()));
121 pdpStats.setPolicyExecutedCount(stats.getPermitDecisionsCount() + stats.getDenyDecisionsCount());
122 pdpStats.setPolicyExecutedSuccessCount(stats.getPermitDecisionsCount());
123 pdpStats.setPolicyExecutedFailCount(stats.getDenyDecisionsCount());
125 pdpStats.setPolicyDeployCount(stats.getDeploySuccessCount() + stats.getDeployFailureCount());
126 pdpStats.setPolicyDeploySuccessCount(stats.getDeploySuccessCount());
127 pdpStats.setPolicyDeployFailCount(stats.getDeployFailureCount());
129 pdpStats.setPolicyUndeployCount(stats.getUndeploySuccessCount() + stats.getUndeployFailureCount());
130 pdpStats.setPolicyUndeploySuccessCount(stats.getUndeploySuccessCount());
131 pdpStats.setPolicyUndeployFailCount(stats.getUndeployFailureCount());
136 * Updates the internal state based on the given message.
138 * @param message message from which to update the internal state
139 * @return a response to the message
141 public synchronized PdpStatus updateInternalState(PdpStateChange message) {
142 LOGGER.info("set state of {} to {}", this, message.getState());
143 status.setState(message.getState());
146 * NOTE: Do NOT update group & subgroup as state-change requests do not set those
147 * fields to indicate new values; they only set them to do broadcasts to all PDPs
148 * within a group/subgroup.
151 PdpStatus status2 = makeResponse(message, "");
153 // start/stop rest controller based on state change
154 handleXacmlRestController();
156 // these fields aren't needed in the response, so clear them out to avoid sending
157 status2.setPolicies(null);
163 * Updates the internal state based on the given message. Assumes that the policies
164 * have already been updated within the application manager.
166 * @param message message from which to update the internal state
167 * @return a response to the message
169 public synchronized PdpStatus updateInternalState(PdpUpdate message, String errMessage) {
170 status.setPdpSubgroup(message.getPdpSubgroup());
171 status.setPolicies(appManager.getToscaPolicyIdentifiers());
173 return makeResponse(message, errMessage);
177 * Updates the internal state to Terminated.
179 * @return the current PdpStatus with Terminated state
181 public synchronized PdpStatus terminatePdpMessage() {
182 LOGGER.info("set state of {} to {}", this, PdpState.TERMINATED);
183 status.setState(PdpState.TERMINATED);
184 return new PdpStatus(status);
188 * Makes a response to the given message, based on the current state.
190 * @param message message for which the response should be made
191 * @param errMessage the error message to be sent to PAP
192 * @return a new response
194 private PdpStatus makeResponse(PdpMessage message, String errMessage) {
195 var resp = new PdpResponseDetails();
197 if (StringUtils.isBlank(errMessage)) {
198 resp.setResponseStatus(PdpResponseStatus.SUCCESS);
200 resp.setResponseStatus(PdpResponseStatus.FAIL);
201 resp.setResponseMessage(errMessage);
203 resp.setResponseTo(message.getRequestId());
205 var status2 = new PdpStatus(status);
206 status2.setResponse(resp);
211 * Manages the Xacml-Pdp rest controller based on the Xacml-Pdp State.
212 * Current supported states:
213 * ACTIVE - rest service is running and handling requests
214 * PASSIVE - rest service is not running
216 private void handleXacmlRestController() {
217 if (status.getState() == PdpState.ACTIVE) {
218 LOGGER.info("State change: {} - Starting rest controller", status.getState());
219 XacmlPdpActivator.getCurrent().enableApi();
220 } else if (status.getState() == PdpState.PASSIVE) {
221 LOGGER.info("State change: {} - Stopping rest controller", status.getState());
222 XacmlPdpActivator.getCurrent().disableApi();
225 LOGGER.warn("Unsupported state: {}", status.getState());