2 * ============LICENSE_START=======================================================
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
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.xacml;
23 import java.util.List;
25 import java.util.concurrent.CompletableFuture;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.MediaType;
28 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
29 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
30 import org.onap.policy.controlloop.actorserviceprovider.CallbackManager;
31 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
32 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
33 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
35 import org.onap.policy.models.decisions.concepts.DecisionRequest;
36 import org.onap.policy.models.decisions.concepts.DecisionResponse;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * Generic Decision Operation.
43 public abstract class DecisionOperation extends HttpOperation<DecisionResponse> {
44 private static final Logger logger = LoggerFactory.getLogger(DecisionOperation.class);
46 protected final DecisionConfig config;
50 * Constructs the object.
52 * @param params operation parameters
53 * @param config configuration for this operation
54 * @param propertyNames names of properties required by this operation
56 protected DecisionOperation(ControlLoopOperationParams params, HttpConfig config,
57 List<String> propertyNames) {
58 super(params, config, DecisionResponse.class, propertyNames);
59 this.config = (DecisionConfig) config;
63 public CompletableFuture<OperationOutcome> start() {
64 if (!config.isDisabled()) {
65 // enabled - do full guard operation
69 // guard is disabled, thus it is always treated as a success
70 logger.info("{}: guard disabled, always succeeds for {}", getFullName(), params.getRequestId());
72 final var executor = params.getExecutor();
73 final var callbacks = new CallbackManager();
75 return CompletableFuture.completedFuture(makeOutcome())
76 .whenCompleteAsync(callbackStarted(callbacks), executor)
77 .whenCompleteAsync(callbackCompleted(callbacks), executor);
81 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
82 DecisionRequest request = makeRequest();
84 Map<String, Object> headers = makeHeaders();
86 headers.put("Accept", MediaType.APPLICATION_JSON);
87 String url = getUrl();
89 String strRequest = prettyPrint(request);
90 logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
92 Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
95 return handleResponse(outcome, url,
96 callback -> getClient().post(callback, getPath(), entity, headers));
101 * Makes a request from the payload.
103 * @return a new request
105 protected abstract DecisionRequest makeRequest();