Merge "Issue-ID: SO-2966 optimization in git clone using --depth"
[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.bbobjects.Pnf;
27 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
28 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
29 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
30 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
31 import org.onap.so.client.exception.BBObjectNotFoundException;
32 import org.onap.so.client.exception.ExceptionBuilder;
33 import org.onap.so.db.catalog.beans.BBNameSelectionReference;
34 import org.onap.so.db.catalog.beans.PnfResourceCustomization;
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 static org.onap.so.client.cds.PayloadConstants.PRC_BLUEPRINT_NAME;
42 import static org.onap.so.client.cds.PayloadConstants.PRC_BLUEPRINT_VERSION;
43
44
45 @Component
46 public class ControllerExecution {
47     private static final Logger logger = LoggerFactory.getLogger(ControllerExecution.class);
48     private static final String CONTROLLER_ACTOR = "actor";
49     private static final String BUILDING_BLOCK = "buildingBlock";
50     private static final String SCOPE = "scope";
51     private static final String ACTION = "action";
52     private static final String BBNAME = "bbName";
53     private static final String MSO_REQUEST_ID = "msoRequestId";
54
55     @Autowired
56     private ExceptionBuilder exceptionUtil;
57     @Autowired
58     private CatalogDbClient catalogDbClient;
59     @Autowired
60     private ExtractPojosForBB extractPojosForBB;
61
62     /**
63      * Setting Controller Actor, Scope and Action Variables in BuildingBlockExecution object
64      * 
65      * @param execution - BuildingBlockExecution object
66      */
67     public void setControllerActorScopeAction(BuildingBlockExecution execution) {
68
69         ExecuteBuildingBlock executeBuildingBlock = execution.getVariable(BUILDING_BLOCK);
70         BuildingBlock buildingBlock = executeBuildingBlock.getBuildingBlock();
71
72         String scope = Optional.ofNullable(buildingBlock.getBpmnScope()).orElseThrow(
73                 () -> new NullPointerException("BPMN Scope is NULL in the orchestration_flow_reference table "));
74         String action = Optional.ofNullable(buildingBlock.getBpmnAction()).orElseThrow(
75                 () -> new NullPointerException("BPMN Action is NULL in the orchestration_flow_reference table "));
76         String controllerActor;
77
78         try {
79             if (String.valueOf(scope).equals("pnf")) {
80                 Pnf pnf = getPnf(execution);
81                 String pnfModelUUID = pnf.getModelInfoPnf().getModelCustomizationUuid();
82                 PnfResourceCustomization pnfResourceCustomization =
83                         catalogDbClient.getPnfResourceCustomizationByModelCustomizationUUID(pnfModelUUID);
84
85                 controllerActor = Optional.ofNullable(pnfResourceCustomization.getControllerActor()).orElse("APPC");
86                 execution.setVariable(MSO_REQUEST_ID,
87                         execution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId());
88                 execution.setVariable(PRC_BLUEPRINT_VERSION, pnfResourceCustomization.getBlueprintVersion());
89                 execution.setVariable(PRC_BLUEPRINT_NAME, pnfResourceCustomization.getBlueprintName());
90             } else {
91                 GenericVnf genericVnf = getGenericVnf(execution);
92                 String modelUuid = genericVnf.getModelInfoGenericVnf().getModelCustomizationUuid();
93                 VnfResourceCustomization vnfResourceCustomization =
94                         catalogDbClient.getVnfResourceCustomizationByModelCustomizationUUID(modelUuid);
95
96                 controllerActor = Optional.ofNullable(vnfResourceCustomization.getControllerActor()).orElse("APPC");
97             }
98
99             execution.setVariable(SCOPE, scope);
100             execution.setVariable(ACTION, action);
101             execution.setVariable(CONTROLLER_ACTOR, controllerActor);
102
103             logger.debug("Executing Controller Execution for ControllerActor: {}, Scope: {} , Action: {}",
104                     controllerActor, scope, action);
105         } catch (Exception ex) {
106             logger.error("An exception occurred while fetching Controller Actor,Scope and Action ", ex);
107             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
108         }
109     }
110
111     /**
112      * Selecting bbName from BBNameSelectionReference and setting the value in a variable of BuildingBlockExecution
113      * 
114      * @param execution - BuildingBlockExecution object
115      */
116     public void selectBB(BuildingBlockExecution execution) {
117         try {
118
119             String controllerActor = execution.getVariable(CONTROLLER_ACTOR);
120             String action = Optional.of((String) execution.getVariable(ACTION)).get();
121             String scope = Optional.of((String) execution.getVariable(SCOPE)).get();
122             BBNameSelectionReference bbNameSelectionReference =
123                     catalogDbClient.getBBNameSelectionReference(controllerActor, scope, action);
124             String bbName = bbNameSelectionReference.getBbName();
125             execution.setVariable(BBNAME, bbName);
126             logger.debug(" Executing {} BPMN", bbName);
127         } catch (Exception ex) {
128             logger.error("An exception occurred while getting bbname from catalogdb ", ex);
129             exceptionUtil.buildAndThrowWorkflowException(execution, 7000, ex);
130
131         }
132
133     }
134
135     private Pnf getPnf(BuildingBlockExecution buildingBlockExecution) throws BBObjectNotFoundException {
136         return extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.PNF);
137     }
138
139     private GenericVnf getGenericVnf(BuildingBlockExecution buildingBlockExecution) throws BBObjectNotFoundException {
140         return extractPojosForBB.extractByKey(buildingBlockExecution, ResourceKey.GENERIC_VNF_ID);
141     }
142 }