/*-
* ============LICENSE_START=======================================================
* ONAP - SO
* ================================================================================
* Copyright (C) 2018 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.openecomp.mso.bpmn.infrastructure.scripts;
import static org.apache.commons.lang3.StringUtils.*;
import groovy.xml.XmlUtil
import groovy.json.*
import org.openecomp.mso.bpmn.common.scripts.AbstractServiceTaskProcessor
import org.openecomp.mso.bpmn.common.scripts.ExceptionUtil
import org.openecomp.mso.bpmn.core.WorkflowException
import org.openecomp.mso.bpmn.core.json.JsonUtils
import org.openecomp.mso.rest.APIResponse
import java.util.List;
import java.util.UUID;
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.runtime.Execution
import org.apache.commons.lang3.*
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.util.UriUtils
/**
* This groovy class supports the UpdateCustomE2EServiceInstance.bpmn process.
* AlaCarte flow for 1702 ServiceInstance Update
*
*/
public class UpdateCustomE2EServiceInstance extends AbstractServiceTaskProcessor {
String Prefix="UPDSI_"
ExceptionUtil exceptionUtil = new ExceptionUtil()
JsonUtils jsonUtil = new JsonUtils()
public void preProcessRequest (Execution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
execution.setVariable("prefix",Prefix)
String msg = ""
utils.log("INFO", " *** preProcessRequest() *** ", isDebugEnabled)
try {
String siRequest = execution.getVariable("bpmnRequest")
utils.logAudit(siRequest)
String requestId = execution.getVariable("mso-request-id")
execution.setVariable("msoRequestId", requestId)
utils.log("INFO", "Input Request:" + siRequest + " reqId:" + requestId, isDebugEnabled)
String serviceInstanceId = execution.getVariable("serviceInstanceId")
if (isBlank(serviceInstanceId)) {
msg = "Input serviceInstanceId' is null"
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
}
//subscriberInfo
String globalSubscriberId = jsonUtil.getJsonValue(siRequest, "requestDetails.subscriberInfo.globalSubscriberId")
if (isBlank(globalSubscriberId)) {
msg = "Input globalSubscriberId' is null"
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
} else {
execution.setVariable("globalSubscriberId", globalSubscriberId)
}
//requestInfo
execution.setVariable("source", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.source"))
execution.setVariable("serviceInstanceName", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.instanceName"))
execution.setVariable("disableRollback", jsonUtil.getJsonValue(siRequest, "requestDetails.requestInfo.suppressRollback"))
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 userParams = jsonUtil.getJsonValue(siRequest, "requestDetails.requestParameters.userParams")
utils.log("INFO", "userParams:" + userParams, isDebugEnabled)
List paramList = jsonUtil.StringArrayToList(execution, userParams)
String uuiRequest = jsonUtil.getJsonValue(paramList.get(0), "UUIRequest")
//modelInfo
if (isBlank(uuiRequest)) {
msg = "Input uuiRequest is null"
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
} else
{
execution.setVariable("uuiRequest", uuiRequest)
}
utils.log("INFO", "uuiRequest:\n" + uuiRequest, isDebugEnabled)
//requestParameters
String serviceType = jsonUtil.getJsonValue(uuiRequest, "service.parameters.serviceType")
if (isBlank(serviceType)) {
msg = "Input serviceType is null"
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 500, msg)
} else {
execution.setVariable("serviceType", serviceType)
}
//operationId
String operationId = jsonUtil.getJsonValue(siRequest, "operationId")
if (isBlank(operationId)) {
operationId = UUID.randomUUID().toString()
}
execution.setVariable("operationId", operationId)
execution.setVariable("operationType", "UPDATE")
execution.setVariable("URN_mso_adapters_openecomp_db_endpoint","http://mso.mso.testlab.openecomp.org:8080/dbadapters/RequestsDbAdapter")
} catch (BpmnError e) {
throw e;
} catch (Exception ex){
msg = "Exception in preProcessRequest " + ex.getMessage()
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
utils.log("INFO"," ***** Exit preProcessRequest *****", isDebugEnabled)
}
public void sendSyncResponse (Execution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
utils.log("INFO", " *** sendSyncResponse *** ", isDebugEnabled)
try {
String operationId = execution.getVariable("operationId")
String serviceInstanceId = execution.getVariable("serviceInstanceId")
// RESTResponse for API Handler (APIH) Reply Task
String updateServiceRestRequest = """{"service":{"serviceId":"${serviceInstanceId}","operationId":"${operationId}"}}""".trim()
utils.log("INFO", " sendSyncResponse to APIH:" + "\n" + updateServiceRestRequest, isDebugEnabled)
sendWorkflowResponse(execution, 202, updateServiceRestRequest)
execution.setVariable("sentSyncResponse", true)
} catch (Exception ex) {
String msg = "Exceptuion in sendSyncResponse:" + ex.getMessage()
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
utils.log("INFO"," ***** Exit sendSyncResopnse *****", isDebugEnabled)
}
public void sendSyncError (Execution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
utils.log("INFO", " *** sendSyncError *** ", isDebugEnabled)
try {
String errorMessage = ""
if (execution.getVariable("WorkflowException") instanceof WorkflowException) {
WorkflowException wfe = execution.getVariable("WorkflowException")
errorMessage = wfe.getErrorMessage()
} else {
errorMessage = "Sending Sync Error."
}
String buildworkflowException =
"""
${errorMessage}
7000
"""
utils.logAudit(buildworkflowException)
sendWorkflowResponse(execution, 500, buildworkflowException)
} catch (Exception ex) {
utils.log("INFO", " Sending Sync Error Activity Failed. " + "\n" + ex.getMessage(), isDebugEnabled)
}
}
public void prepareCompletionRequest (Execution execution) {
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
utils.log("INFO", " *** prepareCompletion *** ", isDebugEnabled)
try {
String requestId = execution.getVariable("msoRequestId")
String serviceInstanceId = execution.getVariable("serviceInstanceId")
String source = execution.getVariable("source")
String msoCompletionRequest =
"""
${requestId}
UPDATE
${source}
Service Instance was updated successfully.
${serviceInstanceId}
UpdateGenericALaCarteServiceInstance
"""
// Format Response
String xmlMsoCompletionRequest = utils.formatXml(msoCompletionRequest)
execution.setVariable("completionRequest", xmlMsoCompletionRequest)
utils.log("INFO", " Overall SUCCESS Response going to CompleteMsoProcess - " + "\n" + xmlMsoCompletionRequest, isDebugEnabled)
} catch (Exception ex) {
String msg = " Exception in prepareCompletion:" + ex.getMessage()
utils.log("INFO", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
utils.log("INFO", "*** Exit prepareCompletionRequest ***", isDebugEnabled)
}
public void prepareFalloutRequest(Execution execution){
def isDebugEnabled=execution.getVariable("isDebugLogEnabled")
utils.log("INFO", " *** prepareFalloutRequest *** ", isDebugEnabled)
try {
WorkflowException wfex = execution.getVariable("WorkflowException")
utils.log("INFO", " Input Workflow Exception: " + wfex.toString(), isDebugEnabled)
String requestId = execution.getVariable("msoRequestId")
String source = execution.getVariable("source")
String requestInfo =
"""
${requestId}
UPDATE
${source}
"""
String falloutRequest = exceptionUtil.processMainflowsBPMNException(execution, requestInfo)
execution.setVariable("falloutRequest", falloutRequest)
} catch (Exception ex) {
utils.log("INFO", "Exception prepareFalloutRequest:" + ex.getMessage(), isDebugEnabled)
String errorException = " Bpmn error encountered in UpdateGenericALaCarteServiceInstance flow. FalloutHandlerRequest, buildErrorResponse() - " + ex.getMessage()
String requestId = execution.getVariable("msoRequestId")
String falloutRequest =
"""
${requestId}
UPDATE
UUI
${errorException}
7000
"""
execution.setVariable("falloutRequest", falloutRequest)
}
utils.log("INFO", "*** Exit prepareFalloutRequest ***", isDebugEnabled)
}
}