0792dc6eabb5644fdbc7406b9853ca1bfb36d1c8
[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  * ================================================================================
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.List;
24 import java.util.Map;
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;
39
40 /**
41  * Generic Decision Operation.
42  */
43 public abstract class DecisionOperation extends HttpOperation<DecisionResponse> {
44     private static final Logger logger = LoggerFactory.getLogger(DecisionOperation.class);
45
46     protected final DecisionConfig config;
47
48
49     /**
50      * Constructs the object.
51      *
52      * @param params operation parameters
53      * @param config configuration for this operation
54      * @param propertyNames names of properties required by this operation
55      */
56     protected DecisionOperation(ControlLoopOperationParams params, HttpConfig config,
57                     List<String> propertyNames) {
58         super(params, config, DecisionResponse.class, propertyNames);
59         this.config = (DecisionConfig) config;
60     }
61
62     @Override
63     public CompletableFuture<OperationOutcome> start() {
64         if (!config.isDisabled()) {
65             // enabled - do full guard operation
66             return super.start();
67         }
68
69         // guard is disabled, thus it is always treated as a success
70         logger.info("{}: guard disabled, always succeeds for {}", getFullName(), params.getRequestId());
71
72         final var executor = params.getExecutor();
73         final var callbacks = new CallbackManager();
74
75         return CompletableFuture.completedFuture(makeOutcome())
76                         .whenCompleteAsync(callbackStarted(callbacks), executor)
77                         .whenCompleteAsync(callbackCompleted(callbacks), executor);
78     }
79
80     @Override
81     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
82         DecisionRequest request = makeRequest();
83
84         Map<String, Object> headers = makeHeaders();
85
86         headers.put("Accept", MediaType.APPLICATION_JSON);
87         String url = getUrl();
88
89         String strRequest = prettyPrint(request);
90         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
91
92         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
93
94         // @formatter:off
95         return handleResponse(outcome, url,
96             callback -> getClient().post(callback, getPath(), entity, headers));
97         // @formatter:on
98     }
99
100     /**
101      * Makes a request from the payload.
102      *
103      * @return a new request
104      */
105     protected abstract DecisionRequest makeRequest();
106 }