Merge "Subnet Type update for respective TN domain"
[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
59     protected List<ExecuteBuildingBlock> buildExecuteBuildingBlockList(List<OrchestrationFlow> orchFlows,
60             List<Resource> originalResourceList, String requestId, String apiVersion, String resourceId,
61             String requestAction, String vnfType, WorkflowResourceIds workflowResourceIds,
62             RequestDetails requestDetails, boolean replaceVnf) {
63         List<Resource> resourceList = getOnlyRootResourceList(originalResourceList);
64
65         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
66
67         boolean ascendingOrder = requestAction.equals(CREATE_INSTANCE) || requestAction.equals(ASSIGN_INSTANCE)
68                 || requestAction.equals(ACTIVATE_INSTANCE);
69
70         ExecutionPlan plan = ExecutionPlan.build(resourceList, ascendingOrder);
71
72         logger.info("Orchestration Flows");
73         for (OrchestrationFlow orchFlow : orchFlows) {
74             String flowDetails = new ToStringBuilder(this).append("id", orchFlow.getId())
75                     .append("action", orchFlow.getAction()).append("sequenceNumber", orchFlow.getSequenceNumber())
76                     .append("flowName", orchFlow.getFlowName()).append("flowVersion", orchFlow.getFlowVersion())
77                     .append("bpmnAction", orchFlow.getBpmnAction()).append("bpmnScope", orchFlow.getBpmnScope())
78                     .toString();
79             logger.info("Flow: " + flowDetails);
80             buildExecuteBuildingBlockListPlan(orchFlow, plan, requestId, apiVersion, resourceId, requestAction, vnfType,
81                     workflowResourceIds, requestDetails, replaceVnf);
82         }
83
84         plan.flushBlocksFromCache(flowsToExecute);
85
86         return flowsToExecute;
87     }
88
89     protected void buildExecuteBuildingBlockListPlan(OrchestrationFlow flow, ExecutionPlan plan, String requestId,
90             String apiVersion, String resourceId, String requestAction, String vnfType,
91             WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails, boolean replaceVnf) {
92
93         List<ExecuteBuildingBlock> mainFlows = buildExecuteBuildingBlockListRaw(flow, plan.getResource(), requestId,
94                 apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, replaceVnf);
95
96         plan.pushBlockToCache(mainFlows);
97
98         for (ExecutionGroup nestedGroup : plan.getNestedExecutions()) {
99             for (ExecutionPlan nestedPlan : nestedGroup.getNestedExecutions()) {
100                 buildExecuteBuildingBlockListPlan(flow, nestedPlan, requestId, apiVersion, resourceId, requestAction,
101                         vnfType, workflowResourceIds, requestDetails, replaceVnf);
102             }
103             if (nestedGroup.getCacheSize() > 0)
104                 plan.changeCurrentGroup(nestedGroup);
105         }
106     }
107
108     private List<ExecuteBuildingBlock> buildExecuteBuildingBlockListRaw(OrchestrationFlow orchFlow, Resource resource,
109             String requestId, String apiVersion, String resourceId, String requestAction, String vnfType,
110             WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails, boolean replaceVnf) {
111         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
112         if (orchFlow.getFlowName().contains(SERVICE) || (orchFlow.getFlowName().contains(CONTROLLER)
113                 && (SERVICE).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
114             if (!replaceVnf) {
115                 workflowResourceIds.setServiceInstanceId(resourceId);
116             }
117             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.SERVICE, orchFlow, requestId,
118                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
119         } else if (orchFlow.getFlowName().contains(VNF)
120                 || (orchFlow.getFlowName().contains(CONTROLLER) && (VNF).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
121             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VNF, orchFlow, requestId, apiVersion,
122                     resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
123         } else if ((orchFlow.getFlowName().equalsIgnoreCase(HEALTH_CHECK))
124                 && (VNF).equalsIgnoreCase(orchFlow.getBpmnScope())) {
125             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VNF, orchFlow, requestId, apiVersion,
126                     resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
127         } else if (orchFlow.getFlowName().contains(PNF)
128                 || (orchFlow.getFlowName().contains(CONTROLLER) && (PNF).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
129             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.PNF, orchFlow, requestId, apiVersion,
130                     resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
131         } else if (orchFlow.getFlowName().contains(NETWORK) && !orchFlow.getFlowName().contains(NETWORKCOLLECTION)) {
132             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.NETWORK, orchFlow, requestId,
133                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
134             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VIRTUAL_LINK, orchFlow, requestId,
135                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, true, false);
136         } else if (orchFlow.getFlowName().contains(VFMODULE) || (orchFlow.getFlowName().contains(CONTROLLER)
137                 && (VFMODULE).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
138             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VFMODULE, orchFlow, requestId,
139                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
140         } else if (orchFlow.getFlowName().contains(VOLUMEGROUP)) {
141             if (requestAction.equalsIgnoreCase(REPLACEINSTANCE)
142                     || requestAction.equalsIgnoreCase(REPLACEINSTANCERETAINASSIGNMENTS)) {
143                 logger.debug("Replacing workflow resource id by volume group id");
144                 resourceId = workflowResourceIds.getVolumeGroupId();
145             }
146             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.VOLUMEGROUP, orchFlow, requestId,
147                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, false);
148         } else if (orchFlow.getFlowName().contains(NETWORKCOLLECTION)) {
149             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.NETWORKCOLLECTION, orchFlow,
150                     requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
151                     false, false);
152         } else if (orchFlow.getFlowName().contains(CONFIGURATION)) {
153             addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.CONFIGURATION, orchFlow, requestId,
154                     apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, true);
155         } else {
156             flowsToExecute
157                     .add(buildExecuteBuildingBlock(orchFlow, requestId, null, apiVersion, resourceId, requestAction,
158                             false, vnfType, workflowResourceIds, requestDetails, false, null, null, false, null));
159         }
160         return flowsToExecute;
161     }
162
163     protected List<Resource> getOnlyRootResourceList(List<Resource> resourceList) {
164         return resourceList.stream().filter(x -> countResourceOnTheResourceList(x, resourceList) == 1)
165                 .collect(Collectors.toList());
166     }
167
168     protected int countResourceOnTheResourceList(Resource resource, List<Resource> resourceList) {
169         int count = resourceList.stream()
170                 .mapToInt(x -> (x.equals(resource) ? 1 : 0) + countResourceOnTheResourceList(resource, x.getChildren()))
171                 .reduce(0, Integer::sum);
172         return count;
173     }
174
175     protected ExecuteBuildingBlock buildExecuteBuildingBlock(OrchestrationFlow orchFlow, String requestId,
176             Resource resource, String apiVersion, String resourceId, String requestAction, boolean aLaCarte,
177             String vnfType, WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails,
178             boolean isVirtualLink, String virtualLinkKey, String vnfcName, boolean isConfiguration,
179             ReplaceInstanceRelatedInformation replaceInfo) {
180
181         BuildingBlock buildingBlock =
182                 new BuildingBlock().setBpmnFlowName(orchFlow.getFlowName()).setMsoId(UUID.randomUUID().toString())
183                         .setIsVirtualLink(isVirtualLink).setVirtualLinkKey(virtualLinkKey)
184                         .setKey(Optional.ofNullable(resource).map(Resource::getResourceId).orElse(""));
185         Optional.ofNullable(orchFlow.getBpmnAction()).ifPresent(buildingBlock::setBpmnAction);
186         Optional.ofNullable(orchFlow.getBpmnScope()).ifPresent(buildingBlock::setBpmnScope);
187         String oldVolumeGroupName = "";
188         if (replaceInfo != null) {
189             oldVolumeGroupName = replaceInfo.getOldVolumeGroupName();
190         }
191         if (resource != null
192                 && (orchFlow.getFlowName().contains(VOLUMEGROUP) && (requestAction.equalsIgnoreCase(REPLACEINSTANCE)
193                         || requestAction.equalsIgnoreCase(REPLACEINSTANCERETAINASSIGNMENTS)))) {
194             logger.debug("Setting resourceId to volume group id for volume group flow on replace");
195             resourceId = workflowResourceIds.getVolumeGroupId();
196         }
197
198         ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock().setApiVersion(apiVersion)
199                 .setaLaCarte(aLaCarte).setRequestAction(requestAction).setResourceId(resourceId).setVnfType(vnfType)
200                 .setWorkflowResourceIds(workflowResourceIds).setRequestId(requestId).setBuildingBlock(buildingBlock)
201                 .setRequestDetails(requestDetails).setOldVolumeGroupName(oldVolumeGroupName);
202
203         if (resource != null && (isConfiguration || resource.getResourceType().equals(WorkflowType.CONFIGURATION))) {
204             ConfigurationResourceKeys configurationResourceKeys = getConfigurationResourceKeys(resource, vnfcName);
205             executeBuildingBlock.setConfigurationResourceKeys(configurationResourceKeys);
206         }
207         return executeBuildingBlock;
208     }
209
210     private void addBuildingBlockToExecuteBBList(List<ExecuteBuildingBlock> flowsToExecute, Resource resource,
211             WorkflowType workflowType, OrchestrationFlow orchFlow, String requestId, String apiVersion,
212             String resourceId, String requestAction, String vnfType, WorkflowResourceIds workflowResourceIds,
213             RequestDetails requestDetails, boolean isVirtualLink, boolean isConfiguration) {
214
215         if (resource == null || !resource.getResourceType().equals(workflowType))
216             return;
217         flowsToExecute.add(buildExecuteBuildingBlock(orchFlow, requestId, resource, apiVersion, resourceId,
218                 requestAction, false, vnfType, workflowResourceIds, requestDetails, isVirtualLink,
219                 resource.getVirtualLinkKey(), null, isConfiguration, null));
220     }
221
222     protected ConfigurationResourceKeys getConfigurationResourceKeys(Resource resource, String vnfcName) {
223         ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
224         Optional.ofNullable(vnfcName).ifPresent(configurationResourceKeys::setVnfcName);
225         configurationResourceKeys.setCvnfcCustomizationUUID(resource.getCvnfModuleCustomizationId());
226         configurationResourceKeys.setVfModuleCustomizationUUID(resource.getVfModuleCustomizationId());
227         configurationResourceKeys.setVnfResourceCustomizationUUID(resource.getVnfCustomizationId());
228         return configurationResourceKeys;
229     }
230 }