dc77ea1f0caa676acff15ff8261e9c029783845d
[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.onap.so.bpmn.common.BBConstants;
28 import org.onap.so.bpmn.common.BuildingBlockExecution;
29 import org.onap.so.bpmn.common.listener.flowmanipulator.PreFlowManipulator;
30 import org.onap.so.bpmn.infrastructure.workflow.tasks.Resource;
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.Service;
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
42 @Component
43 public class SkipCDSBuildingBlockListener implements PreFlowManipulator {
44
45     private static Logger logger = LoggerFactory.getLogger(SkipCDSBuildingBlockListener.class);
46
47     @Autowired
48     private CatalogDbClient catalogDbClient;
49
50     private Set<String> vnfActions =
51             new HashSet<String>(Arrays.asList("config-assign", "config-deploy", "VnfConfigAssign", "VnfConfigDeploy"));
52
53     private Set<String> vFModuleAction =
54             new HashSet<String>(Arrays.asList("VfModuleConfigAssign", "VfModuleConfigDeploy"));
55
56     private Set<String> pnfActions =
57             new HashSet<>(Arrays.asList("config-assign", "config-deploy", "PnfConfigAssign", "PnfConfigDeploy"));
58
59     @Override
60     public boolean shouldRunFor(String currentBBName, boolean isFirst, BuildingBlockExecution execution) {
61
62         return "ControllerExecutionBB".equals(currentBBName);
63     }
64
65     /**
66      * Skip the CDS Building block according to the Skip Flag.
67      *
68      * @param flowsToExecute - List of ExecuteBuildingBlock object.
69      * @param execution - BuildingBlockExecution object
70      * @param currentBB - ExecuteBuildingBlock object
71      *
72      */
73     @Override
74     public void run(List<ExecuteBuildingBlock> flowsToExecute, ExecuteBuildingBlock currentBB,
75             BuildingBlockExecution execution) {
76         String resourceKey = currentBB.getBuildingBlock().getKey();
77         List<Resource> resources = execution.getVariable("resources");
78         Resource resource = resources.stream().filter(r -> resourceKey.equals(r.getResourceId())).findFirst()
79                 .orElseThrow(() -> new IllegalArgumentException("Resource not found for key:" + resourceKey));
80
81         String scope = currentBB.getBuildingBlock().getBpmnScope();
82
83         if ("SERVICE".equalsIgnoreCase(scope)) {
84             Service service = catalogDbClient.getServiceByID(resource.getModelVersionId());
85             currentSequenceSkipCheck(execution, service.getSkipPostInstConf());
86         } else if ("VNF".equalsIgnoreCase(scope) && containsIgnoreCaseAction(currentBB, vnfActions)) {
87             VnfResourceCustomization vrc = catalogDbClient
88                     .getVnfResourceCustomizationByModelCustomizationUUID(resource.getModelCustomizationId());
89             if (vrc != null) {
90                 logger.debug("getSkipPostInstConf value: " + vrc.getSkipPostInstConf());
91                 boolean skipConfigVNF = vrc.getSkipPostInstConf();
92                 currentSequenceSkipCheck(execution, skipConfigVNF);
93             }
94         } else if (currentBB.getBuildingBlock().getBpmnScope().equalsIgnoreCase("VFModule")
95                 && containsIgnoreCaseAction(currentBB, vFModuleAction)) {
96             VfModuleCustomization vfc = catalogDbClient
97                     .getVfModuleCustomizationByModelCuztomizationUUID(resource.getModelCustomizationId());
98             if (null != vfc) {
99                 logger.debug("getSkipPostInstConf value: " + vfc.getSkipPostInstConf().booleanValue());
100                 boolean skipVfModule = vfc.getSkipPostInstConf();
101                 currentSequenceSkipCheck(execution, skipVfModule);
102             }
103         } else if (currentBB.getBuildingBlock().getBpmnScope().equalsIgnoreCase("PNF")
104                 && containsIgnoreCaseAction(currentBB, pnfActions)) {
105             PnfResourceCustomization pnfResourceCustomization = catalogDbClient
106                     .getPnfResourceCustomizationByModelCustomizationUUID(resource.getModelCustomizationId());
107
108             if (null != pnfResourceCustomization) {
109                 logger.debug("getSkipPostInstConf value: " + pnfResourceCustomization.getSkipPostInstConf());
110                 boolean skipConfigPNF = pnfResourceCustomization.getSkipPostInstConf();
111                 currentSequenceSkipCheck(execution, skipConfigPNF);
112             }
113         }
114     }
115
116     private boolean containsIgnoreCaseAction(ExecuteBuildingBlock currentBB, Set<String> actions) {
117         return actions.stream().filter(action -> action.equalsIgnoreCase(currentBB.getBuildingBlock().getBpmnAction()))
118                 .findFirst().isPresent();
119     }
120
121
122     private void currentSequenceSkipCheck(BuildingBlockExecution execution, boolean skipModule) {
123         if (skipModule) {
124             int currentSequence = execution.getVariable(BBConstants.G_CURRENT_SEQUENCE);
125             execution.setVariable(BBConstants.G_CURRENT_SEQUENCE, currentSequence + 1);
126         }
127     }
128
129 }