diff options
author | vinal patel <vinal.narendrabhai.patel@ibm.com> | 2019-02-20 16:02:44 -0500 |
---|---|---|
committer | vinal patel <vinal.narendrabhai.patel@ibm.com> | 2019-03-18 17:24:27 -0400 |
commit | c1ddd6bed42f5293c20365e0e4415c4ccefb29e2 (patch) | |
tree | ea6e021d5a2afad58100622883bb537907e001d3 /ms/blueprintsprocessor/functions/resource-resolution/src/main | |
parent | b9859c7a69aece893b6cacce4dd0d4ce2d0badcb (diff) |
Ressource resolution using configurable database
Change-Id: I40338a221884d6f4df4c8a7dc3dac1f58f142074
Issue-ID: CCSDK-1092
Signed-off-by: vinal patel <vinal.narendrabhai.patel@ibm.com>
Diffstat (limited to 'ms/blueprintsprocessor/functions/resource-resolution/src/main')
1 files changed, 60 insertions, 45 deletions
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 e38a1ccad..a192989e5 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 @@ -19,40 +19,37 @@ package org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.pr import com.fasterxml.jackson.databind.node.JsonNodeFactory import com.fasterxml.jackson.databind.node.MissingNode -import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.DBLibGenericService +import com.fasterxml.jackson.databind.node.NullNode +import org.onap.ccsdk.apps.blueprintsprocessor.db.BluePrintDBLibGenericService +import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.BluePrintDBLibPropertySevice +import org.onap.ccsdk.apps.blueprintsprocessor.db.primary.PrimaryDBLibGenericService 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.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.* 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 import org.slf4j.LoggerFactory 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 * * @author Kapil Singal */ -@Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-primary-db") +@Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-processor-db") @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) -open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: DBLibGenericService) +open class DatabaseResourceAssignmentProcessor(private val bluePrintDBLibPropertySevice: BluePrintDBLibPropertySevice, private val primaryDBLibGenericService: PrimaryDBLibGenericService) : ResourceAssignmentProcessor() { private val logger = LoggerFactory.getLogger(DatabaseResourceAssignmentProcessor::class.java) override fun getName(): String { - return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-primary-db" + return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-processor-db" } override fun process(resourceAssignment: ResourceAssignment) { @@ -60,33 +57,16 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: validate(resourceAssignment) // Check if It has Input - val value = getFromInput(resourceAssignment) - if (value == null || value is MissingNode) { - val dName = resourceAssignment.dictionaryName - val dSource = resourceAssignment.dictionarySource - val resourceDefinition = resourceDictionaries[dName] - ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName") - val resourceSource = resourceDefinition.sources[dSource] - ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)") - val resourceSourceProperties = checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " } - val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java) - - 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" } - - 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(resolvedSql, resolvedInputKeyMapping) - if (rows.isNullOrEmpty()) { - logger.warn("Failed to get $dSource result for dictionary name ($dName) the query ($resolvedSql)") + try { + val value = raRuntimeService.getInputValue(resourceAssignment.name) + if (value !is NullNode && value !is MissingNode) { + logger.info("processor-db source template key (${resourceAssignment.name}) found from input and value is ($value)") + ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value) } else { - populateResource(resourceAssignment, sourceProperties, rows) + setValueFromDB(resourceAssignment) } + } catch (e: BluePrintProcessorException) { + setValueFromDB(resourceAssignment) } // Check the value has populated for mandatory case @@ -97,12 +77,36 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: } } - private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource): NamedParameterJdbcTemplate { + private fun setValueFromDB(resourceAssignment: ResourceAssignment) { + val dName = resourceAssignment.dictionaryName + val dSource = resourceAssignment.dictionarySource + val resourceDefinition = resourceDictionaries[dName] + ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName") + val resourceSource = resourceDefinition.sources[dSource] + ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)") + val resourceSourceProperties = checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " } + val sourceProperties = JacksonUtils.getInstanceFromMap(resourceSourceProperties, DatabaseResourceSource::class.java) + + 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 jdbcTemplate = blueprintDBLibService(sourceProperties) + + val rows = jdbcTemplate.query(sql, populateNamedParameter(inputKeyMapping)) + if (rows.isNullOrEmpty()) { + logger.warn("Failed to get $dSource result for dictionary name ($dName) the query ($sql)") + } else { + populateResource(resourceAssignment, sourceProperties, rows) + } + } + + private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource): BluePrintDBLibGenericService { return if (checkNotEmpty(sourceProperties.endpointSelector)) { val dbPropertiesJson = raRuntimeService.resolveDSLExpression(sourceProperties.endpointSelector!!) - dBLibGenericService.remoteJdbcTemplate(dbPropertiesJson) + bluePrintDBLibPropertySevice.JdbcTemplate(dbPropertiesJson) } else { - dBLibGenericService.primaryJdbcTemplate() + primaryDBLibGenericService } } @@ -111,9 +115,20 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: private fun validate(resourceAssignment: ResourceAssignment) { checkNotEmptyOrThrow(resourceAssignment.name, "resource assignment template key is not defined") checkNotEmptyOrThrow(resourceAssignment.dictionaryName, "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})") - checkEqualsOrThrow(ResourceDictionaryConstants.SOURCE_PRIMARY_DB, resourceAssignment.dictionarySource) { - "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PRIMARY_DB} but it is ${resourceAssignment.dictionarySource}" + checkEqualsOrThrow(ResourceDictionaryConstants.SOURCE_PROCESSOR_DB, resourceAssignment.dictionarySource) { + "resource assignment source is not ${ResourceDictionaryConstants.SOURCE_PROCESSOR_DB} but it is ${resourceAssignment.dictionarySource}" + } + } + + 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) @@ -126,7 +141,7 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: logger.info("Response processing type($type)") // Primitive Types - when(type) { + when (type) { in BluePrintTypes.validPrimitiveTypes() -> { val dbColumnValue = rows[0][outputKeyMapping[dName]] logger.info("For template key (${resourceAssignment.name}) setting value as ($dbColumnValue)") @@ -134,7 +149,7 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: } in BluePrintTypes.validCollectionTypes() -> { val entrySchemaType = returnNotEmptyOrThrow(resourceAssignment.property?.entrySchema?.type) { "Entry schema is not defined for dictionary ($dName) info" } - var arrayNode = JsonNodeFactory.instance.arrayNode() + val arrayNode = JsonNodeFactory.instance.arrayNode() rows.forEach { if (entrySchemaType in BluePrintTypes.validPrimitiveTypes()) { val dbColumnValue = it[outputKeyMapping[dName]] @@ -157,7 +172,7 @@ open class DatabaseResourceAssignmentProcessor(private val dBLibGenericService: else -> { // Complex Types val row = rows[0] - var objectNode = JsonNodeFactory.instance.objectNode() + val objectNode = JsonNodeFactory.instance.objectNode() for (mapping in outputKeyMapping.entries) { val dbColumnValue = checkNotNull(row[mapping.key]) val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, type, mapping.key) |