Merge "Fix CreateSDNCNetworkResource.updateResourceInput() for BBS use case"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / workflow / tasks / listeners / SkipCDSBuildingBlockListener.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2020  Tech Mahindra
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.workflow.tasks.listeners;
22
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import org.apache.logging.log4j.util.Strings;
28 import org.onap.so.bpmn.common.BBConstants;
29 import org.onap.so.bpmn.common.BuildingBlockExecution;
30 import org.onap.so.bpmn.common.listener.flowmanipulator.FlowManipulator;
31 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
32 import org.onap.so.db.catalog.beans.VfModuleCustomization;
33 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
34 import org.onap.so.db.catalog.client.CatalogDbClient;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37 import org.springframework.util.CollectionUtils;
38
39 @Component
40 public class SkipCDSBuildingBlockListener implements FlowManipulator {
41
42     @Autowired
43     private CatalogDbClient catalogDbClient;
44
45     private Set<String> vnfActions =
46             new HashSet<String>(Arrays.asList("config-assign", "config-deploy", "VnfConfigAssign", "VnfConfigDeploy"));
47
48     private Set<String> vFModuleAction =
49             new HashSet<String>(Arrays.asList("VfModuleConfigAssign", "VfModuleConfigDeploy"));
50
51     @Override
52     public boolean shouldRunFor(String currentBBName, boolean isFirst, BuildingBlockExecution execution) {
53
54         return "ControllerExecutionBB".equals(currentBBName);
55     }
56
57     /**
58      * Skip the CDS Building block according to the Skip Flag.
59      *
60      * @param flowsToExecute - List of ExecuteBuildingBlock object.
61      * @param execution - BuildingBlockExecution object
62      * @param currentBB - ExecuteBuildingBlock object
63      *
64      */
65     @Override
66     public void run(List<ExecuteBuildingBlock> flowsToExecute, ExecuteBuildingBlock currentBB,
67             BuildingBlockExecution execution) {
68         String customizationUUID = currentBB.getBuildingBlock().getKey();
69
70         if (Strings.isEmpty(customizationUUID)) {
71             return;
72         }
73
74         if (currentBB.getBuildingBlock().getBpmnScope().equalsIgnoreCase("VNF")
75                 && containsIgnoreCaseAction(currentBB, vnfActions)) {
76             List<VnfResourceCustomization> vnfResourceCustomizations =
77                     catalogDbClient.getVnfResourceCustomizationByModelUuid(
78                             currentBB.getRequestDetails().getModelInfo().getModelUuid());
79             if (!CollectionUtils.isEmpty(vnfResourceCustomizations)) {
80                 VnfResourceCustomization vrc = catalogDbClient.findVnfResourceCustomizationInList(customizationUUID,
81                         vnfResourceCustomizations);
82                 if (null != vrc) {
83                     boolean skipConfigVNF = vrc.isSkipPostInstConf();
84                     currentSequenceSkipCheck(execution, skipConfigVNF);
85                 }
86
87             }
88         } else if (currentBB.getBuildingBlock().getBpmnScope().equalsIgnoreCase("VFModule")
89                 && containsIgnoreCaseAction(currentBB, vFModuleAction)) {
90
91             VfModuleCustomization vfc =
92                     catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(customizationUUID);
93
94             if (null != vfc) {
95                 boolean skipVfModule = vfc.isSkipPostInstConf();
96                 currentSequenceSkipCheck(execution, skipVfModule);
97             }
98         }
99
100
101     }
102
103     private boolean containsIgnoreCaseAction(ExecuteBuildingBlock currentBB, Set<String> actions) {
104         return actions.stream().filter(action -> action.equalsIgnoreCase(currentBB.getBuildingBlock().getBpmnAction()))
105                 .findFirst().isPresent();
106     }
107
108
109     private void currentSequenceSkipCheck(BuildingBlockExecution execution, boolean skipModule) {
110         if (skipModule) {
111             int currentSequence = execution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
112             execution.setVariable(BBConstants.G_CURRENT_SEQUENCE, currentSequence + 1);
113         }
114     }
115
116 }