diff options
Diffstat (limited to 'ms/controllerblueprints/modules')
8 files changed, 1150 insertions, 4 deletions
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 583fc9d4d..4f2b7a121 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 @@ -29,10 +29,20 @@ import kotlin.reflect.KClass * @author Brinda Santh */ +fun String.isJson(): Boolean { + return ((this.startsWith("{") && this.endsWith("}")) + || (this.startsWith("[") && this.endsWith("]"))) +} + fun String.asJsonPrimitive(): TextNode { return TextNode(this) } +// If you know the string is json content, then use the function directly +fun String.jsonAsJsonType(): JsonNode { + return JacksonUtils.jsonNode(this.trim()) +} + fun Boolean.asJsonPrimitive(): BooleanNode { return BooleanNode.valueOf(this) } @@ -52,8 +62,12 @@ fun <T : Any?> T.asJsonType(): JsonNode { when (this) { is JsonNode -> this - is String -> - TextNode(this) + is String -> { + if (this.isJson()) + this.jsonAsJsonType() + else + TextNode(this) + } is Boolean -> BooleanNode.valueOf(this) is Int -> 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 0638cc521..745671ac9 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 @@ -23,6 +23,7 @@ import com.fasterxml.jackson.annotation.JsonProperty import com.fasterxml.jackson.annotation.JsonPropertyOrder import com.fasterxml.jackson.databind.JsonNode import io.swagger.annotations.ApiModelProperty +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants /** * @@ -207,8 +208,12 @@ class OperationDefinition { } class Implementation { - lateinit var primary: String + var primary: String? = null var dependencies: MutableList<String>? = null + @get:JsonProperty("operation_host") + var operationHost: String = BluePrintConstants.PROPERTY_SELF + //Timeout value in seconds + var timeout: Int = 180 } /* @@ -369,7 +374,10 @@ class NodeType : EntityType() { /* 3.6.8 Requirement Type -A Requirement Type is a reusable entity that describes a kind of requirement that a Node Type can declare to expose. The TOSCA Simple Profile seeks to simplify the need for declaring specific Requirement Types from nodes and instead rely upon nodes declaring their features sets using TOSCA Capability Types along with a named Feature notation. +A Requirement Type is a reusable entity that describes a kind of requirement that a Node Type can declare to expose. +The TOSCA Simple Profile seeks to simplify the need for declaring specific Requirement Types +from nodes and instead rely upon nodes declaring their features sets using TOSCA Capability Types +along with a named Feature notation. */ class RequirementType : EntityType() { diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSL.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSL.kt new file mode 100644 index 000000000..4878cb696 --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSL.kt @@ -0,0 +1,76 @@ +/* + * 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.controllerblueprints.core.dsl + +import com.fasterxml.jackson.databind.JsonNode +import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate +import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType + +fun serviceTemplate(name: String, version: String, author: String, tags: String, + block: ServiceTemplateBuilder.() -> Unit): ServiceTemplate { + return ServiceTemplateBuilder(name, version, author, tags).apply(block).build() +} + +// Input Function + +fun getInput(inputKey: String): JsonNode { + return """{"get_input": "$inputKey"}""".jsonAsJsonType() +} + +fun getAttribute(attributeName: String): JsonNode { + return """{"get_attribute": ["SELF", "$attributeName"]}""".jsonAsJsonType() +} + +fun getAttribute(attributeName: String, jsonPath: String): JsonNode { + return """{"get_attribute": ["SELF", "$attributeName", "$jsonPath"]}""".jsonAsJsonType() +} + +fun getNodeTemplateAttribute(nodeTemplateName: String, attributeName: String): JsonNode { + return """{"get_attribute": ["$nodeTemplateName", "$attributeName"]}""".jsonAsJsonType() +} + +fun getNodeTemplateAttribute(nodeTemplateName: String, attributeName: String, jsonPath: String): JsonNode { + return """{"get_attribute": ["$nodeTemplateName", "$attributeName", "$jsonPath]}""".jsonAsJsonType() +} + +// Property Function + +fun getProperty(propertyName: String): JsonNode { + return """{"get_property": ["SELF", "$propertyName"]}""".jsonAsJsonType() +} + +fun getProperty(propertyName: String, jsonPath: String): JsonNode { + return """{"get_property": ["SELF", "$propertyName", "$jsonPath"]}""".jsonAsJsonType() +} + +fun getNodeTemplateProperty(nodeTemplateName: String, propertyName: String): JsonNode { + return """{"get_property": ["$nodeTemplateName", "$propertyName"]}""".jsonAsJsonType() +} + +fun getNodeTemplateProperty(nodeTemplateName: String, propertyName: String, jsonPath: String): JsonNode { + return """{"get_property": ["$nodeTemplateName", "$propertyName", "$jsonPath]}""".jsonAsJsonType() +} + +// Artifact Function + +fun getArtifact(artifactName: String): JsonNode { + return """{"get_artifact": ["SELF", "$artifactName"]}""".jsonAsJsonType() +} + +fun getNodeTemplateArtifact(nodeTemplateName: String, artifactName: String): JsonNode { + return """{"get_artifact": ["$nodeTemplateName", "$artifactName"]}""".jsonAsJsonType() +}
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintServiceDSLBuilder.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintServiceDSLBuilder.kt new file mode 100644 index 000000000..3415be8f3 --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintServiceDSLBuilder.kt @@ -0,0 +1,185 @@ +/* + * 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.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 org.onap.ccsdk.cds.controllerblueprints.core.data.* + +class ServiceTemplateBuilder(private val name: String, + private val version: String, + private val author: String, + private val tags: String) { + private var serviceTemplate = ServiceTemplate() + private lateinit var topologyTemplate: TopologyTemplate + private var metadata: MutableMap<String, String> = hashMapOf() + private var dslDefinitions: MutableMap<String, JsonNode>? = null + private var imports: MutableList<ImportDefinition> = mutableListOf() + private var nodeTypes: MutableMap<String, NodeType>? = null + private var artifactTypes: MutableMap<String, ArtifactType>? = null + private var dataTypes: MutableMap<String, DataType>? = null + private var relationshipTypes: MutableMap<String, RelationshipType>? = null + private var policyTypes: MutableMap<String, PolicyType>? = null + + private fun initMetaData() { + metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name + metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version + metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author + metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags + } + + fun metadata(id: String, value: String) { + metadata[id] = value + } + + fun import(file: String) { + val importDefinition = ImportDefinition().apply { + this.file = file + } + imports.add(importDefinition) + } + + fun dsl(id: String, json: String) { + dsl(id, json.asJsonType()) + } + + fun dsl(id: String, json: JsonNode) { + if (dslDefinitions == null) + dslDefinitions = hashMapOf() + dslDefinitions!![id] = json.asJsonType() + } + + fun dataTypes(dataTypes: MutableMap<String, DataType>) { + if (this.dataTypes == null) + this.dataTypes = hashMapOf() + + this.dataTypes!!.putAll(dataTypes) + } + + fun artifactTypes(artifactTypes: MutableMap<String, ArtifactType>) { + if (this.artifactTypes == null) + this.artifactTypes = hashMapOf() + + this.artifactTypes!!.putAll(artifactTypes) + } + + fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) { + if (this.relationshipTypes == null) + this.relationshipTypes = hashMapOf() + + this.relationshipTypes!!.putAll(relationshipTypes) + } + + fun policyTypes(policyTypes: MutableMap<String, PolicyType>) { + if (this.policyTypes == null) + this.policyTypes = hashMapOf() + + this.policyTypes!!.putAll(policyTypes) + } + + fun nodeType(nodeTypes: MutableMap<String, NodeType>) { + if (this.nodeTypes == null) + this.nodeTypes = hashMapOf() + + this.nodeTypes!!.putAll(nodeTypes) + } + + fun dataType(dataType: DataType) { + if (dataTypes == null) + dataTypes = hashMapOf() + dataTypes!![dataType.id!!] = dataType + } + + fun artifactType(artifactType: ArtifactType) { + if (artifactTypes == null) + artifactTypes = hashMapOf() + artifactTypes!![artifactType.id!!] = artifactType + } + + fun relationshipType(relationshipType: RelationshipType) { + if (relationshipTypes == null) + relationshipTypes = hashMapOf() + relationshipTypes!![relationshipType.id!!] = relationshipType + } + + fun policyType(policyType: PolicyType) { + if (policyTypes == null) + policyTypes = hashMapOf() + + policyTypes!![policyType.id!!] = policyType + } + + fun nodeType(nodeType: NodeType) { + if (nodeTypes == null) + nodeTypes = hashMapOf() + nodeTypes!![nodeType.id!!] = nodeType + } + + fun dataType(id: String, version: String, derivedFrom: String, description: String, + block: DataTypeBuilder.() -> Unit) { + if (dataTypes == null) + dataTypes = hashMapOf() + dataTypes!![id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build() + } + + fun artifactType(id: String, version: String, derivedFrom: String, description: String, + block: ArtifactTypeBuilder.() -> Unit) { + if (artifactTypes == null) + artifactTypes = hashMapOf() + artifactTypes!![id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build() + } + + fun relationshipType(id: String, version: String, derivedFrom: String, description: String, + block: RelationshipTypeBuilder.() -> Unit) { + if (relationshipTypes == null) + relationshipTypes = hashMapOf() + relationshipTypes!![id] = RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build() + } + + fun policyType(id: String, version: String, derivedFrom: String, description: String, + block: PolicyTypeBuilder.() -> Unit) { + if (policyTypes == null) + policyTypes = hashMapOf() + policyTypes!![id] = PolicyTypeBuilder(id, version, derivedFrom, description).apply(block).build() + } + + fun nodeType(id: String, version: String, derivedFrom: String, description: String, + block: NodeTypeBuilder.() -> Unit) { + if (nodeTypes == null) + nodeTypes = hashMapOf() + nodeTypes!![id] = NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build() + } + + fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) { + topologyTemplate = TopologyTemplateBuilder().apply(block).build() + } + + fun build(): ServiceTemplate { + initMetaData() + serviceTemplate.metadata = metadata + serviceTemplate.imports = imports + serviceTemplate.dslDefinitions = dslDefinitions + serviceTemplate.nodeTypes = nodeTypes + serviceTemplate.artifactTypes = artifactTypes + serviceTemplate.dataTypes = dataTypes + serviceTemplate.relationshipTypes = relationshipTypes + serviceTemplate.policyTypes = policyTypes + serviceTemplate.topologyTemplate = topologyTemplate + return serviceTemplate + } +} 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 new file mode 100644 index 000000000..7973293ae --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTemplateDSLBuilder.kt @@ -0,0 +1,260 @@ +/* + * 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.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 org.onap.ccsdk.cds.controllerblueprints.core.data.* + +class TopologyTemplateBuilder { + private var topologyTemplate = TopologyTemplate() + private var nodeTemplates: MutableMap<String, NodeTemplate>? = null + private var workflows: MutableMap<String, Workflow>? = null + + fun nodeTemplate(id: String, type: String, description: String, block: NodeTemplateBuilder.() -> Unit) { + if (nodeTemplates == null) + nodeTemplates = hashMapOf() + nodeTemplates!![id] = NodeTemplateBuilder(id, type, description).apply(block).build() + } + + fun nodeTemplateOperation(nodeTemplateName: String, type: String, interfaceName: String, description: String, + operationBlock: OperationAssignmentBuilder.() -> Unit) { + if (nodeTemplates == null) + nodeTemplates = hashMapOf() + + val nodeTemplateBuilder = NodeTemplateBuilder(nodeTemplateName, type, description) + nodeTemplateBuilder.operation(interfaceName, "$description operation", operationBlock) + nodeTemplates!![nodeTemplateName] = nodeTemplateBuilder.build() + } + + fun workflow(id: String, description: String, block: WorkflowBuilder.() -> Unit) { + if (workflows == null) + workflows = hashMapOf() + workflows!![id] = WorkflowBuilder(id, description).apply(block).build() + } + + //TODO("populate inputs, outputs") + fun workflowNodeTemplate(actionName: String, + nodeTemplateType: String, description: String, block: NodeTemplateBuilder.() -> Unit) { + if (nodeTemplates == null) + nodeTemplates = hashMapOf() + + if (workflows == null) + workflows = hashMapOf() + + val workflowBuilder = WorkflowBuilder(actionName, description) + workflowBuilder.nodeTemplateStep(actionName, description) + // Workflow name is NodeTemplate name + workflows!![actionName] = workflowBuilder.build() + + nodeTemplates!![actionName] = NodeTemplateBuilder(actionName, nodeTemplateType, description).apply(block).build() + } + + fun build(): TopologyTemplate { + topologyTemplate.nodeTemplates = nodeTemplates + topologyTemplate.workflows = workflows + return topologyTemplate + } +} + +class NodeTemplateBuilder(private val id: String, + private val type: String, + private val description: String? = "") { + private var nodeTemplate: NodeTemplate = NodeTemplate() + 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 operation(interfaceName: String, description: String? = "", + block: OperationAssignmentBuilder.() -> Unit) { + if (interfaces == null) + interfaces = hashMapOf() + + val interfaceAssignment = InterfaceAssignment() + val defaultOperationName = "process" + interfaceAssignment.operations = hashMapOf() + interfaceAssignment.operations!![defaultOperationName] = + OperationAssignmentBuilder(defaultOperationName, description).apply(block).build() + interfaces!![interfaceName] = interfaceAssignment + } + + fun artifact(id: String, type: String, file: String) { + if (artifacts == null) + artifacts = hashMapOf() + artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build() + } + + fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) { + if (artifacts == null) + artifacts = hashMapOf() + artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build() + } + + fun capability(id: String, block: CapabilityAssignmentBuilder.() -> Unit) { + if (capabilities == null) + capabilities = hashMapOf() + capabilities!![id] = CapabilityAssignmentBuilder(id).apply(block).build() + } + + fun requirement(id: String, capability: String, node: String, relationship: String) { + if (requirements == null) + requirements = hashMapOf() + requirements!![id] = RequirementAssignmentBuilder(id, capability, node, relationship).build() + } + + fun build(): NodeTemplate { + nodeTemplate.id = id + nodeTemplate.type = type + nodeTemplate.description = description + nodeTemplate.interfaces = interfaces + nodeTemplate.artifacts = artifacts + nodeTemplate.capabilities = capabilities + nodeTemplate.requirements = requirements + return nodeTemplate + } +} + +class ArtifactDefinitionBuilder(private val id: String, private val type: String, private val file: String) { + + private var artifactDefinition: ArtifactDefinition = ArtifactDefinition() + // TODO() + + fun build(): ArtifactDefinition { + artifactDefinition.id = id + artifactDefinition.type = type + artifactDefinition.file = file + return artifactDefinition + } +} + +class CapabilityAssignmentBuilder(private val id: String) { + private var capabilityAssignment: CapabilityAssignment = CapabilityAssignment() + private var attributes: MutableMap<String, JsonNode>? = null + private var properties: MutableMap<String, JsonNode>? = null + + fun attributes(block: AttributesAssignmentBuilder.() -> Unit) { + if (attributes == null) + attributes = hashMapOf() + attributes = AttributesAssignmentBuilder().apply(block).build() + } + + fun properties(block: PropertiesAssignmentBuilder.() -> Unit) { + if (properties == null) + properties = hashMapOf() + properties = PropertiesAssignmentBuilder().apply(block).build() + } + + fun build(): CapabilityAssignment { + capabilityAssignment.properties = properties + capabilityAssignment.attributes = attributes + return capabilityAssignment + } +} + +class RequirementAssignmentBuilder(private val id: String, private val capability: String, + private val node: String, + private val relationship: String) { + private var requirementAssignment: RequirementAssignment = RequirementAssignment() + + fun build(): RequirementAssignment { + requirementAssignment.id = id + requirementAssignment.capability = capability + requirementAssignment.node = node + requirementAssignment.relationship = relationship + return requirementAssignment + } +} + +class InterfaceAssignmentBuilder(private val id: String) { + + private var interfaceAssignment: InterfaceAssignment = InterfaceAssignment() + private var operations: MutableMap<String, OperationAssignment>? = null + + fun operation(id: String, description: String? = "", block: OperationAssignmentBuilder.() -> Unit) { + if (operations == null) + operations = hashMapOf() + operations!![id] = OperationAssignmentBuilder(id, description).apply(block).build() + } + + fun build(): InterfaceAssignment { + interfaceAssignment.id = id + interfaceAssignment.operations = operations + return interfaceAssignment + } +} + +class OperationAssignmentBuilder(private val id: String, + private val description: String? = "") { + + private var operationAssignment: OperationAssignment = OperationAssignment() + + fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) { + val implementation = Implementation().apply { + this.operationHost = operationHost!! + this.timeout = timeout + } + operationAssignment.implementation = implementation + } + + fun inputs(block: PropertiesAssignmentBuilder.() -> Unit) { + operationAssignment.inputs = PropertiesAssignmentBuilder().apply(block).build() + } + + fun outputs(block: PropertiesAssignmentBuilder.() -> Unit) { + operationAssignment.outputs = PropertiesAssignmentBuilder().apply(block).build() + } + + fun build(): OperationAssignment { + operationAssignment.id = id + operationAssignment.description = description + return operationAssignment + } +} + +class PropertiesAssignmentBuilder { + private var properties: MutableMap<String, JsonNode> = hashMapOf() + + fun property(id: String, value: Any) { + property(id, value.asJsonType()) + } + + fun property(id: String, value: JsonNode) { + properties[id] = value + } + + fun build(): MutableMap<String, JsonNode> { + return properties + } +} + +class AttributesAssignmentBuilder { + private var attributes: MutableMap<String, JsonNode> = hashMapOf() + + fun attribute(id: String, value: String) { + attribute(id, value.asJsonType()) + } + + fun attribute(id: String, value: JsonNode) { + attributes[id] = value + } + + fun build(): MutableMap<String, JsonNode> { + return attributes + } +}
\ No newline at end of file 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 new file mode 100644 index 000000000..07cc20ebd --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintTypeDSLBuilder.kt @@ -0,0 +1,382 @@ +/* + * 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.controllerblueprints.core.dsl + +import com.fasterxml.jackson.databind.JsonNode +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.data.* + + +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 + + fun metadata(key: String, value: String) { + if (metadata == null) + metadata = hashMapOf() + metadata!![key] = value + } + + fun attribute(id: String, type: String, required: Boolean, description: String? = "") { + if (attributes == null) + attributes = hashMapOf() + val attribute = AttributeDefinitionBuilder(id, type, required, description).build() + attributes!![id] = attribute + } + + 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() + attributes!![id] = attribute + } + + fun property(id: String, type: String, required: Boolean, description: String? = "") { + if (properties == null) + properties = hashMapOf() + val property = PropertyDefinitionBuilder(id, type, required, description).build() + properties!![id] = property + } + + 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() + properties!![id] = property + } + + fun buildEntityType(entity: EntityType) { + entity.id = id + entity.description = description + entity.version = version + entity.derivedFrom = derivedFrom + entity.metadata = metadata + entity.properties = properties + entity.attributes = attributes + } +} + +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 + private var interfaces: MutableMap<String, InterfaceDefinition>? = null + private var artifacts: MutableMap<String, ArtifactDefinition>? = null + + fun capability(id: String, type: String, description: String, block: CapabilityDefinitionBuilder.() -> Unit) { + if (capabilities == null) + capabilities = hashMapOf() + capabilities!![id] = CapabilityDefinitionBuilder(id, type, description).apply(block).build() + } + + fun requirement(id: String, capability: String, node: String, relationship: String, description: String) { + if (requirements == null) + requirements = hashMapOf() + requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description).build() + } + + 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() + } + + fun artifact(id: String, type: String, file: String) { + if (artifacts == null) + artifacts = hashMapOf() + artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build() + } + + private fun nodeInterface(id: String, block: InterfaceDefinitionBuilder.() -> Unit) { + if (interfaces == null) + interfaces = hashMapOf() + interfaces!![id] = InterfaceDefinitionBuilder(id).apply(block).build() + } + + fun operation(interfaceName: String, description: String?, block: OperationDefinitionBuilder.() -> Unit) { + if (interfaces == null) + interfaces = hashMapOf() + + val interfaceDefinition = InterfaceDefinition() + val defaultOperationName = "process" + interfaceDefinition.operations = hashMapOf() + interfaceDefinition.operations!![defaultOperationName] = + OperationDefinitionBuilder(defaultOperationName, description).apply(block).build() + interfaces!![interfaceName] = interfaceDefinition + } + + fun build(): NodeType { + buildEntityType(nodeType) + nodeType.capabilities = capabilities + nodeType.requirements = requirements + nodeType.interfaces = interfaces + nodeType.artifacts = artifacts + return nodeType + } +} + +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 + + fun fileExt(vararg fileExts: String) { + if (fileExt == null) + fileExt = arrayListOf() + fileExts.forEach { + fileExt!!.add(it) + } + } + + fun build(): ArtifactType { + buildEntityType(artifactType) + artifactType.fileExt = fileExt + return artifactType + } +} + +class PolicyTypeBuilder(id: String, version: String, derivedFrom: String, + description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) { + private var policyType = PolicyType() + // TODO() + 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) { + + private var relationshipType = RelationshipType() + // TODO() + fun build(): RelationshipType { + buildEntityType(relationshipType) + relationshipType.id = id + relationshipType.version = version + relationshipType.description = description + return relationshipType + } +} + +class DataTypeBuilder(id: String, version: String, derivedFrom: String, + description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) { + private var dataType = DataType() + // TODO() + fun build(): DataType { + buildEntityType(dataType) + return dataType + } +} + +class CapabilityDefinitionBuilder(private val id: String, private val type: String, private val description: String? = "") { + + private var capabilityDefinition = CapabilityDefinition() + private val properties: MutableMap<String, PropertyDefinition> = hashMapOf() + + fun property(id: String, type: String? = BluePrintConstants.DATA_TYPE_STRING, required: Boolean? = false, description: String? = "") { + val property = PropertyDefinitionBuilder(id, type, required, description).build() + properties[id] = property + } + + fun build(): CapabilityDefinition { + capabilityDefinition.id = id + capabilityDefinition.description = description + capabilityDefinition.type = type + capabilityDefinition.properties = properties + return capabilityDefinition + } +} + +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 { + requirementDefinition.id = id + requirementDefinition.description = description + requirementDefinition.capability = capability + requirementDefinition.node = node + requirementDefinition.relationship = relationship + return requirementDefinition + } +} + +class InterfaceDefinitionBuilder(private val id: String) { + + private var interfaceDefinition: InterfaceDefinition = InterfaceDefinition() + private var operations: MutableMap<String, OperationDefinition>? = null + + fun operation(id: String, description: String? = "", block: OperationDefinitionBuilder.() -> Unit) { + if (operations == null) + operations = hashMapOf() + operations!![id] = OperationDefinitionBuilder(id, description).apply(block).build() + } + + fun build(): InterfaceDefinition { + interfaceDefinition.id = id + interfaceDefinition.operations = operations + return interfaceDefinition + } +} + +class OperationDefinitionBuilder(private val id: String, + private val description: String? = "") { + private var operationDefinition: OperationDefinition = OperationDefinition() + + fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) { + operationDefinition.inputs = PropertiesDefinitionBuilder().apply(block).build() + } + + fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) { + operationDefinition.outputs = PropertiesDefinitionBuilder().apply(block).build() + } + + fun build(): OperationDefinition { + operationDefinition.id = id + operationDefinition.description = description + return operationDefinition + } +} + +class AttributesDefinitionBuilder { + private val attributes: MutableMap<String, AttributeDefinition> = hashMapOf() + + fun property(id: String, attribute: AttributeDefinition) { + attributes[id] = attribute + } + + fun property(id: String, type: String?, required: Boolean?, description: String?) { + val attribute = AttributeDefinitionBuilder(id, type, required, description).build() + attributes[id] = attribute + } + + fun property(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? = "") { + + private var attributeDefinition: AttributeDefinition = AttributeDefinition() + + fun entrySchema(entrySchemaType: String) { + attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build() + } + + fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) { + attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build() + } + + // TODO("Constrains") + + fun defaultValue(defaultValue: JsonNode) { + attributeDefinition.defaultValue = defaultValue + } + + fun build(): AttributeDefinition { + attributeDefinition.id = id + attributeDefinition.type = type!! + attributeDefinition.required = required + attributeDefinition.description = description + return attributeDefinition + } +} + +class PropertiesDefinitionBuilder { + private val properties: MutableMap<String, PropertyDefinition> = hashMapOf() + + fun property(id: String, property: PropertyDefinition) { + properties[id] = property + } + + fun property(id: String, type: String?, required: Boolean?, description: String? = "") { + val property = PropertyDefinitionBuilder(id, type, required, description).build() + properties[id] = property + } + + 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 + } + + fun build(): MutableMap<String, PropertyDefinition> { + return properties + } +} + +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() + + fun entrySchema(entrySchemaType: String) { + propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build() + } + + fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) { + propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build() + } + // TODO("Constrains") + + fun defaultValue(defaultValue: JsonNode) { + propertyDefinition.defaultValue = defaultValue + } + + fun value(value: JsonNode) { + propertyDefinition.value = value + } + + fun build(): PropertyDefinition { + propertyDefinition.id = id + propertyDefinition.type = type!! + propertyDefinition.required = required + propertyDefinition.description = description + return propertyDefinition + } +} + +class EntrySchemaBuilder(private val type: String) { + private var entrySchema: EntrySchema = EntrySchema() + + fun build(): EntrySchema { + entrySchema.type = type + return entrySchema + } +}
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintWorkflowDSLBuilder.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintWorkflowDSLBuilder.kt new file mode 100644 index 000000000..5ec3df160 --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintWorkflowDSLBuilder.kt @@ -0,0 +1,108 @@ +/* + * 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.controllerblueprints.core.dsl + +import org.onap.ccsdk.cds.controllerblueprints.core.data.Activity +import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition +import org.onap.ccsdk.cds.controllerblueprints.core.data.Step +import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow + +class WorkflowBuilder(private val id: String, private val description: String) { + + private var workflow = Workflow() + private var steps: MutableMap<String, Step>? = null + private var inputs: MutableMap<String, PropertyDefinition>? = null + private var outputs: MutableMap<String, PropertyDefinition>? = null + + // Used Internally + fun nodeTemplateStep(nodeTemplateName: String, description: String) { + step(nodeTemplateName, nodeTemplateName, "") + } + + fun step(id: String, target: String, description: String) { + if (steps == null) + steps = hashMapOf() + steps!![id] = StepBuilder(id, target, description).build() + } + + fun step(id: String, target: String, description: String, block: StepBuilder.() -> Unit) { + if (steps == null) + steps = hashMapOf() + steps!![id] = StepBuilder(id, target, description).apply(block).build() + } + + fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) { + inputs = PropertiesDefinitionBuilder().apply(block).build() + } + + fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) { + outputs = PropertiesDefinitionBuilder().apply(block).build() + } + + fun build(): Workflow { + workflow.id = id + workflow.description = description + workflow.steps = steps + workflow.inputs = inputs + workflow.outputs = outputs + return workflow + } + +} + +class StepBuilder(private val id: String, private val target: String, + private val description: String) { + + private var step = Step() + private var activities: ArrayList<Activity> = arrayListOf() + private var onSuccess: ArrayList<String>? = null + private var onFailure: ArrayList<String>? = null + + fun activity(callOperation: String) { + val activity = Activity() + activity.callOperation = callOperation + activities.add(activity) + } + + fun success(vararg successTargets: String) { + if (onSuccess == null) + onSuccess = arrayListOf() + successTargets.forEach { + onSuccess!!.add(it) + } + } + + fun failure(vararg failureTargets: String) { + if (onFailure == null) + onFailure = arrayListOf() + failureTargets.forEach { + onFailure!!.add(it) + } + } + + fun build(): Step { + step.id = id + step.target = target + // Add Default Activity, Assumption is only one Operation + activity(".process") + step.description = description + step.activities = activities + step.onSuccess = onSuccess + step.onFailure = onFailure + return step + } +} 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 new file mode 100644 index 000000000..dff0f943f --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLTest.kt @@ -0,0 +1,113 @@ +/* + * 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.controllerblueprints.core.dsl + +import org.junit.Test +import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType +import kotlin.test.assertNotNull + +class BluePrintDSLTest { + @Test + fun testServiceTemplate() { + val serviceTemplate = serviceTemplate("sample-bp", "1.0.0", + "brindasanth@onap.com", "sample, blueprints") { + metadata("release", "1806") + import("Definition/data_types.json") + dsl("rest-endpoint", """{ "selector" : "odl-selector"}""") + dsl("db-endpoint", """{ "selector" : "db-selector"}""") + topologyTemplate { + nodeTemplateOperation(nodeTemplateName = "activate", type = "sample-node-type", interfaceName = "RestconfExecutor", + description = "sample activation") { + inputs { + property("json-content", """{ "name" : "cds"}""") + property("array-content", """["controller", "blueprints"]""") + property("int-value", 234) + property("boolean-value", true) + property("string-value", "sample") + property("input-expression", getInput("key-1")) + property("self-property-expression", getProperty("key-1")) + property("self-artifact-expression", getArtifact("key-1")) + property("other-artifact-expression", getNodeTemplateArtifact("node-1", "key-1")) + } + outputs { + property("self-attribute-expression", getAttribute("key-1")) + } + } + // Other way of defining Node Template with artifacts, implementation + nodeTemplate("resolve", "sample-resolve-type", "Resource Resolution") { + operation("ResourceResolutionExecutor", "") { + implementation(180) + inputs { + property("boolean-value", true) + property("string-value", "sample") + } + outputs { + property("resolve-expression", getAttribute("key-1")) + } + } + artifact("sample-template", "artifact-velocity", "Templates/sample-template.vtl") + } + + workflow("resource-resolution", "to resolve resources") { + step("resource-resolution-call", "resolve", "Resource Resolution component invoke") + } + // Alternate way to define workflow + workflow("activate", "to resolve resources") { + // Alternate step definition + step("netconf-activate-call", "activate", "call activation component") { + success("END") + failure("END") + } + inputs { + property("request-content", "json", true) + } + outputs { + property("response-content", "json", true) { + value(getAttribute("key-1")) + defaultValue("""{ "status" : "success"}""".jsonAsJsonType()) + } + } + } + } + } + + assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template") + assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates, "failed to get nodeTypes") + assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates!!["activate"], "failed to get nodeTypes(activate)") + //println(JacksonUtils.getJson(serviceTemplate, true)) + } + + @Test + fun testServiceTemplateWorkflow() { + val serviceTemplate = serviceTemplate("sample-bp", "1.0.0", + "brindasanth@onap.com", "sample, blueprints") { + topologyTemplate { + workflowNodeTemplate("activate", "component-resource-resolution", "") { + operation("ResourceResolutionExecutor", "") { + inputs { + property("string-value", "sample") + } + } + } + } + } + assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template") + assertNotNull(serviceTemplate.topologyTemplate?.workflows?.get("activate"), "failed to get workflow(activate)") + //println(JacksonUtils.getJson(serviceTemplate, true)) + } + +} |