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