Java 17 Upgrade
[policy/models.git] / models-interactions / model-actors / actor.xacml / src / main / java / org / onap / policy / controlloop / actor / xacml / GuardOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.xacml;
23
24 import jakarta.ws.rs.core.Response;
25 import java.util.Collections;
26 import java.util.Map;
27 import java.util.concurrent.CompletableFuture;
28 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
29 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
30 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
31 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
32 import org.onap.policy.models.decisions.concepts.DecisionRequest;
33 import org.onap.policy.models.decisions.concepts.DecisionResponse;
34
35 /**
36  * Guard Operation. The outcome message is set to the guard response. If the guard is
37  * permitted or indeterminate, then the outcome is set to SUCCESS.
38  * <p/>
39  * The input to the request is taken from the payload, where properties are mapped to the
40  * field names in the {@link DecisionRequest} object. Properties whose names begin with
41  * "resource." are placed into the "resource" field of the {@link DecisionRequest}. The
42  * following will be provided, if not specified in the payload:
43  * <dl>
44  * <dt>action</dt>
45  * <dd>"guard"</dd>
46  * <dt>request ID</dt>
47  * <dd>generated</dd>
48  * </dl>
49  */
50 public class GuardOperation extends DecisionOperation {
51     // operation name
52     public static final String NAME = "Guard";
53
54     public static final String PERMIT = "Permit";
55     public static final String DENY = "Deny";
56     public static final String INDETERMINATE = "Indeterminate";
57
58
59     /**
60      * Constructs the object.
61      *
62      * @param params operation parameters
63      * @param config configuration for this operation
64      */
65     public GuardOperation(ControlLoopOperationParams params, HttpConfig config) {
66         super(params, config, Collections.emptyList());
67     }
68
69     /**
70      * Makes a request from the payload.
71      *
72      * @return a new request
73      */
74     protected DecisionRequest makeRequest() {
75         if (params.getPayload() == null) {
76             throw new IllegalArgumentException("missing payload");
77         }
78
79         DecisionRequest req = config.makeRequest();
80         req.setRequestId(getSubRequestId());
81         req.setResource(Map.of("guard", params.getPayload()));
82
83         return req;
84     }
85
86     @Override
87     protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
88                     Response rawResponse, DecisionResponse response) {
89
90         outcome.setResponse(response);
91
92         // determine the result
93         String status = response.getStatus();
94         if (status == null) {
95             outcome.setResult(OperationResult.FAILURE);
96             outcome.setMessage("response contains no status");
97             return CompletableFuture.completedFuture(outcome);
98         }
99
100         if (PERMIT.equalsIgnoreCase(status) || INDETERMINATE.equalsIgnoreCase(status)) {
101             outcome.setResult(OperationResult.SUCCESS);
102         } else {
103             outcome.setResult(OperationResult.FAILURE);
104         }
105
106         // set the message
107         outcome.setMessage(response.getStatus());
108
109         return CompletableFuture.completedFuture(outcome);
110     }
111 }