Java 17 Upgrade
[policy/models.git] / models-interactions / model-actors / actor.xacml / src / main / java / org / onap / policy / controlloop / actor / xacml / DecisionOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.controlloop.actor.xacml;
23
24 import jakarta.ws.rs.client.Entity;
25 import jakarta.ws.rs.core.MediaType;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.concurrent.CompletableFuture;
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.CallbackManager;
32 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
33 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
34 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
35 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
36 import org.onap.policy.models.decisions.concepts.DecisionRequest;
37 import org.onap.policy.models.decisions.concepts.DecisionResponse;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Generic Decision Operation.
43  */
44 public abstract class DecisionOperation extends HttpOperation<DecisionResponse> {
45     private static final Logger logger = LoggerFactory.getLogger(DecisionOperation.class);
46
47     protected final DecisionConfig config;
48
49
50     /**
51      * Constructs the object.
52      *
53      * @param params operation parameters
54      * @param config configuration for this operation
55      * @param propertyNames names of properties required by this operation
56      */
57     protected DecisionOperation(ControlLoopOperationParams params, HttpConfig config,
58                     List<String> propertyNames) {
59         super(params, config, DecisionResponse.class, propertyNames);
60         this.config = (DecisionConfig) config;
61     }
62
63     @Override
64     public CompletableFuture<OperationOutcome> start() {
65         if (!config.isDisabled()) {
66             // enabled - do full guard operation
67             return super.start();
68         }
69
70         // guard is disabled, thus it is always treated as a success
71         logger.info("{}: guard disabled, always succeeds for {}", getFullName(), params.getRequestId());
72
73         final var executor = params.getExecutor();
74         final var callbacks = new CallbackManager();
75
76         return CompletableFuture.completedFuture(makeOutcome())
77                         .whenCompleteAsync(callbackStarted(callbacks), executor)
78                         .whenCompleteAsync(callbackCompleted(callbacks), executor);
79     }
80
81     @Override
82     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
83         DecisionRequest request = makeRequest();
84
85         Map<String, Object> headers = makeHeaders();
86
87         headers.put("Accept", MediaType.APPLICATION_JSON);
88         String url = getUrl();
89
90         String strRequest = prettyPrint(request);
91         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
92
93         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
94
95         // @formatter:off
96         return handleResponse(outcome, url,
97             callback -> getClient().post(callback, getPath(), entity, headers));
98         // @formatter:on
99     }
100
101     /**
102      * Makes a request from the payload.
103      *
104      * @return a new request
105      */
106     protected abstract DecisionRequest makeRequest();
107 }