Merge "Fix in e2e activation flow for option1"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / ExecuteBuildingBlockBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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  * Modifications Copyright (c) 2021 Orange
14  * ================================================================================
15  * Licensed under the Apache License, Version 2.0 (the "License");
16  * you may not use this file except in compliance with the License.
17  * You may obtain a copy of the License at
18  *
19  *      http://www.apache.org/licenses/LICENSE-2.0
20  *
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  * ============LICENSE_END=========================================================
27  */
28
29 package org.onap.so.bpmn.infrastructure.workflow.tasks;
30
31 import org.apache.commons.lang3.builder.ToStringBuilder;
32 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
33 import org.onap.so.bpmn.servicedecomposition.entities.ConfigurationResourceKeys;
34 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
35 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
36 import org.onap.so.db.catalog.beans.macro.OrchestrationFlow;
37 import org.onap.so.serviceinstancebeans.RequestDetails;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.stereotype.Component;
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Optional;
44 import java.util.UUID;
45 import java.util.stream.Collectors;
46 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.*;
47
48 @Component
49 public class ExecuteBuildingBlockBuilder {
50
51     private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockBuilder.class);
52
53     private static final String VNF = "Vnf";
54     private static final String PNF = "Pnf";
55     private static final String VFMODULE = "VfModule";
56     private static final String NETWORK = "Network";
57     private static final String HEALTH_CHECK = "HealthCheckBB";
58     private static final String UPGRADE_CNF = "UpgradeVfModuleBB";
59
60     protected List<ExecuteBuildingBlock> buildExecuteBuildingBlockList(List<OrchestrationFlow> orchFlows,
61             List<Resource> originalResourceList, String requestId, String apiVersion, String resourceId,
62             String requestAction, String vnfType, WorkflowResourceIds workflowResourceIds,
63             RequestDetails requestDetails, boolean replaceVnf) {
64         List<Resource> resourceList = getOnlyRootResourceList(originalResourceList);
65
66         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
67
68         boolean ascendingOrder = requestAction.equals(CREATE_INSTANCE) || requestAction.equals(ASSIGN_INSTANCE)
69                 || requestAction.equals(ACTIVATE_INSTANCE);
70
71         ExecutionPlan plan = ExecutionPlan.build(resourceList, ascendingOrder);
72
73         logger.info("Orchestration Flows");
74         for (OrchestrationFlow orchFlow : orchFlows) {
75             String flowDetails = new ToStringBuilder(this).append("id", orchFlow.getId())
76                     .append("action", orchFlow.getAction()).append("sequenceNumber", orchFlow.getSequenceNumber())
77                     .append("flowName", orchFlow.getFlowName()).append("flowVersion", orchFlow.getFlowVersion())
78                     .append("bpmnAction", orchFlow.getBpmnAction()).append("bpmnScope", orchFlow.getBpmnScope())
79                     .toString();
80             logger.info("Flow: " + flowDetails);
81             buildExecuteBuildingBlockListPlan(orchFlow, plan, requestId, apiVersion, resourceId, requestAction, vnfType,
82                     workflowResourceIds, requestDetails, replaceVnf);
83         }
84
85         plan.flushBlocksFromCache(flowsToExecute);
86
87         return flowsToExecute;
88     }
89
90     protected void buildExecuteBuildingBlockListPlan(OrchestrationFlow flow, ExecutionPlan plan, String requestId,
91             String apiVersion, String resourceId, String requestAction, String vnfType,
92             WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails, boolean replaceVnf) {
93
94         List<ExecuteBuildingBlock> mainFlows = buildExecuteBuildingBlockListRaw(flow, plan.getResource(), requestId,
95                 apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, replaceVnf);
96
97         plan.pushBlockToCache(mainFlows);
98
99         for (ExecutionGroup nestedGroup : plan.getNestedExecutions()) {
100             for (ExecutionPlan nestedPlan : nestedGroup.getNestedExecutions()) {
101                 buildExecuteBuildingBlockListPlan(flow, nestedPlan, requestId, apiVersion, resourceId, requestAction,
102                         vnfType, workflowResourceIds, requestDetails, replaceVnf);
103             }
104             if (nestedGroup.getCacheSize() > 0)
105                 plan.changeCurrentGroup(nestedGroup);
106         }
107     }
108
109     private List<ExecuteBuildingBlock> buildExecuteBuildingBlockListRaw(OrchestrationFlow orchFlow, Resource resource,
110             String requestId, String apiVersion, String resourceId, String requestAction, String vnfType,
111             WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails, boolean replaceVnf) {
112         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
113         if (orchFlow.getFlowName().contains(SERVICE) || (orchFlow.getFlowName().contains(CONTROLLER)
114                 && (SERVICE).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
115             if (!replaceVnf) {
116                 workflowResourceIds.setServiceInstanceId(resourceId);
117             }
118             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.SERVICE, orchFlow, requestId,
119                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
120         } else if (orchFlow.getFlowName().contains(VNF)
121                 || (orchFlow.getFlowName().contains(CONTROLLER) && (VNF).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
122             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VNF, orchFlow, requestId, apiVersion,
123                     resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
124         } else if ((orchFlow.getFlowName().equalsIgnoreCase(HEALTH_CHECK))
125                 && (VNF).equalsIgnoreCase(orchFlow.getBpmnScope())) {
126             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VNF, orchFlow, requestId, apiVersion,
127                     resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
128         } else if ((orchFlow.getFlowName().equalsIgnoreCase(UPGRADE_CNF))
129                 && (VNF).equalsIgnoreCase(orchFlow.getBpmnScope())) {
130             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VNF, orchFlow, requestId, apiVersion,
131                     resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
132         } else if (orchFlow.getFlowName().contains(PNF)
133                 || (orchFlow.getFlowName().contains(CONTROLLER) && (PNF).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
134             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.PNF, orchFlow, requestId, apiVersion,
135                     resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
136         } else if (orchFlow.getFlowName().contains(NETWORK) && !orchFlow.getFlowName().contains(NETWORKCOLLECTION)) {
137             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.NETWORK, orchFlow, requestId,
138                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
139             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VIRTUAL_LINK, orchFlow, requestId,
140                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, true, false);
141         } else if (orchFlow.getFlowName().contains(VFMODULE) || (orchFlow.getFlowName().contains(CONTROLLER)
142                 && (VFMODULE).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
143             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VFMODULE, orchFlow, requestId,
144                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
145         } else if (orchFlow.getFlowName().contains(VOLUMEGROUP)) {
146             if (requestAction.equalsIgnoreCase(REPLACEINSTANCE)
147                     || requestAction.equalsIgnoreCase(REPLACEINSTANCERETAINASSIGNMENTS)) {
148                 logger.debug("Replacing workflow resource id by volume group id");
149                 resourceId = workflowResourceIds.getVolumeGroupId();
150             }
151             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VOLUMEGROUP, orchFlow, requestId,
152                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
153         } else if (orchFlow.getFlowName().contains(NETWORKCOLLECTION)) {
154             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.NETWORKCOLLECTION, orchFlow,
155                     requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
156                     false, false);
157         } else if (orchFlow.getFlowName().contains(CONFIGURATION)) {
158             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.CONFIGURATION, orchFlow, requestId,
159                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, true);
160         } else {
161             flowsToExecute
162                     .add(buildExecuteBuildingBlock(orchFlow, requestId, null, apiVersion, resourceId, requestAction,
163                             false, vnfType, workflowResourceIds, requestDetails, false, null, null, false, null));
164         }
165         return flowsToExecute;
166     }
167
168     protected List<Resource> getOnlyRootResourceList(List<Resource> resourceList) {
169         return resourceList.stream().filter(x -> countResourceOnTheResourceList(x, resourceList) == 1)
170                 .collect(Collectors.toList());
171     }
172
173     protected int countResourceOnTheResourceList(Resource resource, List<Resource> resourceList) {
174         int count = resourceList.stream()
175                 .mapToInt(x -> (x.equals(resource) ? 1 : 0) + countResourceOnTheResourceList(resource, x.getChildren()))
176                 .reduce(0, Integer::sum);
177         return count;
178     }
179
180     protected ExecuteBuildingBlock buildExecuteBuildingBlock(OrchestrationFlow orchFlow, String requestId,
181             Resource resource, String apiVersion, String resourceId, String requestAction, boolean aLaCarte,
182             String vnfType, WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails,
183             boolean isVirtualLink, String virtualLinkKey, String vnfcName, boolean isConfiguration,
184             ReplaceInstanceRelatedInformation replaceInfo) {
185
186         BuildingBlock buildingBlock =
187                 new BuildingBlock().setBpmnFlowName(orchFlow.getFlowName()).setMsoId(UUID.randomUUID().toString())
188                         .setIsVirtualLink(isVirtualLink).setVirtualLinkKey(virtualLinkKey)
189                         .setKey(Optional.ofNullable(resource).map(Resource::getResourceId).orElse(""));
190         Optional.ofNullable(orchFlow.getBpmnAction()).ifPresent(buildingBlock::setBpmnAction);
191         Optional.ofNullable(orchFlow.getBpmnScope()).ifPresent(buildingBlock::setBpmnScope);
192         String oldVolumeGroupName = "";
193         if (replaceInfo != null) {
194             oldVolumeGroupName = replaceInfo.getOldVolumeGroupName();
195         }
196         if (resource != null
197                 && (orchFlow.getFlowName().contains(VOLUMEGROUP) && (requestAction.equalsIgnoreCase(REPLACEINSTANCE)
198                         || requestAction.equalsIgnoreCase(REPLACEINSTANCERETAINASSIGNMENTS)))) {
199             logger.debug("Setting resourceId to volume group id for volume group flow on replace");
200             resourceId = workflowResourceIds.getVolumeGroupId();
201         }
202
203         ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock().setApiVersion(apiVersion)
204                 .setaLaCarte(aLaCarte).setRequestAction(requestAction).setResourceId(resourceId).setVnfType(vnfType)
205                 .setWorkflowResourceIds(workflowResourceIds).setRequestId(requestId).setBuildingBlock(buildingBlock)
206                 .setRequestDetails(requestDetails).setOldVolumeGroupName(oldVolumeGroupName);
207
208         if (resource != null && (isConfiguration || resource.getResourceType().equals(WorkflowType.CONFIGURATION))) {
209             ConfigurationResourceKeys configurationResourceKeys = getConfigurationResourceKeys(resource, vnfcName);
210             executeBuildingBlock.setConfigurationResourceKeys(configurationResourceKeys);
211         }
212         return executeBuildingBlock;
213     }
214
215     private void addBuildingBlockToExecuteBBList(List<ExecuteBuildingBlock> flowsToExecute, Resource resource,
216             WorkflowType workflowType, OrchestrationFlow orchFlow, String requestId, String apiVersion,
217             String resourceId, String requestAction, String vnfType, WorkflowResourceIds workflowResourceIds,
218             RequestDetails requestDetails, boolean isVirtualLink, boolean isConfiguration) {
219
220         if (resource == null || !resource.getResourceType().equals(workflowType))
221             return;
222         flowsToExecute.add(buildExecuteBuildingBlock(orchFlow, requestId, resource, apiVersion, resourceId,
223                 requestAction, false, vnfType, workflowResourceIds, requestDetails, isVirtualLink,
224                 resource.getVirtualLinkKey(), null, isConfiguration, null));
225     }
226
227     protected ConfigurationResourceKeys getConfigurationResourceKeys(Resource resource, String vnfcName) {
228         ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
229         Optional.ofNullable(vnfcName).ifPresent(configurationResourceKeys::setVnfcName);
230         configurationResourceKeys.setCvnfcCustomizationUUID(resource.getCvnfModuleCustomizationId());
231         configurationResourceKeys.setVfModuleCustomizationUUID(resource.getVfModuleCustomizationId());
232         configurationResourceKeys.setVnfResourceCustomizationUUID(resource.getVnfCustomizationId());
233         return configurationResourceKeys;
234     }
235 }