Containerization feature of SO
[so.git] / bpmn / so-bpmn-infrastructure-flows / src / main / groovy / org / onap / so / bpmn / infrastructure / scripts / DoCreateE2EServiceInstanceRollback.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.so.bpmn.infrastructure.scripts
23
24
25 import static org.apache.commons.lang3.StringUtils.*;
26
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.onap.so.rest.APIResponse;
36 import org.onap.so.rest.RESTClient
37 import org.onap.so.rest.RESTConfig
38 import org.onap.so.logger.MsoLogger
39 import org.onap.so.logger.MessageEnum
40
41 import org.onap.so.client.aai.entities.uri.AAIResourceUri
42 import org.onap.so.client.aai.entities.uri.AAIUriFactory
43 import org.onap.so.client.aai.AAIObjectType
44 import org.onap.so.client.aai.AAIResourcesClient
45
46 import java.util.UUID;
47
48 import org.camunda.bpm.engine.delegate.BpmnError
49 import org.camunda.bpm.engine.delegate.DelegateExecution
50 import org.json.JSONObject;
51 import org.apache.commons.lang3.*
52 import org.apache.commons.codec.binary.Base64;
53 import org.springframework.web.util.UriUtils;
54 /**
55  * This groovy class supports the <class>DoCreateServiceInstanceRollback.bpmn</class> process.
56  *
57  * Inputs:
58  * @param - msoRequestId
59  * @param - rollbackData with
60  *          globalCustomerId
61  *                      subscriptionServiceType
62  *                      serviceInstanceId
63  *                      disableRollback
64  *                      rollbackAAI
65  *                      rollbackSDNC
66  *                      sdncRollbackRequest
67  *
68  *
69  * Outputs:
70  * @param - rollbackError
71  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
72  *
73  */
74 public class DoCreateE2EServiceInstanceRollback extends AbstractServiceTaskProcessor{
75
76         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateE2EServiceInstanceRollback.class);
77         String Prefix="DCRESIRB_"
78
79         public void preProcessRequest(DelegateExecution execution) {
80                 def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
81                 execution.setVariable("prefix",Prefix)
82                 String msg = ""
83                 msoLogger.trace("Start preProcessRequest")
84                 execution.setVariable("rollbackAAI",false)
85                 execution.setVariable("rollbackSDNC",false)
86
87                 try {
88                         def rollbackData = execution.getVariable("rollbackData")
89                         msoLogger.debug("RollbackData:" + rollbackData)
90
91                         if (rollbackData != null) {
92                                 if (rollbackData.hasType("SERVICEINSTANCE")) {
93
94                                         def serviceInstanceId = rollbackData.get("SERVICEINSTANCE", "serviceInstanceId")
95                                         execution.setVariable("serviceInstanceId", serviceInstanceId)
96
97                                         def subscriptionServiceType = rollbackData.get("SERVICEINSTANCE", "subscriptionServiceType")
98                                         execution.setVariable("subscriptionServiceType", subscriptionServiceType)
99
100                                         def globalSubscriberId  = rollbackData.get("SERVICEINSTANCE", "globalSubscriberId")
101                                         execution.setVariable("globalSubscriberId", globalSubscriberId)
102
103                                         def rollbackAAI = rollbackData.get("SERVICEINSTANCE", "rollbackAAI")
104                                         if ("true".equals(rollbackAAI))
105                                         {
106                                                 execution.setVariable("rollbackAAI",true)
107                                         }
108
109                                         def rollbackSDNC = rollbackData.get("SERVICEINSTANCE", "rollbackSDNC")
110                                         if ("true".equals(rollbackSDNC))
111                                         {
112                                                 execution.setVariable("rollbackSDNC", true)
113                                         }
114
115                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackSDNC") != true)
116                                         {
117                                                 execution.setVariable("skipRollback", true)
118                                         }
119
120                                         def sdncDelete = rollbackData.get("SERVICEINSTANCE", "sdncDelete")
121                                         execution.setVariable("sdncDelete", sdncDelete)
122                                         def sdncDeactivate = rollbackData.get("SERVICEINSTANCE", "sdncDeactivate")
123                                         execution.setVariable("sdncDeactivate", sdncDeactivate)
124                                         msoLogger.debug("sdncDeactivate:\n" + sdncDeactivate)
125                                         msoLogger.debug("sdncDelete:\n" + sdncDelete)
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 Create ServiceInstance Rollback preProcessRequest " + ex.getMessage()
143                         msoLogger.debug(msg)
144                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg) //TODO how does this even compile without exceptionUtil being declared
145                 }
146                 msoLogger.trace("Exit preProcessRequest ")
147         }
148
149         public void validateSDNCResponse(DelegateExecution execution, String response, String method) {
150
151                 msoLogger.trace("validateSDNCResponse")
152                 String msg = ""
153                 try {
154                         WorkflowException workflowException = execution.getVariable("WorkflowException")
155                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
156                         msoLogger.debug("SDNCResponse: " + response)
157                         msoLogger.debug("workflowException: " + workflowException)
158
159                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
160                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
161
162                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
163                                 msg = "SDNC Adapter service-instance rollback successful for " + method
164                                 msoLogger.debug(msg)
165                         }else{
166                                 execution.setVariable("rolledBack", false)
167                                 msg =  "Error Response from SDNC Adapter service-instance rollback for " + method
168                                 execution.setVariable("rollbackError", msg)
169                                 msoLogger.debug(msg)
170                                 throw new BpmnError("MSOWorkflowException")
171                         }
172                 } catch (BpmnError e) {
173                         throw e;
174                 } catch (Exception ex){
175                         msg = "Exception in Create ServiceInstance rollback for "  + method  + " Exception:" + ex.getMessage()
176                         msoLogger.debug(msg)
177                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
178                 }
179                 msoLogger.trace("Exit validateSDNCResponse ")
180         }
181
182         /**
183          * Deletes the service instance in aai
184          */
185         public void deleteServiceInstance(DelegateExecution execution) {
186                 msoLogger.trace("Started Delete Service Instance")
187                 try {
188                         String globalCustId = execution.getVariable("globalSubscriberId")
189                         String serviceType = execution.getVariable("subscriptionServiceType")
190                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
191
192                         AAIResourcesClient resourceClient = new AAIResourcesClient();
193                         AAIResourceUri serviceInstanceUri = AAIUriFactory.createResourceUri(AAIObjectType.SERVICE_INSTANCE, globalCustId, serviceType, serviceInstanceId)
194                         resourceClient.delete(serviceInstanceUri)
195
196                         msoLogger.trace("Completed Delete Service Instance")
197                 }catch(Exception e){
198                         msoLogger.debug("Error occured within deleteServiceInstance method: " + e)
199                 }
200         }
201
202         public void postProcessRequest(DelegateExecution execution) {
203
204                 msoLogger.trace("postProcessRequest")
205                 String msg = ""
206                 try {
207                         execution.setVariable("rollbackData", null)
208                         String serviceInstanceId = execution.getVariable("serviceInstanceId")
209                         boolean rollbackAAI = execution.getVariable("rollbackAAI")
210                         boolean rollbackSDNC = execution.getVariable("rollbackSDNC")
211                         if (rollbackAAI || rollbackSDNC)
212                         {
213                                 execution.setVariable("rolledBack", true)
214                         }
215                         if (rollbackAAI)
216                         {
217                                 boolean succInAAI = execution.getVariable("GENDS_SuccessIndicator")
218                                 if(!succInAAI){
219                                         execution.setVariable("rolledBack", false) //both sdnc and aai must be successful to declare rollback Succesful
220                                         execution.setVariable("rollbackError", "Error deleting service-instance in AAI for rollback")
221                                         msoLogger.debug("Error deleting service-instance in AAI for rollback", + serviceInstanceId)
222                                 }
223                         }
224                         msoLogger.trace("Exit postProcessRequest ")
225
226                 } catch (BpmnError e) {
227                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + e.getMessage()
228                         msoLogger.debug(msg)
229                 } catch (Exception ex) {
230                         msg = "Exception in Create ServiceInstance Rollback postProcessRequest. " + ex.getMessage()
231                         msoLogger.debug(msg)
232                 }
233
234         }
235
236         public void processRollbackException(DelegateExecution execution){
237
238                 msoLogger.trace("processRollbackException")
239                 try{
240                         msoLogger.debug("Caught an Exception in DoCreateServiceInstanceRollback")
241                         execution.setVariable("rollbackData", null)
242                         execution.setVariable("rollbackError", "Caught exception in ServiceInstance Create Rollback")
243                         execution.setVariable("WorkflowException", null)
244
245                 }catch(BpmnError b){
246                         msoLogger.debug("BPMN Error during processRollbackExceptions Method: ")
247                 }catch(Exception e){
248                         msoLogger.debug("Caught Exception during processRollbackExceptions Method: " + e.getMessage())
249                 }
250
251                 msoLogger.trace(" Exit processRollbackException")
252         }
253
254         public void processRollbackJavaException(DelegateExecution execution){
255
256                 msoLogger.trace("processRollbackJavaException")
257                 try{
258                         execution.setVariable("rollbackData", null)
259                         execution.setVariable("rollbackError", "Caught Java exception in ServiceInstance Create Rollback")
260                         msoLogger.debug("Caught Exception in processRollbackJavaException")
261
262                 }catch(Exception e){
263                         msoLogger.debug("Caught Exception in processRollbackJavaException " + e.getMessage())
264                 }
265                 msoLogger.trace("Exit processRollbackJavaException")
266         }
267
268 }