Merge "Adding Junit"
[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.AbstractServiceTaskProcessor
29 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
30 import org.openecomp.mso.bpmn.core.RollbackData
31 import org.openecomp.mso.bpmn.core.WorkflowException
32 import org.openecomp.mso.rest.APIResponse;
33 import org.openecomp.mso.rest.RESTClient
34 import org.openecomp.mso.rest.RESTConfig
35
36 import java.util.UUID;
37
38 import org.camunda.bpm.engine.delegate.BpmnError
39 import org.camunda.bpm.engine.delegate.DelegateExecution
40 import org.json.JSONObject;
41 import org.apache.commons.lang3.*
42 import org.apache.commons.codec.binary.Base64;
43 import org.springframework.web.util.UriUtils;
44 /**
45  * This groovy class supports the <class>DoUpdateE2EServiceInstanceRollback.bpmn</class> process.
46  *
47  * Inputs:
48  * @param - msoRequestId
49  * @param - rollbackData with
50  *          globalCustomerId
51  *                      subscriptionServiceType
52  *                      serviceInstanceId
53  *                      disableRollback
54  *                      rollbackAAI
55  *                      rollbackAdded
56  *                      rollbackDeleted
57  *
58  *
59  * Outputs:
60  * @param - rollbackError
61  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
62  *
63  */
64 public class DoUpdateE2EServiceInstanceRollback extends AbstractServiceTaskProcessor{
65
66         String Prefix="DUPDSIRB_"
67
68         public void preProcessRequest(DelegateExecution execution) {
69                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
70                 execution.setVariable("prefix",Prefix)
71                 String msg = ""
72                 utils.log("DEBUG"," ***** preProcessRequest *****",  isDebugEnabled)
73                 execution.setVariable("rollbackAAI",false)
74                 execution.setVariable("rollbackAdded",false)
75                 execution.setVariable("rollbackDeleted",false)
76                 
77                 List addResourceList = execution.getVariable("addResourceList")
78         List delResourceList = execution.getVariable("delResourceList")
79         execution.setVariable("addResourceList_o",  addResourceList)
80         execution.setVariable("delResourceList_o",  delResourceList)
81         //exchange add and delete resource list
82         execution.setVariable("addResourceList",  delResourceList)        
83         execution.setVariable("delResourceList",  addResourceList)
84
85                 try {
86                         def rollbackData = execution.getVariable("rollbackData")
87                         utils.log("DEBUG", "RollbackData:" + rollbackData, isDebugEnabled)
88
89                         if (rollbackData != null) {
90                                 if (rollbackData.hasType("SERVICEINSTANCE")) {
91
92                                         def serviceInstanceId = rollbackData.get("SERVICEINSTANCE", "serviceInstanceId")
93                                         execution.setVariable("serviceInstanceId", serviceInstanceId)
94
95                                         def subscriptionServiceType = rollbackData.get("SERVICEINSTANCE", "subscriptionServiceType")
96                                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)
97
98                                         def globalSubscriberId  = rollbackData.get("SERVICEINSTANCE", "globalSubscriberId")
99                                         execution.setVariable("globalSubscriberId", globalSubscriberId)
100
101                                         def rollbackAAI = rollbackData.get("SERVICEINSTANCE", "rollbackAAI")
102                                         if ("true".equals(rollbackAAI))
103                                         {
104                                                 execution.setVariable("rollbackAAI",true)
105                                         }
106
107                                         def rollbackAdded = rollbackData.get("SERVICEINSTANCE", "rollbackAdded")
108                                         if ("true".equals(rollbackAdded))
109                                         {
110                                                 execution.setVariable("rollbackAdded", true)
111                                         }
112                                         
113                                         def rollbackDeleted = rollbackData.get("SERVICEINSTANCE", "rollbackDeleted")
114                                         if ("true".equals(rollbackDeleted))
115                                         {
116                                                 execution.setVariable("rollbackDeleted", true)
117                                         }                                       
118
119                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackAdded") != true 
120                                           && execution.getVariable("rollbackDeleted") != true)
121                                         {
122                                                 execution.setVariable("skipRollback", true)
123                                         }
124
125                                 }
126                                 else {
127                                         execution.setVariable("skipRollback", true)
128                                 }
129                         }
130                         else {
131                                 execution.setVariable("skipRollback", true)
132                         }
133                         if (execution.getVariable("disableRollback").equals("true" ))
134                         {
135                                 execution.setVariable("skipRollback", true)
136                         }
137
138                 } catch (BpmnError e) {
139                         throw e;
140                 } catch (Exception ex){
141                         msg = "Exception in Update ServiceInstance Rollback preProcessRequest " + ex.getMessage()
142                         utils.log("DEBUG", msg, isDebugEnabled)
143                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
144                 }
145                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
146         }
147
148         public void postProcessRequest(DelegateExecution execution) {
149                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
150                 utils.log("DEBUG"," ***** postProcessRequest ***** ", isDebugEnabled)
151                 String msg = ""
152                 try {
153                         execution.setVariable("rollbackData", null)
154                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
155                         boolean rollbackAAI = execution.getVariable("rollbackAAI")
156                         boolean rollbackAdded = execution.getVariable("rollbackAdded")
157                         boolean rollbackDeleted = execution.getVariable("rollbackDeleted")
158                         
159                         List addResourceList = execution.getVariable("addResourceList_o")
160                         List delResourceList = execution.getVariable("delResourceList_o")
161                         execution.setVariable("addResourceList",  addResourceList)
162                         execution.setVariable("delResourceList",  delResourceList)
163
164                         if (rollbackAAI || rollbackAdded || rollbackDeleted)
165                         {
166                                 execution.setVariable("rolledBack", true)
167                         }
168                         if (rollbackAAI)
169                         {
170                                 boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")
171                                 if(!succInAAI){
172                                         execution.setVariable("rolledBack", false) //both sdnc and aai must be successful to declare rollback Succesful
173                                         execution.setVariable("rollbackError", "Error deleting service-instance in AAI for rollback")
174                                         utils.log("DEBUG","Error deleting service-instance in AAI for rollback", + serviceInstanceId, isDebugEnabled)
175                                 }
176                         }
177                         utils.log("DEBUG","*** Exit postProcessRequest ***", isDebugEnabled)
178
179                 } catch (BpmnError e) {
180                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + e.getMessage()
181                         utils.log("DEBUG", msg, isDebugEnabled)
182                 } catch (Exception ex) {
183                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + ex.getMessage()
184                         utils.log("DEBUG", msg, isDebugEnabled)
185                 }
186         }
187         
188     
189     public void preProcessForAddResource(DelegateExecution execution) {
190         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
191     }
192
193     public void postProcessForAddResource(DelegateExecution execution) {
194         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
195     
196     }
197     
198     public void preProcessForDeleteResource(DelegateExecution execution) {
199         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
200
201     }
202
203     public void postProcessForDeleteResource(DelegateExecution execution) {
204         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
205     
206     } 
207
208         public void preProcessAAIGET(DelegateExecution execution) {
209         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
210         }
211         
212         public void postProcessAAIGET(DelegateExecution execution) {
213                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
214                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
215                 String msg = ""
216
217                 try {
218                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
219                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
220                         if(!succInAAI){
221                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET", + serviceInstanceName, isDebugEnabled)
222                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
223                                 utils.logAudit("workflowException: " + workflowException)
224                                 if(workflowException != null){
225                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
226                                 }
227                                 else
228                                 {
229                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
230                                         utils.log("INFO", msg, isDebugEnabled)
231                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
232                                 }
233                         }
234                         else
235                         {
236                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
237                                 if(foundInAAI){
238                                         String aaiService = execution.getVariable("GENGS_service")
239                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "resource-version"))) {
240                                                 execution.setVariable("serviceInstanceVersion_n",  utils.getNodeText1(aaiService, "resource-version"))
241                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)
242                                         }
243                                 }
244                         }
245                 } catch (BpmnError e) {
246                         throw e;
247                 } catch (Exception ex) {
248                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET " + ex.getMessage()
249                         utils.log("INFO", msg, isDebugEnabled)
250                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
251                 }
252                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
253         }    
254
255         public void preProcessAAIPUT(DelegateExecution execution) {             
256                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
257                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
258                 utils.log("INFO","Entered " + method, isDebugEnabled)
259                 String msg = ""
260                 utils.log("INFO"," ***** preProcessAAIPUT *****",  isDebugEnabled)
261
262                 String modelUuid = execution.getVariable("model-version-id-original")
263                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion_n")
264                 execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)
265
266                 String serviceInstanceData =
267                                 """<service-instance xmlns=\"${namespace}\">
268                                <resource-version">${modelUuid}</resource-version>
269                                  </service-instance>""".trim()
270
271                 execution.setVariable("serviceInstanceData", serviceInstanceData)
272                 utils.log("INFO","serviceInstanceData: " + serviceInstanceData, isDebugEnabled)
273                 utils.logAudit(serviceInstanceData)
274                 utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)
275                 utils.log("INFO", " 'payload' to update Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
276         
277                 utils.log("INFO", "Exited " + method, isDebugEnabled)
278         }       
279         
280         public void postProcessAAIPUT(DelegateExecution execution) {
281                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
282                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)
283                 String msg = ""
284                 try {
285                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
286                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
287                         if(!succInAAI){
288                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)
289                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
290                                 utils.logAudit("workflowException: " + workflowException)
291                                 if(workflowException != null){
292                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
293                                 }
294                         }
295                         else
296                         {
297                                 
298                         }
299
300                 } catch (BpmnError e) {
301                         throw e;
302                 } catch (Exception ex) {
303                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
304                         utils.log("INFO", msg, isDebugEnabled)
305                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
306                 }
307                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)
308         }       
309
310         public void processRollbackException(DelegateExecution execution){
311                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
312                 utils.log("DEBUG"," ***** processRollbackException ***** ", isDebugEnabled)
313                 try{
314                         utils.log("DEBUG", "Caught an Exception in DoUpdateE2EServiceInstanceRollback", isDebugEnabled)
315                         execution.setVariable("rollbackData", null)
316                         execution.setVariable("rollbackError", "Caught exception in ServiceInstance Update Rollback")
317                         execution.setVariable("WorkflowException", null)
318
319                 }catch(BpmnError b){
320                         utils.log("DEBUG", "BPMN Error during processRollbackExceptions Method: ", isDebugEnabled)
321                 }catch(Exception e){
322                         utils.log("DEBUG", "Caught Exception during processRollbackExceptions Method: " + e.getMessage(), isDebugEnabled)
323                 }
324
325                 utils.log("DEBUG", " Exit processRollbackException", isDebugEnabled)
326         }
327
328         public void processRollbackJavaException(DelegateExecution execution){
329                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
330                 utils.log("DEBUG"," ***** processRollbackJavaException ***** ", isDebugEnabled)
331                 try{
332                         execution.setVariable("rollbackData", null)
333                         execution.setVariable("rollbackError", "Caught Java exception in ServiceInstance Update Rollback")
334                         utils.log("DEBUG", "Caught Exception in processRollbackJavaException", isDebugEnabled)
335
336                 }catch(Exception e){
337                         utils.log("DEBUG", "Caught Exception in processRollbackJavaException " + e.getMessage(), isDebugEnabled)
338                 }
339                 utils.log("DEBUG", "***** Exit processRollbackJavaException *****", isDebugEnabled)
340         }
341
342 }