/*-
* ============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.openecomp.mso.bpmn.infrastructure.scripts
import org.json.JSONObject
import org.json.XML;
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.common.recipe.ResourceInput;
import org.openecomp.mso.bpmn.common.resource.ResourceRequestBuilder
import org.openecomp.mso.bpmn.core.WorkflowException
import org.openecomp.mso.bpmn.core.json.JsonUtils
import org.openecomp.mso.bpmn.infrastructure.workflow.serviceTask.client.builder.AbstractBuilder
import org.openecomp.mso.rest.APIResponse
import org.openecomp.mso.bpmn.common.scripts.SDNCAdapterUtils
import java.util.UUID;
import org.camunda.bpm.engine.delegate.BpmnError
import org.camunda.bpm.engine.delegate.DelegateExecution
import org.apache.commons.lang3.*
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.util.UriUtils
import org.openecomp.mso.rest.RESTClient
import org.openecomp.mso.rest.RESTConfig
import org.openecomp.mso.rest.APIResponse;
/**
* This groovy class supports the CreateSDNCCNetworkResource.bpmn process.
* flow for SDNC Network Resource Create
*/
public class CreateSDNCNetworkResource extends AbstractServiceTaskProcessor {
String Prefix="CRESDNCRES_"
ExceptionUtil exceptionUtil = new ExceptionUtil()
JsonUtils jsonUtil = new JsonUtils()
SDNCAdapterUtils sdncAdapterUtils = new SDNCAdapterUtils()
public void preProcessRequest(DelegateExecution execution){
def isDebugEnabled = execution.getVariable("isDebugLogEnabled")
utils.log("INFO"," ***** Started preProcessRequest *****", isDebugEnabled)
try {
//get bpmn inputs from resource request.
String requestId = execution.getVariable("mso-request-id")
String requestAction = execution.getVariable("requestAction")
utils.log("INFO","The requestAction is: " + requestAction, isDebugEnabled)
String recipeParamsFromRequest = execution.getVariable("recipeParams")
utils.log("INFO","The recipeParams is: " + recipeParamsFromRequest, isDebugEnabled)
String resourceInput = execution.getVariable("resourceInput")
utils.log("INFO","The resourceInput is: " + resourceInput, isDebugEnabled)
//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")
}
//For sdnc, generate svc_action and request_action
String sdnc_svcAction = "create"
if(StringUtils.containsIgnoreCase(resourceInputObj.getResourceInstanceName(), "overlay")){
//This will be resolved in R3.
sdnc_svcAction ="activate"
operationType = "NCINetwork"
}
if(StringUtils.containsIgnoreCase(resourceInputObj.getResourceInstanceName(), "underlay")){
//This will be resolved in R3.
operationType ="Network"
}
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())
//TODO Here build networkrequest
} catch (BpmnError e) {
throw e;
} catch (Exception ex){
msg = "Exception in preProcessRequest " + ex.getMessage()
utils.log("DEBUG", msg, isDebugEnabled)
exceptionUtil.buildAndThrowWorkflowException(execution, 7000, msg)
}
}
String customizeResourceParam(String netowrkInputParametersJson) {
List