257142e8bdc19cc2dfe8c587f7d1afe03cd53364
[so.git] / bpmn / so-bpmn-infrastructure-common / 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 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
33 import groovy.json.*
34 import org.onap.so.bpmn.common.scripts.AaiUtil;
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 preProcessAAIPUT(DelegateExecution execution) {
195                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
196                 msoLogger.info("Entered " + method)
197                 String msg = ""
198                 msoLogger.trace("preProcessAAIPUT ")
199
200                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion_n")
201 //              execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)
202
203                 //requestDetails.modelInfo.for AAI PUT servieInstanceData
204                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData
205                 String serviceInstanceName = execution.getVariable("serviceInstanceName")
206                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
207                 //aai serviceType and Role can be setted as fixed value now.
208                 String aaiServiceType = "E2E Service"
209                 String aaiServiceRole = "E2E Service"
210                 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
211                 String modelUuid = execution.getVariable("model-version-id-original")
212
213                 //AAI PUT
214                 AaiUtil aaiUriUtil = new AaiUtil(this)
215                 utils.log("INFO","start create aai uri: " + aaiUriUtil, isDebugEnabled)
216                 String aai_uri = aaiUriUtil.getBusinessCustomerUri(execution)
217                 utils.log("INFO","aai_uri: " + aai_uri, isDebugEnabled)
218                 String namespace = aaiUriUtil.getNamespaceFromUri(aai_uri)
219                 utils.log("INFO","namespace: " + namespace, isDebugEnabled)
220
221                 String serviceInstanceData =
222                                 """<service-instance xmlns=\"${namespace}\">
223                     <service-instance-id>${MsoUtils.xmlEscape(serviceInstanceId)}</service-instance-id>
224                     <service-instance-name>${MsoUtils.xmlEscape(serviceInstanceName)}</service-instance-name>
225                     <service-type>${MsoUtils.xmlEscape(aaiServiceType)}</service-type>
226                     <service-role>${MsoUtils.xmlEscape(aaiServiceRole)}</service-role>
227                     <resource-version>${MsoUtils.xmlEscape(serviceInstanceVersion)}</resource-version>
228                     <model-invariant-id>${MsoUtils.xmlEscape(modelInvariantUuid)}</model-invariant-id>
229                     <model-version-id>${MsoUtils.xmlEscape(modelUuid)}</model-version-id>
230                                  </service-instance>""".trim()
231
232                 execution.setVariable("serviceInstanceData", serviceInstanceData)
233                 msoLogger.info("serviceInstanceData: " + serviceInstanceData)
234                 msoLogger.debug(serviceInstanceData)
235                 msoLogger.info(" aai_uri " + aai_uri + " namespace:" + namespace)
236                 msoLogger.info(" 'payload' to update Service Instance in AAI - " + "\n" + serviceInstanceData)
237
238                 msoLogger.info("Exited " + method)
239         }
240
241         public void postProcessAAIPUT(DelegateExecution execution) {
242                 msoLogger.trace("postProcessAAIPUT ")
243                 String msg = ""
244                 try {
245                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
246                         boolean succInAAI = execution.getVariable("GENPS_SuccessIndicator")
247                         if(!succInAAI){
248                                 msoLogger.info("Error putting Service-instance in AAI", + serviceInstanceId)
249                                 WorkflowException workflowException = execution.getVariable("WorkflowException")
250                                 msoLogger.debug("workflowException: " + workflowException)
251                                 if(workflowException != null){
252                                         exceptionUtil.buildAndThrowWorkflowException(execution, workflowException.getErrorCode(), workflowException.getErrorMessage())
253                                 }
254                         }
255                         else
256                         {
257
258                         }
259
260                 } catch (BpmnError e) {
261                         throw e;
262                 } catch (Exception ex) {
263                         msg = "Exception in DoCreateServiceInstance.postProcessAAIDEL. " + ex.getMessage()
264                         msoLogger.info(msg)
265                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
266                 }
267                 msoLogger.trace("Exit postProcessAAIPUT ")
268         }
269
270         public void processRollbackException(DelegateExecution execution){
271                 msoLogger.trace("processRollbackException ")
272                 try{
273                         msoLogger.debug("Caught an Exception in DoUpdateE2EServiceInstanceRollback")
274                         execution.setVariable("rollbackData", null)
275                         execution.setVariable("rollbackError", "Caught exception in ServiceInstance Update Rollback")
276                         execution.setVariable("WorkflowException", null)
277
278                 }catch(BpmnError b){
279                         msoLogger.debug("BPMN Error during processRollbackExceptions Method: ")
280                 }catch(Exception e){
281                         msoLogger.debug("Caught Exception during processRollbackExceptions Method: " + e.getMessage())
282                 }
283
284                 msoLogger.debug(" Exit processRollbackException")
285         }
286
287         public void processRollbackJavaException(DelegateExecution execution){
288                 msoLogger.trace("processRollbackJavaException ")
289                 try{
290                         execution.setVariable("rollbackData", null)
291                         execution.setVariable("rollbackError", "Caught Java exception in ServiceInstance Update Rollback")
292                         msoLogger.debug("Caught Exception in processRollbackJavaException")
293
294                 }catch(Exception e){
295                         msoLogger.debug("Caught Exception in processRollbackJavaException " + e.getMessage())
296                 }
297                 msoLogger.trace("Exit processRollbackJavaException ")
298         }
299
300 }