Merge "Skips the VfModule or VNF Configassign/ConfigDeploy action."
[so.git] / bpmn / MSOCommonBPMN / src / main / java / org / onap / so / client / cds / GeneratePayloadForCds.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 Bell Canada
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.client.cds;
22
23 import org.onap.so.bpmn.common.BuildingBlockExecution;
24 import org.onap.so.bpmn.servicedecomposition.entities.BuildingBlock;
25 import org.onap.so.bpmn.servicedecomposition.entities.ExecuteBuildingBlock;
26 import org.onap.so.bpmn.servicedecomposition.tasks.ExtractPojosForBB;
27 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
28 import org.onap.so.client.exception.PayloadGenerationException;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Component;
31 import java.util.UUID;
32
33 @Component
34 public class GeneratePayloadForCds {
35
36     private static final String ORIGINATOR_ID = "SO";
37     private static final String MODE = "sync";
38     private static final String BUILDING_BLOCK = "buildingBlock";
39
40     @Autowired
41     private VnfCDSRequestProvider vnfCDSRequestProvider;
42
43     @Autowired
44     private VfModuleCDSRequestProvider vfModuleCDSRequestProvider;
45
46     @Autowired
47     private ServiceCDSRequestProvider serviceCDSRequestProvider;
48
49     @Autowired
50     private ExtractPojosForBB extractPojosForBB;
51
52     /**
53      * Build properties like (blueprint name, version, action etc..) along with the request payload.
54      *
55      * @param execution - A building block execution object.
56      * @return AbstractCDSPropertiesBean - A POJO which contains CDS related information.
57      * @throws PayloadGenerationException - Throw an exception if it fails to build payload for CDS.
58      */
59     public AbstractCDSPropertiesBean buildCdsPropertiesBean(BuildingBlockExecution execution)
60             throws PayloadGenerationException {
61
62         ExecuteBuildingBlock executeBuildingBlock = execution.getVariable(BUILDING_BLOCK);
63         BuildingBlock buildingBlock = executeBuildingBlock.getBuildingBlock();
64         String scope = buildingBlock.getBpmnScope();
65         String action = buildingBlock.getBpmnAction();
66
67
68         CDSRequestProvider requestProvider = getRequestProviderByScope(scope);
69         requestProvider.setExecutionObject(execution);
70
71         final String requestPayload = requestProvider.buildRequestPayload(action)
72                 .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS"));
73
74         final String requestId = execution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId();
75         final AbstractCDSPropertiesBean cdsPropertiesBean = new AbstractCDSPropertiesBean();
76         cdsPropertiesBean.setRequestObject(requestPayload);
77         cdsPropertiesBean.setBlueprintName(requestProvider.getBlueprintName());
78         cdsPropertiesBean.setBlueprintVersion(requestProvider.getBlueprintVersion());
79         cdsPropertiesBean.setRequestId(requestId);
80         cdsPropertiesBean.setOriginatorId(ORIGINATOR_ID);
81         cdsPropertiesBean.setSubRequestId(UUID.randomUUID().toString());
82         cdsPropertiesBean.setActionName(action);
83         cdsPropertiesBean.setMode(MODE);
84
85         return cdsPropertiesBean;
86     }
87
88     private CDSRequestProvider getRequestProviderByScope(String scope) throws PayloadGenerationException {
89         CDSRequestProvider requestProvider;
90         switch (scope) {
91             case "vnf":
92                 requestProvider = vnfCDSRequestProvider;
93                 break;
94             case "vfModule":
95                 requestProvider = vfModuleCDSRequestProvider;
96                 break;
97             case "service":
98                 requestProvider = serviceCDSRequestProvider;
99                 break;
100             default:
101                 throw new PayloadGenerationException("No scope defined with " + scope);
102         }
103         return requestProvider;
104     }
105 }