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