/*- * ============LICENSE_START======================================================= * ONAP - SO * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.onap.so.bpmn.infrastructure.scripts; import org.apache.commons.lang3.* import org.camunda.bpm.engine.delegate.DelegateExecution import org.onap.so.bpmn.common.scripts.AbstractServiceTaskProcessor import org.onap.so.bpmn.common.scripts.ExceptionUtil import org.onap.so.bpmn.common.scripts.MsoUtils import org.onap.so.bpmn.common.scripts.NetworkUtils import org.onap.so.bpmn.common.scripts.VidUtils import org.onap.so.bpmn.core.WorkflowException import org.onap.so.bpmn.core.json.JsonUtils; import org.onap.so.logger.MessageEnum import org.onap.so.logger.MsoLogger import groovy.json.* public class DeleteNetworkInstance extends AbstractServiceTaskProcessor { String Prefix="DELNI_" String groovyClassName = "DeleteNetworkInstance" ExceptionUtil exceptionUtil = new ExceptionUtil() JsonUtils jsonUtil = new JsonUtils() VidUtils vidUtils = new VidUtils(this) NetworkUtils networkUtils = new NetworkUtils() private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteNetworkInstance.class); public InitializeProcessVariables(DelegateExecution execution){ execution.setVariable(Prefix + "Success", false) execution.setVariable(Prefix + "CompleteMsoProcessRequest", "") execution.setVariable(Prefix + "FalloutHandlerRequest", "") execution.setVariable(Prefix + "isSilentSuccess", false) } // ************************************************** // Pre or Prepare Request Section // ************************************************** public void preProcessRequest (DelegateExecution execution) { def isDebugEnabled=execution.getVariable("isDebugLogEnabled") execution.setVariable("prefix",Prefix) msoLogger.trace("Inside preProcessRequest() of " + groovyClassName + "") try { // initialize flow variables InitializeProcessVariables(execution) String sdncVersion = execution.getVariable("sdncVersion") if (sdncVersion == null || sdncVersion == '1610') { // 'a-la-cart' default, sdncVersion = '1610' execution.setVariable("sdncVersion", "1610") String bpmnRequest = execution.getVariable("bpmnRequest") // set 'disableRollback' if (bpmnRequest != null) { String disableRollback = jsonUtil.getJsonValue(bpmnRequest, "requestDetails.requestInfo.suppressRollback") if (disableRollback != null) { execution.setVariable("disableRollback", disableRollback) msoLogger.debug("Received 'suppressRollback': " + disableRollback ) } else { execution.setVariable("disableRollback", false) } msoLogger.debug(" Set 'disableRollback' : " + execution.getVariable("disableRollback") ) } else { String dataErrorMessage = " Invalid 'bpmnRequest' request." msoLogger.debug(dataErrorMessage) exceptionUtil.buildAndThrowWorkflowException(execution, 2500, dataErrorMessage) } } else { // 'macro' test ONLY, sdncVersion = '1702' msoLogger.debug(" 'disableRollback' : " + execution.getVariable("disableRollback") ) } // get/set 'msoRequestId' and 'mso-request-id' String requestId = execution.getVariable("msoRequestId") if (requestId != null) { execution.setVariable("mso-request-id", requestId) } else { requestId = execution.getVariable("mso-request-id") } execution.setVariable(Prefix + "requestId", requestId) // get/set 'requestId' if (execution.getVariable("requestId") == null) { execution.setVariable("requestId", requestId) } // set action to "DELETE" execution.setVariable("action", "DELETE") //Place holder for additional code. // TODO ??? // userParams??? 1) pre-loads indicator, 2) 'auto-activation' // Tag/Value parameters // // Map: 'networkInputParams': 'auto-activation'' // Sample format? // "requestParameters": { // "userParams": [ // { // "name": "someUserParam1", // "value": "someValue1" // } // ] // } // // String userParams = //use json util to extract "userParams"// // execution.setVariable("networkInputParams", userParams) // else: execution.setVariable("networkInputParams", null) // } catch (Exception ex){ sendSyncError(execution) String exceptionMessage = "Exception Encountered in " + groovyClassName + ", PreProcessRequest() - " + ex.getMessage() msoLogger.debug(exceptionMessage) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage) } } public void getNetworkModelInfo (DelegateExecution execution) { def isDebugEnabled=execution.getVariable("isDebugLogEnabled") execution.setVariable("prefix", Prefix) msoLogger.trace("Inside getNetworkModelInfo() of DeleteNetworkInstance") try { // For Ala-Carte (sdnc = 1610): // 1. the Network ModelInfo is expected to be sent // via requestDetails.modelInfo (modelType = network), ex: modelCustomizationId. // 2. the Service ModelInfo is expected to be sent but will be IGNORE // via requestDetails.relatedInstanceList.relatedInstance.modelInfo (modelType = service) } catch (Exception ex) { sendSyncError(execution) String exceptionMessage = "Bpmn error encountered in DeleteNetworkInstance flow. getNetworkModelInfo() - " + ex.getMessage() msoLogger.debug(exceptionMessage) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage) } } public void sendSyncResponse (DelegateExecution execution) { def isDebugEnabled=execution.getVariable("isDebugLogEnabled") execution.setVariable("prefix",Prefix) msoLogger.trace("Inside sendSyncResponse() of DeleteNetworkInstance") try { String requestId = execution.getVariable("mso-request-id") String serviceInstanceId = execution.getVariable("serviceInstanceId") // RESTResponse (for API Handler (APIH) Reply Task) String deleteNetworkRestRequest = """{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim() msoLogger.debug(" sendSyncResponse to APIH - " + "\n" + deleteNetworkRestRequest) sendWorkflowResponse(execution, 202, deleteNetworkRestRequest) } catch (Exception ex) { // caught exception String exceptionMessage = "Exception Encountered in DeleteNetworkInstance, sendSyncResponse() - " + ex.getMessage() msoLogger.debug(exceptionMessage) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage) } } public void prepareCompletion (DelegateExecution execution) { def isDebugEnabled=execution.getVariable("isDebugLogEnabled") execution.setVariable("prefix",Prefix) msoLogger.trace("Inside prepareCompletion() of CreateNetworkInstance") try { String requestId = execution.getVariable("mso-request-id") String source = execution.getVariable(Prefix + "source") String msoCompletionRequest = """ ${MsoUtils.xmlEscape(requestId)} DELETE VID Network has been deleted successfully. BPMN Network action: DELETE """ // Format Response String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest) // normal path execution.setVariable(Prefix + "CompleteMsoProcessRequest", xmlMsoCompletionRequest) msoLogger.debug(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest) } catch (Exception ex) { String exceptionMessage = " Bpmn error encountered in CreateNetworkInstance flow. prepareCompletion() - " + ex.getMessage() msoLogger.debug(exceptionMessage) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage) } } public void prepareDBRequestError (DelegateExecution execution) { def isDebugEnabled=execution.getVariable("isDebugLogEnabled") execution.setVariable("prefix", Prefix) try { msoLogger.trace("Inside prepareDBRequestError of DeleteNetworkInstance") // set DB Header Authorization setBasicDBAuthHeader(execution, isDebugEnabled) WorkflowException wfe = execution.getVariable("WorkflowException") String statusMessage = wfe.getErrorMessage() String requestId = execution.getVariable(Prefix +"requestId") String dbRequest = """ ${MsoUtils.xmlEscape(requestId)} BPMN ${MsoUtils.xmlEscape(statusMessage)} FAILED <network-outputs xmlns="http://org.onap/so/infra/vnf-request/v1" xmlns:aetgt="http://org.onap/so/infra/vnf-request/v1" xmlns:rest="http://schemas.activebpel.org/REST/2007/12/01/aeREST.xsd"/> """ execution.setVariable(Prefix + "deleteDBRequest", dbRequest) msoLogger.debug(" DB Adapter Request - " + "\n" + dbRequest) } catch (Exception ex) { // caught exception String exceptionMessage = "Bpmn error encountered in DeleteNetworkInstance, prepareDBRequestError() - " + ex.getMessage() msoLogger.debug(exceptionMessage) exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage) } } // ************************************************** // Post or Validate Response Section // ************************************************** public void postProcessResponse (DelegateExecution execution) { def isDebugEnabled=execution.getVariable("isDebugLogEnabled") execution.setVariable("prefix", Prefix) msoLogger.trace("Inside postProcessResponse() of DeleteNetworkInstance") try { if (execution.getVariable("CMSO_ResponseCode") == "200") { execution.setVariable(Prefix + "Success", true) msoLogger.trace("DeleteNetworkInstance Success") // Place holder for additional code. } else { execution.setVariable(Prefix + "Success", false) msoLogger.trace("DeleteNetworkInstance Failed in CompletionMsoProces flow!.") } } catch (Exception ex) { String exceptionMessage = " Bpmn error encountered in DeleteNetworkInstance flow. postProcessResponse() - " + ex.getMessage() msoLogger.debug(exceptionMessage) exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage) } } // ******************************* // Build Error Section // ******************************* // Prepare for FalloutHandler public void buildErrorResponse (DelegateExecution execution) { def isDebugEnabled=execution.getVariable("isDebugLogEnabled") execution.setVariable("prefix", Prefix) msoLogger.trace("Prepare for FalloutHandler. FAILURE - prepare request for sub-process FalloutHandler.") String dbReturnCode = execution.getVariable(Prefix + "dbReturnCode") msoLogger.debug("DB Update Response Code : " + dbReturnCode) msoLogger.debug("DB Update Response String: " + '\n' + execution.getVariable(Prefix + "deleteDBResponse")) String falloutHandlerRequest = "" String requestId = execution.getVariable("mso-request-id") String source = execution.getVariable(Prefix + "source") execution.setVariable(Prefix + "Success", false) try { WorkflowException wfe = execution.getVariable("WorkflowException") String errorCode = String.valueOf(wfe.getErrorCode()) String errorMessage = wfe.getErrorMessage() falloutHandlerRequest = """ ${MsoUtils.xmlEscape(requestId)} DELETE ${MsoUtils.xmlEscape(source)} ${MsoUtils.xmlEscape(errorMessage)} ${MsoUtils.xmlEscape(errorCode)} """ msoLogger.debug(falloutHandlerRequest) execution.setVariable(Prefix + "FalloutHandlerRequest", falloutHandlerRequest) msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG,"Overall Error Response going to FalloutHandler: " + "\n" + falloutHandlerRequest,"BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "") } catch (Exception ex) { // caught exception String exceptionMessage = "Bpmn error encountered in DeleteNetworkInstance, buildErrorResponse() - " + ex.getMessage() msoLogger.debug(exceptionMessage) falloutHandlerRequest = """ ${MsoUtils.xmlEscape(requestId)} DELEtE ${MsoUtils.xmlEscape(source)} ${MsoUtils.xmlEscape(exceptionMessage)} 9999 """ execution.setVariable(Prefix + "FalloutHandlerRequest", falloutHandlerRequest) msoLogger.error(MessageEnum.BPMN_GENERAL_EXCEPTION_ARG,"Overall Error Response going to FalloutHandler: " + "\n" + falloutHandlerRequest,"BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.UnknownError, "Exception is:\n" + ex) } } public void sendSyncError (DelegateExecution execution) { def isDebugEnabled=execution.getVariable("isDebugLogEnabled") execution.setVariable("prefix", Prefix) try { String requestId = execution.getVariable("mso-request-id") String serviceInstanceId = execution.getVariable("serviceInstanceId") // RESTResponse (for API Handler (APIH) Reply Task) String deleteNetworkRestError = """{"requestReferences":{"instanceId":"${serviceInstanceId}","requestId":"${requestId}"}}""".trim() msoLogger.debug(" sendSyncResponse to APIH - " + "\n" + deleteNetworkRestError) sendWorkflowResponse(execution, 500, deleteNetworkRestError) } catch (Exception ex) { msoLogger.debug(" Sending Sync Error Activity Failed - DeleteNetworkInstance, sendSyncError(): " + "\n" + ex.getMessage()) } } public void processJavaException(DelegateExecution execution){ def isDebugEnabled=execution.getVariable("isDebugLogEnabled") execution.setVariable("prefix",Prefix) try{ msoLogger.debug("Caught a Java Exception") msoLogger.debug("Started processJavaException Method") msoLogger.debug("Variables List: " + execution.getVariables()) execution.setVariable("UnexpectedError", "Caught a Java Lang Exception") // Adding this line temporarily until this flows error handling gets updated exceptionUtil.buildWorkflowException(execution, 500, "Caught a Java Lang Exception") }catch(Exception e){ msoLogger.debug("Caught Exception during processJavaException Method: " + e) execution.setVariable("UnexpectedError", "Exception in processJavaException method") // Adding this line temporarily until this flows error handling gets updated exceptionUtil.buildWorkflowException(execution, 500, "Exception in processJavaException method") } msoLogger.debug("Completed processJavaException Method of " + Prefix) } }