Change payload to Map<String,Object> so it's more versatile
[policy/models.git] / models-interactions / model-actors / actor.so / src / main / java / org / onap / policy / controlloop / actor / so / SoOperation.java
index d8d960e..53fb973 100644 (file)
@@ -96,6 +96,23 @@ public abstract class SoOperation extends HttpOperation<SoResponse> {
         getCount = 0;
     }
 
+    /**
+     * Validates that the parameters contain the required target information to extract
+     * the VF count from the custom query.
+     */
+    protected void validateTarget() {
+        verifyNotNull("Target information", params.getTarget());
+        verifyNotNull("model-customization-id", params.getTarget().getModelCustomizationId());
+        verifyNotNull("model-invariant-id", params.getTarget().getModelInvariantId());
+        verifyNotNull("model-version-id", params.getTarget().getModelVersionId());
+    }
+
+    private void verifyNotNull(String type, Object value) {
+        if (value == null) {
+            throw new IllegalArgumentException("missing " + type + " for guard payload");
+        }
+    }
+
     /**
      * Starts the GUARD.
      */
@@ -104,6 +121,24 @@ public abstract class SoOperation extends HttpOperation<SoResponse> {
         return startGuardAsync();
     }
 
+    /**
+     * Stores the VF count and then runs the guard.
+     *
+     * @return a future to cancel or await the guard response
+     */
+    protected CompletableFuture<OperationOutcome> storeVfCountRunGuard() {
+        String custId = params.getTarget().getModelCustomizationId();
+        String invId = params.getTarget().getModelInvariantId();
+        String verId = params.getTarget().getModelVersionId();
+
+        AaiCqResponse cq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY);
+        int vfcount = cq.getVfModuleCount(custId, invId, verId);
+
+        params.getContext().setProperty(SoConstants.CONTEXT_KEY_VF_COUNT, vfcount);
+
+        return startGuardAsync();
+    }
+
     /**
      * If the response does not indicate that the request has been completed, then sleep a
      * bit and issue a "get".
@@ -116,6 +151,7 @@ public abstract class SoOperation extends HttpOperation<SoResponse> {
         if (rawResponse.getStatus() == 200) {
             String requestState = getRequestState(response);
             if (COMPLETE.equalsIgnoreCase(requestState)) {
+                successfulCompletion();
                 return CompletableFuture
                                 .completedFuture(setOutcome(outcome, PolicyResult.SUCCESS, rawResponse, response));
             }
@@ -146,6 +182,13 @@ public abstract class SoOperation extends HttpOperation<SoResponse> {
         return sleep(getWaitMsGet(), TimeUnit.MILLISECONDS).thenComposeAsync(doGet);
     }
 
+    /**
+     * Invoked when a request completes successfully.
+     */
+    protected void successfulCompletion() {
+        // do nothing
+    }
+
     /**
      * Issues a "get" request to see if the original request is complete yet.
      *
@@ -251,13 +294,13 @@ public abstract class SoOperation extends HttpOperation<SoResponse> {
             return null;
         }
 
-        String json = params.getPayload().get(REQ_PARAM_NM);
-        if (json == null) {
+        Object data = params.getPayload().get(REQ_PARAM_NM);
+        if (data == null) {
             return null;
         }
 
         try {
-            return coder.decode(json, SoRequestParameters.class);
+            return coder.decode(data.toString(), SoRequestParameters.class);
         } catch (CoderException e) {
             throw new IllegalArgumentException("invalid payload value: " + REQ_PARAM_NM);
         }
@@ -271,14 +314,14 @@ public abstract class SoOperation extends HttpOperation<SoResponse> {
             return null;
         }
 
-        String json = params.getPayload().get(CONFIG_PARAM_NM);
-        if (json == null) {
+        Object data = params.getPayload().get(CONFIG_PARAM_NM);
+        if (data == null) {
             return null;
         }
 
         try {
             @SuppressWarnings("unchecked")
-            List<Map<String, String>> result = coder.decode(json, ArrayList.class);
+            List<Map<String, String>> result = coder.decode(data.toString(), ArrayList.class);
             return result;
         } catch (CoderException | RuntimeException e) {
             throw new IllegalArgumentException("invalid payload value: " + CONFIG_PARAM_NM);