Merge "Sonar issue - Refactor"
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / ControllerExecution.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019  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.flowspecific.tasks;
22
23 import java.util.Optional;
24 import org.onap.so.bpmn.common.BuildingBlockExecution;
25 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
26 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
27 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
28 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
29 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
30 import org.onap.so.client.exception.ExceptionBuilder;
31 import org.onap.so.db.catalog.beans.BBNameSelectionReference;
32 import org.onap.so.db.catalog.beans.VnfResourceCustomization;
33 import org.onap.so.db.catalog.client.CatalogDbClient;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Component;
38
39 @Component
40 public class ControllerExecution {
41     private static final Logger logger = LoggerFactory.getLogger(ControllerExecution.class);
42     private static final String CONTROLLER_ACTOR = "controllerActor";
43     private static final String BUILDING_BLOCK = "buildingBlock";
44     private static final String SCOPE = "scope";
45     private static final String ACTION = "action";
46     private static final String BBNAME = "bbName";
47     @Autowired
48     private ExceptionBuilder exceptionUtil;
49     @Autowired
50     private CatalogDbClient catalogDbClient;
51     @Autowired
52     private ExtractPojosForBB extractPojosForBB;
53
54     /**
55      * Setting Controller Actor, Scope and Action Variables in BuildingBlockExecution object
56      * 
57      * @param execution - BuildingBlockExecution object
58      */
59     public void setControllerActorScopeAction(BuildingBlockExecution execution) {
60         try {
61             GenericVnf genericVnf = extractPojosForBB.extractByKey(execution, ResourceKey.GENERIC_VNF_ID);
62             String modleUuid = genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid();
63             VnfResourceCustomization vnfResourceCustomization =
64                     catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(modleUuid);
65
66             // Fetching Controller Actor at VNF level if null then Controller Actor is set as "APPC"
67             String controllerActor = Optional.ofNullable(vnfResourceCustomization.getControllerActor()).orElse("APPC");
68             ExecuteBuildingBlock executeBuildingBlock = execution.getVariable(BUILDING_BLOCK);
69             BuildingBlock buildingBlock = executeBuildingBlock.getBuildingBlock();
70             String scope = Optional.ofNullable(buildingBlock.getBpmnScope()).orElseThrow(
71                     () -> new NullPointerException("BPMN Scope is NULL in the orchestration_flow_reference table "));
72             String action = Optional.ofNullable(buildingBlock.getBpmnAction()).orElseThrow(
73                     () -> new NullPointerException("BPMN Action is NULL in the orchestration_flow_reference table "));
74             execution.setVariable(SCOPE, scope);
75             execution.setVariable(ACTION, action);
76             execution.setVariable(CONTROLLER_ACTOR, controllerActor);
77             logger.debug("Executing Controller Execution for ControllerActor: {}, Scope: {} , Action: {}",
78                     controllerActor, scope, action);
79
80         } catch (Exception ex) {
81             logger.error("An exception occurred while fetching Controller Actor,Scope and Action ", ex);
82             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
83         }
84     }
85
86     /**
87      * Selecting bbName from BBNameSelectionReference and setting the value in a variable of BuildingBlockExecution
88      * 
89      * @param execution - BuildingBlockExecution object
90      */
91     public void selectBB(BuildingBlockExecution execution) {
92         try {
93
94             String controllerActor = execution.getVariable(CONTROLLER_ACTOR);
95             String action = Optional.of((String) execution.getVariable(ACTION)).get();
96             String scope = Optional.of((String) execution.getVariable(SCOPE)).get();
97             BBNameSelectionReference bbNameSelectionReference =
98                     catalogDbClient.getBBNameSelectionReference(controllerActor, scope, action);
99             String bbName = bbNameSelectionReference.getBbName();
100             execution.setVariable(BBNAME, bbName);
101             logger.debug(" Executing {} BPMN", bbName);
102         } catch (Exception ex) {
103             logger.error("An exception occurred while getting bbname from catalogdb ", ex);
104             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
105
106         }
107
108     }
109 }