[MSO-8] Update the maven dependency
[so.git] / bpmn / MSOInfrastructureBPMN / src / main / groovy / org / openecomp / mso / bpmn / infrastructure / scripts / DoCreateNetworkInstanceRollback.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>DoCreateNetworkInstance.bpmn</class> process.
46  *
47  */
48 public class DoCreateNetworkInstanceRollback extends AbstractServiceTaskProcessor {
49         String Prefix="CRENWKIR_"
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>DoCreateNetworkInstanceRollback.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 + "rollbackNetworkRequest", null)
66                 execution.setVariable(Prefix + "rollbackSDNCRequest", null)
67                 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", null)
68                 execution.setVariable(Prefix + "WorkflowException", null)
69                 
70                 execution.setVariable(Prefix + "rollbackNetworkRequest", "")
71                 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
72                 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
73
74                 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
75                 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
76                 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
77                 
78                 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", "")
79                 execution.setVariable(Prefix + "rollbackActivateSDNCResponse", "")
80                 execution.setVariable(Prefix + "rollbackActivateSDNCReturnCode", "")
81
82                 execution.setVariable(Prefix + "Success", false)
83                 execution.setVariable(Prefix + "fullRollback", false)
84                 execution.setVariable(Prefix + "networkId", "")
85                 execution.setVariable(Prefix + "urlRollbackPoNetwork", "")
86
87         }
88
89         // **************************************************
90         //     Pre or Prepare Request Section
91         // **************************************************
92         /**
93          * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
94          * @param execution
95          */
96         public void preProcessRequest (Execution execution) {
97                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
98                 execution.setVariable("prefix",Prefix)
99                 
100                 utils.log("DEBUG", " ***** Inside preProcessRequest() of " + className + ".groovy ***** ", isDebugEnabled)
101
102                 try {
103                         // initialize flow variables
104                         InitializeProcessVariables(execution)
105                         
106                         // GET Incoming request/variables
107                         String rollbackNetworkRequest = null
108                         String rollbackSDNCRequest = null
109                         String rollbackActivateSDNCRequest = null
110                         
111                         // Partial Rollback
112                         Map<String, String> rollbackData = execution.getVariable("rollbackData")
113                         if (rollbackData != null && rollbackData instanceof Map) {
114
115                                         if(rollbackData.containsKey("rollbackSDNCRequest")) {
116                                            rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
117                                         }
118                                         
119                                         if(rollbackData.containsKey("rollbackNetworkRequest")) {
120                                                 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
121                                         }
122
123                                         if(rollbackData.containsKey("rollbackActivateSDNCRequest")) {
124                                            rollbackActivateSDNCRequest = rollbackData["rollbackActivateSDNCRequest"]
125                                         }
126                                         
127                         }
128                         
129                         execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
130                         execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
131                         execution.setVariable(Prefix + "rollbackActivateSDNCRequest", rollbackActivateSDNCRequest)
132                         utils.log("DEBUG", "'rollbackData': " + '\n' + execution.getVariable("rollbackData"), isDebugEnabled)
133                         
134                         String sdncVersion = execution.getVariable("sdncVersion")
135                         utils.log("DEBUG", "sdncVersion? : " + sdncVersion, isDebugEnabled)
136
137                         // PO Authorization Info / headers Authorization=
138                         String basicAuthValuePO = execution.getVariable("URN_mso_adapters_po_auth")
139                         utils.log("DEBUG", " Obtained BasicAuth userid password for PO/SDNC adapter: " + basicAuthValuePO, isDebugEnabled)
140                         try {
141                                 def encodedString = utils.getBasicAuth(basicAuthValuePO, execution.getVariable("URN_mso_msoKey"))
142                                 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
143                                 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
144         
145                         } catch (IOException ex) {
146                                 String exceptionMessage = "Exception Encountered in DoCreateNetworkInstance, PreProcessRequest() - "
147                                 String dataErrorMessage = exceptionMessage + " Unable to encode PO/SDNC user/password string - " + ex.getMessage()
148                                 utils.log("DEBUG", dataErrorMessage , isDebugEnabled)
149                                 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
150                         }
151
152                         if (execution.getVariable("SavedWorkflowException1") != null) {
153                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
154                         } else {
155                                 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
156                         }
157                         utils.log("DEBUG", "*** WorkflowException : " + execution.getVariable(Prefix + "WorkflowException"), isDebugEnabled)
158                         if(execution.getVariable(Prefix + "WorkflowException") != null) {
159                                 // called by: DoCreateNetworkInstance, partial rollback
160                                 execution.setVariable(Prefix + "fullRollback", false)
161
162                         } else {
163                            // called by: Macro - Full Rollback, WorkflowException = null
164                            execution.setVariable(Prefix + "fullRollback", true)
165                         
166                         }
167                         utils.log("DEBUG", "*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"), isDebugEnabled)
168                         
169                 
170                 } catch (BpmnError e) {
171                 throw e;
172                 
173                 } catch (Exception ex) {
174                         // caught exception
175                         String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
176                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
177                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
178                 
179                 }
180
181         }
182         
183         public void callPONetworkAdapter (Execution execution) {
184                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
185                 execution.setVariable("prefix",Prefix)
186
187                 utils.log("DEBUG", " ***** Inside callPONetworkAdapter() of " + className + " ***** ", isDebugEnabled)
188                 
189                 try {
190                         String poUrl = execution.getVariable("URN_mso_adapters_network_rest_endpoint")
191                         String rollbackSDNCRequest = execution.getVariable(Prefix + "rollbackSDNCRequest")
192                         String networkId = utils.getNodeText1(rollbackSDNCRequest, "network-id")
193                 
194                         String rollbackNetworkRequest  = execution.getVariable(Prefix + "rollbackNetworkRequest")
195
196                         String urlRollbackPoNetwork = poUrl+ "/" + networkId + "/rollback"
197                         utils.log("DEBUG", "'urlRollbackPoNetwork': " + urlRollbackPoNetwork, isDebugEnabled)
198                         execution.setVariable(Prefix + "urlRollbackPoNetwork", urlRollbackPoNetwork)
199
200                         RESTConfig config = new RESTConfig(urlRollbackPoNetwork)
201                         RESTClient client = new RESTClient(config).
202                                                                              addHeader("Content-Type", "application/xml").
203                                                                           addAuthorizationHeader(execution.getVariable("BasicAuthHeaderValuePO"));
204
205                     APIResponse response = client.httpDelete(rollbackNetworkRequest)
206                         String responseCode = response.getStatusCode()
207                         String responseBody = response.getResponseBodyAsString() 
208                 
209                         execution.setVariable(Prefix + "rollbackNetworkReturnCode", responseCode)
210                         execution.setVariable(Prefix + "rollbackNetworkResponse", responseBody)
211                 
212                         utils.log("DEBUG", " Network Adapter rollback responseCode: " + responseCode, isDebugEnabled)
213                         utils.log("DEBUG", " Network Adapter rollback responseBody: " + responseBody, isDebugEnabled)
214                 
215                         
216                 } catch (Exception ex) {
217                         String exceptionMessage = "Exception Encountered in callPONetworkAdapter() of DoCreateNetworkInstanceRollback flow - " + ex.getMessage()
218                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
219                         exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
220                 }
221                 
222         }
223         
224         
225         public void validateRollbackResponses (Execution execution) {
226                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
227                 execution.setVariable("prefix",Prefix)
228
229                 utils.log("DEBUG", " ***** Inside validateRollbackResponses() of DoCreateNetworkInstanceRollback ***** ", isDebugEnabled)
230                 
231                 try {
232                         // validate PO network rollback response
233                         String rollbackNetworkErrorMessages = ""
234                         String rollbackNetworkReturnCode = "200"
235                         if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
236                                 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
237                                 String rollbackNetworkResponse = execution.getVariable(Prefix + "rollbackNetworkResponse")
238                                 utils.log("DEBUG", " NetworkRollback Code - " + rollbackNetworkReturnCode, isDebugEnabled)
239                                 utils.log("DEBUG", " NetworkRollback Response - " + rollbackNetworkResponse, isDebugEnabled)
240                                 if (rollbackNetworkReturnCode != "200") {
241                                         rollbackNetworkErrorMessages = " + PO Network rollback failed. "
242                                 } else {
243                                         rollbackNetworkErrorMessages = " + PO Network rollback completed."
244                                 }
245                         }
246                         
247                         // validate SDNC rollback response
248                         String rollbackSdncErrorMessages = ""
249                         String rollbackSDNCReturnCode = "200"
250                         if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
251                                 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
252                                 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
253                                 String rollbackSDNCReturnInnerCode = ""
254                                 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
255                                 rollbackSDNCResponse = sdncAdapterUtils.decodeXML(rollbackSDNCResponse)
256                                 rollbackSDNCResponse = rollbackSDNCResponse.replace("&", "&amp;").replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
257                                 if (rollbackSDNCReturnCode == "200") {
258                                         if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
259                                                 rollbackSDNCReturnInnerCode = utils.getNodeText1(rollbackSDNCResponse, "response-code")
260                                                 if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
261                                                     rollbackSdncErrorMessages = " + SNDC assign rollback completed."
262                                                 } else {
263                                                         rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
264                                                 }
265                                         } else {
266                                                   rollbackSdncErrorMessages = " + SNDC assign rollback completed."
267                                         }  
268                                  } else {
269                                           rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
270                              }
271                                 utils.log("DEBUG", " SDNC assign rollback Code - " + rollbackSDNCReturnCode, isDebugEnabled)
272                                 utils.log("DEBUG", " SDNC assign rollback  Response - " + rollbackSDNCResponse, isDebugEnabled)
273                         }       
274
275                         // validate SDNC activate rollback response
276                         String rollbackActivateSdncErrorMessages = ""
277                         String rollbackActivateSDNCReturnCode = "200"
278                         if (execution.getVariable(Prefix + "rollbackActivateSDNCRequest") != null) {
279                                 rollbackActivateSDNCReturnCode = execution.getVariable(Prefix + "rollbackActivateSDNCReturnCode")
280                                 String rollbackActivateSDNCResponse = execution.getVariable(Prefix + "rollbackActivateSDNCResponse")
281                                 String rollbackActivateSDNCReturnInnerCode = ""
282                                 rollbackActivateSDNCResponse = sdncAdapterUtils.decodeXML(rollbackActivateSDNCResponse)
283                                 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse.replace("&", "&amp;").replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
284                                 if (rollbackActivateSDNCReturnCode == "200") {
285                                         if (utils.nodeExists(rollbackActivateSDNCResponse, "response-code")) {
286                                                 rollbackActivateSDNCReturnInnerCode = utils.getNodeText1(rollbackActivateSDNCResponse, "response-code")
287                                                 if (rollbackActivateSDNCReturnInnerCode == "200" || rollbackActivateSDNCReturnInnerCode == "" || rollbackActivateSDNCReturnInnerCode == "0") {
288                                                    rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
289                                                 } else {
290                                                         rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
291                                                 }
292                                         } else {
293                                                  rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
294                                         }
295                                 } else {
296                                           rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
297                                 }
298                                 utils.log("DEBUG", " SDNC activate rollback Code - " + rollbackActivateSDNCReturnCode, isDebugEnabled)
299                                 utils.log("DEBUG", " SDNC activate rollback  Response - " + rollbackActivateSDNCResponse, isDebugEnabled)
300                         }       
301
302
303                         String statusMessage = ""
304                         int errorCode = 7000
305                         utils.log("DEBUG", "*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"), isDebugEnabled)
306                         if (execution.getVariable(Prefix + "fullRollback") == false) { 
307                                 // original WorkflowException, 
308                                 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException") 
309                                 if (wfe != null) {
310                                    // rollback due to failure in DoCreate - Partial rollback
311                                    statusMessage = wfe.getErrorMessage()
312                                    errorCode = wfe.getErrorCode()
313                                 } else {
314                                    statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
315                                    errorCode = '7000'
316                             }
317
318                                 // set if all rolledbacks are successful
319                                 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
320                                         execution.setVariable("rolledBack", true)
321                                         execution.setVariable("wasDeleted", true)
322                                         
323                                 } else {
324                                         execution.setVariable("rolledBack", false)
325                                         execution.setVariable("wasDeleted", true)
326                                 }
327
328                                 statusMessage = statusMessage + rollbackActivateSdncErrorMessages + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
329                                 utils.log("DEBUG", "Final DoCreateNetworkInstanceRollback status message: " + statusMessage, isDebugEnabled)
330                                 String processKey = getProcessKey(execution);
331                                 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
332                                 execution.setVariable("workflowException", exception);
333                                 
334                         } else { 
335                                 // rollback due to failures in Main flow (Macro) - Full rollback
336                                 // WorkflowException = null
337                             if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
338                                         execution.setVariable("rollbackSuccessful", true)
339                                         execution.setVariable("rollbackError", false)
340                             } else {
341                                     String exceptionMessage = "Network Create Rollback was not Successful. "
342                     utils.log("DEBUG", exceptionMessage, isDebugEnabled)
343                                         execution.setVariable("rollbackSuccessful", false)
344                                     execution.setVariable("rollbackError", true)
345                                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
346                                         throw new BpmnError("MSOWorkflowException")
347                             }
348                                   
349                         } 
350                         
351
352                 } catch (Exception ex) {
353                         String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
354                         String exceptionMessage = " Bpmn error encountered in DoCreateNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
355                         utils.log("DEBUG", exceptionMessage, isDebugEnabled)
356                         exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
357
358                 }
359
360         }
361
362         // *******************************
363         //     Build Error Section
364         // *******************************
365
366
367         
368         public void processJavaException(Execution execution){
369                 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
370                 execution.setVariable("prefix",Prefix)
371                 
372                 try{
373                         utils.log("DEBUG", "Caught a Java Exception in " + Prefix, isDebugEnabled)
374                         utils.log("DEBUG", "Started processJavaException Method", isDebugEnabled)
375                         utils.log("DEBUG", "Variables List: " + execution.getVariables(), isDebugEnabled)
376                         execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
377                         exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
378                         
379                 }catch(Exception e){
380                         utils.log("DEBUG", "Caught Exception during processJavaException Method: " + e, isDebugEnabled)
381                         execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix)  // Adding this line temporarily until this flows error handling gets updated
382                         exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
383                 }
384                 utils.log("DEBUG", "Completed processJavaException Method in " + Prefix, isDebugEnabled)
385         }
386
387 }