2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.bpmn.infrastructure.scripts
26 import org.onap.so.bpmn.core.UrnPropertiesReader
27 import org.onap.so.bpmn.core.json.JsonUtils
28 import org.onap.so.client.HttpClient
29 import org.onap.so.client.HttpClientFactory
30 import org.slf4j.Logger
31 import org.slf4j.LoggerFactory
32 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
33 import org.onap.so.bpmn.common.scripts.ExceptionUtil
34 import org.onap.so.bpmn.common.scripts.NetworkUtils
35 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
36 import org.onap.so.bpmn.common.scripts.VidUtils
37 import org.onap.so.bpmn.core.WorkflowException
38 import org.onap.logging.filter.base.ONAPComponents;
40 import javax.ws.rs.core.Response
41 import org.camunda.bpm.engine.delegate.BpmnError
42 import org.camunda.bpm.engine.delegate.DelegateExecution
45 * This groovy class supports the <class>DoCreateNetworkInstance.bpmn</class> process.
48 public class DoCreateNetworkInstanceRollback extends AbstractServiceTaskProcessor {
49 private static final Logger logger = LoggerFactory.getLogger( DoCreateNetworkInstanceRollback.class);
51 String Prefix="CRENWKIR_"
52 ExceptionUtil exceptionUtil = new ExceptionUtil()
53 JsonUtils jsonUtil = new JsonUtils()
54 VidUtils vidUtils = new VidUtils(this)
55 NetworkUtils networkUtils = new NetworkUtils()
56 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
58 def className = getClass().getSimpleName()
61 * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
64 public InitializeProcessVariables(DelegateExecution execution){
65 /* Initialize all the process variables in this block */
67 execution.setVariable(Prefix + "rollbackNetworkRequest", null)
68 execution.setVariable(Prefix + "rollbackSDNCRequest", null)
69 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", null)
70 execution.setVariable(Prefix + "WorkflowException", null)
72 execution.setVariable(Prefix + "rollbackNetworkRequest", "")
73 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
74 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
76 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
77 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
78 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
80 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", "")
81 execution.setVariable(Prefix + "rollbackActivateSDNCResponse", "")
82 execution.setVariable(Prefix + "rollbackActivateSDNCReturnCode", "")
84 execution.setVariable(Prefix + "Success", false)
85 execution.setVariable(Prefix + "fullRollback", false)
86 execution.setVariable(Prefix + "networkId", "")
87 execution.setVariable(Prefix + "urlRollbackPoNetwork", "")
90 // **************************************************
91 // Pre or Prepare Request Section
92 // **************************************************
94 * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
97 public void preProcessRequest (DelegateExecution execution) {
98 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
99 execution.setVariable("prefix",Prefix)
101 logger.trace("Inside preProcessRequest() of " + className + ".groovy")
104 // initialize flow variables
105 InitializeProcessVariables(execution)
107 // GET Incoming request/variables
108 String rollbackNetworkRequest = null
109 String rollbackSDNCRequest = null
110 String rollbackActivateSDNCRequest = null
113 Map<String, String> rollbackData = execution.getVariable("rollbackData")
114 if (rollbackData != null && rollbackData instanceof Map) {
116 if(rollbackData.containsKey("rollbackSDNCRequest")) {
117 rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
120 if(rollbackData.containsKey("rollbackNetworkRequest")) {
121 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
124 if(rollbackData.containsKey("rollbackActivateSDNCRequest")) {
125 rollbackActivateSDNCRequest = rollbackData["rollbackActivateSDNCRequest"]
130 execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
131 execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
132 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", rollbackActivateSDNCRequest)
133 logger.debug("'rollbackData': " + '\n' + execution.getVariable("rollbackData"))
135 String sdncVersion = execution.getVariable("sdncVersion")
136 logger.debug("sdncVersion? : " + sdncVersion)
138 // PO Authorization Info / headers Authorization=
139 String basicAuthValuePO = UrnPropertiesReader.getVariable("mso.adapters.po.auth",execution)
141 def encodedString = utils.getBasicAuth(basicAuthValuePO, UrnPropertiesReader.getVariable("mso.msoKey", execution))
142 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
143 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
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 logger.debug(dataErrorMessage )
149 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
152 if (execution.getVariable("SavedWorkflowException1") != null) {
153 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
155 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
157 logger.debug("*** WorkflowException : " + execution.getVariable(Prefix + "WorkflowException"))
158 if(execution.getVariable(Prefix + "WorkflowException") != null) {
159 // called by: DoCreateNetworkInstance, partial rollback
160 execution.setVariable(Prefix + "fullRollback", false)
163 // called by: Macro - Full Rollback, WorkflowException = null
164 execution.setVariable(Prefix + "fullRollback", true)
167 logger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
170 } catch (BpmnError e) {
173 } catch (Exception ex) {
175 String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
176 logger.debug(exceptionMessage)
177 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
183 public void setNetworkAdapterResponseCode (DelegateExecution execution) {
184 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
185 execution.setVariable("prefix",Prefix)
188 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "200")
190 } catch (Exception ex) {
191 String exceptionMessage = "Exception Encountered in callPONetworkAdapter() of DoCreateNetworkInstanceRollback flow - " + ex.getMessage()
192 logger.debug(exceptionMessage)
193 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
199 public void validateRollbackResponses (DelegateExecution execution) {
200 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
201 execution.setVariable("prefix",Prefix)
203 logger.trace("Inside validateRollbackResponses() of DoCreateNetworkInstanceRollback")
206 // validate PO network rollback response
207 String rollbackNetworkErrorMessages = ""
208 String rollbackNetworkReturnCode = "200"
209 if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
210 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
211 logger.debug(" NetworkRollback Code - " + rollbackNetworkReturnCode)
212 if (rollbackNetworkReturnCode != "200") {
213 rollbackNetworkErrorMessages = " + PO Network rollback failed. "
215 rollbackNetworkErrorMessages = " + PO Network rollback completed."
219 // validate SDNC rollback response
220 String rollbackSdncErrorMessages = ""
221 String rollbackSDNCReturnCode = "200"
222 if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
223 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
224 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
225 String rollbackSDNCReturnInnerCode = ""
226 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
227 rollbackSDNCResponse = rollbackSDNCResponse
228 rollbackSDNCResponse = rollbackSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
229 if (rollbackSDNCReturnCode == "200") {
230 if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
231 rollbackSDNCReturnInnerCode = utils.getNodeText(rollbackSDNCResponse, "response-code")
232 if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
233 rollbackSdncErrorMessages = " + SNDC assign rollback completed."
235 rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
238 rollbackSdncErrorMessages = " + SNDC assign rollback completed."
241 rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
243 logger.debug(" SDNC assign rollback Code - " + rollbackSDNCReturnCode)
244 logger.debug(" SDNC assign rollback Response - " + rollbackSDNCResponse)
247 // validate SDNC activate rollback response
248 String rollbackActivateSdncErrorMessages = ""
249 String rollbackActivateSDNCReturnCode = "200"
250 if (execution.getVariable(Prefix + "rollbackActivateSDNCRequest") != null) {
251 rollbackActivateSDNCReturnCode = execution.getVariable(Prefix + "rollbackActivateSDNCReturnCode")
252 String rollbackActivateSDNCResponse = execution.getVariable(Prefix + "rollbackActivateSDNCResponse")
253 String rollbackActivateSDNCReturnInnerCode = ""
254 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse
255 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
256 if (rollbackActivateSDNCReturnCode == "200") {
257 if (utils.nodeExists(rollbackActivateSDNCResponse, "response-code")) {
258 rollbackActivateSDNCReturnInnerCode = utils.getNodeText(rollbackActivateSDNCResponse, "response-code")
259 if (rollbackActivateSDNCReturnInnerCode == "200" || rollbackActivateSDNCReturnInnerCode == "" || rollbackActivateSDNCReturnInnerCode == "0") {
260 rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
262 rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
265 rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
268 rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
270 logger.debug(" SDNC activate rollback Code - " + rollbackActivateSDNCReturnCode)
271 logger.debug(" SDNC activate rollback Response - " + rollbackActivateSDNCResponse)
275 String statusMessage = ""
277 logger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
278 if (execution.getVariable(Prefix + "fullRollback") == false) {
279 // original WorkflowException,
280 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException")
282 // rollback due to failure in DoCreate - Partial rollback
283 statusMessage = wfe.getErrorMessage()
284 errorCode = wfe.getErrorCode()
286 statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
290 // set if all rolledbacks are successful
291 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
292 execution.setVariable("rolledBack", true)
293 execution.setVariable("wasDeleted", true)
296 execution.setVariable("rolledBack", false)
297 execution.setVariable("wasDeleted", true)
300 statusMessage = statusMessage + rollbackActivateSdncErrorMessages + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
301 logger.debug("Final DoCreateNetworkInstanceRollback status message: " + statusMessage)
302 String processKey = getProcessKey(execution);
303 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
304 execution.setVariable("workflowException", exception);
307 // rollback due to failures in Main flow (Macro) - Full rollback
308 // WorkflowException = null
309 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
310 execution.setVariable("rollbackSuccessful", true)
311 execution.setVariable("rollbackError", false)
313 String exceptionMessage = "Network Create Rollback was not Successful. "
314 logger.debug(exceptionMessage)
315 execution.setVariable("rollbackSuccessful", false)
316 execution.setVariable("rollbackError", true)
317 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
318 throw new BpmnError("MSOWorkflowException")
324 } catch (Exception ex) {
325 String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
326 String exceptionMessage = " Bpmn error encountered in DoCreateNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
327 logger.debug(exceptionMessage)
328 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
334 // *******************************
335 // Build Error Section
336 // *******************************
340 public void processJavaException(DelegateExecution execution){
341 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
342 execution.setVariable("prefix",Prefix)
345 logger.debug("Caught a Java Exception in " + Prefix)
346 logger.debug("Started processJavaException Method")
347 logger.debug("Variables List: " + execution.getVariables())
348 execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
349 exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
352 logger.debug("Caught Exception during processJavaException Method: " + e)
353 execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
354 exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
356 logger.debug("Completed processJavaException Method in " + Prefix)