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;
23 import java.util.Collections;
24 import org.onap.policy.common.utils.network.NetworkUtil;
25 import org.onap.policy.models.pdp.concepts.PdpMessage;
26 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
27 import org.onap.policy.models.pdp.concepts.PdpStateChange;
28 import org.onap.policy.models.pdp.concepts.PdpStatus;
29 import org.onap.policy.models.pdp.concepts.PdpUpdate;
30 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
31 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
32 import org.onap.policy.models.pdp.enums.PdpState;
33 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
34 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
37 * Current state of this XACML PDP.
39 public class XacmlState {
42 * The application manager.
44 private final XacmlPdpApplicationManager appManager;
47 * Records the current state of this PDP.
49 private final PdpStatus status;
53 * Constructs the object, initializing the state.
55 public XacmlState(XacmlPdpApplicationManager appManager) {
56 this.appManager = appManager;
58 this.status = new PdpStatus();
59 this.status.setName(NetworkUtil.getHostname());
60 this.status.setPdpType("xacml");
61 this.status.setState(PdpState.PASSIVE);
62 this.status.setSupportedPolicyTypes(appManager.getToscaPolicyTypeIdents());
63 this.status.setPolicies(Collections.emptyList());
67 * Determines if this PDP should handle the given message.
69 * @param message message of interest
70 * @return {@code true} if this PDP should handle the message, {@code false} otherwise
72 public boolean shouldHandle(PdpMessage message) {
73 return message.appliesTo(status.getName(), status.getPdpGroup(), status.getPdpSubgroup());
77 * Generates a new heart beat message.
79 * @return a new heart beat message
81 public PdpStatus genHeartbeat() {
82 // first, update status fields
83 status.setHealthy(XacmlPdpActivator.getCurrent().isAlive() ? PdpHealthStatus.HEALTHY
84 : PdpHealthStatus.NOT_HEALTHY);
86 return new PdpStatus(status);
90 * Updates the internal state based on the given message.
92 * @param message message from which to update the internal state
93 * @return a response to the message
95 public PdpStatus updateInternalState(PdpStateChange message) {
96 status.setState(message.getState());
99 * NOTE: Do NOT update group & subgroup as state-change requests do not set those
100 * fields to indicate new values; they only set them to do broadcasts to all PDPs
101 * within a group/subgroup.
104 PdpStatus status2 = makeResponse(message);
106 // these fields aren't needed in the response, so clear them out to avoid sending
107 status2.setPolicies(null);
113 * Updates the internal state based on the given message. Assumes that the policies
114 * have already been updated within the application manager.
116 * @param message message from which to update the internal state
117 * @return a response to the message
119 public PdpStatus updateInternalState(PdpUpdate message) {
120 status.setPdpGroup(message.getPdpGroup());
121 status.setPdpSubgroup(message.getPdpSubgroup());
122 status.setPolicies(appManager.getToscaPolicyIdentifiers());
124 return makeResponse(message);
128 * Updates the internal state to Terminated.
130 * @return the current PdpStatus with Terminated state
132 public PdpStatus terminatePdpMessage() {
133 status.setState(PdpState.TERMINATED);
134 return new PdpStatus(status);
138 * Makes a response to the given message, based on the current state.
140 * @param message message for which the response should be made
141 * @return a new response
143 private PdpStatus makeResponse(PdpMessage message) {
144 PdpResponseDetails resp = new PdpResponseDetails();
145 resp.setResponseStatus(PdpResponseStatus.SUCCESS);
146 resp.setResponseTo(message.getRequestId());
148 PdpStatus status2 = new PdpStatus(status);
149 status2.setResponse(resp);