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