2  * ============LICENSE_START=======================================================
 
   3  *  Copyright (C) 2020 Nordix Foundation.
 
   4  * ================================================================================
 
   5  * Licensed under the Apache License, Version 2.0 (the "License");
 
   6  * you may not use this file except in compliance with the License.
 
   7  * You may obtain a copy of the License at
 
   9  *      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.pnf.delegate;
 
  23 import com.fasterxml.jackson.databind.ObjectMapper;
 
  24 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  25 import org.camunda.bpm.engine.delegate.JavaDelegate;
 
  26 import org.onap.aai.domain.yang.Pnf;
 
  27 import org.onap.so.bpmn.core.json.JsonUtils;
 
  28 import org.onap.so.bpmn.infrastructure.pnf.management.PnfManagement;
 
  29 import org.onap.so.client.exception.ExceptionBuilder;
 
  30 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
 
  31 import org.onap.so.db.catalog.client.CatalogDbClient;
 
  32 import org.onap.so.serviceinstancebeans.RequestDetails;
 
  33 import org.slf4j.Logger;
 
  34 import org.slf4j.LoggerFactory;
 
  35 import org.springframework.beans.factory.annotation.Autowired;
 
  36 import org.springframework.stereotype.Component;
 
  37 import java.io.IOException;
 
  38 import java.util.List;
 
  40 import java.util.Optional;
 
  41 import static org.onap.so.bpmn.infrastructure.pnf.delegate.ExecutionVariableNames.*;
 
  44  * This implementation of {@link JavaDelegate} is used to populate the execution object for pnf software upgrade
 
  47 public class NfSoftwareUpgradeDispatcher implements JavaDelegate {
 
  49     private final Logger logger = LoggerFactory.getLogger(getClass());
 
  50     private static final String SERVICE_INSTANCE_NAME = "serviceInstanceName";
 
  51     private static final String BPMN_REQUEST = "bpmnRequest";
 
  52     private static final String RESOURCE_CUSTOMIZATION_UUID_PARAM = "resource_customization_uuid";
 
  53     private static final String PNF_NAME = "pnfName";
 
  55     // ERROR CODE for variable not found in the delegation Context
 
  56     private static final int ERROR_CODE = 601;
 
  59     private PnfManagement pnfManagement;
 
  62     private ExceptionBuilder exceptionUtil;
 
  65     private CatalogDbClient catalogDbClient;
 
  68     private ObjectMapper mapper;
 
  71     public void execute(DelegateExecution delegateExecution) throws Exception {
 
  72         logger.debug("Running execute block for activity id:{}, name:{}", delegateExecution.getCurrentActivityId(),
 
  73                 delegateExecution.getCurrentActivityName());
 
  75         RequestDetails bpmnRequestDetails = requestVerification(delegateExecution);
 
  77         final String serviceInstanceName = bpmnRequestDetails.getRequestInfo().getInstanceName();
 
  78         final String pnfName = bpmnRequestDetails.getRequestParameters().getUserParamValue(PNF_NAME);
 
  79         final String serviceModelUuid = bpmnRequestDetails.getModelInfo().getModelUuid();
 
  80         final List<Map<String, Object>> userParams = bpmnRequestDetails.getRequestParameters().getUserParams();
 
  81         final Pnf pnf = getPnfByPnfName(delegateExecution, pnfName);
 
  82         final List<PnfResourceCustomization> pnfCustomizations =
 
  83                 getPnfResourceCustomizations(delegateExecution, serviceModelUuid);
 
  84         final PnfResourceCustomization pnfResourceCustomization = pnfCustomizations.get(0);
 
  86         populateExecution(delegateExecution, bpmnRequestDetails, pnfResourceCustomization, pnf, serviceInstanceName,
 
  87                 pnfName, serviceModelUuid, userParams);
 
  89         logger.trace("Completed preProcessRequest PnfSoftwareUpgradeServiceRequest Request ");
 
  92     private RequestDetails requestVerification(DelegateExecution delegateExecution) throws IOException {
 
  93         RequestDetails bpmnRequestDetails = mapper.readValue(
 
  94                 JsonUtils.getJsonValue(String.valueOf(delegateExecution.getVariable(BPMN_REQUEST)), "requestDetails"),
 
  95                 RequestDetails.class);
 
  97         throwIfNull(delegateExecution, bpmnRequestDetails.getModelInfo(), SERVICE_MODEL_INFO);
 
  98         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestInfo(), "RequestInfo");
 
  99         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters(), "RequestParameters");
 
 100         throwIfNull(delegateExecution, bpmnRequestDetails.getRequestParameters().getUserParams(), "UserParams");
 
 102         return bpmnRequestDetails;
 
 105     private void populateExecution(DelegateExecution delegateExecution, RequestDetails bpmnRequestDetails,
 
 106             PnfResourceCustomization pnfResourceCustomization, Pnf pnf, String serviceInstanceName, String pnfName,
 
 107             String serviceModelUuid, List<Map<String, Object>> userParams) {
 
 109         delegateExecution.setVariable(SERVICE_MODEL_INFO, bpmnRequestDetails.getModelInfo());
 
 110         delegateExecution.setVariable(SERVICE_INSTANCE_NAME, serviceInstanceName);
 
 111         delegateExecution.setVariable(PNF_CORRELATION_ID, pnfName);
 
 112         delegateExecution.setVariable(MODEL_UUID, serviceModelUuid);
 
 113         delegateExecution.setVariable(PNF_UUID, pnf.getPnfId());
 
 114         delegateExecution.setVariable(PRC_BLUEPRINT_NAME, pnfResourceCustomization.getBlueprintName());
 
 115         delegateExecution.setVariable(PRC_BLUEPRINT_VERSION, pnfResourceCustomization.getBlueprintVersion());
 
 116         delegateExecution.setVariable(PRC_CUSTOMIZATION_UUID, pnfResourceCustomization.getModelCustomizationUUID());
 
 117         delegateExecution.setVariable(RESOURCE_CUSTOMIZATION_UUID_PARAM,
 
 118                 pnfResourceCustomization.getModelCustomizationUUID());
 
 119         delegateExecution.setVariable(PRC_INSTANCE_NAME, pnfResourceCustomization.getModelInstanceName());
 
 120         delegateExecution.setVariable(PRC_CONTROLLER_ACTOR, pnfResourceCustomization.getControllerActor());
 
 122         for (Map<String, Object> param : userParams) {
 
 123             if (param.containsKey("name") && param.containsKey("value")) {
 
 124                 delegateExecution.setVariable(param.get("name").toString(), param.get("value").toString());
 
 129     private Pnf getPnfByPnfName(DelegateExecution delegateExecution, String pnfName) {
 
 130         Optional<Pnf> pnfOptional = null;
 
 132             pnfOptional = pnfManagement.getEntryFor(pnfName);
 
 133         } catch (IOException e) {
 
 134             logger.warn(e.getMessage(), e);
 
 135             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
 
 136                     "Unable to fetch from AAI" + e.getMessage());
 
 138         if (pnfOptional == null || !pnfOptional.isPresent()) {
 
 139             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
 
 140                     "AAI entry for PNF: " + pnfName + " does not exist");
 
 142         return pnfOptional.get();
 
 145     private List<PnfResourceCustomization> getPnfResourceCustomizations(DelegateExecution delegateExecution,
 
 146             String serviceModelUuid) {
 
 147         List<PnfResourceCustomization> pnfCustomizations =
 
 148                 catalogDbClient.getPnfResourceCustomizationByModelUuid(serviceModelUuid);
 
 150         if (pnfCustomizations == null || pnfCustomizations.isEmpty()) {
 
 151             logger.warn("Unable to find the PNF resource customizations of model service UUID: {}", serviceModelUuid);
 
 152             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
 
 153                     "Unable to find the PNF resource customizations of model service UUID:  " + serviceModelUuid);
 
 155         return pnfCustomizations;
 
 158     private void throwIfNull(DelegateExecution delegateExecution, Object obj, String param) {
 
 160             logger.warn("Unable to find the parameter: {} in the execution context", param);
 
 161             exceptionUtil.buildAndThrowWorkflowException(delegateExecution, ERROR_CODE,
 
 162                     "Unable to find parameter " + param);