Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / UpdateVfModuleInfraV2.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package org.openecomp.mso.bpmn.infrastructure.scripts\r
22  \r
23 \r
24 import groovy.json.JsonOutput\r
25 import groovy.json.JsonSlurper\r
26 import groovy.util.Node\r
27 import groovy.util.XmlParser;\r
28 import groovy.xml.QName\r
29 \r
30 import org.camunda.bpm.engine.delegate.BpmnError\r
31 import org.camunda.bpm.engine.impl.cmd.AbstractSetVariableCmd\r
32 import org.camunda.bpm.engine.delegate.DelegateExecution\r
33 \r
34 import java.io.Serializable;\r
35 import java.util.List\r
36 \r
37 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil\r
38 import org.openecomp.mso.rest.APIResponse\r
39 import org.openecomp.mso.rest.RESTClient\r
40 import org.openecomp.mso.rest.RESTConfig\r
41 import org.springframework.beans.factory.annotation.Autowired\r
42 \r
43 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
44 import org.openecomp.mso.bpmn.common.scripts.VidUtils\r
45 import org.openecomp.mso.bpmn.core.RollbackData\r
46 import org.openecomp.mso.bpmn.core.WorkflowException\r
47 import org.openecomp.mso.client.aai.AAIValidatorImpl\r
48 import org.openecomp.mso.client.aai.AAIUpdatorImpl\r
49 import org.openecomp.mso.client.appc.ApplicationControllerClient\r
50 import org.openecomp.mso.client.sdno.SDNOValidatorImpl\r
51 \r
52 \r
53 \r
54 public class UpdateVfModuleInfraV2 {\r
55 \r
56         ExceptionUtil exceptionUtil = new ExceptionUtil()\r
57 \r
58         boolean preProcessRequestCheck = true;\r
59         boolean sendSynchResponseCheck = true;\r
60         boolean checkPserverFlagCheck = true;\r
61         boolean vfFlagCheckSetCheck = true;\r
62         boolean lockAppCCheck = true;\r
63         boolean healthDiagnosticSDNOCheck = true;\r
64         boolean healthCheckAppCCheck = true;\r
65         boolean stopVfModuleControllerCheck = true;\r
66         boolean healthCheckControllerCheck = true;\r
67         boolean doUpdateVfModulePrepCheck = true;\r
68         boolean completionHandlerPrepCheck = true;\r
69         boolean startVfModuleControllerCheck = true;\r
70         boolean vFFlagUnsetCheck = true;\r
71         boolean unlockAppCCheck = true;\r
72         boolean postUpgradeHealthCheckControllerCheck = true;\r
73 \r
74 \r
75 \r
76         public void initProcessVariables(DelegateExecution execution) {\r
77                 execution.setVariable('prefix', 'UPDVfModI_')\r
78                 execution.setVariable('UPDVfModI_Request', null)\r
79                 execution.setVariable('UPDVfModI_requestInfo', null)\r
80                 execution.setVariable('UPDVfModI_requestId', null)\r
81                 execution.setVariable('UPDVfModI_source', null)\r
82                 execution.setVariable('UPDVfModI_vnfInputs', null)\r
83                 execution.setVariable('UPDVfModI_vnfId', null)\r
84                 execution.setVariable('UPDVFModI_moduleUuid', null)\r
85                 execution.setVariable('UPDVfModI_vfModuleId', null)\r
86                 execution.setVariable('UPDVfModI_tenantId', null)\r
87                 execution.setVariable('UPDVfModI_volumeGroupId', null)\r
88                 execution.setVariable('UPDVfModI_vnfParams', null)\r
89                 execution.setVariable('UPDVfModI_updateInfraRequest', null)\r
90                 execution.setVariable('UpdateVfModuleSuccessIndicator', false)\r
91         }\r
92 \r
93         /**\r
94          * Check for missing elements in the received request.\r
95          *\r
96          * @param execution The flow's execution instance.\r
97          */\r
98         public void preProcessRequest(DelegateExecution execution) {\r
99                 System.out.print("*****************************PreProcessRequest**************************")\r
100 \r
101                 def method = getClass().getSimpleName() + '.preProcessRequest(' +\r
102                                 'execution=' + execution.getId() +\r
103                                 ')'\r
104                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
105                 //logDebug('Entered ' + method, isDebugLogEnabled)\r
106 \r
107                 initProcessVariables(execution)\r
108 \r
109                 def prefix = "UPDVfModI_"\r
110 \r
111                 def incomingRequest = execution.getVariable('bpmnRequest')\r
112 \r
113                 //utils.log("DEBUG", "Incoming Infra Request: " + incomingRequest, isDebugLogEnabled)\r
114                 try {\r
115                         def jsonSlurper = new JsonSlurper()\r
116                         def jsonOutput = new JsonOutput()\r
117                         Map reqMap = jsonSlurper.parseText(incomingRequest)\r
118                         //utils.log("DEBUG", " Request is in JSON format.", isDebugLogEnabled)\r
119 \r
120                         def serviceInstanceId = execution.getVariable('serviceInstanceId')\r
121                         def vnfId = execution.getVariable('vnfId')\r
122                         def moduleUuid = execution.getVariable('moduleUuid')\r
123                         execution.setVariable(prefix + 'moduleUuid',moduleUuid)\r
124                         execution.setVariable(prefix + 'serviceInstanceId', serviceInstanceId)\r
125                         execution.setVariable(prefix+'vnfId', vnfId)\r
126                         execution.setVariable("isVidRequest", "true")\r
127 \r
128                         def vnfName = ''\r
129                         def asdcServiceModelVersion = ''\r
130                         def serviceModelInfo = null\r
131                         def vnfModelInfo = null\r
132 \r
133                         def relatedInstanceList = reqMap.requestDetails?.relatedInstanceList\r
134 \r
135                         if (relatedInstanceList != null) {\r
136                                 relatedInstanceList.each {\r
137                                         if (it.relatedInstance.modelInfo?.modelType == 'service') {\r
138                                                 asdcServiceModelVersion = it.relatedInstance.modelInfo?.modelVersion\r
139                                                 serviceModelInfo = jsonOutput.toJson(it.relatedInstance.modelInfo)\r
140                                         }\r
141                                         if (it.relatedInstance.modelInfo.modelType == 'vnf') {\r
142                                                 vnfName = it.relatedInstance.instanceName ?: ''\r
143                                                 vnfModelInfo = jsonOutput.toJson(it.relatedInstance.modelInfo)\r
144                                         }\r
145                                 }\r
146                         }\r
147 \r
148                         execution.setVariable(prefix + 'vnfName', vnfName)\r
149                         execution.setVariable(prefix + 'asdcServiceModelVersion', asdcServiceModelVersion)\r
150                         execution.setVariable(prefix + 'serviceModelInfo', serviceModelInfo)\r
151                         execution.setVariable(prefix + 'vnfModelInfo', vnfModelInfo)\r
152 \r
153                         def vnfType = execution.getVariable('vnfType')\r
154                         execution.setVariable(prefix + 'vnfType', vnfType)\r
155                         def vfModuleId = execution.getVariable('vfModuleId')\r
156                         execution.setVariable(prefix + 'vfModuleId', vfModuleId)\r
157                         def volumeGroupId = execution.getVariable('volumeGroupId')\r
158                         execution.setVariable(prefix + 'volumeGroupId', volumeGroupId)\r
159                         def userParams = reqMap.requestDetails?.requestParameters?.userParams\r
160 \r
161                         Map<String, String> userParamsMap = [:]\r
162                         if (userParams != null) {\r
163                                 userParams.each { userParam ->\r
164                                         userParamsMap.put(userParam.name, userParam.value.toString())\r
165                                 }\r
166                         }\r
167 \r
168                         //utils.log("DEBUG", 'Processed user params: ' + userParamsMap, isDebugLogEnabled)\r
169 \r
170                         execution.setVariable(prefix + 'vfModuleInputParams', userParamsMap)\r
171 \r
172                         def isBaseVfModule = "false"\r
173                         if (execution.getVariable('isBaseVfModule') == true) {\r
174                                 isBaseVfModule = "true"\r
175                         }\r
176 \r
177                         execution.setVariable(prefix + 'isBaseVfModule', isBaseVfModule)\r
178 \r
179                         def requestId = execution.getVariable("mso-request-id")\r
180                         execution.setVariable(prefix + 'requestId', requestId)\r
181 \r
182                         def vfModuleModelInfo = jsonOutput.toJson(reqMap.requestDetails?.modelInfo)\r
183                         execution.setVariable(prefix + 'vfModuleModelInfo', vfModuleModelInfo)\r
184 \r
185                         def suppressRollback = reqMap.requestDetails?.requestInfo?.suppressRollback\r
186 \r
187 \r
188                         def backoutOnFailure = ""\r
189                         if(suppressRollback != null){\r
190                                 if ( suppressRollback == true) {\r
191                                         backoutOnFailure = "false"\r
192                                 } else if ( suppressRollback == false) {\r
193                                         backoutOnFailure = "true"\r
194                                 }\r
195                         }\r
196 \r
197                         execution.setVariable('disableRollback', suppressRollback)\r
198 \r
199                         def vfModuleName = reqMap.requestDetails?.requestInfo?.instanceName ?: null\r
200                         execution.setVariable(prefix + 'vfModuleName', vfModuleName)\r
201 \r
202                         def serviceId = reqMap.requestDetails?.requestParameters?.serviceId ?: ''\r
203                         execution.setVariable(prefix + 'serviceId', serviceId)\r
204 \r
205                         def usePreload = reqMap.requestDetails?.requestParameters?.usePreload\r
206                         execution.setVariable(prefix + 'usePreload', usePreload)\r
207 \r
208                         def cloudConfiguration = reqMap.requestDetails?.cloudConfiguration\r
209                         def lcpCloudRegionId    = cloudConfiguration.lcpCloudRegionId\r
210                         execution.setVariable(prefix + 'lcpCloudRegionId', lcpCloudRegionId)\r
211                         def tenantId = cloudConfiguration.tenantId\r
212                         execution.setVariable(prefix + 'tenantId', tenantId)\r
213 \r
214                         def globalSubscriberId = reqMap.requestDetails?.subscriberInfo?.globalSubscriberId ?: ''\r
215                         execution.setVariable(prefix + 'globalSubscriberId', globalSubscriberId)\r
216 \r
217                         execution.setVariable(prefix + 'sdncVersion', '1702')\r
218 \r
219                         execution.setVariable("UpdateVfModuleInfraSuccessIndicator", false)\r
220 \r
221                         execution.setVariable("isDebugLogEnabled", isDebugLogEnabled)\r
222 \r
223 \r
224                         def source = reqMap.requestDetails?.requestInfo?.source\r
225                         execution.setVariable(prefix + "source", source)\r
226 \r
227                         //For Completion Handler & Fallout Handler\r
228                         String requestInfo =\r
229                                         """<request-info xmlns="http://org.openecomp/mso/infra/vnf-request/v1">\r
230                                         <request-id>${requestId}</request-id>\r
231                                         <action>UPDATE</action>\r
232                                         <source>${source}</source>\r
233                                    </request-info>"""\r
234 \r
235                         execution.setVariable(prefix + "requestInfo", requestInfo)\r
236 \r
237                         //backoutOnFailure\r
238 \r
239                         //logDebug('RequestInfo: ' + execution.getVariable(prefix + "requestInfo"), isDebugLogEnabled)\r
240 \r
241                         //logDebug('Exited ' + method, isDebugLogEnabled)\r
242 \r
243                 }\r
244                 catch(groovy.json.JsonException je) {\r
245                         //utils.log("DEBUG", " Request is not in JSON format.", isDebugLogEnabled)\r
246                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, "Invalid request format")\r
247                 }\r
248                 catch(Exception e) {\r
249                         String restFaultMessage = e.getMessage()\r
250                         //utils.log("ERROR", " Exception Encountered - " + "\n" + restFaultMessage, isDebugLogEnabled)\r
251                         exceptionUtil.buildAndThrowWorkflowException(execution, 5000, restFaultMessage)\r
252                 }\r
253         }\r
254 \r
255         /**\r
256          * Prepare and send the synchronous response for this flow.\r
257          *\r
258          * @param execution The flow's execution instance.\r
259          */\r
260         public void sendSynchResponse(DelegateExecution execution) {\r
261                 System.out.print("*****************************SendSynchResponse**************************")\r
262 \r
263                 def method = getClass().getSimpleName() + '.sendSynchResponse(' +\r
264                                 'execution=' + execution.getId() +\r
265                                 ')'\r
266                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
267                 //logDebug('Entered ' + method, isDebugLogEnabled)\r
268 \r
269 \r
270                 try {\r
271                         def requestInfo = execution.getVariable('UPDVfModI_requestInfo')\r
272                         def requestId = execution.getVariable('UPDVfModI_requestId')\r
273                         def source = execution.getVariable('UPDVfModI_source')\r
274                         \r
275                         def progress = getNodeTextForce(requestInfo, 'progress')\r
276                         if (progress.isEmpty()) {\r
277                                 progress = '0'\r
278                         }\r
279                         def startTime = getNodeTextForce(requestInfo, 'start-time')\r
280                         if (startTime.isEmpty()) {\r
281                                 startTime = System.currentTimeMillis()\r
282                         }\r
283                         // RESTResponse (for API Handler (APIH) Reply Task)\r
284                         def vfModuleId = execution.getVariable("vfModuleId")\r
285                         String synchResponse = """{"requestReferences":{"instanceId":"${vfModuleId}","requestId":"${requestId}"}}""".trim()\r
286                         sendWorkflowResponse(execution, 200, synchResponse)\r
287                         //logDebug('Exited ' + method, isDebugLogEnabled)\r
288                 } catch (BpmnError e) {\r
289                         throw e;\r
290                 } catch (Exception e) {\r
291                         //logError('Caught exception in ' + method, e)\r
292                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in sendResponse(): ' + e.getMessage())\r
293                 }\r
294         }\r
295 \r
296         //check to see if the Pserver Flag is locked\r
297         public void checkPserverFlag(DelegateExecution execution) {\r
298 \r
299                 System.out.println("*****************************CheckingPserverFlag*************************")\r
300                 String vnfId = (String)execution.getVariable('vnfId')\r
301                 String uuid = (String)execution.getVariable('moduleUuid')\r
302                 AAIValidatorImpl aaiVI = new AAIValidatorImpl()\r
303                 boolean flag = aaiVI.isPhysicalServerLocked(vnfId, uuid)\r
304         }\r
305 \r
306         //check to see if the VFFlag is locked\r
307         public void vfFlagCheck(DelegateExecution execution) {\r
308 \r
309                 System.out.print("*****************************VfFlagCheck*************************")\r
310                 String vnfId = (String)execution.getVariable('vnfId')\r
311                 String uuid = (String)execution.getVariable('moduleUuid')\r
312                 AAIValidatorImpl aaiVI = new AAIValidatorImpl()\r
313                 boolean flag = aaiVI.isVNFLocked(vnfId, uuid)\r
314 \r
315         }\r
316         //lock the VF Flag\r
317         public void vfFlagSet(DelegateExecution execution) {\r
318 \r
319                 System.out.print("*****************************VfFlagSet*************************")\r
320                 String vnfId = (String)execution.getVariable('vnfId')\r
321                 String uuid = (String)execution.getVariable('moduleUuid')\r
322                 AAIValidatorImpl aaiVI = new AAIValidatorImpl()\r
323                 aaiVI.updateVnfToLocked(vnfId,uuid);\r
324                 \r
325         }\r
326 \r
327         //Lock AppC\r
328         public void lockAppC(DelegateExecution execution) {\r
329 \r
330                 System.out.print("*****************************lockAppC*************************")\r
331                 def vfModuleId = ""\r
332                 ApplicationControllerClient aCC = new ApplicationControllerClient();\r
333                 def status = aCC.runCommand("Lock",vfModuleId)\r
334 \r
335 \r
336         }\r
337         //run health check\r
338         public void healthCheckAppC(DelegateExecution execution) {\r
339 \r
340                 System.out.print("*****************************healthCheckAppC*************************")\r
341                 def vfModuleId = ""\r
342                 ApplicationControllerClient aCC = new ApplicationControllerClient();\r
343                 def status = aCC.runCommand("HealthCheck",vfModuleId)\r
344 \r
345         }\r
346         //SDNO health diagnostic\r
347         public void healthDiagnosticSDNO(DelegateExecution execution) {\r
348 \r
349                 System.out.print("*****************************healthDiagnosticSDNO is currently ignored*************************")\r
350                 //SDNOValidatorImpl.healthDiagnostic("","");\r
351 \r
352         }\r
353         //stop VF module controller\r
354         public void stopVfModuleController(DelegateExecution execution) {\r
355 \r
356                 System.out.print("*****************************stopVfModuleController*************************")\r
357                 def vfModuleId = ""\r
358                 ApplicationControllerClient aCC = new ApplicationControllerClient();\r
359                 def status = aCC.runCommand("Stop",vfModuleId)\r
360 \r
361 \r
362         }\r
363 \r
364         public void doUpdateVfModulePrep(DelegateExecution execution) {\r
365 \r
366                 System.out.print("*****************************doUpdateVfModulePrep*************************")\r
367                 def method = getClass().getSimpleName() + '.prepDoUpdateVfModule(' +\r
368                                 'execution=' + execution.getId() +\r
369                                 ')'\r
370                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
371                 //logDebug('Entered ' + method, isDebugLogEnabled)\r
372 \r
373                 try {\r
374 \r
375                         //logDebug('Exited ' + method, isDebugLogEnabled)\r
376                 } catch (BpmnError e) {\r
377                         throw e;\r
378                 } catch (Exception e) {\r
379                         //logError('Caught exception in ' + method, e)\r
380                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, 'Error in prepDoUpdateVfModule(): ' + e.getMessage())\r
381 \r
382                 }\r
383 \r
384         }\r
385 \r
386         public void completionHandlerPrep(DelegateExecution execution,String resultVar) {\r
387 \r
388                 System.out.print("*****************************completionHandlerPrep*************************")\r
389                 def method = getClass().getSimpleName() + '.completionHandlerPrep(' +\r
390                                 'execution=' + execution.getId() +\r
391                                 ', resultVar=' + resultVar +\r
392                                 ')'\r
393                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')\r
394                 //logDebug('Entered ' + method, isDebugLogEnabled)\r
395 \r
396                 try {\r
397                         def requestInfo = getVariable(execution, 'UPDVfModI_requestInfo')\r
398 \r
399                         String content = """\r
400                                         <sdncadapterworkflow:MsoCompletionRequest xmlns:sdncadapterworkflow="http://org.openecomp/mso/workflow/schema/v1"\r
401                                                         xmlns:reqtype="http://org.openecomp/mso/request/types/v1">\r
402                                                 ${requestInfo}\r
403                                                 <sdncadapterworkflow:mso-bpel-name>MSO_ACTIVATE_BPEL</sdncadapterworkflow:mso-bpel-name>\r
404                                         </sdncadapterworkflow:MsoCompletionRequest>\r
405                                 """\r
406 \r
407                         content = utils.formatXml(content)\r
408                         //logDebug(resultVar + ' = ' + System.lineSeparator() + content, isDebugLogEnabled)\r
409                         execution.setVariable(resultVar, content)\r
410 \r
411                         //logDebug('Exited ' + method, isDebugLogEnabled)\r
412                 } catch (BpmnError e) {\r
413                         throw e;\r
414                 } catch (Exception e) {\r
415                         //logError('Caught exception in ' + method, e)\r
416                         exceptionUtil.buildAndThrowWorkflowException(execution, 2000, 'Internal Error')\r
417 \r
418                 }\r
419 \r
420         }\r
421 \r
422         public void healthCheckController(DelegateExecution execution) {\r
423 \r
424                 System.out.print("*****************************healthCheckController*************************")\r
425                 def vfModuleId = ""\r
426                 ApplicationControllerClient aCC = new ApplicationControllerClient();\r
427                 def status = aCC.runCommand("HealthCheck",vfModuleId)\r
428 \r
429         }\r
430 \r
431         public void startVfModuleController(DelegateExecution execution) {\r
432 \r
433                 System.out.print("*****************************startVfModuleController*************************")\r
434                 def vfModuleId = ""\r
435                 ApplicationControllerClient aCC = new ApplicationControllerClient();\r
436                 def status = aCC.runCommand("Start",vfModuleId)\r
437 \r
438         }\r
439 \r
440         public void vFFlagUnset(DelegateExecution execution) {\r
441 \r
442                 System.out.print("*****************************vFFlagUnset*************************")\r
443                 String vnfId = (String)execution.getVariable('vnfId')\r
444                 String uuid = (String)execution.getVariable('moduleUuid')\r
445                 AAIValidatorImpl aaiVI = new AAIValidatorImpl()\r
446                 aaiVI.updateVnfToUnLocked(vnfId,uuid);\r
447 \r
448 \r
449         }\r
450 \r
451         public void unlockAppC(DelegateExecution execution) {\r
452 \r
453                 System.out.print("*****************************unlockAppC*************************")\r
454                 def vfModuleId = ""\r
455                 ApplicationControllerClient aCC = new ApplicationControllerClient();\r
456                 def status = aCC.runCommand("Unlock",vfModuleId)\r
457 \r
458         }\r
459 \r
460         public void postUpgradeHealthCheckController(DelegateExecution execution) {\r
461 \r
462                 System.out.print("*****************************postUpgradeHealthCheckController*************************")\r
463                 def vfModuleId = ""\r
464                 ApplicationControllerClient aCC = new ApplicationControllerClient();\r
465                 def status = aCC.runCommand("HealthCheck",vfModuleId)\r
466 \r
467         }\r
468 \r
469 }\r
470 \r