e35caa06cba7a6bba569bff7f60267faa2552104
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020 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.guard;
22
23 import java.util.Map;
24 import java.util.UUID;
25 import java.util.concurrent.CompletableFuture;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
30 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
31 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
32 import org.onap.policy.controlloop.actorserviceprovider.Util;
33 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
34 import org.onap.policy.controlloop.actorserviceprovider.impl.OperationPartial;
35 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
36 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
37 import org.onap.policy.controlloop.policy.PolicyResult;
38 import org.onap.policy.models.decisions.concepts.DecisionRequest;
39 import org.onap.policy.models.decisions.concepts.DecisionResponse;
40
41 /**
42  * Guard Operation. The outcome message is set to the guard response. If the guard is
43  * permitted or indeterminate, then the outcome is set to SUCCESS.
44  * <p/>
45  * The input to the request is taken from the payload, where properties are mapped to the
46  * field names in the {@link DecisionRequest} object. Properties whose names begin with
47  * "resource." are placed into the "resource" field of the {@link DecisionRequest}. The
48  * following will be provided, if not specified in the payload:
49  * <dl>
50  * <dt>action</dt>
51  * <dd>"guard"</dd>
52  * <dt>request ID</dt>
53  * <dd>generated</dd>
54  * </dl>
55  */
56 public class GuardOperation extends HttpOperation<DecisionResponse> {
57
58     // operation name
59     public static final String NAME = OperationPartial.GUARD_OPERATION_NAME;
60
61     public static final String PERMIT = "Permit";
62     public static final String DENY = "Deny";
63     public static final String INDETERMINATE = "Indeterminate";
64
65     /**
66      * Prefix for properties in the payload that should be copied to the "resource" field
67      * of the request.
68      */
69     public static final String RESOURCE_PREFIX = "resource.";
70
71     private final GuardConfig config;
72
73
74     /**
75      * Constructs the object.
76      *
77      * @param params operation parameters
78      * @param config configuration for this operation
79      */
80     public GuardOperation(ControlLoopOperationParams params, HttpConfig config) {
81         super(params, config, DecisionResponse.class);
82         this.config = (GuardConfig) config;
83     }
84
85     @Override
86     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
87         if (config.isDisabled()) {
88             // guard is disabled, thus it is always treated as a success
89             return CompletableFuture.completedFuture(params.makeOutcome());
90         }
91
92         DecisionRequest request = Util.translate(getName(), makeRequest(), DecisionRequest.class);
93
94         Entity<DecisionRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
95
96         Map<String, Object> headers = makeHeaders();
97
98         headers.put("Accept", MediaType.APPLICATION_JSON);
99         String url = makeUrl();
100
101         logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
102
103         // @formatter:off
104         return handleResponse(outcome, url,
105             callback -> getClient().post(callback, makePath(), entity, headers));
106         // @formatter:on
107     }
108
109     /**
110      * Makes a request from the payload.
111      *
112      * @return a new request map
113      */
114     protected Map<String, Object> makeRequest() {
115         if (params.getPayload() == null) {
116             throw new IllegalArgumentException("missing payload");
117         }
118
119         Map<String, Object> req = config.makeRequest();
120         req.putAll(params.getPayload());
121         req.computeIfAbsent("requestId", key -> UUID.randomUUID().toString());
122
123         return req;
124     }
125
126     @Override
127     protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
128                     Response rawResponse, DecisionResponse response) {
129
130         // determine the result
131         String status = response.getStatus();
132         if (status == null) {
133             outcome.setResult(PolicyResult.FAILURE);
134             outcome.setMessage("response contains no status");
135             return CompletableFuture.completedFuture(outcome);
136         }
137
138         if (PERMIT.equalsIgnoreCase(status) || INDETERMINATE.equalsIgnoreCase(status)) {
139             outcome.setResult(PolicyResult.SUCCESS);
140         } else {
141             outcome.setResult(PolicyResult.FAILURE);
142         }
143
144         // set the message
145         outcome.setMessage(response.getStatus());
146
147         return CompletableFuture.completedFuture(outcome);
148     }
149 }