Java 17 Upgrade
[policy/models.git] / models-interactions / model-actors / actor.sdnc / src / main / java / org / onap / policy / controlloop / actor / sdnc / SdncOperation.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.sdnc;
23
24 import jakarta.ws.rs.client.Entity;
25 import jakarta.ws.rs.core.MediaType;
26 import jakarta.ws.rs.core.Response;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.concurrent.CompletableFuture;
30 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
31 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
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.sdnc.SdncRequest;
37 import org.onap.policy.sdnc.SdncResponse;
38
39 /**
40  * Superclass for SDNC Operators.
41  */
42 public abstract class SdncOperation extends HttpOperation<SdncResponse> {
43
44     /**
45      * Constructs the object.
46      *
47      * @param params operation parameters
48      * @param config configuration for this operation
49      * @param propertyNames names of properties required by this operation
50      */
51     protected SdncOperation(ControlLoopOperationParams params, HttpConfig config, List<String> propertyNames) {
52         super(params, config, SdncResponse.class, propertyNames);
53     }
54
55     @Override
56     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
57
58         SdncRequest request = makeRequest(attempt);
59
60         Map<String, Object> headers = makeHeaders();
61
62         headers.put("Accept", MediaType.APPLICATION_JSON);
63         String path = getPath();
64         String url = getClient().getBaseUrl() + path;
65
66         String strRequest = prettyPrint(request);
67         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
68
69         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
70
71         // @formatter:off
72         return handleResponse(outcome, url,
73             callback -> getClient().post(callback, path, entity, headers));
74         // @formatter:on
75     }
76
77     /**
78      * Makes the request.
79      *
80      * @param attempt current attempt, starting with "1"
81      * @return a new request to be posted
82      */
83     protected abstract SdncRequest makeRequest(int attempt);
84
85     /**
86      * Checks that the response has an "output" and that the output indicates success.
87      */
88     @Override
89     protected boolean isSuccess(Response rawResponse, SdncResponse response) {
90         return response.getResponseOutput() != null && "200".equals(response.getResponseOutput().getResponseCode());
91     }
92 }