Fix nexus and sonar vulnerabilities
[policy/models.git] / models-interactions / model-actors / actor.sdnr / src / main / java / org / onap / policy / controlloop / actor / sdnr / SdnrOperation.java
index 5faa31d..7e57bcc 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * SdnrOperation
  * ================================================================================
- * 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 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package org.onap.policy.controlloop.actor.sdnr;
 
 import java.util.List;
-import java.util.UUID;
-import java.util.concurrent.CompletableFuture;
-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;
+import org.onap.policy.sdnr.PciBody;
 import org.onap.policy.sdnr.PciCommonHeader;
+import org.onap.policy.sdnr.PciMessage;
 import org.onap.policy.sdnr.PciRequest;
-import org.onap.policy.sdnr.PciRequestWrapper;
 import org.onap.policy.sdnr.PciResponse;
-import org.onap.policy.sdnr.PciResponseWrapper;
 import org.onap.policy.sdnr.util.StatusCodeEnum;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public abstract class SdnrOperation extends BidirectionalTopicOperation<PciRequestWrapper, PciResponseWrapper> {
+public class SdnrOperation extends BidirectionalTopicOperation<PciMessage, PciMessage> {
     private static final Logger logger = LoggerFactory.getLogger(SdnrOperation.class);
 
+    /**
+     * Operation name as it should appear within config files.
+     */
+    public static final String NAME = "any";
+
+    private static final List<String> PROPERTY_NAMES = List.of(OperationProperties.EVENT_PAYLOAD);
+
     /**
      * 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, Request)} must be
+     * Note: if these change, then {@link #getExpectedKeyValues(int, PciMessage)} must be
      * updated accordingly.
      */
-    public static final List<SelectorKey> SELECTOR_KEYS = List.of(new SelectorKey("CommonHeader", "SubRequestID"));
+    public static final List<SelectorKey> SELECTOR_KEYS =
+                    List.of(new SelectorKey("body", "output", "CommonHeader", "SubRequestID"));
 
     public SdnrOperation(ControlLoopOperationParams params, BidirectionalTopicConfig config) {
-        super(params, config, PciResponseWrapper.class);
+        super(params, config, PciMessage.class, PROPERTY_NAMES);
     }
 
     /**
-     * Note: these values must match {@link #SELECTOR_KEYS}.
+     * Note: these values must be in correspondence with {@link #SELECTOR_KEYS}.
      */
     @Override
-    protected List<String> getExpectedKeyValues(int attempt, PciRequestWrapper request) {
-        return List.of(request.getBody().getCommonHeader().getSubRequestId());
-    }
-
-    @Override
-    protected CompletableFuture<OperationOutcome> startPreprocessorAsync() {
-        return startGuardAsync();
+    protected List<String> getExpectedKeyValues(int attempt, PciMessage request) {
+        return List.of(getSubRequestId());
     }
 
+    /*
+     * NOTE: This should avoid throwing exceptions, so that a ControlLoopResponse can be
+     * added to the outcome. Consequently, it returns FAILURE if a required field is
+     * missing from the response.
+     */
     @Override
-    protected Status detmStatus(String rawResponse, PciResponseWrapper responseWrapper) {
-        PciResponse response = responseWrapper.getBody();
+    protected Status detmStatus(String rawResponse, PciMessage responseWrapper) {
+        PciResponse response = responseWrapper.getBody().getOutput();
 
-        if (response == null || response.getStatus() == null) {
-            throw new IllegalArgumentException("SDNR response is missing the response status");
+        if (response.getStatus() == null) {
+            logger.warn("SDNR response is missing the response status");
+            return Status.FAILURE;
         }
 
-        StatusCodeEnum code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
+        var code = StatusCodeEnum.fromStatusCode(response.getStatus().getCode());
 
         if (code == null) {
-            throw new IllegalArgumentException(
-                            "unknown SDNR response status code: " + response.getStatus().getCode());
+            logger.warn("unknown SDNR response status code: {}", response.getStatus().getCode());
+            return Status.FAILURE;
         }
 
-        /*
-         * Response and Payload are just printed and no further action needed since
-         * casablanca release
-         */
-        logger.info("SDNR Response Code {} Message is {}", code, response.getStatus().getValue());
-        logger.info("SDNR Response Payload is {}", response.getPayload());
-
-        switch (code) {
-            case SUCCESS:
-            case PARTIAL_SUCCESS:
-                return Status.SUCCESS;
-            case FAILURE:
-            case PARTIAL_FAILURE:
-                return Status.FAILURE;
-            case ERROR:
-            case REJECT:
-                throw new IllegalArgumentException("SDNR request was not accepted, code=" + code);
-            case ACCEPTED:
-            default:
+        return switch (code) {
+            case SUCCESS, PARTIAL_SUCCESS -> Status.SUCCESS;
+            case FAILURE, PARTIAL_FAILURE -> Status.FAILURE;
+            case ERROR, REJECT -> {
+                logger.warn("SDNR request was not accepted, code={}", code);
+                yield Status.FAILURE;
+            }
+            default ->
                 // awaiting a "final" response
-                return Status.STILL_WAITING;
-        }
+                Status.STILL_WAITING;
+        };
     }
 
     /**
      * Sets the message to the status description, if available.
      */
     @Override
-    public OperationOutcome setOutcome(OperationOutcome outcome, PolicyResult result,
-            PciResponseWrapper responseWrapper) {
-        PciResponse response = responseWrapper.getBody();
-        if (response.getStatus() == null || response.getStatus().getValue() == null) {
+    public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, PciMessage responseWrapper) {
+        outcome.setResponse(responseWrapper);
+
+        if (responseWrapper.getBody() == null || responseWrapper.getBody().getOutput() == null) {
+            return setOutcome(outcome, result);
+        }
+
+        var pciResponse = responseWrapper.getBody().getOutput();
+        if (pciResponse.getStatus() == null || pciResponse.getStatus().getValue() == null) {
             return setOutcome(outcome, result);
         }
 
         outcome.setResult(result);
-        outcome.setMessage(response.getStatus().getValue());
+        outcome.setMessage(pciResponse.getStatus().getValue());
         return outcome;
     }
 
     @Override
-    protected PciRequestWrapper makeRequest(int attempt) {
-        VirtualControlLoopEvent onset = params.getContext().getEvent();
-        String subRequestId = UUID.randomUUID().toString();
+    protected PciMessage makeRequest(int attempt) {
+        String subRequestId = getSubRequestId();
 
         /* Construct an SDNR request using pci Model */
 
-        /*
-         * The actual pci request is placed in a wrapper used to send through dmaap. The
-         * current version is 2.0 as of R1.
-         */
-        PciRequestWrapper dmaapRequest = new PciRequestWrapper();
+        var dmaapRequest = new PciMessage();
         dmaapRequest.setVersion("1.0");
-        dmaapRequest.setCorrelationId(onset.getRequestId() + "-" + subRequestId);
+        dmaapRequest.setCorrelationId(params.getRequestId() + "-" + subRequestId);
         dmaapRequest.setType("request");
+        dmaapRequest.setRpcName(params.getOperation().toLowerCase());
 
         /* This is the actual request that is placed in the dmaap wrapper. */
-        final PciRequest sdnrRequest = new PciRequest();
+        final var sdnrRequest = new PciRequest();
 
         /* The common header is a required field for all SDNR requests. */
-        PciCommonHeader requestCommonHeader = new PciCommonHeader();
-        requestCommonHeader.setRequestId(onset.getRequestId());
+        var requestCommonHeader = new PciCommonHeader();
+        requestCommonHeader.setRequestId(params.getRequestId());
         requestCommonHeader.setSubRequestId(subRequestId);
 
         sdnrRequest.setCommonHeader(requestCommonHeader);
-        sdnrRequest.setPayload(onset.getPayload());
+        sdnrRequest.setPayload(getRequiredProperty(OperationProperties.EVENT_PAYLOAD, "event payload"));
+        sdnrRequest.setAction(params.getOperation());
 
         /*
-         * Once the pci request is constructed, add it into the body of the dmaap
-         * wrapper.
+         * Once the pci request is constructed, add it into the body of the dmaap wrapper.
          */
-        dmaapRequest.setBody(sdnrRequest);
-        logger.info("SDNR Request to be sent is {}", dmaapRequest);
+        var body = new PciBody();
+        body.setInput(sdnrRequest);
+        dmaapRequest.setBody(body);
 
         /* Return the request to be sent through dmaap. */
         return dmaapRequest;