743eb1a46e2282d99ed733024d6ddf2b93b4c1e2
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCreateE2EServiceInstanceRollback.groovy
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP - SO\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.\r
7  * ================================================================================\r
8  * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * you may not use this file except in compliance with the License.\r
10  * You may obtain a copy of the License at\r
11  *\r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  *\r
14  * Unless required by applicable law or agreed to in writing, software\r
15  * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * See the License for the specific language governing permissions and\r
18  * limitations under the License.\r
19  * ============LICENSE_END=========================================================\r
20  */\r
21 package org.openecomp.mso.bpmn.infrastructure.scripts\r
22 \r
23 \r
24 import static org.apache.commons.lang3.StringUtils.*;\r
25 import groovy.xml.XmlUtil\r
26 import groovy.json.*\r
27 \r
28 import org.openecomp.mso.bpmn.core.json.JsonUtils\r
29 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor\r
30 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils\r
31 import org.openecomp.mso.bpmn.core.RollbackData\r
32 import org.openecomp.mso.bpmn.core.WorkflowException\r
33 import org.openecomp.mso.rest.APIResponse;\r
34 import org.openecomp.mso.rest.RESTClient\r
35 import org.openecomp.mso.rest.RESTConfig\r
36 \r
37 import java.util.UUID;\r
38 \r
39 import org.camunda.bpm.engine.delegate.BpmnError\r
40 import org.camunda.bpm.engine.runtime.Execution\r
41 import org.json.JSONObject;\r
42 import org.apache.commons.lang3.*\r
43 import org.apache.commons.codec.binary.Base64;\r
44 import org.springframework.web.util.UriUtils;\r
45 /**\r
46  * This groovy class supports the <class>DoCreateServiceInstanceRollback.bpmn</class> process.\r
47  *\r
48  * Inputs:\r
49  * @param - msoRequestId\r
50  * @param - rollbackData with\r
51  *          globalCustomerId\r
52  *                      subscriptionServiceType\r
53  *                      serviceInstanceId\r
54  *                      disableRollback\r
55  *                      rollbackAAI\r
56  *                      rollbackSDNC\r
57  *                      sdncRollbackRequest\r
58  * \r
59  *\r
60  * Outputs:\r
61  * @param - rollbackError\r
62  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)\r
63  *\r
64  */\r
65 public class DoCreateE2EServiceInstanceRollback extends AbstractServiceTaskProcessor{\r
66 \r
67         String Prefix="DCRESIRB_"\r
68 \r
69         public void preProcessRequest(Execution execution) {\r
70                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")\r
71                 execution.setVariable("prefix",Prefix)\r
72                 String msg = ""\r
73                 utils.log("DEBUG"," ***** preProcessRequest *****",  isDebugEnabled)\r
74                 execution.setVariable("rollbackAAI",false)\r
75                 execution.setVariable("rollbackSDNC",false)\r
76 \r
77                 try {\r
78                         def rollbackData = execution.getVariable("rollbackData")\r
79                         utils.log("DEBUG", "RollbackData:" + rollbackData, isDebugEnabled)\r
80 \r
81                         if (rollbackData != null) {\r
82                                 if (rollbackData.hasType("SERVICEINSTANCE")) {\r
83 \r
84                                         def serviceInstanceId = rollbackData.get("SERVICEINSTANCE", "serviceInstanceId")\r
85                                         execution.setVariable("serviceInstanceId", serviceInstanceId)\r
86 \r
87                                         def subscriptionServiceType = rollbackData.get("SERVICEINSTANCE", "subscriptionServiceType")\r
88                                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)\r
89 \r
90                                         def globalSubscriberId  = rollbackData.get("SERVICEINSTANCE", "globalSubscriberId")\r
91                                         execution.setVariable("globalSubscriberId", globalSubscriberId)\r
92 \r
93                                         def rollbackAAI = rollbackData.get("SERVICEINSTANCE", "rollbackAAI")\r
94                                         if ("true".equals(rollbackAAI))\r
95                                         {\r
96                                                 execution.setVariable("rollbackAAI",true)\r
97                                         }\r
98 \r
99                                         def rollbackSDNC = rollbackData.get("SERVICEINSTANCE", "rollbackSDNC")\r
100                                         if ("true".equals(rollbackSDNC))\r
101                                         {\r
102                                                 execution.setVariable("rollbackSDNC", true)\r
103                                         }\r
104 \r
105                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackSDNC") != true)\r
106                                         {\r
107                                                 execution.setVariable("skipRollback", true)\r
108                                         }\r
109 \r
110                                         def sdncDelete = rollbackData.get("SERVICEINSTANCE", "sdncDelete")\r
111                                         execution.setVariable("sdncDelete", sdncDelete)\r
112                                         def sdncDeactivate = rollbackData.get("SERVICEINSTANCE", "sdncDeactivate")\r
113                                         execution.setVariable("sdncDeactivate", sdncDeactivate)\r
114                                         utils.log("DEBUG","sdncDeactivate:\n" + sdncDeactivate, isDebugEnabled)\r
115                                         utils.log("DEBUG","sdncDelete:\n" + sdncDelete, isDebugEnabled)\r
116                                 }\r
117                                 else {\r
118                                         execution.setVariable("skipRollback", true)\r
119                                 }\r
120                         }\r
121                         else {\r
122                                 execution.setVariable("skipRollback", true)\r
123                         }\r
124                         if (execution.getVariable("disableRollback").equals("true" ))\r
125                         {\r
126                                 execution.setVariable("skipRollback", true)\r
127                         }\r
128 \r
129                 } catch (BpmnError e) {\r
130                         throw e;\r
131                 } catch (Exception ex){\r
132                         msg = "Exception in Create ServiceInstance Rollback preProcessRequest " + ex.getMessage()\r
133                         utils.log("DEBUG", msg, isDebugEnabled)\r
134                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
135                 }\r
136                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)\r
137         }\r
138 \r
139         public void validateSDNCResponse(Execution execution, String response, String method) {\r
140                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
141                 utils.log("DEBUG"," ***** validateSDNCResponse ***** ", isDebugEnabled)\r
142                 String msg = ""\r
143                 try {\r
144                         WorkflowException workflowException = execution.getVariable("WorkflowException")\r
145                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")\r
146                         utils.log("DEBUG", "SDNCResponse: " + response, isDebugEnabled)\r
147                         utils.log("DEBUG", "workflowException: " + workflowException, isDebugEnabled)\r
148                         \r
149                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)\r
150                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)\r
151 \r
152                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){\r
153                                 msg = "SDNC Adapter service-instance rollback successful for " + method\r
154                                 utils.log("DEBUG", msg, isDebugEnabled)\r
155                         }else{\r
156                                 execution.setVariable("rolledBack", false)\r
157                                 msg =  "Error Response from SDNC Adapter service-instance rollback for " + method\r
158                                 execution.setVariable("rollbackError", msg)\r
159                                 utils.log("DEBUG", msg, isDebugEnabled)\r
160                                 throw new BpmnError("MSOWorkflowException")\r
161                         }\r
162                 } catch (BpmnError e) {\r
163                         throw e;\r
164                 } catch (Exception ex){\r
165                         msg = "Exception in Create ServiceInstance rollback for "  + method  + " Exception:" + ex.getMessage()\r
166                         utils.log("DEBUG", msg, isDebugEnabled)\r
167                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)\r
168                 }\r
169                 utils.log("DEBUG"," ***** Exit validateSDNCResponse ***** ", isDebugEnabled)\r
170         }\r
171 \r
172         public void postProcessRequest(Execution execution) {\r
173                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
174                 utils.log("DEBUG"," ***** postProcessRequest ***** ", isDebugEnabled)\r
175                 String msg = ""\r
176                 try {\r
177                         execution.setVariable("rollbackData", null)\r
178                         String serviceInstanceId = execution.getVariable("serviceInstanceId")\r
179                         boolean rollbackAAI = execution.getVariable("rollbackAAI")\r
180                         boolean rollbackSDNC = execution.getVariable("rollbackSDNC")\r
181                         if (rollbackAAI || rollbackSDNC)\r
182                         {\r
183                                 execution.setVariable("rolledBack", true)\r
184                         }\r
185                         if (rollbackAAI)\r
186                         {\r
187                                 boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")\r
188                                 if(!succInAAI){\r
189                                         execution.setVariable("rolledBack", false) //both sdnc and aai must be successful to declare rollback Succesful\r
190                                         execution.setVariable("rollbackError", "Error deleting service-instance in AAI for rollback")\r
191                                         utils.log("DEBUG","Error deleting service-instance in AAI for rollback", + serviceInstanceId, isDebugEnabled)\r
192                                 }\r
193                         }\r
194                         utils.log("DEBUG","*** Exit postProcessRequest ***", isDebugEnabled)\r
195 \r
196                 } catch (BpmnError e) {\r
197                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + e.getMessage()\r
198                         utils.log("DEBUG", msg, isDebugEnabled)\r
199                 } catch (Exception ex) {\r
200                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + ex.getMessage()\r
201                         utils.log("DEBUG", msg, isDebugEnabled)\r
202                 }\r
203 \r
204         }\r
205 \r
206         public void processRollbackException(Execution execution){\r
207                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
208                 utils.log("DEBUG"," ***** processRollbackException ***** ", isDebugEnabled)\r
209                 try{\r
210                         utils.log("DEBUG", "Caught an Exception in DoCreateServiceInstanceRollback", isDebugEnabled)\r
211                         execution.setVariable("rollbackData", null)\r
212                         execution.setVariable("rollbackError", "Caught exception in ServiceInstance Create Rollback")\r
213                         execution.setVariable("WorkflowException", null)\r
214 \r
215                 }catch(BpmnError b){\r
216                         utils.log("DEBUG", "BPMN Error during processRollbackExceptions Method: ", isDebugEnabled)\r
217                 }catch(Exception e){\r
218                         utils.log("DEBUG", "Caught Exception during processRollbackExceptions Method: " + e.getMessage(), isDebugEnabled)\r
219                 }\r
220 \r
221                 utils.log("DEBUG", " Exit processRollbackException", isDebugEnabled)\r
222         }\r
223 \r
224         public void processRollbackJavaException(Execution execution){\r
225                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")\r
226                 utils.log("DEBUG"," ***** processRollbackJavaException ***** ", isDebugEnabled)\r
227                 try{\r
228                         execution.setVariable("rollbackData", null)\r
229                         execution.setVariable("rollbackError", "Caught Java exception in ServiceInstance Create Rollback")\r
230                         utils.log("DEBUG", "Caught Exception in processRollbackJavaException", isDebugEnabled)\r
231 \r
232                 }catch(Exception e){\r
233                         utils.log("DEBUG", "Caught Exception in processRollbackJavaException " + e.getMessage(), isDebugEnabled)\r
234                 }\r
235                 utils.log("DEBUG", "***** Exit processRollbackJavaException *****", isDebugEnabled)\r
236         }\r
237 \r
238 }\r