[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoDeleteNetworkInstanceRollback.groovy
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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.infrastructure.scripts;
21
22 import groovy.xml.XmlUtil
23 import groovy.json.*
24
25 import org.openecomp.mso.bpmn.core.json.JsonUtils
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
28 import org.openecomp.mso.bpmn.common.scripts.NetworkUtils
29 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
30 import org.openecomp.mso.bpmn.common.scripts.VidUtils
31 import org.openecomp.mso.bpmn.core.WorkflowException
32 import org.openecomp.mso.rest.APIResponse;
33 import org.openecomp.mso.rest.RESTClient
34 import org.openecomp.mso.rest.RESTConfig
35
36 import java.util.UUID;
37
38 import org.camunda.bpm.engine.delegate.BpmnError
39 import org.camunda.bpm.engine.runtime.Execution
40 import org.apache.commons.lang3.*
41 import org.apache.commons.codec.binary.Base64;
42 import org.springframework.web.util.UriUtils
43
44 /**
45  * This groovy class supports the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
46  *
47  */
48 public class DoDeleteNetworkInstanceRollback extends AbstractServiceTaskProcessor {
49         String Prefix="DELNWKIR_"
50         ExceptionUtil exceptionUtil = new ExceptionUtil()
51         JsonUtils jsonUtil = new JsonUtils()
52         VidUtils vidUtils = new VidUtils(this)
53         NetworkUtils networkUtils = new NetworkUtils()
54         SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
55
56         def className = getClass().getSimpleName()
57         
58         /**
59          * This method is executed during the preProcessRequest task of the <class>DoDeleteNetworkInstanceRollback.bpmn</class> process.
60          * @param execution
61          */
62         public InitializeProcessVariables(Execution execution){
63                 /* Initialize all the process variables in this block */
64                 
65                 execution.setVariable(Prefix + "WorkflowException", null)
66         
67                 execution.setVariable(Prefix + "rollbackDeactivateSDNCRequest", null)
68                 execution.setVariable(Prefix + "rollbackDeactivateSDNCResponse", "")
69                 execution.setVariable(Prefix + "rollbackDeactivateSDNCReturnCode", "")
70
71                 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
72                 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
73                 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
74                 
75                 execution.setVariable(Prefix + "rollbackNetworkRequest", null)
76                 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
77                 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
78                                 
79                 execution.setVariable(Prefix + "Success", false)
80                 execution.setVariable(Prefix + "fullRollback", false)
81
82         }
83
84         // **************************************************
85         //     Pre or Prepare Request Section
86         // **************************************************
87         /**
88          * This method is executed during the preProcessRequest task of the <class>DoDeleteNetworkInstanceRollback.bpmn</class> process.
89          * @param execution
90          */
91         public void preProcessRequest (Execution execution) {
92                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
93                 execution.setVariable("prefix",Prefix)
94                 
95                 utils.log("DEBUG", " ***** Inside preProcessRequest() of " + className + ".groovy ***** ", isDebugEnabled)
96
97                 try {
98                         // initialize flow variables
99                         InitializeProcessVariables(execution)
100                         
101                         // GET Incoming request/variables
102                         String rollbackDeactivateSDNCRequest = null
103                         String rollbackSDNCRequest = null
104                         String rollbackNetworkRequest = null
105
106                         Map<String, String> rollbackData = execution.getVariable("rollbackData")
107                         if (rollbackData != null && rollbackData instanceof Map) {
108                                 
109                                 if(rollbackData.containsKey("rollbackDeactivateSDNCRequest")) {
110                                    rollbackDeactivateSDNCRequest = rollbackData["rollbackDeactivateSDNCRequest"]
111                                 }
112                                 
113                                 if(rollbackData.containsKey("rollbackSDNCRequest")) {
114                                         rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
115                                  }
116                                 
117                                 if(rollbackData.containsKey("rollbackNetworkRequest")) {
118                                         rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
119                                  }
120                         }
121                         
122                         execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
123                         execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
124                         execution.setVariable(Prefix + "rollbackDeactivateSDNCRequest", rollbackDeactivateSDNCRequest)
125                         utils.log("DEBUG", "'rollbackData': " + '\n' + execution.getVariable("rollbackData"), isDebugEnabled)
126
127                         String sdncVersion = execution.getVariable("sdncVersion")
128                         utils.log("DEBUG", "sdncVersion? : " + sdncVersion, isDebugEnabled)
129                                 
130                         // PO Authorization Info / headers Authorization=
131                         String basicAuthValuePO = execution.getVariable("URN_mso_adapters_po_auth")
132                         utils.log("DEBUG", " Obtained BasicAuth userid password for PO/SDNC adapter: " + basicAuthValuePO, isDebugEnabled)
133                         try {
134                                 def encodedString = utils.getBasicAuth(basicAuthValuePO, execution.getVariable("URN_mso_msoKey"))
135                                 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
136                                 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
137         
138                         } catch (IOException ex) {
139                                 String exceptionMessage = "Exception Encountered in DoCreateNetworkInstance, PreProcessRequest() - "
140                                 String dataErrorMessage = exceptionMessage + " Unable to encode PO/SDNC user/password string - " + ex.getMessage()
141                                 utils.log("DEBUG", dataErrorMessage , isDebugEnabled)
142                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
143                         }
144
145                         if (execution.getVariable("SavedWorkflowException1") != null) {
146                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
147                         } else {
148                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
149                         }
150                         utils.log("DEBUG", "*** WorkflowException : " + execution.getVariable(Prefix + "WorkflowException"), isDebugEnabled)
151                         if(execution.getVariable(Prefix + "WorkflowException") != null) {
152                                 // called by: DoCreateNetworkInstance, partial rollback
153                                 execution.setVariable(Prefix + "fullRollback", false)
154
155                         } else {
156                            // called by: Macro - Full Rollback, WorkflowException = null
157                            execution.setVariable(Prefix + "fullRollback", true)
158                         
159                         }
160                         
161                         utils.log("DEBUG", "*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"), isDebugEnabled)
162                         
163                 } catch (BpmnError e) {
164                 throw e;
165                 
166                 } catch (Exception ex) {
167                         sendSyncError(execution)
168                         // caught exception
169                         String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
170                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
171                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
172                 
173                 }
174
175         }
176         
177         public void validateRollbackResponses (Execution execution) {
178                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
179                 execution.setVariable("prefix",Prefix)
180
181                 utils.log("DEBUG", " ***** Inside validateRollbackResponses() of DoDeleteNetworkInstanceRollback ***** ", isDebugEnabled)
182                 
183                 try {
184
185                         // validate SDNC activate response
186                         String rollbackDeactivateSDNCMessages = ""
187                         String rollbackDeactivateSDNCReturnCode = "200"
188                         if (execution.getVariable(Prefix + "rollbackDeactivateSDNCRequest") != null) {
189                                 rollbackDeactivateSDNCReturnCode = execution.getVariable(Prefix + "rollbackDeactivateSDNCReturnCode")
190                                 String rollbackDeactivateSDNCResponse = execution.getVariable(Prefix + "rollbackDeactivateSDNCResponse")
191                                 String rollbackDeactivateSDNCReturnInnerCode = ""
192                                 rollbackDeactivateSDNCResponse = sdncAdapterUtils.decodeXML(rollbackDeactivateSDNCResponse)
193                                 rollbackDeactivateSDNCResponse = rollbackDeactivateSDNCResponse.replace("&", "&amp;").replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
194                                 if (rollbackDeactivateSDNCReturnCode == "200") {
195                                         if (utils.nodeExists(rollbackDeactivateSDNCResponse, "response-code")) {
196                                                 rollbackDeactivateSDNCReturnInnerCode = utils.getNodeText1(rollbackDeactivateSDNCResponse, "response-code")
197                                                 if (rollbackDeactivateSDNCReturnInnerCode == "200" || rollbackDeactivateSDNCReturnInnerCode == "" || rollbackDeactivateSDNCReturnInnerCode == "0") {
198                                                    rollbackDeactivateSDNCMessages = " + SNDC deactivate rollback completed."
199                                                 } else {
200                                                         rollbackDeactivateSDNCMessages = " + SDNC deactivate rollback failed. "
201                                                 }
202                                         } else {
203                                                  rollbackDeactivateSDNCMessages = " + SNDC deactivate rollback completed."
204                                         }
205                                 } else {
206                                           rollbackDeactivateSDNCMessages = " + SDNC deactivate rollback failed. "
207                                 }
208                                 utils.log("DEBUG", " SDNC deactivate rollback Code - " + rollbackDeactivateSDNCReturnCode, isDebugEnabled)
209                                 utils.log("DEBUG", " SDNC deactivate rollback  Response - " + rollbackDeactivateSDNCResponse, isDebugEnabled)
210                         }
211                 
212                         // validate SDNC rollback response
213                         String rollbackSdncErrorMessages = ""
214                         String rollbackSDNCReturnCode = "200"
215                         if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
216                                 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
217                                 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
218                                 String rollbackSDNCReturnInnerCode = ""
219                                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
220                                 rollbackSDNCResponse = sdncAdapterUtils.decodeXML(rollbackSDNCResponse)
221                                 rollbackSDNCResponse = rollbackSDNCResponse.replace("&", "&amp;").replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
222                                 if (rollbackSDNCReturnCode == "200") {
223                                         if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
224                                                 rollbackSDNCReturnInnerCode = utils.getNodeText1(rollbackSDNCResponse, "response-code")
225                                                 if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
226                                                         rollbackSdncErrorMessages = " + SNDC unassign rollback completed."
227                                                 } else {
228                                                         rollbackSdncErrorMessages = " + SDNC unassign rollback failed. "
229                                                 }
230                                         } else {
231                                                   rollbackSdncErrorMessages = " + SNDC unassign rollback completed."
232                                         }
233                                  } else {
234                                           rollbackSdncErrorMessages = " + SDNC unassign rollback failed. "
235                                  }
236                                 utils.log("DEBUG", " SDNC assign rollback Code - " + rollbackSDNCReturnCode, isDebugEnabled)
237                                 utils.log("DEBUG", " SDNC assign rollback  Response - " + rollbackSDNCResponse, isDebugEnabled)
238                         }
239                         
240                         // validate PO network rollback response
241                         String rollbackNetworkErrorMessages = ""
242                         String rollbackNetworkReturnCode = "200"
243                         if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
244                                 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
245                                 String rollbackNetworkResponse = execution.getVariable(Prefix + "rollbackNetworkResponse")
246                                 if (rollbackNetworkReturnCode != "200") {
247                                         rollbackNetworkErrorMessages = " + PO Network rollback failed. "
248                                 } else {
249                                         rollbackNetworkErrorMessages = " + PO Network rollback completed."
250                                 }
251         
252                                 utils.log("DEBUG", " NetworkRollback Code - " + rollbackNetworkReturnCode, isDebugEnabled)
253                                 utils.log("DEBUG", " NetworkRollback Response - " + rollbackNetworkResponse, isDebugEnabled)
254                         }
255                                 
256                         String statusMessage = ""
257                         int errorCode = 7000
258                         utils.log("DEBUG", "*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"), isDebugEnabled)
259                         if (execution.getVariable(Prefix + "fullRollback") == false) {
260                                 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException") // original WorkflowException
261                                 if (wfe != null) {
262                                         statusMessage = wfe.getErrorMessage()
263                                         errorCode = wfe.getErrorCode()
264                                 } else {
265                                         statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
266                                         errorCode = '7000'
267                                 }
268
269                                 // set if all rolledbacks are successful
270                                 if (rollbackDeactivateSDNCReturnCode == "200" && rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200") {
271                                         execution.setVariable("rolledBack", true)
272                                         execution.setVariable("wasDeleted", true)
273                                 
274                                 } else {
275                                         execution.setVariable("rolledBack", false)
276                                         execution.setVariable("wasDeleted", true)
277                                 }
278
279                                 statusMessage =  statusMessage + rollbackDeactivateSDNCMessages + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
280                                 utils.log("DEBUG", "Final DoDeleteNetworkInstanceRollback status message: " + statusMessage, isDebugEnabled)
281                                 String processKey = getProcessKey(execution);
282                                 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
283                                 execution.setVariable("workflowException", exception);
284                         
285                         } else {
286                                 // rollback due to failures in Main flow (Macro or a-ala-carte) - Full rollback
287                             if (rollbackDeactivateSDNCReturnCode == "200" && rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200") {
288                                     execution.setVariable("rollbackSuccessful", true)
289                                     execution.setVariable("rollbackError", false) 
290                             } else {
291                                     String exceptionMessage = "Network Delete Rollback was not Successful. "
292                     utils.log("DEBUG", exceptionMessage, isDebugEnabled)
293                                         execution.setVariable("rollbackSuccessful", false)
294                                     execution.setVariable("rollbackError", true)
295                                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
296                                         throw new BpmnError("MSOWorkflowException")
297                             }  
298                         }                       
299                         
300                 } catch (Exception ex) {
301                         String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
302                         String exceptionMessage = " Bpmn error encountered in DoDeleteNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
303                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
304                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
305
306                 }
307
308         }
309
310         // *******************************
311         //     Build Error Section
312         // *******************************
313
314
315         
316         public void processJavaException(Execution execution){
317                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
318                 execution.setVariable("prefix",Prefix)
319                 
320                 try{
321                         utils.log("DEBUG", "Caught a Java Exception in " + Prefix, isDebugEnabled)
322                         utils.log("DEBUG", "Started processJavaException Method", isDebugEnabled)
323                         utils.log("DEBUG", "Variables List: " + execution.getVariables(), isDebugEnabled)
324                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
325                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
326                         
327                 }catch(Exception e){
328                         utils.log("DEBUG", "Caught Exception during processJavaException Method: " + e, isDebugEnabled)
329                         execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
330                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
331                 }
332                 utils.log("DEBUG", "Completed processJavaException Method in " + Prefix, isDebugEnabled)
333         }
334
335 }