2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.controlloop.actor.aai;
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;
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;
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.
49 public class AaiCustomQueryOperation extends HttpOperation<String> {
50 public static final String NAME = AaiCqResponse.OPERATION;
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";
56 private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.AAI_VSERVER_LINK);
59 * Constructs the object.
61 * @param params operation parameters
62 * @param config configuration for this operation
64 public AaiCustomQueryOperation(ControlLoopOperationParams params, HttpConfig config) {
65 super(params, config, String.class, PROPERTY_NAMES);
69 public void generateSubRequestId(int attempt) {
70 setSubRequestId(String.valueOf(attempt));
74 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
75 outcome.setSubRequestId(String.valueOf(attempt));
77 final Map<String, String> request = makeRequest();
78 Map<String, Object> headers = makeHeaders();
80 var str = new StringBuilder(getClient().getBaseUrl());
82 String path = getPath();
83 WebTarget web = getClient().getWebTarget().path(path);
86 web = addQuery(web, str, "?", "format", "resource");
88 Builder webldr = web.request();
89 for (Entry<String, Object> header : headers.entrySet()) {
90 webldr.header(header.getKey(), header.getValue());
93 var url = str.toString();
95 String strRequest = prettyPrint(request);
96 logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
98 Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
100 return handleResponse(outcome, url, callback -> webldr.async().put(entity, callback));
103 private WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) {
104 str.append(separator);
109 return web.queryParam(name, value);
113 * Constructs the custom query using the previously retrieved tenant data.
115 private Map<String, String> makeRequest() {
116 return Map.of("start", getVserverLink(), "query", "query/closed-loop");
120 * Gets the vserver link, first checking the properties, and then the tenant data.
122 * @return the vserver link
124 protected String getVserverLink() {
125 return getRequiredProperty(OperationProperties.AAI_VSERVER_LINK, "vserver link");
129 protected Map<String, Object> makeHeaders() {
130 return AaiUtil.makeHeaders(params);
134 public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
137 super.setOutcome(outcome, result, rawResponse, response);
139 if (response != null) {
140 outcome.setResponse(new AaiCqResponse(response));