35947ee4da5a80c4b33d3893c925f85b3cbf3d8a
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / UpdateVfModule.groovy
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.camunda.bpm.engine.delegate.BpmnError
26 import org.camunda.bpm.engine.delegate.DelegateExecution
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.slf4j.Logger
34 import org.slf4j.LoggerFactory
35
36
37 public class UpdateVfModule extends AbstractServiceTaskProcessor {
38     private static final Logger logger = LoggerFactory.getLogger( UpdateVfModule.class);
39
40         ExceptionUtil exceptionUtil = new ExceptionUtil()
41
42         /**
43          * Initialize the flow's variables.
44          *
45          * @param execution The flow's execution instance.
46          */
47         public void initProcessVariables(DelegateExecution execution) {
48                 execution.setVariable('prefix', 'UPDVfMod_')
49                 execution.setVariable('UPDVfMod_Request', null)
50                 execution.setVariable('UPDVfMod_requestInfo', null)
51                 execution.setVariable('UPDVfMod_requestId', null)
52                 execution.setVariable('UPDVfMod_source', null)
53                 execution.setVariable('UPDVfMod_vnfInputs', null)
54                 execution.setVariable('UPDVfMod_vnfId', null)
55                 execution.setVariable('UPDVfMod_vfModuleId', null)
56                 execution.setVariable('UPDVfMod_tenantId', null)
57                 execution.setVariable('UPDVfMod_volumeGroupId', null)
58                 execution.setVariable('UPDVfMod_vnfParams', null)
59                 execution.setVariable('UPDVfMod_updateInfraRequest', null)
60                 execution.setVariable('UpdateVfModuleSuccessIndicator', false)
61         }
62
63         /**
64          * Check for missing elements in the received request.
65          *
66          * @param execution The flow's execution instance.
67          */
68         public void preProcessRequest(DelegateExecution execution) {
69                 def method = getClass().getSimpleName() + '.preProcessRequest(' +
70                         'execution=' + execution.getId() +
71                         ')'
72
73                 logger.trace('Entered ' + method)
74
75                 try {
76                         initProcessVariables(execution)
77                         String request = validateRequest(execution)
78
79                         logger.debug("UpdateVfModule request: " + request)
80                         def requestInfo = getRequiredNodeXml(execution, request, 'request-info')
81                         execution.setVariable('UPDVfMod_requestInfo', requestInfo)
82                         execution.setVariable('UPDVfMod_requestId', getRequiredNodeText(execution, requestInfo, 'request-id'))
83                         execution.setVariable('UPDVfMod_source', getNodeTextForce(requestInfo, 'source'))
84
85                         def vnfInputs = getRequiredNodeXml(execution, request, 'vnf-inputs')
86                         execution.setVariable('UPDVfMod_vnfInputs', vnfInputs)
87                         execution.setVariable('UPDVfMod_vnfId', getRequiredNodeText(execution, vnfInputs, 'vnf-id'))
88                         execution.setVariable('UPDVfMod_vfModuleId', getRequiredNodeText(execution, vnfInputs, 'vf-module-id'))
89                         execution.setVariable('UPDVfMod_tenantId', getRequiredNodeText(execution, vnfInputs, 'tenant-id'))
90                         execution.setVariable('UPDVfMod_volumeGroupId', getNodeTextForce(vnfInputs, 'volume-group-id'))
91
92                         def vnfParams = utils.getNodeXml(request, 'vnf-params')
93                         execution.setVariable('UPDVfMod_vnfParams', vnfParams)
94
95                         logger.trace('Exited ' + method)
96                 } catch (BpmnError e) {
97                         throw e;
98                 } catch (Exception e) {
99                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
100                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
101                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
102                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in preProcessRequest(): ' + e.getMessage())
103                 }
104         }
105
106         /**
107          * Prepare and send the synchronous response for this flow.
108          *
109          * @param execution The flow's execution instance.
110          */
111         public void sendSynchResponse(DelegateExecution execution) {
112                 def method = getClass().getSimpleName() + '.sendSynchResponse(' +
113                         'execution=' + execution.getId() +
114                         ')'
115
116                 logger.trace('Entered ' + method)
117
118                 try {
119                         def requestInfo = execution.getVariable('UPDVfMod_requestInfo')
120                         def requestId = execution.getVariable('UPDVfMod_requestId')
121                         def source = execution.getVariable('UPDVfMod_source')
122                         def progress = getNodeTextForce(requestInfo, 'progress')
123                         if (progress.isEmpty()) {
124                                 progress = '0'
125                         }
126                         def startTime = getNodeTextForce(requestInfo, 'start-time')
127                         if (startTime.isEmpty()) {
128                                 startTime = System.currentTimeMillis()
129                         }
130                         def vnfInputs = execution.getVariable('UPDVfMod_vnfInputs')
131
132                         String synchResponse = """
133                                 <vnf-request xmlns="http://org.onap/so/infra/vnf-request/v1">
134                                         <request-info>
135                                                 <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
136                                                 <action>UPDATE_VF_MODULE</action>
137                                                 <request-status>IN_PROGRESS</request-status>
138                                                 <progress>${MsoUtils.xmlEscape(progress)}</progress>
139                                                 <start-time>${MsoUtils.xmlEscape(startTime)}</start-time>
140                                                 <source>${MsoUtils.xmlEscape(source)}</source>
141                                         </request-info>
142                                         ${vnfInputs}
143                                 </vnf-request>
144                         """
145
146                         synchResponse = utils.formatXml(synchResponse)
147                         sendWorkflowResponse(execution, 200, synchResponse)
148
149                         logger.debug("UpdateVfModule Synch Response: " + synchResponse)
150                         logger.trace('Exited ' + method)
151                 } catch (BpmnError e) {
152                         throw e;
153                 } catch (Exception e) {
154                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
155                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
156                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
157                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendSynchResponse(): ' + e.getMessage())
158                 }
159         }
160
161         /**
162          * Prepare the Request for invoking the DoUpdateVfModule subflow.
163          *
164          * NOTE: Currently, the method just logs passing through as the
165          * incoming Request to this main flow is used as the Request to
166          * the DoUpdateVfModule subflow. No preparation processing is
167          * necessary.
168          *
169          * @param execution The flow's execution instance.
170          */
171         public void prepDoUpdateVfModule(DelegateExecution execution) {
172                 def method = getClass().getSimpleName() + '.prepDoUpdateVfModule(' +
173                         'execution=' + execution.getId() +
174                         ')'
175
176                 logger.trace('Entered ' + method)
177
178                 try {
179
180                         logger.trace('Exited ' + method)
181                 } catch (BpmnError e) {
182                         throw e;
183                 } catch (Exception e) {
184                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
185                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
186                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
187                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepDoUpdateVfModule(): ' + e.getMessage())
188                 }
189         }
190
191         /**
192          * Prepare the Request for updating the DB for this Infra Request.
193          *
194          * @param execution The flow's execution instance.
195          */
196         public void prepUpdateInfraRequest(DelegateExecution execution) {
197                 def method = getClass().getSimpleName() + '.prepUpdateInfraRequest(' +
198                         'execution=' + execution.getId() +
199                         ')'
200
201                 logger.trace('Entered ' + method)
202
203                 try {
204                         def requestId = execution.getVariable('UPDVfMod_requestId')
205                         def vnfId = execution.getVariable('UPDVfMod_vnfId')
206                         def vfModuleId = execution.getVariable('UPDVfMod_vfModuleId')
207                         def tenantId = execution.getVariable('UPDVfMod_tenantId')
208                         def volumeGroupId = execution.getVariable('UPDVfMod_volumeGroupId')
209
210                         String updateInfraRequest = """
211                                 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
212                                                 xmlns:req="http://org.onap.so/requestsdb">
213                                         <soapenv:Header/>
214                                         <soapenv:Body>
215                                                 <req:updateInfraRequest>
216                                                         <requestId>${MsoUtils.xmlEscape(requestId)}</requestId>
217                                                         <lastModifiedBy>BPEL</lastModifiedBy>
218                                                         <requestStatus>COMPLETE</requestStatus>
219                                                         <progress>100</progress>
220                                                         <vnfOutputs>
221                                                                 <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
222                                                                 <vf-module-id>${MsoUtils.xmlEscape(vfModuleId)}</vf-module-id>
223                                                                 <tenant-id>${MsoUtils.xmlEscape(tenantId)}</tenant-id>
224                                                                 <volume-group-id>${MsoUtils.xmlEscape(volumeGroupId)}</volume-group-id>
225                                                         </vnfOutputs>
226                                                 </req:updateInfraRequest>
227                                         </soapenv:Body>
228                                 </soapenv:Envelope>
229                         """
230
231                         updateInfraRequest = utils.formatXml(updateInfraRequest)
232                         execution.setVariable('UPDVfMod_updateInfraRequest', updateInfraRequest)
233                         logger.debug('Request for Update Infra Request:\n' + updateInfraRequest)
234
235                         logger.debug("UpdateVfModule Infra Request: " + updateInfraRequest)
236                         logger.trace('Exited ' + method)
237                 } catch (BpmnError e) {
238                         throw e;
239                 } catch (Exception e) {
240                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
241                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
242                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
243                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepUpdateInfraRequest(): ' + e.getMessage())
244                 }
245         }
246
247         /**
248          * Builds a "CompletionHandler" request and stores it in the specified execution variable.
249          *
250          * @param execution the execution
251          * @param resultVar the execution variable in which the result will be stored
252          */
253         public void completionHandlerPrep(DelegateExecution execution, String resultVar) {
254                 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +
255                         'execution=' + execution.getId() +
256                         ', resultVar=' + resultVar +
257                         ')'
258
259                 logger.trace('Entered ' + method)
260
261                 try {
262                         def requestInfo = getVariable(execution, 'UPDVfMod_requestInfo')
263
264                         String content = """
265                                 <sdncadapterworkflow:MsoCompletionRequest xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
266                                                 xmlns:reqtype="http://org.onap/so/request/types/v1">
267                                         ${requestInfo}
268                                         <sdncadapterworkflow:mso-bpel-name>MSO_ACTIVATE_BPEL</sdncadapterworkflow:mso-bpel-name>
269                                 </sdncadapterworkflow:MsoCompletionRequest>
270                         """
271
272                         content = utils.formatXml(content)
273                         logger.debug(resultVar + ' = ' + System.lineSeparator() + content)
274                         execution.setVariable(resultVar, content)
275
276                         logger.debug("UpdateVfModule CompletionHandler Request: " + content)
277                         logger.trace('Exited ' + method)
278                 } catch (BpmnError e) {
279                         throw e;
280                 } catch (Exception e) {
281                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
282                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
283                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
284                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, 'Internal Error')
285                 }
286         }
287
288         /**
289          * Builds a "FalloutHandler" 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 falloutHandlerPrep(DelegateExecution execution, String resultVar) {
295                 def method = getClass().getSimpleName() + '.falloutHandlerPrep(' +
296                         'execution=' + execution.getId() +
297                         ', resultVar=' + resultVar +
298                         ')'
299
300                 logger.trace('Entered ' + method)
301
302                 try {
303                         def prefix = execution.getVariable('prefix')
304                         def request = getVariable(execution, prefix+'Request')
305                         def requestInformation = utils.getNodeXml(request, 'request-information', false)
306
307                         def WorkflowException workflowException = execution.getVariable("WorkflowException")
308                         def errorResponseCode = workflowException.getErrorCode()
309                         def errorResponseMsg = workflowException.getErrorMessage()
310                         def encErrorResponseMsg = ""
311                         if (errorResponseMsg != null) {
312                                 encErrorResponseMsg = errorResponseMsg
313                         }
314
315                         String content = """
316                                 <sdncadapterworkflow:FalloutHandlerRequest xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
317                                                 xmlns:reqtype="http://org.onap/so/request/types/v1"
318                                                 xmlns:msoservtypes="http://org.onap/so/request/types/v1"
319                                                 xmlns:structuredtypes="http://org.onap/so/structured/types/v1">
320                                         ${requestInformation}
321                                         <sdncadapterworkflow:WorkflowException>
322                                                 <sdncadapterworkflow:ErrorMessage>${MsoUtils.xmlEscape(encErrorResponseMsg)}</sdncadapterworkflow:ErrorMessage>
323                                                 <sdncadapterworkflow:ErrorCode>${MsoUtils.xmlEscape(errorResponseCode)}</sdncadapterworkflow:ErrorCode>
324                                         </sdncadapterworkflow:WorkflowException>
325                                 </sdncadapterworkflow:FalloutHandlerRequest>
326                         """
327                         content = utils.formatXml(content)
328                         logger.debug(resultVar + ' = ' + System.lineSeparator() + content)
329                         execution.setVariable(resultVar, content)
330
331                         logger.debug("UpdateVfModule fallOutHandler Request: " + content)
332                         logger.trace('Exited ' + method)
333                 } catch (BpmnError e) {
334                         throw e;
335                 } catch (Exception e) {
336                         logger.error("{} {} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
337                                         'Caught exception in ' + method, "BPMN", MsoLogger.getServiceName(),
338                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
339                         exceptionUtil.buildWorkflowException(execution, 2000, 'Internal Error')
340                 }
341         }
342 }