Merge "Reorder modifiers"
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoUpdateE2EServiceInstanceRollback.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.mso.bpmn.infrastructure.scripts
21
22
23 import static org.apache.commons.lang3.StringUtils.*;
24 import groovy.xml.XmlUtil
25 import groovy.json.*
26
27 import org.openecomp.mso.bpmn.core.json.JsonUtils
28 import org.openecomp.mso.bpmn.common.scripts.AaiUtil
29 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
31 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
32 import org.openecomp.mso.bpmn.core.RollbackData
33 import org.openecomp.mso.bpmn.core.WorkflowException
34 import org.openecomp.mso.rest.APIResponse;
35 import org.openecomp.mso.rest.RESTClient
36 import org.openecomp.mso.rest.RESTConfig
37
38 import java.util.UUID;
39
40 import org.camunda.bpm.engine.delegate.BpmnError
41 import org.camunda.bpm.engine.delegate.DelegateExecution
42 import org.json.JSONObject;
43 import org.apache.commons.lang3.*
44 import org.apache.commons.codec.binary.Base64;
45 import org.springframework.web.util.UriUtils;
46 /**
47  * This groovy class supports the <class>DoUpdateE2EServiceInstanceRollback.bpmn</class> process.
48  *
49  * Inputs:
50  * @param - msoRequestId
51  * @param - rollbackData with
52  *          globalCustomerId
53  *                      subscriptionServiceType
54  *                      serviceInstanceId
55  *                      disableRollback
56  *                      rollbackAAI
57  *                      rollbackAdded
58  *                      rollbackDeleted
59  *
60  *
61  * Outputs:
62  * @param - rollbackError
63  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
64  *
65  */
66 public class DoUpdateE2EServiceInstanceRollback extends AbstractServiceTaskProcessor{
67
68         String Prefix="DUPDSIRB_"
69         ExceptionUtil exceptionUtil = new ExceptionUtil()
70
71         public void preProcessRequest(DelegateExecution execution) {
72                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
73                 execution.setVariable("prefix",Prefix)
74                 String msg = ""
75                 utils.log("DEBUG"," ***** preProcessRequest *****",  isDebugEnabled)
76                 execution.setVariable("rollbackAAI",false)
77                 execution.setVariable("rollbackAdded",false)
78                 execution.setVariable("rollbackDeleted",false)
79                 
80                 List addResourceList = execution.getVariable("addResourceList")
81         List delResourceList = execution.getVariable("delResourceList")
82         execution.setVariable("addResourceList_o",  addResourceList)
83         execution.setVariable("delResourceList_o",  delResourceList)
84         //exchange add and delete resource list
85         execution.setVariable("addResourceList",  delResourceList)        
86         execution.setVariable("delResourceList",  addResourceList)
87
88                 try {
89                         def rollbackData = execution.getVariable("rollbackData")
90                         utils.log("DEBUG", "RollbackData:" + rollbackData, isDebugEnabled)
91
92                         if (rollbackData != null) {
93                                 if (rollbackData.hasType("SERVICEINSTANCE")) {
94
95                                         def serviceInstanceId = rollbackData.get("SERVICEINSTANCE", "serviceInstanceId")
96                                         execution.setVariable("serviceInstanceId", serviceInstanceId)
97
98                                         def subscriptionServiceType = rollbackData.get("SERVICEINSTANCE", "subscriptionServiceType")
99                                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)
100
101                                         def globalSubscriberId  = rollbackData.get("SERVICEINSTANCE", "globalSubscriberId")
102                                         execution.setVariable("globalSubscriberId", globalSubscriberId)
103
104                                         def rollbackAAI = rollbackData.get("SERVICEINSTANCE", "rollbackAAI")
105                                         if ("true".equals(rollbackAAI))
106                                         {
107                                                 execution.setVariable("rollbackAAI",true)
108                                         }
109
110                                         def rollbackAdded = rollbackData.get("SERVICEINSTANCE", "rollbackAdded")
111                                         if ("true".equals(rollbackAdded))
112                                         {
113                                                 execution.setVariable("rollbackAdded", true)
114                                         }
115                                         
116                                         def rollbackDeleted = rollbackData.get("SERVICEINSTANCE", "rollbackDeleted")
117                                         if ("true".equals(rollbackDeleted))
118                                         {
119                                                 execution.setVariable("rollbackDeleted", true)
120                                         }                                       
121
122                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackAdded") != true 
123                                           && execution.getVariable("rollbackDeleted") != true)
124                                         {
125                                                 execution.setVariable("skipRollback", true)
126                                         }
127
128                                 }
129                                 else {
130                                         execution.setVariable("skipRollback", true)
131                                 }
132                         }
133                         else {
134                                 execution.setVariable("skipRollback", true)
135                         }
136                         if (execution.getVariable("disableRollback").equals("true" ))
137                         {
138                                 execution.setVariable("skipRollback", true)
139                         }
140
141                 } catch (BpmnError e) {
142                         throw e;
143                 } catch (Exception ex){
144                         msg = "Exception in Update ServiceInstance Rollback preProcessRequest " + ex.getMessage()
145                         utils.log("DEBUG", msg, isDebugEnabled)
146                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
147                 }
148                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
149         }
150
151         public void postProcessRequest(DelegateExecution execution) {
152                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
153                 utils.log("DEBUG"," ***** postProcessRequest ***** ", isDebugEnabled)
154                 String msg = ""
155                 try {
156                         execution.setVariable("rollbackData", null)
157                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
158                         boolean rollbackAAI = execution.getVariable("rollbackAAI")
159                         boolean rollbackAdded = execution.getVariable("rollbackAdded")
160                         boolean rollbackDeleted = execution.getVariable("rollbackDeleted")
161                         
162                         List addResourceList = execution.getVariable("addResourceList_o")
163                         List delResourceList = execution.getVariable("delResourceList_o")
164                         execution.setVariable("addResourceList",  addResourceList)
165                         execution.setVariable("delResourceList",  delResourceList)
166
167                         if (rollbackAAI || rollbackAdded || rollbackDeleted)
168                         {
169                                 execution.setVariable("rolledBack", true)
170                         }
171                         if (rollbackAAI)
172                         {
173                                 boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")
174                                 if(!succInAAI){
175                                         execution.setVariable("rolledBack", false) //both sdnc and aai must be successful to declare rollback Succesful
176                                         execution.setVariable("rollbackError", "Error deleting service-instance in AAI for rollback")
177                                         utils.log("DEBUG","Error deleting service-instance in AAI for rollback", + serviceInstanceId, isDebugEnabled)
178                                 }
179                         }
180                         utils.log("DEBUG","*** Exit postProcessRequest ***", isDebugEnabled)
181
182                 } catch (BpmnError e) {
183                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + e.getMessage()
184                         utils.log("DEBUG", msg, isDebugEnabled)
185                 } catch (Exception ex) {
186                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + ex.getMessage()
187                         utils.log("DEBUG", msg, isDebugEnabled)
188                 }
189         }
190         
191     
192     public void preProcessForAddResource(DelegateExecution execution) {
193         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
194     }
195
196     public void postProcessForAddResource(DelegateExecution execution) {
197         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
198     
199     }
200     
201     public void preProcessForDeleteResource(DelegateExecution execution) {
202         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
203
204     }
205
206     public void postProcessForDeleteResource(DelegateExecution execution) {
207         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
208     
209     } 
210
211         public void preProcessAAIGET(DelegateExecution execution) {
212         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
213         }
214         
215         public void postProcessAAIGET(DelegateExecution execution) {
216                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
217                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
218                 String msg = ""
219
220                 try {
221                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
222                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
223                         if(!succInAAI){
224                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET", + serviceInstanceName, isDebugEnabled)
225                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
226                                 utils.logAudit("workflowException: " + workflowException)
227                                 if(workflowException != null){
228                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
229                                 }
230                                 else
231                                 {
232                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
233                                         utils.log("INFO", msg, isDebugEnabled)
234                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
235                                 }
236                         }
237                         else
238                         {
239                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
240                                 if(foundInAAI){
241                                         String aaiService = execution.getVariable("GENGS_service")
242                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "resource-version"))) {
243                                                 execution.setVariable("serviceInstanceVersion_n",  utils.getNodeText1(aaiService, "resource-version"))
244                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)
245                                         }
246                                 }
247                         }
248                 } catch (BpmnError e) {
249                         throw e;
250                 } catch (Exception ex) {
251                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET " + ex.getMessage()
252                         utils.log("INFO", msg, isDebugEnabled)
253                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
254                 }
255                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
256         }    
257
258         public void preProcessAAIPUT(DelegateExecution execution) {             
259                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
260                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
261                 utils.log("INFO","Entered " + method, isDebugEnabled)
262                 String msg = ""
263                 utils.log("INFO"," ***** preProcessAAIPUT *****",  isDebugEnabled)
264
265                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion_n")
266 //              execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)
267         
268                 //requestDetails.modelInfo.for AAI PUT servieInstanceData
269                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData
270                 String serviceInstanceName = execution.getVariable("serviceInstanceName")
271                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
272                 //aai serviceType and Role can be setted as fixed value now.
273                 String aaiServiceType = "E2E Service"
274                 String aaiServiceRole = "E2E Service"
275                 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
276                 String modelUuid = execution.getVariable("model-version-id-original")
277
278                 //AAI PUT      
279                 AaiUtil aaiUriUtil = new AaiUtil(this)
280                 utils.log("INFO","start create aai uri: " + aaiUriUtil, isDebugEnabled)
281                 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
282                 utils.log("INFO","aai_uri: " + aai_uri, isDebugEnabled)
283                 String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
284                 utils.log("INFO","namespace: " + namespace, isDebugEnabled)
285
286                 String serviceInstanceData =
287                                 """<service-instance xmlns=\"${namespace}\">
288                     <service-instance-id>${serviceInstanceId}</service-instance-id>
289                     <service-instance-name>${serviceInstanceName}</service-instance-name>
290                     <service-type>${aaiServiceType}</service-type>
291                     <service-role>${aaiServiceRole}</service-role>
292                     <resource-version>${serviceInstanceVersion}</resource-version>
293                     <model-invariant-id>${modelInvariantUuid}</model-invariant-id>
294                     <model-version-id>${modelUuid}</model-version-id>   
295                                  </service-instance>""".trim()
296
297                 execution.setVariable("serviceInstanceData", serviceInstanceData)
298                 utils.log("INFO","serviceInstanceData: " + serviceInstanceData, isDebugEnabled)
299                 utils.logAudit(serviceInstanceData)
300                 utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)
301                 utils.log("INFO", " 'payload' to update Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
302         
303                 utils.log("INFO", "Exited " + method, isDebugEnabled)
304         }       
305         
306         public void postProcessAAIPUT(DelegateExecution execution) {
307                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
308                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)
309                 String msg = ""
310                 try {
311                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
312                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
313                         if(!succInAAI){
314                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)
315                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
316                                 utils.logAudit("workflowException: " + workflowException)
317                                 if(workflowException != null){
318                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
319                                 }
320                         }
321                         else
322                         {
323                                 
324                         }
325
326                 } catch (BpmnError e) {
327                         throw e;
328                 } catch (Exception ex) {
329                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
330                         utils.log("INFO", msg, isDebugEnabled)
331                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
332                 }
333                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)
334         }       
335
336         public void processRollbackException(DelegateExecution execution){
337                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
338                 utils.log("DEBUG"," ***** processRollbackException ***** ", isDebugEnabled)
339                 try{
340                         utils.log("DEBUG", "Caught an Exception in DoUpdateE2EServiceInstanceRollback", isDebugEnabled)
341                         execution.setVariable("rollbackData", null)
342                         execution.setVariable("rollbackError", "Caught exception in ServiceInstance Update Rollback")
343                         execution.setVariable("WorkflowException", null)
344
345                 }catch(BpmnError b){
346                         utils.log("DEBUG", "BPMN Error during processRollbackExceptions Method: ", isDebugEnabled)
347                 }catch(Exception e){
348                         utils.log("DEBUG", "Caught Exception during processRollbackExceptions Method: " + e.getMessage(), isDebugEnabled)
349                 }
350
351                 utils.log("DEBUG", " Exit processRollbackException", isDebugEnabled)
352         }
353
354         public void processRollbackJavaException(DelegateExecution execution){
355                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
356                 utils.log("DEBUG"," ***** processRollbackJavaException ***** ", isDebugEnabled)
357                 try{
358                         execution.setVariable("rollbackData", null)
359                         execution.setVariable("rollbackError", "Caught Java exception in ServiceInstance Update Rollback")
360                         utils.log("DEBUG", "Caught Exception in processRollbackJavaException", isDebugEnabled)
361
362                 }catch(Exception e){
363                         utils.log("DEBUG", "Caught Exception in processRollbackJavaException " + e.getMessage(), isDebugEnabled)
364                 }
365                 utils.log("DEBUG", "***** Exit processRollbackJavaException *****", isDebugEnabled)
366         }
367
368 }