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