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;
24 import java.util.Map.Entry;
26 import java.util.concurrent.CompletableFuture;
27 import javax.ws.rs.client.Invocation.Builder;
28 import javax.ws.rs.client.WebTarget;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31 import org.onap.policy.aai.AaiConstants;
32 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
33 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
34 import org.onap.policy.common.utils.coder.StandardCoderObject;
35 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
36 import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
37 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
38 import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Superclass of A&AI operators that use "get" to perform their request and store their
44 * response within the context as a {@link StandardCoderObject}. The property name under
45 * which they are stored is ${actor}.${operation}.${targetEntity}.
47 public class AaiGetOperation extends HttpOperation<StandardCoderObject> {
48 private static final Logger logger = LoggerFactory.getLogger(AaiGetOperation.class);
50 public static final int DEFAULT_RETRY = 3;
53 public static final String TENANT = "Tenant";
56 private static final String TENANT_KEY_PREFIX = AaiConstants.CONTEXT_PREFIX + TENANT + ".";
59 * Operation names supported by this operator.
61 public static final Set<String> OPERATIONS = Set.of(TENANT);
65 * Responses that are retrieved from A&AI are placed in the operation context under
66 * the name "${propertyPrefix}.${targetEntity}".
68 private final String propertyPrefix;
71 * Constructs the object.
73 * @param params operation parameters
74 * @param config configuration for this operation
76 public AaiGetOperation(ControlLoopOperationParams params, HttpConfig config) {
77 super(params, config, StandardCoderObject.class);
78 this.propertyPrefix = getFullName() + ".";
82 * Gets the "context key" for the tenant query response associated with the given
85 * @param targetEntity target entity
86 * @return the "context key" for the response associated with the given target
88 public static String getTenantKey(String targetEntity) {
89 return (TENANT_KEY_PREFIX + targetEntity);
93 protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
95 Map<String, Object> headers = makeHeaders();
97 headers.put("Accept", MediaType.APPLICATION_JSON);
99 StringBuilder str = new StringBuilder(getClient().getBaseUrl());
101 String path = getPath();
102 WebTarget web = getClient().getWebTarget().path(path);
105 web = addQuery(web, str, "?", "search-node-type", "vserver");
106 web = addQuery(web, str, "&", "filter", "vserver-name:EQUALS:" + params.getTargetEntity());
108 Builder webldr = web.request();
109 for (Entry<String, Object> header : headers.entrySet()) {
110 webldr.header(header.getKey(), header.getValue());
113 String url = str.toString();
115 logMessage(EventType.OUT, CommInfrastructure.REST, url, null);
117 return handleResponse(outcome, url, callback -> webldr.async().get(callback));
120 private WebTarget addQuery(WebTarget web, StringBuilder str, String separator, String name, String value) {
121 str.append(separator);
126 return web.queryParam(name, value);
130 protected Map<String, Object> makeHeaders() {
131 return AaiUtil.makeHeaders(params);
135 * Injects the response into the context.
138 protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
139 Response rawResponse, StandardCoderObject response) {
140 String entity = params.getTargetEntity();
142 logger.info("{}: caching response of {} for {}", getFullName(), entity, params.getRequestId());
144 params.getContext().setProperty(propertyPrefix + entity, response);
146 return super.postProcessResponse(outcome, url, rawResponse, response);
150 * Provides a default retry value, if none specified.
153 protected int getRetry(Integer retry) {
154 return (retry == null ? DEFAULT_RETRY : retry);