Added code to send Termination message to PAP
[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.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;
35
36 /**
37  * Current state of this XACML PDP.
38  */
39 public class XacmlState {
40
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     /**
53      * Constructs the object, initializing the state.
54      */
55     public XacmlState(XacmlPdpApplicationManager appManager) {
56         this.appManager = appManager;
57
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());
64     }
65
66     /**
67      * Determines if this PDP should handle the given message.
68      *
69      * @param message message of interest
70      * @return {@code true} if this PDP should handle the message, {@code false} otherwise
71      */
72     public boolean shouldHandle(PdpMessage message) {
73         return message.appliesTo(status.getName(), status.getPdpGroup(), status.getPdpSubgroup());
74     }
75
76     /**
77      * Generates a new heart beat message.
78      *
79      * @return a new heart beat message
80      */
81     public PdpStatus genHeartbeat() {
82         // first, update status fields
83         status.setHealthy(XacmlPdpActivator.getCurrent().isAlive() ? PdpHealthStatus.HEALTHY
84                         : PdpHealthStatus.NOT_HEALTHY);
85
86         return new PdpStatus(status);
87     }
88
89     /**
90      * Updates the internal state based on the given message.
91      *
92      * @param message message from which to update the internal state
93      * @return a response to the message
94      */
95     public PdpStatus updateInternalState(PdpStateChange message) {
96         status.setState(message.getState());
97
98         /*
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.
102          */
103
104         PdpStatus status2 = makeResponse(message);
105
106         // these fields aren't needed in the response, so clear them out to avoid sending
107         status2.setPolicies(null);
108
109         return status2;
110     }
111
112     /**
113      * Updates the internal state based on the given message. Assumes that the policies
114      * have already been updated within the application manager.
115      *
116      * @param message message from which to update the internal state
117      * @return a response to the message
118      */
119     public PdpStatus updateInternalState(PdpUpdate message) {
120         status.setPdpGroup(message.getPdpGroup());
121         status.setPdpSubgroup(message.getPdpSubgroup());
122         status.setPolicies(appManager.getToscaPolicyIdentifiers());
123
124         return makeResponse(message);
125     }
126
127     /**
128      * Updates the internal state to Terminated.
129      *
130      * @return the current PdpStatus with Terminated state
131      */
132     public PdpStatus terminatePdpMessage() {
133         status.setState(PdpState.TERMINATED);
134         return status;
135     }
136
137     /**
138      * Makes a response to the given message, based on the current state.
139      *
140      * @param message message for which the response should be made
141      * @return a new response
142      */
143     private PdpStatus makeResponse(PdpMessage message) {
144         PdpResponseDetails resp = new PdpResponseDetails();
145         resp.setResponseStatus(PdpResponseStatus.SUCCESS);
146         resp.setResponseTo(message.getRequestId());
147
148         PdpStatus status2 = new PdpStatus(status);
149         status2.setResponse(resp);
150         return status2;
151     }
152 }