/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* Copyright (C) 2017 Huawei Technologies Co., Ltd. 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 static org.apache.commons.lang3.StringUtils.*;
import org.apache.commons.lang3.*
import org.camunda.bpm.engine.delegate.BpmnError
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.VidUtils
import org.onap.so.bpmn.core.WorkflowException
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.logger.MsoLogger
import org.springframework.web.util.UriUtils;
import groovy.json.*
/**
* This groovy class supports the DelE2EServiceInstance.bpmn process.
*
*/
public class DeleteCustomE2EServiceInstance extends AbstractServiceTaskProcessor {
String Prefix="DELSI_"
ExceptionUtil exceptionUtil = new ExceptionUtil()
JsonUtils jsonUtil = new JsonUtils()
VidUtils vidUtils = new VidUtils()
private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, DeleteCustomE2EServiceInstance.class);
public void preProcessRequest (DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
execution.setVariable("prefix",Prefix)
String msg = ""
msoLogger.info("Starting preProcessRequest")
try {
// check for incoming json message/input
String siRequest = execution.getVariable("bpmnRequest")
msoLogger.debug(siRequest)
String requestId = execution.getVariable("mso-request-id")
execution.setVariable("msoRequestId", requestId)
msoLogger.info("Input Request:" + siRequest + " reqId:" + requestId)
String serviceInstanceId = execution.getVariable("serviceInstanceId")
if (isBlank(serviceInstanceId)) {
msg = "Input serviceInstanceId' is null"
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
}
//requestInfo
// String productFamilyId = jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.productFamilyId")
// if (isBlank(productFamilyId))
// {
// msg = "Input productFamilyId is null"
// utils.log("INFO", msg, isDebugEnabled)
// //exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
// } else {
// execution.setVariable("productFamilyId", productFamilyId)
// }
String source = jsonUtil.getJsonValue(siRequest, "source")
execution.setVariable("source", source)
//subscriberInfo
String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "globalSubscriberId")
if (isBlank(globalSubscriberId)) {
msg = "Input globalSubscriberId' is null"
msoLogger.info(msg)
} else {
execution.setVariable("globalSubscriberId", globalSubscriberId)
}
//requestParameters
String subscriptionServiceType = jsonUtil.getJsonValue(siRequest, "serviceType")
if (isBlank(subscriptionServiceType)) {
msg = "Input subscriptionServiceType is null"
msoLogger.debug(msg)
//exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
} else {
execution.setVariable("subscriptionServiceType", subscriptionServiceType)
}
String operationId = jsonUtil.getJsonValue(siRequest, "operationId")
execution.setVariable("operationId", operationId)
execution.setVariable("operationType", "DELETE")
} catch (BpmnError e) {
throw e;
} catch (Exception ex){
msg = "Exception in preProcessRequest " + ex.getMessage()
msoLogger.info(msg)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
msoLogger.trace("Exit preProcessRequest")
}
public void sendSyncResponse (DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
msoLogger.trace("Staring sendSyncResponse")
try {
String operationId = execution.getVariable("operationId")
String syncResponse = """{"operationId":"${operationId}"}""".trim()
msoLogger.info("sendSynchResponse: xmlSyncResponse - " + "\n" + syncResponse)
sendWorkflowResponse(execution, 202, syncResponse)
} catch (Exception ex) {
String msg = "Exception in sendSyncResponse: " + ex.getMessage()
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
}
msoLogger.trace("Exit sendSyncResopnse")
}
public void sendSyncError (DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
msoLogger.info("Starting sendSyncError")
try {
String errorMessage = ""
if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
WorkflowException wfe = execution.getVariable("WorkflowException")
errorMessage = wfe.getErrorMessage()
} else {
errorMessage = "Sending Sync Error."
}
String buildworkflowException =
"""
${MsoUtils.xmlEscape(errorMessage)}
7000
"""
msoLogger.info(buildworkflowException)
sendWorkflowResponse(execution, 500, buildworkflowException)
} catch (Exception ex) {
msoLogger.info("Sending Sync Error Activity Failed. " + "\n" + ex.getMessage())
}
}
public void prepareCompletionRequest (DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
msoLogger.trace("Starting prepareCompletion")
try {
String requestId = execution.getVariable("msoRequestId")
String source = execution.getVariable("source")
String msoCompletionRequest =
"""
${MsoUtils.xmlEscape(requestId)}
DELETE
E2E Service Instance was deleted successfully.
DeleteCustomE2EServiceInstance
"""
// Format Response
String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
execution.setVariable("completionRequest", xmlMsoCompletionRequest)
msoLogger.info(" Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest)
} catch (Exception ex) {
String msg = " Exception in prepareCompletion:" + ex.getMessage()
msoLogger.info(msg)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
msoLogger.trace("Exit prepareCompletionRequest")
}
public void prepareFalloutRequest(DelegateExecution execution){
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
msoLogger.trace("Starting prepareFalloutRequest ")
try {
WorkflowException wfex = execution.getVariable("WorkflowException")
msoLogger.info(" Input Workflow Exception: " + wfex.toString())
String requestId = execution.getVariable("msoRequestId")
String source = execution.getVariable("source")
String requestInfo =
"""
${MsoUtils.xmlEscape(requestId)}
DELETE
"""
String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
execution.setVariable("falloutRequest", falloutRequest)
} catch (Exception ex) {
msoLogger.info("Exception prepareFalloutRequest:" + ex.getMessage())
String errorException = " Bpmn error encountered in CreateServiceInstance flow. FalloutHandlerRequest, buildErrorResponse() - " + ex.getMessage()
String requestId = execution.getVariable("msoRequestId")
String falloutRequest =
"""
${MsoUtils.xmlEscape(requestId)}
DELETE
${MsoUtils.xmlEscape(errorException)}
7000
"""
execution.setVariable("falloutRequest", falloutRequest)
}
msoLogger.trace("Exit prepareFalloutRequest")
}
// *******************************
// Build DB request Section
// *******************************
public void prepareDBRequest (DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
execution.setVariable("prefix", Prefix)
try {
msoLogger.info("Starting prepareDBRequest")
String requestId = execution.getVariable("DELSI_requestId")
String statusMessage = "E2E Service Instance successfully deleted."
//TODO - verify the format for Service Instance Delete,
String dbRequest =
"""
${MsoUtils.xmlEscape(requestId)}
BPMN
${MsoUtils.xmlEscape(statusMessage)}
COMPLETED
"""
String buildDeleteDBRequestAsString = utils.formatXml(dbRequest)
execution.setVariable("DELSI_createDBRequest", buildDeleteDBRequestAsString)
msoLogger.info(buildDeleteDBRequestAsString)
} catch (Exception ex) {
// try error in method block
String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequest() - " + ex.getMessage()
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, exceptionMessage)
}
}
// *******************************
// Build Error Section
// *******************************
public void prepareDBRequestError (DelegateExecution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
execution.setVariable("prefix", Prefix)
msoLogger.trace("Start prepareDBRequestError")
try {
String requestId = execution.getVariable("DELSI_requestId")
String statusMessage = ""
if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
WorkflowException wfe = execution.getVariable("WorkflowException")
statusMessage = wfe.getErrorMessage()
} else {
statusMessage = "Encountered Error during DeleteCustomE2EServiceInstance proccessing. "
}
//TODO - verify the format for Service Instance Create,
String dbRequest =
"""
${MsoUtils.xmlEscape(requestId)}
BPMN
${MsoUtils.xmlEscape(statusMessage)}
FAILED
"""
String buildDBRequestAsString = utils.formatXml(dbRequest)
execution.setVariable("DELSI_createDBInfraErrorRequest", buildDBRequestAsString)
msoLogger.info(buildDBRequestAsString)
} catch (Exception ex) {
// try error in method block
String exceptionMessage = "Bpmn error encountered in DeleteCustomE2EServiceInstance flow. Unexpected Error from method prepareDBRequestError() - " + ex.getMessage()
exceptionUtil.buildWorkflowException(execution, 7000, exceptionMessage)
}
}
public void processJavaException(DelegateExecution execution) {
//TODO:
}
/**
* Init the service Operation Status
*/
public void prepareInitServiceOperationStatus(DelegateExecution execution){
def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
utils.log("DEBUG", " ======== STARTED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
try{
String serviceId = execution.getVariable("serviceInstanceId")
String operationId = execution.getVariable("operationId")
String userId = ""
String result = "processing"
String progress = "0"
String reason = ""
String operationContent = "Prepare service creation"
utils.log("DEBUG", "Generated new operation for Service Instance serviceId:" + serviceId + " operationId:" + operationId, isDebugEnabled)
serviceId = UriUtils.encode(serviceId,"UTF-8")
def dbAdapterEndpoint = execution.getVariable("URN_mso_adapters_openecomp_db_endpoint")
execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
utils.log("DEBUG", "DB Adapter Endpoint is: " + dbAdapterEndpoint, isDebugEnabled)
String payload =
"""
${MsoUtils.xmlEscape(serviceId)}
${MsoUtils.xmlEscape(operationId)}
DELETE
${MsoUtils.xmlEscape(userId)}
${MsoUtils.xmlEscape(result)}
${MsoUtils.xmlEscape(operationContent)}
${MsoUtils.xmlEscape(reason)}
"""
payload = utils.formatXml(payload)
execution.setVariable("CVFMI_updateServiceOperStatusRequest", payload)
utils.log("DEBUG", "Outgoing updateServiceOperStatusRequest: \n" + payload, isDebugEnabled)
}catch(Exception e){
utils.log("ERROR", "Exception Occured Processing prepareInitServiceOperationStatus. Exception is:\n" + e, isDebugEnabled)
execution.setVariable("CVFMI_ErrorResponse", "Error Occurred during prepareInitServiceOperationStatus Method:\n" + e.getMessage())
}
utils.log("DEBUG", "======== COMPLETED prepareInitServiceOperationStatus Process ======== ", isDebugEnabled)
}
}