2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
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.bpmn.infrastructure.scripts
25 import org.json.JSONObject
28 import static org.apache.commons.lang3.StringUtils.*;
29 import groovy.xml.XmlUtil
31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
33 import org.onap.so.bpmn.common.recipe.ResourceInput;
34 import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
35 import org.onap.so.bpmn.core.WorkflowException
36 import org.onap.so.bpmn.core.domain.ModelInfo
37 import org.onap.so.bpmn.core.domain.VnfResource
38 import org.onap.so.bpmn.core.json.JsonUtils
39 import org.onap.so.bpmn.core.UrnPropertiesReader
40 import org.onap.so.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
41 import org.slf4j.Logger
42 import org.slf4j.LoggerFactory
43 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
45 import java.util.UUID;
47 import org.camunda.bpm.engine.delegate.BpmnError
48 import org.camunda.bpm.engine.delegate.DelegateExecution
49 import org.apache.commons.lang3.*
50 import org.apache.commons.codec.binary.Base64
54 * This groovy class supports the <class>CreateDeviceResource.bpmn</class> process.
55 * flow for Device Resource Create
57 public class CreateDeviceResource extends AbstractServiceTaskProcessor {
59 String Prefix="CREDEVRES_"
61 ExceptionUtil exceptionUtil = new ExceptionUtil()
63 JsonUtils jsonUtil = new JsonUtils()
65 private static final Logger logger = LoggerFactory.getLogger( CreateDeviceResource.class);
67 public void preProcessRequest(DelegateExecution execution){
68 logger.info(" ***** Started preProcessRequest *****")
72 //get bpmn inputs from resource request.
73 String requestId = execution.getVariable("mso-request-id")
74 String requestAction = execution.getVariable("requestAction")
75 logger.info("The requestAction is: " + requestAction)
76 String recipeParamsFromRequest = execution.getVariable("recipeParams")
77 logger.info("The recipeParams is: " + recipeParamsFromRequest)
78 String resourceInput = execution.getVariable("resourceInput")
79 logger.info("The resourceInput is: " + resourceInput)
80 //Get ResourceInput Object
81 ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
82 execution.setVariable(Prefix + "ResourceInput", resourceInputObj)
84 String resourceInputPrameters = resourceInputObj.getResourceParameters()
85 String inputParametersJson = jsonUtil.getJsonValue(resourceInputPrameters, "requestInputs")
86 JSONObject inputParameters = new JSONObject(inputParametersJson)
87 execution.setVariable(Prefix + "ResourceRequestInputs", inputParameters)
89 // String incomingRequest = resourceInputObj.getRequestsInputs()
90 // String serviceParameters = JsonUtils.getJsonValue(incomingRequest, "service.parameters")
91 // String requestInputs = JsonUtils.getJsonValue(serviceParameters, "requestInputs")
92 // JSONObject serviceInputParameters = new JSONObject(requestInputs)
93 // execution.setVariable(Prefix + "ServiceParameters", serviceInputParameters)
95 //Deal with recipeParams
96 String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
97 String resourceName = resourceInputObj.getResourceInstanceName()
98 if (isBlank(resourceName)) {
99 msg = "Input resourceName is null"
102 execution.setVariable("resourceName", resourceName)
103 logger.info("resourceName:" + resourceName)
105 String resourceModelInvariantUuid = resourceInputObj.getResourceModelInfo().getModelInvariantUuid()
106 if (isBlank(resourceModelInvariantUuid)) {
107 msg = "Input resourceModelInvariantUuid is null"
110 execution.setVariable(Prefix + "ResourceModelInvariantUuid", resourceModelInvariantUuid)
111 logger.info("resourceModelInvariantUuid:" + resourceModelInvariantUuid)
113 String resourceModelUuid = resourceInputObj.getResourceModelInfo().getModelUuid()
114 if (isBlank(resourceModelUuid)) {
115 msg = "Input resourceModelUuid is null"
118 execution.setVariable(Prefix + "ResourceModelUuid", resourceModelUuid)
119 logger.info("resourceModelUuid:" + resourceModelUuid)
121 String resourceModelCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
122 if (isBlank(resourceModelCustomizationUuid)) {
123 msg = "Input resourceModelCustomizationUuid is null"
126 execution.setVariable(Prefix + "ResourceModelCustomizationUuid", resourceModelCustomizationUuid)
127 logger.info("resourceModelCustomizationUuid:" + resourceModelCustomizationUuid)
129 execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
130 execution.setVariable("mso-request-id", requestId)
132 } catch (Exception ex){
133 msg = "Exception in preProcessRequest " + ex.getMessage()
135 // exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
139 public void checkDevType(DelegateExecution execution){
140 logger.info(" ***** Started checkDevType *****")
143 JSONObject resourceInputParameters = execution.getVariable(Prefix + "ResourceRequestInputs")
144 String devType = resourceInputParameters.get("device_class")
146 if(StringUtils.isBlank(devType)) {
150 execution.setVariable("device_class", devType)
152 } catch (Exception ex){
153 String msg = "Exception in checkDevType " + ex.getMessage()
155 // exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
159 private void setProgressUpdateVariables(DelegateExecution execution, String body) {
160 def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
161 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
162 execution.setVariable("CVFMI_updateResOperStatusRequest", body)
165 public void prepareUpdateProgress(DelegateExecution execution) {
166 logger.info(" ***** Started prepareUpdateProgress *****")
167 ResourceInput resourceInputObj = execution.getVariable(Prefix + "ResourceInput")
168 String operType = resourceInputObj.getOperationType()
169 String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
170 String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
171 String modelName = resourceInputObj.getResourceModelInfo().getModelName()
172 String operationId = resourceInputObj.getOperationId()
173 String progress = execution.getVariable("progress")
174 String status = execution.getVariable("status")
175 String statusDescription = execution.getVariable("statusDescription")
178 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
179 xmlns:ns="http://org.onap.so/requestsdb">
182 <ns:updateResourceOperationStatus>
183 <operType>${operType}</operType>
184 <operationId>${operationId}</operationId>
185 <progress>${progress}</progress>
186 <resourceTemplateUUID>${resourceCustomizationUuid}</resourceTemplateUUID>
187 <serviceId>${ServiceInstanceId}</serviceId>
188 <status>${status}</status>
189 <statusDescription>${statusDescription}</statusDescription>
190 </ns:updateResourceOperationStatus>
192 </soapenv:Envelope>"""
194 setProgressUpdateVariables(execution, body)
195 logger.info(" ***** Exit prepareUpdateProgress *****")
198 private void getVNFTemplatefromSDC(DelegateExecution execution){
199 logger.info(" ***** Started getVNFTemplatefromSDC *****")
204 } catch (Exception ex){
205 String msg = "Exception in getVNFTemplatefromSDC " + ex.getMessage()
207 // exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
211 public void prepareVnfAndModulesCreate(DelegateExecution execution) {
213 logger.trace("Inside prepareVnfAndModulesCreate of CreateDeviceResource ")
215 JSONObject resourceInputParameters = execution.getVariable(Prefix + "ResourceRequestInputs")
216 String devType = resourceInputParameters.get("device_type")
217 String devVersion = resourceInputParameters.get("device_version")
219 //get vnf model from SDC
220 getVNFTemplatefromSDC(execution)
222 VnfResource vnf = execution.getVariable("vnfResource")
223 Integer vnfsCreatedCount = execution.getVariable(Prefix + "VnfsCreatedCount")
224 String vnfModelInfoString = null;
227 logger.debug("getting model info for vnf # " + vnfsCreatedCount)
228 ModelInfo vnfModelInfo = vnf.getModelInfo()
229 vnfModelInfoString = vnfModelInfo.toString()
231 logger.debug("vnf is null")
232 vnfModelInfoString = execution.getVariable("vnfModelInfo")
235 logger.debug(" vnfModelInfoString :" + vnfModelInfoString)
237 // extract cloud configuration
238 // String vimId = jsonUtil.getJsonValue(createVcpeServiceRequest,
239 // "requestDetails.cloudConfiguration.lcpCloudRegionId")
240 // def cloudRegion = vimId.split("_")
241 // execution.setVariable("cloudOwner", cloudRegion[0])
242 // logger.debug("cloudOwner: "+ cloudRegion[0])
243 // execution.setVariable("cloudRegionId", cloudRegion[1])
244 // logger.debug("cloudRegionId: "+ cloudRegion[1])
245 // execution.setVariable("lcpCloudRegionId", cloudRegion[1])
246 // logger.debug("lcpCloudRegionId: "+ cloudRegion[1])
247 // String tenantId = jsonUtil.getJsonValue(createVcpeServiceRequest,
248 // "requestDetails.cloudConfiguration.tenantId")
249 // execution.setVariable("tenantId", tenantId)
250 // logger.debug("tenantId: " + tenantId)
253 execution.setVariable("cloudOwner", "")
255 execution.setVariable("cloudRegionId", "")
257 execution.setVariable("lcpCloudRegionId", "")
259 execution.setVariable("tenantId", "")
261 String sdncVersion = execution.getVariable("sdncVersion")
262 logger.debug("sdncVersion: " + sdncVersion)
264 logger.trace("Completed prepareVnfAndModulesCreate of CreateVcpeResCustService ")
265 } catch (Exception ex) {
266 // try error in method block
267 String exceptionMessage = "Bpmn error encountered in CreateDeviceResource flow. Unexpected Error from method prepareVnfAndModulesCreate() - " + ex.getMessage()
268 logger.debug(exceptionMessage)
272 // *******************************
273 // Validate Vnf request Section -> increment count
274 // *******************************
275 public void validateVnfCreate(DelegateExecution execution) {
278 logger.trace("Inside validateVnfCreate of CreateDeviceResource ")
280 //Update Relationship between VNF to Device
281 addVNFAAIRelationShip(execution)
283 Integer vnfsCreatedCount = execution.getVariable(Prefix + "VnfsCreatedCount")
286 execution.setVariable(Prefix + "VnfsCreatedCount", vnfsCreatedCount)
288 logger.debug(" ***** Completed validateVnfCreate of CreateDeviceResource ***** " + " vnf # " + vnfsCreatedCount)
289 } catch (Exception ex) {
290 // try error in method block
291 String exceptionMessage = "Bpmn error encountered in CreateDeviceResource flow. Unexpected Error from method validateVnfCreate() - " + ex.getMessage()
292 logger.debug(exceptionMessage)
296 public void addVNFAAIRelationShip(DelegateExecution execution) {
299 logger.trace("Inside addVNFAAIRelationShip of CreateDeviceResource ")
303 logger.debug(" ***** Completed addVNFAAIRelationShip of CreateDeviceResource ***** ")
304 } catch (Exception ex) {
305 // try error in method block
306 String exceptionMessage = "Bpmn error encountered in CreateDeviceResource flow. Unexpected Error from method addVNFAAIRelationShip() - " + ex.getMessage()
307 logger.debug(exceptionMessage)
311 public void sendSyncResponse (DelegateExecution execution) {
312 logger.debug(" *** sendSyncResponse *** ")
315 String operationStatus = "finished"
316 // RESTResponse for main flow
317 String resourceOperationResp = """{"operationStatus":"${operationStatus}"}""".trim()
318 logger.debug(" sendSyncResponse to APIH:" + "\n" + resourceOperationResp)
319 sendWorkflowResponse(execution, 202, resourceOperationResp)
320 execution.setVariable("sentSyncResponse", true)
322 } catch (Exception ex) {
323 String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
325 // exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
327 logger.debug(" ***** Exit sendSyncResopnse *****")