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
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.logger.MsoLogger
30 import org.onap.so.logger.MessageEnum
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.so.utils.TargetEntity
40 import java.util.UUID;
41 import javax.ws.rs.core.Response
42 import org.camunda.bpm.engine.delegate.BpmnError
43 import org.camunda.bpm.engine.delegate.DelegateExecution
44 import org.apache.commons.lang3.*
45 import org.apache.commons.codec.binary.Base64;
46 import org.springframework.web.util.UriUtils
49 * This groovy class supports the <class>DoCreateNetworkInstance.bpmn</class> process.
52 public class DoCreateNetworkInstanceRollback extends AbstractServiceTaskProcessor {
53 private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DoCreateNetworkInstanceRollback.class);
55 String Prefix="CRENWKIR_"
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>DoCreateNetworkInstanceRollback.bpmn</class> process.
68 public InitializeProcessVariables(DelegateExecution execution){
69 /* Initialize all the process variables in this block */
71 execution.setVariable(Prefix + "rollbackNetworkRequest", null)
72 execution.setVariable(Prefix + "rollbackSDNCRequest", null)
73 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", null)
74 execution.setVariable(Prefix + "WorkflowException", null)
76 execution.setVariable(Prefix + "rollbackNetworkRequest", "")
77 execution.setVariable(Prefix + "rollbackNetworkResponse", "")
78 execution.setVariable(Prefix + "rollbackNetworkReturnCode", "")
80 execution.setVariable(Prefix + "rollbackSDNCRequest", "")
81 execution.setVariable(Prefix + "rollbackSDNCResponse", "")
82 execution.setVariable(Prefix + "rollbackSDNCReturnCode", "")
84 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", "")
85 execution.setVariable(Prefix + "rollbackActivateSDNCResponse", "")
86 execution.setVariable(Prefix + "rollbackActivateSDNCReturnCode", "")
88 execution.setVariable(Prefix + "Success", false)
89 execution.setVariable(Prefix + "fullRollback", false)
90 execution.setVariable(Prefix + "networkId", "")
91 execution.setVariable(Prefix + "urlRollbackPoNetwork", "")
95 // **************************************************
96 // Pre or Prepare Request Section
97 // **************************************************
99 * This method is executed during the preProcessRequest task of the <class>DoCreateNetworkInstanceRollback.bpmn</class> process.
102 public void preProcessRequest (DelegateExecution execution) {
103 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
104 execution.setVariable("prefix",Prefix)
106 msoLogger.trace("Inside preProcessRequest() of " + className + ".groovy")
109 // initialize flow variables
110 InitializeProcessVariables(execution)
112 // GET Incoming request/variables
113 String rollbackNetworkRequest = null
114 String rollbackSDNCRequest = null
115 String rollbackActivateSDNCRequest = null
118 Map<String, String> rollbackData = execution.getVariable("rollbackData")
119 if (rollbackData != null && rollbackData instanceof Map) {
121 if(rollbackData.containsKey("rollbackSDNCRequest")) {
122 rollbackSDNCRequest = rollbackData["rollbackSDNCRequest"]
125 if(rollbackData.containsKey("rollbackNetworkRequest")) {
126 rollbackNetworkRequest = rollbackData["rollbackNetworkRequest"]
129 if(rollbackData.containsKey("rollbackActivateSDNCRequest")) {
130 rollbackActivateSDNCRequest = rollbackData["rollbackActivateSDNCRequest"]
135 execution.setVariable(Prefix + "rollbackNetworkRequest", rollbackNetworkRequest)
136 execution.setVariable(Prefix + "rollbackSDNCRequest", rollbackSDNCRequest)
137 execution.setVariable(Prefix + "rollbackActivateSDNCRequest", rollbackActivateSDNCRequest)
138 msoLogger.debug("'rollbackData': " + '\n' + execution.getVariable("rollbackData"))
140 String sdncVersion = execution.getVariable("sdncVersion")
141 msoLogger.debug("sdncVersion? : " + sdncVersion)
143 // PO Authorization Info / headers Authorization=
144 String basicAuthValuePO = UrnPropertiesReader.getVariable("mso.adapters.po.auth",execution)
146 def encodedString = utils.getBasicAuth(basicAuthValuePO, UrnPropertiesReader.getVariable("mso.msoKey", execution))
147 execution.setVariable("BasicAuthHeaderValuePO",encodedString)
148 execution.setVariable("BasicAuthHeaderValueSDNC", encodedString)
150 } catch (IOException ex) {
151 String exceptionMessage = "Exception Encountered in DoCreateNetworkInstance, PreProcessRequest() - "
152 String dataErrorMessage = exceptionMessage + " Unable to encode PO/SDNC user/password string - " + ex.getMessage()
153 msoLogger.debug(dataErrorMessage )
154 exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage)
157 if (execution.getVariable("SavedWorkflowException1") != null) {
158 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("SavedWorkflowException1"))
160 execution.setVariable(Prefix + "WorkflowException", execution.getVariable("WorkflowException"))
162 msoLogger.debug("*** WorkflowException : " + execution.getVariable(Prefix + "WorkflowException"))
163 if(execution.getVariable(Prefix + "WorkflowException") != null) {
164 // called by: DoCreateNetworkInstance, partial rollback
165 execution.setVariable(Prefix + "fullRollback", false)
168 // called by: Macro - Full Rollback, WorkflowException = null
169 execution.setVariable(Prefix + "fullRollback", true)
172 msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
175 } catch (BpmnError e) {
178 } catch (Exception ex) {
180 String exceptionMessage = "Exception Encountered in PreProcessRequest() of " + className + ".groovy ***** : " + ex.getMessage()
181 msoLogger.debug(exceptionMessage)
182 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
188 public void callPONetworkAdapter (DelegateExecution execution) {
189 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
190 execution.setVariable("prefix",Prefix)
192 msoLogger.trace("Inside callPONetworkAdapter() of " + className + "")
195 String poUrl = UrnPropertiesReader.getVariable("mso.adapters.network.rest.endpoint",execution)
196 String rollbackSDNCRequest = execution.getVariable(Prefix + "rollbackSDNCRequest")
197 String networkId = utils.getNodeText(rollbackSDNCRequest, "network-id")
199 String rollbackNetworkRequest = execution.getVariable(Prefix + "rollbackNetworkRequest")
201 String urlRollbackPoNetwork = poUrl+ "/" + networkId + "/rollback"
202 msoLogger.debug("'urlRollbackPoNetwork': " + urlRollbackPoNetwork)
203 execution.setVariable(Prefix + "urlRollbackPoNetwork", urlRollbackPoNetwork)
205 URL url = new URL(urlRollbackPoNetwork)
206 HttpClient httpClient = new HttpClient(url, "application/xml", TargetEntity.OPENSTACK_ADAPTER)
207 httpClient.addAdditionalHeader("Authorization", execution.getVariable("BasicAuthHeaderValuePO"))
208 Response response = httpClient.delete(rollbackNetworkRequest)
210 execution.setVariable(Prefix + "rollbackNetworkReturnCode", response.getStatus())
212 msoLogger.debug(" Network Adapter rollback responseCode: " + response.getStatus())
215 } catch (Exception ex) {
216 String exceptionMessage = "Exception Encountered in callPONetworkAdapter() of DoCreateNetworkInstanceRollback flow - " + ex.getMessage()
217 msoLogger.debug(exceptionMessage)
218 exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
224 public void validateRollbackResponses (DelegateExecution execution) {
225 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
226 execution.setVariable("prefix",Prefix)
228 msoLogger.trace("Inside validateRollbackResponses() of DoCreateNetworkInstanceRollback")
231 // validate PO network rollback response
232 String rollbackNetworkErrorMessages = ""
233 String rollbackNetworkReturnCode = "200"
234 if (execution.getVariable(Prefix + "rollbackNetworkRequest") != null) {
235 rollbackNetworkReturnCode = execution.getVariable(Prefix + "rollbackNetworkReturnCode")
236 msoLogger.debug(" NetworkRollback Code - " + rollbackNetworkReturnCode)
237 if (rollbackNetworkReturnCode != "200") {
238 rollbackNetworkErrorMessages = " + PO Network rollback failed. "
240 rollbackNetworkErrorMessages = " + PO Network rollback completed."
244 // validate SDNC rollback response
245 String rollbackSdncErrorMessages = ""
246 String rollbackSDNCReturnCode = "200"
247 if (execution.getVariable(Prefix + "rollbackSDNCRequest") != null) {
248 rollbackSDNCReturnCode = execution.getVariable(Prefix + "rollbackSDNCReturnCode")
249 String rollbackSDNCResponse = execution.getVariable(Prefix + "rollbackSDNCResponse")
250 String rollbackSDNCReturnInnerCode = ""
251 SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils(this)
252 rollbackSDNCResponse = rollbackSDNCResponse
253 rollbackSDNCResponse = rollbackSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
254 if (rollbackSDNCReturnCode == "200") {
255 if (utils.nodeExists(rollbackSDNCResponse, "response-code")) {
256 rollbackSDNCReturnInnerCode = utils.getNodeText(rollbackSDNCResponse, "response-code")
257 if (rollbackSDNCReturnInnerCode == "200" || rollbackSDNCReturnInnerCode == "" || rollbackSDNCReturnInnerCode == "0") {
258 rollbackSdncErrorMessages = " + SNDC assign rollback completed."
260 rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
263 rollbackSdncErrorMessages = " + SNDC assign rollback completed."
266 rollbackSdncErrorMessages = " + SDNC assign rollback failed. "
268 msoLogger.debug(" SDNC assign rollback Code - " + rollbackSDNCReturnCode)
269 msoLogger.debug(" SDNC assign rollback Response - " + rollbackSDNCResponse)
272 // validate SDNC activate rollback response
273 String rollbackActivateSdncErrorMessages = ""
274 String rollbackActivateSDNCReturnCode = "200"
275 if (execution.getVariable(Prefix + "rollbackActivateSDNCRequest") != null) {
276 rollbackActivateSDNCReturnCode = execution.getVariable(Prefix + "rollbackActivateSDNCReturnCode")
277 String rollbackActivateSDNCResponse = execution.getVariable(Prefix + "rollbackActivateSDNCResponse")
278 String rollbackActivateSDNCReturnInnerCode = ""
279 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse
280 rollbackActivateSDNCResponse = rollbackActivateSDNCResponse.replace('$', '').replace('<?xml version="1.0" encoding="UTF-8"?>', "")
281 if (rollbackActivateSDNCReturnCode == "200") {
282 if (utils.nodeExists(rollbackActivateSDNCResponse, "response-code")) {
283 rollbackActivateSDNCReturnInnerCode = utils.getNodeText(rollbackActivateSDNCResponse, "response-code")
284 if (rollbackActivateSDNCReturnInnerCode == "200" || rollbackActivateSDNCReturnInnerCode == "" || rollbackActivateSDNCReturnInnerCode == "0") {
285 rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
287 rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
290 rollbackActivateSdncErrorMessages = " + SNDC activate rollback completed."
293 rollbackActivateSdncErrorMessages = " + SDNC activate rollback failed. "
295 msoLogger.debug(" SDNC activate rollback Code - " + rollbackActivateSDNCReturnCode)
296 msoLogger.debug(" SDNC activate rollback Response - " + rollbackActivateSDNCResponse)
300 String statusMessage = ""
302 msoLogger.debug("*** fullRollback? : " + execution.getVariable(Prefix + "fullRollback"))
303 if (execution.getVariable(Prefix + "fullRollback") == false) {
304 // original WorkflowException,
305 WorkflowException wfe = execution.getVariable(Prefix + "WorkflowException")
307 // rollback due to failure in DoCreate - Partial rollback
308 statusMessage = wfe.getErrorMessage()
309 errorCode = wfe.getErrorCode()
311 statusMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
315 // set if all rolledbacks are successful
316 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
317 execution.setVariable("rolledBack", true)
318 execution.setVariable("wasDeleted", true)
321 execution.setVariable("rolledBack", false)
322 execution.setVariable("wasDeleted", true)
325 statusMessage = statusMessage + rollbackActivateSdncErrorMessages + rollbackNetworkErrorMessages + rollbackSdncErrorMessages
326 msoLogger.debug("Final DoCreateNetworkInstanceRollback status message: " + statusMessage)
327 String processKey = getProcessKey(execution);
328 WorkflowException exception = new WorkflowException(processKey, errorCode, statusMessage);
329 execution.setVariable("workflowException", exception);
332 // rollback due to failures in Main flow (Macro) - Full rollback
333 // WorkflowException = null
334 if (rollbackNetworkReturnCode == "200" && rollbackSDNCReturnCode == "200" && rollbackActivateSDNCReturnCode == "200") {
335 execution.setVariable("rollbackSuccessful", true)
336 execution.setVariable("rollbackError", false)
338 String exceptionMessage = "Network Create Rollback was not Successful. "
339 msoLogger.debug(exceptionMessage)
340 execution.setVariable("rollbackSuccessful", false)
341 execution.setVariable("rollbackError", true)
342 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
343 throw new BpmnError("MSOWorkflowException")
349 } catch (Exception ex) {
350 String errorMessage = "See Previous Camunda flows for cause of Error: Undetermined Exception."
351 String exceptionMessage = " Bpmn error encountered in DoCreateNetworkInstanceRollback flow. validateRollbackResponses() - " + errorMessage + ": " + ex.getMessage()
352 msoLogger.debug(exceptionMessage)
353 exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
359 // *******************************
360 // Build Error Section
361 // *******************************
365 public void processJavaException(DelegateExecution execution){
366 def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
367 execution.setVariable("prefix",Prefix)
370 msoLogger.debug("Caught a Java Exception in " + Prefix)
371 msoLogger.debug("Started processJavaException Method")
372 msoLogger.debug("Variables List: " + execution.getVariables())
373 execution.setVariable("UnexpectedError", "Caught a Java Lang Exception - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
374 exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception")
377 msoLogger.debug("Caught Exception during processJavaException Method: " + e)
378 execution.setVariable("UnexpectedError", "Exception in processJavaException method - " + Prefix) // Adding this line temporarily until this flows error handling gets updated
379 exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method" + Prefix)
381 msoLogger.debug("Completed processJavaException Method in " + Prefix)