fb7988057281b7300648ee6cc2b282188ae9f160
[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  * Modifications Copyright (c) 2020 Nordix
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.client.cds;
24
25 import org.camunda.bpm.engine.delegate.DelegateExecution;
26 import org.onap.so.bpmn.common.BuildingBlockExecution;
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.tasks.ExtractPojosForBB;
30 import org.onap.so.client.cds.beans.AbstractCDSPropertiesBean;
31 import org.onap.so.client.exception.PayloadGenerationException;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.stereotype.Component;
34 import java.util.UUID;
35
36 @Component
37 public class GeneratePayloadForCds {
38
39     private static final String ORIGINATOR_ID = "SO";
40     private static final String BUILDING_BLOCK = "buildingBlock";
41     private static final String DEFAULT_SYNC_MODE = "sync";
42     private static final String MSO_REQUEST_ID = "msoRequestId";
43
44     @Autowired
45     private VnfCDSRequestProvider vnfCDSRequestProvider;
46
47     @Autowired
48     private VfModuleCDSRequestProvider vfModuleCDSRequestProvider;
49
50     @Autowired
51     private ServiceCDSRequestProvider serviceCDSRequestProvider;
52
53     @Autowired
54     private ExtractPojosForBB extractPojosForBB;
55
56     @Autowired
57     private PnfCDSRequestProvider pnfCDSRequestProvider;
58
59     /**
60      * Build properties like (blueprint name, version, action etc..) along with the request payload for vnf, vf-module
61      * and service.
62      *
63      * @param execution - A building block execution object.
64      * @return AbstractCDSPropertiesBean - A POJO which contains CDS related information.
65      * @throws PayloadGenerationException - Throw an exception if it fails to build payload for CDS.
66      */
67     public AbstractCDSPropertiesBean buildCdsPropertiesBean(BuildingBlockExecution execution)
68             throws PayloadGenerationException {
69
70         ExecuteBuildingBlock executeBuildingBlock = execution.getVariable(BUILDING_BLOCK);
71         BuildingBlock buildingBlock = executeBuildingBlock.getBuildingBlock();
72         final String requestId = execution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId();
73         final String scope = buildingBlock.getBpmnScope();
74         final String action = buildingBlock.getBpmnAction();
75
76
77         CDSRequestProvider requestProvider = getRequestProviderByScope(scope);
78         requestProvider.setExecutionObject(execution);
79
80         final String requestPayload = requestProvider.buildRequestPayload(action)
81                 .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS"));
82
83         return prepareAndSetCdsPropertyBean(requestProvider, requestPayload, requestId, action, DEFAULT_SYNC_MODE);
84     }
85
86     /**
87      * Build properties like (blueprint name, version, action etc..) along with the request payload for pnf.
88      *
89      * @param execution - A building block execution object.
90      * @return AbstractCDSPropertiesBean - A POJO which contains CDS related information.
91      * @throws PayloadGenerationException - Throw an exception if it fails to build payload for CDS.
92      */
93     public AbstractCDSPropertiesBean buildCdsPropertiesBean(DelegateExecution execution)
94             throws PayloadGenerationException {
95
96         final String scope = String.valueOf(execution.getVariable(PayloadConstants.SCOPE));
97         final String action = String.valueOf(execution.getVariable(PayloadConstants.ACTION));
98         final String requestId = String.valueOf(execution.getVariable(MSO_REQUEST_ID));
99         final String mode = extractAndSetMode(execution);
100
101         CDSRequestProvider requestProvider = getRequestProviderByScope(scope);
102         requestProvider.setExecutionObject(execution);
103
104         final String requestPayload = requestProvider.buildRequestPayload(action)
105                 .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS"));
106
107         return prepareAndSetCdsPropertyBean(requestProvider, requestPayload, requestId, action, mode);
108     }
109
110     private AbstractCDSPropertiesBean prepareAndSetCdsPropertyBean(CDSRequestProvider requestProvider,
111             String requestPayload, String requestId, String action, String mode) {
112         final AbstractCDSPropertiesBean cdsPropertiesBean = new AbstractCDSPropertiesBean();
113         cdsPropertiesBean.setRequestObject(requestPayload);
114         cdsPropertiesBean.setBlueprintName(requestProvider.getBlueprintName());
115         cdsPropertiesBean.setBlueprintVersion(requestProvider.getBlueprintVersion());
116         cdsPropertiesBean.setRequestId(requestId);
117         cdsPropertiesBean.setOriginatorId(ORIGINATOR_ID);
118         cdsPropertiesBean.setSubRequestId(UUID.randomUUID().toString());
119         cdsPropertiesBean.setActionName(action);
120         cdsPropertiesBean.setMode(mode);
121         return cdsPropertiesBean;
122     }
123
124     private String extractAndSetMode(DelegateExecution execution) {
125         String mode = DEFAULT_SYNC_MODE;
126         Object obj = execution.getVariable(PayloadConstants.MODE);
127         if (obj != null && !String.valueOf(obj).isEmpty()) {
128             mode = String.valueOf(obj);
129         }
130         return mode;
131     }
132
133     private CDSRequestProvider getRequestProviderByScope(String scope) throws PayloadGenerationException {
134         CDSRequestProvider requestProvider;
135         switch (scope) {
136             case PayloadConstants.VNF_SCOPE:
137                 requestProvider = vnfCDSRequestProvider;
138                 break;
139             case PayloadConstants.VF_MODULE_SCOPE:
140                 requestProvider = vfModuleCDSRequestProvider;
141                 break;
142             case PayloadConstants.SERVICE_SCOPE:
143                 requestProvider = serviceCDSRequestProvider;
144                 break;
145             case PayloadConstants.PNF_SCOPE:
146                 requestProvider = pnfCDSRequestProvider;
147                 break;
148             default:
149                 throw new PayloadGenerationException("No scope defined with " + scope);
150         }
151         return requestProvider;
152     }
153 }