79aa745792cacff72f73754f41c9835d6b57e02d
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.bpmn.infrastructure.scripts
24
25 import org.onap.so.logger.LoggingAnchor
26 import org.camunda.bpm.engine.delegate.BpmnError
27 import org.camunda.bpm.engine.delegate.DelegateExecution
28 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor;
29 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
30 import org.onap.so.bpmn.common.scripts.MsoUtils
31 import org.onap.so.bpmn.common.scripts.VidUtils;
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.bpmn.core.json.JsonUtils
34 import org.onap.so.logger.ErrorCode;
35 import org.onap.so.logger.MessageEnum
36 import org.slf4j.Logger
37 import org.slf4j.LoggerFactory
38
39 import groovy.json.JsonSlurper
40
41
42
43 public class DeleteVfModuleInfra extends AbstractServiceTaskProcessor {
44
45     private static final Logger logger = LoggerFactory.getLogger( DeleteVfModuleInfra.class);
46         ExceptionUtil exceptionUtil = new ExceptionUtil()
47         JsonUtils jsonUtil = new JsonUtils()
48         /**
49          * Initialize the flow's variables.
50          *
51          * @param execution The flow's execution instance.
52          */
53         public void initProcessVariables(DelegateExecution execution) {
54                 execution.setVariable('prefix', 'DELVfModI_')
55                 execution.setVariable('DELVfModI_requestInfo', null)
56                 execution.setVariable('DELVfModI_requestId', null)
57                 execution.setVariable('DELVfModI_source', null)
58                 execution.setVariable('DELVfModI_vnfInputs', null)
59                 execution.setVariable('DELVfModI_vnfId', null)
60                 execution.setVariable('DELVfModI_vfModuleId', null)
61                 execution.setVariable('DELVfModI_tenantId', null)
62                 execution.setVariable('DELVfModI_volumeGroupId', null)
63                 execution.setVariable('DELVfModI_vnfParams', null)
64                 execution.setVariable('DELVfModI_updateInfraRequest', null)
65                 execution.setVariable('DeleteVfModuleRequest', null)
66                 execution.setVariable('DeleteVfModuleSuccessIndicator', false)
67         }
68
69         /**
70          * Process the incoming DELETE_VF_MODULE vnf-request.
71          *
72          * @param execution The flow's execution instance.
73          */
74         public void preProcessRequest(DelegateExecution execution) {
75                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
76                         'execution=' + execution.getId() +
77                         ')'
78                 logger.trace('Entered ' + method)
79                 execution.setVariable("isVidRequest", "false")
80                 initProcessVariables(execution)
81
82                 def prefix = execution.getVariable('prefix')
83
84                 def incomingRequest = execution.getVariable('bpmnRequest')
85                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
86                 
87                 logger.debug("Incoming Infra Request: " + incomingRequest)
88
89                 // check if request is xml or json
90                 try {
91                         def jsonSlurper = new JsonSlurper()
92                         Map reqMap = jsonSlurper.parseText(incomingRequest)
93                         logger.debug(" Request is in JSON format.")
94
95                         def serviceInstanceId = execution.getVariable('serviceInstanceId')
96                         logger.debug("serviceInstanceId is: " + serviceInstanceId)
97                         def vnfId = execution.getVariable('vnfId')
98                         logger.debug("vnfId is: " + vnfId)
99                         def cloudConfiguration = jsonUtil.getJsonValue(incomingRequest, "requestDetails.cloudConfiguration")
100                         execution.setVariable("cloudConfiguration", cloudConfiguration)
101                         logger.debug("CloudConfiguration is: " + cloudConfiguration)
102                         def vfModuleModelInfo = jsonUtil.getJsonValue(incomingRequest, "requestDetails.modelInfo")
103
104                         execution.setVariable("vfModuleModelInfo", vfModuleModelInfo)
105                         logger.debug("VfModuleModelInfo is: " + vfModuleModelInfo)
106                         
107                         // This is aLaCarte flow, so aLaCarte flag is always on
108                         execution.setVariable('aLaCarte', true)
109                         
110                         def vidUtils = new VidUtils(this)
111
112                         String requestInXmlFormat = vidUtils.createXmlVfModuleRequest(execution, reqMap, 'DELETE_VF_MODULE', serviceInstanceId)
113
114                         logger.debug(" Request in XML format: " + requestInXmlFormat)
115
116                         setBasicDBAuthHeader(execution, isDebugLogEnabled)
117                         
118                         execution.setVariable(prefix + 'Request', requestInXmlFormat)
119                         execution.setVariable(prefix+'vnfId', vnfId)
120                         execution.setVariable("isVidRequest", "true")
121
122                 }
123                 catch(groovy.json.JsonException je) {
124                         logger.debug(" Request is not in JSON format.")
125                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Internal Error - During PreProcess Request")
126
127                 }
128                 catch(Exception e) {
129                         String restFaultMessage = e.getMessage()
130                         logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), "Caught exception",
131                                         "BPMN", ErrorCode.UnknownError.getValue(),
132                                         "Exception is:\n" + e);
133                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, "Internal Error - During PreProcess Request")
134                 }
135
136
137                 try {
138
139                         String request = validateRequest(execution)
140                         execution.setVariable('DeleteVfModuleRequest', request)
141                         logger.debug("DeleteVfModuleInfra Request: " + request)
142
143                         def requestInfo = getRequiredNodeXml(execution, request, 'request-info')
144                         execution.setVariable('DELVfModI_requestInfo', requestInfo)
145                         execution.setVariable('DELVfModI_requestId', getRequiredNodeText(execution, requestInfo, 'request-id'))
146                         execution.setVariable('DELVfModI_source', getNodeTextForce(requestInfo, 'source'))
147
148                         def vnfInputs = getRequiredNodeXml(execution, request, 'vnf-inputs')
149                         execution.setVariable('DELVfModI_vnfInputs', vnfInputs)
150                         execution.setVariable('DELVfModI_vnfId', getRequiredNodeText(execution, vnfInputs, 'vnf-id'))
151                         execution.setVariable('DELVfModI_vfModuleId', getRequiredNodeText(execution, vnfInputs, 'vf-module-id'))
152                         execution.setVariable('DELVfModI_vfModuleName', getNodeTextForce(vnfInputs, 'vf-module-name'))
153                         execution.setVariable('DELVfModI_tenantId', getRequiredNodeText(execution, vnfInputs, 'tenant-id'))
154                         execution.setVariable('DELVfModI_volumeGroupId', getNodeTextForce(vnfInputs, 'volume-group-id'))
155
156                         def vnfParams = utils.getNodeXml(request, 'vnf-params')
157                         execution.setVariable('DELVfModI_vnfParams', vnfParams)
158
159                         logger.trace('Exited ' + method)
160                 } catch (BpmnError e) {
161                         throw e;
162                 } catch (Exception e) {
163                         logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
164                                         "Caught exception in " + method, "BPMN",
165                                         ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
166                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
167                 }
168         }
169
170         /**
171          * Sends the 'IN_PROGRESS' synchronous response.
172          *
173          * @param execution The flow's execution instance.
174          */
175         public void sendSynchResponse(DelegateExecution execution) {
176                 def method = getClass().getSimpleName() + '.sendResponse(' +
177                         'execution=' + execution.getId() +
178                         ')'
179                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
180                 logger.trace('Entered ' + method)
181
182                 try {
183                         def requestInfo = execution.getVariable('DELVfModI_requestInfo')
184                         def requestId = execution.getVariable('DELVfModI_requestId')
185                         def source = execution.getVariable('DELVfModI_source')
186                         def progress = getNodeTextForce(requestInfo, 'progress')
187                         if (progress.isEmpty()) {
188                                 progress = '0'
189                         }
190                         def startTime = getNodeTextForce(requestInfo, 'start-time')
191                         if (startTime.isEmpty()) {
192                                 startTime = System.currentTimeMillis()
193                         }
194
195                         // RESTResponse (for API Handler (APIH) Reply Task)
196                         def vfModuleId = execution.getVariable('DELVfModI_vfModuleId')
197                         String synchResponse = """{"requestReferences":{"instanceId":"${vfModuleId}","requestId":"${requestId}"}}""".trim()
198
199                         logger.debug("DeleteVfModuleInfra Synch Response: " + synchResponse)
200                         sendWorkflowResponse(execution, 200, synchResponse)
201
202                         logger.trace('Exited ' + method)
203                 } catch (BpmnError e) {
204                         throw e;
205                 } catch (Exception e) {
206                         logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
207                                         "Caught exception in " + method, "BPMN",
208                                         ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
209                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())
210                 }
211         }
212
213         /**
214          * Currently passing the entire DELETE_VF_MODULE vnf-request to DoDeleteVfModule.
215          * 'DeleteVfModuleRequest' is now being set in preProcessRequest().
216          * TBD: may want to eventually create a specific request that only contains the needed fields.
217          *
218          * @param execution The flow's execution instance.
219          */
220         public void prepDoDeleteVfModule(DelegateExecution execution) {
221                 def method = getClass().getSimpleName() + '.prepDoDeleteVfModule(' +
222                         'execution=' + execution.getId() +
223                         ')'
224                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
225                 logger.trace('Entered ' + method)
226
227                 try {
228
229                         logger.trace('Exited ' + method)
230                 } catch (BpmnError e) {
231                         throw e;
232                 } catch (Exception e) {
233                         logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), 'Caught exception in '
234                                         + method, "BPMN", ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
235                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepDoDeleteVfModule(): ' + e.getMessage())
236                 }
237         }
238
239         /**
240          * Prepare the DB update to add an entry for the Vf Module request.
241          *
242          * @param execution The flow's execution instance.
243          */
244         public void prepUpdateInfraRequest(DelegateExecution execution) {
245                 def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
246                         'execution=' + execution.getId() +
247                         ')'
248                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
249                 logger.trace('Entered ' + method)
250
251                 try {
252                         def requestId = execution.getVariable('DELVfModI_requestId')
253                         def vnfId = execution.getVariable('DELVfModI_vnfId')
254                         def vfModuleId = execution.getVariable('DELVfModI_vfModuleId')
255                         def tenantId = execution.getVariable('DELVfModI_tenantId')
256                         def volumeGroupId = execution.getVariable('DELVfModI_volumeGroupId')
257
258                         String updateInfraRequest = """
259                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
260                                         xmlns:req="http://org.onap.so/requestsdb">
261                                         <soapenv:Header/>
262                                         <soapenv:Body>
263                                                 <req:updateInfraRequest>
264                                                         <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
265                                                         <lastModifiedBy>BPMN</lastModifiedBy>
266                                                         <requestStatus>COMPLETED</requestStatus>
267                                                         <progress>100</progress>
268                                                 </req:updateInfraRequest>
269                                         </soapenv:Body>
270                                 </soapenv:Envelope>
271                         """
272
273                         updateInfraRequest = utils.formatXml(updateInfraRequest)
274                         execution.setVariable('DELVfModI_updateInfraRequest', updateInfraRequest)
275                         logger.debug('Request for Update Infra Request:\n' + updateInfraRequest)
276
277                         logger.trace('Exited ' + method)
278                 } catch (BpmnError e) {
279                         throw e;
280                 } catch (Exception e) {
281                         logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
282                                         'Caught exception in ' + method, "BPMN",
283                                         ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
284                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepInfraRequest(): ' + e.getMessage())
285                 }
286         }
287
288         /**
289          * Builds a "CompletionHandler" request and stores it in the specified execution variable.
290          *
291          * @param execution the execution
292          * @param resultVar the execution variable in which the result will be stored
293          */
294         public void completionHandlerPrep(DelegateExecution execution, String resultVar) {
295                 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
296                         'execution=' + execution.getId() +
297                         ', resultVar=' + resultVar +
298                         ')'
299                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
300                 logger.trace('Entered ' + method)
301
302                 try {
303                         def request = execution.getVariable("DeleteVfModuleRequest")
304                         def requestInfo = utils.getNodeXml(request, 'request-info', false)
305                         def action = utils.getNodeText(requestInfo, "action")
306
307                         String content =
308                                         """  <aetgt:MsoCompletionRequest xmlns:aetgt="http://org.onap/so/workflow/schema/v1"
309                                                    xmlns:ns="http://org.onap/so/request/types/v1"
310                                                    xmlns:ns8="http://org.onap/so/workflow/schema/v1">
311                 <request-info xmlns="http://org.onap/so/infra/vnf-request/v1">
312                         ${requestInfo}
313                         </request-info>
314                         <ns8:status-message>Vf Module has been deleted successfully.</ns8:status-message>
315                         <ns8:mso-bpel-name>BPMN</ns8:mso-bpel-name>
316                         </aetgt:MsoCompletionRequest>"""
317
318                         content = utils.formatXml(content)
319                         logger.debug(resultVar + ' = ' + System.lineSeparator() + content)
320                         execution.setVariable(resultVar, content)
321
322                         logger.trace('Exited ' + method)
323                 } catch (BpmnError e) {
324                         throw e;
325                 } catch (Exception e) {
326                         logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
327                                         'Caught exception in ' + method, "BPMN",
328                                         ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
329                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, 'Internal Error')
330                 }
331         }
332
333         /**
334          * Builds a "FalloutHandler" request and stores it in the specified execution variable.
335          *
336          * @param execution the execution
337          * @param resultVar the execution variable in which the result will be stored
338          */
339         public void falloutHandlerPrep(DelegateExecution execution, String resultVar) {
340                 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
341                         'execution=' + execution.getId() +
342                         ', resultVar=' + resultVar +
343                         ')'
344                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
345                 logger.trace('Entered ' + method)
346
347                 try {
348                         def prefix = execution.getVariable('prefix')
349                         def request = execution.getVariable("DeleteVfModuleRequest")
350                         def requestInfo = utils.getNodeXml(request, 'request-info', false)
351                         def WorkflowException workflowException = execution.getVariable("WorkflowException")
352                         def errorResponseCode = workflowException.getErrorCode()
353                         def errorResponseMsg = workflowException.getErrorMessage()
354                         def encErrorResponseMsg = ""
355                         if (errorResponseMsg != null) {
356                                 encErrorResponseMsg = errorResponseMsg
357                         }
358
359                         String content = """
360                         <sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
361                                         xmlns:reqtype="http://org.onap/so/request/types/v1"
362                                         xmlns:msoservtypes="http://org.onap/so/request/types/v1"
363                                         xmlns:structuredtypes="http://org.onap/so/structured/types/v1">
364                                         ${requestInfo}
365                                         <sdncadapterworkflow:WorkflowException>
366                                                 <sdncadapterworkflow:ErrorMessage>${MsoUtils.xmlEscape(encErrorResponseMsg)}</sdncadapterworkflow:ErrorMessage>
367                                                 <sdncadapterworkflow:ErrorCode>${MsoUtils.xmlEscape(errorResponseCode)}</sdncadapterworkflow:ErrorCode>
368                                         </sdncadapterworkflow:WorkflowException>
369                                 </sdncadapterworkflow:FalloutHandlerRequest>
370                         """
371                         content = utils.formatXml(content)
372                         logger.debug(resultVar + ' = ' + System.lineSeparator() + content)
373                         execution.setVariable(resultVar, content)
374
375                         logger.trace('Exited ' + method)
376                 } catch (BpmnError e) {
377                         throw e;
378                 } catch (Exception e) {
379                         logger.error(LoggingAnchor.FIVE, MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
380                                         'Caught exception in ' + method, "BPMN",
381                                         ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
382                         exceptionUtil.buildWorkflowException(execution, 2000, 'Internal Error')
383                 }
384         }
385 }