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=========================================================
21 package org.onap.so.bpmn.infrastructure.scripts
24 import org.onap.so.bpmn.core.UrnPropertiesReader
25 import org.onap.so.bpmn.core.json.JsonUtils
26 import org.onap.so.client.HttpClient
27 import org.onap.so.client.HttpClientFactory
28 import org.onap.so.logger.MsoLogger
29 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
30 import org.onap.so.bpmn.common.scripts.ExceptionUtil
31 import org.onap.so.bpmn.common.scripts.NetworkUtils
32 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
33 import org.onap.so.bpmn.common.scripts.VidUtils
34 import org.onap.so.bpmn.core.WorkflowException
35 import org.onap.so.utils.TargetEntity
37 import javax.ws.rs.core.Response
38 import org.camunda.bpm.engine.delegate.BpmnError
39 import org.camunda.bpm.engine.delegate.DelegateExecution
42 * This groovy class supports the <class>DoCreateNetworkInstance.bpmn</class> process.
45 public class DoCreateNetworkInstanceRollback extends AbstractServiceTaskProcessor {
46 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateNetworkInstanceRollback.class);
48 String Prefix="CRENWKIR_"
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>DoCreateNetworkInstanceRollback.bpmn</class> process.
61 public InitializeProcessVariables(DelegateExecution 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 + "rollbackActivateSDNCRequest", null)
67 execution.setVariable(Prefix + "WorkflowException", null)
69 execution.setVariable(Prefix + "rollbackNetworkRequest", "")
70 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
71 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
73 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
74 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
75 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
77 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", "")
78 execution.setVariable(Prefix + "rollbackActivateSDNCResponse", "")
79 execution.setVariable(Prefix + "rollbackActivateSDNCReturnCode", "")
81 execution.setVariable(Prefix + "Success", false)
82 execution.setVariable(Prefix + "fullRollback", false)
83 execution.setVariable(Prefix + "networkId", "")
84 execution.setVariable(Prefix + "urlRollbackPoNetwork", "")
88 // **************************************************
89 // Pre or Prepare Request Section
90 // **************************************************
92 * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
95 public void preProcessRequest (DelegateExecution execution) {
96 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
97 execution.setVariable("prefix",Prefix)
99 msoLogger.trace("Inside preProcessRequest() of " + className + ".groovy")
102 // initialize flow variables
103 InitializeProcessVariables(execution)
105 // GET Incoming request/variables
106 String rollbackNetworkRequest = null
107 String rollbackSDNCRequest = null
108 String rollbackActivateSDNCRequest = null
111 Map<String, String> rollbackData = execution.getVariable("rollbackData")
112 if (rollbackData != null && rollbackData instanceof Map) {
114 if(rollbackData.containsKey("rollbackSDNCRequest")) {
115 rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
118 if(rollbackData.containsKey("rollbackNetworkRequest")) {
119 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
122 if(rollbackData.containsKey("rollbackActivateSDNCRequest")) {
123 rollbackActivateSDNCRequest = rollbackData["rollbackActivateSDNCRequest"]
128 execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
129 execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
130 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", rollbackActivateSDNCRequest)
131 msoLogger.debug("'rollbackData': " + '\n' + execution.getVariable("rollbackData"))
133 String sdncVersion = execution.getVariable("sdncVersion")
134 msoLogger.debug("sdncVersion? : " + sdncVersion)
136 // PO Authorization Info / headers Authorization=
137 String basicAuthValuePO = UrnPropertiesReader.getVariable("mso.adapters.po.auth",execution)
139 def encodedString = utils.getBasicAuth(basicAuthValuePO, UrnPropertiesReader.getVariable("mso.msoKey", execution))
140 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
141 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
143 } catch (IOException ex) {
144 String exceptionMessage = "Exception Encountered in DoCreateNetworkInstance, PreProcessRequest() - "
145 String dataErrorMessage = exceptionMessage + " Unable to encode PO/SDNC user/password string - " + ex.getMessage()
146 msoLogger.debug(dataErrorMessage )
147 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
150 if (execution.getVariable("SavedWorkflowException1") != null) {
151 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
153 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
155 msoLogger.debug("*** WorkflowException : " + execution.getVariable(Prefix + "WorkflowException"))
156 if(execution.getVariable(Prefix + "WorkflowException") != null) {
157 // called by: DoCreateNetworkInstance, partial rollback
158 execution.setVariable(Prefix + "fullRollback", false)
161 // called by: Macro - Full Rollback, WorkflowException = null
162 execution.setVariable(Prefix + "fullRollback", true)
165 msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
168 } catch (BpmnError e) {
171 } catch (Exception ex) {
173 String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
174 msoLogger.debug(exceptionMessage)
175 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
181 public void callPONetworkAdapter (DelegateExecution execution) {
182 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
183 execution.setVariable("prefix",Prefix)
185 msoLogger.trace("Inside callPONetworkAdapter() of " + className + "")
188 String poUrl = UrnPropertiesReader.getVariable("mso.adapters.network.rest.endpoint",execution)
189 String rollbackSDNCRequest = execution.getVariable(Prefix + "rollbackSDNCRequest")
190 String networkId = utils.getNodeText(rollbackSDNCRequest, "network-id")
192 String rollbackNetworkRequest = execution.getVariable(Prefix + "rollbackNetworkRequest")
194 String urlRollbackPoNetwork = poUrl+ "/" + networkId + "/rollback"
195 msoLogger.debug("'urlRollbackPoNetwork': " + urlRollbackPoNetwork)
196 execution.setVariable(Prefix + "urlRollbackPoNetwork", urlRollbackPoNetwork)
198 URL url = new URL(urlRollbackPoNetwork)
199 HttpClient httpClient = new HttpClientFactory().newXmlClient(url, TargetEntity.OPENSTACK_ADAPTER)
200 httpClient.addAdditionalHeader("Authorization", execution.getVariable("BasicAuthHeaderValuePO"))
201 Response response = httpClient.delete(rollbackNetworkRequest)
203 execution.setVariable(Prefix + "rollbackNetworkReturnCode", response.getStatus())
205 msoLogger.debug(" Network Adapter rollback responseCode: " + response.getStatus())
208 } catch (Exception ex) {
209 String exceptionMessage = "Exception Encountered in callPONetworkAdapter() of DoCreateNetworkInstanceRollback flow - " + ex.getMessage()
210 msoLogger.debug(exceptionMessage)
211 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
217 public void validateRollbackResponses (DelegateExecution execution) {
218 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
219 execution.setVariable("prefix",Prefix)
221 msoLogger.trace("Inside validateRollbackResponses() of DoCreateNetworkInstanceRollback")
224 // validate PO network rollback response
225 String rollbackNetworkErrorMessages = ""
226 String rollbackNetworkReturnCode = "200"
227 if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
228 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
229 msoLogger.debug(" NetworkRollback Code - " + rollbackNetworkReturnCode)
230 if (rollbackNetworkReturnCode != "200") {
231 rollbackNetworkErrorMessages = " + PO Network rollback failed. "
233 rollbackNetworkErrorMessages = " + PO Network rollback completed."
237 // validate SDNC rollback response
238 String rollbackSdncErrorMessages = ""
239 String rollbackSDNCReturnCode = "200"
240 if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
241 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
242 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
243 String rollbackSDNCReturnInnerCode = ""
244 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
245 rollbackSDNCResponse = rollbackSDNCResponse
246 rollbackSDNCResponse = rollbackSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
247 if (rollbackSDNCReturnCode == "200") {
248 if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
249 rollbackSDNCReturnInnerCode = utils.getNodeText(rollbackSDNCResponse, "response-code")
250 if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
251 rollbackSdncErrorMessages = " + SNDC assign rollback completed."
253 rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
256 rollbackSdncErrorMessages = " + SNDC assign rollback completed."
259 rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
261 msoLogger.debug(" SDNC assign rollback Code - " + rollbackSDNCReturnCode)
262 msoLogger.debug(" SDNC assign rollback Response - " + rollbackSDNCResponse)
265 // validate SDNC activate rollback response
266 String rollbackActivateSdncErrorMessages = ""
267 String rollbackActivateSDNCReturnCode = "200"
268 if (execution.getVariable(Prefix + "rollbackActivateSDNCRequest") != null) {
269 rollbackActivateSDNCReturnCode = execution.getVariable(Prefix + "rollbackActivateSDNCReturnCode")
270 String rollbackActivateSDNCResponse = execution.getVariable(Prefix + "rollbackActivateSDNCResponse")
271 String rollbackActivateSDNCReturnInnerCode = ""
272 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse
273 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
274 if (rollbackActivateSDNCReturnCode == "200") {
275 if (utils.nodeExists(rollbackActivateSDNCResponse, "response-code")) {
276 rollbackActivateSDNCReturnInnerCode = utils.getNodeText(rollbackActivateSDNCResponse, "response-code")
277 if (rollbackActivateSDNCReturnInnerCode == "200" || rollbackActivateSDNCReturnInnerCode == "" || rollbackActivateSDNCReturnInnerCode == "0") {
278 rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
280 rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
283 rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
286 rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
288 msoLogger.debug(" SDNC activate rollback Code - " + rollbackActivateSDNCReturnCode)
289 msoLogger.debug(" SDNC activate rollback Response - " + rollbackActivateSDNCResponse)
293 String statusMessage = ""
295 msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
296 if (execution.getVariable(Prefix + "fullRollback") == false) {
297 // original WorkflowException,
298 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException")
300 // rollback due to failure in DoCreate - Partial rollback
301 statusMessage = wfe.getErrorMessage()
302 errorCode = wfe.getErrorCode()
304 statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
308 // set if all rolledbacks are successful
309 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
310 execution.setVariable("rolledBack", true)
311 execution.setVariable("wasDeleted", true)
314 execution.setVariable("rolledBack", false)
315 execution.setVariable("wasDeleted", true)
318 statusMessage = statusMessage + rollbackActivateSdncErrorMessages + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
319 msoLogger.debug("Final DoCreateNetworkInstanceRollback status message: " + statusMessage)
320 String processKey = getProcessKey(execution);
321 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
322 execution.setVariable("workflowException", exception);
325 // rollback due to failures in Main flow (Macro) - Full rollback
326 // WorkflowException = null
327 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
328 execution.setVariable("rollbackSuccessful", true)
329 execution.setVariable("rollbackError", false)
331 String exceptionMessage = "Network Create Rollback was not Successful. "
332 msoLogger.debug(exceptionMessage)
333 execution.setVariable("rollbackSuccessful", false)
334 execution.setVariable("rollbackError", true)
335 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
336 throw new BpmnError("MSOWorkflowException")
342 } catch (Exception ex) {
343 String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
344 String exceptionMessage = " Bpmn error encountered in DoCreateNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
345 msoLogger.debug(exceptionMessage)
346 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
352 // *******************************
353 // Build Error Section
354 // *******************************
358 public void processJavaException(DelegateExecution execution){
359 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
360 execution.setVariable("prefix",Prefix)
363 msoLogger.debug("Caught a Java Exception in " + Prefix)
364 msoLogger.debug("Started processJavaException Method")
365 msoLogger.debug("Variables List: " + execution.getVariables())
366 execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
367 exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
370 msoLogger.debug("Caught Exception during processJavaException Method: " + e)
371 execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
372 exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
374 msoLogger.debug("Completed processJavaException Method in " + Prefix)