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