Changed Xacml-pdp to report pdp group defined in XacmlPdpParameters config file
[policy/xacml-pdp.git] / main / src / main / java / org / onap / policy / pdpx / main / XacmlState.java
1 /*-
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
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     // The logger for this class
44     private static final Logger LOGGER = LoggerFactory.getLogger(XacmlState.class);
45
46     /**
47      * The application manager.
48      */
49     private final XacmlPdpApplicationManager appManager;
50
51     /**
52      * Records the current state of this PDP.
53      */
54     private final PdpStatus status;
55
56     /**
57      * Constructs the object, initializing the state.
58      */
59     public XacmlState(XacmlPdpApplicationManager appManager, String pdpGroupName) {
60         this.appManager = appManager;
61
62         this.status = new PdpStatus();
63         this.status.setName(NetworkUtil.getHostname());
64         this.status.setPdpType("xacml");
65         this.status.setState(PdpState.PASSIVE);
66         this.status.setSupportedPolicyTypes(appManager.getToscaPolicyTypeIdents());
67         this.status.setPolicies(Collections.emptyList());
68         this.status.setPdpGroup(pdpGroupName);
69     }
70
71     /**
72      * Determines if this PDP should handle the given message.
73      *
74      * @param message message of interest
75      * @return {@code true} if this PDP should handle the message, {@code false} otherwise
76      */
77     public boolean shouldHandle(PdpMessage message) {
78         return message.appliesTo(status.getName(), status.getPdpGroup(), status.getPdpSubgroup());
79     }
80
81     /**
82      * Generates a new heart beat message.
83      *
84      * @return a new heart beat message
85      */
86     public PdpStatus genHeartbeat() {
87         // first, update status fields
88         status.setHealthy(XacmlPdpActivator.getCurrent().isAlive() ? PdpHealthStatus.HEALTHY
89                         : PdpHealthStatus.NOT_HEALTHY);
90
91         return new PdpStatus(status);
92     }
93
94     /**
95      * Updates the internal state based on the given message.
96      *
97      * @param message message from which to update the internal state
98      * @return a response to the message
99      */
100     public PdpStatus updateInternalState(PdpStateChange message) {
101         status.setState(message.getState());
102
103         /*
104          * NOTE: Do NOT update group & subgroup as state-change requests do not set those
105          * fields to indicate new values; they only set them to do broadcasts to all PDPs
106          * within a group/subgroup.
107          */
108
109         PdpStatus status2 = makeResponse(message, "");
110
111         // start/stop rest controller based on state change
112         handleXacmlRestController();
113
114         // these fields aren't needed in the response, so clear them out to avoid sending
115         status2.setPolicies(null);
116
117         return status2;
118     }
119
120     /**
121      * Updates the internal state based on the given message. Assumes that the policies
122      * have already been updated within the application manager.
123      *
124      * @param message message from which to update the internal state
125      * @return a response to the message
126      */
127     public PdpStatus updateInternalState(PdpUpdate message, String errMessage) {
128         status.setPdpSubgroup(message.getPdpSubgroup());
129         status.setPolicies(appManager.getToscaPolicyIdentifiers());
130
131         return makeResponse(message, errMessage);
132     }
133
134     /**
135      * Updates the internal state to Terminated.
136      *
137      * @return the current PdpStatus with Terminated state
138      */
139     public PdpStatus terminatePdpMessage() {
140         status.setState(PdpState.TERMINATED);
141         return new PdpStatus(status);
142     }
143
144     /**
145      * Makes a response to the given message, based on the current state.
146      *
147      * @param message message for which the response should be made
148      * @param errMessage the error message to be sent to PAP
149      * @return a new response
150      */
151     private PdpStatus makeResponse(PdpMessage message, String errMessage) {
152         PdpResponseDetails resp = new PdpResponseDetails();
153
154         if (StringUtils.isBlank(errMessage)) {
155             resp.setResponseStatus(PdpResponseStatus.SUCCESS);
156         } else {
157             resp.setResponseStatus(PdpResponseStatus.FAIL);
158             resp.setResponseMessage(errMessage);
159         }
160         resp.setResponseTo(message.getRequestId());
161
162         PdpStatus status2 = new PdpStatus(status);
163         status2.setResponse(resp);
164         return status2;
165     }
166
167     /**
168      * Manages the Xacml-Pdp rest controller based on the Xacml-Pdp State.
169      * Current supported states:
170      * ACTIVE  - rest service is running and handling requests
171      * PASSIVE - rest service is not running
172      */
173     private void handleXacmlRestController() {
174         if (status.getState() == PdpState.ACTIVE) {
175             LOGGER.info("State change: {} - Starting rest controller", status.getState());
176             XacmlPdpActivator.getCurrent().startXacmlRestController();
177         } else if (status.getState() == PdpState.PASSIVE) {
178             LOGGER.info("State change: {} - Stopping rest controller", status.getState());
179             XacmlPdpActivator.getCurrent().stopXacmlRestController();
180         } else {
181             // unsupported state
182             LOGGER.warn("Unsupported state: {}", status.getState());
183         }
184     }
185 }