ed8e6778b3d5c751c22e65e78f788c6f705bed94
[policy/models.git] / models-interactions / model-actors / actor.xacml / src / main / java / org / onap / policy / controlloop / actor / xacml / ConfigureOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 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 public class ConfigureOperation extends DecisionOperation {
35
36     // operation name
37     public static final String NAME = "Configure";
38
39     /**
40      * Constructs the object.
41      *
42      * @param params operation parameters
43      * @param config configuration for this operation
44      */
45     public ConfigureOperation(ControlLoopOperationParams params, HttpConfig config) {
46         super(params, config, Collections.emptyList());
47     }
48
49     @Override
50     protected DecisionRequest makeRequest() {
51         if (params.getPayload() == null) {
52             throw new IllegalArgumentException("missing payload");
53         }
54
55         DecisionRequest req = config.makeRequest();
56         req.setRequestId(getSubRequestId());
57         req.setResource(params.getPayload());
58
59         return req;
60     }
61
62     @Override
63     protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
64                     Response rawResponse, DecisionResponse response) {
65
66         outcome.setResponse(response);
67
68         // check for policies
69         Map<String, Object> policies = response.getPolicies();
70         if (policies == null || policies.isEmpty()) {
71             outcome.setResult(OperationResult.FAILURE);
72             outcome.setMessage("response contains no policies");
73             return CompletableFuture.completedFuture(outcome);
74         }
75
76         outcome.setResult(OperationResult.SUCCESS);
77
78         // set the message
79         outcome.setMessage(response.getMessage());
80
81         return CompletableFuture.completedFuture(outcome);
82     }
83 }