2891855e8f454b05cddf38661b901d9778f02568
[so.git] /
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("addResourceList",  delResourceList)
81         execution.setVariable("delResourceList_o",  delResourceList)
82         execution.setVariable("delResourceList",  addResourceList)
83
84                 try {
85                         def rollbackData = execution.getVariable("rollbackData")
86                         utils.log("DEBUG", "RollbackData:" + rollbackData, isDebugEnabled)
87
88                         if (rollbackData != null) {
89                                 if (rollbackData.hasType("SERVICEINSTANCE")) {
90
91                                         def serviceInstanceId = rollbackData.get("SERVICEINSTANCE", "serviceInstanceId")
92                                         execution.setVariable("serviceInstanceId", serviceInstanceId)
93
94                                         def subscriptionServiceType = rollbackData.get("SERVICEINSTANCE", "subscriptionServiceType")
95                                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)
96
97                                         def globalSubscriberId  = rollbackData.get("SERVICEINSTANCE", "globalSubscriberId")
98                                         execution.setVariable("globalSubscriberId", globalSubscriberId)
99
100                                         def rollbackAAI = rollbackData.get("SERVICEINSTANCE", "rollbackAAI")
101                                         if ("true".equals(rollbackAAI))
102                                         {
103                                                 execution.setVariable("rollbackAAI",true)
104                                         }
105
106                                         def rollbackAdded = rollbackData.get("SERVICEINSTANCE", "rollbackAdded")
107                                         if ("true".equals(rollbackAdded))
108                                         {
109                                                 execution.setVariable("rollbackAdded", true)
110                                         }
111                                         
112                                         def rollbackDeleted = rollbackData.get("SERVICEINSTANCE", "rollbackDeleted")
113                                         if ("true".equals(rollbackDeleted))
114                                         {
115                                                 execution.setVariable("rollbackDeleted", true)
116                                         }                                       
117
118                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackAdded") != true 
119                                           && execution.getVariable("rollbackDeleted") != true)
120                                         {
121                                                 execution.setVariable("skipRollback", true)
122                                         }
123
124                                 }
125                                 else {
126                                         execution.setVariable("skipRollback", true)
127                                 }
128                         }
129                         else {
130                                 execution.setVariable("skipRollback", true)
131                         }
132                         if (execution.getVariable("disableRollback").equals("true" ))
133                         {
134                                 execution.setVariable("skipRollback", true)
135                         }
136
137                 } catch (BpmnError e) {
138                         throw e;
139                 } catch (Exception ex){
140                         msg = "Exception in Update ServiceInstance Rollback preProcessRequest " + ex.getMessage()
141                         utils.log("DEBUG", msg, isDebugEnabled)
142                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
143                 }
144                 utils.log("DEBUG"," ***** Exit preProcessRequest *****",  isDebugEnabled)
145         }
146
147         public void postProcessRequest(DelegateExecution execution) {
148                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
149                 utils.log("DEBUG"," ***** postProcessRequest ***** ", isDebugEnabled)
150                 String msg = ""
151                 try {
152                         execution.setVariable("rollbackData", null)
153                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
154                         boolean rollbackAAI = execution.getVariable("rollbackAAI")
155                         boolean rollbackAdded = execution.getVariable("rollbackAdded")
156                         boolean rollbackDeleted = execution.getVariable("rollbackDeleted")
157                         
158                         List addResourceList = execution.getVariable("addResourceList_o")
159                         List delResourceList = execution.getVariable("delResourceList_o")
160                         execution.setVariable("addResourceList",  addResourceList)
161                         execution.setVariable("delResourceList",  delResourceList)
162
163                         if (rollbackAAI || rollbackAdded || rollbackDeleted)
164                         {
165                                 execution.setVariable("rolledBack", true)
166                         }
167                         if (rollbackAAI)
168                         {
169                                 boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")
170                                 if(!succInAAI){
171                                         execution.setVariable("rolledBack", false) //both sdnc and aai must be successful to declare rollback Succesful
172                                         execution.setVariable("rollbackError", "Error deleting service-instance in AAI for rollback")
173                                         utils.log("DEBUG","Error deleting service-instance in AAI for rollback", + serviceInstanceId, isDebugEnabled)
174                                 }
175                         }
176                         utils.log("DEBUG","*** Exit postProcessRequest ***", isDebugEnabled)
177
178                 } catch (BpmnError e) {
179                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + e.getMessage()
180                         utils.log("DEBUG", msg, isDebugEnabled)
181                 } catch (Exception ex) {
182                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + ex.getMessage()
183                         utils.log("DEBUG", msg, isDebugEnabled)
184                 }
185         }
186         
187     
188     public void preProcessForAddResource(DelegateExecution execution) {
189         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
190     }
191
192     public void postProcessForAddResource(DelegateExecution execution) {
193         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
194     
195     }
196     
197     public void preProcessForDeleteResource(DelegateExecution execution) {
198         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
199
200     }
201
202     public void postProcessForDeleteResource(DelegateExecution execution) {
203         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
204     
205     } 
206
207         public void preProcessAAIGET2(DelegateExecution execution) {
208         def isDebugEnabled=execution.getVariable("isDebugLogEnabled")   
209         }
210         
211         public void postProcessAAIGET(DelegateExecution execution) {
212                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
213                 utils.log("INFO"," ***** postProcessAAIGET ***** ", isDebugEnabled)
214                 String msg = ""
215
216                 try {
217                         String serviceInstanceName = execution.getVariable("serviceInstanceName")
218                         boolean succInAAI = execution.getVariable("GENGS_SuccessIndicator")
219                         if(!succInAAI){
220                                 utils.log("INFO","Error getting Service-instance from AAI in postProcessAAIGET", + serviceInstanceName, isDebugEnabled)
221                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
222                                 utils.logAudit("workflowException: " + workflowException)
223                                 if(workflowException != null){
224                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
225                                 }
226                                 else
227                                 {
228                                         msg = "Failure in postProcessAAIGET GENGS_SuccessIndicator:" + succInAAI
229                                         utils.log("INFO", msg, isDebugEnabled)
230                                         exceptionUtil.buildAndThrowWorkflowException(execution, 2500, msg)
231                                 }
232                         }
233                         else
234                         {
235                                 boolean foundInAAI = execution.getVariable("GENGS_FoundIndicator")
236                                 if(foundInAAI){
237                                         String aaiService = execution.getVariable("GENGS_service")
238                                         if (!isBlank(aaiService) && (utils.nodeExists(aaiService, "resource-version"))) {
239                                                 execution.setVariable("serviceInstanceVersion_n",  utils.getNodeText1(aaiService, "resource-version"))
240                                                 utils.log("INFO","Found Service-instance in AAI.serviceInstanceName:" + execution.getVariable("serviceInstanceName"), isDebugEnabled)
241                                         }
242                                 }
243                         }
244                 } catch (BpmnError e) {
245                         throw e;
246                 } catch (Exception ex) {
247                         msg = "Exception in DoCreateServiceInstance.postProcessAAIGET " + ex.getMessage()
248                         utils.log("INFO", msg, isDebugEnabled)
249                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
250                 }
251                 utils.log("INFO"," *** Exit postProcessAAIGET *** ", isDebugEnabled)
252         }    
253
254         public void preProcessAAIPUT(DelegateExecution execution) {             
255                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
256                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
257                 utils.log("INFO","Entered " + method, isDebugEnabled)
258                 String msg = ""
259                 utils.log("INFO"," ***** preProcessAAIPUT *****",  isDebugEnabled)
260
261                 String modelUuid = execution.getVariable("model-version-id-original")
262                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion_n")
263                 execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)
264
265                 String serviceInstanceData =
266                                 """<service-instance xmlns=\"${namespace}\">
267                                <resource-version">${serviceInstanceVersion}</resource-version>
268                                  </service-instance>""".trim()
269
270                 execution.setVariable("serviceInstanceData", serviceInstanceData)
271                 utils.log("INFO","serviceInstanceData: " + serviceInstanceData, isDebugEnabled)
272                 utils.logAudit(serviceInstanceData)
273                 utils.log("INFO", " aai_uri " + aai_uri + " namespace:" + namespace, isDebugEnabled)
274                 utils.log("INFO", " 'payload' to update Service Instance in AAI - " + "\n" + serviceInstanceData, isDebugEnabled)
275         
276                 utils.log("INFO", "Exited " + method, isDebugEnabled)
277         }       
278         
279         public void postProcessAAIPUT(DelegateExecution execution) {
280                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
281                 utils.log("INFO"," ***** postProcessAAIPUT ***** ", isDebugEnabled)
282                 String msg = ""
283                 try {
284                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
285                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
286                         if(!succInAAI){
287                                 utils.log("INFO","Error putting Service-instance in AAI", + serviceInstanceId, isDebugEnabled)
288                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
289                                 utils.logAudit("workflowException: " + workflowException)
290                                 if(workflowException != null){
291                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
292                                 }
293                         }
294                         else
295                         {
296                                 
297                         }
298
299                 } catch (BpmnError e) {
300                         throw e;
301                 } catch (Exception ex) {
302                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
303                         utils.log("INFO", msg, isDebugEnabled)
304                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
305                 }
306                 utils.log("INFO"," *** Exit postProcessAAIPUT *** ", isDebugEnabled)
307         }       
308
309         public void processRollbackException(DelegateExecution execution){
310                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
311                 utils.log("DEBUG"," ***** processRollbackException ***** ", isDebugEnabled)
312                 try{
313                         utils.log("DEBUG", "Caught an Exception in DoCreateServiceInstanceRollback", isDebugEnabled)
314                         execution.setVariable("rollbackData", null)
315                         execution.setVariable("rollbackError", "Caught exception in ServiceInstance Update Rollback")
316                         execution.setVariable("WorkflowException", null)
317
318                 }catch(BpmnError b){
319                         utils.log("DEBUG", "BPMN Error during processRollbackExceptions Method: ", isDebugEnabled)
320                 }catch(Exception e){
321                         utils.log("DEBUG", "Caught Exception during processRollbackExceptions Method: " + e.getMessage(), isDebugEnabled)
322                 }
323
324                 utils.log("DEBUG", " Exit processRollbackException", isDebugEnabled)
325         }
326
327         public void processRollbackJavaException(DelegateExecution execution){
328                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
329                 utils.log("DEBUG"," ***** processRollbackJavaException ***** ", isDebugEnabled)
330                 try{
331                         execution.setVariable("rollbackData", null)
332                         execution.setVariable("rollbackError", "Caught Java exception in ServiceInstance Update Rollback")
333                         utils.log("DEBUG", "Caught Exception in processRollbackJavaException", isDebugEnabled)
334
335                 }catch(Exception e){
336                         utils.log("DEBUG", "Caught Exception in processRollbackJavaException " + e.getMessage(), isDebugEnabled)
337                 }
338                 utils.log("DEBUG", "***** Exit processRollbackJavaException *****", isDebugEnabled)
339         }
340
341 }