2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
20 package org.openecomp.mso.bpmn.infrastructure.scripts;
\r
22 import groovy.xml.XmlUtil
\r
23 import groovy.json.*
\r
25 import org.openecomp.mso.bpmn.core.json.JsonUtils
\r
26 import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
\r
27 import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
\r
28 import org.openecomp.mso.bpmn.common.scripts.NetworkUtils
\r
29 import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
\r
30 import org.openecomp.mso.bpmn.common.scripts.VidUtils
\r
31 import org.openecomp.mso.bpmn.core.WorkflowException
\r
32 import org.openecomp.mso.rest.APIResponse;
\r
33 import org.openecomp.mso.rest.RESTClient
\r
34 import org.openecomp.mso.rest.RESTConfig
\r
36 import java.util.UUID;
\r
38 import org.camunda.bpm.engine.delegate.BpmnError
\r
39 import org.camunda.bpm.engine.delegate.DelegateExecution
\r
40 import org.apache.commons.lang3.*
\r
41 import org.apache.commons.codec.binary.Base64;
\r
42 import org.springframework.web.util.UriUtils
\r
45 * This groovy class supports the <class>DoCreateNetworkInstance.bpmn</class> process.
\r
48 public class DoCreateNetworkInstanceRollback extends AbstractServiceTaskProcessor {
\r
49 String Prefix="CRENWKIR_"
\r
50 ExceptionUtil exceptionUtil = new ExceptionUtil()
\r
51 JsonUtils jsonUtil = new JsonUtils()
\r
52 VidUtils vidUtils = new VidUtils(this)
\r
53 NetworkUtils networkUtils = new NetworkUtils()
\r
54 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
\r
56 def className = getClass().getSimpleName()
\r
59 * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
\r
62 public InitializeProcessVariables(DelegateExecution execution){
\r
63 /* Initialize all the process variables in this block */
\r
65 execution.setVariable(Prefix + "rollbackNetworkRequest", null)
\r
66 execution.setVariable(Prefix + "rollbackSDNCRequest", null)
\r
67 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", null)
\r
68 execution.setVariable(Prefix + "WorkflowException", null)
\r
70 execution.setVariable(Prefix + "rollbackNetworkRequest", "")
\r
71 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
\r
72 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
\r
74 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
\r
75 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
\r
76 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
\r
78 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", "")
\r
79 execution.setVariable(Prefix + "rollbackActivateSDNCResponse", "")
\r
80 execution.setVariable(Prefix + "rollbackActivateSDNCReturnCode", "")
\r
82 execution.setVariable(Prefix + "Success", false)
\r
83 execution.setVariable(Prefix + "fullRollback", false)
\r
84 execution.setVariable(Prefix + "networkId", "")
\r
85 execution.setVariable(Prefix + "urlRollbackPoNetwork", "")
\r
89 // **************************************************
\r
90 // Pre or Prepare Request Section
\r
91 // **************************************************
\r
93 * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
\r
96 public void preProcessRequest (DelegateExecution execution) {
\r
97 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
98 execution.setVariable("prefix",Prefix)
\r
100 utils.log("DEBUG", " ***** Inside preProcessRequest() of " + className + ".groovy ***** ", isDebugEnabled)
\r
103 // initialize flow variables
\r
104 InitializeProcessVariables(execution)
\r
106 // GET Incoming request/variables
\r
107 String rollbackNetworkRequest = null
\r
108 String rollbackSDNCRequest = null
\r
109 String rollbackActivateSDNCRequest = null
\r
111 // Partial Rollback
\r
112 Map<String, String> rollbackData = execution.getVariable("rollbackData")
\r
113 if (rollbackData != null && rollbackData instanceof Map) {
\r
115 if(rollbackData.containsKey("rollbackSDNCRequest")) {
\r
116 rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
\r
119 if(rollbackData.containsKey("rollbackNetworkRequest")) {
\r
120 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
\r
123 if(rollbackData.containsKey("rollbackActivateSDNCRequest")) {
\r
124 rollbackActivateSDNCRequest = rollbackData["rollbackActivateSDNCRequest"]
\r
129 execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
\r
130 execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
\r
131 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", rollbackActivateSDNCRequest)
\r
132 utils.log("DEBUG", "'rollbackData': " + '\n' + execution.getVariable("rollbackData"), isDebugEnabled)
\r
134 String sdncVersion = execution.getVariable("sdncVersion")
\r
135 utils.log("DEBUG", "sdncVersion? : " + sdncVersion, isDebugEnabled)
\r
137 // PO Authorization Info / headers Authorization=
\r
138 String basicAuthValuePO = execution.getVariable("URN_mso_adapters_po_auth")
\r
139 utils.log("DEBUG", " Obtained BasicAuth userid password for PO/SDNC adapter: " + basicAuthValuePO, isDebugEnabled)
\r
141 def encodedString = utils.getBasicAuth(basicAuthValuePO, execution.getVariable("URN_mso_msoKey"))
\r
142 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
\r
143 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
\r
145 } catch (IOException ex) {
\r
146 String exceptionMessage = "Exception Encountered in DoCreateNetworkInstance, PreProcessRequest() - "
\r
147 String dataErrorMessage = exceptionMessage + " Unable to encode PO/SDNC user/password string - " + ex.getMessage()
\r
148 utils.log("DEBUG", dataErrorMessage , isDebugEnabled)
\r
149 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
\r
152 if (execution.getVariable("SavedWorkflowException1") != null) {
\r
153 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
\r
155 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
\r
157 utils.log("DEBUG", "*** WorkflowException : " + execution.getVariable(Prefix + "WorkflowException"), isDebugEnabled)
\r
158 if(execution.getVariable(Prefix + "WorkflowException") != null) {
\r
159 // called by: DoCreateNetworkInstance, partial rollback
\r
160 execution.setVariable(Prefix + "fullRollback", false)
\r
163 // called by: Macro - Full Rollback, WorkflowException = null
\r
164 execution.setVariable(Prefix + "fullRollback", true)
\r
167 utils.log("DEBUG", "*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"), isDebugEnabled)
\r
170 } catch (BpmnError e) {
\r
173 } catch (Exception ex) {
\r
174 // caught exception
\r
175 String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
\r
176 utils.log("DEBUG", exceptionMessage, isDebugEnabled)
\r
177 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
\r
183 public void callPONetworkAdapter (DelegateExecution execution) {
\r
184 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
185 execution.setVariable("prefix",Prefix)
\r
187 utils.log("DEBUG", " ***** Inside callPONetworkAdapter() of " + className + " ***** ", isDebugEnabled)
\r
190 String poUrl = execution.getVariable("URN_mso_adapters_network_rest_endpoint")
\r
191 String rollbackSDNCRequest = execution.getVariable(Prefix + "rollbackSDNCRequest")
\r
192 String networkId = utils.getNodeText1(rollbackSDNCRequest, "network-id")
\r
194 String rollbackNetworkRequest = execution.getVariable(Prefix + "rollbackNetworkRequest")
\r
196 String urlRollbackPoNetwork = poUrl+ "/" + networkId + "/rollback"
\r
197 utils.log("DEBUG", "'urlRollbackPoNetwork': " + urlRollbackPoNetwork, isDebugEnabled)
\r
198 execution.setVariable(Prefix + "urlRollbackPoNetwork", urlRollbackPoNetwork)
\r
200 RESTConfig config = new RESTConfig(urlRollbackPoNetwork)
\r
201 RESTClient client = new RESTClient(config).
\r
202 addHeader("Content-Type", "application/xml").
\r
203 addAuthorizationHeader(execution.getVariable("BasicAuthHeaderValuePO"));
\r
205 APIResponse response = client.httpDelete(rollbackNetworkRequest)
\r
206 String responseCode = response.getStatusCode()
\r
207 String responseBody = response.getResponseBodyAsString()
\r
209 execution.setVariable(Prefix + "rollbackNetworkReturnCode", responseCode)
\r
210 execution.setVariable(Prefix + "rollbackNetworkResponse", responseBody)
\r
212 utils.log("DEBUG", " Network Adapter rollback responseCode: " + responseCode, isDebugEnabled)
\r
213 utils.log("DEBUG", " Network Adapter rollback responseBody: " + responseBody, isDebugEnabled)
\r
216 } catch (Exception ex) {
\r
217 String exceptionMessage = "Exception Encountered in callPONetworkAdapter() of DoCreateNetworkInstanceRollback flow - " + ex.getMessage()
\r
218 utils.log("DEBUG", exceptionMessage, isDebugEnabled)
\r
219 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
\r
225 public void validateRollbackResponses (DelegateExecution execution) {
\r
226 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
227 execution.setVariable("prefix",Prefix)
\r
229 utils.log("DEBUG", " ***** Inside validateRollbackResponses() of DoCreateNetworkInstanceRollback ***** ", isDebugEnabled)
\r
232 // validate PO network rollback response
\r
233 String rollbackNetworkErrorMessages = ""
\r
234 String rollbackNetworkReturnCode = "200"
\r
235 if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
\r
236 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
\r
237 String rollbackNetworkResponse = execution.getVariable(Prefix + "rollbackNetworkResponse")
\r
238 utils.log("DEBUG", " NetworkRollback Code - " + rollbackNetworkReturnCode, isDebugEnabled)
\r
239 utils.log("DEBUG", " NetworkRollback Response - " + rollbackNetworkResponse, isDebugEnabled)
\r
240 if (rollbackNetworkReturnCode != "200") {
\r
241 rollbackNetworkErrorMessages = " + PO Network rollback failed. "
\r
243 rollbackNetworkErrorMessages = " + PO Network rollback completed."
\r
247 // validate SDNC rollback response
\r
248 String rollbackSdncErrorMessages = ""
\r
249 String rollbackSDNCReturnCode = "200"
\r
250 if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
\r
251 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
\r
252 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
\r
253 String rollbackSDNCReturnInnerCode = ""
\r
254 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
\r
255 rollbackSDNCResponse = sdncAdapterUtils.decodeXML(rollbackSDNCResponse)
\r
256 rollbackSDNCResponse = rollbackSDNCResponse.replace("&", "&").replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
\r
257 if (rollbackSDNCReturnCode == "200") {
\r
258 if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
\r
259 rollbackSDNCReturnInnerCode = utils.getNodeText1(rollbackSDNCResponse, "response-code")
\r
260 if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
\r
261 rollbackSdncErrorMessages = " + SNDC assign rollback completed."
\r
263 rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
\r
266 rollbackSdncErrorMessages = " + SNDC assign rollback completed."
\r
269 rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
\r
271 utils.log("DEBUG", " SDNC assign rollback Code - " + rollbackSDNCReturnCode, isDebugEnabled)
\r
272 utils.log("DEBUG", " SDNC assign rollback Response - " + rollbackSDNCResponse, isDebugEnabled)
\r
275 // validate SDNC activate rollback response
\r
276 String rollbackActivateSdncErrorMessages = ""
\r
277 String rollbackActivateSDNCReturnCode = "200"
\r
278 if (execution.getVariable(Prefix + "rollbackActivateSDNCRequest") != null) {
\r
279 rollbackActivateSDNCReturnCode = execution.getVariable(Prefix + "rollbackActivateSDNCReturnCode")
\r
280 String rollbackActivateSDNCResponse = execution.getVariable(Prefix + "rollbackActivateSDNCResponse")
\r
281 String rollbackActivateSDNCReturnInnerCode = ""
\r
282 rollbackActivateSDNCResponse = sdncAdapterUtils.decodeXML(rollbackActivateSDNCResponse)
\r
283 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse.replace("&", "&").replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
\r
284 if (rollbackActivateSDNCReturnCode == "200") {
\r
285 if (utils.nodeExists(rollbackActivateSDNCResponse, "response-code")) {
\r
286 rollbackActivateSDNCReturnInnerCode = utils.getNodeText1(rollbackActivateSDNCResponse, "response-code")
\r
287 if (rollbackActivateSDNCReturnInnerCode == "200" || rollbackActivateSDNCReturnInnerCode == "" || rollbackActivateSDNCReturnInnerCode == "0") {
\r
288 rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
\r
290 rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
\r
293 rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
\r
296 rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
\r
298 utils.log("DEBUG", " SDNC activate rollback Code - " + rollbackActivateSDNCReturnCode, isDebugEnabled)
\r
299 utils.log("DEBUG", " SDNC activate rollback Response - " + rollbackActivateSDNCResponse, isDebugEnabled)
\r
303 String statusMessage = ""
\r
304 int errorCode = 7000
\r
305 utils.log("DEBUG", "*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"), isDebugEnabled)
\r
306 if (execution.getVariable(Prefix + "fullRollback") == false) {
\r
307 // original WorkflowException,
\r
308 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException")
\r
310 // rollback due to failure in DoCreate - Partial rollback
\r
311 statusMessage = wfe.getErrorMessage()
\r
312 errorCode = wfe.getErrorCode()
\r
314 statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
\r
318 // set if all rolledbacks are successful
\r
319 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
\r
320 execution.setVariable("rolledBack", true)
\r
321 execution.setVariable("wasDeleted", true)
\r
324 execution.setVariable("rolledBack", false)
\r
325 execution.setVariable("wasDeleted", true)
\r
328 statusMessage = statusMessage + rollbackActivateSdncErrorMessages + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
\r
329 utils.log("DEBUG", "Final DoCreateNetworkInstanceRollback status message: " + statusMessage, isDebugEnabled)
\r
330 String processKey = getProcessKey(execution);
\r
331 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
\r
332 execution.setVariable("workflowException", exception);
\r
335 // rollback due to failures in Main flow (Macro) - Full rollback
\r
336 // WorkflowException = null
\r
337 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
\r
338 execution.setVariable("rollbackSuccessful", true)
\r
339 execution.setVariable("rollbackError", false)
\r
341 String exceptionMessage = "Network Create Rollback was not Successful. "
\r
342 utils.log("DEBUG", exceptionMessage, isDebugEnabled)
\r
343 execution.setVariable("rollbackSuccessful", false)
\r
344 execution.setVariable("rollbackError", true)
\r
345 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
\r
346 throw new BpmnError("MSOWorkflowException")
\r
352 } catch (Exception ex) {
\r
353 String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
\r
354 String exceptionMessage = " Bpmn error encountered in DoCreateNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
\r
355 utils.log("DEBUG", exceptionMessage, isDebugEnabled)
\r
356 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
\r
362 // *******************************
\r
363 // Build Error Section
\r
364 // *******************************
\r
368 public void processJavaException(DelegateExecution execution){
\r
369 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
\r
370 execution.setVariable("prefix",Prefix)
\r
373 utils.log("DEBUG", "Caught a Java Exception in " + Prefix, isDebugEnabled)
\r
374 utils.log("DEBUG", "Started processJavaException Method", isDebugEnabled)
\r
375 utils.log("DEBUG", "Variables List: " + execution.getVariables(), isDebugEnabled)
\r
376 execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
\r
377 exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
\r
379 }catch(Exception e){
\r
380 utils.log("DEBUG", "Caught Exception during processJavaException Method: " + e, isDebugEnabled)
\r
381 execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
\r
382 exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
\r
384 utils.log("DEBUG", "Completed processJavaException Method in " + Prefix, isDebugEnabled)
\r