diff options
Diffstat (limited to 'ms/blueprintsprocessor/functions')
12 files changed, 199 insertions, 213 deletions
diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/api/NetconfRpcService.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/api/NetconfRpcService.kt index 550852165..02c0a3426 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/api/NetconfRpcService.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/api/NetconfRpcService.kt @@ -57,6 +57,24 @@ interface NetconfRpcService { editDefaultOperation: String = ModifyAction.NONE.action): DeviceResponse /** + * Invoke custom RPC as provided as input. + * + * Some use cases might required one to directly invoke a device + * specific RPC. The RPC must be correctly formatted. + * + * Ex: in order to rollback last submitted configuration + * for JUNOS devices, such RPC can be use: + * <code> + * <rpc> + * <load-configuration rollback="1"/> + * </rpc> + * </code> + * + * @param rpc the rpc content. + */ + fun invokeRpc(rpc: String): DeviceResponse + + /** * Validate * * @param configTarget running or candidate, default candidate diff --git a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfRpcServiceImpl.kt b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfRpcServiceImpl.kt index 8d8e0ea46..15fb3122c 100644 --- a/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfRpcServiceImpl.kt +++ b/ms/blueprintsprocessor/functions/netconf-executor/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/netconf/executor/core/NetconfRpcServiceImpl.kt @@ -41,6 +41,19 @@ class NetconfRpcServiceImpl(private var deviceInfo: DeviceInfo) : NetconfRpcServ this.netconfSession = netconfSession } + override fun invokeRpc(rpc: String): DeviceResponse { + var output = DeviceResponse() + val messageId = messageIdInteger.getAndIncrement().toString() + log.info("$deviceInfo: invokeRpc: messageId($messageId)") + try { + output = asyncRpc(rpc, messageId) + } catch (e: Exception) { + output.status = RpcStatus.FAILURE + output.errorMessage = "$deviceInfo: failed in invokeRpc command $e.message" + } + return output + } + override fun getConfig(filter: String, configTarget: String): DeviceResponse { var output = DeviceResponse() val messageId = messageIdInteger.getAndIncrement().toString() diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt index c12823442..335aea1ef 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt @@ -59,14 +59,14 @@ interface ResourceResolutionService { @Service(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION) open class ResourceResolutionServiceImpl(private var applicationContext: ApplicationContext, private var resolutionResultService: ResourceResolutionResultService) : - ResourceResolutionService { + ResourceResolutionService { private val log = LoggerFactory.getLogger(ResourceResolutionService::class.java) override fun registeredResourceSources(): List<String> { return applicationContext.getBeanNamesForType(ResourceAssignmentProcessor::class.java) - .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) } - .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) } + .filter { it.startsWith(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) } + .map { it.substringAfter(ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR) } } override fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String, @@ -92,7 +92,7 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica val result = resolveResources(bluePrintRuntimeService, nodeTemplateName, artifactMapping, artifactTemplate) if (properties.containsKey(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT) - && properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean) { + && properties[ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT] as Boolean) { resolutionResultService.write(properties, result, bluePrintRuntimeService, artifactPrefix) } @@ -113,32 +113,32 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica val identifierName = artifactTemplate ?: "no-template" val resourceAssignmentContent = - bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping) + bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactMapping) val resourceAssignments: MutableList<ResourceAssignment> = - JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java) - as? MutableList<ResourceAssignment> - ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions") + JacksonUtils.getListFromJson(resourceAssignmentContent, ResourceAssignment::class.java) + as? MutableList<ResourceAssignment> + ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions") // Get the Resource Dictionary Name val dictionaryFile = bluePrintRuntimeService.bluePrintContext().rootPath.plus(File.separator) - .plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR).plus(File.separator) - .plus(ResourceResolutionConstants.FILE_NAME_RESOURCE_DEFINITION_TYPES) + .plus(BluePrintConstants.TOSCA_DEFINITIONS_DIR).plus(File.separator) + .plus(ResourceResolutionConstants.FILE_NAME_RESOURCE_DEFINITION_TYPES) val resourceDictionaries: MutableMap<String, ResourceDefinition> = - JacksonUtils.getMapFromFile(dictionaryFile, ResourceDefinition::class.java) - ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions") + JacksonUtils.getMapFromFile(dictionaryFile, ResourceDefinition::class.java) + ?: throw BluePrintProcessorException("couldn't get Dictionary Definitions") // Resolve resources resolveResourceAssignments(bluePrintRuntimeService, resourceDictionaries, resourceAssignments, identifierName) val resolvedParamJsonContent = - ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList()) + ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignments.toList()) // Check Template is there if (artifactTemplate != null) { val templateContent = - bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactTemplate) + bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactTemplate) resolvedContent = BluePrintTemplateService.generateContent(templateContent, resolvedParamJsonContent) } else { resolvedContent = resolvedParamJsonContent @@ -159,35 +159,33 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments) val resourceAssignmentRuntimeService = - ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, identifierName) + ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, identifierName) bulkSequenced.map { batchResourceAssignments -> batchResourceAssignments.filter { it.name != "*" && it.name != "start" } - .forEach { resourceAssignment -> - val dictionaryName = resourceAssignment.dictionaryName - val dictionarySource = resourceAssignment.dictionarySource - /** - * Get the Processor name - */ - val processorName = processorName(dictionaryName!!, dictionarySource!!, - resourceDictionaries) - - val resourceAssignmentProcessor = - applicationContext.getBean(processorName) as? ResourceAssignmentProcessor - ?: throw BluePrintProcessorException("failed to get resource processor for name($processorName) " + - "for resource assignment(${resourceAssignment.name})") - try { - // Set BluePrint Runtime Service - resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService - // Set Resource Dictionaries - resourceAssignmentProcessor.resourceDictionaries = resourceDictionaries - // Invoke Apply Method - resourceAssignmentProcessor.apply(resourceAssignment) - } catch (e: RuntimeException) { - resourceAssignmentProcessor.recover(e, resourceAssignment) - throw BluePrintProcessorException(e) + .forEach { resourceAssignment -> + val dictionaryName = resourceAssignment.dictionaryName + val dictionarySource = resourceAssignment.dictionarySource + /** + * Get the Processor name + */ + val processorName = processorName(dictionaryName!!, dictionarySource!!, resourceDictionaries) + + val resourceAssignmentProcessor = + applicationContext.getBean(processorName) as? ResourceAssignmentProcessor + ?: throw BluePrintProcessorException("failed to get resource processor for name($processorName) " + + "for resource assignment(${resourceAssignment.name})") + try { + // Set BluePrint Runtime Service + resourceAssignmentProcessor.raRuntimeService = resourceAssignmentRuntimeService + // Set Resource Dictionaries + resourceAssignmentProcessor.resourceDictionaries = resourceDictionaries + // Invoke Apply Method + resourceAssignmentProcessor.apply(resourceAssignment) + } catch (e: RuntimeException) { + throw BluePrintProcessorException(e) + } } - } } } @@ -208,18 +206,18 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica } else -> { val resourceDefinition = resourceDictionaries[dictionaryName] - ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName") + ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dictionaryName") val resourceSource = resourceDefinition.sources[dictionarySource] - ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)") + ?: throw BluePrintProcessorException("couldn't get resource definition $dictionaryName source($dictionarySource)") processorName = ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR - .plus(resourceSource.type) + .plus(resourceSource.type) } } checkNotEmptyOrThrow(processorName, - "couldn't get processor name for resource dictionary definition($dictionaryName) source" + - "($dictionarySource)") + "couldn't get processor name for resource dictionary definition($dictionaryName) source" + + "($dictionarySource)") return processorName diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt index 1c3574461..25fc8c010 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/ResourceSourceProperties.kt @@ -48,6 +48,9 @@ open class DatabaseResourceSource : ResourceSourceProperties() { } open class RestResourceSource : ResourceSourceProperties() { + lateinit var verb: String + @get:JsonProperty("payload") + var payload: String? = null lateinit var type: String @get:JsonProperty("endpoint-selector") var endpointSelector: String? = null diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessor.kt index c2dbd7312..6469e78da 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessor.kt @@ -20,10 +20,8 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.pr import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.CapabilityResourceSource import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR -import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.BlueprintJythonService -import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.ComponentFunctionScriptingService import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException -import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintScriptsService import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment import org.slf4j.LoggerFactory @@ -31,14 +29,16 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory import org.springframework.context.ApplicationContext import org.springframework.context.annotation.Scope import org.springframework.stereotype.Service -import java.io.File @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-capability") @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) -open class CapabilityResourceResolutionProcessor(private var applicationContext: ApplicationContext, - private val bluePrintScriptsService: BluePrintScriptsService, - private val bluePrintJythonService: BlueprintJythonService) : - ResourceAssignmentProcessor() { +open class CapabilityResourceResolutionProcessor(private val applicationContext: ApplicationContext, + private var componentFunctionScriptingService: ComponentFunctionScriptingService) + : ResourceAssignmentProcessor() { + + private val log = LoggerFactory.getLogger(CapabilityResourceResolutionProcessor::class.java) + + var componentResourceAssignmentProcessor: ResourceAssignmentProcessor? = null override fun getName(): String { return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-capability" @@ -62,118 +62,43 @@ open class CapabilityResourceResolutionProcessor(private var applicationContext: val scriptType = capabilityResourceSourceProperty.scriptType val scriptClassReference = capabilityResourceSourceProperty.scriptClassReference + val instanceDependencies = capabilityResourceSourceProperty.instanceDependencies ?: listOf() - var componentResourceAssignmentProcessor: ResourceAssignmentProcessor? = null - - when (scriptType) { - BluePrintConstants.SCRIPT_KOTLIN -> { - componentResourceAssignmentProcessor = getKotlinResourceAssignmentProcessorInstance(scriptClassReference, - capabilityResourceSourceProperty.instanceDependencies) - } - BluePrintConstants.SCRIPT_INTERNAL -> { - // Initialize Capability Resource Assignment Processor - componentResourceAssignmentProcessor = applicationContext.getBean(scriptClassReference, ResourceAssignmentProcessor::class.java) - } - BluePrintConstants.SCRIPT_JYTHON -> { - val content = getJythonContent(scriptClassReference) - componentResourceAssignmentProcessor = getJythonResourceAssignmentProcessorInstance(scriptClassReference, - content, capabilityResourceSourceProperty.instanceDependencies) - } - } + componentResourceAssignmentProcessor = scriptInstance(scriptType, scriptClassReference, instanceDependencies) - checkNotNull(componentResourceAssignmentProcessor) { "failed to get capability resource assignment processor($scriptClassReference)" } + checkNotNull(componentResourceAssignmentProcessor) { + "failed to get capability resource assignment processor($scriptClassReference)" + } // Assign Current Blueprint runtime and ResourceDictionaries - componentResourceAssignmentProcessor.raRuntimeService = raRuntimeService - componentResourceAssignmentProcessor.resourceDictionaries = resourceDictionaries + componentResourceAssignmentProcessor!!.raRuntimeService = raRuntimeService + componentResourceAssignmentProcessor!!.resourceDictionaries = resourceDictionaries // Invoke componentResourceAssignmentProcessor - componentResourceAssignmentProcessor.apply(resourceAssignment) + componentResourceAssignmentProcessor!!.apply(resourceAssignment) } override fun recover(runtimeException: RuntimeException, resourceAssignment: ResourceAssignment) { - - TODO("To Implement") - } - - private fun getKotlinResourceAssignmentProcessorInstance(scriptClassName: String, - instanceNames: List<String>? = null): ResourceAssignmentProcessor { - var scriptPropertyInstances: MutableMap<String, Any>? = null - - if (instanceNames != null && instanceNames.isNotEmpty()) { - scriptPropertyInstances = hashMapOf() - instanceNames.forEach { - scriptPropertyInstances[it] = applicationContext.getBean(it) - ?: throw BluePrintProcessorException("couldn't get the dependency instance($it)") - } - } - - return getKotlinResourceAssignmentProcessorInstance(scriptClassName, scriptPropertyInstances) - - } - - fun getKotlinResourceAssignmentProcessorInstance(scriptClassName: String, - scriptPropertyInstances: MutableMap<String, Any>? = null): - ResourceAssignmentProcessor { - - val resourceAssignmentProcessor = bluePrintScriptsService - .scriptInstance<ResourceAssignmentProcessor>(raRuntimeService.bluePrintContext(), - scriptClassName, false) - - // Add additional Instance - if (scriptPropertyInstances != null) { - resourceAssignmentProcessor.scriptPropertyInstances = scriptPropertyInstances + log.info("Recovering for : ${resourceAssignment.name} : ${runtimeException.toString()}") + if (componentResourceAssignmentProcessor != null) { + componentResourceAssignmentProcessor!!.recover(runtimeException, resourceAssignment) } - - return resourceAssignmentProcessor - } - - private fun getJythonContent(instanceName: String): String { - val absolutePath = raRuntimeService.bluePrintContext().rootPath - .plus(File.separator) - .plus(BluePrintConstants.TOSCA_SCRIPTS_JYTHON_DIR) - .plus(File.separator) - .plus("$instanceName.py") - - return JacksonUtils.getContent(absolutePath) - } - /** - * getJythonResourceAssignmentProcessorInstance Purpose: prepare the jython - * executor component as a resource assignment processor - * - * @param pythonClassName String - * @param content String - * @param dependencyInstances List<String> - * @return resourceAssignmentProcessor ResourceAssignmentProcessor - */ - private fun getJythonResourceAssignmentProcessorInstance(pythonClassName: String, content: String, - dependencyInstances: List<String>?): - ResourceAssignmentProcessor { - val jythonContextInstance: MutableMap<String, Any> = hashMapOf() - jythonContextInstance["log"] = LoggerFactory.getLogger(pythonClassName) - jythonContextInstance["raRuntimeService"] = raRuntimeService - dependencyInstances?.forEach { instanceName -> - jythonContextInstance[instanceName] = applicationContext.getBean(instanceName) - } - - return getJythonResourceAssignmentProcessorInstance(pythonClassName, content, jythonContextInstance) - } + fun scriptInstance(scriptType: String, scriptClassReference: String, instanceDependencies: List<String>) + : ResourceAssignmentProcessor { - fun getJythonResourceAssignmentProcessorInstance(pythonClassName: String, content: String, - dependencyInstances: MutableMap<String, Any>): - ResourceAssignmentProcessor { + log.info("creating resource resolution of script type($scriptType), reference name($scriptClassReference) and" + + "instanceDependencies($instanceDependencies)") - val resourceAssignmentProcessor = bluePrintJythonService - .jythonInstance<ResourceAssignmentProcessor>(raRuntimeService.bluePrintContext(), pythonClassName, - content, dependencyInstances) + val scriptComponent = componentFunctionScriptingService + .scriptInstance<ResourceAssignmentProcessor>(raRuntimeService.bluePrintContext(), scriptType, + scriptClassReference) - // Add additional Instance - if (dependencyInstances != null) { - resourceAssignmentProcessor.scriptPropertyInstances = dependencyInstances + instanceDependencies.forEach { instanceDependency -> + scriptPropertyInstances[instanceDependency] = applicationContext + .getBean(instanceDependency) } - - return resourceAssignmentProcessor + return scriptComponent } }
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt index 39be14c93..c76bff322 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt @@ -18,11 +18,18 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor import com.fasterxml.jackson.databind.node.JsonNodeFactory +import com.fasterxml.jackson.databind.node.MissingNode import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.DBLibGenericService import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.DatabaseResourceSource import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils -import org.onap.ccsdk.apps.controllerblueprints.core.* +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes +import org.onap.ccsdk.apps.controllerblueprints.core.checkEqualsOrThrow +import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmpty +import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow +import org.onap.ccsdk.apps.controllerblueprints.core.nullToEmpty +import org.onap.ccsdk.apps.controllerblueprints.core.returnNotEmptyOrThrow import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDictionaryConstants @@ -31,7 +38,6 @@ import org.springframework.beans.factory.config.ConfigurableBeanFactory import org.springframework.context.annotation.Scope import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate import org.springframework.stereotype.Service -import java.util.* /** * DatabaseResourceAssignmentProcessor @@ -54,11 +60,8 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: validate(resourceAssignment) // Check if It has Input - try { - val value = raRuntimeService.getInputValue(resourceAssignment.name) - logger.info("primary-db source template key (${resourceAssignment.name}) found from input and value is ($value)") - ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value) - } catch (e: BluePrintProcessorException) { + val value = getFromInput(resourceAssignment) + if (value == null || value is MissingNode) { val dName = resourceAssignment.dictionaryName val dSource = resourceAssignment.dictionarySource val resourceDefinition = resourceDictionaries[dName] @@ -71,12 +74,16 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: val sql = checkNotNull(sourceProperties.query) { "failed to get request query for $dName under $dSource properties" } val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" } - logger.info("$dSource dictionary information : ($sql), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})") + val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping) + + val resolvedSql = resolveFromInputKeyMapping(sql, resolvedInputKeyMapping) + + logger.info("$dSource dictionary information : ($resolvedSql), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})") val jdbcTemplate = blueprintDBLibService(sourceProperties) - val rows = jdbcTemplate.queryForList(sql, populateNamedParameter(inputKeyMapping)) + val rows = jdbcTemplate.queryForList(resolvedSql, resolvedInputKeyMapping) if (rows.isNullOrEmpty()) { - logger.warn("Failed to get $dSource result for dictionary name ($dName) the query ($sql)") + logger.warn("Failed to get $dSource result for dictionary name ($dName) the query ($resolvedSql)") } else { populateResource(resourceAssignment, sourceProperties, rows) } @@ -91,7 +98,7 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: } private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource): NamedParameterJdbcTemplate { - return if (checkNotEmpty(sourceProperties.endpointSelector!!)) { + return if (checkNotEmpty(sourceProperties.endpointSelector)) { val dbPropertiesJson = raRuntimeService.resolveDSLExpression(sourceProperties.endpointSelector!!) dBLibGenericService.remoteJdbcTemplate(dbPropertiesJson) } else { @@ -109,17 +116,6 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: } } - private fun populateNamedParameter(inputKeyMapping: Map<String, String>): Map<String, Any> { - val namedParameters = HashMap<String, Any>() - inputKeyMapping.forEach { - val expressionValue = raRuntimeService.getDictionaryStore(it.value).textValue() - logger.trace("Reference dictionary key (${it.key}) resulted in value ($expressionValue)") - namedParameters[it.key] = expressionValue - } - logger.info("Parameter information : ({})", namedParameters) - return namedParameters - } - @Throws(BluePrintProcessorException::class) private fun populateResource(resourceAssignment: ResourceAssignment, sourceProperties: DatabaseResourceSource, rows: List<Map<String, Any>>) { val dName = resourceAssignment.dictionaryName diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DefaultResourceResolutionProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DefaultResourceResolutionProcessor.kt index 528705f1b..d487eab69 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DefaultResourceResolutionProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/DefaultResourceResolutionProcessor.kt @@ -17,7 +17,7 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor -import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.node.MissingNode import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException @@ -44,19 +44,11 @@ open class DefaultResourceResolutionProcessor : ResourceAssignmentProcessor() { override fun process(resourceAssignment: ResourceAssignment) { try { - // Check if It has Input - var value: JsonNode? - try { - value = raRuntimeService.getInputValue(resourceAssignment.name) - } catch (e: BluePrintProcessorException) { - // If value is null get it from default source - logger.info("Looking for defaultValue as couldn't find value in input For template key (${resourceAssignment.name})") + var value = getFromInput(resourceAssignment) + if (value == null || value is MissingNode) { value = resourceAssignment.property?.defaultValue + ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value) } - - logger.info("For template key (${resourceAssignment.name}) setting value as ($value)") - ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value) - // Check the value has populated for mandatory case ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment) } catch (e: Exception) { diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt index 9b7c70aa4..8e9606c4e 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt @@ -17,12 +17,18 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.processor +import com.fasterxml.jackson.databind.JsonNode +import org.apache.commons.collections.MapUtils import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService +import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BlueprintFunctionNode +import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintTemplateService +import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition import org.slf4j.LoggerFactory +import java.util.* abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssignment, ResourceAssignment> { @@ -31,19 +37,50 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssig lateinit var raRuntimeService: ResourceAssignmentRuntimeService lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition> - var scriptPropertyInstances: Map<String, Any> = hashMapOf() + var scriptPropertyInstances: MutableMap<String, Any> = hashMapOf() /** * This will be called from the scripts to serve instance from runtime to scripts. */ open fun <T> scriptPropertyInstanceType(name: String): T { return scriptPropertyInstances as? T - ?: throw BluePrintProcessorException("couldn't get script property instance ($name)") + ?: throw BluePrintProcessorException("couldn't get script property instance ($name)") + } + + open fun getFromInput(resourceAssignment: ResourceAssignment): JsonNode? { + var value: JsonNode? = null + try { + value = raRuntimeService.getInputValue(resourceAssignment.name) + ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value) + } catch (e: BluePrintProcessorException) { + // NoOp - couldn't find value from input + } + return value } open fun resourceDefinition(name: String): ResourceDefinition { return resourceDictionaries[name] - ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)") + ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)") + } + + open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, Any> { + val resolvedInputKeyMapping = HashMap<String, Any>() + if (MapUtils.isNotEmpty(inputKeyMapping)) { + for ((key, value) in inputKeyMapping) { + val resultValue = raRuntimeService.getResolutionStore(value) + val expressionValue = JacksonUtils.getValue(resultValue) + log.trace("Reference dictionary key ({}), value ({})", key, expressionValue) + resolvedInputKeyMapping[key] = expressionValue + } + } + return resolvedInputKeyMapping + } + + open fun resolveFromInputKeyMapping(valueToResolve: String, keyMapping: Map<String, Any>): String { + if (valueToResolve.isEmpty() || !valueToResolve.contains("$")) { + return valueToResolve + } + return BluePrintTemplateService.generateContent(valueToResolve, additionalContext = keyMapping) } override fun prepareRequest(resourceAssignment: ResourceAssignment): ResourceAssignment { diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt index 37b4774a7..f279f5445 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt @@ -20,8 +20,6 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.pr import com.fasterxml.jackson.databind.node.ArrayNode import com.fasterxml.jackson.databind.node.JsonNodeFactory import com.fasterxml.jackson.databind.node.MissingNode -import com.fasterxml.jackson.databind.node.NullNode -import com.fasterxml.jackson.databind.node.ObjectNode import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.RestResourceSource import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils @@ -63,11 +61,8 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS validate(resourceAssignment) // Check if It has Input - val value = raRuntimeService.getInputValue(resourceAssignment.name) - if (value !is MissingNode && value !is NullNode) { - logger.info("primary-db source template key (${resourceAssignment.name}) found from input and value is ($value)") - ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value) - } else { + val value = getFromInput(resourceAssignment) + if (value == null || value is MissingNode) { val dName = resourceAssignment.dictionaryName val dSource = resourceAssignment.dictionarySource val resourceDefinition = resourceDictionaries[dName] @@ -78,17 +73,22 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " } val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java) - - val urlPath = - checkNotNull(sourceProperties.urlPath) { "failed to get request urlPath for $dName under $dSource properties" } val path = nullToEmpty(sourceProperties.path) val inputKeyMapping = checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" } + val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping) + + // Resolving content Variables + val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping) + val urlPath = + resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping) + val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping) logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})") // Get the Rest Client Service val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties) - val response = restClientService.getResource(urlPath, String::class.java) + + val response = restClientService.exchangeResource(verb, urlPath, payload) if (response.isBlank()) { logger.warn("Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath)") } else { @@ -104,8 +104,8 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS } } - open fun blueprintWebClientService(resourceAssignment: ResourceAssignment, - restResourceSource: RestResourceSource): BlueprintWebClientService { + private fun blueprintWebClientService(resourceAssignment: ResourceAssignment, + restResourceSource: RestResourceSource): BlueprintWebClientService { return if (checkNotEmpty(restResourceSource.endpointSelector)) { val restPropertiesJson = raRuntimeService.resolveDSLExpression(restResourceSource.endpointSelector!!) blueprintRestLibPropertyService.blueprintWebClientService(restPropertiesJson) @@ -165,7 +165,9 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS } else -> { // Complex Types - val objectNode = responseNode as ObjectNode + entrySchemaType = + returnNotEmptyOrThrow(resourceAssignment.property?.type) { "Entry schema is not defined for dictionary ($dName) info" } + val objectNode = JsonNodeFactory.instance.objectNode() outputKeyMapping.map { val responseKeyValue = responseNode.get(it.key) val propertyTypeForDataType = diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt index b5b126ab6..d55ccacb2 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt @@ -24,7 +24,13 @@ import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.databind.node.NullNode import com.fasterxml.jackson.databind.node.ObjectNode import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService -import org.onap.ccsdk.apps.controllerblueprints.core.* +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes +import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmpty +import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow +import org.onap.ccsdk.apps.controllerblueprints.core.nullToEmpty +import org.onap.ccsdk.apps.controllerblueprints.core.returnNotEmptyOrThrow import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment @@ -36,7 +42,6 @@ class ResourceAssignmentUtils { private val logger: EELFLogger = EELFManager.getInstance().getLogger(ResourceAssignmentUtils::class.toString()) // TODO("Modify Value type from Any to JsonNode") - @Synchronized @Throws(BluePrintProcessorException::class) fun setResourceDataValue(resourceAssignment: ResourceAssignment, raRuntimeService: ResourceAssignmentRuntimeService, value: Any?) { @@ -85,7 +90,6 @@ class ResourceAssignmentUtils { } - @Synchronized fun setFailedResourceDataValue(resourceAssignment: ResourceAssignment, message: String?) { if (checkNotEmpty(resourceAssignment.name)) { resourceAssignment.updatedDate = Date() @@ -95,7 +99,6 @@ class ResourceAssignmentUtils { } } - @Synchronized @Throws(BluePrintProcessorException::class) fun assertTemplateKeyValueNotNull(resourceAssignment: ResourceAssignment) { val resourceProp = checkNotNull(resourceAssignment.property) { "Failed to populate mandatory resource resource mapping $resourceAssignment" } @@ -105,7 +108,6 @@ class ResourceAssignmentUtils { } } - @Synchronized @Throws(BluePrintProcessorException::class) fun generateResourceDataForAssignments(assignments: List<ResourceAssignment>): String { val result: String @@ -139,10 +141,6 @@ class ResourceAssignmentUtils { return resourceAssignmentRuntimeService } - /* - * Populate the Field property type for the Data type - */ - @Synchronized @Throws(BluePrintProcessorException::class) fun getPropertyType(raRuntimeService: ResourceAssignmentRuntimeService, dataTypeName: String, propertyName: String): String { lateinit var type: String diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessorTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessorTest.kt index f779054e9..6da3fd71e 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessorTest.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessorTest.kt @@ -21,6 +21,7 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.pr import org.junit.Test import org.junit.runner.RunWith import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService +import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.ComponentFunctionScriptingService import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.BlueprintJythonService import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.PythonExecutorProperty import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition @@ -36,7 +37,8 @@ import org.springframework.test.context.junit4.SpringRunner import kotlin.test.assertNotNull @RunWith(SpringRunner::class) -@ContextConfiguration(classes = [CapabilityResourceResolutionProcessor::class, BluePrintScriptsServiceImpl::class, +@ContextConfiguration(classes = [CapabilityResourceResolutionProcessor::class, ComponentFunctionScriptingService::class, + BluePrintScriptsServiceImpl::class, BlueprintJythonService::class, PythonExecutorProperty::class, MockCapabilityService::class]) @TestPropertySource(properties = ["blueprints.processor.functions.python.executor.modulePaths=./../../../../components/scripts/python/ccsdk_blueprints", @@ -62,9 +64,11 @@ class CapabilityResourceResolutionProcessorTest { scriptPropertyInstances["mock-service1"] = MockCapabilityService() scriptPropertyInstances["mock-service2"] = MockCapabilityService() + val instanceDependencies: List<String> = listOf() + val resourceAssignmentProcessor = capabilityResourceResolutionProcessor - .getKotlinResourceAssignmentProcessorInstance( - "ResourceAssignmentProcessor_cba\$ScriptResourceAssignmentProcessor", scriptPropertyInstances) + .scriptInstance("kotlin", + "ResourceAssignmentProcessor_cba\$ScriptResourceAssignmentProcessor", instanceDependencies) assertNotNull(resourceAssignmentProcessor, "couldn't get kotlin script resource assignment processor") diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/mapping/capability/jython-resource-definitions.json b/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/mapping/capability/jython-resource-definitions.json index fe89291c1..b565eec81 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/mapping/capability/jython-resource-definitions.json +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/resources/mapping/capability/jython-resource-definitions.json @@ -11,7 +11,7 @@ "type": "source-capability", "properties": { "script-type": "jython", - "script-class-reference": "SampleRAProcessor", + "script-class-reference": "Scripts/python/SampleRAProcessor.py", "instance-dependencies": [] } } |