2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.mso.bpmn.infrastructure.scripts;
22 import groovy.xml.XmlUtil
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
36 import java.util.UUID;
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
45 * This groovy class supports the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
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()
56 def className = getClass().getSimpleName()
59 * This method is executed during the preProcessRequest task of the <class>DoDeleteNetworkInstanceRollback.bpmn</class> process.
62 public InitializeProcessVariables(Execution execution){
63 /* Initialize all the process variables in this block */
65 execution.setVariable(Prefix + "WorkflowException", null)
67 execution.setVariable(Prefix + "rollbackDeactivateSDNCRequest", null)
68 execution.setVariable(Prefix + "rollbackDeactivateSDNCResponse", "")
69 execution.setVariable(Prefix + "rollbackDeactivateSDNCReturnCode", "")
71 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
72 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
73 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
75 execution.setVariable(Prefix + "rollbackNetworkRequest", null)
76 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
77 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
79 execution.setVariable(Prefix + "Success", false)
80 execution.setVariable(Prefix + "fullRollback", false)
84 // **************************************************
85 // Pre or Prepare Request Section
86 // **************************************************
88 * This method is executed during the preProcessRequest task of the <class>DoDeleteNetworkInstanceRollback.bpmn</class> process.
91 public void preProcessRequest (Execution execution) {
92 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
93 execution.setVariable("prefix",Prefix)
95 utils.log("DEBUG", " ***** Inside preProcessRequest() of " + className + ".groovy ***** ", isDebugEnabled)
98 // initialize flow variables
99 InitializeProcessVariables(execution)
101 // GET Incoming request/variables
102 String rollbackDeactivateSDNCRequest = null
103 String rollbackSDNCRequest = null
104 String rollbackNetworkRequest = null
106 Map<String, String> rollbackData = execution.getVariable("rollbackData")
107 if (rollbackData != null && rollbackData instanceof Map) {
109 if(rollbackData.containsKey("rollbackDeactivateSDNCRequest")) {
110 rollbackDeactivateSDNCRequest = rollbackData["rollbackDeactivateSDNCRequest"]
113 if(rollbackData.containsKey("rollbackSDNCRequest")) {
114 rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
117 if(rollbackData.containsKey("rollbackNetworkRequest")) {
118 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
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)
127 String sdncVersion = execution.getVariable("sdncVersion")
128 utils.log("DEBUG", "sdncVersion? : " + sdncVersion, isDebugEnabled)
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)
134 def encodedString = utils.getBasicAuth(basicAuthValuePO, execution.getVariable("URN_mso_msoKey"))
135 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
136 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
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)
145 if (execution.getVariable("SavedWorkflowException1") != null) {
146 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
148 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
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)
156 // called by: Macro - Full Rollback, WorkflowException = null
157 execution.setVariable(Prefix + "fullRollback", true)
161 utils.log("DEBUG", "*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"), isDebugEnabled)
163 } catch (BpmnError e) {
166 } catch (Exception ex) {
167 sendSyncError(execution)
169 String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
170 utils.log("DEBUG", exceptionMessage, isDebugEnabled)
171 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
177 public void validateRollbackResponses (Execution execution) {
178 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
179 execution.setVariable("prefix",Prefix)
181 utils.log("DEBUG", " ***** Inside validateRollbackResponses() of DoDeleteNetworkInstanceRollback ***** ", isDebugEnabled)
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("&", "&").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."
200 rollbackDeactivateSDNCMessages = " + SDNC deactivate rollback failed. "
203 rollbackDeactivateSDNCMessages = " + SNDC deactivate rollback completed."
206 rollbackDeactivateSDNCMessages = " + SDNC deactivate rollback failed. "
208 utils.log("DEBUG", " SDNC deactivate rollback Code - " + rollbackDeactivateSDNCReturnCode, isDebugEnabled)
209 utils.log("DEBUG", " SDNC deactivate rollback Response - " + rollbackDeactivateSDNCResponse, isDebugEnabled)
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("&", "&").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."
228 rollbackSdncErrorMessages = " + SDNC unassign rollback failed. "
231 rollbackSdncErrorMessages = " + SNDC unassign rollback completed."
234 rollbackSdncErrorMessages = " + SDNC unassign rollback failed. "
236 utils.log("DEBUG", " SDNC assign rollback Code - " + rollbackSDNCReturnCode, isDebugEnabled)
237 utils.log("DEBUG", " SDNC assign rollback Response - " + rollbackSDNCResponse, isDebugEnabled)
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. "
249 rollbackNetworkErrorMessages = " + PO Network rollback completed."
252 utils.log("DEBUG", " NetworkRollback Code - " + rollbackNetworkReturnCode, isDebugEnabled)
253 utils.log("DEBUG", " NetworkRollback Response - " + rollbackNetworkResponse, isDebugEnabled)
256 String statusMessage = ""
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
262 statusMessage = wfe.getErrorMessage()
263 errorCode = wfe.getErrorCode()
265 statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
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)
275 execution.setVariable("rolledBack", false)
276 execution.setVariable("wasDeleted", true)
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);
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)
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")
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)
310 // *******************************
311 // Build Error Section
312 // *******************************
316 public void processJavaException(Execution execution){
317 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
318 execution.setVariable("prefix",Prefix)
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")
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)
332 utils.log("DEBUG", "Completed processJavaException Method in " + Prefix, isDebugEnabled)