Synchronize updates to xacml-pdp state
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / XacmlState.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main;
22
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;
38
39 /**
40  * Current state of this XACML PDP.
41  */
42 public class XacmlState {
43     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlState.class);
44
45     /**
46      * Unique name for the xacml-pdp JVM, used in PdpStatus messages.
47      */
48     public static final String PDP_NAME = NetworkUtil.genUniqueName("xacml");
49
50     /**
51      * The application manager.
52      */
53     private final XacmlPdpApplicationManager appManager;
54
55     /**
56      * Records the current state of this PDP.
57      */
58     private final PdpStatus status;
59
60     /**
61      * Constructs the object, initializing the state.
62      */
63     public XacmlState(XacmlPdpApplicationManager appManager, String pdpGroupName, String pdpType) {
64         this.appManager = appManager;
65
66         this.status = new PdpStatus();
67         this.status.setName(PDP_NAME);
68         this.status.setPdpType(pdpType);
69         this.status.setState(PdpState.PASSIVE);
70         this.status.setPolicies(Collections.emptyList());
71         this.status.setPdpGroup(pdpGroupName);
72     }
73
74     /**
75      * Determines if this PDP should handle the given message.
76      *
77      * @param message message of interest
78      * @return {@code true} if this PDP should handle the message, {@code false} otherwise
79      */
80     public boolean shouldHandle(PdpMessage message) {
81         return message.appliesTo(status.getName(), status.getPdpGroup(), status.getPdpType());
82     }
83
84     /**
85      * Generates a new heart beat message.
86      *
87      * @return a new heart beat message
88      */
89     public synchronized PdpStatus genHeartbeat() {
90         // first, update status fields
91         status.setHealthy(XacmlPdpActivator.getCurrent().isAlive() ? PdpHealthStatus.HEALTHY
92                         : PdpHealthStatus.NOT_HEALTHY);
93
94         return new PdpStatus(status);
95     }
96
97     /**
98      * Updates the internal state based on the given message.
99      *
100      * @param message message from which to update the internal state
101      * @return a response to the message
102      */
103     public synchronized PdpStatus updateInternalState(PdpStateChange message) {
104         LOGGER.info("set state of {} to {}", this, message.getState());
105         status.setState(message.getState());
106
107         /*
108          * NOTE: Do NOT update group & subgroup as state-change requests do not set those
109          * fields to indicate new values; they only set them to do broadcasts to all PDPs
110          * within a group/subgroup.
111          */
112
113         PdpStatus status2 = makeResponse(message, "");
114
115         // these fields aren't needed in the response, so clear them out to avoid sending
116         status2.setPolicies(null);
117
118         return status2;
119     }
120
121     /**
122      * Updates the internal state based on the given message. Assumes that the policies
123      * have already been updated within the application manager.
124      *
125      * @param message message from which to update the internal state
126      * @return a response to the message
127      */
128     public synchronized PdpStatus updateInternalState(PdpUpdate message, String errMessage) {
129         status.setPdpSubgroup(message.getPdpSubgroup());
130         status.setPolicies(appManager.getToscaPolicyIdentifiers());
131
132         return makeResponse(message, errMessage);
133     }
134
135     /**
136      * Updates the internal state to Terminated.
137      *
138      * @return the current PdpStatus with Terminated state
139      */
140     public synchronized PdpStatus terminatePdpMessage() {
141         LOGGER.info("set state of {} to {}", this, PdpState.TERMINATED);
142         status.setState(PdpState.TERMINATED);
143         return new PdpStatus(status);
144     }
145
146     /**
147      * Makes a response to the given message, based on the current state.
148      *
149      * @param message message for which the response should be made
150      * @param errMessage the error message to be sent to PAP
151      * @return a new response
152      */
153     private PdpStatus makeResponse(PdpMessage message, String errMessage) {
154         var resp = new PdpResponseDetails();
155
156         if (StringUtils.isBlank(errMessage)) {
157             resp.setResponseStatus(PdpResponseStatus.SUCCESS);
158         } else {
159             resp.setResponseStatus(PdpResponseStatus.FAIL);
160             resp.setResponseMessage(errMessage);
161         }
162         resp.setResponseTo(message.getRequestId());
163
164         var status2 = new PdpStatus(status);
165         status2.setResponse(resp);
166         return status2;
167     }
168 }