diff options
author | 2019-08-05 09:50:15 -0400 | |
---|---|---|
committer | 2019-08-09 21:51:14 +0000 | |
commit | 977287b586e27e2f835a5d24950abf1f2adae8be (patch) | |
tree | 86200548402e907c6cf969bd4f0a516eed937ed0 /ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org | |
parent | 4001ac13397c082ee97c7ff440fa2ead5d50b421 (diff) |
Add resource definition resolution service.
Change-Id: Ife75d290540e3ed0e0dfd0a82785a498607a2d25
Issue-ID: CCSDK-1577
Signed-off-by: Brinda Santh <brindasanth@in.ibm.com>
Diffstat (limited to 'ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org')
9 files changed, 202 insertions, 93 deletions
diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceAssignmentProcessorScriptConfiguration.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceAssignmentProcessorScriptConfiguration.kt deleted file mode 100644 index 5f3a5cdaf..000000000 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceAssignmentProcessorScriptConfiguration.kt +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * - * 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. - */ - -package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution - -import org.jetbrains.kotlin.script.util.DependsOn -import org.jetbrains.kotlin.script.util.Repository -import kotlin.script.experimental.annotations.KotlinScript -import kotlin.script.experimental.api.ScriptCompilationConfiguration -import kotlin.script.experimental.api.defaultImports -import kotlin.script.experimental.jvm.dependenciesFromCurrentContext -import kotlin.script.experimental.jvm.jvm - -@KotlinScript(fileExtension = "resourceassignmentprocessor.kts", - compilationConfiguration = ResourceAssignmentProcessorScriptConfiguration::class) -abstract class ResourceAssignmentProcessorScript { - -} - -object ResourceAssignmentProcessorScriptConfiguration : ScriptCompilationConfiguration( - { - defaultImports(DependsOn::class, Repository::class) - jvm { - dependenciesFromCurrentContext( - wholeClasspath = true - ) - } - } -)
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceDefinitionDSL.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceDefinitionDSL.kt index a48762832..1b02ebe70 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceDefinitionDSL.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceDefinitionDSL.kt @@ -163,6 +163,35 @@ class ResourceAssignmentBuilder(private val name: String, private val dictionary resourceAssignment.property = PropertyDefinitionBuilder(name, type, required, description).apply(block).build() } + fun source(source: NodeTemplate) { + resourceAssignment.dictionarySourceDefinition = source + } + + fun sourceInput(block: SourceInputNodeTemplateBuilder.() -> Unit) { + resourceAssignment.dictionarySourceDefinition = SourceInputNodeTemplateBuilder(dictionarySource, "") + .apply(block).build() + } + + fun sourceDefault(block: SourceDefaultNodeTemplateBuilder.() -> Unit) { + resourceAssignment.dictionarySourceDefinition = SourceDefaultNodeTemplateBuilder(dictionarySource, "") + .apply(block).build() + } + + fun sourceDb(block: SourceDbNodeTemplateBuilder.() -> Unit) { + resourceAssignment.dictionarySourceDefinition = SourceDbNodeTemplateBuilder(dictionarySource, "") + .apply(block).build() + } + + fun sourceRest(block: SourceRestNodeTemplateBuilder.() -> Unit) { + resourceAssignment.dictionarySourceDefinition = SourceRestNodeTemplateBuilder(dictionarySource, "") + .apply(block).build() + } + + fun sourceCapability(block: SourceCapabilityNodeTemplateBuilder.() -> Unit) { + resourceAssignment.dictionarySourceDefinition = SourceCapabilityNodeTemplateBuilder(dictionarySource, "") + .apply(block).build() + } + fun dependencies(dependencies: MutableList<String>) { resourceAssignment.dependencies = dependencies } diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt index b9b710390..d3b69c713 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt @@ -26,6 +26,7 @@ import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.R import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.TemplateResolutionService import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.ResourceAssignmentProcessor import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceDefinitionUtils.createResourceAssignments import org.onap.ccsdk.cds.controllerblueprints.core.* import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintTemplateService @@ -36,6 +37,7 @@ import org.onap.ccsdk.cds.controllerblueprints.resource.dict.utils.BulkResourceS import org.slf4j.LoggerFactory import org.springframework.context.ApplicationContext import org.springframework.stereotype.Service +import java.util.* interface ResourceResolutionService { @@ -50,6 +52,14 @@ interface ResourceResolutionService { suspend fun resolveResources(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String, artifactPrefix: String, properties: Map<String, Any>): String + /** Resolve resources for all the sources defined in a particular resource Definition[resolveDefinition] + * with other [resourceDefinitions] dependencies for the sources [sources] + * Used to get the same resource values from multiple sources. **/ + suspend fun resolveResourceDefinition(blueprintRuntimeService: BluePrintRuntimeService<*>, + resourceDefinitions: MutableMap<String, ResourceDefinition>, + resolveDefinition: String, sources: List<String>) + : MutableMap<String, JsonNode> + suspend fun resolveResourceAssignments(blueprintRuntimeService: BluePrintRuntimeService<*>, resourceDefinitions: MutableMap<String, ResourceDefinition>, resourceAssignments: MutableList<ResourceAssignment>, @@ -86,12 +96,12 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica properties: Map<String, Any>): MutableMap<String, JsonNode> { val resourceAssignmentRuntimeService = - ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString()) + ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString()) val resolvedParams: MutableMap<String, JsonNode> = hashMapOf() artifactNames.forEach { artifactName -> val resolvedContent = resolveResources(resourceAssignmentRuntimeService, nodeTemplateName, - artifactName, properties) + artifactName, properties) resolvedParams[artifactName] = resolvedContent.asJsonType() } @@ -151,6 +161,21 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica return resolvedContent } + override suspend fun resolveResourceDefinition(blueprintRuntimeService: BluePrintRuntimeService<*>, + resourceDefinitions: MutableMap<String, ResourceDefinition>, + resolveDefinition: String, sources: List<String>) + : MutableMap<String, JsonNode> { + + // Populate Dummy Resource Assignments + val resourceAssignments = createResourceAssignments(resourceDefinitions, resolveDefinition, sources) + + resolveResourceAssignments(blueprintRuntimeService, resourceDefinitions, resourceAssignments, + UUID.randomUUID().toString(), hashMapOf()) + + // Get the data from Resource Assignments + return ResourceAssignmentUtils.generateResourceForAssignments(resourceAssignments) + } + /** * Iterate the Batch, get the Resource Assignment, dictionary Name, Look for the Resource definition for the * name, then get the type of the Resource Definition, Get the instance for the Resource Type and process the @@ -165,7 +190,7 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica val bulkSequenced = BulkResourceSequencingUtils.process(resourceAssignments) // Check the BlueprintRuntime Service Should be ResourceAssignmentRuntimeService - val resourceAssignmentRuntimeService = if (!(blueprintRuntimeService is ResourceAssignmentRuntimeService)) { + val resourceAssignmentRuntimeService = if (blueprintRuntimeService !is ResourceAssignmentRuntimeService) { ResourceAssignmentUtils.transformToRARuntimeService(blueprintRuntimeService, artifactPrefix) } else { blueprintRuntimeService diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessor.kt index 49cb83e09..0ea71ece7 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessor.kt @@ -27,14 +27,12 @@ import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment import org.slf4j.LoggerFactory import org.springframework.beans.factory.config.ConfigurableBeanFactory -import org.springframework.context.ApplicationContext import org.springframework.context.annotation.Scope import org.springframework.stereotype.Service @Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-capability") @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) -open class CapabilityResourceResolutionProcessor(private val applicationContext: ApplicationContext, - private var componentFunctionScriptingService: ComponentFunctionScriptingService) +open class CapabilityResourceResolutionProcessor(private var componentFunctionScriptingService: ComponentFunctionScriptingService) : ResourceAssignmentProcessor() { private val log = LoggerFactory.getLogger(CapabilityResourceResolutionProcessor::class.java) @@ -47,12 +45,14 @@ open class CapabilityResourceResolutionProcessor(private val applicationContext: override suspend fun processNB(resourceAssignment: ResourceAssignment) { - val resourceDefinition = resourceDictionaries[resourceAssignment.dictionaryName] - ?: throw BluePrintProcessorException("couldn't get resource definition for ${resourceAssignment.dictionaryName}") + val dName = resourceAssignment.dictionaryName!! + val dSource = resourceAssignment.dictionarySource!! + val resourceDefinition = resourceDefinition(resourceAssignment.dictionaryName!!) - val resourceSource = resourceDefinition.sources[resourceAssignment.dictionarySource] - ?: throw BluePrintProcessorException("couldn't get resource definition " + - "${resourceAssignment.dictionaryName} source(${resourceAssignment.dictionarySource})") + /** 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)") val resourceSourceProps = checkNotNull(resourceSource.properties) { "failed to get $resourceSource properties" } /** @@ -96,10 +96,6 @@ open class CapabilityResourceResolutionProcessor(private val applicationContext: .scriptInstance<ResourceAssignmentProcessor>(raRuntimeService.bluePrintContext(), scriptType, scriptClassReference) - instanceDependencies.forEach { instanceDependency -> - scriptPropertyInstances[instanceDependency] = applicationContext - .getBean(instanceDependency) - } return scriptComponent } 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 987390fdb..600de134e 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 @@ -18,8 +18,6 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor import com.fasterxml.jackson.databind.node.JsonNodeFactory -import com.fasterxml.jackson.databind.node.MissingNode -import com.fasterxml.jackson.databind.node.NullNode import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibGenericService import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.BluePrintDBLibPropertySevice import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.PrimaryDBLibGenericService @@ -41,7 +39,7 @@ import java.util.* * * @author Kapil Singal */ -@Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-processor-db") +@Service("${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-db") @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) open class DatabaseResourceAssignmentProcessor(private val bluePrintDBLibPropertySevice: BluePrintDBLibPropertySevice, private val primaryDBLibGenericService: PrimaryDBLibGenericService) @@ -50,7 +48,7 @@ open class DatabaseResourceAssignmentProcessor(private val bluePrintDBLibPropert private val logger = LoggerFactory.getLogger(DatabaseResourceAssignmentProcessor::class.java) override fun getName(): String { - return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-processor-db" + return "${PREFIX_RESOURCE_RESOLUTION_PROCESSOR}source-db" } override suspend fun processNB(resourceAssignment: ResourceAssignment) { @@ -60,7 +58,7 @@ open class DatabaseResourceAssignmentProcessor(private val bluePrintDBLibPropert // Check if It has Input try { val value = raRuntimeService.getInputValue(resourceAssignment.name) - if (value !is NullNode && value !is MissingNode) { + if (value.returnNullIfMissing() != null) { logger.info("processor-db source template key (${resourceAssignment.name}) found from input and value is ($value)") ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value) } else { @@ -79,11 +77,13 @@ open class DatabaseResourceAssignmentProcessor(private val bluePrintDBLibPropert } 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] + val dName = resourceAssignment.dictionaryName!! + val dSource = resourceAssignment.dictionarySource!! + val resourceDefinition = resourceDefinition(dName) + + /** 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)") val resourceSourceProperties = checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " @@ -188,7 +188,7 @@ open class DatabaseResourceAssignmentProcessor(private val bluePrintDBLibPropert val row = rows[0] val objectNode = JacksonUtils.objectMapper.createObjectNode() for (mapping in outputKeyMapping.entries) { - val dbColumnValue = checkNotNull(row[mapping.key]) + val dbColumnValue = checkNotNull(row[mapping.value]) val propertyTypeForDataType = ResourceAssignmentUtils.getPropertyType(raRuntimeService, type, mapping.key) JacksonUtils.populatePrimitiveValues(mapping.key, dbColumnValue, propertyTypeForDataType, objectNode) } diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt index 1abcea825..2a7d19b94 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt @@ -38,7 +38,7 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssig private val log = LoggerFactory.getLogger(ResourceAssignmentProcessor::class.java) lateinit var raRuntimeService: ResourceAssignmentRuntimeService - lateinit var resourceDictionaries: MutableMap<String, ResourceDefinition> + var resourceDictionaries: MutableMap<String, ResourceDefinition> = hashMapOf() var scriptPropertyInstances: MutableMap<String, Any> = hashMapOf() lateinit var scriptType: String @@ -62,9 +62,8 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssig return value } - open fun resourceDefinition(name: String): ResourceDefinition { - return resourceDictionaries[name] - ?: throw BluePrintProcessorException("couldn't get resource definition for ($name)") + open fun resourceDefinition(name: String): ResourceDefinition? { + return if (resourceDictionaries.containsKey(name)) resourceDictionaries[name] else null } open fun resolveInputKeyMappingVariables(inputKeyMapping: Map<String, String>): Map<String, JsonNode> { diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt index 3bf0b359a..57e028618 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt @@ -17,7 +17,6 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor -import com.fasterxml.jackson.databind.node.ArrayNode import com.fasterxml.jackson.databind.node.MissingNode import com.fasterxml.jackson.databind.node.NullNode import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.PREFIX_RESOURCE_RESOLUTION_PROCESSOR @@ -56,29 +55,30 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS // Check if It has Input val value = getFromInput(resourceAssignment) if (value == null || value is MissingNode || value is NullNode) { - val dName = resourceAssignment.dictionaryName - val dSource = resourceAssignment.dictionarySource - val resourceDefinition = resourceDictionaries[dName] - ?: throw BluePrintProcessorException("couldn't get resource dictionary definition for $dName") + val dName = resourceAssignment.dictionaryName!! + val dSource = resourceAssignment.dictionarySource!! + val resourceDefinition = resourceDefinition(dName) - val resourceSource = resourceDefinition.sources[dSource] - ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)") + /** Check Resource Assignment has the source definitions, If not get from Resource Definitions **/ + val resourceSource = resourceAssignment.dictionarySourceDefinition + ?: resourceDefinition?.sources?.get(dSource) + ?: throw BluePrintProcessorException("couldn't get resource definition $dName source($dSource)") val resourceSourceProperties = - checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " } + checkNotNull(resourceSource.properties) { "failed to get source properties for $dName " } val sourceProperties = - JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java) + JacksonUtils.getInstanceFromMap(resourceSourceProperties, RestResourceSource::class.java) val path = nullToEmpty(sourceProperties.path) val inputKeyMapping = - checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" } + checkNotNull(sourceProperties.inputKeyMapping) { "failed to get input-key-mappings for $dName under $dSource properties" } val resolvedInputKeyMapping = resolveInputKeyMappingVariables(inputKeyMapping).toMutableMap() // Resolving content Variables val payload = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.payload), resolvedInputKeyMapping) val urlPath = - resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping) + resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping) val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping) logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})") @@ -91,12 +91,11 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS val outputKeyMapping = sourceProperties.outputKeyMapping if (responseStatusCode in 200..299 && outputKeyMapping.isNullOrEmpty()) { logger.info("AS>> outputKeyMapping==null, will not populateResource") - } - else if (responseStatusCode in 200..299 && !responseBody.isBlank()) { + } else if (responseStatusCode in 200..299 && !responseBody.isBlank()) { populateResource(resourceAssignment, sourceProperties, responseBody, path) } else { val errMsg = - "Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath) response_code: ($responseStatusCode)" + "Failed to get $dSource result for dictionary name ($dName) using urlPath ($urlPath) response_code: ($responseStatusCode)" logger.warn(errMsg) throw BluePrintProcessorException(errMsg) } @@ -106,7 +105,7 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS } catch (e: Exception) { ResourceAssignmentUtils.setFailedResourceDataValue(resourceAssignment, e.message) throw BluePrintProcessorException("Failed in template key ($resourceAssignment) assignments with: ${e.message}", - e) + e) } } @@ -161,13 +160,13 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS outputKeyMapping.map { val responseKeyValue = responseSingleJsonNode.get(it.key) val propertyTypeForDataType = ResourceAssignmentUtils - .getPropertyType(raRuntimeService, entrySchemaType, it.key) + .getPropertyType(raRuntimeService, entrySchemaType, it.key) logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), " + "type ({$propertyTypeForDataType})") JacksonUtils.populateJsonNodeValues(it.value, - responseKeyValue, propertyTypeForDataType, arrayChildNode) + responseKeyValue, propertyTypeForDataType, arrayChildNode) } arrayNode.add(arrayChildNode) } @@ -185,7 +184,7 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS outputKeyMapping.map { val responseKeyValue = responseNode.get(it.key) val propertyTypeForDataType = ResourceAssignmentUtils - .getPropertyType(raRuntimeService, entrySchemaType, it.key) + .getPropertyType(raRuntimeService, entrySchemaType, it.key) logger.info("For List Type Resource: key (${it.key}), value ($responseKeyValue), type ({$propertyTypeForDataType})") JacksonUtils.populateJsonNodeValues(it.value, responseKeyValue, propertyTypeForDataType, objectNode) diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt index 8a8bfbf32..f8024d92e 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt @@ -19,7 +19,6 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.uti import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.databind.node.NullNode import com.fasterxml.jackson.databind.node.ObjectNode import com.fasterxml.jackson.databind.node.TextNode import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService @@ -70,7 +69,8 @@ class ResourceAssignmentUtils { try { if (resourceProp.type.isNotEmpty()) { logger.info("Setting Resource Value ($value) for Resource Name " + - "(${resourceAssignment.name}) of type (${resourceProp.type})") + "(${resourceAssignment.name}), definition(${resourceAssignment.dictionaryName}) " + + "of type (${resourceProp.type})") setResourceValue(resourceAssignment, raRuntimeService, value) resourceAssignment.updatedDate = Date() resourceAssignment.updatedBy = BluePrintConstants.USER_SYSTEM @@ -106,7 +106,7 @@ class ResourceAssignmentUtils { "Failed to populate mandatory resource resource mapping $resourceAssignment" } if (resourceProp.required != null && resourceProp.required!! - && (resourceProp.value == null || resourceProp.value !is NullNode)) { + && (resourceProp.value == null || resourceProp.value!!.returnNullIfMissing() == null)) { logger.error("failed to populate mandatory resource mapping ($resourceAssignment)") throw BluePrintProcessorException("failed to populate mandatory resource mapping ($resourceAssignment)") } @@ -137,6 +137,21 @@ class ResourceAssignmentUtils { return result } + @Throws(BluePrintProcessorException::class) + fun generateResourceForAssignments(assignments: List<ResourceAssignment>): MutableMap<String, JsonNode> { + val data: MutableMap<String, JsonNode> = hashMapOf() + assignments.forEach { + if (isNotEmpty(it.name) && it.property != null) { + val rName = it.name + val type = nullToEmpty(it.property?.type).toLowerCase() + val value = useDefaultValueIfNull(it, rName) + logger.trace("Generating Resource name ($rName), type ($type), value ($value)") + data[rName] = value + } + } + return data + } + private fun useDefaultValueIfNull(resourceAssignment: ResourceAssignment, resourceAssignmentName: String): JsonNode { if (resourceAssignment.property?.value == null) { val defaultValue = "\${$resourceAssignmentName}" diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceDefinitionUtils.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceDefinitionUtils.kt new file mode 100644 index 000000000..15a8c6c80 --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceDefinitionUtils.kt @@ -0,0 +1,88 @@ +/* + * Copyright © 2019 IBM. + * + * 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. + */ + +package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils + +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString +import org.onap.ccsdk.cds.controllerblueprints.core.asListOfString +import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition + +object ResourceDefinitionUtils { + + fun definitionDependencies(definition: ResourceDefinition, sources: List<String>): Set<String> { + val dependencies: MutableSet<String> = mutableSetOf() + definition.sources.forEach { (sourceName, source) -> + if (sources.contains(sourceName)) { + val keyDependenciesExists = source.properties?.containsKey("key-dependencies") ?: false + if (keyDependenciesExists) { + dependencies.addAll(source.properties!!["key-dependencies"]!!.asListOfString()) + } + } + } + return dependencies + } + + /** Create a processing resource assignments for the resource definition */ + fun createResourceAssignments(resourceDefinitions: MutableMap<String, ResourceDefinition>, + resolveDefinition: String, sources: List<String>) + : MutableList<ResourceAssignment> { + /** Check if resolve definition is defined in the resource definition Map */ + val resourceDefinition = resourceDefinitions[resolveDefinition] + ?: throw BluePrintProcessorException("failed to get resolve definition($resolveDefinition)") + + val resourceAssignments: MutableList<ResourceAssignment> = arrayListOf() + /** Get the dependency property fields for the the resource definition to resolve */ + val definitionDependencies = definitionDependencies(resourceDefinition, sources) + definitionDependencies.forEach { definitionDependencyName -> + val definitionDependency = resourceDefinitions[definitionDependencyName] + ?: throw BluePrintProcessorException("failed to get dependency definition($definitionDependencyName)") + + val resourceAssignment = ResourceAssignment().apply { + name = definitionDependency.name + dictionaryName = definitionDependency.name + /** The assumption is al resource are already resolved and shall get as input source */ + dictionarySource = "input" + property = definitionDependency.property + } + resourceAssignments.add(resourceAssignment) + } + + resourceDefinition.sources.forEach { (sourceName, source) -> + if (sources.contains(sourceName)) { + val resourceAssignment = ResourceAssignment().apply { + name = "$sourceName:${resourceDefinition.name}" + dictionaryName = resourceDefinition.name + dictionarySource = sourceName + dictionarySourceDefinition = source + // Clone the PropertyDefinition, otherwise property value will be overridden + property = JacksonUtils + .readValue(resourceDefinition.property.asJsonString(), PropertyDefinition::class.java) + val keyDependenciesExists = source.properties?.containsKey("key-dependencies") ?: false + if (keyDependenciesExists) { + dependencies = source.properties!!["key-dependencies"]!!.asListOfString().toMutableList() + } + } + resourceAssignments.add(resourceAssignment) + } + } + // Populate Resource Definition's dependencies as Input Resource Assignment + return resourceAssignments + } +}
\ No newline at end of file |