X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ms%2Fblueprintsprocessor%2Ffunctions%2Fresource-resolution%2Fsrc%2Fmain%2Fkotlin%2Forg%2Fonap%2Fccsdk%2Fcds%2Fblueprintsprocessor%2Ffunctions%2Fresource%2Fresolution%2Fprocessor%2FDatabaseResourceAssignmentProcessor.kt;h=98df3f1cfd7a453a1893c22371f502b3845751c2;hb=refs%2Fheads%2Fmaster;hp=db5307f17ecc5b48f7ab00568644cdc4598e4ee2;hpb=b96b44d6d7ca11dbbc3ad4bd2194df31fba5efb6;p=ccsdk%2Fcds.git diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt index db5307f17..6072a9233 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt @@ -17,14 +17,15 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor -import org.onap.ccsdk.cds.blueprintsprocessor.db.BlueprintDBLibGenericService +import com.fasterxml.jackson.databind.JsonNode +import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibGenericService import org.onap.ccsdk.cds.blueprintsprocessor.db.PrimaryDBLibGenericService -import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.BlueprintDBLibPropertyService +import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.BluePrintDBLibPropertyService import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.DatabaseResourceSource import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ExecutionServiceDomains -import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty import org.onap.ccsdk.cds.controllerblueprints.core.isNotEmpty import org.onap.ccsdk.cds.controllerblueprints.core.nullToEmpty @@ -32,7 +33,7 @@ import org.onap.ccsdk.cds.controllerblueprints.core.updateErrorMessage import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.cds.controllerblueprints.resource.dict.KeyIdentifier import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment -import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDictionaryConstants +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory import org.slf4j.LoggerFactory import org.springframework.beans.factory.config.ConfigurableBeanFactory import org.springframework.context.annotation.Scope @@ -47,7 +48,7 @@ import java.util.HashMap @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-db") @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) open class DatabaseResourceAssignmentProcessor( - private val bluePrintDBLibPropertyService: BlueprintDBLibPropertyService, + private val bluePrintDBLibPropertyService: BluePrintDBLibPropertyService, private val primaryDBLibGenericService: PrimaryDBLibGenericService ) : ResourceAssignmentProcessor() { @@ -66,7 +67,7 @@ open class DatabaseResourceAssignmentProcessor( } // Check the value has populated for mandatory case ResourceAssignmentUtils.assertTemplateKeyValueNotNull(resourceAssignment) - } catch (e: BlueprintProcessorException) { + } catch (e: BluePrintProcessorException) { val errorMsg = "Failed to process Database resource resolution in template key ($resourceAssignment) assignments." throw e.updateErrorMessage( ExecutionServiceDomains.RESOURCE_RESOLUTION, errorMsg, @@ -74,11 +75,11 @@ open class DatabaseResourceAssignmentProcessor( ) } catch (e: Exception) { ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message) - throw BlueprintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", e) + throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", e) } } - private fun setValueFromDB(resourceAssignment: ResourceAssignment) { + open fun setValueFromDB(resourceAssignment: ResourceAssignment) { val dName = resourceAssignment.dictionaryName!! val dSource = resourceAssignment.dictionarySource!! val resourceDefinition = resourceDefinition(dName) @@ -86,7 +87,7 @@ open class DatabaseResourceAssignmentProcessor( /** Check Resource Assignment has the source definitions, If not get from Resource Definition **/ val resourceSource = resourceAssignment.dictionarySourceDefinition ?: resourceDefinition?.sources?.get(dSource) - ?: throw BlueprintProcessorException("couldn't get resource definition $dName source($dSource)") + ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)") val resourceSourceProperties = checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " } @@ -100,10 +101,15 @@ open class DatabaseResourceAssignmentProcessor( "failed to get input-key-mappings for $dName under $dSource properties" } - sourceProperties.inputKeyMapping - ?.mapValues { raRuntimeService.getResolutionStore(it.value) } - ?.map { KeyIdentifier(it.key, it.value) } - ?.let { resourceAssignment.keyIdentifiers.addAll(it) } + val resolvedInputKeyMapping = resolveInputKeyMappingVariables( + inputKeyMapping, + resourceAssignment.templatingConstants + ).toMutableMap() + logger.info("\nResolved Input Key mappings: \n$resolvedInputKeyMapping") + + resolvedInputKeyMapping.map { KeyIdentifier(it.key, it.value) }.let { + resourceAssignment.keyIdentifiers.addAll(it) + } logger.info( "DatabaseResource ($dSource) dictionary information: " + @@ -111,15 +117,15 @@ open class DatabaseResourceAssignmentProcessor( ) val jdbcTemplate = blueprintDBLibService(sourceProperties, dSource) - 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) + val rows = jdbcTemplate.query(sql, populateNamedParameter(resolvedInputKeyMapping)) + if (rows.isEmpty()) { + logger.warn("Emptyset from dictionary-source($dSource) for dictionary name ($dName) the query ($sql).") } + logger.debug("Query returned ${rows.size} values") + populateResource(resourceAssignment, sourceProperties, rows) } - private fun blueprintDBLibService(sourceProperties: DatabaseResourceSource, selector: String): BlueprintDBLibGenericService { + open fun blueprintDBLibService(sourceProperties: DatabaseResourceSource, selector: String): BluePrintDBLibGenericService { return if (isNotEmpty(sourceProperties.endpointSelector)) { val dbPropertiesJson = raRuntimeService.resolveDSLExpression(sourceProperties.endpointSelector!!) bluePrintDBLibPropertyService.JdbcTemplate(dbPropertiesJson) @@ -128,25 +134,27 @@ open class DatabaseResourceAssignmentProcessor( } } - @Throws(BlueprintProcessorException::class) - private fun validate(resourceAssignment: ResourceAssignment) { + @Throws(BluePrintProcessorException::class) + open fun validate(resourceAssignment: ResourceAssignment) { checkNotEmpty(resourceAssignment.name) { "resource assignment template key is not defined" } checkNotEmpty(resourceAssignment.dictionaryName) { "resource assignment dictionary name is not defined for template key (${resourceAssignment.name})" } check(resourceAssignment.dictionarySource in getListOfDBSources()) { - "resource assignment source is not ${ResourceDictionaryConstants.PROCESSOR_DB} but it is ${resourceAssignment.dictionarySource}" + "resource assignment source ${resourceAssignment.dictionarySource} is not registered in \"resourceSourceMappings\"" } } // placeholder to get the list of DB sources. - // TODO: This will be replaced with a DB - private fun getListOfDBSources(): Array = arrayOf(ResourceDictionaryConstants.PROCESSOR_DB) + open fun getListOfDBSources(): Array { + return ResourceSourceMappingFactory.getRegisterSourceMapping() + .resourceSourceMappings.filterValues { it == "source-db" }.keys.toTypedArray() + } - private fun populateNamedParameter(inputKeyMapping: Map): Map { + open fun populateNamedParameter(inputKeyMapping: Map): Map { val namedParameters = HashMap() inputKeyMapping.forEach { - val expressionValue = raRuntimeService.getResolutionStore(it.value).textValue() + val expressionValue = it.value.textValue() logger.trace("Reference dictionary key (${it.key}) resulted in value ($expressionValue)") namedParameters[it.key] = expressionValue } @@ -156,8 +164,8 @@ open class DatabaseResourceAssignmentProcessor( return namedParameters } - @Throws(BlueprintProcessorException::class) - private fun populateResource( + @Throws(BluePrintProcessorException::class) + open fun populateResource( resourceAssignment: ResourceAssignment, sourceProperties: DatabaseResourceSource, rows: List>