Formatting Code base with ktlint
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BluePrintTypeDSLBuilder.kt
index 0f01194..e183b80 100644 (file)
 package org.onap.ccsdk.cds.controllerblueprints.core.dsl
 
 import com.fasterxml.jackson.databind.JsonNode
+import com.fasterxml.jackson.databind.node.ArrayNode
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
+import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
-import org.onap.ccsdk.cds.controllerblueprints.core.data.*
+import org.onap.ccsdk.cds.controllerblueprints.core.asListOfString
+import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactDefinition
+import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
+import org.onap.ccsdk.cds.controllerblueprints.core.data.AttributeDefinition
+import org.onap.ccsdk.cds.controllerblueprints.core.data.CapabilityDefinition
+import org.onap.ccsdk.cds.controllerblueprints.core.data.ConstraintClause
+import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
+import org.onap.ccsdk.cds.controllerblueprints.core.data.EntityType
+import org.onap.ccsdk.cds.controllerblueprints.core.data.EntrySchema
+import org.onap.ccsdk.cds.controllerblueprints.core.data.InterfaceDefinition
+import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
+import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationDefinition
+import org.onap.ccsdk.cds.controllerblueprints.core.data.PolicyType
+import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
+import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
+import org.onap.ccsdk.cds.controllerblueprints.core.data.RequirementDefinition
+import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
+
+open class EntityTypeBuilder(
+    private val id: String,
+    private val version: String,
+    private val derivedFrom: String,
+    private val description: String? = ""
+) {
 
-
-open class EntityTypeBuilder(private val id: String,
-                             private val version: String,
-                             private val derivedFrom: String,
-                             private val description: String? = "") {
     var metadata: MutableMap<String, String>? = null
     var properties: MutableMap<String, PropertyDefinition>? = null
     var attributes: MutableMap<String, AttributeDefinition>? = null
@@ -43,8 +63,13 @@ open class EntityTypeBuilder(private val id: String,
         attributes!![id] = attribute
     }
 
-    fun attribute(id: String, type: String, required: Boolean, description: String? = "",
-                  block: AttributeDefinitionBuilder.() -> Unit) {
+    fun attribute(
+        id: String,
+        type: String,
+        required: Boolean,
+        description: String? = "",
+        block: AttributeDefinitionBuilder.() -> Unit
+    ) {
         if (attributes == null)
             attributes = hashMapOf()
         val attribute = AttributeDefinitionBuilder(id, type, required, description).apply(block).build()
@@ -58,8 +83,13 @@ open class EntityTypeBuilder(private val id: String,
         properties!![id] = property
     }
 
-    fun property(id: String, type: String, required: Boolean, description: String? = "",
-                 block: PropertyDefinitionBuilder.() -> Unit) {
+    fun property(
+        id: String,
+        type: String,
+        required: Boolean,
+        description: String? = "",
+        block: PropertyDefinitionBuilder.() -> Unit
+    ) {
         if (properties == null)
             properties = hashMapOf()
         val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
@@ -77,8 +107,13 @@ open class EntityTypeBuilder(private val id: String,
     }
 }
 
-class NodeTypeBuilder(id: String, version: String, derivedFrom: String,
-                      description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
+class NodeTypeBuilder(
+    id: String,
+    version: String,
+    derivedFrom: String,
+    description: String?
+) : EntityTypeBuilder(id, version, derivedFrom, description) {
+
     private var nodeType = NodeType()
     private var capabilities: MutableMap<String, CapabilityDefinition>? = null
     private var requirements: MutableMap<String, RequirementDefinition>? = null
@@ -97,12 +132,18 @@ class NodeTypeBuilder(id: String, version: String, derivedFrom: String,
         requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description).build()
     }
 
-    fun requirement(id: String, capability: String, node: String, relationship: String, description: String,
-                    block: RequirementDefinitionBuilder.() -> Unit) {
+    fun requirement(
+        id: String,
+        capability: String,
+        node: String,
+        relationship: String,
+        description: String,
+        block: RequirementDefinitionBuilder.() -> Unit
+    ) {
         if (requirements == null)
             requirements = hashMapOf()
         requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description)
-                .apply(block).build()
+            .apply(block).build()
     }
 
     fun artifact(id: String, type: String, file: String) {
@@ -125,7 +166,7 @@ class NodeTypeBuilder(id: String, version: String, derivedFrom: String,
         val defaultOperationName = "process"
         interfaceDefinition.operations = hashMapOf()
         interfaceDefinition.operations!![defaultOperationName] =
-                OperationDefinitionBuilder(defaultOperationName, description).apply(block).build()
+            OperationDefinitionBuilder(defaultOperationName, description).apply(block).build()
         interfaces!![interfaceName] = interfaceDefinition
     }
 
@@ -139,8 +180,13 @@ class NodeTypeBuilder(id: String, version: String, derivedFrom: String,
     }
 }
 
-class ArtifactTypeBuilder(id: String, version: String, derivedFrom: String,
-                          description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
+class ArtifactTypeBuilder(
+    id: String,
+    version: String,
+    derivedFrom: String,
+    description: String?
+) : EntityTypeBuilder(id, version, derivedFrom, description) {
+
     private var artifactType = ArtifactType()
     private var fileExt: MutableList<String>? = null
 
@@ -159,22 +205,49 @@ class ArtifactTypeBuilder(id: String, version: String, derivedFrom: String,
     }
 }
 
-class PolicyTypeBuilder(id: String, version: String, derivedFrom: String,
-                        description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
+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
     }
 }
 
-class RelationshipTypeBuilder(private val id: String, private val version: String,
-                              derivedFrom: String, private val description: String?)
-    : EntityTypeBuilder(id, version, derivedFrom, description) {
+class RelationshipTypeBuilder(
+    private val id: String,
+    private val version: String,
+    derivedFrom: String,
+    private val description: String?
+) :
+    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
@@ -184,10 +257,23 @@ 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) {
+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
@@ -213,8 +299,14 @@ class CapabilityDefinitionBuilder(private val id: String, private val type: Stri
     }
 }
 
-class RequirementDefinitionBuilder(private val id: String, private val capability: String, private val node: String,
-                                   private val relationship: String, private val description: String? = "") {
+class RequirementDefinitionBuilder(
+    private val id: String,
+    private val capability: String,
+    private val node: String,
+    private val relationship: String,
+    private val description: String? = ""
+) {
+
     private var requirementDefinition = RequirementDefinition()
 
     fun build(): RequirementDefinition {
@@ -245,8 +337,11 @@ class InterfaceDefinitionBuilder(private val id: String) {
     }
 }
 
-class OperationDefinitionBuilder(private val id: String,
-                                 private val description: String? = "") {
+class OperationDefinitionBuilder(
+    private val id: String,
+    private val description: String? = ""
+) {
+
     private var operationDefinition: OperationDefinition = OperationDefinition()
 
     fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
@@ -264,33 +359,12 @@ 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,
-                                 private val description: String? = "") {
+class AttributeDefinitionBuilder(
+    private val id: String,
+    private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
+    private val required: Boolean? = false,
+    private val description: String? = ""
+) {
 
     private var attributeDefinition: AttributeDefinition = AttributeDefinition()
 
@@ -302,7 +376,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
@@ -329,8 +413,13 @@ class PropertiesDefinitionBuilder {
         properties[id] = property
     }
 
-    fun property(id: String, type: String?, required: Boolean?, description: String? = "",
-                 block: PropertyDefinitionBuilder.() -> Unit) {
+    fun property(
+        id: String,
+        type: String?,
+        required: Boolean?,
+        description: String? = "",
+        block: PropertyDefinitionBuilder.() -> Unit
+    ) {
         val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
         properties[id] = property
     }
@@ -340,10 +429,12 @@ class PropertiesDefinitionBuilder {
     }
 }
 
-class PropertyDefinitionBuilder(private val id: String,
-                                private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
-                                private val required: Boolean? = false,
-                                private val description: String? = "") {
+class PropertyDefinitionBuilder(
+    private val id: String,
+    private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
+    private val required: Boolean? = false,
+    private val description: String? = ""
+) {
 
     private var propertyDefinition: PropertyDefinition = PropertyDefinition()
 
@@ -355,8 +446,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) {
@@ -367,6 +462,17 @@ class PropertyDefinitionBuilder(private val id: String,
         propertyDefinition.defaultValue = defaultValue
     }
 
+    fun metadata(name: String, value: String) {
+        if (propertyDefinition.metadata == null) {
+            propertyDefinition.metadata = hashMapOf()
+        }
+        propertyDefinition.metadata!![name] = value
+    }
+
+    fun value(value: Any) {
+        value(value.asJsonType())
+    }
+
     fun value(value: JsonNode) {
         propertyDefinition.value = value
     }
@@ -380,13 +486,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,12 +499,83 @@ 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
     }
-}
\ No newline at end of file
+}