2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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.apache.commons.collections.CollectionUtils
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.aai.domain.yang.SearchResults
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor;
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
31 import org.onap.so.bpmn.common.scripts.MsoUtils
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.client.aai.AAIObjectType
34 import org.onap.so.client.aai.AAIResourcesClient
35 import org.onap.so.client.aai.entities.AAIResultWrapper
36 import org.onap.so.client.aai.entities.uri.AAIResourceUri
37 import org.onap.so.client.aai.entities.uri.AAIUriFactory
38 import org.onap.so.logger.MessageEnum
39 import org.onap.so.logger.MsoLogger
40 import org.slf4j.Logger
41 import org.slf4j.LoggerFactory
43 import groovy.json.JsonOutput
44 import groovy.json.JsonSlurper
47 class CreateVfModuleVolumeInfraV1 extends AbstractServiceTaskProcessor {
49 private static final Logger logger = LoggerFactory.getLogger( CreateVfModuleVolumeInfraV1.class);
50 public static final String prefix='CVMVINFRAV1_'
53 * Perform initial processing, such as request validation, initialization of variables, etc.
56 public void preProcessRequest (DelegateExecution execution) {
57 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
58 setBasicDBAuthHeader(execution, isDebugEnabled)
59 preProcessRequest(execution, isDebugEnabled)
64 * Perform initial processing, such as request validation, initialization of variables, etc.
66 * @param isDebugEnabled
68 public void preProcessRequest (DelegateExecution execution, isDebugEnabled) {
70 execution.setVariable("prefix",prefix)
71 setSuccessIndicator(execution, false)
72 execution.setVariable(prefix+'syncResponseSent', false)
74 String createVolumeIncoming = validateRequest(execution, 'vnfId')
75 logger.debug(createVolumeIncoming)
78 def jsonSlurper = new JsonSlurper()
79 Map reqMap = jsonSlurper.parseText(createVolumeIncoming)
80 setupVariables(execution, reqMap, isDebugEnabled)
81 logger.debug("XML request:\n" + createVolumeIncoming)
83 catch(groovy.json.JsonException je) {
84 (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, 'Request is not a valid JSON document')
87 // For rollback in this flow
88 setBasicDBAuthHeader(execution, isDebugEnabled)
89 setRollbackEnabled(execution, isDebugEnabled)
94 * Set up variables that will be passed to the BB DoCreatevfModuleVolume flow
97 * @param serviceInstanceId
98 * @param isDebugLogEnabled
100 public void setupVariables(DelegateExecution execution, Map requestMap, isDebugLogEnabled) {
102 def jsonOutput = new JsonOutput()
104 // volumeGroupId - is generated
105 String volumeGroupId = UUID.randomUUID()
106 execution.setVariable('volumeGroupId', volumeGroupId)
107 logger.debug("Generated volumeGroupId: " + volumeGroupId)
110 def volGrpName = requestMap.requestDetails.requestInfo?.instanceName ?: ''
111 execution.setVariable('volumeGroupName', volGrpName)
114 def vfModuleModelInfo = jsonOutput.toJson(requestMap.requestDetails?.modelInfo)
115 execution.setVariable('vfModuleModelInfo', vfModuleModelInfo)
118 def lcpCloudRegionId = requestMap.requestDetails.cloudConfiguration.lcpCloudRegionId
119 execution.setVariable('lcpCloudRegionId', lcpCloudRegionId)
122 def cloudOwner = requestMap.requestDetails.cloudConfiguration.cloudOwner
123 execution.setVariable('cloudOwner', cloudOwner)
126 def tenantId = requestMap.requestDetails.cloudConfiguration.tenantId
127 execution.setVariable('tenantId', tenantId)
130 def source = requestMap.requestDetails.requestInfo.source
131 execution.setVariable(prefix+'source', source)
133 // vnfType and asdcServiceModelVersion
136 def asdcServiceModelVersion = ''
137 def modelCustomizationName = ''
139 def relatedInstanceList = requestMap.requestDetails.relatedInstanceList
140 relatedInstanceList.each {
141 if (it.relatedInstance.modelInfo?.modelType == 'service') {
142 serviceName = it.relatedInstance.modelInfo?.modelName
143 asdcServiceModelVersion = it.relatedInstance.modelInfo?.modelVersion
145 if (it.relatedInstance.modelInfo?.modelType == 'vnf') {
146 modelCustomizationName = it.relatedInstance.modelInfo?.modelCustomizationName
150 def vnfType = serviceName + '/' + modelCustomizationName
151 execution.setVariable('vnfType', vnfType)
152 execution.setVariable('asdcServiceModelVersion', asdcServiceModelVersion)
154 // vfModuleInputParams
155 def userParams = requestMap.requestDetails?.requestParameters?.userParams
156 Map<String, String> vfModuleInputMap = [:]
158 userParams.each { userParam ->
159 vfModuleInputMap.put(userParam.name, userParam.value.toString())
161 execution.setVariable('vfModuleInputParams', vfModuleInputMap)
163 // disableRollback (true or false)
164 def disableRollback = requestMap.requestDetails.requestInfo.suppressRollback
165 execution.setVariable('disableRollback', disableRollback)
166 logger.debug('disableRollback (suppressRollback) from request: ' + disableRollback)
172 public void sendSyncResponse (DelegateExecution execution, isDebugEnabled) {
173 def volumeGroupId = execution.getVariable('volumeGroupId')
174 def requestId = execution.getVariable("mso-request-id")
175 def serviceInstanceId = execution.getVariable("serviceInstanceId")
177 String syncResponse = """{"requestReferences":{"instanceId":"${volumeGroupId}","requestId":"${requestId}"}}""".trim()
179 logger.debug("Sync Response: " + "\n" + syncResponse)
180 sendWorkflowResponse(execution, 200, syncResponse)
182 execution.setVariable(prefix+'syncResponseSent', true)
186 public void sendSyncError (DelegateExecution execution, isDebugEnabled) {
187 WorkflowException we = execution.getVariable('WorkflowException')
188 def errorCode = we?.getErrorCode()
189 def errorMessage = we?.getErrorMessage()
190 //default to 400 since only invalid request will trigger this method
191 sendWorkflowResponse(execution, 400, errorMessage)
196 * Create a WorkflowException
198 * @param isDebugEnabled
200 public void buildWorkflowException(DelegateExecution execution, int errorCode, errorMessage, isDebugEnabled) {
201 logger.debug(errorMessage)
202 (new ExceptionUtil()).buildWorkflowException(execution, 2500, errorMessage)
207 * Build Infra DB Request
209 * @param isDebugEnabled
211 public void prepareDbInfraSuccessRequest(DelegateExecution execution, isDebugEnabled) {
212 def dbVnfOutputs = execution.getVariable(prefix+'volumeOutputs')
213 def requestId = execution.getVariable('mso-request-id')
214 def statusMessage = "VolumeGroup successfully created."
215 def requestStatus = "COMPLETED"
219 from: $gVolumeGroup/aai:volume-group-id/text()
220 to: vnfreq:volume-outputs/vnfreq:volume-group-id
222 // for now assume, generated volumeGroupId is accepted
223 def volumeGroupId = execution.getVariable(prefix+'volumeGroupId')
226 """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
229 <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
230 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
231 <lastModifiedBy>BPMN</lastModifiedBy>
232 <statusMessage>${MsoUtils.xmlEscape(statusMessage)}</statusMessage>
233 <responseBody></responseBody>
234 <requestStatus>${MsoUtils.xmlEscape(requestStatus)}</requestStatus>
235 <progress>${MsoUtils.xmlEscape(progress)}</progress>
236 <vnfOutputs>${MsoUtils.xmlEscape(dbVnfOutputs)}</vnfOutputs>
237 <volumeGroupId>${MsoUtils.xmlEscape(volumeGroupId)}</volumeGroupId>
238 </ns:updateInfraRequest>
240 </soapenv:Envelope>"""
242 String buildDBRequestAsString = utils.formatXml(dbRequest)
243 execution.setVariable(prefix+"createDBRequest", buildDBRequestAsString)
244 logger.debug("DB Infra Request: " + buildDBRequestAsString)
249 * Build CommpleteMsoProcess request
251 * @param isDebugEnabled
253 public void postProcessResponse (DelegateExecution execution, isDebugEnabled) {
255 def dbReturnCode = execution.getVariable(prefix+'dbReturnCode')
256 def createDBResponse = execution.getVariable(prefix+'createDBResponse')
258 logger.debug('DB return code: ' + dbReturnCode)
259 logger.debug('DB response: ' + createDBResponse)
261 def requestId = execution.getVariable("mso-request-id")
262 def source = execution.getVariable(prefix+'source')
264 String msoCompletionRequest =
265 """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
266 xmlns:ns="http://org.onap/so/request/types/v1">
267 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
268 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
269 <action>CREATE</action>
270 <source>${MsoUtils.xmlEscape(source)}</source>
272 <aetgt:status-message>Volume Group has been created successfully.</aetgt:status-message>
273 <aetgt:mso-bpel-name>BPMN VF Module Volume action: CREATE</aetgt:mso-bpel-name>
274 </aetgt:MsoCompletionRequest>"""
276 String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
278 execution.setVariable(prefix+'Success', true)
279 execution.setVariable(prefix+'CompleteMsoProcessRequest', xmlMsoCompletionRequest)
280 logger.debug(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
284 public void prepareFalloutHandlerRequest(DelegateExecution execution, isDebugEnabled) {
286 WorkflowException we = execution.getVariable('WorkflowException')
287 def errorCode = we?.getErrorCode()
288 def errorMessage = we?.getErrorMessage()
290 def requestId = execution.getVariable("mso-request-id")
291 def source = execution.getVariable(prefix+'source')
293 String falloutHandlerRequest =
294 """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
295 xmlns:ns="http://org.onap/so/request/types/v1"
296 xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
297 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
298 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
299 <action>CREATE</action>
300 <source>${MsoUtils.xmlEscape(source)}</source>
302 <aetgt:WorkflowException>
303 <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
304 <aetgt:ErrorCode>${MsoUtils.xmlEscape(errorCode)}</aetgt:ErrorCode>
305 </aetgt:WorkflowException>
307 </aetgt:FalloutHandlerRequest>"""
310 String xmlHandlerRequest = utils.formatXml(falloutHandlerRequest)
312 execution.setVariable(prefix+'FalloutHandlerRequest', xmlHandlerRequest)
313 logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), "Overall Error Response " +
314 "going to FalloutHandler", "BPMN", MsoLogger.ErrorCode.UnknownError.getValue(), "\n" + xmlHandlerRequest);
319 * Query AAI service instance
321 * @param isDebugEnabled
323 public void callRESTQueryAAIServiceInstance(DelegateExecution execution, isDebugEnabled) {
325 def request = execution.getVariable(prefix+"Request")
326 def serviceInstanceId = utils.getNodeText(request, "service-instance-id")
327 ExceptionUtil exceptionUtil = new ExceptionUtil()
330 AAIResourceUri uri = AAIUriFactory.createNodesUri(AAIObjectType.SERVICE_INSTANCE,serviceInstanceId)
331 if(getAAIClient().exists(uri)){
332 logger.debug('Service instance ' + serviceInstanceId + ' found in AAI.')
334 def message = 'Service instance ' + serviceInstanceId + ' was not found in AAI. Return code: 404.'
335 logger.debug(message)
336 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, message)
338 }catch(BpmnError bpmnError){
340 }catch(Exception ex){
341 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, ex.getMessage())
345 public void logAndSaveOriginalException(DelegateExecution execution, isDebugLogEnabled) {
346 logWorkflowException(execution, 'CreateVfModuleVolumeInfraV1 caught an event')
347 saveWorkflowException(execution, 'CVMVINFRAV1_originalWorkflowException')
350 public void validateRollbackResponse(DelegateExecution execution, isDebugLogEnabled) {
352 def originalException = execution.getVariable("CVMVINFRAV1_originalWorkflowException")
353 execution.setVariable("WorkflowException", originalException)
354 execution.setVariable("RollbackCompleted", true)