2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controlloop.actor.aai;
23 import java.util.List;
25 import java.util.Map.Entry;
26 import java.util.concurrent.CompletableFuture;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.client.Invocation.Builder;
29 import javax.ws.rs.client.WebTarget;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import org.onap.policy.aai.AaiCqResponse;
33 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
34 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
35 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
36 import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
37 import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
38 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
39 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
40 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
43 * A&AI Custom Query. Stores the {@link AaiCqResponse} in the context. In addition, if the
44 * context does not contain the "tenant" data for the vserver, then it will request that,
45 * as well. Note: this ignores the "target entity" in the parameters as this query always
46 * applies to the vserver, thus the target entity may be set to an empty string.
48 public class AaiCustomQueryOperation extends HttpOperation<String> {
49 public static final String NAME = AaiCqResponse.OPERATION;
51 public static final String VSERVER_VSERVER_NAME = "vserver.vserver-name";
52 public static final String RESOURCE_LINK = "resource-link";
53 public static final String RESULT_DATA = "result-data";
55 private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.AAI_VSERVER_LINK);
58 * Constructs the object.
60 * @param params operation parameters
61 * @param config configuration for this operation
63 public AaiCustomQueryOperation(ControlLoopOperationParams params, HttpConfig config) {
64 super(params, config, String.class, PROPERTY_NAMES);
68 public void generateSubRequestId(int attempt) {
69 setSubRequestId(String.valueOf(attempt));
73 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
74 outcome.setSubRequestId(String.valueOf(attempt));
76 final Map<String, String> request = makeRequest();
77 Map<String, Object> headers = makeHeaders();
79 StringBuilder str = new StringBuilder(getClient().getBaseUrl());
81 String path = getPath();
82 WebTarget web = getClient().getWebTarget().path(path);
85 web = addQuery(web, str, "?", "format", "resource");
87 Builder webldr = web.request();
88 for (Entry<String, Object> header : headers.entrySet()) {
89 webldr.header(header.getKey(), header.getValue());
92 String url = str.toString();
94 String strRequest = prettyPrint(request);
95 logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
97 Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
99 return handleResponse(outcome, url, callback -> webldr.async().put(entity, callback));
102 private WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) {
103 str.append(separator);
108 return web.queryParam(name, value);
112 * Constructs the custom query using the previously retrieved tenant data.
114 private Map<String, String> makeRequest() {
115 return Map.of("start", getVserverLink(), "query", "query/closed-loop");
119 * Gets the vserver link, first checking the properties, and then the tenant data.
121 * @return the vserver link
123 protected String getVserverLink() {
124 return getRequiredProperty(OperationProperties.AAI_VSERVER_LINK, "vserver link");
128 protected Map<String, Object> makeHeaders() {
129 return AaiUtil.makeHeaders(params);
133 public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
136 super.setOutcome(outcome, result, rawResponse, response);
138 if (response != null) {
139 outcome.setResponse(new AaiCqResponse(response));