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