2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2019 Nordix
 
   4  *  Modifications Copyright (C) 2020 Huawei
 
   5  *  ================================================================================
 
   6  *  Licensed under the Apache License, Version 2.0 (the "License");
 
   7  *  you may not use this file except in compliance with the License.
 
   8  *  You may obtain a copy of the License at
 
  10  *        http://www.apache.org/licenses/LICENSE-2.0
 
  11  *  Unless required by applicable law or agreed to in writing, software
 
  12  *  distributed under the License is distributed on an "AS IS" BASIS,
 
  13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  14  *  See the License for the specific language governing permissions and
 
  15  *  limitations under the License.
 
  17  *  SPDX-License-Identifier: Apache-2.0
 
  18  *  ============LICENSE_END=========================================================
 
  21 package org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.sdnc;
 
  23 import java.util.List;
 
  24 import java.util.UUID;
 
  25 import com.fasterxml.jackson.core.JsonProcessingException;
 
  26 import com.fasterxml.jackson.databind.ObjectMapper;
 
  27 import org.springframework.stereotype.Component;
 
  28 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  29 import org.onap.so.bpmn.core.json.JsonUtils;
 
  30 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
 
  31 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.common.SoPropertyConstants;
 
  32 import org.onap.so.bpmn.infrastructure.decisionpoint.impl.camunda.controller.LcmControllerDE;
 
  33 import org.onap.so.client.sdnc.common.SDNCConstants;
 
  34 import org.onap.so.client.sdnc.lcm.*;
 
  35 import org.onap.so.client.sdnc.lcm.beans.*;
 
  36 import org.onap.so.client.sdnc.lcm.beans.payload.*;
 
  37 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_ID;
 
  38 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.PNF_CORRELATION_ID;
 
  39 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.REQUEST_PAYLOAD;
 
  42 public class SdncControllerDE extends LcmControllerDE {
 
  44     private static final int SDNC_DELEGATE_EXECUTION_ERROR_CODE = 1103;
 
  47     public Boolean understand(ControllerContext<DelegateExecution> context) {
 
  48         return context.getControllerActor().equalsIgnoreCase("sdnc");
 
  52     public Boolean ready(ControllerContext<DelegateExecution> context) {
 
  57     protected int callLcmClient(ControllerContext<DelegateExecution> context) {
 
  58         DelegateExecution execution = context.getExecution();
 
  60         logger.debug("Running activity for id: {}, name: {}", execution.getCurrentActivityId(),
 
  61                 execution.getCurrentActivityName());
 
  65             LcmInput lcmInput = buildLcmInput(execution);
 
  66             if (lcmInput != null) {
 
  67                 result = sendLcmRequest(execution, lcmInput);
 
  69                 logger.error("Build LCM Input error");
 
  72         } catch (Exception e) {
 
  73             logger.error("Call SDNC LCM Client failure: ", e);
 
  78             execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Success");
 
  80             execution.setVariable(SoPropertyConstants.CONTROLLER_STATUS, "Failure");
 
  87     protected int getErrorCode() {
 
  88         return SDNC_DELEGATE_EXECUTION_ERROR_CODE;
 
  91     private LcmOutput sendSyncRequest(String operation, LcmInput lcmInput) {
 
  92         SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
 
  93         SDNCLcmRestClient sdncLcmRestClient;
 
  95             sdncLcmRestClient = sdncLcmClientBuilder.newSDNCLcmRestClient(operation);
 
  96         } catch (SDNCLcmClientBuilderException e) {
 
  97             logger.error("Create SDNCLcmRestClient error: ", e);
 
 101         return sdncLcmRestClient.sendRequest(lcmInput);
 
 104     private LcmOutput selectLcmOutputFromDmaapResponses(List<LcmDmaapResponse> lcmDmaapResponses, LcmInput lcmInput) {
 
 105         String expectedCorrelationId =
 
 106                 lcmInput.getCommonHeader().getRequestId() + "-" + lcmInput.getCommonHeader().getSubRequestId();
 
 108         for (LcmDmaapResponse lcmDmaapResponse : lcmDmaapResponses) {
 
 109             String correlationId = lcmDmaapResponse.getCorrelationId();
 
 110             if (expectedCorrelationId.equals(correlationId)) {
 
 111                 return lcmDmaapResponse.getBody().getOutput();
 
 118     private LcmOutput sendAsyncRequest(String operation, LcmInput lcmInput) {
 
 119         SDNCLcmClientBuilder sdncLcmClientBuilder = new SDNCLcmClientBuilder();
 
 120         SDNCLcmDmaapClient sdncLcmDmaapClient;
 
 122             sdncLcmDmaapClient = sdncLcmClientBuilder.newSDNCLcmDmaapClient();
 
 123         } catch (SDNCLcmClientBuilderException e) {
 
 124             logger.error("Create SDNCLcmDmaapClient error: ", e);
 
 128         LcmDmaapRequest lcmDmaapRequest = SDNCLcmMessageBuilder.buildLcmDmaapRequest(operation, lcmInput);
 
 130             sdncLcmDmaapClient.sendRequest(lcmDmaapRequest);
 
 131         } catch (Exception e) {
 
 132             logger.error("SDNCLcmDmaapClient sends request error: ", e);
 
 136         long timeout = sdncLcmClientBuilder.getSDNCLcmProperties().getActionTimeout();
 
 137         long startTime = System.currentTimeMillis();
 
 139             List<LcmDmaapResponse> lcmDmaapResponses = sdncLcmDmaapClient.getResponse();
 
 140             if (lcmDmaapResponses.size() > 0) {
 
 141                 LcmOutput lcmOutput = selectLcmOutputFromDmaapResponses(lcmDmaapResponses, lcmInput);
 
 142                 if (lcmOutput != null) {
 
 147             long stopTime = System.currentTimeMillis();
 
 148             if ((stopTime - startTime) > timeout) {
 
 149                 logger.error("Timeout for SDNC LCM action {}", lcmInput.getAction());
 
 155     public static String toLowerHyphen(String lcmAction) {
 
 156         String regex = "([a-z0-9A-Z])(?=[A-Z])";
 
 157         String replacement = "$1-";
 
 158         return lcmAction.replaceAll(regex, replacement).toLowerCase();
 
 161     private LcmInput buildLcmInput(DelegateExecution execution) throws JsonProcessingException {
 
 162         String requestId = String.valueOf(execution.getVariable(REQUEST_ID));
 
 163         String requestAction = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION));
 
 164         String pnfName = String.valueOf(execution.getVariable(PNF_CORRELATION_ID));
 
 165         logger.debug(String.format("requestId: %s, action: %s, pnfName: %s", requestId, requestAction, pnfName));
 
 167         String requestPayload = String.valueOf(execution.getVariable(REQUEST_PAYLOAD));
 
 168         logger.debug("SO request payload: " + requestPayload);
 
 173         switch (requestAction) {
 
 174             case SoPropertyConstants.ACTION_PRE_CHECK:
 
 175                 lcmAction = SDNCLcmActionConstants.UPGRADE_PRE_CHECK;
 
 177                 UpgradePreCheckPayload upgradePreCheckPayload;
 
 178                 upgradePreCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePreCheckPayload(execution);
 
 179                 lcmPayload = SDNCLcmPayloadBuilder.convertToSting(upgradePreCheckPayload);
 
 181             case SoPropertyConstants.ACTION_DOWNLOAD_N_E_SW:
 
 182                 lcmAction = SDNCLcmActionConstants.DOWNLOAD_N_E_SW;
 
 184                 DownloadNESwPayload downloadNESwPayload;
 
 185                 downloadNESwPayload = SDNCLcmPayloadBuilder.buildDownloadNESwPayload(execution);
 
 186                 lcmPayload = SDNCLcmPayloadBuilder.convertToSting(downloadNESwPayload);
 
 188             case SoPropertyConstants.ACTION_ACTIVATE_N_E_SW:
 
 189                 lcmAction = SDNCLcmActionConstants.ACTIVATE_N_E_SW;
 
 191                 ActivateNESwPayload activateNESwPayload;
 
 192                 activateNESwPayload = SDNCLcmPayloadBuilder.buildActivateNESwPayload(execution);
 
 193                 lcmPayload = SDNCLcmPayloadBuilder.convertToSting(activateNESwPayload);
 
 195             case SoPropertyConstants.ACTION_POST_CHECK:
 
 196                 lcmAction = SDNCLcmActionConstants.UPGRADE_POST_CHECK;
 
 198                 UpgradePostCheckPayload upgradePostCheckPayload;
 
 199                 upgradePostCheckPayload = SDNCLcmPayloadBuilder.buildUpgradePostCheckPayload(execution);
 
 200                 lcmPayload = SDNCLcmPayloadBuilder.convertToSting(upgradePostCheckPayload);
 
 203                 logger.error("Unsupported SO Action: " + requestAction);
 
 207         logger.debug("SDNC LCM payload for {}: {}", lcmAction, lcmPayload);
 
 209         String subRequestId = UUID.randomUUID().toString();
 
 211                 SDNCLcmMessageBuilder.buildLcmInputForPnf(requestId, subRequestId, pnfName, lcmAction, lcmPayload);
 
 213         ObjectMapper mapper = new ObjectMapper();
 
 214         String lcmInputMsg = mapper.writeValueAsString(lcmInput);
 
 215         logger.debug("SDNC input message:\n" + lcmInputMsg);
 
 220     private boolean parseLcmOutput(LcmOutput lcmOutput, String lcmAction) {
 
 221         if (lcmOutput == null) {
 
 222             logger.error("Call SDNC LCM API failure");
 
 226         LcmStatus lcmStatus = lcmOutput.getStatus();
 
 228         if (lcmStatus.getCode() == SDNCConstants.LCM_OUTPUT_SUCCESS_CODE) {
 
 229             logger.debug("Call SDNC LCM API success: " + lcmStatus.getMessage());
 
 231             logger.error("Call SDNC LCM API failure: " + lcmStatus.getMessage());
 
 234         String outputPayload = lcmOutput.getPayload();
 
 235         logger.debug("SDNC LCM action: {}, result: {}", lcmAction, outputPayload);
 
 236         if (outputPayload != null) {
 
 237             String result = JsonUtils.getJsonValue(outputPayload, "result");
 
 238             if ("Success".equals(result)) {
 
 239                 logger.debug("Run SDNC LCM action {} success", lcmAction);
 
 242                 String reason = JsonUtils.getJsonValue(outputPayload, "reason");
 
 243                 logger.error("Run SDNC LCM action {} failure, reason: {}", lcmAction, reason);
 
 250     private boolean sendLcmRequest(DelegateExecution execution, LcmInput lcmInput) {
 
 251         String actionMode = String.valueOf(execution.getVariable(SoPropertyConstants.SO_ACTION_MODE));
 
 252         String lcmOperation = toLowerHyphen(lcmInput.getAction());
 
 255         if ("async".equals(actionMode)) {
 
 256             lcmOutput = sendAsyncRequest(lcmOperation, lcmInput);
 
 258             lcmOutput = sendSyncRequest(lcmOperation, lcmInput);
 
 261         return parseLcmOutput(lcmOutput, lcmInput.getAction());