2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package com.att.bpm.scripts;
23 import groovy.json.JsonSlurper
25 import org.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.runtime.Execution
27 import org.apache.commons.lang3.*
28 import org.openecomp.mso.bpmn.core.RollbackData
29 import org.openecomp.mso.bpmn.core.WorkflowException
31 public class CreateVfModuleInfra extends AbstractServiceTaskProcessor {
34 * Validates the request message and sets up the workflow.
35 * @param execution the execution
37 public void preProcessRequest(Execution execution) {
38 def method = getClass().getSimpleName() + '.preProcessRequest(' +
39 'execution=' + execution.getId() +
41 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
42 logDebug('Entered ' + method, isDebugLogEnabled)
45 logDebug('Entered 1' + method, isDebugLogEnabled)
46 execution.setVariable('prefix', prefix)
47 logDebug('Entered 2' + method, isDebugLogEnabled)
48 execution.setVariable("isVidRequest", "false")
50 logDebug("Set variables", isDebugLogEnabled)
52 def rollbackData = execution.getVariable("RollbackData")
53 if (rollbackData == null) {
54 rollbackData = new RollbackData()
56 execution.setVariable("RollbackData", rollbackData)
58 logDebug("Set rollback data", isDebugLogEnabled)
59 def incomingRequest = execution.getVariable('bpmnRequest')
61 utils.log("DEBUG", "Incoming Infra Request: " + incomingRequest, isDebugLogEnabled)
63 // check if request is xml or json
65 def jsonSlurper = new JsonSlurper()
66 Map reqMap = jsonSlurper.parseText(incomingRequest)
67 utils.log("DEBUG", " Request is in JSON format.", isDebugLogEnabled)
69 def serviceInstanceId = execution.getVariable('serviceInstanceId')
70 def vnfId = execution.getVariable('vnfId')
72 def vidUtils = new VidUtils(this)
74 String requestInXmlFormat = vidUtils.createXmlVfModuleRequest(execution, reqMap, 'CREATE_VF_MODULE', serviceInstanceId)
76 utils.log("DEBUG", " Request in XML format: " + requestInXmlFormat, isDebugLogEnabled)
78 execution.setVariable(prefix + 'Request', requestInXmlFormat)
79 execution.setVariable(prefix+'vnfId', vnfId)
80 execution.setVariable("isVidRequest", "true")
83 catch(groovy.json.JsonException je) {
84 utils.log("DEBUG", " Request is not in JSON format.", isDebugLogEnabled)
85 workflowException(execution, "Invalid request format", 400)
89 String restFaultMessage = e.getMessage()
90 //execution.setVariable("CVFMODVOL2_RESTFault", restFaultMessage)
91 //execution.setVariable("CVFMODVOL2_isDataOk", false)
92 utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)
93 workflowException(execution, restFaultMessage, 400)
97 String request = validateInfraRequest(execution)
99 execution.setVariable("CreateVfModuleInfraSuccessIndicator", false)
100 execution.setVariable("RollbackCompleted", false)
101 execution.setVariable("DoCreateVfModuleRequest", request)
102 execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)
103 execution.setVariable("CVFMI_requestInfo",utils.getNodeXml(request,"request-info"))
104 execution.setVariable("CVFMI_requestId",utils.getNodeText1(request,"request-id"))
105 execution.setVariable("CVFMI_source",utils.getNodeText1(request,"source"))
106 execution.setVariable("CVFMI_serviceInstanceId", utils.getNodeText1(request, "service-instance-id"))
107 execution.setVariable("CVFMI_vnfInputs",utils.getNodeXml(request,"vnf-inputs"))
110 NetworkUtils networkUtils = new NetworkUtils()
111 execution.setVariable("CVFMI_rollbackEnabled", networkUtils.isRollbackEnabled(execution,request))
112 execution.setVariable("CVFMI_originalWorkflowException", null)
114 if (utils.nodeExists(request, "vnf-params")) {
115 vnfParams = utils.getNodeXml(request,"vnf-params")
117 execution.setVariable("CVFMI_vnfParams", vnfParams)
119 def newVfModuleId = UUID.randomUUID().toString()
120 execution.setVariable("newVfModuleId", newVfModuleId)
122 logDebug('RequestInfo: ' + execution.getVariable("CVFMI_requestInfo"), isDebugLogEnabled)
123 logDebug('VnfInputs: ' + execution.getVariable("CVFMI_vnfInputs"), isDebugLogEnabled)
124 logDebug('VnfParams: ' + execution.getVariable("CVFMI_vnfParams"), isDebugLogEnabled)
125 logDebug('rollbackEnabled: ' + execution.getVariable("CVFMI_rollbackEnabled"), isDebugLogEnabled)
127 logDebug('Exited ' + method, isDebugLogEnabled)
128 } catch (BpmnError bpmnError) {
129 buildErrorResponse(execution,bpmnError.getMessage(),"400")
131 } catch (Exception exception) {
132 workflowException(execution, exception.getMessage(), 400)
137 * Validates a workflow response.
138 * @param execution the execution
139 * @param responseVar the execution variable in which the response is stored
140 * @param responseCodeVar the execution variable in which the response code is stored
141 * @param errorResponseVar the execution variable in which the error response is stored
143 public void validateWorkflowResponse(Execution execution, String responseVar,
144 String responseCodeVar, String errorResponseVar) {
145 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
146 sdncAdapterUtils.validateSDNCResponse(execution, responseVar, responseCodeVar, errorResponseVar)
151 * Sends the empty, synchronous response back to the API Handler.
152 * @param execution the execution
154 public void sendResponse(Execution execution) {
155 def method = getClass().getSimpleName() + '.sendResponse(' +
156 'execution=' + execution.getId() +
158 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
159 logDebug('Entered ' + method, isDebugLogEnabled)
162 def requestInfo = execution.getVariable('CVFMI_requestInfo')
163 def requestId = execution.getVariable('CVFMI_requestId')
164 def source = execution.getVariable('CVFMI_source')
165 def progress = getNodeTextForce(requestInfo, 'progress')
166 if (progress.isEmpty()) {
169 def startTime = getNodeTextForce(requestInfo, 'start-time')
170 if (startTime.isEmpty()) {
171 startTime = System.currentTimeMillis()
174 // RESTResponse (for API Handler (APIH) Reply Task)
175 def newVfModuleId = execution.getVariable("newVfModuleId")
176 String synchResponse = """{"requestReferences":{"instanceId":"${newVfModuleId}","requestId":"${requestId}"}}""".trim()
178 sendWorkflowResponse(execution, 200, synchResponse)
180 logDebug('Exited ' + method, isDebugLogEnabled)
181 } catch (BpmnError e) {
183 } catch (Exception e) {
184 logError('Caught exception in ' + method, e)
185 createWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
191 * @param execution the execution
193 public void postProcessResponse(Execution execution){
194 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
196 utils.log("DEBUG", " ======== STARTED PostProcessResponse Process ======== ", isDebugEnabled)
198 def request = execution.getVariable("DoCreateVfModuleRequest")
199 def requestInfo = utils.getNodeXml(request, 'request-info', false)
200 def action = utils.getNodeText1(requestInfo, "action")
202 utils.log("DEBUG", "requestInfo is: " + requestInfo, isDebugEnabled)
203 utils.log("DEBUG", "action is: " + action, isDebugEnabled)
206 """ <aetgt:MsoCompletionRequest xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1"
207 xmlns:ns="http://ecomp.att.com/mso/request/types/v1"
208 xmlns:ns8="http://ecomp.att.com/mso/workflow/schema/v1">
209 <request-info xmlns="http://ecomp.att.com/mso/infra/vnf-request/v1">
212 <ns8:status-message>Vf Module has been created successfully.</ns8:status-message>
213 <ns8:mso-bpel-name>BPMN</ns8:mso-bpel-name>
214 </aetgt:MsoCompletionRequest>"""
216 payload = utils.formatXml(payload)
217 execution.setVariable("CVFMI_SuccessFlag", true)
218 execution.setVariable("CVFMI_msoCompletionRequest", payload)
219 utils.log("DEBUG", "Outgoing MsoCompletionRequest: \n" + payload, isDebugEnabled)
222 utils.log("ERROR", "Exception Occured Processing PostProcessResponse. Exception is:\n" + e, isDebugEnabled)
223 execution.setVariable("CVFMI_ErrorResponse", "Error Occured during PostProcessResponse Method:\n" + e.getMessage())
225 utils.log("DEBUG", "======== COMPLETED PostProcessResponse Process ======== ", isDebugEnabled)
233 * Validates the request, request id and service instance id. If a problem is found,
234 * a WorkflowException is generated and an MSOWorkflowException event is thrown. This
235 * method also sets up the log context for the workflow.
236 * @param execution the execution
237 * @return the validated request
239 public String validateInfraRequest(Execution execution) {
240 def method = getClass().getSimpleName() + '.validateInfraRequest(' +
241 'execution=' + execution.getId() +
243 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
244 logDebug('Entered ' + method, isDebugLogEnabled)
246 String processKey = getProcessKey(execution);
247 def prefix = execution.getVariable("prefix")
249 if (prefix == null) {
250 createWorkflowException(execution, 1002, processKey + " prefix is null")
254 def request = execution.getVariable(prefix + 'Request')
256 if (request == null) {
257 request = execution.getVariable(processKey + 'Request')
259 if (request == null) {
260 request = execution.getVariable('bpmnRequest')
263 setVariable(execution, processKey + 'Request', null);
264 setVariable(execution, 'bpmnRequest', null);
265 setVariable(execution, prefix + 'Request', request);
268 if (request == null) {
269 createWorkflowException(execution, 1002, processKey + " request is null")
274 def requestId = execution.getVariable("att-mso-request-id")
276 if (requestId == null) {
277 createWorkflowException(execution, 1002, processKey + " request has no att-mso-request-id")
280 setVariable(execution, prefix + 'requestId', requestId)
282 def serviceInstanceId = execution.getVariable("att-mso-service-instance-id")
284 if (serviceInstanceId == null) {
285 createWorkflowException(execution, 1002, processKey + " request message has no att-mso-service-instance-id")
288 utils.logContext(requestId, serviceInstanceId)
290 logDebug('Incoming message: ' + System.lineSeparator() + request, isDebugLogEnabled)
291 logDebug('Exited ' + method, isDebugLogEnabled)
293 } catch (BpmnError e) {
295 } catch (Exception e) {
296 logError('Caught exception in ' + method, e)
297 createWorkflowException(execution, 1002, "Invalid Message")
301 public void prepareUpdateInfraRequest(Execution execution){
302 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
304 utils.log("DEBUG", " ======== STARTED prepareUpdateInfraRequest Process ======== ", isDebugEnabled)
307 String vnfInputs = execution.getVariable("CVFMI_vnfInputs")
308 String requestInfo = execution.getVariable("CVFMI_requestInfo")
309 def aicCloudRegion = utils.getNodeText1(vnfInputs, "aic-cloud-region")
310 def tenantId = utils.getNodeText1(vnfInputs, "tenant-id")
311 def requestId = utils.getNodeText1(requestInfo, "request-id")
312 def vnfId = execution.getVariable("CVFMI_vnfId")
313 def vfModuleId = execution.getVariable("CVFMI_vfModuleId")
315 def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_db_endpoint")
316 execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
317 utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
320 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
321 xmlns:ns="http://com.att.mso/requestsdb">
324 <ns:updateInfraRequest xmlns:ns="http://com.att.mso/requestsdb">
325 <requestId>${requestId}</requestId>
326 <lastModifiedBy>BPMN</lastModifiedBy>
327 <statusMessage>VF Module successfully created</statusMessage>
328 <responseBody></responseBody>
329 <requestStatus>COMPLETE</requestStatus>
330 <progress>100</progress>
331 <vnfOutputs><vnf-outputs xmlns="http://ecomp.att.com/mso/infra/vnf-request/v1" xmlns:aetgt="http://ecomp.att.com/mso/infra/vnf-request/v1" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"><vnf-id>${vnfId}</vnf-id><vf-module-id>${vfModuleId}</vf-module-id></vnf-outputs></vnfOutputs>
332 <vfModuleId>${vfModuleId}</vfModuleId>
333 </ns:updateInfraRequest>
335 </soapenv:Envelope>"""
337 payload = utils.formatXml(payload)
338 execution.setVariable("CVFMI_updateInfraRequest", payload)
339 utils.log("DEBUG", "Outgoing UpdateInfraRequest: \n" + payload, isDebugEnabled)
343 utils.log("ERROR", "Exception Occured Processing prepareUpdateInfraRequest. Exception is:\n" + e, isDebugEnabled)
344 execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareUpdateInfraRequest Method:\n" + e.getMessage())
346 utils.log("DEBUG", "======== COMPLETED prepareUpdateInfraRequest Process ======== ", isDebugEnabled)
350 * Builds a "FalloutHandler" request and stores it in the specified execution variable.
352 * @param execution the execution
353 * @param resultVar the execution variable in which the result will be stored
355 public void falloutHandlerPrep(Execution execution, String resultVar) {
356 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
357 'execution=' + execution.getId() +
358 ', resultVar=' + resultVar +
360 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
361 logDebug('Entered ' + method, isDebugLogEnabled)
365 def WorkflowException workflowException = execution.getVariable("WorkflowException")
366 def request = execution.getVariable("DoCreateVfModuleRequest")
367 def requestInformation = utils.getNodeXml(request, 'request-info', false)
368 def errorResponseCode = workflowException.getErrorCode()
369 def errorResponseMsg = workflowException.getErrorMessage()
370 def encErrorResponseMsg = ""
371 if (errorResponseMsg != null) {
372 encErrorResponseMsg = errorResponseMsg.replace("&", "&").replace("<", "<").replace(">", ">")
376 <aetgt:FalloutHandlerRequest xmlns:aetgt="http://ecomp.att.com/mso/workflow/schema/v1"
377 xmlns:reqtype="http://ecomp.att.com/mso/request/types/v1"
378 xmlns:msoservtypes="http://ecomp.att.com/mso/request/types/v1"
379 xmlns:structuredtypes="http://ecomp.att.com/mso/structured/types/v1">
380 ${requestInformation}
381 <aetgt:WorkflowException>
382 <aetgt:ErrorMessage>${encErrorResponseMsg}</aetgt:ErrorMessage>
383 <aetgt:ErrorCode>${errorResponseCode}</aetgt:ErrorCode>
384 </aetgt:WorkflowException>
385 </aetgt:FalloutHandlerRequest>
388 logDebug("CONTENT before translation: " + content, isDebugLogEnabled)
389 content = utils.formatXml(content)
390 logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)
391 execution.setVariable(resultVar, content)
393 logDebug('Exited ' + method, isDebugLogEnabled)
394 } catch (BpmnError e) {
396 } catch (Exception e) {
397 logError('Caught exception in ' + method, e)
398 createWorkflowException(execution, 2000, 'Internal Error')
402 public void logAndSaveOriginalException(Execution execution) {
403 def method = getClass().getSimpleName() + '.validateRollbackResponse(' +
404 'execution=' + execution.getId() +
406 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
407 logDebug('Entered ' + method, isDebugLogEnabled)
409 logWorkflowException(execution, 'CreateVfModuleInfra caught an event')
410 saveWorkflowException(execution, 'CVFMI_originalWorkflowException')
413 public void validateRollbackResponse(Execution execution) {
414 def method = getClass().getSimpleName() + '.validateRollbackResponse(' +
415 'execution=' + execution.getId() +
417 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
418 logDebug('Entered ' + method, isDebugLogEnabled)
420 def originalException = execution.getVariable("CVFMI_originalWorkflowException")
421 execution.setVariable("WorkflowException", originalException)
423 execution.setVariable("RollbackCompleted", true)