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