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