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.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
35 import java.util.UUID;
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
44 * This groovy class supports the <class>DoCreateNetworkInstance.bpmn</class> process.
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()
55 def className = getClass().getSimpleName()
58 * This method is executed during the preProcessRequest task of the <class>DoUpdateNetworkInstanceRollback.bpmn</class> process.
61 public InitializeProcessVariables(Execution execution){
62 /* Initialize all the process variables in this block */
64 execution.setVariable(Prefix + "rollbackNetworkRequest", null)
65 execution.setVariable(Prefix + "rollbackSDNCRequest", null)
66 execution.setVariable(Prefix + "WorkflowException", null)
68 execution.setVariable(Prefix + "rollbackNetworkRequest", "")
69 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
70 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
72 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
73 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
74 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
76 execution.setVariable(Prefix + "Success", false)
77 execution.setVariable(Prefix + "fullRollback", false)
81 // **************************************************
82 // Pre or Prepare Request Section
83 // **************************************************
85 * This method is executed during the preProcessRequest task of the <class>DoUpdateNetworkInstanceRollback.bpmn</class> process.
88 public void preProcessRequest (Execution execution) {
89 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
90 execution.setVariable("prefix",Prefix)
92 utils.log("DEBUG", " ***** Inside preProcessRequest() of " + className + ".groovy ***** ", isDebugEnabled)
95 // initialize flow variables
96 InitializeProcessVariables(execution)
98 // GET Incoming request/variables
99 String rollbackNetworkRequest = null
100 String rollbackSDNCRequest = null
103 Map<String, String> rollbackData = execution.getVariable("rollbackData")
104 if (rollbackData != null && rollbackData instanceof Map) {
106 if(rollbackData.containsKey("rollbackNetworkRequest")) {
107 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
110 if(rollbackData.containsKey("rollbackSDNCRequest")) {
111 rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
115 execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
116 execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
117 utils.log("DEBUG", "'rollbackData': " + '\n' + execution.getVariable("rollbackData"), isDebugEnabled)
119 String sdncVersion = execution.getVariable("sdncVersion")
120 utils.log("DEBUG", "sdncVersion? : " + sdncVersion, isDebugEnabled)
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)
126 def encodedString = utils.getBasicAuth(basicAuthValuePO, execution.getVariable("URN_mso_msoKey"))
127 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
128 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
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)
137 if (execution.getVariable("SavedWorkflowException1") != null) {
138 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
140 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
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)
148 // called by: Macro - Full Rollback, WorkflowException = null
149 execution.setVariable(Prefix + "fullRollback", true)
152 utils.log("DEBUG", "*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"), isDebugEnabled)
155 } catch (BpmnError e) {
158 } catch (Exception ex) {
159 sendSyncError(execution)
161 String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
162 utils.log("DEBUG", exceptionMessage, isDebugEnabled)
163 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
169 public void validateRollbackResponses (Execution execution) {
170 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
171 execution.setVariable("prefix",Prefix)
173 utils.log("DEBUG", " ***** Inside validateRollbackResponses() of DoUpdateNetworkInstanceRollback ***** ", isDebugEnabled)
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. "
187 rollbackNetworkErrorMessages = " + PO Network rollback completed."
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("&", "&").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."
207 rollbackSdncErrorMessages = " + SDNC changeassign rollback failed. "
210 rollbackSdncErrorMessages = " + SNDC changeassign rollback completed."
213 rollbackSdncErrorMessages = " + SDNC changeassign rollback failed. "
215 utils.log("DEBUG", " SDNC changeassign rollback Code - " + rollbackSDNCReturnCode, isDebugEnabled)
216 utils.log("DEBUG", " SDNC changeassign rollback Response - " + rollbackSDNCResponse, isDebugEnabled)
219 String statusMessage = ""
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")
226 // rollback due to failure in DoCreate - Partial rollback
227 statusMessage = wfe.getErrorMessage()
228 errorCode = wfe.getErrorCode()
230 statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
234 // set if all rolledbacks are successful
235 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200") {
236 execution.setVariable("rolledBack", true)
239 execution.setVariable("rolledBack", false)
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);
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)
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")
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"))
279 // *******************************
280 // Build Error Section
281 // *******************************
285 public void processJavaException(Execution execution){
286 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
287 execution.setVariable("prefix",Prefix)
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")
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)
301 utils.log("DEBUG", "Completed processJavaException Method in " + Prefix, isDebugEnabled)