Upgrade Java 17 in xacml-pdp
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / XacmlState.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019, 2021-2022 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2023 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main;
23
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.PdpStatus;
31 import org.onap.policy.models.pdp.concepts.PdpUpdate;
32 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
33 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
34 import org.onap.policy.models.pdp.enums.PdpState;
35 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
36 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * Current state of this XACML PDP.
42  */
43 public class XacmlState {
44     // The logger for this class
45     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlState.class);
46
47     /**
48      * Unique name for the xacml-pdp JVM, used in PdpStatus messages.
49      */
50     public static final String PDP_NAME = NetworkUtil.genUniqueName("xacml");
51
52     /**
53      * The application manager.
54      */
55     private final XacmlPdpApplicationManager appManager;
56
57     /**
58      * Records the current state of this PDP.
59      */
60     private final PdpStatus status;
61
62     /**
63      * Constructs the object, initializing the state.
64      */
65     public XacmlState(XacmlPdpApplicationManager appManager, String pdpGroupName, String pdpType) {
66         this.appManager = appManager;
67
68         this.status = new PdpStatus();
69         this.status.setName(PDP_NAME);
70         this.status.setPdpType(pdpType);
71         this.status.setState(PdpState.PASSIVE);
72         this.status.setPolicies(Collections.emptyList());
73         this.status.setPdpGroup(pdpGroupName);
74     }
75
76     /**
77      * Determines if this PDP should handle the given message.
78      *
79      * @param message message of interest
80      * @return {@code true} if this PDP should handle the message, {@code false} otherwise
81      */
82     public boolean shouldHandle(PdpMessage message) {
83         return message.appliesTo(status.getName(), status.getPdpGroup(), status.getPdpType());
84     }
85
86     /**
87      * Generates a new heart beat message.
88      *
89      * @return a new heart beat message
90      */
91     public synchronized PdpStatus genHeartbeat() {
92         // first, update status fields
93         status.setHealthy(XacmlPdpActivator.getCurrent().isAlive() ? PdpHealthStatus.HEALTHY
94             : PdpHealthStatus.NOT_HEALTHY);
95
96         PdpStatus heartbeat = new PdpStatus(status);
97         return heartbeat;
98     }
99
100     /**
101      * Updates the internal state based on the given message.
102      *
103      * @param message message from which to update the internal state
104      * @return a response to the message
105      */
106     public synchronized PdpStatus updateInternalState(PdpStateChange message) {
107         LOGGER.info("set state of {} to {}", this, message.getState());
108         status.setState(message.getState());
109
110         /*
111          * NOTE: Do NOT update group & subgroup as state-change requests do not set those
112          * fields to indicate new values; they only set them to do broadcasts to all PDPs
113          * within a group/subgroup.
114          */
115
116         PdpStatus status2 = makeResponse(message, "");
117
118         // start/stop rest controller based on state change
119         handleXacmlRestController();
120
121         // these fields aren't needed in the response, so clear them out to avoid sending
122         status2.setPolicies(null);
123
124         return status2;
125     }
126
127     /**
128      * Updates the internal state based on the given message. Assumes that the policies
129      * have already been updated within the application manager.
130      *
131      * @param message message from which to update the internal state
132      * @return a response to the message
133      */
134     public synchronized PdpStatus updateInternalState(PdpUpdate message, String errMessage) {
135         status.setPdpSubgroup(message.getPdpSubgroup());
136         status.setPolicies(appManager.getToscaPolicyIdentifiers());
137
138         return makeResponse(message, errMessage);
139     }
140
141     /**
142      * Updates the internal state to Terminated.
143      *
144      * @return the current PdpStatus with Terminated state
145      */
146     public synchronized PdpStatus terminatePdpMessage() {
147         LOGGER.info("set state of {} to {}", this, PdpState.TERMINATED);
148         status.setState(PdpState.TERMINATED);
149         return new PdpStatus(status);
150     }
151
152     /**
153      * Makes a response to the given message, based on the current state.
154      *
155      * @param message message for which the response should be made
156      * @param errMessage the error message to be sent to PAP
157      * @return a new response
158      */
159     private PdpStatus makeResponse(PdpMessage message, String errMessage) {
160         var resp = new PdpResponseDetails();
161
162         if (StringUtils.isBlank(errMessage)) {
163             resp.setResponseStatus(PdpResponseStatus.SUCCESS);
164         } else {
165             resp.setResponseStatus(PdpResponseStatus.FAIL);
166             resp.setResponseMessage(errMessage);
167         }
168         resp.setResponseTo(message.getRequestId());
169
170         var status2 = new PdpStatus(status);
171         status2.setResponse(resp);
172         return status2;
173     }
174
175     /**
176      * Manages the Xacml-Pdp rest controller based on the Xacml-Pdp State.
177      * Current supported states:
178      * ACTIVE  - rest service is running and handling requests
179      * PASSIVE - rest service is not running
180      */
181     private void handleXacmlRestController() {
182         if (status.getState() == PdpState.ACTIVE) {
183             LOGGER.info("State change: {} - Starting rest controller", status.getState());
184             XacmlPdpActivator.getCurrent().enableApi();
185         } else if (status.getState() == PdpState.PASSIVE) {
186             LOGGER.info("State change: {} - Stopping rest controller", status.getState());
187             XacmlPdpActivator.getCurrent().disableApi();
188         } else {
189             // unsupported state
190             LOGGER.warn("Unsupported state: {}", status.getState());
191         }
192     }
193 }