Add VNF-Macro-Create and VNF-Macro-Delete
[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 java.util.Comparator;
30 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
31 import org.onap.so.bpmn.servicedecomposition.entities.ConfigurationResourceKeys;
32 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
33 import org.onap.so.bpmn.servicedecomposition.entities.WorkflowResourceIds;
34 import org.onap.so.db.catalog.beans.macro.OrchestrationFlow;
35 import org.onap.so.serviceinstancebeans.RequestDetails;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39 import java.util.ArrayList;
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.ACTIVATE_INSTANCE;
45 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.ASSIGN_INSTANCE;
46 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.CONFIGURATION;
47 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.CONTROLLER;
48 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.CREATE_INSTANCE;
49 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.NETWORKCOLLECTION;
50 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.REPLACEINSTANCE;
51 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.REPLACEINSTANCERETAINASSIGNMENTS;
52 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.SERVICE;
53 import static org.onap.so.bpmn.infrastructure.workflow.tasks.WorkflowActionConstants.VOLUMEGROUP;
54
55 @Component
56 public class ExecuteBuildingBlockBuilder {
57
58     private static final Logger logger = LoggerFactory.getLogger(ExecuteBuildingBlockBuilder.class);
59
60     private static final String VNF = "Vnf";
61     private static final String PNF = "Pnf";
62     private static final String VFMODULE = "VfModule";
63     private static final String NETWORK = "Network";
64
65     protected List<ExecuteBuildingBlock> buildExecuteBuildingBlockList(List<OrchestrationFlow> orchFlows,
66             List<Resource> resourceList, String requestId, String apiVersion, String resourceId, String requestAction,
67             String vnfType, WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails,
68             boolean replaceVnf) {
69         List<ExecuteBuildingBlock> flowsToExecute = new ArrayList<>();
70         for (OrchestrationFlow orchFlow : orchFlows) {
71             if (orchFlow.getFlowName().contains(SERVICE)) {
72                 if (!replaceVnf) {
73                     workflowResourceIds.setServiceInstanceId(resourceId);
74                 }
75                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.SERVICE, orchFlow, requestId,
76                         apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false,
77                         false);
78             } else if (orchFlow.getFlowName().contains(VNF) || (orchFlow.getFlowName().contains(CONTROLLER)
79                     && (VNF).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
80                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.VNF, orchFlow, requestId,
81                         apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false,
82                         false);
83             } else if (orchFlow.getFlowName().contains(PNF) || (orchFlow.getFlowName().contains(CONTROLLER)
84                     && (PNF).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
85                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.PNF, orchFlow, requestId,
86                         apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false,
87                         false);
88             } else if (orchFlow.getFlowName().contains(NETWORK)
89                     && !orchFlow.getFlowName().contains(NETWORKCOLLECTION)) {
90                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.NETWORK, orchFlow, requestId,
91                         apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails, false,
92                         false);
93                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.VIRTUAL_LINK, orchFlow,
94                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
95                         true, false);
96             } else if (orchFlow.getFlowName().contains(VFMODULE) || (orchFlow.getFlowName().contains(CONTROLLER)
97                     && (VFMODULE).equalsIgnoreCase(orchFlow.getBpmnScope()))) {
98                 Comparator<Resource> resourceComparator;
99                 if (requestAction.equals(CREATE_INSTANCE) || requestAction.equals(ASSIGN_INSTANCE)
100                         || requestAction.equals(ACTIVATE_INSTANCE)) {
101                     resourceComparator = Resource.sortBaseFirst;
102                 } else {
103                     resourceComparator = Resource.sortBaseLast;
104                 }
105                 List<Resource> vfModuleResourcesSorted =
106                         resourceList.stream().filter(x -> WorkflowType.VFMODULE == x.getResourceType())
107                                 .sorted(resourceComparator).collect(Collectors.toList());
108
109                 for (Resource resource : vfModuleResourcesSorted) {
110                     flowsToExecute.add(buildExecuteBuildingBlock(orchFlow, requestId, resource, apiVersion, resourceId,
111                             requestAction, false, vnfType, workflowResourceIds, requestDetails, false, null, null,
112                             false, null));
113                 }
114             } else if (orchFlow.getFlowName().contains(VOLUMEGROUP)) {
115                 if (requestAction.equalsIgnoreCase(REPLACEINSTANCE)
116                         || requestAction.equalsIgnoreCase(REPLACEINSTANCERETAINASSIGNMENTS)) {
117                     logger.debug("Replacing workflow resource id by volume group id");
118                     resourceId = workflowResourceIds.getVolumeGroupId();
119                 }
120                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.VOLUMEGROUP, orchFlow,
121                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
122                         false, false);
123             } else if (orchFlow.getFlowName().contains(NETWORKCOLLECTION)) {
124                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.NETWORKCOLLECTION, orchFlow,
125                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
126                         false, false);
127             } else if (orchFlow.getFlowName().contains(CONFIGURATION)) {
128                 addBuildingBlockToExecuteBBList(flowsToExecute, resourceList, WorkflowType.CONFIGURATION, orchFlow,
129                         requestId, apiVersion, resourceId, requestAction, vnfType, workflowResourceIds, requestDetails,
130                         false, true);
131             } else {
132                 flowsToExecute
133                         .add(buildExecuteBuildingBlock(orchFlow, requestId, null, apiVersion, resourceId, requestAction,
134                                 false, vnfType, workflowResourceIds, requestDetails, false, null, null, false, null));
135             }
136         }
137         return flowsToExecute;
138     }
139
140     protected ExecuteBuildingBlock buildExecuteBuildingBlock(OrchestrationFlow orchFlow, String requestId,
141             Resource resource, String apiVersion, String resourceId, String requestAction, boolean aLaCarte,
142             String vnfType, WorkflowResourceIds workflowResourceIds, RequestDetails requestDetails,
143             boolean isVirtualLink, String virtualLinkKey, String vnfcName, boolean isConfiguration,
144             ReplaceInstanceRelatedInformation replaceInfo) {
145
146         BuildingBlock buildingBlock =
147                 new BuildingBlock().setBpmnFlowName(orchFlow.getFlowName()).setMsoId(UUID.randomUUID().toString())
148                         .setIsVirtualLink(isVirtualLink).setVirtualLinkKey(virtualLinkKey)
149                         .setKey(Optional.ofNullable(resource).map(Resource::getResourceId).orElse(""));
150         Optional.ofNullable(orchFlow.getBpmnAction()).ifPresent(buildingBlock::setBpmnAction);
151         Optional.ofNullable(orchFlow.getBpmnScope()).ifPresent(buildingBlock::setBpmnScope);
152         String oldVolumeGroupName = "";
153         if (replaceInfo != null) {
154             oldVolumeGroupName = replaceInfo.getOldVolumeGroupName();
155         }
156         if (resource != null
157                 && (orchFlow.getFlowName().contains(VOLUMEGROUP) && (requestAction.equalsIgnoreCase(REPLACEINSTANCE)
158                         || requestAction.equalsIgnoreCase(REPLACEINSTANCERETAINASSIGNMENTS)))) {
159             logger.debug("Setting resourceId to volume group id for volume group flow on replace");
160             resourceId = workflowResourceIds.getVolumeGroupId();
161         }
162
163         ExecuteBuildingBlock executeBuildingBlock = new ExecuteBuildingBlock().setApiVersion(apiVersion)
164                 .setaLaCarte(aLaCarte).setRequestAction(requestAction).setResourceId(resourceId).setVnfType(vnfType)
165                 .setWorkflowResourceIds(workflowResourceIds).setRequestId(requestId).setBuildingBlock(buildingBlock)
166                 .setRequestDetails(requestDetails).setOldVolumeGroupName(oldVolumeGroupName);
167
168         if (resource != null && (isConfiguration || resource.getResourceType().equals(WorkflowType.CONFIGURATION))) {
169             ConfigurationResourceKeys configurationResourceKeys = getConfigurationResourceKeys(resource, vnfcName);
170             executeBuildingBlock.setConfigurationResourceKeys(configurationResourceKeys);
171         }
172         return executeBuildingBlock;
173     }
174
175     private void addBuildingBlockToExecuteBBList(List<ExecuteBuildingBlock> flowsToExecute, List<Resource> resourceList,
176             WorkflowType workflowType, OrchestrationFlow orchFlow, String requestId, String apiVersion,
177             String resourceId, String requestAction, String vnfType, WorkflowResourceIds workflowResourceIds,
178             RequestDetails requestDetails, boolean isVirtualLink, boolean isConfiguration) {
179
180         resourceList.stream().filter(resource -> resource.getResourceType().equals(workflowType))
181                 .forEach(resource -> flowsToExecute.add(buildExecuteBuildingBlock(orchFlow, requestId, resource,
182                         apiVersion, resourceId, requestAction, false, vnfType, workflowResourceIds, requestDetails,
183                         isVirtualLink, resource.getVirtualLinkKey(), null, isConfiguration, null)));
184     }
185
186     protected ConfigurationResourceKeys getConfigurationResourceKeys(Resource resource, String vnfcName) {
187         ConfigurationResourceKeys configurationResourceKeys = new ConfigurationResourceKeys();
188         Optional.ofNullable(vnfcName).ifPresent(configurationResourceKeys::setVnfcName);
189         configurationResourceKeys.setCvnfcCustomizationUUID(resource.getCvnfModuleCustomizationId());
190         configurationResourceKeys.setVfModuleCustomizationUUID(resource.getVfModuleCustomizationId());
191         configurationResourceKeys.setVnfResourceCustomizationUUID(resource.getVnfCustomizationId());
192         return configurationResourceKeys;
193     }
194
195
196 }