/*-
* ============LICENSE_START=======================================================
* OPENECOMP - 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.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.json.JsonUtils
import org.onap.so.bpmn.core.UrnPropertiesReader
import org.onap.so.logger.MsoLogger
/**
* This groovy class supports the ActivateSDNCCNetworkResource.bpmn process.
* flow for SDNC Network Resource Activate
*/
public class ActivateSDNCNetworkResource extends AbstractServiceTaskProcessor {
private static final MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL, ActivateSDNCNetworkResource.class);
String Prefix = "ACTSDNCRES_"
ExceptionUtil exceptionUtil = new ExceptionUtil()
JsonUtils jsonUtil = new JsonUtils()
SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
MsoUtils msoUtils = new MsoUtils()
public void preProcessRequest(DelegateExecution execution) {
def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
msoLogger.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)
//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 sdnc_svcAction = "activate"
switch (resourceInputObj.getResourceModelInfo().getModelName()) {
case ~/[\w\s\W]*SOTNConnectivity[\w\s\W]*/ :
operationType = "SOTNConnectivity"
break
case ~/[\w\s\W]*sotnvpnattachment[\w\s\W]*/ :
operationType = "SOTNAttachment"
break
case ~/[\w\s\W]*SiteVF[\w\s\W]*/ :
operationType = "Site"
break
case ~/[\w\s\W]*deviceVF[\w\s\W]*/ :
operationType = "SDWANDevice"
execution.setVariable("isActivateRequired", "true")
break
case ~/[\w\s\W]*SiteWANVF[\w\s\W]*/ :
operationType = "SDWANPort"
execution.setVariable("isActivateRequired", "true")
break
case ~/[\w\s\W]*SDWANConnectivity[\w\s\W]*/ :
operationType = "SDWANConnectivity"
break
case ~/[\w\s\W]*sdwanvpnattachment[\w\s\W]*/ :
operationType = "SDWANAttachment"
break
default:
break
}
String sdnc_requestAction = StringUtils.capitalize(sdnc_svcAction) + 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){
msg = "Exception in preProcessRequest " + ex.getMessage()
msoLogger.debug( msg)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
}
public void prepareUpdateAfterActivateSDNCResource(DelegateExecution execution) {
msoLogger.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