Merge "bump the so version to 1.7.4"
[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.PnfResourceCustomization;
33 import org.onap.so.db.catalog.beans.VfModuleCustomization;
34 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
35 import org.onap.so.db.catalog.client.CatalogDbClient;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38 import org.springframework.util.CollectionUtils;
39
40 @Component
41 public class SkipCDSBuildingBlockListener implements FlowManipulator {
42
43     @Autowired
44     private CatalogDbClient catalogDbClient;
45
46     private Set<String> vnfActions =
47             new HashSet<String>(Arrays.asList("config-assign", "config-deploy", "VnfConfigAssign", "VnfConfigDeploy"));
48
49     private Set<String> vFModuleAction =
50             new HashSet<String>(Arrays.asList("VfModuleConfigAssign", "VfModuleConfigDeploy"));
51
52     private Set<String> pnfActions =
53             new HashSet<>(Arrays.asList("config-assign", "config-deploy", "PnfConfigAssign", "PnfConfigDeploy"));
54
55     @Override
56     public boolean shouldRunFor(String currentBBName, boolean isFirst, BuildingBlockExecution execution) {
57
58         return "ControllerExecutionBB".equals(currentBBName);
59     }
60
61     /**
62      * Skip the CDS Building block according to the Skip Flag.
63      *
64      * @param flowsToExecute - List of ExecuteBuildingBlock object.
65      * @param execution - BuildingBlockExecution object
66      * @param currentBB - ExecuteBuildingBlock object
67      *
68      */
69     @Override
70     public void run(List<ExecuteBuildingBlock> flowsToExecute, ExecuteBuildingBlock currentBB,
71             BuildingBlockExecution execution) {
72         String customizationUUID = currentBB.getBuildingBlock().getKey();
73
74         if (Strings.isEmpty(customizationUUID)) {
75             return;
76         }
77
78         if (currentBB.getBuildingBlock().getBpmnScope().equalsIgnoreCase("VNF")
79                 && containsIgnoreCaseAction(currentBB, vnfActions)) {
80             List<VnfResourceCustomization> vnfResourceCustomizations =
81                     catalogDbClient.getVnfResourceCustomizationByModelUuid(
82                             currentBB.getRequestDetails().getModelInfo().getModelUuid());
83             if (!CollectionUtils.isEmpty(vnfResourceCustomizations)) {
84                 VnfResourceCustomization vrc = catalogDbClient.findVnfResourceCustomizationInList(customizationUUID,
85                         vnfResourceCustomizations);
86                 if (null != vrc) {
87                     boolean skipConfigVNF = vrc.isSkipPostInstConf();
88                     currentSequenceSkipCheck(execution, skipConfigVNF);
89                 }
90
91             }
92         } else if (currentBB.getBuildingBlock().getBpmnScope().equalsIgnoreCase("VFModule")
93                 && containsIgnoreCaseAction(currentBB, vFModuleAction)) {
94
95             VfModuleCustomization vfc =
96                     catalogDbClient.getVfModuleCustomizationByModelCuztomizationUUID(customizationUUID);
97
98             if (null != vfc) {
99                 boolean skipVfModule = vfc.isSkipPostInstConf();
100                 currentSequenceSkipCheck(execution, skipVfModule);
101             }
102
103         } else if (currentBB.getBuildingBlock().getBpmnScope().equalsIgnoreCase("PNF")
104                 && containsIgnoreCaseAction(currentBB, pnfActions)) {
105             PnfResourceCustomization pnfResourceCustomization =
106                     catalogDbClient.getPnfResourceCustomizationByModelCustomizationUUID(customizationUUID);
107
108             if (null != pnfResourceCustomization) {
109                 boolean skipConfigPNF = pnfResourceCustomization.isSkipPostInstConf();
110                 currentSequenceSkipCheck(execution, skipConfigPNF);
111             }
112         }
113     }
114
115     private boolean containsIgnoreCaseAction(ExecuteBuildingBlock currentBB, Set<String> actions) {
116         return actions.stream().filter(action -> action.equalsIgnoreCase(currentBB.getBuildingBlock().getBpmnAction()))
117                 .findFirst().isPresent();
118     }
119
120
121     private void currentSequenceSkipCheck(BuildingBlockExecution execution, boolean skipModule) {
122         if (skipModule) {
123             int currentSequence = execution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
124             execution.setVariable(BBConstants.G_CURRENT_SEQUENCE, currentSequence + 1);
125         }
126     }
127
128 }