24b919524f9368af644d523b7f0d6269565b0d59
[so.git] / bpmn / so-bpmn-infrastructure-common / src / main / groovy / org / onap / so / bpmn / vcpe / scripts / DoCreateAllottedResourceTXCRollback.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.vcpe.scripts;
22
23 import org.onap.so.bpmn.common.scripts.*;
24 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
25 import org.onap.so.bpmn.core.WorkflowException
26 import org.onap.so.bpmn.common.scripts.ExceptionUtil
27 import org.onap.so.bpmn.common.scripts.MsoUtils
28 import org.onap.so.bpmn.common.scripts.AaiUtil
29 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
30 import org.onap.so.rest.APIResponse
31
32 import java.util.UUID;
33 import org.camunda.bpm.engine.delegate.BpmnError
34 import org.camunda.bpm.engine.delegate.DelegateExecution
35 import org.apache.commons.lang3.*
36 import org.springframework.web.util.UriUtils;
37 import static org.apache.commons.lang3.StringUtils.*
38
39 import org.onap.so.logger.MessageEnum
40 import org.onap.so.logger.MsoLogger
41
42 /**
43  * This groovy class supports the <class>CreateAllottedResourceTXCRollback.bpmn</class> process.
44  *
45  * @author
46  * 
47  * Inputs:
48  * @param - msoRequestId
49  * @param - isDebugLogEnabled
50  * @param - disableRollback - O 
51  * @param - rollbackData
52  *
53  * Outputs:
54  * @param - rollbackError 
55  * @param - rolledBack (no localRB->null, localRB F->false, localRB S->true)
56  *
57  */
58 public class DoCreateAllottedResourceTXCRollback extends AbstractServiceTaskProcessor{
59         private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateAllottedResourceTXCRollback.class);
60
61         String Prefix="DCARTXCRB_"
62         ExceptionUtil exceptionUtil = new ExceptionUtil()
63
64         public void preProcessRequest (DelegateExecution execution) {
65                 
66
67                 String msg = ""
68                 msoLogger.trace("start preProcessRequest")
69                 execution.setVariable("prefix", Prefix)
70                 String rbType = "DCARTXC_"
71                 try {
72                         
73                         def rollbackData = execution.getVariable("rollbackData")
74                         msoLogger.debug("RollbackData:" + rollbackData)
75
76                         if (rollbackData != null) {
77                                 if (rollbackData.hasType(rbType)) {
78                                         
79                                         execution.setVariable("serviceInstanceId", rollbackData.get(rbType, "serviceInstanceId"))
80                                         execution.setVariable("parentServiceInstanceId", rollbackData.get(rbType, "parentServiceInstanceId"))
81                                         execution.setVariable("allottedResourceId", rollbackData.get("SERVICEINSTANCE", "allottedResourceId"))
82                                         
83                                         
84                                         def rollbackAAI = rollbackData.get(rbType, "rollbackAAI")
85                                         if ("true".equals(rollbackAAI))
86                                         {
87                                                 execution.setVariable("rollbackAAI",true)
88                                                 execution.setVariable("aaiARPath", rollbackData.get(rbType, "aaiARPath"))
89                                                 
90                                         }
91                                         def rollbackSDNC = rollbackData.get(rbType, "rollbackSDNCassign")
92                                         if ("true".equals(rollbackSDNC))
93                                         {
94                                                 execution.setVariable("rollbackSDNC", true)
95                                                 execution.setVariable("deactivateSdnc", rollbackData.get(rbType, "rollbackSDNCactivate"))
96                                                 execution.setVariable("deleteSdnc",  rollbackData.get(rbType, "rollbackSDNCcreate"))
97                                                 execution.setVariable("unassignSdnc", rollbackData.get(rbType, "rollbackSDNCassign"))
98                                                 
99                                                 msoLogger.debug("sdncDeactivate:\n" + execution.getVariable("deactivateSdnc") )
100                                                 msoLogger.debug("sdncDelete:\n" + execution.getVariable("deleteSdnc"))
101                                                 msoLogger.debug("sdncUnassign:\n" + execution.getVariable("unassignSdnc"))
102                                                 
103                                                 execution.setVariable("sdncDeactivateRequest", rollbackData.get(rbType, "sdncActivateRollbackReq"))
104                                                 execution.setVariable("sdncDeleteRequest",  rollbackData.get(rbType, "sdncCreateRollbackReq"))
105                                                 execution.setVariable("sdncUnassignRequest", rollbackData.get(rbType, "sdncAssignRollbackReq"))
106                                         }
107
108                                         if (execution.getVariable("rollbackAAI") != true && execution.getVariable("rollbackSDNC") != true)
109                                         {
110                                                 execution.setVariable("skipRollback", true)
111                                         }
112                                 }
113                                 else {
114                                         execution.setVariable("skipRollback", true)
115                                 }
116                         }
117                         else {
118                                 execution.setVariable("skipRollback", true)
119                         }
120                         if (execution.getVariable("disableRollback").equals("true" ))
121                         {
122                                 execution.setVariable("skipRollback", true)
123                         }
124                         
125                 }catch(BpmnError b){
126                         msoLogger.debug("Rethrowing MSOWorkflowException")
127                         throw b
128                 } catch (Exception ex){
129                         msg = "Exception in preProcessRequest " + ex.getMessage()
130                         msoLogger.debug(msg)
131                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
132                 }
133                 msoLogger.trace("end preProcessRequest")
134         }
135
136         // aaiARPath set during query (existing AR)
137         public void updateAaiAROrchStatus(DelegateExecution execution, String status){
138
139                 String msg = null;
140                 msoLogger.trace("start updateAaiAROrchStatus")
141                 AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
142                 String aaiARPath  = execution.getVariable("aaiARPath")
143                 msoLogger.debug(" aaiARPath:" + aaiARPath)
144                 String ar = null; //need this for getting resourceVersion for delete
145                 if (!isBlank(aaiARPath))
146                 {
147                         ar = arUtils.getARbyLink(execution, aaiARPath, "")
148                 }
149                 if (isBlank(ar))
150                 {
151                         msg = "AR not found in AAI at:" + aaiARPath
152                         msoLogger.debug(msg)
153                         exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
154                 }
155                 String orchStatus = arUtils.updateAROrchStatus(execution, status, aaiARPath)
156                 msoLogger.trace("end updateAaiAROrchStatus")
157         }
158
159         public void validateSDNCResp(DelegateExecution execution, String response, String method){
160
161
162                 msoLogger.trace("start ValidateSDNCResponse Process")
163                 String msg = ""
164
165                 try {
166                         WorkflowException workflowException = execution.getVariable("WorkflowException")
167                         msoLogger.debug("workflowException: " + workflowException)
168
169                         boolean successIndicator = execution.getVariable("SDNCA_SuccessIndicator")
170                         msoLogger.debug("SDNCResponse: " + response)
171
172                         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
173                         sdncAdapterUtils.validateSDNCResponse(execution, response, workflowException, successIndicator)
174
175                         if(execution.getVariable(Prefix + 'sdncResponseSuccess') == true){
176                                 msoLogger.debug("Received a Good Response from SDNC Adapter for " + method + " SDNC Call.  Response is: \n" + response)
177
178                         }else{
179
180                                 msoLogger.debug("Received a BAD Response from SDNC Adapter for " + method + " SDNC Call.")
181                                 throw new BpmnError("MSOWorkflowException")
182                         }
183                 } catch (BpmnError e) {
184                         if ("404".contentEquals(e.getErrorCode()))
185                         {
186                                 msg = "SDNC rollback " + method + " returned a 404. Proceding with rollback"
187                                 msoLogger.debug(msg)
188                         }
189                         else {
190                                 throw e;
191                         }
192                 } catch(Exception ex) {
193                         msg = "Exception in validateSDNCResp. " + ex.getMessage()
194                         msoLogger.debug(msg)
195                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
196                 }
197                 msoLogger.trace("Exit ValidateSDNCResp Process")
198         }
199
200         public void deleteAaiAR(DelegateExecution execution){
201
202                 try{
203                         msoLogger.trace("start deleteAaiAR")
204                         AllottedResourceUtils arUtils = new AllottedResourceUtils(this)
205                         String ar = null //need to get resource-version 
206                         String arLink = execution.getVariable("aaiARPath")
207                         if (!isBlank(arLink))
208                         {
209                                 ar = arUtils.getARbyLink(execution, arLink, "")
210                         }
211                         arUtils.deleteAR(execution, arLink + '?resource-version=' + UriUtils.encode(execution.getVariable("aaiARResourceVersion"),"UTF-8"))
212                 } catch (BpmnError e) {
213                         throw e;
214                 }catch(Exception ex){
215                         msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG, "Exception Occurred Processing preProcessSDNCGetRequest.", "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + ex);
216                         exceptionUtil.buildAndThrowWorkflowException(execution, 1002, "Error Occured during SDNC GET Method:\n" + ex.getMessage())
217                 }
218                 msoLogger.trace("end deleteAaiAR")
219         }
220         
221         public void postProcessRequest(DelegateExecution execution) {
222
223                 msoLogger.trace("start postProcessRequest")
224                 String msg = ""
225                 try {
226                         execution.setVariable("rollbackData", null)
227                         boolean skipRollback = execution.getVariable("skipRollback")
228                         if (skipRollback != true)
229                         {
230                                 execution.setVariable("rolledBack", true)
231                                 msoLogger.debug("rolledBack")
232                         }
233                         msoLogger.trace("end postProcessRequest")
234
235                 } catch (BpmnError e) {
236                         msoLogger.debug(msg)
237                 } catch (Exception ex) {
238                         msg = "Exception in postProcessRequest. " + ex.getMessage()
239                         msoLogger.debug(msg)
240                 }
241
242         }
243         
244         public void processRollbackException(DelegateExecution execution){
245
246                 msoLogger.trace("start processRollbackException")
247                 try{
248                         msoLogger.debug("Caught an Exception in DoCreateAllottedResourceRollback")
249                         execution.setVariable("rollbackData", null)
250                         execution.setVariable("rolledBack", false)
251                         execution.setVariable("rollbackError", "Caught exception in AllottedResource Create Rollback")
252                         execution.setVariable("WorkflowException", null)
253
254                 }catch(BpmnError b){
255                         msoLogger.debug("BPMN Error during processRollbackExceptions Method: ")
256                 }catch(Exception e){
257                         msoLogger.debug("Caught Exception during processRollbackExceptions Method: " + e.getMessage())
258                 }
259
260                 msoLogger.trace("end processRollbackException")
261         }
262
263         public void processRollbackJavaException(DelegateExecution execution){
264
265                 msoLogger.trace("start processRollbackJavaException")
266                 try{
267                         execution.setVariable("rollbackData", null)
268                         execution.setVariable("rolledBack", false)
269                         execution.setVariable("rollbackError", "Caught Java exception in AllottedResource Create Rollback")
270                         msoLogger.debug("Caught Exception in processRollbackJavaException")
271
272                 }catch(Exception e){
273                         msoLogger.debug("Caught Exception in processRollbackJavaException " + e.getMessage())
274                 }
275                 msoLogger.trace("end processRollbackJavaException")
276         }
277
278 }