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