* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2020 Wipro Limited.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.Response;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
-import java.util.concurrent.CompletableFuture;
-import java.util.function.Function;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
import org.onap.aai.domain.yang.CloudRegion;
import org.onap.aai.domain.yang.GenericVnf;
import org.onap.aai.domain.yang.ModelVer;
import org.onap.aai.domain.yang.ServiceInstance;
import org.onap.aai.domain.yang.Tenant;
-import org.onap.policy.aai.AaiConstants;
-import org.onap.policy.aai.AaiCqResponse;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
import org.onap.policy.common.utils.coder.Coder;
import org.onap.policy.common.utils.coder.CoderException;
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.HttpPollingConfig;
-import org.onap.policy.controlloop.policy.PolicyResult;
-import org.onap.policy.controlloop.policy.Target;
import org.onap.policy.so.SoCloudConfiguration;
import org.onap.policy.so.SoModelInfo;
import org.onap.policy.so.SoRequest;
public abstract class SoOperation extends HttpOperation<SoResponse> {
private static final Coder coder = new SoCoder();
- public static final String PAYLOAD_KEY_VF_COUNT = "vfCount";
public static final String FAILED = "FAILED";
public static final String COMPLETE = "COMPLETE";
public static final int SO_RESPONSE_CODE = 999;
public static final String REQ_PARAM_NM = "requestParameters";
public static final String CONFIG_PARAM_NM = "configurationParameters";
- // values extracted from the parameter Target
+ /* Values extracted from the parameter Target. These fields are required by any
+ subclasses that make use of prepareSoModelInfo().
+ */
private final String modelCustomizationId;
private final String modelInvariantId;
private final String modelVersionId;
+ private final String modelName;
+ private final String modelVersion;
- private final String vfCountKey;
/**
* @param config configuration for this operation
* @param propertyNames names of properties required by this operation
*/
- public SoOperation(ControlLoopOperationParams params, HttpPollingConfig config, List<String> propertyNames) {
+ protected SoOperation(ControlLoopOperationParams params, HttpPollingConfig config, List<String> propertyNames) {
super(params, config, SoResponse.class, propertyNames);
- setUsePolling();
+ this.modelCustomizationId = null;
+ this.modelInvariantId = null;
+ this.modelVersionId = null;
+ this.modelVersion = null;
+ this.modelName = null;
- verifyNotNull("Target information", params.getTarget());
+ verifyNotNull("Target information", params.getTargetType());
+ }
- this.modelCustomizationId = params.getTarget().getModelCustomizationId();
- this.modelInvariantId = params.getTarget().getModelInvariantId();
- this.modelVersionId = params.getTarget().getModelVersionId();
+ /**
+ * Constructs the object.
+ *
+ * @param params operation parameters
+ * @param config configuration for this operation
+ * @param propertyNames names of properties required by this operation
+ * @param targetEntityIds Target Entity information
+ */
+ protected SoOperation(ControlLoopOperationParams params, HttpPollingConfig config, List<String> propertyNames,
+ Map<String, String> targetEntityIds) {
+ super(params, config, SoResponse.class, propertyNames);
+
+ verifyNotNull("Target entity Ids information", targetEntityIds);
- vfCountKey = SoConstants.VF_COUNT_PREFIX + "[" + modelCustomizationId + "][" + modelInvariantId + "]["
- + modelVersionId + "]";
+ this.modelCustomizationId = targetEntityIds
+ .get(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID);
+ this.modelInvariantId = targetEntityIds
+ .get(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_INVARIANT_ID);
+ this.modelVersionId = targetEntityIds
+ .get(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION_ID);
+ this.modelVersion = targetEntityIds
+ .get(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION);
+ this.modelName = targetEntityIds
+ .get(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_NAME);
+
+ verifyNotNull("Target information", params.getTargetType());
}
@Override
}
/**
- * Validates that the parameters contain the required target information to extract
- * the VF count from the custom query.
+ * Validates that the parameters contain the required target information to construct
+ * the request.
*/
protected void validateTarget() {
- verifyNotNull("modelCustomizationId", modelCustomizationId);
- verifyNotNull("modelInvariantId", modelInvariantId);
- verifyNotNull("modelVersionId", modelVersionId);
+ verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_CUSTOMIZATION_ID, modelCustomizationId);
+ verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_INVARIANT_ID, modelInvariantId);
+ verifyNotNull(ControlLoopOperationParams.PARAMS_ENTITY_MODEL_VERSION_ID, modelVersionId);
}
private void verifyNotNull(String type, Object value) {
if (value == null) {
- throw new IllegalArgumentException("missing " + type + " for guard payload");
- }
- }
-
- /**
- * Starts the GUARD.
- */
- @Override
- protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
- return startGuardAsync();
- }
-
- /**
- * Gets the VF Count.
- *
- * @return a future to cancel or await the VF Count
- */
- @SuppressWarnings("unchecked")
- protected CompletableFuture<OperationOutcome> obtainVfCount() {
- if (params.getContext().contains(vfCountKey)) {
- // already have the VF count
- return null;
- }
-
- // need custom query from which to extract the VF count
- ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
- .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
-
- // run Custom Query and then extract the VF count
- return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), this::storeVfCount);
- }
-
- /**
- * Stores the VF count.
- *
- * @return {@code null}
- */
- private CompletableFuture<OperationOutcome> storeVfCount() {
- if (!params.getContext().contains(vfCountKey)) {
- AaiCqResponse cq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
- int vfcount = cq.getVfModuleCount(modelCustomizationId, modelInvariantId, modelVersionId);
-
- params.getContext().setProperty(vfCountKey, vfcount);
+ throw new IllegalArgumentException("missing Target." + type);
}
-
- return null;
}
protected int getVfCount() {
- if (containsProperty(OperationProperties.DATA_VF_COUNT)) {
- return getProperty(OperationProperties.DATA_VF_COUNT);
- }
-
- return params.getContext().getProperty(vfCountKey);
+ return getRequiredProperty(OperationProperties.DATA_VF_COUNT, "VF Count");
}
protected void setVfCount(int vfCount) {
- if (containsProperty(OperationProperties.DATA_VF_COUNT)) {
- setProperty(OperationProperties.DATA_VF_COUNT, vfCount);
- return;
- }
-
- params.getContext().setProperty(vfCountKey, vfCount);
+ setProperty(OperationProperties.DATA_VF_COUNT, vfCount);
}
@Override
* Prepends the message with the http status code.
*/
@Override
- public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, Response rawResponse,
+ public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, Response rawResponse,
SoResponse response) {
// set default result and message
setOutcome(outcome, result);
- int code = (result == PolicyResult.FAILURE_TIMEOUT ? SO_RESPONSE_CODE : rawResponse.getStatus());
+ int code = (result == OperationResult.FAILURE_TIMEOUT ? SO_RESPONSE_CODE : rawResponse.getStatus());
outcome.setResponse(response);
outcome.setMessage(code + " " + outcome.getMessage());
}
protected SoModelInfo prepareSoModelInfo() {
- Target target = params.getTarget();
- if (target == null) {
- throw new IllegalArgumentException("missing Target");
- }
-
- if (target.getModelCustomizationId() == null || target.getModelInvariantId() == null
- || target.getModelName() == null || target.getModelVersion() == null
- || target.getModelVersionId() == null) {
- throw new IllegalArgumentException("missing VF Module model");
- }
-
- SoModelInfo soModelInfo = new SoModelInfo();
- soModelInfo.setModelCustomizationId(target.getModelCustomizationId());
- soModelInfo.setModelInvariantId(target.getModelInvariantId());
- soModelInfo.setModelName(target.getModelName());
- soModelInfo.setModelVersion(target.getModelVersion());
- soModelInfo.setModelVersionId(target.getModelVersionId());
+ var soModelInfo = new SoModelInfo();
+ soModelInfo.setModelCustomizationId(modelCustomizationId);
+ soModelInfo.setModelInvariantId(modelInvariantId);
+ soModelInfo.setModelName(modelName);
+ soModelInfo.setModelVersion(modelVersion);
+ soModelInfo.setModelVersionId(modelVersionId);
soModelInfo.setModelType("vfModule");
return soModelInfo;
}
* @return SO request information
*/
protected SoRequestInfo constructRequestInfo() {
- SoRequestInfo soRequestInfo = new SoRequestInfo();
+ var soRequestInfo = new SoRequestInfo();
soRequestInfo.setSource("POLICY");
soRequestInfo.setSuppressRollback(false);
soRequestInfo.setRequestorId("policy");
}
/**
- * Construct cloudConfiguration for the SO requestDetails. Overridden for custom
- * query.
+ * Construct cloudConfiguration for the SO requestDetails.
*
* @param tenantItem tenant item from A&AI named-query response
* @return SO cloud configuration
*/
- protected SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
- SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
- cloudConfiguration.setTenantId(tenantItem.getTenantId());
- cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
+ protected SoCloudConfiguration constructCloudConfiguration(Tenant tenantItem, CloudRegion cloudRegionItem) {
+ var cloudConfiguration = new SoCloudConfiguration();
+ cloudConfiguration.setTenantId(getRequiredText("tenant ID", tenantItem.getTenantId()));
+ cloudConfiguration.setLcpCloudRegionId(getRequiredText("cloud region ID", cloudRegionItem.getCloudRegionId()));
return cloudConfiguration;
}
+ /**
+ * Verifies that a value is not {@code null}.
+ *
+ * @param name value name
+ * @param value value to check
+ * @return the value
+ */
+ protected String getRequiredText(String name, String value) {
+ if (value == null) {
+ throw new IllegalArgumentException("missing " + name);
+ }
+
+ return value;
+ }
+
/**
* Create simple HTTP headers for unauthenticated requests to SO.
*
return headers;
}
- /**
- * Gets an item from a property. If the property is not found, then it invokes the
- * given function to retrieve it from the custom query data. If that fails as well,
- * then an exception is thrown.
- *
- * @param propName property name
- * @param getter method to extract the value from the custom query data
- * @param errmsg error message to include in any exception
- * @return the retrieved item
- */
- protected <T> T getItem(String propName, Function<AaiCqResponse, T> getter, String errmsg) {
- if (containsProperty(propName)) {
- return getProperty(propName);
- }
-
- final AaiCqResponse aaiCqResponse = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
- T item = getter.apply(aaiCqResponse);
- if (item == null) {
- throw new IllegalArgumentException(errmsg);
- }
-
- return item;
- }
-
/*
* These methods extract data from the Custom Query and throw an
* IllegalArgumentException if the desired data item is not found.
*/
- protected GenericVnf getVnfItem(SoModelInfo soModelInfo) {
- // @formatter:off
- return getItem(OperationProperties.AAI_VNF,
- cq -> cq.getGenericVnfByVfModuleModelInvariantId(soModelInfo.getModelInvariantId()),
- "missing generic VNF");
- // @formatter:on
+ protected GenericVnf getVnfItem() {
+ return getRequiredProperty(OperationProperties.AAI_VNF, "generic VNF");
}
protected ServiceInstance getServiceInstance() {
- return getItem(OperationProperties.AAI_SERVICE, AaiCqResponse::getServiceInstance, "missing VNF Service Item");
+ return getRequiredProperty(OperationProperties.AAI_SERVICE, "VNF Service Item");
}
protected Tenant getDefaultTenant() {
- // @formatter:off
- return getItem(OperationProperties.AAI_DEFAULT_TENANT,
- AaiCqResponse::getDefaultTenant,
- "missing Default Tenant Item");
- // @formatter:on
+ return getRequiredProperty(OperationProperties.AAI_DEFAULT_TENANT, "Default Tenant Item");
}
protected CloudRegion getDefaultCloudRegion() {
- // @formatter:off
- return getItem(OperationProperties.AAI_DEFAULT_CLOUD_REGION,
- AaiCqResponse::getDefaultCloudRegion,
- "missing Default Cloud Region");
- // @formatter:on
+ return getRequiredProperty(OperationProperties.AAI_DEFAULT_CLOUD_REGION, "Default Cloud Region");
}
- protected ModelVer getVnfModel(GenericVnf vnfItem) {
- // @formatter:off
- return getItem(OperationProperties.AAI_VNF_MODEL,
- cq -> cq.getModelVerByVersionId(vnfItem.getModelVersionId()),
- "missing generic VNF Model");
- // @formatter:on
+ protected ModelVer getVnfModel() {
+ return getRequiredProperty(OperationProperties.AAI_VNF_MODEL, "generic VNF Model");
}
- protected ModelVer getServiceModel(ServiceInstance vnfServiceItem) {
- // @formatter:off
- return getItem(OperationProperties.AAI_SERVICE_MODEL,
- cq -> cq.getModelVerByVersionId(vnfServiceItem.getModelVersionId()),
- "missing Service Model");
- // @formatter:on
+ protected ModelVer getServiceModel() {
+ return getRequiredProperty(OperationProperties.AAI_SERVICE_MODEL, "Service Model");
}
// these may be overridden by junit tests