Code changes in BPMN infra for RAN Slice Use case
[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     @Autowired
60     private NssiCDSRequestProvider nssiCDSRequestProvider;
61
62
63     /**
64      * Build properties like (blueprint name, version, action etc..) along with the request payload for vnf, vf-module
65      * and service.
66      *
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.
70      */
71     public AbstractCDSPropertiesBean buildCdsPropertiesBean(BuildingBlockExecution execution)
72             throws PayloadGenerationException {
73
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();
79
80
81         CDSRequestProvider requestProvider = getRequestProviderByScope(scope);
82         requestProvider.setExecutionObject(execution);
83
84         final String requestPayload = requestProvider.buildRequestPayload(action)
85                 .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS"));
86
87         return prepareAndSetCdsPropertyBean(requestProvider, requestPayload, requestId, action, DEFAULT_SYNC_MODE);
88     }
89
90     /**
91      * Build properties like (blueprint name, version, action etc..) along with the request payload for pnf.
92      *
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.
96      */
97     public AbstractCDSPropertiesBean buildCdsPropertiesBean(DelegateExecution execution)
98             throws PayloadGenerationException {
99
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);
104
105         CDSRequestProvider requestProvider = getRequestProviderByScope(scope);
106         requestProvider.setExecutionObject(execution);
107
108         final String requestPayload = requestProvider.buildRequestPayload(action)
109                 .orElseThrow(() -> new PayloadGenerationException("Failed to build payload for CDS"));
110
111         return prepareAndSetCdsPropertyBean(requestProvider, requestPayload, requestId, action, mode);
112     }
113
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;
126     }
127
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);
133         }
134         return mode;
135     }
136
137     private CDSRequestProvider getRequestProviderByScope(String scope) throws PayloadGenerationException {
138         CDSRequestProvider requestProvider;
139         switch (scope) {
140             case PayloadConstants.VNF_SCOPE:
141                 requestProvider = vnfCDSRequestProvider;
142                 break;
143             case PayloadConstants.VF_MODULE_SCOPE:
144                 requestProvider = vfModuleCDSRequestProvider;
145                 break;
146             case PayloadConstants.SERVICE_SCOPE:
147                 requestProvider = serviceCDSRequestProvider;
148                 break;
149             case PayloadConstants.PNF_SCOPE:
150                 requestProvider = pnfCDSRequestProvider;
151                 break;
152
153             case PayloadConstants.NSSI_SCOPE:
154                 requestProvider = nssiCDSRequestProvider;
155                 break;
156             default:
157                 throw new PayloadGenerationException("No scope defined with " + scope);
158         }
159         return requestProvider;
160     }
161 }