1fae5ce06c391cd1012d6dcbe221e7b37920679a
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / bpmn / infrastructure / flowspecific / tasks / GenericCDSProcessingBB.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 Bell Canada
6  * ================================================================================
7  * Modifications Copyright (c) 2020 Tech Mahindra
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.flowspecific.tasks;
24
25 import org.onap.so.bpmn.common.BuildingBlockExecution;
26 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerContext;
27 import org.onap.so.bpmn.infrastructure.decisionpoint.api.ControllerRunnable;
28 import org.onap.so.client.cds.AbstractCDSProcessingBBUtils;
29 import org.onap.so.client.cds.GeneratePayloadForCds;
30 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
31 import org.onap.so.client.exception.ExceptionBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 /**
38  * This class is used in context of Building Block flow for configuration of vnf/vfmodule/service.
39  *
40  * @param - BuildingBlockExecution
41  */
42 @Component
43 public class GenericCDSProcessingBB implements ControllerRunnable<BuildingBlockExecution> {
44     private static final Logger logger = LoggerFactory.getLogger(GenericCDSProcessingBB.class);
45     private static final String EXECUTION_OBJECT = "executionObject";
46     public static final String CDS_ACTOR = "cds";
47     public static final String VNF_SCOPE = "vnf";
48     public static final String VF_MODULE_SCOPE = "vfmodule";
49
50     @Autowired
51     private ExceptionBuilder exceptionBuilder;
52
53     @Autowired
54     private AbstractCDSProcessingBBUtils cdsDispather;
55
56     @Autowired
57     private GeneratePayloadForCds generatePayloadForCds;
58
59     @Override
60     public Boolean understand(ControllerContext<BuildingBlockExecution> context) {
61         String scope = context.getControllerScope();
62         return CDS_ACTOR.equalsIgnoreCase(context.getControllerActor())
63                 && (VNF_SCOPE.equalsIgnoreCase(scope) || VF_MODULE_SCOPE.equalsIgnoreCase(scope));
64     }
65
66     @Override
67     public Boolean ready(ControllerContext<BuildingBlockExecution> context) {
68         return true;
69     }
70
71     @Override
72     public void prepare(ControllerContext<BuildingBlockExecution> context) {
73         BuildingBlockExecution buildingBlockExecution = context.getExecution();
74         try {
75             AbstractCDSPropertiesBean abstractCDSPropertiesBean =
76                     generatePayloadForCds.buildCdsPropertiesBean(buildingBlockExecution);
77             buildingBlockExecution.setVariable(EXECUTION_OBJECT, abstractCDSPropertiesBean);
78         } catch (Exception ex) {
79             logger.error("An exception occurred when creating payload for CDS request", ex);
80             exceptionBuilder.buildAndThrowWorkflowException(buildingBlockExecution, 7000, ex);
81         }
82     }
83
84     @Override
85     public void run(ControllerContext<BuildingBlockExecution> context) {
86         BuildingBlockExecution obj = context.getExecution();
87         cdsDispather.constructExecutionServiceInputObjectBB(obj);
88         cdsDispather.sendRequestToCDSClientBB(obj);
89     }
90 }