import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
import org.onap.policy.aai.AaiConstants;
import org.onap.policy.aai.AaiCqResponse;
import org.onap.policy.common.utils.coder.StandardCoderObject;
import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
+import org.onap.policy.controlloop.actorserviceprovider.OperationResult;
import org.onap.policy.controlloop.actorserviceprovider.impl.HttpOperation;
import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
// TODO make this configurable
private static final String PREFIX = "/aai/v16";
- @Getter
- private final String vserver;
-
/**
* Constructs the object.
*
*/
public AaiCustomQueryOperation(ControlLoopOperationParams params, HttpConfig config) {
super(params, config, String.class, PROPERTY_NAMES);
+ }
- this.vserver = params.getContext().getEnrichment().get(VSERVER_VSERVER_NAME);
- if (StringUtils.isBlank(this.vserver)) {
+ /**
+ * Gets the vserver name from the enrichment data.
+ *
+ * @return the vserver name
+ */
+ protected String getVserver() {
+ String vserver = this.params.getContext().getEnrichment().get(VSERVER_VSERVER_NAME);
+ if (StringUtils.isBlank(vserver)) {
throw new IllegalArgumentException("missing " + VSERVER_VSERVER_NAME + " in enrichment data");
}
+
+ return vserver;
}
/**
*/
@Override
protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
+ if (params.isPreprocessed()) {
+ return null;
+ }
+
+ String vserver = getVserver();
ControlLoopOperationParams tenantParams =
params.toBuilder().actor(AaiConstants.ACTOR_NAME).operation(AaiGetTenantOperation.NAME)
.targetEntity(vserver).payload(null).retry(null).timeoutSec(null).build();
* Constructs the custom query using the previously retrieved tenant data.
*/
private Map<String, String> makeRequest() {
+ return Map.of("start", getVserverLink(), "query", "query/closed-loop");
+ }
+
+ /**
+ * Gets the vserver link, first checking the properties, and then the tenant data.
+ *
+ * @return the vserver link
+ */
+ protected String getVserverLink() {
+ String resourceLink = getProperty(OperationProperties.AAI_VSERVER_LINK);
+ if (resourceLink != null) {
+ return resourceLink;
+ }
+
+ String vserver = getVserver();
StandardCoderObject tenant = params.getContext().getProperty(AaiGetTenantOperation.getKey(vserver));
+ if (tenant == null) {
+ throw new IllegalStateException("cannot perform custom query - cannot determine resource-link");
+ }
- String resourceLink = tenant.getString(RESULT_DATA, 0, RESOURCE_LINK);
+ resourceLink = tenant.getString(RESULT_DATA, 0, RESOURCE_LINK);
if (resourceLink == null) {
throw new IllegalArgumentException("cannot perform custom query - no resource-link");
}
resourceLink = resourceLink.replace(PREFIX, "");
-
- return Map.of("start", resourceLink, "query", "query/closed-loop");
+ return resourceLink;
}
@Override
return AaiUtil.makeHeaders(params);
}
+ @Override
+ public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
+ String response) {
+
+ super.setOutcome(outcome, result, rawResponse, response);
+
+ if (response != null) {
+ outcome.setResponse(new AaiCqResponse(response));
+ }
+
+ return outcome;
+ }
+
/**
* Injects the response into the context.
*/
protected CompletableFuture<OperationOutcome> postProcessResponse(OperationOutcome outcome, String url,
Response rawResponse, String response) {
- logger.info("{}: caching response for {}", getFullName(), params.getRequestId());
- params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, new AaiCqResponse(response));
+ if (params.getContext() != null) {
+ logger.info("{}: caching response for {}", getFullName(), params.getRequestId());
+ params.getContext().setProperty(AaiCqResponse.CONTEXT_KEY, new AaiCqResponse(response));
+ }
return super.postProcessResponse(outcome, url, rawResponse, response);
}