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