Merge "add instance group support to SO"
[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  * Modifications Copyright (c) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.so.bpmn.infrastructure.scripts
23
24
25 import static org.apache.commons.lang3.StringUtils.*;
26
27 import org.apache.commons.lang3.*
28 import org.camunda.bpm.engine.delegate.BpmnError
29 import org.camunda.bpm.engine.delegate.DelegateExecution
30 import org.onap.so.bpmn.common.scripts.AaiUtil
31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.onap.so.bpmn.common.scripts.ExceptionUtil
33 import org.onap.so.bpmn.common.scripts.MsoUtils
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.onap.so.client.aai.AAIObjectType
36 import org.onap.so.client.aai.AAIResourcesClient
37 import org.onap.so.client.aai.entities.AAIResultWrapper
38 import org.onap.so.client.aai.entities.uri.AAIResourceUri
39 import org.onap.so.client.aai.entities.uri.AAIUriFactory
40 import org.slf4j.Logger
41 import org.slf4j.LoggerFactory
42 import org.onap.so.bpmn.common.scripts.ExceptionUtil;
43 import groovy.json.*
44 import org.onap.so.bpmn.common.scripts.AaiUtil;
45
46 /**
47  * This groovy class supports the <class>DoUpdateE2EServiceInstanceRollback.bpmn</class> process.
48  *
49  * Inputs:
50  * @param - msoRequestId
51  * @param - rollbackData with
52  *          globalCustomerId
53  *                      subscriptionServiceType
54  *                      serviceInstanceId
55  *                      disableRollback
56  *                      rollbackAAI
57  *                      rollbackAdded
58  *                      rollbackDeleted
59  *
60  *
61  * Outputs:
62  * @param - rollbackError
63  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
64  *
65  */
66 public class DoUpdateE2EServiceInstanceRollback extends AbstractServiceTaskProcessor{
67     private static final Logger logger = LoggerFactory.getLogger( DoUpdateE2EServiceInstanceRollback.class);
68
69
70         String Prefix="DUPDSIRB_"
71         ExceptionUtil exceptionUtil = new ExceptionUtil()
72
73         public void preProcessRequest(DelegateExecution execution) {
74                 execution.setVariable("prefix",Prefix)
75                 String msg = ""
76                 logger.trace("preProcessRequest ")
77                 execution.setVariable("rollbackAAI",false)
78                 execution.setVariable("rollbackAdded",false)
79                 execution.setVariable("rollbackDeleted",false)
80
81                 List addResourceList = execution.getVariable("addResourceList")
82         List delResourceList = execution.getVariable("delResourceList")
83         execution.setVariable("addResourceList_o",  addResourceList)
84         execution.setVariable("delResourceList_o",  delResourceList)
85         //exchange add and delete resource list
86         execution.setVariable("addResourceList",  delResourceList)
87         execution.setVariable("delResourceList",  addResourceList)
88
89                 try {
90                         def rollbackData = execution.getVariable("rollbackData")
91                         logger.debug("RollbackData:" + rollbackData)
92
93                         if (rollbackData != null) {
94                                 if (rollbackData.hasType("SERVICEINSTANCE")) {
95
96                                         def serviceInstanceId = rollbackData.get("SERVICEINSTANCE", "serviceInstanceId")
97                                         execution.setVariable("serviceInstanceId", serviceInstanceId)
98
99                                         def subscriptionServiceType = rollbackData.get("SERVICEINSTANCE", "subscriptionServiceType")
100                                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)
101
102                                         def globalSubscriberId  = rollbackData.get("SERVICEINSTANCE", "globalSubscriberId")
103                                         execution.setVariable("globalSubscriberId", globalSubscriberId)
104
105                                         def rollbackAAI = rollbackData.get("SERVICEINSTANCE", "rollbackAAI")
106                                         if ("true".equals(rollbackAAI))
107                                         {
108                                                 execution.setVariable("rollbackAAI",true)
109                                         }
110
111                                         def rollbackAdded = rollbackData.get("SERVICEINSTANCE", "rollbackAdded")
112                                         if ("true".equals(rollbackAdded))
113                                         {
114                                                 execution.setVariable("rollbackAdded", true)
115                                         }
116
117                                         def rollbackDeleted = rollbackData.get("SERVICEINSTANCE", "rollbackDeleted")
118                                         if ("true".equals(rollbackDeleted))
119                                         {
120                                                 execution.setVariable("rollbackDeleted", true)
121                                         }
122
123                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackAdded") != true
124                                           && execution.getVariable("rollbackDeleted") != true)
125                                         {
126                                                 execution.setVariable("skipRollback", true)
127                                         }
128
129                                 }
130                                 else {
131                                         execution.setVariable("skipRollback", true)
132                                 }
133                         }
134                         else {
135                                 execution.setVariable("skipRollback", true)
136                         }
137                         if (execution.getVariable("disableRollback").equals("true" ))
138                         {
139                                 execution.setVariable("skipRollback", true)
140                         }
141
142                 } catch (BpmnError e) {
143                         throw e;
144                 } catch (Exception ex){
145                         msg = "Exception in Update ServiceInstance Rollback preProcessRequest " + ex.getMessage()
146                         logger.debug(msg)
147                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
148                 }
149                 logger.trace("Exit preProcessRequest ")
150         }
151
152         public void postProcessRequest(DelegateExecution execution) {
153                 logger.trace("postProcessRequest ")
154                 String msg = ""
155                 try {
156                         execution.setVariable("rollbackData", null)
157                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
158                         boolean rollbackAAI = execution.getVariable("rollbackAAI")
159                         boolean rollbackAdded = execution.getVariable("rollbackAdded")
160                         boolean rollbackDeleted = execution.getVariable("rollbackDeleted")
161
162                         List addResourceList = execution.getVariable("addResourceList_o")
163                         List delResourceList = execution.getVariable("delResourceList_o")
164                         execution.setVariable("addResourceList",  addResourceList)
165                         execution.setVariable("delResourceList",  delResourceList)
166
167                         if (rollbackAAI || rollbackAdded || rollbackDeleted)
168                         {
169                                 execution.setVariable("rolledBack", true)
170                         }
171                         if (rollbackAAI)
172                         {
173                                 boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")
174                                 if(!succInAAI){
175                                         execution.setVariable("rolledBack", false) //both sdnc and aai must be successful to declare rollback Succesful
176                                         execution.setVariable("rollbackError", "Error deleting service-instance in AAI for rollback")
177                                         logger.debug("Error deleting service-instance in AAI for rollback", + serviceInstanceId)
178                                 }
179                         }
180                         logger.trace("Exit postProcessRequest ")
181
182                 } catch (BpmnError e) {
183                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + e.getMessage()
184                         logger.debug(msg)
185                 } catch (Exception ex) {
186                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + ex.getMessage()
187                         logger.debug(msg)
188                 }
189         }
190
191
192     public void preProcessForAddResource(DelegateExecution execution) {
193     }
194
195     public void postProcessForAddResource(DelegateExecution execution) {
196     }
197
198     public void preProcessForDeleteResource(DelegateExecution execution) {
199     }
200
201     public void postProcessForDeleteResource(DelegateExecution execution) {
202     }
203
204         public void preProcessAAIPUT(DelegateExecution execution) {
205                 def method = getClass().getSimpleName() + '.preProcessRequest(' +'execution=' + execution.getId() +')'
206                 logger.info("Entered " + method)
207                 String msg = ""
208                 logger.trace("preProcessAAIPUT ")
209
210                 String serviceInstanceVersion = execution.getVariable("serviceInstanceVersion_n")
211 //              execution.setVariable("GENPS_serviceResourceVersion", serviceInstanceVersion)
212
213                 //requestDetails.modelInfo.for AAI PUT servieInstanceData
214                 //requestDetails.requestInfo. for AAI GET/PUT serviceInstanceData
215                 String serviceInstanceName = execution.getVariable("serviceInstanceName")
216                 String serviceInstanceId = execution.getVariable("serviceInstanceId")
217                 //aai serviceType and Role can be setted as fixed value now.
218                 String aaiServiceType = "E2E Service"
219                 String aaiServiceRole = "E2E Service"
220                 String modelInvariantUuid = execution.getVariable("modelInvariantUuid")
221                 String modelUuid = execution.getVariable("model-version-id-original")
222
223                 org.onap.aai.domain.yang.ServiceInstance si = new org.onap.aai.domain.yang.ServiceInstance()
224                 si.setServiceInstanceId(serviceInstanceId)
225                 si.setServiceInstanceName(serviceInstanceName)
226                 si.setServiceType(aaiServiceType)
227                 si.setServiceRole(aaiServiceRole)
228                 si.setModelInvariantId(modelInvariantUuid)
229                 si.setModelVersionId(modelUuid)
230
231                 execution.setVariable("serviceInstanceData", si)
232
233                 logger.info("Exited " + method)
234         }
235
236         public void updateServiceInstance(DelegateExecution execution) {
237                 logger.trace("updateServiceInstance ")
238                 String msg = ""
239                 try {
240                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
241                         org.onap.aai.domain.yang.ServiceInstance si = execution.getVariable("serviceInstanceData")
242
243                         AAIResourcesClient client = new AAIResourcesClient()
244                         AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, serviceInstanceId)
245                         client.update(uri, si)
246
247                 } catch (BpmnError e) {
248                         throw e;
249                 } catch (Exception ex) {
250                         msg = "Exception in DoCreateServiceInstance.updateServiceInstance. " + ex.getMessage()
251                         logger.info(msg)
252                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
253                 }
254                 logger.trace("Exit updateServiceInstance ")
255         }
256
257         public void processRollbackException(DelegateExecution execution){
258                 logger.trace("processRollbackException ")
259                 try{
260                         logger.debug("Caught an Exception in DoUpdateE2EServiceInstanceRollback")
261                         execution.setVariable("rollbackData", null)
262                         execution.setVariable("rollbackError", "Caught exception in ServiceInstance Update Rollback")
263                         execution.setVariable("WorkflowException", null)
264
265                 }catch(BpmnError b){
266                         logger.debug("BPMN Error during processRollbackExceptions Method: ")
267                 }catch(Exception e){
268                         logger.debug("Caught Exception during processRollbackExceptions Method: " + e.getMessage())
269                 }
270
271                 logger.debug(" Exit processRollbackException")
272         }
273
274         public void processRollbackJavaException(DelegateExecution execution){
275                 logger.trace("processRollbackJavaException ")
276                 try{
277                         execution.setVariable("rollbackData", null)
278                         execution.setVariable("rollbackError", "Caught Java exception in ServiceInstance Update Rollback")
279                         logger.debug("Caught Exception in processRollbackJavaException")
280
281                 }catch(Exception e){
282                         logger.debug("Caught Exception in processRollbackJavaException " + e.getMessage())
283                 }
284                 logger.trace("Exit processRollbackJavaException ")
285         }
286
287 }