2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Modifications Copyright (c) 2019 Samsung
 
   8  * ================================================================================
 
   9  * Modifications Copyright (c) 2021 Nokia
 
  10  * ================================================================================
 
  11  * Modifications Copyright (c) 2020 Tech Mahindra
 
  12  * ================================================================================
 
  13  * Licensed under the Apache License, Version 2.0 (the "License");
 
  14  * you may not use this file except in compliance with the License.
 
  15  * You may obtain a copy of the License at
 
  17  *      http://www.apache.org/licenses/LICENSE-2.0
 
  19  * Unless required by applicable law or agreed to in writing, software
 
  20  * distributed under the License is distributed on an "AS IS" BASIS,
 
  21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  22  * See the License for the specific language governing permissions and
 
  23  * limitations under the License.
 
  24  * ============LICENSE_END=========================================================
 
  27 package org.onap.so.bpmn.infrastructure.workflow.tasks;
 
  29 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
 
  30 import org.onap.so.bpmn.servicedecomposition.entities.ConfigurationResourceKeys;
 
  31 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
 
  32 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
 
  33 import org.onap.so.db.catalog.beans.macro.OrchestrationFlow;
 
  34 import org.onap.so.serviceinstancebeans.RequestDetails;
 
  35 import org.slf4j.Logger;
 
  36 import org.slf4j.LoggerFactory;
 
  37 import org.springframework.stereotype.Component;
 
  38 import java.util.ArrayList;
 
  39 import java.util.Collections;
 
  40 import java.util.List;
 
  41 import java.util.Optional;
 
  42 import java.util.UUID;
 
  43 import java.util.stream.Collectors;
 
  44 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.ASSIGNINSTANCE;
 
  45 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.CONFIGURATION;
 
  46 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.CONTROLLER;
 
  47 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.CREATE_INSTANCE;
 
  48 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.NETWORKCOLLECTION;
 
  49 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.REPLACEINSTANCE;
 
  50 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.REPLACEINSTANCERETAINASSIGNMENTS;
 
  51 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.SERVICE;
 
  52 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.VOLUMEGROUP;
 
  55 public class ExecuteBuildingBlockBuilder {
 
  57     private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockBuilder.class);
 
  59     private static final String VNF = "Vnf";
 
  60     private static final String PNF = "Pnf";
 
  61     private static final String VFMODULE = "VfModule";
 
  62     private static final String NETWORK = "Network";
 
  64     protected List<ExecuteBuildingBlock> buildExecuteBuildingBlockList(List<OrchestrationFlow> orchFlows,
 
  65             List<Resource> resourceList, String requestId, String apiVersion, String resourceId, String requestAction,
 
  66             String vnfType, WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails,
 
  68         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
 
  69         for (OrchestrationFlow orchFlow : orchFlows) {
 
  70             if (orchFlow.getFlowName().contains(SERVICE)) {
 
  72                     workflowResourceIds.setServiceInstanceId(resourceId);
 
  74                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.SERVICE, orchFlow, requestId,
 
  75                         apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false,
 
  77             } else if (orchFlow.getFlowName().contains(VNF) || (orchFlow.getFlowName().contains(CONTROLLER)
 
  78                     && (VNF).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
 
  79                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.VNF, orchFlow, requestId,
 
  80                         apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false,
 
  82             } else if (orchFlow.getFlowName().contains(PNF) || (orchFlow.getFlowName().contains(CONTROLLER)
 
  83                     && (PNF).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
 
  84                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.PNF, orchFlow, requestId,
 
  85                         apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false,
 
  87             } else if (orchFlow.getFlowName().contains(NETWORK)
 
  88                     && !orchFlow.getFlowName().contains(NETWORKCOLLECTION)) {
 
  89                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.NETWORK, orchFlow, requestId,
 
  90                         apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false,
 
  92                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.VIRTUAL_LINK, orchFlow,
 
  93                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
 
  95             } else if (orchFlow.getFlowName().contains(VFMODULE) || (orchFlow.getFlowName().contains(CONTROLLER)
 
  96                     && (VFMODULE).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
 
  97                 List<Resource> vfModuleResourcesSorted;
 
  98                 if (requestAction.equals(CREATE_INSTANCE) || requestAction.equals(ASSIGNINSTANCE)
 
  99                         || requestAction.equals("activateInstance")) {
 
 100                     vfModuleResourcesSorted = sortVfModulesByBaseFirst(resourceList.stream()
 
 101                             .filter(x -> WorkflowType.VFMODULE == x.getResourceType()).collect(Collectors.toList()));
 
 103                     vfModuleResourcesSorted = sortVfModulesByBaseLast(resourceList.stream()
 
 104                             .filter(x -> WorkflowType.VFMODULE == x.getResourceType()).collect(Collectors.toList()));
 
 106                 for (Resource resource : vfModuleResourcesSorted) {
 
 107                     flowsToExecute.add(buildExecuteBuildingBlock(orchFlow, requestId, resource, apiVersion, resourceId,
 
 108                             requestAction, false, vnfType, workflowResourceIds, requestDetails, false, null, null,
 
 111             } else if (orchFlow.getFlowName().contains(VOLUMEGROUP)) {
 
 112                 if (requestAction.equalsIgnoreCase(REPLACEINSTANCE)
 
 113                         || requestAction.equalsIgnoreCase(REPLACEINSTANCERETAINASSIGNMENTS)) {
 
 114                     logger.debug("Replacing workflow resource id by volume group id");
 
 115                     resourceId = workflowResourceIds.getVolumeGroupId();
 
 117                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.VOLUMEGROUP, orchFlow,
 
 118                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
 
 120             } else if (orchFlow.getFlowName().contains(NETWORKCOLLECTION)) {
 
 121                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.NETWORKCOLLECTION, orchFlow,
 
 122                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
 
 124             } else if (orchFlow.getFlowName().contains(CONFIGURATION)) {
 
 125                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.CONFIGURATION, orchFlow,
 
 126                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
 
 130                         .add(buildExecuteBuildingBlock(orchFlow, requestId, null, apiVersion, resourceId, requestAction,
 
 131                                 false, vnfType, workflowResourceIds, requestDetails, false, null, null, false, null));
 
 134         return flowsToExecute;
 
 137     protected ExecuteBuildingBlock buildExecuteBuildingBlock(OrchestrationFlow orchFlow, String requestId,
 
 138             Resource resource, String apiVersion, String resourceId, String requestAction, boolean aLaCarte,
 
 139             String vnfType, WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails,
 
 140             boolean isVirtualLink, String virtualLinkKey, String vnfcName, boolean isConfiguration,
 
 141             ReplaceInstanceRelatedInformation replaceInfo) {
 
 143         BuildingBlock buildingBlock =
 
 144                 new BuildingBlock().setBpmnFlowName(orchFlow.getFlowName()).setMsoId(UUID.randomUUID().toString())
 
 145                         .setIsVirtualLink(isVirtualLink).setVirtualLinkKey(virtualLinkKey)
 
 146                         .setKey(Optional.ofNullable(resource).map(Resource::getResourceId).orElse(""));
 
 147         Optional.ofNullable(orchFlow.getBpmnAction()).ifPresent(buildingBlock::setBpmnAction);
 
 148         Optional.ofNullable(orchFlow.getBpmnScope()).ifPresent(buildingBlock::setBpmnScope);
 
 149         String oldVolumeGroupName = "";
 
 150         if (replaceInfo != null) {
 
 151             oldVolumeGroupName = replaceInfo.getOldVolumeGroupName();
 
 154                 && (orchFlow.getFlowName().contains(VOLUMEGROUP) && (requestAction.equalsIgnoreCase(REPLACEINSTANCE)
 
 155                         || requestAction.equalsIgnoreCase(REPLACEINSTANCERETAINASSIGNMENTS)))) {
 
 156             logger.debug("Setting resourceId to volume group id for volume group flow on replace");
 
 157             resourceId = workflowResourceIds.getVolumeGroupId();
 
 160         ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock().setApiVersion(apiVersion)
 
 161                 .setaLaCarte(aLaCarte).setRequestAction(requestAction).setResourceId(resourceId).setVnfType(vnfType)
 
 162                 .setWorkflowResourceIds(workflowResourceIds).setRequestId(requestId).setBuildingBlock(buildingBlock)
 
 163                 .setRequestDetails(requestDetails).setOldVolumeGroupName(oldVolumeGroupName);
 
 165         if (resource != null && (isConfiguration || resource.getResourceType().equals(WorkflowType.CONFIGURATION))) {
 
 166             ConfigurationResourceKeys configurationResourceKeys = getConfigurationResourceKeys(resource, vnfcName);
 
 167             executeBuildingBlock.setConfigurationResourceKeys(configurationResourceKeys);
 
 169         return executeBuildingBlock;
 
 172     protected List<Resource> sortVfModulesByBaseFirst(List<Resource> vfModuleResources) {
 
 174         for (Resource resource : vfModuleResources) {
 
 175             if (resource.isBaseVfModule()) {
 
 176                 Collections.swap(vfModuleResources, 0, count);
 
 181         return vfModuleResources;
 
 184     protected List<Resource> sortVfModulesByBaseLast(List<Resource> vfModuleResources) {
 
 186         for (Resource resource : vfModuleResources) {
 
 187             if (resource.isBaseVfModule()) {
 
 188                 Collections.swap(vfModuleResources, vfModuleResources.size() - 1, count);
 
 193         return vfModuleResources;
 
 196     private void addBuildingBlockToExecuteBBList(List<ExecuteBuildingBlock> flowsToExecute, List<Resource> resourceList,
 
 197             WorkflowType workflowType, OrchestrationFlow orchFlow, String requestId, String apiVersion,
 
 198             String resourceId, String requestAction, String vnfType, WorkflowResourceIds workflowResourceIds,
 
 199             RequestDetails requestDetails, boolean isVirtualLink, boolean isConfiguration) {
 
 201         resourceList.stream().filter(resource -> resource.getResourceType().equals(workflowType))
 
 202                 .forEach(resource -> flowsToExecute.add(buildExecuteBuildingBlock(orchFlow, requestId, resource,
 
 203                         apiVersion, resourceId, requestAction, false, vnfType, workflowResourceIds, requestDetails,
 
 204                         isVirtualLink, resource.getVirtualLinkKey(), null, isConfiguration, null)));
 
 207     private ConfigurationResourceKeys getConfigurationResourceKeys(Resource resource, String vnfcName) {
 
 208         ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
 
 209         Optional.ofNullable(vnfcName).ifPresent(configurationResourceKeys::setVnfcName);
 
 210         configurationResourceKeys.setCvnfcCustomizationUUID(resource.getCvnfModuleCustomizationId());
 
 211         configurationResourceKeys.setVfModuleCustomizationUUID(resource.getVfModuleCustomizationId());
 
 212         configurationResourceKeys.setVnfResourceCustomizationUUID(resource.getVnfCustomizationId());
 
 213         return configurationResourceKeys;