From 54b50882876119f4954bcfba2a466f34b5f79512 Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Mon, 15 Jul 2019 13:08:07 -0400 Subject: Fix missing constrains definitions. Change-Id: I1d60b1a24a5876f47af3a7b9936a48187bbf2d16 Issue-ID: CCSDK-1380 Signed-off-by: Brinda Santh --- .../controllerblueprints/core/CustomFunctions.kt | 35 ++++- .../core/data/BluePrintModel.kt | 23 +-- .../core/dsl/BluePrintTemplateDSLBuilder.kt | 25 +++- .../core/dsl/BluePrintTypeDSLBuilder.kt | 161 ++++++++++++++++----- .../core/dsl/BluePrintDSLTest.kt | 36 +++++ 5 files changed, 224 insertions(+), 56 deletions(-) (limited to 'ms/controllerblueprints') diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt index 42ff8827d..efdefd040 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt @@ -64,8 +64,33 @@ fun Double.asJsonPrimitive(): DoubleNode { return DoubleNode.valueOf(this) } +/** + * Utility to convert Primitive object to Json Type Primitive. + */ +fun T.asJsonPrimitive(): JsonNode { + return if (this == null || this is MissingNode || this is NullNode) { + NullNode.instance + } else { + when (this) { + is String -> + this.asJsonPrimitive() + is Boolean -> + this.asJsonPrimitive() + is Int -> + this.asJsonPrimitive() + is Double -> + this.asJsonPrimitive() + else -> + throw BluePrintException("$this type is not supported") + } + } +} + +/** + * Utility to convert Complex or Primitive object to Json Type. + */ fun T.asJsonType(): JsonNode { - return if (this == null) { + return if (this == null || this is MissingNode || this is NullNode) { NullNode.instance } else { when (this) { @@ -131,15 +156,11 @@ fun JsonNode.returnNullIfMissing(): JsonNode? { } fun T?.isNull(): Boolean { - return if (this == null || this is NullNode || this is MissingNode) { - true - } else false + return this == null || this is NullNode || this is MissingNode } fun T?.isNotNull(): Boolean { - return if (this == null || this is NullNode || this is MissingNode) { - false - } else true + return !(this == null || this is NullNode || this is MissingNode) } /** diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/data/BluePrintModel.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/data/BluePrintModel.kt index 9e934c7c7..68e5b0aec 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/data/BluePrintModel.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/data/BluePrintModel.kt @@ -63,26 +63,24 @@ class Credential { A constraint clause defines an operation along with one or more compatible values that can be used to define a constraint on a property or parameter’s allowed values when it is defined in a TOSCA Service Template or one of its entities. */ class ConstraintClause { - @get:JsonProperty("equal") var equal: JsonNode? = null @get:JsonProperty("greater_than") - var greaterThan: Any? = null + var greaterThan: JsonNode? = null @get:JsonProperty("greater_or_equal") - var greaterOrEqual: Any? = null + var greaterOrEqual: JsonNode? = null @get:JsonProperty("less_than") - var lessThan: Any? = null + var lessThan: JsonNode? = null @get:JsonProperty("less_or_equal") - var lessOrEqual: Any? = null + var lessOrEqual: JsonNode? = null @get:JsonProperty("in_range") - var inRange: Any? = null + var inRange: MutableList? = null @get:JsonProperty("valid_values") var validValues: MutableList? = null - @get:JsonProperty("length") - var length: Any? = null + var length: JsonNode? = null @get:JsonProperty("min_length") - var minLength: Any? = null + var minLength: JsonNode? = null @get:JsonProperty("max_length") - var maxLength: Any? = null + var maxLength: JsonNode? = null var pattern: String? = null var schema: String? = null } @@ -164,6 +162,9 @@ class PropertyDefinition { var constraints: MutableList? = null @get:JsonProperty("entry_schema") var entrySchema: EntrySchema? = null + @get:JsonProperty("external-schema") + var externalSchema: String? = null + var metadata: MutableMap? = null // Mainly used in Workflow Outputs @get:ApiModelProperty(notes = "Property Value, It may be Expression or Json type values") var value: JsonNode? = null @@ -398,7 +399,7 @@ A Relationship Type is a reusable entity that defines the type of one or more re class RelationshipType : EntityType() { var interfaces: MutableMap? = null @get:JsonProperty("valid_target_types") - var validTargetTypes: ArrayList? = null + var validTargetTypes: MutableList? = null } /* diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt index f19ae8eb8..eec59d1a7 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt @@ -76,11 +76,18 @@ class NodeTemplateBuilder(private val id: String, private val type: String, private val description: String? = "") { private var nodeTemplate: NodeTemplate = NodeTemplate() + private var properties: MutableMap? = null private var interfaces: MutableMap? = null private var artifacts: MutableMap? = null private var capabilities: MutableMap? = null private var requirements: MutableMap? = null + fun properties(block: PropertiesAssignmentBuilder.() -> Unit) { + if (properties == null) + properties = hashMapOf() + properties = PropertiesAssignmentBuilder().apply(block).build() + } + fun operation(interfaceName: String, description: String? = "", block: OperationAssignmentBuilder.() -> Unit) { if (interfaces == null) @@ -122,6 +129,7 @@ class NodeTemplateBuilder(private val id: String, nodeTemplate.id = id nodeTemplate.type = type nodeTemplate.description = description + nodeTemplate.properties = properties nodeTemplate.interfaces = interfaces nodeTemplate.artifacts = artifacts nodeTemplate.capabilities = capabilities @@ -133,12 +141,27 @@ class NodeTemplateBuilder(private val id: String, class ArtifactDefinitionBuilder(private val id: String, private val type: String, private val file: String) { private var artifactDefinition: ArtifactDefinition = ArtifactDefinition() - // TODO() + private var properties: MutableMap? = null + + fun repository(repository: String) { + artifactDefinition.repository = repository + } + + fun deployPath(deployPath: String) { + artifactDefinition.deployPath = deployPath + } + + fun properties(block: PropertiesAssignmentBuilder.() -> Unit) { + if (properties == null) + properties = hashMapOf() + properties = PropertiesAssignmentBuilder().apply(block).build() + } fun build(): ArtifactDefinition { artifactDefinition.id = id artifactDefinition.type = type artifactDefinition.file = file + artifactDefinition.properties = properties return artifactDefinition } } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTypeDSLBuilder.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTypeDSLBuilder.kt index 0f011948d..8afe695ca 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTypeDSLBuilder.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTypeDSLBuilder.kt @@ -17,8 +17,8 @@ package org.onap.ccsdk.cds.controllerblueprints.core.dsl import com.fasterxml.jackson.databind.JsonNode -import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants -import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType +import com.fasterxml.jackson.databind.node.ArrayNode +import org.onap.ccsdk.cds.controllerblueprints.core.* import org.onap.ccsdk.cds.controllerblueprints.core.data.* @@ -162,7 +162,16 @@ class ArtifactTypeBuilder(id: String, version: String, derivedFrom: String, class PolicyTypeBuilder(id: String, version: String, derivedFrom: String, description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) { private var policyType = PolicyType() - // TODO() + + fun targets(targetsStr: String) { + val arrayNode = targetsStr.jsonAsJsonType() as ArrayNode + targets(arrayNode.asListOfString()) + } + + fun targets(target: List) { + policyType.targets = target.toMutableList() + } + fun build(): PolicyType { buildEntityType(policyType) return policyType @@ -174,7 +183,16 @@ class RelationshipTypeBuilder(private val id: String, private val version: Strin : EntityTypeBuilder(id, version, derivedFrom, description) { private var relationshipType = RelationshipType() - // TODO() + + fun validTargetTypes(validTargetTypesStr: String) { + val arrayNode = validTargetTypesStr.jsonAsJsonType() as ArrayNode + validTargetTypes(arrayNode.asListOfString()) + } + + fun validTargetTypes(validTargetTypes: List) { + relationshipType.validTargetTypes = validTargetTypes.toMutableList() + } + fun build(): RelationshipType { buildEntityType(relationshipType) relationshipType.id = id @@ -187,7 +205,15 @@ class RelationshipTypeBuilder(private val id: String, private val version: Strin class DataTypeBuilder(id: String, version: String, derivedFrom: String, description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) { private var dataType = DataType() - // TODO() + + fun constrain(block: ConstraintClauseBuilder.() -> Unit) { + if (dataType.constraints == null) { + dataType.constraints = mutableListOf() + } + val constraintClause = ConstraintClauseBuilder().apply(block).build() + dataType.constraints!!.add(constraintClause) + } + fun build(): DataType { buildEntityType(dataType) return dataType @@ -264,29 +290,6 @@ class OperationDefinitionBuilder(private val id: String, } } -class AttributesDefinitionBuilder { - private val attributes: MutableMap = hashMapOf() - - fun attribute(id: String, attribute: AttributeDefinition) { - attributes[id] = attribute - } - - fun attribute(id: String, type: String?, required: Boolean?, description: String?) { - val attribute = AttributeDefinitionBuilder(id, type, required, description).build() - attributes[id] = attribute - } - - fun attribute(id: String, type: String?, required: Boolean?, description: String?, - block: AttributeDefinitionBuilder.() -> Unit) { - val attribute = AttributeDefinitionBuilder(id, type, required, description).apply(block).build() - attributes[id] = attribute - } - - fun build(): MutableMap { - return attributes - } -} - class AttributeDefinitionBuilder(private val id: String, private val type: String? = BluePrintConstants.DATA_TYPE_STRING, private val required: Boolean? = false, @@ -302,7 +305,17 @@ class AttributeDefinitionBuilder(private val id: String, attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build() } - // TODO("Constrains") + fun constrain(block: ConstraintClauseBuilder.() -> Unit) { + if (attributeDefinition.constraints == null) { + attributeDefinition.constraints = mutableListOf() + } + val constraintClause = ConstraintClauseBuilder().apply(block).build() + attributeDefinition.constraints!!.add(constraintClause) + } + + fun defaultValue(defaultValue: Any) { + defaultValue(defaultValue.asJsonType()) + } fun defaultValue(defaultValue: JsonNode) { attributeDefinition.defaultValue = defaultValue @@ -355,8 +368,12 @@ class PropertyDefinitionBuilder(private val id: String, propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build() } - fun constrains(block: ConstraintClauseBuilder.() -> Unit) { - propertyDefinition.constraints = ConstraintClauseBuilder().apply(block).build() + fun constrain(block: ConstraintClauseBuilder.() -> Unit) { + if (propertyDefinition.constraints == null) { + propertyDefinition.constraints = mutableListOf() + } + val constraintClause = ConstraintClauseBuilder().apply(block).build() + propertyDefinition.constraints!!.add(constraintClause) } fun defaultValue(defaultValue: Any) { @@ -380,13 +397,11 @@ class PropertyDefinitionBuilder(private val id: String, } } -class ConstraintClauseBuilder { - private val constraints: MutableList = mutableListOf() - //TODO("Implementation") +class ConstraintsClauseBuilder { + val constraints: MutableList = mutableListOf() - fun validValues(values: List) { - val constraintClause = ConstraintClause() - constraintClause.validValues = values.toMutableList() + fun constrain(block: ConstraintClauseBuilder.() -> Unit) { + val constraintClause = ConstraintClauseBuilder().apply(block).build() constraints.add(constraintClause) } @@ -395,10 +410,82 @@ class ConstraintClauseBuilder { } } +class ConstraintClauseBuilder { + private val constraintClause = ConstraintClause() + + fun equal(equal: Any) = equal(equal.asJsonType()) + + fun equal(equal: JsonNode) { + constraintClause.equal = equal + } + + fun greaterOrEqual(greaterOrEqual: Any) { + constraintClause.greaterOrEqual = greaterOrEqual.asJsonPrimitive() + } + + fun greaterThan(greaterThan: Any) { + constraintClause.greaterThan = greaterThan.asJsonPrimitive() + } + + fun lessOrEqual(lessOrEqual: Any) { + constraintClause.lessOrEqual = lessOrEqual.asJsonPrimitive() + } + + fun lessThan(lessThan: Any) { + constraintClause.lessThan = lessThan.asJsonPrimitive() + } + + fun inRange(inRangeStr: String) = inRange(inRangeStr.jsonAsJsonType() as ArrayNode) + + fun inRange(inRangeNode: ArrayNode) { + constraintClause.inRange = inRangeNode.toMutableList() + } + + fun validValues(validValuesStr: String) = validValues(validValuesStr.jsonAsJsonType() as ArrayNode) + + fun validValues(validValuesNode: ArrayNode) = validValues(validValuesNode.toMutableList()) + + fun validValues(validValues: List) { + constraintClause.validValues = validValues.toMutableList() + } + + fun length(length: Any) { + constraintClause.length = length.asJsonPrimitive() + } + + fun minLength(minLength: Any) { + constraintClause.minLength = minLength.asJsonPrimitive() + } + + fun maxLength(maxLength: Any) { + constraintClause.maxLength = maxLength.asJsonPrimitive() + } + + fun pattern(pattern: String) { + constraintClause.pattern = pattern + } + + fun schema(schema: String) { + constraintClause.schema = schema + } + + fun build(): ConstraintClause { + return constraintClause + } +} + class EntrySchemaBuilder(private val type: String) { private var entrySchema: EntrySchema = EntrySchema() + fun constrain(block: ConstraintClauseBuilder.() -> Unit) { + if (entrySchema.constraints == null) { + entrySchema.constraints = mutableListOf() + } + val constraintClause = ConstraintClauseBuilder().apply(block).build() + entrySchema.constraints!!.add(constraintClause) + } + fun build(): EntrySchema { entrySchema.type = type return entrySchema diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt index 020edc78e..c0641be69 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt @@ -18,6 +18,7 @@ package org.onap.ccsdk.cds.controllerblueprints.core.dsl import org.junit.Test import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType import kotlin.test.assertNotNull @@ -152,6 +153,41 @@ class BluePrintDSLTest { //println(serviceTemplate.asJsonString(true)) } + @Test + fun testNodeTypePropertyConstrains() { + val nodeType = nodeType("data-node", "1.0.0", "tosca.Nodes.root", "") { + property("ip-address", "string", true, "") { + defaultValue("127.0.0.1") + constrain { + validValues(arrayListOf("""127.0.0.1""".asJsonPrimitive())) + length(10) + maxLength(20) + minLength(10) + } + + } + property("disk-space", "string", true, "") { + defaultValue(10) + constrain { + validValues("""["200KB", "400KB"]""") + equal("200KB") + inRange("""["100KB", "500KB" ]""") + maxLength("10MB") + minLength("10KB") + } + constrain { + validValues("""[ 200, 400]""") + greaterOrEqual("10KB") + greaterThan("20KB") + lessOrEqual("200KB") + lessThan("190KB") + } + } + } + assertNotNull(nodeType, "failed to get nodeType") + // println(nodeType.asJsonString(true)) + } + @Test fun testServiceTemplateWorkflow() { val serviceTemplate = serviceTemplate("sample-bp", "1.0.0", -- cgit 1.2.3-korg