/*-
* ============LICENSE_START=======================================================
* OPENECOMP - SO
* ================================================================================
* Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
* ================================================================================
* Modifications Copyright (c) 2019 Samsung
* ================================================================================
* 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.StringUtils
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.json.JSONObject
import org.json.XML
import org.onap.so.bpmn.common.recipe.ResourceInput
import org.onap.so.bpmn.common.resource.ResourceRequestBuilder
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.SDNCAdapterUtils
import org.onap.so.bpmn.core.domain.ModelInfo
import org.onap.so.bpmn.core.domain.ResourceType
import org.onap.so.bpmn.core.json.JsonUtils
import org.onap.so.bpmn.core.UrnPropertiesReader
import org.slf4j.Logger
import org.slf4j.LoggerFactory
/**
* This groovy class supports the ActivateSDNCCNetworkResource.bpmn process.
* flow for SDNC Network Resource Activate
*/
public class ActivateSDNCNetworkResource extends AbstractServiceTaskProcessor {
private static final Logger logger = LoggerFactory.getLogger( ActivateSDNCNetworkResource.class)
String Prefix = "ACTSDNCRES_"
ExceptionUtil exceptionUtil = new ExceptionUtil()
JsonUtils jsonUtil = new JsonUtils()
SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
MsoUtils msoUtils = new MsoUtils()
void preProcessRequest(DelegateExecution execution) {
logger.info(" ***** Started preProcessRequest *****")
try {
//get bpmn inputs from resource request.
String requestId = execution.getVariable("mso-request-id")
String requestAction = execution.getVariable("requestAction")
String recipeParamsFromRequest = execution.getVariable("recipeParams")
String resourceInput = execution.getVariable("resourceInput")
//Get ResourceInput Object
ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(resourceInput, ResourceInput.class)
execution.setVariable(Prefix + "resourceInput", resourceInputObj.toString())
//Deal with recipeParams
String recipeParamsFromWf = execution.getVariable("recipeParamXsd")
String resourceName = resourceInputObj.getResourceInstanceName()
//For sdnc requestAction default is "createNetworkInstance"
String operationType = "Network"
if(!StringUtils.isBlank(recipeParamsFromRequest)){
//the operationType from worflow(first node) is second priority.
operationType = jsonUtil.getJsonValue(recipeParamsFromRequest, "operationType")
}
if(!StringUtils.isBlank(recipeParamsFromWf)){
//the operationType from worflow(first node) is highest priority.
operationType = jsonUtil.getJsonValue(recipeParamsFromWf, "operationType")
}
String operationTypeFromConfig = UrnPropertiesReader.getVariable("resource-config." + resourceInputObj.resourceModelInfo.getModelName() + ".operation-type")
if (StringUtils.isNotEmpty(operationTypeFromConfig)) {
// highest priority if operation type configured
operationType = operationTypeFromConfig
}
String sdnc_svcAction = "activate"
String sdnc_requestAction = sdnc_svcAction.capitalize() + operationType + "Instance"
execution.setVariable(Prefix + "svcAction", sdnc_svcAction)
execution.setVariable(Prefix + "requestAction", sdnc_requestAction)
execution.setVariable(Prefix + "serviceInstanceId", resourceInputObj.getServiceInstanceId())
execution.setVariable("mso-request-id", requestId)
execution.setVariable("mso-service-instance-id", resourceInputObj.getServiceInstanceId())
} catch (BpmnError e) {
throw e
} catch (Exception ex){
String msg = "Exception in preProcessRequest " + ex.getMessage()
logger.debug(msg)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
}
void prepareUpdateAfterActivateSDNCResource(DelegateExecution execution) {
logger.info("started prepareUpdateAfterActivateSDNCResource ")
ResourceInput resourceInputObj = ResourceRequestBuilder.getJsonObject(execution.getVariable(Prefix + "resourceInput"), ResourceInput.class)
String operType = resourceInputObj.getOperationType()
String resourceCustomizationUuid = resourceInputObj.getResourceModelInfo().getModelCustomizationUuid()
String ServiceInstanceId = resourceInputObj.getServiceInstanceId()
String operationId = resourceInputObj.getOperationId()
String progress = "80"
String status = "activated"
String statusDescription = "SDCN resource activation completed"
execution.getVariable("operationId")
String body = """
${msoUtils.xmlEscape(operType)}
${msoUtils.xmlEscape(operationId)}
${msoUtils.xmlEscape(resourceCustomizationUuid)}
${msoUtils.xmlEscape(ServiceInstanceId)}
${msoUtils.xmlEscape(status)}
${msoUtils.xmlEscape(statusDescription)}
"""
setProgressUpdateVariables(execution, body)
}
private void setProgressUpdateVariables(DelegateExecution execution, String body) {
def dbAdapterEndpoint = UrnPropertiesReader.getVariable("mso.adapters.openecomp.db.endpoint", execution)
execution.setVariable("CVFMI_dbAdapterEndpoint", dbAdapterEndpoint)
execution.setVariable("CVFMI_updateResOperStatusRequest", body)
}
String customizeResourceParam(String networkInputParametersJson) {
List