X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-interactions%2Fmodel-actors%2Factor.appclcm%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fcontrolloop%2Factor%2Fappclcm%2FAppcLcmOperation.java;h=1fd6f2d99ac5aa0fa376064b69cef47777b7adbb;hb=49f07db935d114b72a44e446867b16262dd552aa;hp=c0b83319c13de15dcafc0d5b7d82de3451fb6f1d;hpb=7f1903bae3069d5e14b4c322c09c1317d90114b6;p=policy%2Fmodels.git diff --git a/models-interactions/model-actors/actor.appclcm/src/main/java/org/onap/policy/controlloop/actor/appclcm/AppcLcmOperation.java b/models-interactions/model-actors/actor.appclcm/src/main/java/org/onap/policy/controlloop/actor/appclcm/AppcLcmOperation.java index c0b83319c..1fd6f2d99 100644 --- a/models-interactions/model-actors/actor.appclcm/src/main/java/org/onap/policy/controlloop/actor/appclcm/AppcLcmOperation.java +++ b/models-interactions/model-actors/actor.appclcm/src/main/java/org/onap/policy/controlloop/actor/appclcm/AppcLcmOperation.java @@ -1,8 +1,9 @@ /*- * ============LICENSE_START======================================================= - * AppcLcmOperation + * 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,42 +23,38 @@ 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.onap.aai.domain.yang.GenericVnf; -import org.onap.policy.aai.AaiConstants; -import org.onap.policy.aai.AaiCqResponse; 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.common.utils.coder.StandardCoder; -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.slf4j.Logger; -import org.slf4j.LoggerFactory; -public abstract class AppcLcmOperation extends BidirectionalTopicOperation { +public class AppcLcmOperation extends BidirectionalTopicOperation { - private static final Logger logger = LoggerFactory.getLogger(AppcLcmOperation.class); - private static final StandardCoder coder = new StandardCoder(); + 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 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. *

- * Note: if these change, then {@link #getExpectedKeyValues(int, Request)} must be - * updated accordingly. + * Note: if these change, then {@link #getExpectedKeyValues(int, AppcLcmMessageWrapper)} + * must be updated accordingly. */ - public static final List SELECTOR_KEYS = List.of(new SelectorKey("common-header", "sub-request-id")); + public static final List SELECTOR_KEYS = + List.of(new SelectorKey("body", "output", "common-header", "sub-request-id")); /** * Constructs the object. @@ -66,74 +63,36 @@ public abstract class AppcLcmOperation extends BidirectionalTopicOperation startPreprocessorAsync() { - if (params != null) { - ControlLoopOperationParams cqParams = params.toBuilder().actor(AaiConstants.ACTOR_NAME) - .operation(AaiCqResponse.OPERATION).payload(null).retry(null).timeoutSec(null).build(); - - // run Custom Query and Guard, in parallel - return allOf(() -> params.getContext().obtain(AaiCqResponse.CONTEXT_KEY, cqParams), this::startGuardAsync); - } - return startGuardAsync(); - } - - @Override - protected AppcLcmDmaapWrapper makeRequest(int attempt) { - AaiCqResponse cq = params.getContext().getProperty(AaiCqResponse.CONTEXT_KEY); - - GenericVnf genvnf = cq.getGenericVnfByModelInvariantId(params.getTarget().getResourceID()); - if (genvnf == null) { - logger.info("{}: target entity could not be found for {}", getFullName(), params.getRequestId()); - throw new IllegalArgumentException("target vnf-id could not be found"); - } - - return makeRequest(attempt, genvnf.getVnfId()); - } + protected AppcLcmMessageWrapper makeRequest(int attempt) { + String subRequestId = getSubRequestId(); - /** - * Makes a request, given the target VNF. This is a support function for - * {@link #makeRequest(int)}. - * - * @param attempt attempt number - * @param targetVnf target VNF - * @return a new request - */ - protected AppcLcmDmaapWrapper makeRequest(int attempt, String targetVnf) { - VirtualControlLoopEvent onset = params.getContext().getEvent(); - String subRequestId = UUID.randomUUID().toString(); - - 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); - inputRequest.setAction(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. + * Action Identifiers are required for APPC LCM requests. For R1, the recipes + * supported by Policy only require a vnf-id. */ - if (inputRequest.getActionIdentifiers() != null) { - inputRequest.getActionIdentifiers().put(VNF_ID_KEY, targetVnf); - } else { - inputRequest.setActionIdentifiers(Map.of(VNF_ID_KEY, targetVnf)); - } + 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 called. + * 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 convertPayload() is + * called. */ if (operationSupportsPayload()) { convertPayload(params.getPayload(), inputRequest); @@ -141,22 +100,19 @@ public abstract class AppcLcmOperation extends BidirectionalTopicOperation source, AppcLcmInput request) { - String encodedPayloadString = null; + private void convertPayload(Map source, AppcLcmInput request) { try { - encodedPayloadString = coder.encode(source); + var encodedPayloadString = getCoder().encode(source); request.setPayload(encodedPayloadString); } catch (CoderException e) { - logger.error("Cannot convert payload. Error encoding source as a string.", e); - throw new IllegalArgumentException("Cannot convert payload. Error encoding source as a string."); + throw new IllegalArgumentException("Cannot convert payload", e); } } @@ -181,55 +135,85 @@ public abstract class AppcLcmOperation extends BidirectionalTopicOperation getExpectedKeyValues(int attempt, AppcLcmDmaapWrapper request) { - return List.of(request.getBody().getInput().getCommonHeader().getSubRequestId()); + protected List getExpectedKeyValues(int attempt, AppcLcmMessageWrapper request) { + return List.of(getSubRequestId()); } @Override - protected Status detmStatus(String rawResponse, AppcLcmDmaapWrapper response) { - if (response == null || response.getBody() == null || response.getBody().getOutput() == null - || response.getBody().getOutput().getStatus() == null) { - throw new IllegalArgumentException("APPC-LCM response is missing the response status"); + protected Status detmStatus(String rawResponse, AppcLcmMessageWrapper response) { + AppcLcmResponseStatus status = getStatus(response); + if (status == null) { + throw new IllegalArgumentException(MISSING_STATUS); } - String code = AppcLcmResponseCode.toResponseValue(response.getBody().getOutput().getStatus().getCode()); - + String code = AppcLcmResponseCode.toResponseValue(status.getCode()); if (code == null) { - throw new IllegalArgumentException( - "unknown APPC-LCM response status code: " + response.getBody().getOutput().getStatus().getCode()); + 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) { - if (response == null || response.getBody() == null || response.getBody().getOutput() == null - || response.getBody().getOutput().getStatus() == null - || response.getBody().getOutput().getStatus().getMessage() == null) { + public OperationOutcome setOutcome(OperationOutcome outcome, OperationResult result, + AppcLcmMessageWrapper response) { + outcome.setResponse(response); + + AppcLcmResponseStatus status = getStatus(response); + if (status == null) { + return setOutcome(outcome, result); + } + + String message = status.getMessage(); + if (message == null) { return setOutcome(outcome, result); } outcome.setResult(result); - outcome.setMessage(response.getBody().getOutput().getStatus().getMessage()); + outcome.setMessage(message); return outcome; } + /** + * Gets the status from the response. + * + * @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(AppcLcmMessageWrapper response) { + if (response == null) { + return null; + } + + AppcLcmBody body = response.getBody(); + if (body == null) { + return null; + } + + AppcLcmOutput output = body.getOutput(); + if (output == null) { + return null; + } + + return output.getStatus(); + } + + /** + * Determines if the operation supports a payload. + * + * @return {@code true} if the operation supports a payload, {@code false} otherwise + */ protected boolean operationSupportsPayload() { - return params.getPayload() != null && !params.getPayload().isEmpty(); + return params.getPayload() != null && !params.getPayload().isEmpty() + && AppcLcmConstants.SUPPORTS_PAYLOAD.contains(params.getOperation().toLowerCase()); } }