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