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 * 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
19 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
29 package org.onap.so.bpmn.infrastructure.workflow.tasks;
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.*;
49 public class ExecuteBuildingBlockBuilder {
51 private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockBuilder.class);
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";
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);
66 List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
68 boolean ascendingOrder = requestAction.equals(CREATE_INSTANCE) || requestAction.equals(ASSIGN_INSTANCE)
69 || requestAction.equals(ACTIVATE_INSTANCE);
71 ExecutionPlan plan = ExecutionPlan.build(resourceList, ascendingOrder);
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())
80 logger.info("Flow: " + flowDetails);
81 buildExecuteBuildingBlockListPlan(orchFlow, plan, requestId, apiVersion, resourceId, requestAction, vnfType,
82 workflowResourceIds, requestDetails, replaceVnf);
85 plan.flushBlocksFromCache(flowsToExecute);
87 return flowsToExecute;
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) {
94 List<ExecuteBuildingBlock> mainFlows = buildExecuteBuildingBlockListRaw(flow, plan.getResource(), requestId,
95 apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, replaceVnf);
97 plan.pushBlockToCache(mainFlows);
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);
104 if (nestedGroup.getCacheSize() > 0)
105 plan.changeCurrentGroup(nestedGroup);
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()))) {
116 workflowResourceIds.setServiceInstanceId(resourceId);
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();
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,
157 } else if (orchFlow.getFlowName().contains(CONFIGURATION)) {
158 addBuildingBlockToExecuteBBList(flowsToExecute, resource, WorkflowType.CONFIGURATION, orchFlow, requestId,
159 apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false, true);
162 .add(buildExecuteBuildingBlock(orchFlow, requestId, null, apiVersion, resourceId, requestAction,
163 false, vnfType, workflowResourceIds, requestDetails, false, null, null, false, null));
165 return flowsToExecute;
168 protected List<Resource> getOnlyRootResourceList(List<Resource> resourceList) {
169 return resourceList.stream().filter(x -> countResourceOnTheResourceList(x, resourceList) == 1)
170 .collect(Collectors.toList());
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);
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) {
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();
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();
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);
208 if (resource != null && (isConfiguration || resource.getResourceType().equals(WorkflowType.CONFIGURATION))) {
209 ConfigurationResourceKeys configurationResourceKeys = getConfigurationResourceKeys(resource, vnfcName);
210 executeBuildingBlock.setConfigurationResourceKeys(configurationResourceKeys);
212 return executeBuildingBlock;
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) {
220 if (resource == null || !resource.getResourceType().equals(workflowType))
222 flowsToExecute.add(buildExecuteBuildingBlock(orchFlow, requestId, resource, apiVersion, resourceId,
223 requestAction, false, vnfType, workflowResourceIds, requestDetails, isVirtualLink,
224 resource.getVirtualLinkKey(), null, isConfiguration, null));
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;