2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.guard;
23 import java.util.LinkedHashMap;
25 import java.util.Map.Entry;
26 import java.util.UUID;
27 import java.util.concurrent.CompletableFuture;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
32 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
33 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
34 import org.onap.policy.controlloop.actorserviceprovider.Util;
35 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
36 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperator;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
38 import org.onap.policy.controlloop.policy.PolicyResult;
39 import org.onap.policy.models.decisions.concepts.DecisionRequest;
40 import org.onap.policy.models.decisions.concepts.DecisionResponse;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Guard Operation. The outcome message is set to the guard response. If the guard is
46 * permitted or indeterminate, then the outcome is set to SUCCESS.
48 * The input to the request is taken from the payload, where properties are mapped to the
49 * field names in the {@link DecisionRequest} object. Properties whose names begin with
50 * "resource." are placed into the "resource" field of the {@link DecisionRequest}. The
51 * following will be provided, if not specified in the payload:
59 public class GuardOperation extends HttpOperation<DecisionResponse> {
60 private static final Logger logger = LoggerFactory.getLogger(GuardOperation.class);
63 public static final String NAME = "Decision";
65 public static final String PERMIT = "Permit";
66 public static final String DENY = "Deny";
67 public static final String INDETERMINATE = "Indeterminate";
69 private static final String RESOURCE = "resource";
72 * Prefix for properties in the payload that should be copied to the "resource" field
75 public static final String RESOURCE_PREFIX = "resource.";
79 * Constructs the object.
81 * @param params operation parameters
82 * @param operator operator that created this operation
84 public GuardOperation(ControlLoopOperationParams params, HttpOperator operator) {
85 super(params, operator, DecisionResponse.class);
89 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
91 DecisionRequest request = Util.translate(getName(), makeRequest(), DecisionRequest.class);
93 Entity<DecisionRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
95 Map<String, Object> headers = makeHeaders();
97 headers.put("Accept", MediaType.APPLICATION_JSON);
98 String url = makeUrl();
100 logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
103 return handleResponse(outcome, url,
104 callback -> getOperator().getClient().post(callback, makePath(), entity, headers));
109 * Makes a request from the payload.
111 * @return a new request map
113 protected Map<String, Object> makeRequest() {
114 if (params.getPayload() == null) {
115 throw new IllegalArgumentException("missing payload");
119 * This code could be easily modified to allow the context and/or resource to be
120 * an encoded JSON string, that is decoded into a Map and stuffed into the
124 Map<String, Object> req = new LinkedHashMap<>();
125 Map<String, Object> resource = new LinkedHashMap<>();
127 for (Entry<String, String> ent : params.getPayload().entrySet()) {
128 String key = ent.getKey();
130 if (key.startsWith(RESOURCE_PREFIX)) {
131 // it's a resource property - put into the resource map
132 key = key.substring(RESOURCE_PREFIX.length());
133 resource.put(key, ent.getValue());
135 } else if (key.indexOf('.') < 0) {
136 // it's a normal property - put into the request map
137 req.put(key, ent.getValue());
140 logger.warn("{}: unused key {} in payload for {}", getFullName(), key, params.getRequestId());
144 req.putIfAbsent("action", "guard");
145 req.computeIfAbsent("requestId", key -> UUID.randomUUID().toString());
146 req.put(RESOURCE, resource);
152 protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
153 Response rawResponse, DecisionResponse response) {
155 // determine the result
156 String status = response.getStatus();
157 if (status == null) {
158 outcome.setResult(PolicyResult.FAILURE);
159 outcome.setMessage("response contains no status");
160 return CompletableFuture.completedFuture(outcome);
163 if (PERMIT.equalsIgnoreCase(status) || INDETERMINATE.equalsIgnoreCase(status)) {
164 outcome.setResult(PolicyResult.SUCCESS);
166 outcome.setResult(PolicyResult.FAILURE);
170 outcome.setMessage(response.getStatus());
172 return CompletableFuture.completedFuture(outcome);