965d178cce543c1ffcb3c949e1a40ca83dd66846
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / CreateVfModuleVolumeInfraV1.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.scripts
22
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
34
35 import groovy.json.JsonOutput
36 import groovy.json.JsonSlurper
37
38 class CreateVfModuleVolumeInfraV1 extends AbstractServiceTaskProcessor {
39         
40         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, CreateVfModuleVolumeInfraV1.class);
41         public static final String  prefix='CVMVINFRAV1_'
42
43         /**
44          * Perform initial processing, such as request validation, initialization of variables, etc.
45          * * @param execution
46          */
47         public void preProcessRequest (DelegateExecution execution) {
48                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
49                 setBasicDBAuthHeader(execution, isDebugEnabled)
50                 preProcessRequest(execution, isDebugEnabled)
51         }
52
53
54         /**
55          * Perform initial processing, such as request validation, initialization of variables, etc.
56          * @param execution
57          * @param isDebugEnabled
58          */
59         public void preProcessRequest (DelegateExecution execution, isDebugEnabled) {
60
61                 execution.setVariable("prefix",prefix)
62                 setSuccessIndicator(execution, false)
63                 execution.setVariable(prefix+'syncResponseSent', false)
64
65                 String createVolumeIncoming = validateRequest(execution, 'vnfId')
66                 msoLogger.debug(createVolumeIncoming)
67
68                 try {
69                         def jsonSlurper = new JsonSlurper()
70                         Map reqMap = jsonSlurper.parseText(createVolumeIncoming)
71                         setupVariables(execution, reqMap, isDebugEnabled)
72                         msoLogger.debug("XML request:\n" + createVolumeIncoming)
73                 }
74                 catch(groovy.json.JsonException je) {
75                         (new ExceptionUtil()).buildAndThrowWorkflowException(execution, 2500, 'Request is not a valid JSON document')
76                 }
77
78                 // For rollback in this flow
79                 setBasicDBAuthHeader(execution, isDebugEnabled)
80                 setRollbackEnabled(execution, isDebugEnabled)
81         }
82
83         
84         /**
85          * Set up variables that will be passed to the BB DoCreatevfModuleVolume flow 
86          * @param execution
87          * @param requestMap
88          * @param serviceInstanceId
89          * @param isDebugLogEnabled
90          */
91         public void setupVariables(DelegateExecution execution, Map requestMap, isDebugLogEnabled) {
92                 
93                 def jsonOutput = new JsonOutput()
94                 
95                 // volumeGroupId - is generated
96                 String volumeGroupId = UUID.randomUUID()
97                 execution.setVariable('volumeGroupId', volumeGroupId)
98                 msoLogger.debug("Generated volumeGroupId: " + volumeGroupId)
99                 
100                 // volumeGroupName
101                 def volGrpName = requestMap.requestDetails.requestInfo?.instanceName ?: ''
102                 execution.setVariable('volumeGroupName', volGrpName)
103
104                 // vfModuleModelInfo
105                 def vfModuleModelInfo = jsonOutput.toJson(requestMap.requestDetails?.modelInfo)
106                 execution.setVariable('vfModuleModelInfo', vfModuleModelInfo)
107                 
108                 // lcpCloudRegonId
109                 def lcpCloudRegionId = requestMap.requestDetails.cloudConfiguration.lcpCloudRegionId
110                 execution.setVariable('lcpCloudRegionId', lcpCloudRegionId)
111                 
112                 // cloudOwner
113                 def cloudOwner = requestMap.requestDetails.cloudConfiguration.cloudOwner
114                 execution.setVariable('cloudOwner', cloudOwner)
115                 
116                 // tenant
117                 def tenantId = requestMap.requestDetails.cloudConfiguration.tenantId
118                 execution.setVariable('tenantId', tenantId)
119                 
120                 // source
121                 def source = requestMap.requestDetails.requestInfo.source
122                 execution.setVariable(prefix+'source', source)
123                 
124                 // vnfType and asdcServiceModelVersion
125                 
126                 def serviceName = ''
127                 def asdcServiceModelVersion = ''
128                 def modelCustomizationName = ''
129                 
130                 def relatedInstanceList = requestMap.requestDetails.relatedInstanceList
131                 relatedInstanceList.each {
132                         if (it.relatedInstance.modelInfo?.modelType == 'service') {
133                                 serviceName = it.relatedInstance.modelInfo?.modelName
134                                 asdcServiceModelVersion = it.relatedInstance.modelInfo?.modelVersion
135                         }
136                         if (it.relatedInstance.modelInfo?.modelType == 'vnf') {
137                                 modelCustomizationName = it.relatedInstance.modelInfo?.modelCustomizationName
138                         }
139                 }
140                 
141                 def vnfType = serviceName + '/' + modelCustomizationName
142                 execution.setVariable('vnfType', vnfType)
143                 execution.setVariable('asdcServiceModelVersion', asdcServiceModelVersion)
144                 
145                 // vfModuleInputParams
146                 def userParams = requestMap.requestDetails?.requestParameters?.userParams
147                 Map<String, String> vfModuleInputMap = [:]
148                 
149                 userParams.each { userParam ->
150                         vfModuleInputMap.put(userParam.name, userParam.value.toString())
151                 }
152                 execution.setVariable('vfModuleInputParams', vfModuleInputMap)
153
154                 // disableRollback (true or false)
155                 def disableRollback = requestMap.requestDetails.requestInfo.suppressRollback
156                 execution.setVariable('disableRollback', disableRollback)
157                 msoLogger.debug('disableRollback (suppressRollback) from request: ' + disableRollback)
158                 
159         }
160
161         
162         
163         public void sendSyncResponse (DelegateExecution execution, isDebugEnabled) {
164                 def volumeGroupId = execution.getVariable('volumeGroupId')
165                 def requestId = execution.getVariable("mso-request-id")
166                 def serviceInstanceId = execution.getVariable("serviceInstanceId")
167
168                 String syncResponse = """{"requestReferences":{"instanceId":"${volumeGroupId}","requestId":"${requestId}"}}""".trim()
169
170                 msoLogger.debug("Sync Response: " + "\n" + syncResponse)
171                 sendWorkflowResponse(execution, 200, syncResponse)
172
173                 execution.setVariable(prefix+'syncResponseSent', true)
174         }
175
176
177         public void sendSyncError (DelegateExecution execution, isDebugEnabled) {
178                 WorkflowException we = execution.getVariable('WorkflowException')
179                 def errorCode = we?.getErrorCode()
180                 def errorMessage = we?.getErrorMessage()
181                 //default to 400 since only invalid request will trigger this method
182                 sendWorkflowResponse(execution, 400, errorMessage)
183         }
184
185
186         /**
187          * Create a WorkflowException
188          * @param execution
189          * @param isDebugEnabled
190          */
191         public void buildWorkflowException(DelegateExecution execution, int errorCode, errorMessage, isDebugEnabled) {
192                 msoLogger.debug(errorMessage)
193                 (new ExceptionUtil()).buildWorkflowException(execution, 2500, errorMessage)
194         }
195
196
197         /**
198          * Build Infra DB Request
199          * @param execution
200          * @param isDebugEnabled
201          */
202         public void prepareDbInfraSuccessRequest(DelegateExecution execution, isDebugEnabled) {
203                 def dbVnfOutputs = execution.getVariable(prefix+'volumeOutputs')
204                 def requestId = execution.getVariable('mso-request-id')
205                 def statusMessage = "VolumeGroup successfully created."
206                 def requestStatus = "COMPLETED"
207                 def progress = "100"
208                 
209                 /*
210                 from: $gVolumeGroup/aai:volume-group-id/text()
211                 to: vnfreq:volume-outputs/vnfreq:volume-group-id
212                 */
213                 // for now assume, generated volumeGroupId is accepted
214                 def volumeGroupId = execution.getVariable(prefix+'volumeGroupId')
215
216                 String dbRequest =
217                         """<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
218                                 <soapenv:Header/>
219                                 <soapenv:Body>
220                                         <ns:updateInfraRequest xmlns:ns="http://org.onap.so/requestsdb">
221                                                 <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
222                                                 <lastModifiedBy>BPMN</lastModifiedBy>
223                                                 <statusMessage>${MsoUtils.xmlEscape(statusMessage)}</statusMessage>
224                                                 <responseBody></responseBody>
225                                                 <requestStatus>${MsoUtils.xmlEscape(requestStatus)}</requestStatus>
226                                                 <progress>${MsoUtils.xmlEscape(progress)}</progress>
227                                                 <vnfOutputs>${MsoUtils.xmlEscape(dbVnfOutputs)}</vnfOutputs>
228                                                 <volumeGroupId>${MsoUtils.xmlEscape(volumeGroupId)}</volumeGroupId>
229                                         </ns:updateInfraRequest>
230                                 </soapenv:Body>
231                            </soapenv:Envelope>"""
232
233                 String buildDBRequestAsString = utils.formatXml(dbRequest)
234                 execution.setVariable(prefix+"createDBRequest", buildDBRequestAsString)
235                 msoLogger.debug("DB Infra Request: " + buildDBRequestAsString)
236         }
237
238
239         /**
240          * Build CommpleteMsoProcess request
241          * @param execution
242          * @param isDebugEnabled
243          */
244         public void postProcessResponse (DelegateExecution execution, isDebugEnabled) {
245
246                 def dbReturnCode = execution.getVariable(prefix+'dbReturnCode')
247                 def createDBResponse =  execution.getVariable(prefix+'createDBResponse')
248
249                 msoLogger.debug('DB return code: ' + dbReturnCode)
250                 msoLogger.debug('DB response: ' + createDBResponse)
251
252                 def requestId = execution.getVariable("mso-request-id")
253                 def source = execution.getVariable(prefix+'source')
254
255                 String msoCompletionRequest =
256                         """<aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
257                                                         xmlns:ns="http://org.onap/so/request/types/v1">
258                                         <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
259                                                 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
260                                                 <action>CREATE</action>
261                                                 <source>${MsoUtils.xmlEscape(source)}</source>
262                                         </request-info>
263                                         <aetgt:status-message>Volume Group has been created successfully.</aetgt:status-message>
264                                         <aetgt:mso-bpel-name>BPMN VF Module Volume action: CREATE</aetgt:mso-bpel-name>
265                                 </aetgt:MsoCompletionRequest>"""
266
267                 String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
268
269                 execution.setVariable(prefix+'Success', true)
270                 execution.setVariable(prefix+'CompleteMsoProcessRequest', xmlMsoCompletionRequest)
271                 msoLogger.debug(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
272
273         }
274
275         public void prepareFalloutHandlerRequest(DelegateExecution execution, isDebugEnabled) {
276
277                 WorkflowException we = execution.getVariable('WorkflowException')
278                 def errorCode = we?.getErrorCode()
279                 def errorMessage = we?.getErrorMessage()
280
281                 def requestId = execution.getVariable("mso-request-id")
282                 def source = execution.getVariable(prefix+'source')
283
284                 String falloutHandlerRequest =
285                         """<aetgt:FalloutHandlerRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
286                                                              xmlns:ns="http://org.onap/so/request/types/v1"
287                                                              xmlns:wfsch="http://org.onap/so/workflow/schema/v1">
288                                    <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
289                                       <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
290                                       <action>CREATE</action>
291                                       <source>${MsoUtils.xmlEscape(source)}</source>
292                                    </request-info>
293                                            <aetgt:WorkflowException>
294                                               <aetgt:ErrorMessage>${MsoUtils.xmlEscape(errorMessage)}</aetgt:ErrorMessage>
295                                               <aetgt:ErrorCode>${MsoUtils.xmlEscape(errorCode)}</aetgt:ErrorCode>
296                                                 </aetgt:WorkflowException>
297
298                                 </aetgt:FalloutHandlerRequest>"""
299
300                 // Format Response
301                 String xmlHandlerRequest = utils.formatXml(falloutHandlerRequest)
302
303                 execution.setVariable(prefix+'FalloutHandlerRequest', xmlHandlerRequest)
304                 msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Overall Error Response going to FalloutHandler", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "\n" + xmlHandlerRequest);
305         }
306
307
308         /**
309          * Query AAI service instance
310          * @param execution
311          * @param isDebugEnabled
312          */
313         public void callRESTQueryAAIServiceInstance(DelegateExecution execution, isDebugEnabled) {
314
315                 def request = execution.getVariable(prefix+"Request")
316                 def serviceInstanceId = utils.getNodeText(request, "service-instance-id")
317
318                 AaiUtil aaiUtil = new AaiUtil(this)
319                 String aaiEndpoint = aaiUtil.getSearchNodesQueryEndpoint(execution)
320
321                 def String queryAAIRequest = aaiEndpoint + "?search-node-type=service-instance&filter=service-instance-id:EQUALS:" + serviceInstanceId
322                 msoLogger.debug("AAI query service instance request: " + queryAAIRequest)
323
324                 APIResponse response = aaiUtil.executeAAIGetCall(execution, queryAAIRequest)
325
326                 String returnCode = response.getStatusCode()
327                 String aaiResponseAsString = response.getResponseBodyAsString()
328
329                 msoLogger.debug("AAI query service instance return code: " + returnCode)
330                 msoLogger.debug("AAI query service instance response: " + aaiResponseAsString)
331
332                 ExceptionUtil exceptionUtil = new ExceptionUtil()
333
334                 if (returnCode=='200') {
335                         msoLogger.debug('Service instance ' + serviceInstanceId + ' found in AAI.')
336                 } else {
337                         if (returnCode=='404') {
338                                 def message = 'Service instance ' + serviceInstanceId + ' was not found in AAI. Return code: 404.'
339                                 msoLogger.debug(message)
340                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, message)
341                         } else {
342                                 WorkflowException aWorkflowException = exceptionUtil.MapAAIExceptionToWorkflowException(aaiResponseAsString, execution)
343                                 throw new BpmnError("MSOWorkflowException")
344                         }
345                 }
346         }
347         
348         public void logAndSaveOriginalException(DelegateExecution execution, isDebugLogEnabled) {
349                 logWorkflowException(execution, 'CreateVfModuleVolumeInfraV1 caught an event')
350                 saveWorkflowException(execution, 'CVMVINFRAV1_originalWorkflowException')
351         }
352         
353         public void validateRollbackResponse(DelegateExecution execution, isDebugLogEnabled) {
354
355                 def originalException = execution.getVariable("CVMVINFRAV1_originalWorkflowException")
356                 execution.setVariable("WorkflowException", originalException)
357                 execution.setVariable("RollbackCompleted", true)
358
359         }
360 }