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