2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.so.client.cds;
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;
37 public class GeneratePayloadForCds {
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";
45 private VnfCDSRequestProvider vnfCDSRequestProvider;
48 private VfModuleCDSRequestProvider vfModuleCDSRequestProvider;
51 private ServiceCDSRequestProvider serviceCDSRequestProvider;
54 private ExtractPojosForBB extractPojosForBB;
57 private PnfCDSRequestProvider pnfCDSRequestProvider;
60 * Build properties like (blueprint name, version, action etc..) along with the request payload for vnf, vf-module
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.
67 public AbstractCDSPropertiesBean buildCdsPropertiesBean(BuildingBlockExecution execution)
68 throws PayloadGenerationException {
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();
77 CDSRequestProvider requestProvider = getRequestProviderByScope(scope);
78 requestProvider.setExecutionObject(execution);
80 final String requestPayload = requestProvider.buildRequestPayload(action)
81 .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS"));
83 return prepareAndSetCdsPropertyBean(requestProvider, requestPayload, requestId, action, DEFAULT_SYNC_MODE);
87 * Build properties like (blueprint name, version, action etc..) along with the request payload for pnf.
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.
93 public AbstractCDSPropertiesBean buildCdsPropertiesBean(DelegateExecution execution)
94 throws PayloadGenerationException {
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);
101 CDSRequestProvider requestProvider = getRequestProviderByScope(scope);
102 requestProvider.setExecutionObject(execution);
104 final String requestPayload = requestProvider.buildRequestPayload(action)
105 .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS"));
107 return prepareAndSetCdsPropertyBean(requestProvider, requestPayload, requestId, action, mode);
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;
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);
133 private CDSRequestProvider getRequestProviderByScope(String scope) throws PayloadGenerationException {
134 CDSRequestProvider requestProvider;
136 case PayloadConstants.VNF_SCOPE:
137 requestProvider = vnfCDSRequestProvider;
139 case PayloadConstants.VF_MODULE_SCOPE:
140 requestProvider = vfModuleCDSRequestProvider;
142 case PayloadConstants.SERVICE_SCOPE:
143 requestProvider = serviceCDSRequestProvider;
145 case PayloadConstants.PNF_SCOPE:
146 requestProvider = pnfCDSRequestProvider;
149 throw new PayloadGenerationException("No scope defined with " + scope);
151 return requestProvider;