Fix missing constrains definitions. 67/91467/2
authorBrinda Santh <brindasanth@in.ibm.com>
Mon, 15 Jul 2019 17:08:07 +0000 (13:08 -0400)
committerBrinda Santh <brindasanth@in.ibm.com>
Mon, 15 Jul 2019 20:31:19 +0000 (16:31 -0400)
Change-Id: I1d60b1a24a5876f47af3a7b9936a48187bbf2d16
Issue-ID: CCSDK-1380
Signed-off-by: Brinda Santh <brindasanth@in.ibm.com>
ms/blueprintsprocessor/modules/services/execution-service/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/services/execution/ComponentScriptExecutor.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/data/BluePrintModel.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt
ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTypeDSLBuilder.kt
ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt

index 056f7e9..b5dac5a 100644 (file)
@@ -86,7 +86,7 @@ fun BluePrintTypes.componentScriptExecutor(): NodeType {
                 property(ComponentScriptExecutor.SCRIPT_TYPE, BluePrintConstants.DATA_TYPE_STRING, true,
                         "Script Type") {
                     defaultValue(BluePrintConstants.SCRIPT_INTERNAL)
-                    constrains {
+                    constrain {
                         validValues(listOf(BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive(),
                                 BluePrintConstants.SCRIPT_JYTHON.asJsonPrimitive(),
                                 BluePrintConstants.SCRIPT_KOTLIN.asJsonPrimitive()))
index 42ff882..efdefd0 100644 (file)
@@ -64,8 +64,33 @@ fun Double.asJsonPrimitive(): DoubleNode {
     return DoubleNode.valueOf(this)
 }
 
+/**
+ * Utility to convert Primitive object to Json Type Primitive.
+ */
+fun <T : Any?> 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 : Any?> 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 : JsonNode> 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 : JsonNode> 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)
 }
 
 /**
index 9e934c7..68e5b0a 100644 (file)
@@ -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<JsonNode>? = null
     @get:JsonProperty("valid_values")
     var validValues: MutableList<JsonNode>? = 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<ConstraintClause>? = null
     @get:JsonProperty("entry_schema")
     var entrySchema: EntrySchema? = null
+    @get:JsonProperty("external-schema")
+    var externalSchema: String? = null
+    var metadata: MutableMap<String, String>? = 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<String, InterfaceDefinition>? = null
     @get:JsonProperty("valid_target_types")
-    var validTargetTypes: ArrayList<String>? = null
+    var validTargetTypes: MutableList<String>? = null
 }
 
 /*
index f19ae8e..eec59d1 100644 (file)
@@ -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<String, JsonNode>? = null
     private var interfaces: MutableMap<String, InterfaceAssignment>? = null
     private var artifacts: MutableMap<String, ArtifactDefinition>? = null
     private var capabilities: MutableMap<String, CapabilityAssignment>? = null
     private var requirements: MutableMap<String, RequirementAssignment>? = 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<String, JsonNode>? = 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
     }
 }
index 0f01194..8afe695 100644 (file)
@@ -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<String>) {
+        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<String>) {
+        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<String, AttributeDefinition> = 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<String, AttributeDefinition> {
-        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<ConstraintClause> = mutableListOf()
-    //TODO("Implementation")
+class ConstraintsClauseBuilder {
+    val constraints: MutableList<ConstraintClause> = mutableListOf()
 
-    fun validValues(values: List<JsonNode>) {
-        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<JsonNode>) {
+        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
index 020edc7..c0641be 100644 (file)
@@ -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",