Java 17 Upgrade
[policy/models.git] / models-interactions / model-actors / actor.aai / src / main / java / org / onap / policy / controlloop / actor / aai / AaiCustomQueryOperation.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.aai;
23
24 import jakarta.ws.rs.client.Entity;
25 import jakarta.ws.rs.client.Invocation.Builder;
26 import jakarta.ws.rs.client.WebTarget;
27 import jakarta.ws.rs.core.MediaType;
28 import jakarta.ws.rs.core.Response;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.concurrent.CompletableFuture;
33 import org.onap.policy.aai.AaiCqResponse;
34 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
35 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
37 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
38 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
39 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
41 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
42
43 /**
44  * A&AI Custom Query. Stores the {@link AaiCqResponse} in the context. In addition, if the
45  * context does not contain the "tenant" data for the vserver, then it will request that,
46  * as well. Note: this ignores the "target entity" in the parameters as this query always
47  * applies to the vserver, thus the target entity may be set to an empty string.
48  */
49 public class AaiCustomQueryOperation extends HttpOperation<String> {
50     public static final String NAME = AaiCqResponse.OPERATION;
51
52     public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
53     public static final String RESOURCE_LINK = "resource-link";
54     public static final String RESULT_DATA = "result-data";
55
56     private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.AAI_VSERVER_LINK);
57
58     /**
59      * Constructs the object.
60      *
61      * @param params operation parameters
62      * @param config configuration for this operation
63      */
64     public AaiCustomQueryOperation(ControlLoopOperationParams params, HttpConfig config) {
65         super(params, config, String.class, PROPERTY_NAMES);
66     }
67
68     @Override
69     public void generateSubRequestId(int attempt) {
70         setSubRequestId(String.valueOf(attempt));
71     }
72
73     @Override
74     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
75         outcome.setSubRequestId(String.valueOf(attempt));
76
77         final Map<String, String> request = makeRequest();
78         Map<String, Object> headers = makeHeaders();
79
80         var str = new StringBuilder(getClient().getBaseUrl());
81
82         String path = getPath();
83         WebTarget web = getClient().getWebTarget().path(path);
84         str.append(path);
85
86         web = addQuery(web, str, "?", "format", "resource");
87
88         Builder webldr = web.request();
89         for (Entry<String, Object> header : headers.entrySet()) {
90             webldr.header(header.getKey(), header.getValue());
91         }
92
93         var url = str.toString();
94
95         String strRequest = prettyPrint(request);
96         logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
97
98         Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
99
100         return handleResponse(outcome, url, callback -> webldr.async().put(entity, callback));
101     }
102
103     private WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) {
104         str.append(separator);
105         str.append(name);
106         str.append('=');
107         str.append(value);
108
109         return web.queryParam(name, value);
110     }
111
112     /**
113      * Constructs the custom query using the previously retrieved tenant data.
114      */
115     private Map<String, String> makeRequest() {
116         return Map.of("start", getVserverLink(), "query", "query/closed-loop");
117     }
118
119     /**
120      * Gets the vserver link, first checking the properties, and then the tenant data.
121      *
122      * @return the vserver link
123      */
124     protected String getVserverLink() {
125         return getRequiredProperty(OperationProperties.AAI_VSERVER_LINK, "vserver link");
126     }
127
128     @Override
129     protected Map<String, Object> makeHeaders() {
130         return AaiUtil.makeHeaders(params);
131     }
132
133     @Override
134     public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
135                     String response) {
136
137         super.setOutcome(outcome, result, rawResponse, response);
138
139         if (response != null) {
140             outcome.setResponse(new AaiCqResponse(response));
141         }
142
143         return outcome;
144     }
145 }