Skip preprocessor step in Actors
[policy/models.git] / models-interactions / model-actors / actor.so / src / main / java / org / onap / policy / controlloop / actor / so / VfModuleCreate.java
index c17d252..4bae1e8 100644 (file)
 
 package org.onap.policy.controlloop.actor.so;
 
+import java.util.List;
+import java.util.Map;
 import java.util.concurrent.CompletableFuture;
 import javax.ws.rs.client.Entity;
 import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
 import org.apache.commons.lang3.tuple.Pair;
 import org.onap.aai.domain.yang.CloudRegion;
 import org.onap.aai.domain.yang.GenericVnf;
@@ -32,11 +35,10 @@ import org.onap.policy.aai.AaiConstants;
 import org.onap.policy.aai.AaiCqResponse;
 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 import org.onap.policy.common.endpoints.utils.NetLoggerUtil.EventType;
-import org.onap.policy.controlloop.actor.aai.AaiCustomQueryOperation;
 import org.onap.policy.controlloop.actorserviceprovider.OperationOutcome;
+import org.onap.policy.controlloop.actorserviceprovider.OperationProperties;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
-import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpConfig;
-import org.onap.policy.so.SoCloudConfiguration;
+import org.onap.policy.controlloop.actorserviceprovider.parameters.HttpPollingConfig;
 import org.onap.policy.so.SoModelInfo;
 import org.onap.policy.so.SoOperationType;
 import org.onap.policy.so.SoRelatedInstance;
@@ -44,45 +46,108 @@ import org.onap.policy.so.SoRelatedInstanceListElement;
 import org.onap.policy.so.SoRequest;
 import org.onap.policy.so.SoRequestDetails;
 import org.onap.policy.so.SoRequestParameters;
+import org.onap.policy.so.SoResponse;
 
+/**
+ * Operation to create a VF Module. This gets the VF count from the A&AI Custom Query
+ * response and stores it in the context. It also passes the count+1 to the guard. Once
+ * the "create" completes successfully, it bumps the VF count that's stored in the
+ * context.
+ */
 public class VfModuleCreate extends SoOperation {
     public static final String NAME = "VF Module Create";
 
-    public VfModuleCreate(ControlLoopOperationParams params, HttpConfig config) {
-        super(params, config);
+    private static final String PATH_PREFIX = "/";
+
+    // @formatter:off
+    private static final List<String> PROPERTY_NAMES = List.of(
+                            OperationProperties.AAI_MODEL_SERVICE,
+                            OperationProperties.AAI_MODEL_VNF,
+                            OperationProperties.AAI_MODEL_CLOUD_REGION,
+                            OperationProperties.AAI_MODEL_TENANT,
+                            OperationProperties.DATA_VF_COUNT);
+    // @formatter:off
+
+    /**
+     * Constructs the object.
+     *
+     * @param params operation parameters
+     * @param config configuration for this operation
+     */
+    public VfModuleCreate(ControlLoopOperationParams params, HttpPollingConfig config) {
+        super(params, config, PROPERTY_NAMES);
+
+        // ensure we have the necessary parameters
+        validateTarget();
     }
 
     /**
-     * Ensures that A&AI customer query has been performed, and then runs the guard query.
+     * Ensures that A&AI custom query has been performed, and then runs the guard.
      */
     @Override
     @SuppressWarnings("unchecked")
     protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
+        if (params.isPreprocessed()) {
+            return null;
+        }
+
+        // need the VF count
         ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME)
-                        .operation(AaiCustomQueryOperation.NAME).payload(null).retry(null).timeoutSec(null).build();
+                        .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build();
+
+        // run Custom Query, extract the VF count, and then run the Guard
+
+        // @formatter:off
+        return sequence(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams),
+                        this::obtainVfCount, this::startGuardAsync);
+        // @formatter:on
+    }
+
+    @Override
+    protected Map<String, Object> makeGuardPayload() {
+        Map<String, Object> payload = super.makeGuardPayload();
+
+        // run guard with the proposed vf count
+        payload.put(PAYLOAD_KEY_VF_COUNT, getVfCount() + 1);
 
-        // run Custom Query and Guard, in parallel
-        return allOf(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), this::startGuardAsync);
+        return payload;
     }
 
     @Override
     protected CompletableFuture<OperationOutcome> startOperationAsync(int attempt, OperationOutcome outcome) {
 
         // starting a whole new attempt - reset the count
-        resetGetCount();
+        resetPollCount();
 
         Pair<String, SoRequest> pair = makeRequest();
-        String path = pair.getLeft();
+        String path = getPath() + pair.getLeft();
         SoRequest request = pair.getRight();
 
-        Entity<SoRequest> entity = Entity.entity(request, MediaType.APPLICATION_JSON);
         String url = getClient().getBaseUrl() + path;
 
-        logMessage(EventType.OUT, CommInfrastructure.REST, url, request);
+        String strRequest = prettyPrint(request);
+        logMessage(EventType.OUT, CommInfrastructure.REST, url, strRequest);
 
-        // TODO should this use "path" or the full "url"?
+        Entity<String> entity = Entity.entity(strRequest, MediaType.APPLICATION_JSON);
 
-        return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, null));
+        Map<String, Object> headers = createSimpleHeaders();
+
+        return handleResponse(outcome, url, callback -> getClient().post(callback, path, entity, headers));
+    }
+
+    /**
+     * Increments the VF count that's stored in the context, if the request was
+     * successful.
+     */
+    @Override
+    protected Status detmStatus(Response rawResponse, SoResponse response) {
+        Status status = super.detmStatus(rawResponse, response);
+
+        if (status == Status.SUCCESS) {
+            setVfCount(getVfCount() + 1);
+        }
+
+        return status;
     }
 
     /**
@@ -159,29 +224,15 @@ public class VfModuleCreate extends SoOperation {
         request.getRequestDetails().getRelatedInstanceList().add(relatedInstanceListElement2);
 
         // Request Parameters
-        request.getRequestDetails().setRequestParameters(buildRequestParameters());
+        buildRequestParameters().ifPresent(request.getRequestDetails()::setRequestParameters);
 
         // Configuration Parameters
-        request.getRequestDetails().setConfigurationParameters(buildConfigurationParameters());
+        buildConfigurationParameters().ifPresent(request.getRequestDetails()::setConfigurationParameters);
 
         // compute the path
-        String path = "/serviceInstantiation/v7/serviceInstances/" + vnfServiceItem.getServiceInstanceId() + "/vnfs/"
-                        + vnfItem.getVnfId() + "/vfModules/scaleOut";
+        String path = PATH_PREFIX + vnfServiceItem.getServiceInstanceId() + "/vnfs/" + vnfItem.getVnfId()
+                        + "/vfModules/scaleOut";
 
         return Pair.of(path, request);
     }
-
-    /**
-     * Construct cloudConfiguration for the SO requestDetails. Overridden for custom
-     * query.
-     *
-     * @param tenantItem tenant item from A&AI named-query response
-     * @return SO cloud configuration
-     */
-    private SoCloudConfiguration constructCloudConfigurationCq(Tenant tenantItem, CloudRegion cloudRegionItem) {
-        SoCloudConfiguration cloudConfiguration = new SoCloudConfiguration();
-        cloudConfiguration.setTenantId(tenantItem.getTenantId());
-        cloudConfiguration.setLcpCloudRegionId(cloudRegionItem.getCloudRegionId());
-        return cloudConfiguration;
-    }
 }