Dynamic Cloud Owner Support
[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.AaiUtil
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.bpmn.common.scripts.MsoUtils
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.client.aai.AAIObjectType
34 import org.onap.so.client.aai.AAIResourcesClient
35 import org.onap.so.client.aai.entities.AAIResultWrapper
36 import org.onap.so.client.aai.entities.uri.AAIResourceUri
37 import org.onap.so.client.aai.entities.uri.AAIUriFactory
38 import org.onap.so.logger.MsoLogger
39 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
40 import groovy.json.*
41 import org.onap.so.bpmn.common.scripts.AaiUtil;
42
43 /**
44  * This groovy class supports the <class>DoUpdateE2EServiceInstanceRollback.bpmn</class> process.
45  *
46  * Inputs:
47  * @param - msoRequestId
48  * @param - rollbackData with
49  *          globalCustomerId
50  *                      subscriptionServiceType
51  *                      serviceInstanceId
52  *                      disableRollback
53  *                      rollbackAAI
54  *                      rollbackAdded
55  *                      rollbackDeleted
56  *
57  *
58  * Outputs:
59  * @param - rollbackError
60  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
61  *
62  */
63 public class DoUpdateE2EServiceInstanceRollback extends AbstractServiceTaskProcessor{
64         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoUpdateE2EServiceInstanceRollback.class);
65
66
67         String Prefix="DUPDSIRB_"
68         ExceptionUtil exceptionUtil = new ExceptionUtil()
69
70         public void preProcessRequest(DelegateExecution execution) {
71                 execution.setVariable("prefix",Prefix)
72                 String msg = ""
73                 msoLogger.trace("preProcessRequest ")
74                 execution.setVariable("rollbackAAI",false)
75                 execution.setVariable("rollbackAdded",false)
76                 execution.setVariable("rollbackDeleted",false)
77
78                 List addResourceList = execution.getVariable("addResourceList")
79         List delResourceList = execution.getVariable("delResourceList")
80         execution.setVariable("addResourceList_o",  addResourceList)
81         execution.setVariable("delResourceList_o",  delResourceList)
82         //exchange add and delete resource list
83         execution.setVariable("addResourceList",  delResourceList)
84         execution.setVariable("delResourceList",  addResourceList)
85
86                 try {
87                         def rollbackData = execution.getVariable("rollbackData")
88                         msoLogger.debug("RollbackData:" + rollbackData)
89
90                         if (rollbackData != null) {
91                                 if (rollbackData.hasType("SERVICEINSTANCE")) {
92
93                                         def serviceInstanceId = rollbackData.get("SERVICEINSTANCE", "serviceInstanceId")
94                                         execution.setVariable("serviceInstanceId", serviceInstanceId)
95
96                                         def subscriptionServiceType = rollbackData.get("SERVICEINSTANCE", "subscriptionServiceType")
97                                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)
98
99                                         def globalSubscriberId  = rollbackData.get("SERVICEINSTANCE", "globalSubscriberId")
100                                         execution.setVariable("globalSubscriberId", globalSubscriberId)
101
102                                         def rollbackAAI = rollbackData.get("SERVICEINSTANCE", "rollbackAAI")
103                                         if ("true".equals(rollbackAAI))
104                                         {
105                                                 execution.setVariable("rollbackAAI",true)
106                                         }
107
108                                         def rollbackAdded = rollbackData.get("SERVICEINSTANCE", "rollbackAdded")
109                                         if ("true".equals(rollbackAdded))
110                                         {
111                                                 execution.setVariable("rollbackAdded", true)
112                                         }
113
114                                         def rollbackDeleted = rollbackData.get("SERVICEINSTANCE", "rollbackDeleted")
115                                         if ("true".equals(rollbackDeleted))
116                                         {
117                                                 execution.setVariable("rollbackDeleted", true)
118                                         }
119
120                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackAdded") != true
121                                           && execution.getVariable("rollbackDeleted") != true)
122                                         {
123                                                 execution.setVariable("skipRollback", true)
124                                         }
125
126                                 }
127                                 else {
128                                         execution.setVariable("skipRollback", true)
129                                 }
130                         }
131                         else {
132                                 execution.setVariable("skipRollback", true)
133                         }
134                         if (execution.getVariable("disableRollback").equals("true" ))
135                         {
136                                 execution.setVariable("skipRollback", true)
137                         }
138
139                 } catch (BpmnError e) {
140                         throw e;
141                 } catch (Exception ex){
142                         msg = "Exception in Update ServiceInstance Rollback preProcessRequest " + ex.getMessage()
143                         msoLogger.debug(msg)
144                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
145                 }
146                 msoLogger.trace("Exit preProcessRequest ")
147         }
148
149         public void postProcessRequest(DelegateExecution execution) {
150                 msoLogger.trace("postProcessRequest ")
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                                         msoLogger.debug("Error deleting service-instance in AAI for rollback", + serviceInstanceId)
175                                 }
176                         }
177                         msoLogger.trace("Exit postProcessRequest ")
178
179                 } catch (BpmnError e) {
180                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + e.getMessage()
181                         msoLogger.debug(msg)
182                 } catch (Exception ex) {
183                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + ex.getMessage()
184                         msoLogger.debug(msg)
185                 }
186         }
187
188
189     public void preProcessForAddResource(DelegateExecution execution) {
190     }
191
192     public void postProcessForAddResource(DelegateExecution execution) {
193     }
194
195     public void preProcessForDeleteResource(DelegateExecution execution) {
196     }
197
198     public void postProcessForDeleteResource(DelegateExecution execution) {
199     }
200
201         public void preProcessAAIPUT(DelegateExecution execution) {
202                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
203                 msoLogger.info("Entered " + method)
204                 String msg = ""
205                 msoLogger.trace("preProcessAAIPUT ")
206
207                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion_n")
208 //              execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)
209
210                 //requestDetails.modelInfo.for AAI PUT servieInstanceData
211                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData
212                 String serviceInstanceName = execution.getVariable("serviceInstanceName")
213                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
214                 //aai serviceType and Role can be setted as fixed value now.
215                 String aaiServiceType = "E2E Service"
216                 String aaiServiceRole = "E2E Service"
217                 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
218                 String modelUuid = execution.getVariable("model-version-id-original")
219
220                 org.onap.aai.domain.yang.ServiceInstance si = new org.onap.aai.domain.yang.ServiceInstance()
221                 si.setServiceInstanceId(serviceInstanceId)
222                 si.setServiceInstanceName(serviceInstanceName)
223                 si.setServiceType(aaiServiceType)
224                 si.setServiceRole(aaiServiceRole)
225                 si.setModelInvariantId(modelInvariantUuid)
226                 si.setModelVersionId(modelUuid)
227
228                 execution.setVariable("serviceInstanceData", si)
229
230                 msoLogger.info("Exited " + method)
231         }
232
233         public void updateServiceInstance(DelegateExecution execution) {
234                 msoLogger.trace("updateServiceInstance ")
235                 String msg = ""
236                 try {
237                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
238                         org.onap.aai.domain.yang.ServiceInstance si = execution.getVariable("serviceInstanceData")
239
240                         AAIResourcesClient client = new AAIResourcesClient()
241                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
242                         client.update(uri, si)
243
244                 } catch (BpmnError e) {
245                         throw e;
246                 } catch (Exception ex) {
247                         msg = "Exception in DoCreateServiceInstance.updateServiceInstance. " + ex.getMessage()
248                         msoLogger.info(msg)
249                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
250                 }
251                 msoLogger.trace("Exit updateServiceInstance ")
252         }
253
254         public void processRollbackException(DelegateExecution execution){
255                 msoLogger.trace("processRollbackException ")
256                 try{
257                         msoLogger.debug("Caught an Exception in DoUpdateE2EServiceInstanceRollback")
258                         execution.setVariable("rollbackData", null)
259                         execution.setVariable("rollbackError", "Caught exception in ServiceInstance Update Rollback")
260                         execution.setVariable("WorkflowException", null)
261
262                 }catch(BpmnError b){
263                         msoLogger.debug("BPMN Error during processRollbackExceptions Method: ")
264                 }catch(Exception e){
265                         msoLogger.debug("Caught Exception during processRollbackExceptions Method: " + e.getMessage())
266                 }
267
268                 msoLogger.debug(" Exit processRollbackException")
269         }
270
271         public void processRollbackJavaException(DelegateExecution execution){
272                 msoLogger.trace("processRollbackJavaException ")
273                 try{
274                         execution.setVariable("rollbackData", null)
275                         execution.setVariable("rollbackError", "Caught Java exception in ServiceInstance Update Rollback")
276                         msoLogger.debug("Caught Exception in processRollbackJavaException")
277
278                 }catch(Exception e){
279                         msoLogger.debug("Caught Exception in processRollbackJavaException " + e.getMessage())
280                 }
281                 msoLogger.trace("Exit processRollbackJavaException ")
282         }
283
284 }