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