Merge "Added post process to AAI in HandlePNF" into dublin
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateServiceInstanceRollback.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. 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
23 package org.onap.so.bpmn.infrastructure.scripts
24
25
26 import static org.apache.commons.lang3.StringUtils.*;
27 import groovy.xml.XmlUtil
28 import groovy.json.*
29
30 import org.onap.so.bpmn.core.json.JsonUtils
31 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
32 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
33 import org.onap.so.bpmn.core.RollbackData
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.slf4j.Logger
36 import org.slf4j.LoggerFactory
37
38 import java.util.UUID;
39
40 import org.camunda.bpm.engine.delegate.BpmnError
41 import org.camunda.bpm.engine.delegate.DelegateExecution
42 import org.json.JSONObject;
43 import org.apache.commons.lang3.*
44 import org.apache.commons.codec.binary.Base64;
45 import org.springframework.web.util.UriUtils;
46 /**
47  * This groovy class supports the <class>DoCreateServiceInstanceRollback.bpmn</class> process.
48  *
49  * Inputs:
50  * @param - msoRequestId
51  * @param - rollbackData with
52  *          globalCustomerId
53  *                      subscriptionServiceType
54  *                      serviceInstanceId
55  *                      disableRollback
56  *                      rollbackAAI
57  *                      rollbackSDNC
58  *                      sdncRollbackRequest
59  *
60  *
61  * Outputs:
62  * @param - rollbackError
63  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
64  *
65  */
66 public class DoCreateServiceInstanceRollback extends AbstractServiceTaskProcessor{
67     private static final Logger logger = LoggerFactory.getLogger( DoCreateServiceInstanceRollback.class);
68
69
70         String Prefix="DCRESIRB_"
71
72         public void preProcessRequest(DelegateExecution execution) {
73                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
74                 execution.setVariable("prefix",Prefix)
75                 String msg = ""
76                 logger.trace("preProcessRequest")
77                 execution.setVariable("rollbackAAI",false)
78                 execution.setVariable("rollbackSDNC",false)
79
80                 try {
81                         def rollbackData = execution.getVariable("rollbackData")
82                         logger.debug("RollbackData:" + rollbackData)
83
84                         if (rollbackData != null) {
85                                 if (rollbackData.hasType("SERVICEINSTANCE")) {
86
87                                         def serviceInstanceId = rollbackData.get("SERVICEINSTANCE", "serviceInstanceId")
88                                         execution.setVariable("serviceInstanceId", serviceInstanceId)
89
90                                         def subscriptionServiceType = rollbackData.get("SERVICEINSTANCE", "subscriptionServiceType")
91                                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)
92
93                                         def globalSubscriberId  = rollbackData.get("SERVICEINSTANCE", "globalSubscriberId")
94                                         execution.setVariable("globalSubscriberId", globalSubscriberId)
95
96                                         def rollbackAAI = rollbackData.get("SERVICEINSTANCE", "rollbackAAI")
97                                         if ("true".equals(rollbackAAI))
98                                         {
99                                                 execution.setVariable("rollbackAAI",true)
100                                         }
101
102                                         def rollbackSDNC = rollbackData.get("SERVICEINSTANCE", "rollbackSDNC")
103                                         if ("true".equals(rollbackSDNC))
104                                         {
105                                                 execution.setVariable("rollbackSDNC", true)
106                                         }
107
108                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackSDNC") != true)
109                                         {
110                                                 execution.setVariable("skipRollback", true)
111                                         }
112
113                                         def sdncDelete = rollbackData.get("SERVICEINSTANCE", "sdncDelete")
114                                         execution.setVariable("sdncDelete", sdncDelete)
115                                         def sdncDeactivate = rollbackData.get("SERVICEINSTANCE", "sdncDeactivate")
116                                         execution.setVariable("sdncDeactivate", sdncDeactivate)
117                                         logger.debug("sdncDeactivate:\n" + sdncDeactivate)
118                                         logger.debug("sdncDelete:\n" + sdncDelete)
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 Create ServiceInstance Rollback preProcessRequest " + ex.getMessage()
136                         logger.debug(msg)
137                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
138                 }
139                 logger.trace("Exit preProcessRequest")
140         }
141
142         public void validateSDNCResponse(DelegateExecution execution, String response, String method) {
143
144                 logger.trace("validateSDNCResponse")
145                 String msg = ""
146                 try {
147                         WorkflowException workflowException = execution.getVariable("WorkflowException")
148                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
149                         logger.debug("SDNCResponse: " + response)
150                         logger.debug("workflowException: " + workflowException)
151
152                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
153                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
154
155                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
156                                 msg = "SDNC Adapter service-instance rollback successful for " + method
157                                 logger.debug(msg)
158                         }else{
159                                 execution.setVariable("rolledBack", false)
160                                 msg =  "Error Response from SDNC Adapter service-instance rollback for " + method
161                                 execution.setVariable("rollbackError", msg)
162                                 logger.debug(msg)
163                                 throw new BpmnError("MSOWorkflowException")
164                         }
165                 } catch (BpmnError e) {
166                         throw e;
167                 } catch (Exception ex){
168                         msg = "Exception in Create ServiceInstance rollback for "  + method  + " Exception:" + ex.getMessage()
169                         logger.debug(msg)
170                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
171                 }
172                 logger.trace("Exit validateSDNCResponse")
173         }
174
175         public void postProcessRequest(DelegateExecution execution) {
176
177                 logger.trace("postProcessRequest")
178                 String msg = ""
179                 try {
180                         execution.setVariable("rollbackData", null)
181                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
182                         boolean rollbackAAI = execution.getVariable("rollbackAAI")
183                         boolean rollbackSDNC = execution.getVariable("rollbackSDNC")
184                         if (rollbackAAI || rollbackSDNC)
185                         {
186                                 execution.setVariable("rolledBack", true)
187                         }
188                         if (rollbackAAI)
189                         {
190                                 boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")
191                                 if(!succInAAI){
192                                         execution.setVariable("rolledBack", false) //both sdnc and aai must be successful to declare rollback Succesful
193                                         execution.setVariable("rollbackError", "Error deleting service-instance in AAI for rollback")
194                                         logger.debug("Error deleting service-instance in AAI for rollback", + serviceInstanceId)
195                                 }
196                         }
197                         logger.trace("Exit postProcessRequest")
198
199                 } catch (BpmnError e) {
200                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + e.getMessage()
201                         logger.debug(msg)
202                 } catch (Exception ex) {
203                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + ex.getMessage()
204                         logger.debug(msg)
205                 }
206
207         }
208
209         public void processRollbackException(DelegateExecution execution){
210
211                 logger.trace("processRollbackException")
212                 try{
213                         logger.debug("Caught an Exception in DoCreateServiceInstanceRollback")
214                         execution.setVariable("rollbackData", null)
215                         execution.setVariable("rollbackError", "Caught exception in ServiceInstance Create Rollback")
216                         execution.setVariable("WorkflowException", null)
217
218                 }catch(BpmnError b){
219                         logger.debug("BPMN Error during processRollbackExceptions Method: ")
220                 }catch(Exception e){
221                         logger.debug("Caught Exception during processRollbackExceptions Method: " + e.getMessage())
222                 }
223
224                 logger.debug("Exit processRollbackException")
225         }
226
227         public void processRollbackJavaException(DelegateExecution execution){
228
229                 logger.trace("processRollbackJavaException")
230                 try{
231                         execution.setVariable("rollbackData", null)
232                         execution.setVariable("rollbackError", "Caught Java exception in ServiceInstance Create Rollback")
233                         logger.debug("Caught Exception in processRollbackJavaException")
234
235                 }catch(Exception e){
236                         logger.debug("Caught Exception in processRollbackJavaException " + e.getMessage())
237                 }
238                 logger.trace("Exit processRollbackJavaException")
239         }
240
241 }