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 private NssiCDSRequestProvider nssiCDSRequestProvider;
64 * Build properties like (blueprint name, version, action etc..) along with the request payload for vnf, vf-module
67 * @param execution - A building block execution object.
68 * @return AbstractCDSPropertiesBean - A POJO which contains CDS related information.
69 * @throws PayloadGenerationException - Throw an exception if it fails to build payload for CDS.
71 public AbstractCDSPropertiesBean buildCdsPropertiesBean(BuildingBlockExecution execution)
72 throws PayloadGenerationException {
74 ExecuteBuildingBlock executeBuildingBlock = execution.getVariable(BUILDING_BLOCK);
75 BuildingBlock buildingBlock = executeBuildingBlock.getBuildingBlock();
76 final String requestId = execution.getGeneralBuildingBlock().getRequestContext().getMsoRequestId();
77 final String scope = buildingBlock.getBpmnScope();
78 final String action = buildingBlock.getBpmnAction();
81 CDSRequestProvider requestProvider = getRequestProviderByScope(scope);
82 requestProvider.setExecutionObject(execution);
84 final String requestPayload = requestProvider.buildRequestPayload(action)
85 .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS"));
87 return prepareAndSetCdsPropertyBean(requestProvider, requestPayload, requestId, action, DEFAULT_SYNC_MODE);
91 * Build properties like (blueprint name, version, action etc..) along with the request payload for pnf.
93 * @param execution - A building block execution object.
94 * @return AbstractCDSPropertiesBean - A POJO which contains CDS related information.
95 * @throws PayloadGenerationException - Throw an exception if it fails to build payload for CDS.
97 public AbstractCDSPropertiesBean buildCdsPropertiesBean(DelegateExecution execution)
98 throws PayloadGenerationException {
100 final String scope = String.valueOf(execution.getVariable(PayloadConstants.SCOPE));
101 final String action = String.valueOf(execution.getVariable(PayloadConstants.ACTION));
102 final String requestId = String.valueOf(execution.getVariable(MSO_REQUEST_ID));
103 final String mode = extractAndSetMode(execution);
105 CDSRequestProvider requestProvider = getRequestProviderByScope(scope);
106 requestProvider.setExecutionObject(execution);
108 final String requestPayload = requestProvider.buildRequestPayload(action)
109 .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS"));
111 return prepareAndSetCdsPropertyBean(requestProvider, requestPayload, requestId, action, mode);
114 private AbstractCDSPropertiesBean prepareAndSetCdsPropertyBean(CDSRequestProvider requestProvider,
115 String requestPayload, String requestId, String action, String mode) {
116 final AbstractCDSPropertiesBean cdsPropertiesBean = new AbstractCDSPropertiesBean();
117 cdsPropertiesBean.setRequestObject(requestPayload);
118 cdsPropertiesBean.setBlueprintName(requestProvider.getBlueprintName());
119 cdsPropertiesBean.setBlueprintVersion(requestProvider.getBlueprintVersion());
120 cdsPropertiesBean.setRequestId(requestId);
121 cdsPropertiesBean.setOriginatorId(ORIGINATOR_ID);
122 cdsPropertiesBean.setSubRequestId(UUID.randomUUID().toString());
123 cdsPropertiesBean.setActionName(action);
124 cdsPropertiesBean.setMode(mode);
125 return cdsPropertiesBean;
128 private String extractAndSetMode(DelegateExecution execution) {
129 String mode = DEFAULT_SYNC_MODE;
130 Object obj = execution.getVariable(PayloadConstants.MODE);
131 if (obj != null && !String.valueOf(obj).isEmpty()) {
132 mode = String.valueOf(obj);
137 private CDSRequestProvider getRequestProviderByScope(String scope) throws PayloadGenerationException {
138 CDSRequestProvider requestProvider;
140 case PayloadConstants.VNF_SCOPE:
141 requestProvider = vnfCDSRequestProvider;
143 case PayloadConstants.VF_MODULE_SCOPE:
144 requestProvider = vfModuleCDSRequestProvider;
146 case PayloadConstants.SERVICE_SCOPE:
147 requestProvider = serviceCDSRequestProvider;
149 case PayloadConstants.PNF_SCOPE:
150 requestProvider = pnfCDSRequestProvider;
153 case PayloadConstants.NSSI_SCOPE:
154 requestProvider = nssiCDSRequestProvider;
157 throw new PayloadGenerationException("No scope defined with " + scope);
159 return requestProvider;