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