Merge "remove sub process error handling that silent successes"
[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  * 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
16  *
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  *
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=========================================================
25  */
26
27 package org.onap.so.bpmn.infrastructure.workflow.tasks;
28
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;
53
54 @Component
55 public class ExecuteBuildingBlockBuilder {
56
57     private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockBuilder.class);
58
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";
63
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,
67             boolean replaceVnf) {
68         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
69         for (OrchestrationFlow orchFlow : orchFlows) {
70             if (orchFlow.getFlowName().contains(SERVICE)) {
71                 if (!replaceVnf) {
72                     workflowResourceIds.setServiceInstanceId(resourceId);
73                 }
74                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.SERVICE, orchFlow, requestId,
75                         apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false,
76                         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,
81                         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,
86                         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,
91                         false);
92                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.VIRTUAL_LINK, orchFlow,
93                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
94                         true, false);
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()));
102                 } else {
103                     vfModuleResourcesSorted = sortVfModulesByBaseLast(resourceList.stream()
104                             .filter(x -> WorkflowType.VFMODULE == x.getResourceType()).collect(Collectors.toList()));
105                 }
106                 for (Resource resource : vfModuleResourcesSorted) {
107                     flowsToExecute.add(buildExecuteBuildingBlock(orchFlow, requestId, resource, apiVersion, resourceId,
108                             requestAction, false, vnfType, workflowResourceIds, requestDetails, false, null, null,
109                             false, null));
110                 }
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();
116                 }
117                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.VOLUMEGROUP, orchFlow,
118                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
119                         false, false);
120             } else if (orchFlow.getFlowName().contains(NETWORKCOLLECTION)) {
121                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.NETWORKCOLLECTION, orchFlow,
122                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
123                         false, false);
124             } else if (orchFlow.getFlowName().contains(CONFIGURATION)) {
125                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.CONFIGURATION, orchFlow,
126                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
127                         false, true);
128             } else {
129                 flowsToExecute
130                         .add(buildExecuteBuildingBlock(orchFlow, requestId, null, apiVersion, resourceId, requestAction,
131                                 false, vnfType, workflowResourceIds, requestDetails, false, null, null, false, null));
132             }
133         }
134         return flowsToExecute;
135     }
136
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) {
142
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();
152         }
153         if (resource != null
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();
158         }
159
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);
164
165         if (resource != null && (isConfiguration || resource.getResourceType().equals(WorkflowType.CONFIGURATION))) {
166             ConfigurationResourceKeys configurationResourceKeys = getConfigurationResourceKeys(resource, vnfcName);
167             executeBuildingBlock.setConfigurationResourceKeys(configurationResourceKeys);
168         }
169         return executeBuildingBlock;
170     }
171
172     protected List<Resource> sortVfModulesByBaseFirst(List<Resource> vfModuleResources) {
173         int count = 0;
174         for (Resource resource : vfModuleResources) {
175             if (resource.isBaseVfModule()) {
176                 Collections.swap(vfModuleResources, 0, count);
177                 break;
178             }
179             count++;
180         }
181         return vfModuleResources;
182     }
183
184     protected List<Resource> sortVfModulesByBaseLast(List<Resource> vfModuleResources) {
185         int count = 0;
186         for (Resource resource : vfModuleResources) {
187             if (resource.isBaseVfModule()) {
188                 Collections.swap(vfModuleResources, vfModuleResources.size() - 1, count);
189                 break;
190             }
191             count++;
192         }
193         return vfModuleResources;
194     }
195
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) {
200
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)));
205     }
206
207     protected 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;
214     }
215
216
217 }