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;
23 import groovy.xml.XmlUtil
25 import org.onap.so.bpmn.core.UrnPropertiesReader
26 import org.onap.so.bpmn.core.json.JsonUtils
27 import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor
28 import org.onap.so.bpmn.common.scripts.ExceptionUtil
29 import org.onap.so.bpmn.common.scripts.NetworkUtils
30 import org.onap.so.bpmn.common.scripts.SDNCAdapterUtils
31 import org.onap.so.bpmn.common.scripts.VidUtils
32 import org.onap.so.bpmn.core.WorkflowException
33 import org.onap.so.rest.APIResponse;
34 import org.onap.so.rest.RESTClient
35 import org.onap.so.rest.RESTConfig
37 import java.util.UUID;
39 import org.camunda.bpm.engine.delegate.BpmnError
40 import org.camunda.bpm.engine.delegate.DelegateExecution
41 import org.apache.commons.lang3.*
42 import org.apache.commons.codec.binary.Base64;
43 import org.springframework.web.util.UriUtils
45 import org.onap.so.logger.MessageEnum
46 import org.onap.so.logger.MsoLogger
49 * This groovy class supports the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
52 public class DoDeleteNetworkInstanceRollback extends AbstractServiceTaskProcessor {
53 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoDeleteNetworkInstanceRollback.class);
55 String Prefix="DELNWKIR_"
56 ExceptionUtil exceptionUtil = new ExceptionUtil()
57 JsonUtils jsonUtil = new JsonUtils()
58 VidUtils vidUtils = new VidUtils(this)
59 NetworkUtils networkUtils = new NetworkUtils()
60 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
62 def className = getClass().getSimpleName()
65 * This method is executed during the preProcessRequest task of the <class>DoDeleteNetworkInstanceRollback.bpmn</class> process.
68 public InitializeProcessVariables(DelegateExecution execution){
69 /* Initialize all the process variables in this block */
71 execution.setVariable(Prefix + "WorkflowException", null)
73 execution.setVariable(Prefix + "rollbackDeactivateSDNCRequest", null)
74 execution.setVariable(Prefix + "rollbackDeactivateSDNCResponse", "")
75 execution.setVariable(Prefix + "rollbackDeactivateSDNCReturnCode", "")
77 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
78 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
79 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
81 execution.setVariable(Prefix + "rollbackNetworkRequest", null)
82 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
83 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
85 execution.setVariable(Prefix + "Success", false)
86 execution.setVariable(Prefix + "fullRollback", false)
90 // **************************************************
91 // Pre or Prepare Request Section
92 // **************************************************
94 * This method is executed during the preProcessRequest task of the <class>DoDeleteNetworkInstanceRollback.bpmn</class> process.
97 public void preProcessRequest (DelegateExecution execution) {
99 execution.setVariable("prefix",Prefix)
101 msoLogger.trace("Inside preProcessRequest() of " + className + ".groovy ")
104 // initialize flow variables
105 InitializeProcessVariables(execution)
107 // GET Incoming request/variables
108 String rollbackDeactivateSDNCRequest = null
109 String rollbackSDNCRequest = null
110 String rollbackNetworkRequest = null
112 Map<String, String> rollbackData = execution.getVariable("rollbackData")
113 if (rollbackData != null && rollbackData instanceof Map) {
115 if(rollbackData.containsKey("rollbackDeactivateSDNCRequest")) {
116 rollbackDeactivateSDNCRequest = rollbackData["rollbackDeactivateSDNCRequest"]
119 if(rollbackData.containsKey("rollbackSDNCRequest")) {
120 rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
123 if(rollbackData.containsKey("rollbackNetworkRequest")) {
124 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
128 execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
129 execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
130 execution.setVariable(Prefix + "rollbackDeactivateSDNCRequest", rollbackDeactivateSDNCRequest)
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)
166 msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
168 } catch (BpmnError e) {
171 } catch (Exception ex) {
172 sendSyncError(execution)
174 String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
175 msoLogger.debug(exceptionMessage)
176 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
182 public void validateRollbackResponses (DelegateExecution execution) {
184 execution.setVariable("prefix",Prefix)
186 msoLogger.trace("Inside validateRollbackResponses() of DoDeleteNetworkInstanceRollback ")
190 // validate SDNC activate response
191 String rollbackDeactivateSDNCMessages = ""
192 String rollbackDeactivateSDNCReturnCode = "200"
193 if (execution.getVariable(Prefix + "rollbackDeactivateSDNCRequest") != null) {
194 rollbackDeactivateSDNCReturnCode = execution.getVariable(Prefix + "rollbackDeactivateSDNCReturnCode")
195 String rollbackDeactivateSDNCResponse = execution.getVariable(Prefix + "rollbackDeactivateSDNCResponse")
196 String rollbackDeactivateSDNCReturnInnerCode = ""
197 rollbackDeactivateSDNCResponse = rollbackDeactivateSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
198 if (rollbackDeactivateSDNCReturnCode == "200") {
199 if (utils.nodeExists(rollbackDeactivateSDNCResponse, "response-code")) {
200 rollbackDeactivateSDNCReturnInnerCode = utils.getNodeText(rollbackDeactivateSDNCResponse, "response-code")
201 if (rollbackDeactivateSDNCReturnInnerCode == "200" || rollbackDeactivateSDNCReturnInnerCode == "" || rollbackDeactivateSDNCReturnInnerCode == "0") {
202 rollbackDeactivateSDNCMessages = " + SNDC deactivate rollback completed."
204 rollbackDeactivateSDNCMessages = " + SDNC deactivate rollback failed. "
207 rollbackDeactivateSDNCMessages = " + SNDC deactivate rollback completed."
210 rollbackDeactivateSDNCMessages = " + SDNC deactivate rollback failed. "
212 msoLogger.debug(" SDNC deactivate rollback Code - " + rollbackDeactivateSDNCReturnCode)
213 msoLogger.debug(" SDNC deactivate rollback Response - " + rollbackDeactivateSDNCResponse)
216 // validate SDNC rollback response
217 String rollbackSdncErrorMessages = ""
218 String rollbackSDNCReturnCode = "200"
219 if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
220 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
221 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
222 String rollbackSDNCReturnInnerCode = ""
223 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
224 rollbackSDNCResponse = rollbackSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
225 if (rollbackSDNCReturnCode == "200") {
226 if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
227 rollbackSDNCReturnInnerCode = utils.getNodeText(rollbackSDNCResponse, "response-code")
228 if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
229 rollbackSdncErrorMessages = " + SNDC unassign rollback completed."
231 rollbackSdncErrorMessages = " + SDNC unassign rollback failed. "
234 rollbackSdncErrorMessages = " + SNDC unassign rollback completed."
237 rollbackSdncErrorMessages = " + SDNC unassign rollback failed. "
239 msoLogger.debug(" SDNC assign rollback Code - " + rollbackSDNCReturnCode)
240 msoLogger.debug(" SDNC assign rollback Response - " + rollbackSDNCResponse)
243 // validate PO network rollback response
244 String rollbackNetworkErrorMessages = ""
245 String rollbackNetworkReturnCode = "200"
246 if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
247 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
248 String rollbackNetworkResponse = execution.getVariable(Prefix + "rollbackNetworkResponse")
249 if (rollbackNetworkReturnCode != "200") {
250 rollbackNetworkErrorMessages = " + PO Network rollback failed. "
252 rollbackNetworkErrorMessages = " + PO Network rollback completed."
255 msoLogger.debug(" NetworkRollback Code - " + rollbackNetworkReturnCode)
256 msoLogger.debug(" NetworkRollback Response - " + rollbackNetworkResponse)
259 String statusMessage = ""
261 msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
262 if (execution.getVariable(Prefix + "fullRollback") == false) {
263 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException") // original WorkflowException
265 statusMessage = wfe.getErrorMessage()
266 errorCode = wfe.getErrorCode()
268 statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
272 // set if all rolledbacks are successful
273 if (rollbackDeactivateSDNCReturnCode == "200" && rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200") {
274 execution.setVariable("rolledBack", true)
275 execution.setVariable("wasDeleted", true)
278 execution.setVariable("rolledBack", false)
279 execution.setVariable("wasDeleted", true)
282 statusMessage = statusMessage + rollbackDeactivateSDNCMessages + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
283 msoLogger.debug("Final DoDeleteNetworkInstanceRollback status message: " + statusMessage)
284 String processKey = getProcessKey(execution);
285 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
286 execution.setVariable("workflowException", exception);
289 // rollback due to failures in Main flow (Macro or a-ala-carte) - Full rollback
290 if (rollbackDeactivateSDNCReturnCode == "200" && rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200") {
291 execution.setVariable("rollbackSuccessful", true)
292 execution.setVariable("rollbackError", false)
294 String exceptionMessage = "Network Delete Rollback was not Successful. "
295 msoLogger.debug(exceptionMessage)
296 execution.setVariable("rollbackSuccessful", false)
297 execution.setVariable("rollbackError", true)
298 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
299 throw new BpmnError("MSOWorkflowException")
303 } catch (Exception ex) {
304 String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
305 String exceptionMessage = " Bpmn error encountered in DoDeleteNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
306 msoLogger.debug(exceptionMessage)
307 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
313 // *******************************
314 // Build Error Section
315 // *******************************
319 public void processJavaException(DelegateExecution execution){
321 execution.setVariable("prefix",Prefix)
324 msoLogger.debug("Caught a Java Exception in " + Prefix)
325 msoLogger.debug("Started processJavaException Method")
326 msoLogger.debug("Variables List: " + execution.getVariables())
327 execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
328 exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
331 msoLogger.debug("Caught Exception during processJavaException Method: " + e)
332 execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
333 exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
335 msoLogger.debug("Completed processJavaException Method in " + Prefix)