2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2021 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;
38 * Current state of this XACML PDP.
40 public class XacmlState {
42 * Unique name for the xacml-pdp JVM, used in PdpStatus messages.
44 public static final String PDP_NAME = NetworkUtil.genUniqueName("xacml");
47 * The application manager.
49 private final XacmlPdpApplicationManager appManager;
52 * Records the current state of this PDP.
54 private final PdpStatus status;
57 * Constructs the object, initializing the state.
59 public XacmlState(XacmlPdpApplicationManager appManager, String pdpGroupName, String pdpType) {
60 this.appManager = appManager;
62 this.status = new PdpStatus();
63 this.status.setName(PDP_NAME);
64 this.status.setPdpType(pdpType);
65 this.status.setState(PdpState.PASSIVE);
66 this.status.setPolicies(Collections.emptyList());
67 this.status.setPdpGroup(pdpGroupName);
71 * Determines if this PDP should handle the given message.
73 * @param message message of interest
74 * @return {@code true} if this PDP should handle the message, {@code false} otherwise
76 public boolean shouldHandle(PdpMessage message) {
77 return message.appliesTo(status.getName(), status.getPdpGroup(), status.getPdpSubgroup());
81 * Generates a new heart beat message.
83 * @return a new heart beat message
85 public PdpStatus genHeartbeat() {
86 // first, update status fields
87 status.setHealthy(XacmlPdpActivator.getCurrent().isAlive() ? PdpHealthStatus.HEALTHY
88 : PdpHealthStatus.NOT_HEALTHY);
90 return new PdpStatus(status);
94 * Updates the internal state based on the given message.
96 * @param message message from which to update the internal state
97 * @return a response to the message
99 public PdpStatus updateInternalState(PdpStateChange message) {
100 status.setState(message.getState());
103 * NOTE: Do NOT update group & subgroup as state-change requests do not set those
104 * fields to indicate new values; they only set them to do broadcasts to all PDPs
105 * within a group/subgroup.
108 PdpStatus status2 = makeResponse(message, "");
110 // these fields aren't needed in the response, so clear them out to avoid sending
111 status2.setPolicies(null);
117 * Updates the internal state based on the given message. Assumes that the policies
118 * have already been updated within the application manager.
120 * @param message message from which to update the internal state
121 * @return a response to the message
123 public PdpStatus updateInternalState(PdpUpdate message, String errMessage) {
124 status.setPdpSubgroup(message.getPdpSubgroup());
125 status.setPolicies(appManager.getToscaPolicyIdentifiers());
127 return makeResponse(message, errMessage);
131 * Updates the internal state to Terminated.
133 * @return the current PdpStatus with Terminated state
135 public PdpStatus terminatePdpMessage() {
136 status.setState(PdpState.TERMINATED);
137 return new PdpStatus(status);
141 * Makes a response to the given message, based on the current state.
143 * @param message message for which the response should be made
144 * @param errMessage the error message to be sent to PAP
145 * @return a new response
147 private PdpStatus makeResponse(PdpMessage message, String errMessage) {
148 var resp = new PdpResponseDetails();
150 if (StringUtils.isBlank(errMessage)) {
151 resp.setResponseStatus(PdpResponseStatus.SUCCESS);
153 resp.setResponseStatus(PdpResponseStatus.FAIL);
154 resp.setResponseMessage(errMessage);
156 resp.setResponseTo(message.getRequestId());
158 var status2 = new PdpStatus(status);
159 status2.setResponse(resp);