diff options
Diffstat (limited to 'ms/blueprintsprocessor/functions/resource-resolution')
17 files changed, 1175 insertions, 225 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 new file mode 100644 index 000000000..141bad619 --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceDefinitionDSL.kt @@ -0,0 +1,210 @@ +/* + * 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 + +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes +import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate +import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition +import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertyDefinitionBuilder +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment +import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition + +/** Resource Definition DSL **/ +fun BluePrintTypes.resourceDefinitions(block: ResourceDefinitionsBuilder.() -> Unit) + : MutableMap<String, ResourceDefinition> { + return ResourceDefinitionsBuilder().apply(block).build() +} + +fun BluePrintTypes.resourceDefinition(name: String, description: String, + block: ResourceDefinitionBuilder.() -> Unit): ResourceDefinition { + return ResourceDefinitionBuilder(name, description).apply(block).build() +} + +/** Resource Mapping DSL **/ +fun BluePrintTypes.resourceAssignments(block: ResourceAssignmentsBuilder.() -> Unit) + : MutableMap<String, ResourceAssignment> { + return ResourceAssignmentsBuilder().apply(block).build() +} + +fun BluePrintTypes.resourceAssignment(name: String, dictionaryName: String, dictionarySource: String, + block: ResourceAssignmentBuilder.() -> Unit): ResourceAssignment { + return ResourceAssignmentBuilder(name, dictionaryName, dictionarySource).apply(block).build() +} + +class ResourceDefinitionsBuilder() { + private val resourceDefinitions: MutableMap<String, ResourceDefinition> = hashMapOf() + + fun resourceDefinition(name: String, description: String, + block: ResourceDefinitionBuilder.() -> Unit) { + val resourceDefinition = ResourceDefinitionBuilder(name, description).apply(block).build() + resourceDefinitions[resourceDefinition.name] = resourceDefinition + } + + fun resourceDefinition(resourceDefinition: ResourceDefinition) { + resourceDefinitions[resourceDefinition.name] = resourceDefinition + } + + fun build(): MutableMap<String, ResourceDefinition> { + return resourceDefinitions + } +} + +class ResourceDefinitionBuilder(private val name: String, private val description: String) { + private val resourceDefinition = ResourceDefinition() + + fun updatedBy(updatedBy: String) { + resourceDefinition.updatedBy = updatedBy + } + + fun tags(tags: String) { + resourceDefinition.tags = tags + } + + fun property(property: PropertyDefinition) { + resourceDefinition.property = property + } + + fun property(type: String, required: Boolean) { + resourceDefinition.property = PropertyDefinitionBuilder(name, type, required, description).build() + } + + fun property(type: String, required: Boolean, + block: PropertyDefinitionBuilder.() -> Unit) { + resourceDefinition.property = PropertyDefinitionBuilder(name, type, required, description).apply(block).build() + } + + fun sources(block: ResourceDefinitionSourcesBuilder.() -> Unit) { + resourceDefinition.sources = ResourceDefinitionSourcesBuilder().apply(block).build() + } + + fun sources(sources: MutableMap<String, NodeTemplate>) { + resourceDefinition.sources = sources + } + + fun build(): ResourceDefinition { + resourceDefinition.name = name + return resourceDefinition + } +} + +class ResourceDefinitionSourcesBuilder { + var sources: MutableMap<String, NodeTemplate> = hashMapOf() + + fun source(source: NodeTemplate) { + sources[source.id!!] = source + } + + fun sourceInput(id: String, description: String, block: SourceInputNodeTemplateBuilder.() -> Unit) { + sources[id] = SourceInputNodeTemplateBuilder(id, description).apply(block).build() + } + + fun sourceDefault(id: String, description: String, block: SourceDefaultNodeTemplateBuilder.() -> Unit) { + sources[id] = SourceDefaultNodeTemplateBuilder(id, description).apply(block).build() + } + + fun sourceDb(id: String, description: String, block: SourceDbNodeTemplateBuilder.() -> Unit) { + sources[id] = SourceDbNodeTemplateBuilder(id, description).apply(block).build() + } + + fun sourceRest(id: String, description: String, block: SourceRestNodeTemplateBuilder.() -> Unit) { + sources[id] = SourceRestNodeTemplateBuilder(id, description).apply(block).build() + } + + fun sourceCapability(id: String, description: String, block: SourceCapabilityNodeTemplateBuilder.() -> Unit) { + sources[id] = SourceCapabilityNodeTemplateBuilder(id, description).apply(block).build() + } + + fun build(): MutableMap<String, NodeTemplate> { + return sources + } +} + +class ResourceAssignmentsBuilder() { + private val resourceAssignments: MutableMap<String, ResourceAssignment> = hashMapOf() + + fun resourceAssignment(name: String, dictionaryName: String, dictionarySource: String, + block: ResourceAssignmentBuilder.() -> Unit) { + val resourceAssignment = ResourceAssignmentBuilder(name, dictionaryName, dictionarySource).apply(block).build() + resourceAssignments[resourceAssignment.name] = resourceAssignment + } + + fun resourceAssignment(resourceAssignment: ResourceAssignment) { + resourceAssignments[resourceAssignment.name] = resourceAssignment + } + + fun build(): MutableMap<String, ResourceAssignment> { + return resourceAssignments + } +} + +class ResourceAssignmentBuilder(private val name: String, private val dictionaryName: String, + private val dictionarySource: String) { + private val resourceAssignment = ResourceAssignment() + + fun inputParameter(inputParameter: Boolean) { + resourceAssignment.inputParameter = inputParameter + } + + fun property(type: String, required: Boolean, description: String? = "") { + resourceAssignment.property = PropertyDefinitionBuilder(name, type, required, description).build() + } + + fun property(type: String, required: Boolean, description: String? = "", + block: PropertyDefinitionBuilder.() -> Unit) { + 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 + } + + fun build(): ResourceAssignment { + resourceAssignment.name = name + resourceAssignment.dictionaryName = dictionaryName + resourceAssignment.dictionarySource = dictionarySource + return resourceAssignment + } +}
\ 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/ResourceResolutionComponent.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponent.kt index 875b9ae32..0521919aa 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponent.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponent.kt @@ -53,7 +53,7 @@ open class ResourceResolutionComponent(private val resourceResolutionService: Re override suspend fun processNB(executionRequest: ExecutionServiceInput) { - val occurrence = getOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE).asInt() + val occurrence = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_OCCURRENCE)?.asInt() ?: 1 val resolutionKey = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOLUTION_KEY)?.returnNullIfMissing()?.textValue() ?: "" val storeResult = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_STORE_RESULT)?.asBoolean() ?: false val resourceId = getOptionalOperationInput(ResourceResolutionConstants.RESOURCE_RESOLUTION_INPUT_RESOURCE_ID)?.returnNullIfMissing()?.textValue() ?: "" diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponentDSL.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponentDSL.kt index a44750d11..40ea47e33 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponentDSL.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponentDSL.kt @@ -20,7 +20,7 @@ import com.fasterxml.jackson.databind.JsonNode import org.onap.ccsdk.cds.controllerblueprints.core.* import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType -import org.onap.ccsdk.cds.controllerblueprints.core.dsl.AbstractNodeTemplateImplBuilder +import org.onap.ccsdk.cds.controllerblueprints.core.dsl.AbstractNodeTemplateOperationImplBuilder import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType @@ -81,89 +81,91 @@ fun BluePrintTypes.nodeTypeComponentResourceResolution(): NodeType { /** Component Builder */ fun BluePrintTypes.nodeTemplateComponentResourceResolution(id: String, description: String, - block: ComponentResourceResolutionNodeTemplateImplBuilder.() -> Unit) + block: ComponentResourceResolutionNodeTemplateBuilder.() -> Unit) : NodeTemplate { - return ComponentResourceResolutionNodeTemplateImplBuilder(id, description).apply(block).build() + return ComponentResourceResolutionNodeTemplateBuilder(id, description).apply(block).build() } -class ComponentResourceResolutionNodeTemplateImplBuilder(id: String, description: String) : - AbstractNodeTemplateImplBuilder<ComponentResourceResolutionInputAssignmentBuilder, - ComponentResourceResolutionOutputAssignmentBuilder>(id, "component-script-executor", +class ComponentResourceResolutionNodeTemplateBuilder(id: String, description: String) : + AbstractNodeTemplateOperationImplBuilder<PropertiesAssignmentBuilder, + ComponentResourceResolutionNodeTemplateBuilder.InputsBuilder, + ComponentResourceResolutionNodeTemplateBuilder.OutputsBuilder>(id, "component-script-executor", "ComponentResourceResolution", - description) + description) { -class ComponentResourceResolutionInputAssignmentBuilder : PropertiesAssignmentBuilder() { + class InputsBuilder : PropertiesAssignmentBuilder() { - fun requestId(requestId: String) = requestId(requestId.asJsonPrimitive()) + fun requestId(requestId: String) = requestId(requestId.asJsonPrimitive()) - fun requestId(requestId: JsonNode) { - property(ResourceResolutionComponent.INPUT_REQUEST_ID, requestId) - } + fun requestId(requestId: JsonNode) { + property(ResourceResolutionComponent.INPUT_REQUEST_ID, requestId) + } - fun resourceId(resourceId: String) = resourceId(resourceId.asJsonPrimitive()) + fun resourceId(resourceId: String) = resourceId(resourceId.asJsonPrimitive()) - fun resourceId(resourceId: JsonNode) { - property(ResourceResolutionComponent.INPUT_RESOURCE_ID, resourceId) - } + fun resourceId(resourceId: JsonNode) { + property(ResourceResolutionComponent.INPUT_RESOURCE_ID, resourceId) + } - fun actionName(actionName: String) = actionName(actionName.asJsonPrimitive()) + fun actionName(actionName: String) = actionName(actionName.asJsonPrimitive()) - fun actionName(actionName: JsonNode) { - property(ResourceResolutionComponent.INPUT_ACTION_NAME, actionName) - } + fun actionName(actionName: JsonNode) { + property(ResourceResolutionComponent.INPUT_ACTION_NAME, actionName) + } - fun resolutionKey(resolutionKey: String) = resolutionKey(resolutionKey.asJsonPrimitive()) + fun resolutionKey(resolutionKey: String) = resolutionKey(resolutionKey.asJsonPrimitive()) - fun resolutionKey(resolutionKey: JsonNode) { - property(ResourceResolutionComponent.INPUT_RESOLUTION_KEY, resolutionKey) - } + fun resolutionKey(resolutionKey: JsonNode) { + property(ResourceResolutionComponent.INPUT_RESOLUTION_KEY, resolutionKey) + } - fun dynamicProperties(dynamicProperties: String) = dynamicProperties(dynamicProperties.asJsonType()) + fun dynamicProperties(dynamicProperties: String) = dynamicProperties(dynamicProperties.asJsonType()) - fun dynamicProperties(dynamicProperties: JsonNode) { - property(ResourceResolutionComponent.INPUT_DYNAMIC_PROPERTIES, dynamicProperties) - } + fun dynamicProperties(dynamicProperties: JsonNode) { + property(ResourceResolutionComponent.INPUT_DYNAMIC_PROPERTIES, dynamicProperties) + } - fun occurrence(occurrence: Int) = occurrence(occurrence.asJsonPrimitive()) + fun occurrence(occurrence: Int) = occurrence(occurrence.asJsonPrimitive()) - fun occurrence(resolutionKey: JsonNode) { - property(ResourceResolutionComponent.INPUT_OCCURRENCE, resolutionKey) - } + fun occurrence(resolutionKey: JsonNode) { + property(ResourceResolutionComponent.INPUT_OCCURRENCE, resolutionKey) + } - fun storeResult(storeResult: Boolean) = storeResult(storeResult.asJsonPrimitive()) + fun storeResult(storeResult: Boolean) = storeResult(storeResult.asJsonPrimitive()) - fun storeResult(storeResult: JsonNode) { - property(ResourceResolutionComponent.INPUT_STORE_RESULT, storeResult) - } + fun storeResult(storeResult: JsonNode) { + property(ResourceResolutionComponent.INPUT_STORE_RESULT, storeResult) + } - fun resourceType(resourceType: String) = resourceType(resourceType.asJsonPrimitive()) + fun resourceType(resourceType: String) = resourceType(resourceType.asJsonPrimitive()) - fun resourceType(resourceType: JsonNode) { - property(ResourceResolutionComponent.INPUT_RESOURCE_TYPE, resourceType) - } + fun resourceType(resourceType: JsonNode) { + property(ResourceResolutionComponent.INPUT_RESOURCE_TYPE, resourceType) + } - fun artifactPrefixNames(artifactPrefixNames: String) = artifactPrefixNames(artifactPrefixNames.jsonAsJsonType()) + fun artifactPrefixNames(artifactPrefixNames: String) = artifactPrefixNames(artifactPrefixNames.jsonAsJsonType()) - fun artifactPrefixNames(artifactPrefixNameList: List<String>) { - artifactPrefixNames(artifactPrefixNameList.asJsonString()) - } + fun artifactPrefixNames(artifactPrefixNameList: List<String>) = + artifactPrefixNames(artifactPrefixNameList.asJsonString()) - fun artifactPrefixNames(artifactPrefixNames: JsonNode) { - property(ResourceResolutionComponent.INPUT_ARTIFACT_PREFIX_NAMES, artifactPrefixNames) + fun artifactPrefixNames(artifactPrefixNames: JsonNode) { + property(ResourceResolutionComponent.INPUT_ARTIFACT_PREFIX_NAMES, artifactPrefixNames) + } } -} -class ComponentResourceResolutionOutputAssignmentBuilder : PropertiesAssignmentBuilder() { + class OutputsBuilder : PropertiesAssignmentBuilder() { - fun status(status: String) = status(status.asJsonPrimitive()) + fun status(status: String) = status(status.asJsonPrimitive()) - fun status(status: JsonNode) { - property(ResourceResolutionComponent.OUTPUT_STATUS, status) - } + fun status(status: JsonNode) { + property(ResourceResolutionComponent.OUTPUT_STATUS, status) + } - fun resourceAssignmentParams(resourceAssignmentParams: String) = resourceAssignmentParams(resourceAssignmentParams.asJsonType()) + fun resourceAssignmentParams(resourceAssignmentParams: String) = + resourceAssignmentParams(resourceAssignmentParams.asJsonType()) - fun resourceAssignmentParams(resourceAssignmentParams: JsonNode) { - property(ResourceResolutionComponent.OUTPUT_RESOURCE_ASSIGNMENT_PARAMS, resourceAssignmentParams) + fun resourceAssignmentParams(resourceAssignmentParams: JsonNode) { + property(ResourceResolutionComponent.OUTPUT_RESOURCE_ASSIGNMENT_PARAMS, resourceAssignmentParams) + } } }
\ 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/ResourceResolutionService.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt index 6514df249..641175ca2 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, String> { val resourceAssignmentRuntimeService = - ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString()) + ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, artifactNames.toString()) val resolvedParams: MutableMap<String, String> = hashMapOf() artifactNames.forEach { artifactName -> val resolvedContent = resolveResources(resourceAssignmentRuntimeService, nodeTemplateName, - artifactName, properties) + artifactName, properties) resolvedParams[artifactName] = resolvedContent } @@ -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/ResourceSourceDSL.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceSourceDSL.kt new file mode 100644 index 000000000..182f3a178 --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceSourceDSL.kt @@ -0,0 +1,333 @@ +/* + * 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 + +import com.fasterxml.jackson.databind.JsonNode +import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentScriptExecutor +import org.onap.ccsdk.cds.controllerblueprints.core.* +import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate +import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType +import org.onap.ccsdk.cds.controllerblueprints.core.dsl.AbstractNodeTemplatePropertyImplBuilder +import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder +import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType +import kotlin.reflect.KClass + +fun BluePrintTypes.nodeTypeSourceInput(): NodeType { + return nodeType(id = "source-input", version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE, + description = "This is Input Resource Source Node Type") {} +} + +fun BluePrintTypes.nodeTypeSourceDefault(): NodeType { + return nodeType(id = "source-default", version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE, + description = "This is Default Resource Source Node Type") {} +} + +fun BluePrintTypes.nodeTypeSourceDb(): NodeType { + return nodeType(id = "source-db", version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE, + description = "This is Database Resource Source Node Type") { + property("type", BluePrintConstants.DATA_TYPE_STRING, + true, "") { + defaultValue("SQL".asJsonPrimitive()) + constrain { + validValues(arrayListOf("SQL".asJsonPrimitive(), "PLSQL".asJsonPrimitive())) + } + } + property("endpoint-selector", BluePrintConstants.DATA_TYPE_STRING, + false, "") + property("query", BluePrintConstants.DATA_TYPE_STRING, + true, "") + property("input-key-mapping", BluePrintConstants.DATA_TYPE_MAP, + true, "") { + entrySchema(BluePrintConstants.DATA_TYPE_STRING) + } + property("output-key-mapping", BluePrintConstants.DATA_TYPE_MAP, + false, "") { + entrySchema(BluePrintConstants.DATA_TYPE_STRING) + } + property("key-dependencies", BluePrintConstants.DATA_TYPE_LIST, + true, "Resource Resolution dependency dictionary names.") { + entrySchema(BluePrintConstants.DATA_TYPE_STRING) + } + } +} + +fun BluePrintTypes.nodeTypeSourceRest(): NodeType { + return nodeType(id = "source-rest", version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE, + description = "This is Rest Resource Source Node Type") { + property("type", BluePrintConstants.DATA_TYPE_STRING, + true, "") { + defaultValue("JSON".asJsonPrimitive()) + constrain { + validValues(arrayListOf("JSON".asJsonPrimitive(), "XML".asJsonPrimitive())) + } + } + property("verb", BluePrintConstants.DATA_TYPE_STRING, + true, "") { + defaultValue("GET".asJsonPrimitive()) + constrain { + validValues(arrayListOf("GET".asJsonPrimitive(), "POST".asJsonPrimitive(), + "DELETE".asJsonPrimitive(), "PUT".asJsonPrimitive())) + } + } + property("payload", BluePrintConstants.DATA_TYPE_STRING, + false, "") { + defaultValue("".asJsonPrimitive()) + } + property("endpoint-selector", BluePrintConstants.DATA_TYPE_STRING, + false, "") + property("url-path", BluePrintConstants.DATA_TYPE_STRING, + true, "") + property("path", BluePrintConstants.DATA_TYPE_STRING, + true, "") + property("expression-type", BluePrintConstants.DATA_TYPE_STRING, + false, "") { + defaultValue("JSON_PATH".asJsonPrimitive()) + constrain { + validValues(arrayListOf("JSON_PATH".asJsonPrimitive(), "JSON_POINTER".asJsonPrimitive())) + } + } + property("input-key-mapping", BluePrintConstants.DATA_TYPE_MAP, + true, "") { + entrySchema(BluePrintConstants.DATA_TYPE_STRING) + } + property("output-key-mapping", BluePrintConstants.DATA_TYPE_MAP, + false, "") { + entrySchema(BluePrintConstants.DATA_TYPE_STRING) + } + property("key-dependencies", BluePrintConstants.DATA_TYPE_LIST, + true, "Resource Resolution dependency dictionary names.") { + entrySchema(BluePrintConstants.DATA_TYPE_STRING) + } + } +} + +fun BluePrintTypes.nodeTypeSourceCapability(): NodeType { + return nodeType(id = "source-capability", version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE, + description = "This is Component Resource Source Node Type") { + property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, BluePrintConstants.DATA_TYPE_STRING, + true, "Request Id, Unique Id for the request.") { + defaultValue(BluePrintConstants.SCRIPT_KOTLIN) + constrain { + validValues(arrayListOf(BluePrintConstants.SCRIPT_KOTLIN.asJsonPrimitive(), + BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive(), + BluePrintConstants.SCRIPT_JYTHON.asJsonPrimitive())) + } + } + property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, BluePrintConstants.DATA_TYPE_STRING, + true, "Kotlin Script class name or jython script name.") + property("key-dependencies", BluePrintConstants.DATA_TYPE_LIST, + true, "Resource Resolution dependency dictionary names.") { + entrySchema(BluePrintConstants.DATA_TYPE_STRING) + } + } +} + +/** Node Template Source Input **/ +fun BluePrintTypes.nodeTemplateSourceInput(id: String, description: String, + block: SourceInputNodeTemplateBuilder.() -> Unit): NodeTemplate { + return SourceInputNodeTemplateBuilder(id, description).apply(block).build() +} + +class SourceInputNodeTemplateBuilder(id: String, description: String) : + AbstractNodeTemplatePropertyImplBuilder<PropertiesAssignmentBuilder>(id, + "source-input", description) + +/** Node Template Source Default **/ +fun BluePrintTypes.nodeTemplateSourceDefault(id: String, description: String, + block: SourceDefaultNodeTemplateBuilder.() -> Unit): NodeTemplate { + return SourceDefaultNodeTemplateBuilder(id, description).apply(block).build() +} + +class SourceDefaultNodeTemplateBuilder(id: String, description: String) : + AbstractNodeTemplatePropertyImplBuilder<PropertiesAssignmentBuilder>(id, + "source-default", description) + +/** Node Template Source DB **/ +fun BluePrintTypes.nodeTemplateSourceDb(id: String, description: String, + block: SourceDbNodeTemplateBuilder.() -> Unit): NodeTemplate { + return SourceDbNodeTemplateBuilder(id, description).apply(block).build() +} + +class SourceDbNodeTemplateBuilder(id: String, description: String) : + AbstractNodeTemplatePropertyImplBuilder<SourceDbNodeTemplateBuilder.PropertiesBuilder>(id, + "source-db", description) { + + class PropertiesBuilder : PropertiesAssignmentBuilder() { + fun type(type: String) = type(type.asJsonPrimitive()) + + fun type(type: JsonNode) { + property("type", type) + } + + fun endpointSelector(endpointSelector: String) = endpointSelector(endpointSelector.asJsonPrimitive()) + + fun endpointSelector(endpointSelector: JsonNode) { + property("endpoint-selector", endpointSelector) + } + + fun query(query: String) = query(query.asJsonPrimitive()) + + fun query(query: JsonNode) { + property("query", query) + } + + fun inputKeyMapping(block: KeyMappingBuilder.() -> Unit) { + val map = KeyMappingBuilder().apply(block).build() + property("input-key-mapping", map.asJsonType()) + } + + fun outputKeyMapping(block: KeyMappingBuilder.() -> Unit) { + val map = KeyMappingBuilder().apply(block).build() + property("output-key-mapping", map.asJsonType()) + } + + fun keyDependencies(keyDependencies: String) = keyDependencies(keyDependencies.asJsonType()) + + fun keyDependencies(keyDependencies: List<String>) = keyDependencies(keyDependencies.asJsonString()) + + fun keyDependencies(keyDependencies: JsonNode) { + property("key-dependencies", keyDependencies) + } + } +} + +class KeyMappingBuilder() { + val map: MutableMap<String, String> = hashMapOf() + fun map(key: String, value: String) { + map[key] = value + } + + fun build(): MutableMap<String, String> { + return map + } +} + + +/** Node Template Source Rest **/ +fun BluePrintTypes.nodeTemplateSourceRest(id: String, description: String, + block: SourceRestNodeTemplateBuilder.() -> Unit): NodeTemplate { + return SourceRestNodeTemplateBuilder(id, description).apply(block).build() +} + +class SourceRestNodeTemplateBuilder(id: String, description: String) : + AbstractNodeTemplatePropertyImplBuilder<SourceRestNodeTemplateBuilder.PropertiesBuilder>(id, + "source-rest", description) { + + class PropertiesBuilder : PropertiesAssignmentBuilder() { + fun type(type: String) = type(type.asJsonPrimitive()) + + fun type(type: JsonNode) { + property("type", type) + } + + fun endpointSelector(endpointSelector: String) = endpointSelector(endpointSelector.asJsonPrimitive()) + + fun endpointSelector(endpointSelector: JsonNode) { + property("endpoint-selector", endpointSelector) + } + + fun verb(verb: String) = verb(verb.asJsonPrimitive()) + + fun verb(verb: JsonNode) { + property("verb", verb) + } + + fun payload(payload: String) = payload(payload.asJsonPrimitive()) + + fun payload(payload: JsonNode) { + property("payload", payload) + } + + fun urlPath(urlPath: String) = urlPath(urlPath.asJsonPrimitive()) + + fun urlPath(urlPath: JsonNode) { + property("url-path", urlPath) + } + + fun path(path: String) = path(path.asJsonPrimitive()) + + fun path(path: JsonNode) { + property("path", path) + } + + fun expressionType(expressionType: String) = expressionType(expressionType.asJsonPrimitive()) + + fun expressionType(expressionType: JsonNode) { + property("expression-type", expressionType) + } + + fun inputKeyMapping(block: KeyMappingBuilder.() -> Unit) { + val map = KeyMappingBuilder().apply(block).build() + property("input-key-mapping", map.asJsonType()) + } + + fun outputKeyMapping(block: KeyMappingBuilder.() -> Unit) { + val map = KeyMappingBuilder().apply(block).build() + property("output-key-mapping", map.asJsonType()) + } + + fun keyDependencies(keyDependencies: String) = keyDependencies(keyDependencies.asJsonType()) + + fun keyDependencies(keyDependencies: List<String>) = keyDependencies(keyDependencies.asJsonString()) + + fun keyDependencies(keyDependencies: JsonNode) { + property("key-dependencies", keyDependencies) + } + } +} + +/** Node Template Source Rest **/ +fun BluePrintTypes.nodeTemplateSourceCapability(id: String, description: String, + block: SourceCapabilityNodeTemplateBuilder.() -> Unit): NodeTemplate { + return SourceCapabilityNodeTemplateBuilder(id, description).apply(block).build() +} + +class SourceCapabilityNodeTemplateBuilder(id: String, description: String) : + AbstractNodeTemplatePropertyImplBuilder<SourceCapabilityNodeTemplateBuilder.PropertiesBuilder>(id, + "source-capability", description) { + + class PropertiesBuilder : PropertiesAssignmentBuilder() { + fun type(type: String) = type(type.asJsonPrimitive()) + + fun type(type: JsonNode) { + property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, type) + } + + fun scriptClassReference(scriptClassReference: KClass<*>) { + scriptClassReference(scriptClassReference.qualifiedName!!) + } + + fun scriptClassReference(scriptClassReference: String) = scriptClassReference(scriptClassReference.asJsonPrimitive()) + + fun scriptClassReference(scriptClassReference: JsonNode) { + property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, scriptClassReference) + } + + fun keyDependencies(keyDependencies: String) = keyDependencies(keyDependencies.asJsonType()) + + fun keyDependencies(keyDependencies: List<String>) = keyDependencies(keyDependencies.asJsonString()) + + fun keyDependencies(keyDependencies: JsonNode) { + property("key-dependencies", keyDependencies) + } + } +}
\ 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/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 diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceDefinitionDSLTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceDefinitionDSLTest.kt new file mode 100644 index 000000000..f8f0e991e --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceDefinitionDSLTest.kt @@ -0,0 +1,111 @@ +/* + * 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 + +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull + +class ResourceDefinitionDSLTest { + + @Test + fun testResourceDefinitionDSL() { + val testResourceDefinition = BluePrintTypes.resourceDefinition("service-instance-id", + "VFW Service Instance Name") { + tags("service-instance-name, vfw, resources") + updatedBy("brindasanth@onap.com") + property("string", true) + sources { + sourceInput("input", "") {} + sourceDefault("default", "") {} + sourceDb("sdnctl", "") { + definedProperties { + type("SQL") + query("SELECT name FROM SERVICE_INSTANCE WHERE id = \$id") + endpointSelector("db-source-endpoint") + inputKeyMapping { + map("id", "\$service-instance-id") + } + outputKeyMapping { + map("service-instance-name", "\$name") + } + keyDependencies(arrayListOf("service-instance-id")) + } + } + sourceRest("odl-mdsal", "") { + definedProperties { + type("JSON") + endpointSelector("rest-source-endpoint") + expressionType("JSON_PATH") + urlPath("/service-instance/\$id") + path(".\$name") + verb("GET") + payload("sample payload") + inputKeyMapping { + map("id", "\$service-instance-id") + } + outputKeyMapping { + map("service-instance-name", "\$name") + } + keyDependencies(arrayListOf("service-instance-id")) + } + } + sourceCapability("custom-component", "") { + definedProperties { + type("kotlin") + scriptClassReference("Scripts/ServiceInstance.kt") + keyDependencies(arrayListOf("service-instance-id")) + } + } + } + } + //println(resourceDefinition.asJsonString(true)) + assertNotNull(testResourceDefinition, "failed to generate testResourceDefinition") + + val testResourceDefinitions = BluePrintTypes.resourceDefinitions { + resourceDefinition(testResourceDefinition) + } + assertNotNull(testResourceDefinitions, "failed to generate testResourceDefinitions") + assertEquals(1, testResourceDefinitions.size, "testResourceDefinitions size doesn't match") + } + + @Test + fun testResourceAssignment() { + val testResourceAssignment = BluePrintTypes.resourceAssignment("instance-name", + "service-instance-name", "odl-mdsal") { + inputParameter(true) + property("string", true) + dependencies(arrayListOf("service-instance-id")) + } + //println(resourceAssignment.asJsonString(true)) + assertNotNull(testResourceAssignment, "failed to generate resourceAssignment") + + val testResourceAssignments = BluePrintTypes.resourceAssignments { + resourceAssignment(testResourceAssignment) + resourceAssignment("instance-name1", + "service-instance-name", "odl-mdsal") { + inputParameter(true) + property("string", true) + dependencies(arrayListOf("service-instance-id")) + } + } + //println(testResourceAssignments.asJsonString(true)) + assertNotNull(testResourceAssignments, "failed to generate testResourceAssignments") + assertEquals(2, testResourceAssignments.size, "testResourceAssignments size doesn't match") + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponentDSLTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponentDSLTest.kt index d0566785e..671acff95 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponentDSLTest.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionComponentDSLTest.kt @@ -33,7 +33,7 @@ class ResourceResolutionComponentDSLTest { @Test fun testNodeTemplateComponentResourceResolution() { val nodeTemplate = BluePrintTypes.nodeTemplateComponentResourceResolution("resource-resolve", "") { - operation("Resolve resources") { + definedOperation("Resolve resources") { inputs { actionName("resolve") requestId("1234") diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt index f1ad03054..775c501c2 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionServiceTest.kt @@ -18,6 +18,8 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution +import io.mockk.every +import io.mockk.mockk import kotlinx.coroutines.runBlocking import org.junit.Assert import org.junit.Before @@ -30,12 +32,18 @@ import org.onap.ccsdk.cds.blueprintsprocessor.core.utils.PayloadUtils import org.onap.ccsdk.cds.blueprintsprocessor.db.BluePrintDBLibConfiguration import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor.* import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils.ResourceAssignmentUtils +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintError +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.autoconfigure.EnableAutoConfiguration +import org.springframework.context.ApplicationContext import org.springframework.context.annotation.ComponentScan import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.TestPropertySource @@ -84,8 +92,8 @@ class ResourceResolutionServiceTest { fun testRegisteredSource() { val sources = resourceResolutionService.registeredResourceSources() assertNotNull(sources, "failed to get registered sources") - assertTrue(sources.containsAll(arrayListOf("source-input", "source-default", "source-processor-db", - "source-rest")), "failed to get registered sources : $sources") + assertTrue(sources.containsAll(arrayListOf("source-input", "source-default", "source-db", + "source-rest", "source-capability")), "failed to get registered sources : $sources") } @Test @@ -96,27 +104,27 @@ class ResourceResolutionServiceTest { Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService) val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234", - "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") + "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") val executionServiceInput = - JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json", - ExecutionServiceInput::class.java)!! + JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json", + ExecutionServiceInput::class.java)!! val resourceAssignmentRuntimeService = - ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, - "testResolveResource") + ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, + "testResolveResource") // Prepare Inputs PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService, - executionServiceInput.payload, - "resource-assignment") + executionServiceInput.payload, + "resource-assignment") resourceResolutionService.resolveResources(resourceAssignmentRuntimeService, - "resource-assignment", - "baseconfig", - props) + "resource-assignment", + "baseconfig", + props) } } @@ -128,23 +136,23 @@ class ResourceResolutionServiceTest { Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService) val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234", - "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") + "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") val executionServiceInput = - JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json", - ExecutionServiceInput::class.java)!! + JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json", + ExecutionServiceInput::class.java)!! val artefactNames = listOf("baseconfig", "another") // Prepare Inputs PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService, - executionServiceInput.payload, - "resource-assignment") + executionServiceInput.payload, + "resource-assignment") resourceResolutionService.resolveResources(bluePrintRuntimeService, - "resource-assignment", - artefactNames, - props) + "resource-assignment", + artefactNames, + props) } } @@ -156,27 +164,27 @@ class ResourceResolutionServiceTest { Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService) val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234", - "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") + "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") val executionServiceInput = - JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json", - ExecutionServiceInput::class.java)!! + JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json", + ExecutionServiceInput::class.java)!! val resourceAssignmentRuntimeService = - ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, - "testResolveResourcesWithMappingAndTemplate") + ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, + "testResolveResourcesWithMappingAndTemplate") val artifactPrefix = "another" // Prepare Inputs PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService, - executionServiceInput.payload, - "resource-assignment") + executionServiceInput.payload, + "resource-assignment") resourceResolutionService.resolveResources(resourceAssignmentRuntimeService, - "resource-assignment", - artifactPrefix, - props) + "resource-assignment", + artifactPrefix, + props) } } @@ -190,27 +198,81 @@ class ResourceResolutionServiceTest { Assert.assertNotNull("failed to create ResourceResolutionService", resourceResolutionService) val bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime("1234", - "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") + "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") val executionServiceInput = - JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json", - ExecutionServiceInput::class.java)!! + JacksonUtils.readValueFromClassPathFile("payload/requests/sample-resourceresolution-request.json", + ExecutionServiceInput::class.java)!! val resourceAssignmentRuntimeService = - ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, - "testResolveResourcesWithMappingAndTemplate") + ResourceAssignmentUtils.transformToRARuntimeService(bluePrintRuntimeService, + "testResolveResourcesWithMappingAndTemplate") val artifactPrefix = "another" // Prepare Inputs PayloadUtils.prepareInputsFromWorkflowPayload(bluePrintRuntimeService, - executionServiceInput.payload, - "resource-assignment") + executionServiceInput.payload, + "resource-assignment") resourceResolutionService.resolveResources(resourceAssignmentRuntimeService, - "resource-assignment", - artifactPrefix, - props) + "resource-assignment", + artifactPrefix, + props) + } + } + + @Test + fun testResourceResolutionForDefinition() { + val resourceDefinitions = BluePrintTypes.resourceDefinitions { + resourceDefinition(name = "port-speed", description = "Port Speed") { + property(type = "string", required = true) + sources { + sourceCapability(id = "sdno", description = "SDNO Source") { + definedProperties { + type(BluePrintConstants.SCRIPT_KOTLIN) + scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!) + keyDependencies(arrayListOf("device-id")) + } + } + sourceDb(id = "sdnc", description = "SDNC Controller") { + definedProperties { + endpointSelector("processor-db") + query("SELECT PORT_SPEED FROM XXXX WHERE DEVICE_ID = :device_id") + inputKeyMapping { + map("device_id", "\$device-id") + } + keyDependencies(arrayListOf("device-id")) + } + } + } + } + resourceDefinition(name = "device-id", description = "Device Id") { + property(type = "string", required = true) { + sources { + sourceInput(id = "input", description = "Dependency Source") {} + } + } + } + } + runBlocking { + val raRuntimeService = mockk<ResourceAssignmentRuntimeService>() + every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>() + every { raRuntimeService.getBluePrintError() } returns BluePrintError() + every { raRuntimeService.setBluePrintError(any())} returns Unit + every { raRuntimeService.getInputValue("device-id") } returns "123456".asJsonPrimitive() + + val applicationContext = mockk<ApplicationContext>() + every { applicationContext.getBean("rr-processor-source-capability") } returns MockCapabilityScriptRA() + every { applicationContext.getBean("rr-processor-source-db") } returns MockCapabilityScriptRA() + every { applicationContext.getBean("rr-processor-source-input") } returns MockCapabilityScriptRA() + + val sources = arrayListOf<String>("sdno", "sdnc") + + val resourceResolutionService = ResourceResolutionServiceImpl(applicationContext, mockk(), mockk(), mockk()) + val resolvedResources = resourceResolutionService.resolveResourceDefinition(raRuntimeService, + resourceDefinitions, "port-speed", sources) + assertNotNull(resolvedResources, "failed to resolve the resources") } } } diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceSourceDSLTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceSourceDSLTest.kt new file mode 100644 index 000000000..2eb208566 --- /dev/null +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceSourceDSLTest.kt @@ -0,0 +1,134 @@ +/* + * 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 + +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes +import kotlin.test.Test +import kotlin.test.assertNotNull + +class ResourceSourceDSLTest { + + @Test + fun testNodeTypeSourceInput() { + val nodeType = BluePrintTypes.nodeTypeSourceInput() + //println(nodeType.asJsonString(true)) + assertNotNull(nodeType, "failed to generate nodeTypeSourceInput") + } + + @Test + fun testNodeTypeSourceDefault() { + val nodeType = BluePrintTypes.nodeTypeSourceDefault() + //println(nodeType.asJsonString(true)) + assertNotNull(nodeType, "failed to generate nodeTypeSourceDefault") + } + + @Test + fun testNodeTypeSourceDb() { + val nodeType = BluePrintTypes.nodeTypeSourceDb() + //println(nodeType.asJsonString(true)) + assertNotNull(nodeType, "failed to generate nodeTypeSourceDb") + } + + @Test + fun testNodeTypeSourceRest() { + val nodeType = BluePrintTypes.nodeTypeSourceRest() + //println(nodeType.asJsonString(true)) + assertNotNull(nodeType, "failed to generate nodeTypeSourceRest") + } + + @Test + fun testNodeTypeSourceCapability() { + val nodeType = BluePrintTypes.nodeTypeSourceCapability() + //println(nodeType.asJsonString(true)) + assertNotNull(nodeType, "failed to generate nodeTypeSourceCapability") + } + + @Test + fun testNodeTemplateSourceInput() { + val nodeTemplate = BluePrintTypes.nodeTemplateSourceInput("InputSystem", "") { + + } + //println(nodeTemplate.asJsonString(true)) + assertNotNull(nodeTemplate, "failed to generate nodeTemplateSourceInput") + } + + @Test + fun testNodeTemplateSourceDefault() { + val nodeTemplate = BluePrintTypes.nodeTemplateSourceDefault("DefaultSystem", "") { + + } + //println(nodeTemplate.asJsonString(true)) + assertNotNull(nodeTemplate, "failed to generate nodeTemplateSourceDefault") + } + + @Test + fun testNodeTemplateSourceDb() { + val nodeTemplate = BluePrintTypes.nodeTemplateSourceDb("DbSystem", "") { + definedProperties { + type("SQL") + query("SELECT * FROM DB WHERE name = \$name") + endpointSelector("db-source-endpoint") + inputKeyMapping { + map("name", "\$name") + } + outputKeyMapping { + map("field_name", "\$fieldValue") + } + keyDependencies(arrayListOf("name")) + } + } + //println(nodeTemplate.asJsonString(true)) + assertNotNull(nodeTemplate, "failed to generate nodeTemplateSourceDb") + } + + @Test + fun testNodeTemplateSourceRest() { + val nodeTemplate = BluePrintTypes.nodeTemplateSourceRest("restSystem", "") { + definedProperties { + type("JSON") + endpointSelector("rest-source-endpoint") + expressionType("JSON_PATH") + urlPath("/location") + path(".\$name") + verb("GET") + payload("sample payload") + inputKeyMapping { + map("name", "\$name") + } + outputKeyMapping { + map("field_name", "\$fieldValue") + } + keyDependencies(arrayListOf("name")) + } + } + //println(nodeTemplate.asJsonString(true)) + assertNotNull(nodeTemplate, "failed to generate nodeTemplateSourceRest") + } + + @Test + fun testNodeTemplateSourceCapability() { + val nodeTemplate = BluePrintTypes.nodeTemplateSourceCapability("capabiltySystem", "") { + definedProperties { + type("kotlin") + scriptClassReference("Scripts/Sample.kt") + keyDependencies(arrayListOf("name")) + } + } + //println(nodeTemplate.asJsonString(true)) + assertNotNull(nodeTemplate, "failed to generate nodeTemplateSourceCapability") + } +}
\ No newline at end of file diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessorTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessorTest.kt index 4a4bcc026..f020f2952 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessorTest.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/CapabilityResourceResolutionProcessorTest.kt @@ -18,25 +18,34 @@ package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.processor +import io.mockk.coEvery +import io.mockk.every +import io.mockk.mockk import kotlinx.coroutines.runBlocking -import org.junit.Ignore import org.junit.Test import org.junit.runner.RunWith import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService +import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.resourceAssignment import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts.BlueprintJythonService import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts.PythonExecutorProperty +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition +import org.onap.ccsdk.cds.controllerblueprints.core.logger +import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl +import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils 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 -import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl import org.springframework.beans.factory.annotation.Autowired import org.springframework.test.context.ContextConfiguration import org.springframework.test.context.TestPropertySource import org.springframework.test.context.junit4.SpringRunner +import kotlin.test.assertEquals import kotlin.test.assertNotNull +import kotlin.test.assertTrue @RunWith(SpringRunner::class) @ContextConfiguration(classes = [CapabilityResourceResolutionProcessor::class, ComponentFunctionScriptingService::class, @@ -50,44 +59,36 @@ class CapabilityResourceResolutionProcessorTest { @Autowired lateinit var capabilityResourceResolutionProcessor: CapabilityResourceResolutionProcessor - @Ignore @Test fun `test kotlin capability`() { runBlocking { - - val bluePrintContext = BluePrintMetadataUtils.getBluePrintContext( - "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration") - - val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService("1234", bluePrintContext) - - capabilityResourceResolutionProcessor.raRuntimeService = resourceAssignmentRuntimeService - capabilityResourceResolutionProcessor.resourceDictionaries = hashMapOf() - - - val scriptPropertyInstances: MutableMap<String, Any> = mutableMapOf() - scriptPropertyInstances["mock-service1"] = MockCapabilityService() - scriptPropertyInstances["mock-service2"] = MockCapabilityService() - - val instanceDependencies: List<String> = listOf() - - val resourceAssignmentProcessor = capabilityResourceResolutionProcessor - .scriptInstance("kotlin", - "ResourceAssignmentProcessor_cba\$ScriptResourceAssignmentProcessor", instanceDependencies) - - assertNotNull(resourceAssignmentProcessor, "couldn't get kotlin script resource assignment processor") - - val resourceAssignment = ResourceAssignment().apply { - name = "ra-name" - dictionaryName = "ra-dict-name" - dictionarySource = "capability" - property = PropertyDefinition().apply { - type = "string" + val componentFunctionScriptingService = mockk<ComponentFunctionScriptingService>() + coEvery { + componentFunctionScriptingService + .scriptInstance<ResourceAssignmentProcessor>(any(), any(), any()) + } returns MockCapabilityScriptRA() + + val raRuntimeService = mockk<ResourceAssignmentRuntimeService>() + every { raRuntimeService.bluePrintContext() } returns mockk<BluePrintContext>() + + val capabilityResourceResolutionProcessor = CapabilityResourceResolutionProcessor(componentFunctionScriptingService) + capabilityResourceResolutionProcessor.raRuntimeService = raRuntimeService + + val resourceAssignment = BluePrintTypes.resourceAssignment(name = "test-property", dictionaryName = "ra-dict-name", + dictionarySource = "capability") { + property("string", true, "") + sourceCapability { + definedProperties { + type("internal") + scriptClassReference(MockCapabilityScriptRA::class.qualifiedName!!) + keyDependencies(arrayListOf("dep-property")) + } } } - - val processorName = resourceAssignmentProcessor.applyNB(resourceAssignment) - assertNotNull(processorName, "couldn't get kotlin script resource assignment processor name") - println(processorName) + val status = capabilityResourceResolutionProcessor.applyNB(resourceAssignment) + assertTrue(status, "failed to execute capability source") + assertEquals("assigned-data".asJsonPrimitive(), resourceAssignment.property!!.value, + "assigned value miss match") } } @@ -127,4 +128,21 @@ class CapabilityResourceResolutionProcessorTest { open class MockCapabilityService { +} + +open class MockCapabilityScriptRA : ResourceAssignmentProcessor() { + val log = logger(MockCapabilityScriptRA::class) + + override fun getName(): String { + return "MockCapabilityScriptRA" + } + + override suspend fun processNB(executionRequest: ResourceAssignment) { + log.info("executing RA mock capability : ${executionRequest.name}") + executionRequest.property!!.value = "assigned-data".asJsonPrimitive() + } + + override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ResourceAssignment) { + TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + } }
\ No newline at end of file |