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 org.onap.so.bpmn.infrastructure.scripts
23 import org.apache.commons.lang3.*
24 import org.camunda.bpm.engine.delegate.BpmnError
25 import org.camunda.bpm.engine.delegate.DelegateExecution;
26 import org.onap.so.bpmn.common.scripts.AaiUtil;
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor;
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
29 import org.onap.so.bpmn.common.scripts.MsoUtils
30 import org.onap.so.bpmn.core.WorkflowException
31 import org.onap.so.logger.MessageEnum
32 import org.onap.so.logger.MsoLogger
33 import org.onap.so.rest.APIResponse
35 import groovy.json.JsonOutput
36 import groovy.json.JsonSlurper
38 class CreateVfModuleVolumeInfraV1 extends AbstractServiceTaskProcessor {
40 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateVfModuleVolumeInfraV1.class);
41 public static final String prefix='CVMVINFRAV1_'
44 * Perform initial processing, such as request validation, initialization of variables, etc.
47 public void preProcessRequest (DelegateExecution execution) {
48 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
49 setBasicDBAuthHeader(execution, isDebugEnabled)
50 preProcessRequest(execution, isDebugEnabled)
55 * Perform initial processing, such as request validation, initialization of variables, etc.
57 * @param isDebugEnabled
59 public void preProcessRequest (DelegateExecution execution, isDebugEnabled) {
61 execution.setVariable("prefix",prefix)
62 setSuccessIndicator(execution, false)
63 execution.setVariable(prefix+'syncResponseSent', false)
65 String createVolumeIncoming = validateRequest(execution, 'vnfId')
66 msoLogger.debug(createVolumeIncoming)
69 def jsonSlurper = new JsonSlurper()
70 Map reqMap = jsonSlurper.parseText(createVolumeIncoming)
71 setupVariables(execution, reqMap, isDebugEnabled)
72 msoLogger.debug("XML request:\n" + createVolumeIncoming)
74 catch(groovy.json.JsonException je) {
75 (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, 'Request is not a valid JSON document')
78 // For rollback in this flow
79 setBasicDBAuthHeader(execution, isDebugEnabled)
80 setRollbackEnabled(execution, isDebugEnabled)
85 * Set up variables that will be passed to the BB DoCreatevfModuleVolume flow
88 * @param serviceInstanceId
89 * @param isDebugLogEnabled
91 public void setupVariables(DelegateExecution execution, Map requestMap, isDebugLogEnabled) {
93 def jsonOutput = new JsonOutput()
95 // volumeGroupId - is generated
96 String volumeGroupId = UUID.randomUUID()
97 execution.setVariable('volumeGroupId', volumeGroupId)
98 msoLogger.debug("Generated volumeGroupId: " + volumeGroupId)
101 def volGrpName = requestMap.requestDetails.requestInfo?.instanceName ?: ''
102 execution.setVariable('volumeGroupName', volGrpName)
105 def vfModuleModelInfo = jsonOutput.toJson(requestMap.requestDetails?.modelInfo)
106 execution.setVariable('vfModuleModelInfo', vfModuleModelInfo)
109 def lcpCloudRegionId = requestMap.requestDetails.cloudConfiguration.lcpCloudRegionId
110 execution.setVariable('lcpCloudRegionId', lcpCloudRegionId)
113 def tenantId = requestMap.requestDetails.cloudConfiguration.tenantId
114 execution.setVariable('tenantId', tenantId)
117 def source = requestMap.requestDetails.requestInfo.source
118 execution.setVariable(prefix+'source', source)
120 // vnfType and asdcServiceModelVersion
123 def asdcServiceModelVersion = ''
124 def modelCustomizationName = ''
126 def relatedInstanceList = requestMap.requestDetails.relatedInstanceList
127 relatedInstanceList.each {
128 if (it.relatedInstance.modelInfo?.modelType == 'service') {
129 serviceName = it.relatedInstance.modelInfo?.modelName
130 asdcServiceModelVersion = it.relatedInstance.modelInfo?.modelVersion
132 if (it.relatedInstance.modelInfo?.modelType == 'vnf') {
133 modelCustomizationName = it.relatedInstance.modelInfo?.modelCustomizationName
137 def vnfType = serviceName + '/' + modelCustomizationName
138 execution.setVariable('vnfType', vnfType)
139 execution.setVariable('asdcServiceModelVersion', asdcServiceModelVersion)
141 // vfModuleInputParams
142 def userParams = requestMap.requestDetails?.requestParameters?.userParams
143 Map<String, String> vfModuleInputMap = [:]
145 userParams.each { userParam ->
146 vfModuleInputMap.put(userParam.name, userParam.value.toString())
148 execution.setVariable('vfModuleInputParams', vfModuleInputMap)
150 // disableRollback (true or false)
151 def disableRollback = requestMap.requestDetails.requestInfo.suppressRollback
152 execution.setVariable('disableRollback', disableRollback)
153 msoLogger.debug('disableRollback (suppressRollback) from request: ' + disableRollback)
159 public void sendSyncResponse (DelegateExecution execution, isDebugEnabled) {
160 def volumeGroupId = execution.getVariable('volumeGroupId')
161 def requestId = execution.getVariable("mso-request-id")
162 def serviceInstanceId = execution.getVariable("serviceInstanceId")
164 String syncResponse = """{"requestReferences":{"instanceId":"${volumeGroupId}","requestId":"${requestId}"}}""".trim()
166 msoLogger.debug("Sync Response: " + "\n" + syncResponse)
167 sendWorkflowResponse(execution, 200, syncResponse)
169 execution.setVariable(prefix+'syncResponseSent', true)
173 public void sendSyncError (DelegateExecution execution, isDebugEnabled) {
174 WorkflowException we = execution.getVariable('WorkflowException')
175 def errorCode = we?.getErrorCode()
176 def errorMessage = we?.getErrorMessage()
177 //default to 400 since only invalid request will trigger this method
178 sendWorkflowResponse(execution, 400, errorMessage)
183 * Create a WorkflowException
185 * @param isDebugEnabled
187 public void buildWorkflowException(DelegateExecution execution, int errorCode, errorMessage, isDebugEnabled) {
188 msoLogger.debug(errorMessage)
189 (new ExceptionUtil()).buildWorkflowException(execution, 2500, errorMessage)
194 * Build Infra DB Request
196 * @param isDebugEnabled
198 public void prepareDbInfraSuccessRequest(DelegateExecution execution, isDebugEnabled) {
199 def dbVnfOutputs = execution.getVariable(prefix+'volumeOutputs')
200 def requestId = execution.getVariable('mso-request-id')
201 def statusMessage = "VolumeGroup successfully created."
202 def requestStatus = "COMPLETED"
206 from: $gVolumeGroup/aai:volume-group-id/text()
207 to: vnfreq:volume-outputs/vnfreq:volume-group-id
209 // for now assume, generated volumeGroupId is accepted
210 def volumeGroupId = execution.getVariable(prefix+'volumeGroupId')
213 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
216 <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
217 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
218 <lastModifiedBy>BPMN</lastModifiedBy>
219 <statusMessage>${MsoUtils.xmlEscape(statusMessage)}</statusMessage>
220 <responseBody></responseBody>
221 <requestStatus>${MsoUtils.xmlEscape(requestStatus)}</requestStatus>
222 <progress>${MsoUtils.xmlEscape(progress)}</progress>
223 <vnfOutputs>${MsoUtils.xmlEscape(dbVnfOutputs)}</vnfOutputs>
224 <volumeGroupId>${MsoUtils.xmlEscape(volumeGroupId)}</volumeGroupId>
225 </ns:updateInfraRequest>
227 </soapenv:Envelope>"""
229 String buildDBRequestAsString = utils.formatXml(dbRequest)
230 execution.setVariable(prefix+"createDBRequest", buildDBRequestAsString)
231 msoLogger.debug("DB Infra Request: " + buildDBRequestAsString)
236 * Build CommpleteMsoProcess request
238 * @param isDebugEnabled
240 public void postProcessResponse (DelegateExecution execution, isDebugEnabled) {
242 def dbReturnCode = execution.getVariable(prefix+'dbReturnCode')
243 def createDBResponse = execution.getVariable(prefix+'createDBResponse')
245 msoLogger.debug('DB return code: ' + dbReturnCode)
246 msoLogger.debug('DB response: ' + createDBResponse)
248 def requestId = execution.getVariable("mso-request-id")
249 def source = execution.getVariable(prefix+'source')
251 String msoCompletionRequest =
252 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
253 xmlns:ns="http://org.onap/so/request/types/v1">
254 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
255 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
256 <action>CREATE</action>
257 <source>${MsoUtils.xmlEscape(source)}</source>
259 <aetgt:status-message>Volume Group has been created successfully.</aetgt:status-message>
260 <aetgt:mso-bpel-name>BPMN VF Module Volume action: CREATE</aetgt:mso-bpel-name>
261 </aetgt:MsoCompletionRequest>"""
263 String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
265 execution.setVariable(prefix+'Success', true)
266 execution.setVariable(prefix+'CompleteMsoProcessRequest', xmlMsoCompletionRequest)
267 msoLogger.debug(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
271 public void prepareFalloutHandlerRequest(DelegateExecution execution, isDebugEnabled) {
273 WorkflowException we = execution.getVariable('WorkflowException')
274 def errorCode = we?.getErrorCode()
275 def errorMessage = we?.getErrorMessage()
277 def requestId = execution.getVariable("mso-request-id")
278 def source = execution.getVariable(prefix+'source')
280 String falloutHandlerRequest =
281 """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
282 xmlns:ns="http://org.onap/so/request/types/v1"
283 xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
284 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
285 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
286 <action>CREATE</action>
287 <source>${MsoUtils.xmlEscape(source)}</source>
289 <aetgt:WorkflowException>
290 <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
291 <aetgt:ErrorCode>${MsoUtils.xmlEscape(errorCode)}</aetgt:ErrorCode>
292 </aetgt:WorkflowException>
294 </aetgt:FalloutHandlerRequest>"""
297 String xmlHandlerRequest = utils.formatXml(falloutHandlerRequest)
299 execution.setVariable(prefix+'FalloutHandlerRequest', xmlHandlerRequest)
300 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Overall Error Response going to FalloutHandler", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "\n" + xmlHandlerRequest);
305 * Query AAI service instance
307 * @param isDebugEnabled
309 public void callRESTQueryAAIServiceInstance(DelegateExecution execution, isDebugEnabled) {
311 def request = execution.getVariable(prefix+"Request")
312 def serviceInstanceId = utils.getNodeText(request, "service-instance-id")
314 AaiUtil aaiUtil = new AaiUtil(this)
315 String aaiEndpoint = aaiUtil.getSearchNodesQueryEndpoint(execution)
317 def String queryAAIRequest = aaiEndpoint + "?search-node-type=service-instance&filter=service-instance-id:EQUALS:" + serviceInstanceId
318 msoLogger.debug("AAI query service instance request: " + queryAAIRequest)
320 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIRequest)
322 String returnCode = response.getStatusCode()
323 String aaiResponseAsString = response.getResponseBodyAsString()
325 msoLogger.debug("AAI query service instance return code: " + returnCode)
326 msoLogger.debug("AAI query service instance response: " + aaiResponseAsString)
328 ExceptionUtil exceptionUtil = new ExceptionUtil()
330 if (returnCode=='200') {
331 msoLogger.debug('Service instance ' + serviceInstanceId + ' found in AAI.')
333 if (returnCode=='404') {
334 def message = 'Service instance ' + serviceInstanceId + ' was not found in AAI. Return code: 404.'
335 msoLogger.debug(message)
336 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, message)
338 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
339 throw new BpmnError("MSOWorkflowException")
344 public void logAndSaveOriginalException(DelegateExecution execution, isDebugLogEnabled) {
345 logWorkflowException(execution, 'CreateVfModuleVolumeInfraV1 caught an event')
346 saveWorkflowException(execution, 'CVMVINFRAV1_originalWorkflowException')
349 public void validateRollbackResponse(DelegateExecution execution, isDebugLogEnabled) {
351 def originalException = execution.getVariable("CVMVINFRAV1_originalWorkflowException")
352 execution.setVariable("WorkflowException", originalException)
353 execution.setVariable("RollbackCompleted", true)