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