From 7c866aaefe19df34cb0c38e1f989179cf21dc497 Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Mon, 27 Aug 2018 23:16:39 -0400 Subject: Controller Blueprints Microservice Add resource dictionary node template property assignments validation for primitive and complex types. Change-Id: Ic6f3a521310c8e15ebb5b5b5d6ad3edb0ede9ecb Issue-ID: CCSDK-488 Signed-off-by: Brinda Santh --- .../service/ResourceDictionaryValidationService.kt | 35 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) (limited to 'ms/controllerblueprints/modules/resource-dict') diff --git a/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/service/ResourceDictionaryValidationService.kt b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/service/ResourceDictionaryValidationService.kt index 5a1e38126..81bb37d02 100644 --- a/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/service/ResourceDictionaryValidationService.kt +++ b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/resource/dict/service/ResourceDictionaryValidationService.kt @@ -20,12 +20,14 @@ package org.onap.ccsdk.apps.controllerblueprints.resource.dict.service import com.fasterxml.jackson.databind.JsonNode import com.google.common.base.Preconditions import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException +import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeTemplate import org.onap.ccsdk.apps.controllerblueprints.core.data.NodeType import org.onap.ccsdk.apps.controllerblueprints.core.data.PropertyDefinition import org.onap.ccsdk.apps.controllerblueprints.core.format import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintExpressionService import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRepoService +import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceDefinition import org.slf4j.LoggerFactory import java.io.Serializable @@ -43,12 +45,13 @@ open class ResourceDictionaryDefaultValidationService(val bluePrintRepoService: override fun validate(resourceDefinition: ResourceDefinition) { Preconditions.checkNotNull(resourceDefinition, "Failed to get Resource Definition") + log.trace("Validating Resource Dictionary Definition {}", resourceDefinition.name) resourceDefinition.sources.forEach { (name, nodeTemplate) -> val sourceType = nodeTemplate.type val sourceNodeType = bluePrintRepoService.getNodeType(sourceType)?.block() - ?: throw BluePrintException(format("Failed to get node type definition for source({})", sourceType)) + ?: throw BluePrintException(format("Failed to get source({}) node type definition({})", name, sourceType)) // Validate Property Name, expression, values and Data Type validateNodeTemplateProperties(nodeTemplate, sourceNodeType) @@ -62,7 +65,7 @@ open class ResourceDictionaryDefaultValidationService(val bluePrintRepoService: open fun validatePropertyAssignments(nodeTypeProperties: MutableMap, - properties: MutableMap) { + properties: MutableMap) { properties.forEach { propertyName, propertyAssignment -> val propertyDefinition: PropertyDefinition = nodeTypeProperties[propertyName] ?: throw BluePrintException(format("failed to get definition for the property ({})", propertyName)) @@ -70,12 +73,34 @@ open class ResourceDictionaryDefaultValidationService(val bluePrintRepoService: val expressionData = BluePrintExpressionService.getExpressionData(propertyAssignment) if (!expressionData.isExpression) { checkPropertyValue(propertyDefinition, propertyName, propertyAssignment) + } else { + throw BluePrintException(format("property({}) of expression ({}) is not supported", + propertyName, propertyAssignment)) } } } - open fun checkPropertyValue(propertyDefinition: PropertyDefinition, propertyName: String, jsonNode: JsonNode) { - //log.info("validating Property {}, name ({}) value ({})", propertyDefinition, propertyName, jsonNode) - //TODO + open fun checkPropertyValue(propertyDefinition: PropertyDefinition, propertyName: String, propertyAssignment: JsonNode) { + val propertyType = propertyDefinition.type + var isValid = false + + if (BluePrintTypes.validPrimitiveTypes().contains(propertyType)) { + isValid = JacksonUtils.checkJsonNodeValueOfPrimitiveType(propertyType, propertyAssignment) + + } else if (BluePrintTypes.validCollectionTypes().contains(propertyType)) { + + isValid = JacksonUtils.checkJsonNodeValueOfCollectionType(propertyType, propertyAssignment) + } else { + bluePrintRepoService.getDataType(propertyType) + ?: throw BluePrintException(format("property({}) defined of data type({}) is not in repository", + propertyName, propertyType)) + + isValid = true; + } + + check(isValid) { + throw BluePrintException(format("property({}) defined of type({}) is not compatable with the value ({})", + propertyName, propertyType, propertyAssignment)) + } } } \ No newline at end of file -- cgit 1.2.3-korg