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