Cleaned up content of MsoLogger
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateVnfAndModulesRollback.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 static org.apache.commons.lang3.StringUtils.*;
26
27 import org.camunda.bpm.engine.delegate.BpmnError
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.bpmn.common.scripts.MsoUtils
32 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
33 import org.onap.so.bpmn.core.RollbackData
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.onap.so.bpmn.core.json.JsonUtils
36 import org.onap.so.logger.MessageEnum
37 import org.onap.so.logger.MsoLogger
38 import org.slf4j.Logger
39 import org.slf4j.LoggerFactory
40
41 /**
42  * This class supports the macro VID Flow
43  * with the rollback of a creation of a generic vnf and related VF modules.
44  */
45 class DoCreateVnfAndModulesRollback extends AbstractServiceTaskProcessor {
46
47     private static final Logger logger = LoggerFactory.getLogger( DoCreateVnfAndModulesRollback.class);
48         String Prefix="DCVAMR_"
49         ExceptionUtil exceptionUtil = new ExceptionUtil()
50         JsonUtils jsonUtil = new JsonUtils()
51         
52
53         /**
54          * This method gets and validates the incoming
55          * request.
56          *
57          * @param - execution
58          *
59          */
60         public void preProcessRequest(DelegateExecution execution) {
61                 
62                 execution.setVariable("prefix",Prefix)
63                 logger.debug("STARTED DoCreateVnfAndModulesRollback PreProcessRequest Process")
64
65                 try{
66                         // Get Rollback Variables
67                         
68                         def rollbackData = execution.getVariable("rollbackData")
69                         logger.debug("Incoming RollbackData is: " + rollbackData.toString())
70                         execution.setVariable("rolledBack", null)
71                         execution.setVariable("rollbackError", null)
72                         
73                         if (execution.getVariable("disableRollback").equals("true" ))
74                         {
75                                 execution.setVariable("skipRollback", true)
76                         }
77
78                         String vnfId = rollbackData.get("VNF", "vnfId")
79                         logger.debug("Rollback vnfId is: " + vnfId)
80                         execution.setVariable("DCVAMR_vnfId", vnfId)
81                         
82                         execution.setVariable("mso-request-id", execution.getVariable("msoRequestId"))
83                         
84                         execution.setVariable("DCVAMR_rollbackSDNCAssign", rollbackData.get("VNF", "rollbackSDNCAssign"))
85                         execution.setVariable("DCVAMR_rollbackSDNCActivate", rollbackData.get("VNF", "rollbackSDNCActivate"))
86                         execution.setVariable("DCVAMR_rollbackVnfCreate", rollbackData.get("VNF", "rollbackVnfCreate"))
87                         
88                         String sdncCallbackUrl = rollbackData.get("VNF", "sdncCallbackUrl")
89                         logger.debug("Rollback sdncCallbackUrl is: " + sdncCallbackUrl)
90                         execution.setVariable("DCVAMR_sdncCallbackUrl", sdncCallbackUrl)
91                         
92                         String tenantId= rollbackData.get("VNF", "tenantId")
93                         logger.debug("Rollback tenantId is: " + tenantId)
94                         execution.setVariable("DCVAMR_tenantId", tenantId)
95                         
96                         String source= rollbackData.get("VNF", "source")
97                         logger.debug("Rollback source is: " + source)
98                         execution.setVariable("DCVAMR_source", source)
99                         
100                         String serviceInstanceId = rollbackData.get("VNF", "serviceInstanceId")
101                         logger.debug("Rollback serviceInstanceId is: " + serviceInstanceId)
102                         execution.setVariable("DCVAMR_serviceInstanceId", serviceInstanceId)
103                         
104                         String cloudSiteId = rollbackData.get("VNF", "cloudSiteId")
105                         logger.debug("Rollback cloudSiteId is: " + cloudSiteId)
106                         execution.setVariable("DCVAMR_cloudSiteId", cloudSiteId)
107                         
108                         def numOfAddOnModulesString = rollbackData.get("VNFANDMODULES", "numOfCreatedAddOnModules")
109                         int numOfAddOnModules = 0
110                         if (numOfAddOnModulesString != null) {
111                                 numOfAddOnModules = Integer.parseInt(numOfAddOnModulesString)                           
112                         }
113                         execution.setVariable("DCVAMR_numOfAddOnModules", numOfAddOnModules)
114                         
115                         def baseVfModuleRollbackMap = rollbackData.get("VFMODULE_BASE")
116                         if (baseVfModuleRollbackMap == null) {
117                                 // there are no VF Modules to delete
118                                 execution.setVariable("DCVAMR_numOfModulesToDelete", 0)
119                         }
120                         else {
121                                 execution.setVariable("DCVAMR_numOfModulesToDelete", numOfAddOnModules + 1)                             
122                         }
123                         
124                         // Set aLaCarte to false
125                         execution.setVariable("DCVAMR_aLaCarte", false)                 
126                         
127                 }catch(BpmnError b){
128                         logger.debug("Rethrowing MSOWorkflowException")
129                         throw b
130                 }catch(Exception e){
131                         logger.debug(" Error Occured in DoCreateVnfAndModulesRollback PreProcessRequest method!" + e.getMessage())
132                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, "Internal Error - Occured in DoCreateVnfAndModulesRollback PreProcessRequest")
133
134                 }
135                 logger.trace("COMPLETED DoCreateVnfAndModulesRollback PreProcessRequest Process")
136         }
137
138         
139         
140         public void preProcessCreateVfModuleRollback(DelegateExecution execution){
141                 
142                 execution.setVariable("prefix", Prefix)
143                 logger.trace("STARTED preProcessCreateVfModuleRollback")
144                 
145                 try {
146                         
147                         def rollbackData = execution.getVariable("rollbackData")
148                         
149                         def vfModuleRollbackData = new RollbackData()
150                         
151                         def numOfModulesToDelete = execution.getVariable("DCVAMR_numOfModulesToDelete")
152                         logger.debug("numOfModulesToDelete: " + numOfModulesToDelete)
153                         def moduleMap = null
154                         
155                         if (numOfModulesToDelete > 1) {
156                                 int addOnModuleIndex = numOfModulesToDelete - 1
157                                 moduleMap = rollbackData.get("VFMODULE_ADDON_" + addOnModuleIndex)
158                                 logger.debug("Removing ADDON VF module # " + addOnModuleIndex)
159                         }
160                         else {
161                                 moduleMap = rollbackData.get("VFMODULE_BASE")
162                                 logger.debug("Removing BASE VF module")
163                         }
164                         moduleMap.each{ k, v -> vfModuleRollbackData.put("VFMODULE", "${k}","${v}") }
165                         execution.setVariable("DCVAMR_RollbackData", vfModuleRollbackData)                                                              
166                         
167                 }catch(Exception e){
168                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
169                                         "Exception Occured Processing preProcessCreateVfModuleRollback ", "BPMN",
170                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
171                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessCreateVfModuleRollback Method:\n" + e.getMessage())
172                 }
173                 logger.trace("COMPLETED preProcessCreateVfModuleRollback")
174         }
175         
176         
177         public void postProcessCreateVfModuleRollback(DelegateExecution execution){
178                 
179                 execution.setVariable("prefix", Prefix)
180                 logger.trace("STARTED postProcessCreateVfModuleRollback")
181                 def rolledBack = false
182                 
183                 try {
184                         rolledBack = execution.getVariable("DCVM_rolledBack")
185                         def numOfModulesToDelete = execution.getVariable("DCVAMR_numOfModulesToDelete")
186                         execution.setVariable("DCVAMR_numOfModulesToDelete", numOfModulesToDelete - 1)          
187                 }catch(Exception e){
188                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
189                                         "Exception Occured Processing postProcessCreateVfModuleRollback ", "BPMN",
190                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
191                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during postProcessCreateVfModuleRollback Method:\n" + e.getMessage())
192                 }
193                 if (rolledBack == false) {
194                         logger.debug("Failure on DoCreateVfModuleRollback")
195                         logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
196                                         "Unsuccessful rollback of DoCreateVfModule ", "BPMN",
197                                         MsoLogger.ErrorCode.UnknownError.getValue());
198                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during rollback of DoCreateVfModule")
199                 }
200                 logger.trace("COMPLETED postProcessCreateVfModuleRollback")
201         }
202         
203         
204         public void preProcessSDNCDeactivateRequest(DelegateExecution execution){
205                 
206                 execution.setVariable("prefix", Prefix)
207                 logger.trace("STARTED preProcessSDNCDeactivateRequest")
208                 def vnfId = execution.getVariable("vnfId")
209                 def serviceInstanceId = execution.getVariable("serviceInstanceId")
210
211                 try{
212                         //Build SDNC Request
213                         
214                         String deactivateSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "deactivate")
215
216                         deactivateSDNCRequest = utils.formatXml(deactivateSDNCRequest)
217                         execution.setVariable(Prefix + "deactivateSDNCRequest", deactivateSDNCRequest)
218                         logger.debug("Outgoing DeactivateSDNCRequest is: \n" + deactivateSDNCRequest)
219
220                 }catch(Exception e){
221                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(),
222                                         "Exception Occured Processing preProcessSDNCDeactivateRequest ", "BPMN",
223                                         MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
224                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during preProcessSDNCDeactivateRequest Method:\n" + e.getMessage())
225                 }
226                 logger.trace("COMPLETED preProcessSDNCDeactivateRequest")
227         }
228         
229         public void preProcessSDNCUnassignRequest(DelegateExecution execution) {
230                 def method = getClass().getSimpleName() + '.preProcessSDNCUnassignRequest(' +
231                         'execution=' + execution.getId() +
232                         ')'
233                 def isDebugLogEnabled = execution.getVariable('isDebugLogEnabled')
234                 logger.trace('Entered ' + method)
235                 execution.setVariable("prefix", Prefix)
236                 logger.trace("STARTED preProcessSDNCUnassignRequest Process")
237                 try{
238                         String vnfId = execution.getVariable("vnfId")
239                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
240
241                         String unassignSDNCRequest = buildSDNCRequest(execution, serviceInstanceId, "unassign")
242
243                         execution.setVariable(Prefix + "unassignSDNCRequest", unassignSDNCRequest)
244                         logger.debug("Outgoing UnassignSDNCRequest is: \n" + unassignSDNCRequest)
245
246                 }catch(Exception e){
247                         logger.debug("Exception Occured Processing preProcessSDNCUnassignRequest. Exception is:\n" + e)
248                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during  preProcessSDNCUnassignRequest Method:\n" + e.getMessage())
249                 }
250                 logger.trace("COMPLETED  preProcessSDNCUnassignRequest Process")
251         }
252         
253         public String buildSDNCRequest(DelegateExecution execution, String svcInstId, String action){
254                 
255                                 String uuid = execution.getVariable('testReqId') // for junits
256                                 if(uuid==null){
257                                         uuid = execution.getVariable("msoRequestId") + "-" +    System.currentTimeMillis()
258                                 }
259                                 def callbackURL = execution.getVariable(Prefix + "sdncCallbackUrl")
260                                 def requestId = execution.getVariable("msoRequestId")                           
261                                 def tenantId = execution.getVariable(Prefix + "tenantId")
262                                 def source = execution.getVariable(Prefix + "source")
263                                 def vnfId = execution.getVariable(Prefix + "vnfId")
264                                 def serviceInstanceId = execution.getVariable(Prefix + "serviceInstanceId")
265                                 def cloudSiteId = execution.getVariable(Prefix + "cloudSiteId")
266                                 
267                                 String sdncRequest =
268                                 """<sdncadapterworkflow:SDNCAdapterWorkflowRequest xmlns:ns5="http://org.onap/so/request/types/v1"
269                                                                                                         xmlns:sdncadapterworkflow="http://org.onap/so/workflow/schema/v1"
270                                                                                                         xmlns:sdncadapter="http://org.onap.so/workflow/sdnc/adapter/schema/v1">
271            <sdncadapter:RequestHeader>
272                                 <sdncadapter:RequestId>${MsoUtils.xmlEscape(uuid)}</sdncadapter:RequestId>
273                                 <sdncadapter:SvcInstanceId>${MsoUtils.xmlEscape(svcInstId)}</sdncadapter:SvcInstanceId>
274                                 <sdncadapter:SvcAction>${MsoUtils.xmlEscape(action)}</sdncadapter:SvcAction>
275                                 <sdncadapter:SvcOperation>vnf-topology-operation</sdncadapter:SvcOperation>
276                                 <sdncadapter:CallbackUrl>${MsoUtils.xmlEscape(callbackURL)}</sdncadapter:CallbackUrl>
277                                 <sdncadapter:MsoAction>generic-resource</sdncadapter:MsoAction>
278                 </sdncadapter:RequestHeader>
279         <sdncadapterworkflow:SDNCRequestData>
280                 <request-information>
281                         <request-id>${MsoUtils.xmlEscape(requestId)}</request-id>
282                         <request-action>DeleteVnfInstance</request-action>
283                         <source>${MsoUtils.xmlEscape(source)}</source>
284                         <notification-url/>
285                         <order-number/>
286                         <order-version/>
287                 </request-information>
288                 <service-information>
289                         <service-id/>
290                         <subscription-service-type/>                    
291                         <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
292                         <global-customer-id/>
293                 </service-information>
294                 <vnf-information>
295                         <vnf-id>${MsoUtils.xmlEscape(vnfId)}</vnf-id>
296                         <vnf-type/>                     
297                 </vnf-information>
298                 <vnf-request-input>                     
299                         <vnf-name/>
300                         <tenant>${MsoUtils.xmlEscape(tenantId)}</tenant>
301                         <aic-cloud-region>${MsoUtils.xmlEscape(cloudSiteId)}</aic-cloud-region>                 
302                 </vnf-request-input>
303         </sdncadapterworkflow:SDNCRequestData>
304         </sdncadapterworkflow:SDNCAdapterWorkflowRequest>"""
305                 
306                         logger.debug("sdncRequest:  " + sdncRequest)
307                         return sdncRequest
308         }
309                 
310         public void validateSDNCResponse(DelegateExecution execution, String response, String method){
311                 
312                 execution.setVariable("prefix",Prefix)
313                 logger.debug("STARTED ValidateSDNCResponse Process")
314
315                 WorkflowException workflowException = execution.getVariable("WorkflowException")
316                 boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
317
318                 logger.debug("workflowException: " + workflowException)
319
320                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
321                 sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
322
323                 String sdncResponse = response
324                 if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
325                         logger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + sdncResponse)
326                 }else{
327                         logger.debug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.")
328                         throw new BpmnError("MSOWorkflowException")
329                 }
330                 logger.debug("COMPLETED ValidateSDNCResponse Process")
331         }
332         
333         public void setSuccessfulRollbackStatus (DelegateExecution execution){
334                 
335                 execution.setVariable("prefix", Prefix)
336                 logger.trace("STARTED setSuccessfulRollbackStatus")
337         
338                 try{
339                         // Set rolledBack to true, rollbackError to null
340                         execution.setVariable("rolledBack", true)
341                         execution.setVariable("rollbackError", null)
342         
343                 }catch(Exception e){
344                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), "Exception Occured " +
345                                         "Processing setSuccessfulRollbackStatus ", "BPMN", MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
346                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during setSuccessfulRollbackStatus Method:\n" + e.getMessage())
347                 }
348                 logger.trace("COMPLETED setSuccessfulRollbackStatus")
349         }
350         
351         public void setFailedRollbackStatus (DelegateExecution execution){
352                 
353                 execution.setVariable("prefix", Prefix)
354                 logger.trace("STARTED setFailedRollbackStatus")
355         
356                 try{
357                         // Set rolledBack to false, rollbackError to actual value, rollbackData to null
358                         execution.setVariable("rolledBack", false)
359                         def rollbackError = execution.getVariable("rollbackError")
360                         if (rollbackError == null) {
361                                 execution.setVariable("rollbackError", 'Caught exception in DoCreateVnfAndModulesRollback')
362                         }
363                         execution.setVariable("rollbackData", null)
364         
365                 }catch(Exception e){
366                         logger.error("{} {} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION_ARG.toString(), "Exception Occured " +
367                                         "Processing setFailedRollbackStatus. ", "BPMN",MsoLogger.ErrorCode.UnknownError.getValue(), "Exception is:\n" + e);
368                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occurred during setFailedRollbackStatus Method:\n" + e.getMessage())
369                 }
370                 logger.trace("COMPLETED setFailedRollbackStatus")
371         }
372         
373         
374 }