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