Remove dmaap from models
[policy/models.git] / models-interactions / model-actors / actor.appclcm / src / main / java / org / onap / policy / controlloop / actor / appclcm / AppcLcmOperation.java
index 7496227..1fd6f2d 100644 (file)
@@ -2,7 +2,8 @@
  * ============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) 2023-2024 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,35 +23,34 @@ package org.onap.policy.controlloop.actor.appclcm;
 
 import java.util.List;
 import java.util.Map;
-import java.util.UUID;
-import java.util.concurrent.CompletableFuture;
-import org.apache.commons.lang3.StringUtils;
 import org.onap.policy.appclcm.AppcLcmBody;
 import org.onap.policy.appclcm.AppcLcmCommonHeader;
-import org.onap.policy.appclcm.AppcLcmDmaapWrapper;
 import org.onap.policy.appclcm.AppcLcmInput;
+import org.onap.policy.appclcm.AppcLcmMessageWrapper;
 import org.onap.policy.appclcm.AppcLcmOutput;
 import org.onap.policy.appclcm.AppcLcmResponseCode;
 import org.onap.policy.appclcm.AppcLcmResponseStatus;
 import org.onap.policy.common.utils.coder.CoderException;
-import org.onap.policy.controlloop.VirtualControlLoopEvent;
 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.BidirectionalTopicOperation;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.BidirectionalTopicConfig;
 import org.onap.policy.controlloop.actorserviceprovider.parameters.ControlLoopOperationParams;
 import org.onap.policy.controlloop.actorserviceprovider.topic.SelectorKey;
-import org.onap.policy.controlloop.policy.PolicyResult;
 
-public class AppcLcmOperation extends BidirectionalTopicOperation<AppcLcmDmaapWrapper, AppcLcmDmaapWrapper> {
+public class AppcLcmOperation extends BidirectionalTopicOperation<AppcLcmMessageWrapper, AppcLcmMessageWrapper> {
 
     private static final String MISSING_STATUS = "APPC-LCM response is missing the response status";
     public static final String VNF_ID_KEY = "vnf-id";
 
+    private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.AAI_TARGET_ENTITY);
+
     /**
      * Keys used to match the response with the request listener. The sub request ID is a
      * UUID, so it can be used to uniquely identify the response.
      * <p/>
-     * Note: if these change, then {@link #getExpectedKeyValues(int, AppcLcmDmaapWrapper)}
+     * Note: if these change, then {@link #getExpectedKeyValues(int, AppcLcmMessageWrapper)}
      * must be updated accordingly.
      */
     public static final List<SelectorKey> SELECTOR_KEYS =
@@ -63,48 +63,35 @@ public class AppcLcmOperation extends BidirectionalTopicOperation<AppcLcmDmaapWr
      * @param config configuration for this operation
      */
     public AppcLcmOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
-        super(params, config, AppcLcmDmaapWrapper.class);
-
-        if (StringUtils.isBlank(params.getTargetEntity())) {
-            throw new IllegalArgumentException("missing targetEntity");
-        }
-    }
-
-    /**
-     * Ensures that A&AI customer query has been performed, and then runs the guard query.
-     * Starts the GUARD using startGuardAsync.
-     */
-    @Override
-    protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
-        return startGuardAsync();
+        super(params, config, AppcLcmMessageWrapper.class, PROPERTY_NAMES);
     }
 
     @Override
-    protected AppcLcmDmaapWrapper makeRequest(int attempt) {
-        VirtualControlLoopEvent onset = params.getContext().getEvent();
-        String subRequestId = UUID.randomUUID().toString();
+    protected AppcLcmMessageWrapper makeRequest(int attempt) {
+        String subRequestId = getSubRequestId();
 
-        AppcLcmCommonHeader header = new AppcLcmCommonHeader();
-        header.setOriginatorId(onset.getRequestId().toString());
-        header.setRequestId(onset.getRequestId());
+        var header = new AppcLcmCommonHeader();
+        header.setOriginatorId(params.getRequestId().toString());
+        header.setRequestId(params.getRequestId());
         header.setSubRequestId(subRequestId);
 
-        AppcLcmInput inputRequest = new AppcLcmInput();
+        var inputRequest = new AppcLcmInput();
         inputRequest.setCommonHeader(header);
 
-        AppcLcmRecipeFormatter recipeFormatter = new AppcLcmRecipeFormatter(getName());
+        var recipeFormatter = new AppcLcmRecipeFormatter(getName());
         inputRequest.setAction(recipeFormatter.getBodyRecipe());
 
         /*
          * Action Identifiers are required for APPC LCM requests. For R1, the recipes
          * supported by Policy only require a vnf-id.
          */
-        inputRequest.setActionIdentifiers(Map.of(VNF_ID_KEY, params.getTargetEntity()));
+        String target = getRequiredProperty(OperationProperties.AAI_TARGET_ENTITY, "target entity");
+        inputRequest.setActionIdentifiers(Map.of(VNF_ID_KEY, target));
 
         /*
          * For R1, the payloads will not be required for the Restart, Rebuild, or Migrate
          * recipes. APPC will populate the payload based on A&AI look up of the vnd-id
-         * provided in the action identifiers. The payload is set when converPayload() is
+         * provided in the action identifiers. The payload is set when convertPayload() is
          * called.
          */
         if (operationSupportsPayload()) {
@@ -113,19 +100,19 @@ public class AppcLcmOperation extends BidirectionalTopicOperation<AppcLcmDmaapWr
             inputRequest.setPayload(null);
         }
 
-        AppcLcmBody body = new AppcLcmBody();
+        var body = new AppcLcmBody();
         body.setInput(inputRequest);
 
-        AppcLcmDmaapWrapper dmaapRequest = new AppcLcmDmaapWrapper();
-        dmaapRequest.setBody(body);
-        dmaapRequest.setVersion("2.0");
-        dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + subRequestId);
-        dmaapRequest.setRpcName(recipeFormatter.getUrlRecipe());
-        dmaapRequest.setType("request");
+        var messageRequest = new AppcLcmMessageWrapper();
+        messageRequest.setBody(body);
+        messageRequest.setVersion("2.0");
+        messageRequest.setCorrelationId(params.getRequestId() + "-" + subRequestId);
+        messageRequest.setRpcName(recipeFormatter.getUrlRecipe());
+        messageRequest.setType("request");
 
         body.setInput(inputRequest);
-        dmaapRequest.setBody(body);
-        return dmaapRequest;
+        messageRequest.setBody(body);
+        return messageRequest;
     }
 
     /**
@@ -133,11 +120,11 @@ public class AppcLcmOperation extends BidirectionalTopicOperation<AppcLcmDmaapWr
      * decoded into an object.
      *
      * @param source source from which to get the values
-     * @param map where to place the decoded values
+     * @param request where to place the decoded values
      */
     private void convertPayload(Map<String, Object> source, AppcLcmInput request) {
         try {
-            String encodedPayloadString = makeCoder().encode(source);
+            var encodedPayloadString = getCoder().encode(source);
             request.setPayload(encodedPayloadString);
         } catch (CoderException e) {
             throw new IllegalArgumentException("Cannot convert payload", e);
@@ -148,12 +135,12 @@ public class AppcLcmOperation extends BidirectionalTopicOperation<AppcLcmDmaapWr
      * Note: these values must match {@link #SELECTOR_KEYS}.
      */
     @Override
-    protected List<String> getExpectedKeyValues(int attempt, AppcLcmDmaapWrapper request) {
-        return List.of(request.getBody().getInput().getCommonHeader().getSubRequestId());
+    protected List<String> getExpectedKeyValues(int attempt, AppcLcmMessageWrapper request) {
+        return List.of(getSubRequestId());
     }
 
     @Override
-    protected Status detmStatus(String rawResponse, AppcLcmDmaapWrapper response) {
+    protected Status detmStatus(String rawResponse, AppcLcmMessageWrapper response) {
         AppcLcmResponseStatus status = getStatus(response);
         if (status == null) {
             throw new IllegalArgumentException(MISSING_STATUS);
@@ -164,25 +151,23 @@ public class AppcLcmOperation extends BidirectionalTopicOperation<AppcLcmDmaapWr
             throw new IllegalArgumentException("unknown APPC-LCM response status code: " + status.getCode());
         }
 
-        switch (code) {
-            case AppcLcmResponseCode.SUCCESS:
-                return Status.SUCCESS;
-            case AppcLcmResponseCode.FAILURE:
-                return Status.FAILURE;
-            case AppcLcmResponseCode.ERROR:
-            case AppcLcmResponseCode.REJECT:
+        return switch (code) {
+            case AppcLcmResponseCode.SUCCESS -> Status.SUCCESS;
+            case AppcLcmResponseCode.FAILURE -> Status.FAILURE;
+            case AppcLcmResponseCode.ERROR, AppcLcmResponseCode.REJECT ->
                 throw new IllegalArgumentException("APPC-LCM request was not accepted, code=" + code);
-            case AppcLcmResponseCode.ACCEPTED:
-            default:
-                return Status.STILL_WAITING;
-        }
+            default -> Status.STILL_WAITING;
+        };
     }
 
     /**
      * Sets the message to the status description, if available.
      */
     @Override
-    public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result, AppcLcmDmaapWrapper response) {
+    public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result,
+                                       AppcLcmMessageWrapper response) {
+        outcome.setResponse(response);
+
         AppcLcmResponseStatus status = getStatus(response);
         if (status == null) {
             return setOutcome(outcome, result);
@@ -204,7 +189,7 @@ public class AppcLcmOperation extends BidirectionalTopicOperation<AppcLcmDmaapWr
      * @param response the response from which to extract the status, or {@code null}
      * @return the status, or {@code null} if it does not exist
      */
-    protected AppcLcmResponseStatus getStatus(AppcLcmDmaapWrapper response) {
+    protected AppcLcmResponseStatus getStatus(AppcLcmMessageWrapper response) {
         if (response == null) {
             return null;
         }