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