diff options
author | Brinda Santh <brindasanth@in.ibm.com> | 2019-06-25 13:18:35 -0400 |
---|---|---|
committer | Brinda Santh <brindasanth@in.ibm.com> | 2019-06-25 14:34:30 -0400 |
commit | 1aacdb023fc2f05cc30e7308ab02a2fd874b53b1 (patch) | |
tree | b0499a0b78da205386d33bb8683af0c5f9d995f7 /ms/controllerblueprints/modules | |
parent | 5c04d574527c41c241361d44bcd58983065ac0c3 (diff) |
DSL to service template generator
Change-Id: I179bb3bf83d6c1688a13dce965a3d4388f011fcd
Issue-ID: CCSDK-1421
Signed-off-by: Brinda Santh <brindasanth@in.ibm.com>
Diffstat (limited to 'ms/controllerblueprints/modules')
7 files changed, 482 insertions, 48 deletions
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/BluePrintConstants.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/BluePrintConstants.kt index 786bcd3a5..67a215ef5 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/BluePrintConstants.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/BluePrintConstants.kt @@ -180,8 +180,18 @@ object BluePrintConstants { const val PROPERTY_CURRENT_IMPLEMENTATION = "current-implementation" const val PROPERTY_EXECUTION_REQUEST = "execution-request" + const val DEFAULT_VERSION_NUMBER = "1.0.0" const val DEFAULT_STEP_OPERATION = "process" + const val DEFAULT_STEP_INTERFACE = "ComponentInterface" const val ARTIFACT_VELOCITY_TYPE_NAME = "artifact-template-velocity" const val ARTIFACT_JINJA_TYPE_NAME = "artifact-template-jinja" + + const val MODEL_TYPE_ARTIFACT_TEMPLATE_VELOCITY = "artifact-template-velocity" + const val MODEL_TYPE_ARTIFACT_TEMPLATE_JINJA = "artifact-template-jinja" + const val MODEL_TYPE_ARTIFACT_MAPPING_RESOURCE = "artifact-mapping-resource" + const val MODEL_TYPE_ARTIFACT_SCRIPT_JYTHON = "artifact-script-jython" + const val MODEL_TYPE_ARTIFACT_SCRIPT_KOTLIN = "artifact-script-kotlin" + const val MODEL_TYPE_ARTIFACT_DIRECTED_GRAPH = "artifact-directed-graph" + const val MODEL_TYPE_ARTIFACT_COMPONENT_JAR = "artifact-component-jar" }
\ No newline at end of file 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 a508c8fbc..ab54f566d 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 @@ -19,6 +19,7 @@ package org.onap.ccsdk.cds.controllerblueprints.core import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.node.* +import org.apache.commons.lang3.ObjectUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.slf4j.helpers.MessageFormatter import kotlin.reflect.KClass @@ -29,6 +30,10 @@ import kotlin.reflect.KClass * @author Brinda Santh */ +fun <T : Any> T.bpClone(): T { + return ObjectUtils.clone(this) +} + fun String.isJson(): Boolean { return ((this.startsWith("{") && this.endsWith("}")) || (this.startsWith("[") && this.endsWith("]"))) @@ -100,10 +105,10 @@ fun format(message: String, vararg args: Any?): String { } fun <T : Any> Map<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? { - if (containsKey(key)) { - return get(key) as? T + return if (containsKey(key)) { + get(key) as? T } else { - return null + null } } 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 index 95303ea23..c88408f19 100644 --- 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 @@ -17,6 +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.BluePrintTypes import org.onap.ccsdk.cds.controllerblueprints.core.data.* import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType @@ -56,6 +58,11 @@ fun artifactType(id: String, version: String, derivedFrom: String, description: return ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build() } +fun relationshipType(id: String, version: String, derivedFrom: String, description: String, + block: RelationshipTypeBuilder.() -> Unit): RelationshipType { + return RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build() +} + // Input Function fun getInput(inputKey: String): JsonNode { @@ -104,4 +111,122 @@ fun getArtifact(artifactId: String): JsonNode { fun getNodeTemplateArtifact(nodeTemplateName: String, artifactId: String): JsonNode { return """{"get_artifact": ["$nodeTemplateName", "$artifactId"]}""".jsonAsJsonType() -}
\ No newline at end of file +} + + +/** Blueprint Type Extensions */ + +fun BluePrintTypes.nodeTypeComponent(): NodeType { + return nodeType(id = BluePrintConstants.MODEL_TYPE_NODE_COMPONENT, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT, + description = "This is default Component Node") { + } +} + +fun BluePrintTypes.nodeTypeWorkflow(): NodeType { + return nodeType(id = BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT, + description = "This is default Workflow Node") { + } +} + +fun BluePrintTypes.nodeTypeVnf(): NodeType { + return nodeType(id = BluePrintConstants.MODEL_TYPE_NODE_VNF, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT, + description = "This is default VNF Node") { + } +} + +fun BluePrintTypes.nodeTypeResourceSource(): NodeType { + return nodeType(id = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT, + description = "This is default Resource Source Node") { + } +} + +/** Artifacts */ + +fun BluePrintTypes.artifactTypeTemplateVelocity(): ArtifactType { + return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_TEMPLATE_VELOCITY, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION, + description = "Velocity Artifact") { + fileExt("vtl") + } +} + +fun BluePrintTypes.artifactTypeTempleJinja(): ArtifactType { + return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_TEMPLATE_JINJA, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION, + description = "Jinja Artifact") { + fileExt("jinja") + } +} + +fun BluePrintTypes.artifactTypeMappingResource(): ArtifactType { + return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_MAPPING_RESOURCE, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION, + description = "Mapping Resource Artifact") { + fileExt("json") + } +} + +fun BluePrintTypes.artifactTypeScriptJython(): ArtifactType { + return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_SCRIPT_JYTHON, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION, + description = "Jython Script Artifact") { + fileExt("py") + } +} + +fun BluePrintTypes.artifactTypeScriptKotlin(): ArtifactType { + return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_SCRIPT_KOTLIN, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION, + description = "Kotlin Script Artifact") { + fileExt("kts") + } +} + +fun BluePrintTypes.artifactTypeDirectedGraph(): ArtifactType { + return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_DIRECTED_GRAPH, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION, + description = "Directed Graph Artifact") { + fileExt("xml", "json") + } +} + +fun BluePrintTypes.artifactTypeComponentJar(): ArtifactType { + return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_COMPONENT_JAR, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION, + description = "Component Artifact") { + fileExt("jar") + } +} + +/** Relationship Types */ + +fun BluePrintTypes.relationshipTypeConnectsTo(): RelationshipType { + return relationshipType(id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_ROOT, + description = "Relationship connects to") { + } +} + +fun BluePrintTypes.relationshipTypeDependsOn(): RelationshipType { + return relationshipType(id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_DEPENDS_ON, + version = BluePrintConstants.DEFAULT_VERSION_NUMBER, + derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_ROOT, + description = "Relationship depends on") { + } +} diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLBuilder.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLBuilder.kt index 8cfa90b15..d46d478a8 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLBuilder.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLBuilder.kt @@ -32,9 +32,10 @@ class DSLBluePrintBuilder(private val name: String, private var dslBluePrint = DSLBluePrint() private var metadata: MutableMap<String, String> = hashMapOf() var properties: MutableMap<String, JsonNode>? = null - var dataType: MutableMap<String, DataType> = hashMapOf() - var artifacts: MutableMap<String, ArtifactDefinition> = hashMapOf() + var dataTypes: MutableMap<String, DataType> = hashMapOf() + var artifactTypes: MutableMap<String, ArtifactType> = hashMapOf() var components: MutableMap<String, DSLComponent> = hashMapOf() + private var registryComponents: MutableMap<String, DSLRegistryComponent> = hashMapOf() var workflows: MutableMap<String, DSLWorkflow> = hashMapOf() private fun initMetaData() { @@ -54,15 +55,35 @@ class DSLBluePrintBuilder(private val name: String, properties!![id] = expression.asJsonType() } + fun dataType(dataType: DataType) { + dataTypes[dataType.id!!] = dataType + } + fun dataType(id: String, version: String, derivedFrom: String, description: String, block: DataTypeBuilder.() -> Unit) { - dataType[id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build() + dataTypes[id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build() } - fun component(id: String, type: String, version: String, description: String, block: DSLComponentBuilder.() -> Unit) { + fun artifactType(artifactType: ArtifactType) { + artifactTypes[artifactType.id!!] = artifactType + } + + fun artifactType(id: String, version: String, derivedFrom: String, description: String, + block: ArtifactTypeBuilder.() -> Unit) { + artifactTypes[id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build() + } + + fun component(id: String, type: String, version: String, description: String, + block: DSLComponentBuilder.() -> Unit) { components[id] = DSLComponentBuilder(id, type, version, description).apply(block).build() } + fun registryComponent(id: String, type: String, version: String, interfaceName: String, description: String, + block: DSLRegistryComponentBuilder.() -> Unit) { + registryComponents[id] = DSLRegistryComponentBuilder(id, type, version, interfaceName, description) + .apply(block).build() + } + fun workflow(id: String, description: String, block: DSLWorkflowBuilder.() -> Unit) { workflows[id] = DSLWorkflowBuilder(id, description).apply(block).build() } @@ -71,8 +92,10 @@ class DSLBluePrintBuilder(private val name: String, initMetaData() dslBluePrint.metadata = metadata dslBluePrint.properties = properties - dslBluePrint.dataType = dataType + dslBluePrint.dataTypes = dataTypes + dslBluePrint.artifactTypes = artifactTypes dslBluePrint.components = components + dslBluePrint.registryComponents = registryComponents dslBluePrint.workflows = workflows return dslBluePrint } @@ -84,18 +107,11 @@ class DSLComponentBuilder(private val id: String, private val type: String, var properties: MutableMap<String, PropertyDefinition>? = null var attributes: MutableMap<String, AttributeDefinition>? = null - // For already registered components - private var assignProperties: MutableMap<String, JsonNode>? = null - var artifacts: MutableMap<String, ArtifactDefinition>? = null var implementation: Implementation? = null var inputs: MutableMap<String, PropertyDefinition>? = null var outputs: MutableMap<String, PropertyDefinition>? = null - // For already registered components - private var assignInputs: MutableMap<String, JsonNode>? = null - private var assignOutputs: MutableMap<String, JsonNode>? = null - fun attribute(id: String, type: String, required: Boolean, expression: Any, description: String? = "") { if (attributes == null) attributes = hashMapOf() @@ -128,12 +144,6 @@ class DSLComponentBuilder(private val id: String, private val type: String, properties!![id] = property } - fun assignProperty(id: String, expression: Any) { - if (assignProperties == null) - assignProperties = hashMapOf() - assignProperties!![id] = expression.asJsonType() - } - fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) { implementation = Implementation().apply { this.operationHost = operationHost!! @@ -141,13 +151,13 @@ class DSLComponentBuilder(private val id: String, private val type: String, } } - fun artifacts(id: String, type: String, file: String) { + fun artifact(id: String, type: String, file: String) { if (artifacts == null) artifacts = hashMapOf() artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build() } - fun artifacts(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) { + 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() @@ -170,12 +180,6 @@ class DSLComponentBuilder(private val id: String, private val type: String, inputs!![id] = property } - fun assignInput(id: String, expression: Any) { - if (assignInputs == null) - assignInputs = hashMapOf() - assignInputs!![id] = expression.asJsonType() - } - fun output(id: String, type: String, required: Boolean, expression: Any, description: String? = "") { if (outputs == null) outputs = hashMapOf() @@ -192,12 +196,6 @@ class DSLComponentBuilder(private val id: String, private val type: String, outputs!![id] = property } - fun assignOutput(id: String, expression: Any) { - if (assignOutputs == null) - assignOutputs = hashMapOf() - assignOutputs!![id] = expression.asJsonType() - } - fun build(): DSLComponent { dslComponent.id = id dslComponent.type = type @@ -205,19 +203,79 @@ class DSLComponentBuilder(private val id: String, private val type: String, dslComponent.description = description dslComponent.attributes = attributes dslComponent.properties = properties - dslComponent.assignProperties = assignProperties dslComponent.implementation = implementation dslComponent.artifacts = artifacts dslComponent.inputs = inputs dslComponent.outputs = outputs - dslComponent.assignInputs = assignInputs - dslComponent.assignOutputs = assignOutputs - dslComponent.outputs = outputs return dslComponent } } +class DSLRegistryComponentBuilder(private val id: String, private val type: String, + private val version: String, + private val interfaceName: String, + private val description: String) { + private val dslComponent = DSLRegistryComponent() + var properties: MutableMap<String, JsonNode>? = null + + var artifacts: MutableMap<String, ArtifactDefinition>? = null + var implementation: Implementation? = null + var inputs: MutableMap<String, JsonNode>? = null + var outputs: MutableMap<String, JsonNode>? = null + + fun property(id: String, expression: Any) { + if (properties == null) + properties = hashMapOf() + properties!![id] = expression.asJsonType() + } + + fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) { + implementation = Implementation().apply { + this.operationHost = operationHost!! + this.timeout = timeout + } + } + + 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 input(id: String, expression: Any) { + if (inputs == null) + inputs = hashMapOf() + inputs!![id] = expression.asJsonType() + } + + fun output(id: String, expression: Any) { + if (outputs == null) + outputs = hashMapOf() + outputs!![id] = expression.asJsonType() + } + + fun build(): DSLRegistryComponent { + dslComponent.id = id + dslComponent.type = type + dslComponent.version = version + dslComponent.interfaceName = interfaceName + dslComponent.description = description + dslComponent.properties = properties + dslComponent.implementation = implementation + dslComponent.artifacts = artifacts + dslComponent.inputs = inputs + dslComponent.outputs = outputs + return dslComponent + } +} + class DSLWorkflowBuilder(private val actionName: String, private val description: String) { private val dslWorkflow = DSLWorkflow() private var steps: MutableMap<String, Step>? = null diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLData.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLData.kt index 12428a90c..61b52a6f9 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLData.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintDSLData.kt @@ -28,8 +28,10 @@ import org.onap.ccsdk.cds.controllerblueprints.core.data.* class DSLBluePrint { var metadata: MutableMap<String, String> = hashMapOf() var properties: MutableMap<String, JsonNode>? = null - var dataType: MutableMap<String, DataType> = hashMapOf() + var dataTypes: MutableMap<String, DataType> = hashMapOf() + var artifactTypes: MutableMap<String, ArtifactType> = hashMapOf() var components: MutableMap<String, DSLComponent> = hashMapOf() + var registryComponents: MutableMap<String, DSLRegistryComponent> = hashMapOf() var workflows: MutableMap<String, DSLWorkflow> = hashMapOf() } @@ -52,10 +54,21 @@ class DSLComponent { var implementation: Implementation? = null var attributes: MutableMap<String, AttributeDefinition>? = null var properties: MutableMap<String, PropertyDefinition>? = null - var assignProperties: MutableMap<String, JsonNode>? = null var artifacts: MutableMap<String, ArtifactDefinition>? = null var inputs: MutableMap<String, PropertyDefinition>? = null var outputs: MutableMap<String, PropertyDefinition>? = null - var assignInputs: MutableMap<String, JsonNode>? = null - var assignOutputs: MutableMap<String, JsonNode>? = null +} + + +class DSLRegistryComponent { + lateinit var id: String + lateinit var type: String + lateinit var version: String + lateinit var interfaceName: String + lateinit var description: String + var implementation: Implementation? = null + var properties: MutableMap<String, JsonNode>? = null + var artifacts: MutableMap<String, ArtifactDefinition>? = null + var inputs: MutableMap<String, JsonNode>? = null + var outputs: MutableMap<String, JsonNode>? = null }
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintServiceTemplateGenerator.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintServiceTemplateGenerator.kt new file mode 100644 index 000000000..a4a5e05ec --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/dsl/BluePrintServiceTemplateGenerator.kt @@ -0,0 +1,200 @@ +/* + * 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 com.fasterxml.jackson.databind.node.NullNode +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants +import org.onap.ccsdk.cds.controllerblueprints.core.bpClone +import org.onap.ccsdk.cds.controllerblueprints.core.data.* + +/** + * Generate Service Template for the simplified DSL. + * @author Brinda Santh + */ +class BluePrintServiceTemplateGenerator(private val dslBluePrint: DSLBluePrint) { + + private var serviceTemplate = ServiceTemplate() + + private val nodeTypes: MutableMap<String, NodeType> = hashMapOf() + private val artifactTypes: MutableMap<String, ArtifactType> = hashMapOf() + private val dataTypes: MutableMap<String, DataType> = hashMapOf() + + fun serviceTemplate(): ServiceTemplate { + serviceTemplate.metadata = dslBluePrint.metadata + serviceTemplate.dslDefinitions = dslBluePrint.properties + + dataTypes.putAll(dslBluePrint.dataTypes) + artifactTypes.putAll(dslBluePrint.artifactTypes) + + serviceTemplate.dataTypes = dataTypes + serviceTemplate.artifactTypes = artifactTypes + serviceTemplate.nodeTypes = nodeTypes + + serviceTemplate.topologyTemplate = populateTopologyTemplate() + + return serviceTemplate + } + + private fun populateTopologyTemplate(): TopologyTemplate { + val topologyTemplate = TopologyTemplate() + topologyTemplate.nodeTemplates = populateNodeTemplates() + topologyTemplate.workflows = populateWorkflow() + return topologyTemplate + } + + private fun populateNodeTemplates(): MutableMap<String, NodeTemplate> { + + val nodeTemplates: MutableMap<String, NodeTemplate> = hashMapOf() + + // For New or Dynamic Components + val components = dslBluePrint.components + components.forEach { (dslCompName, dslComp) -> + val nodeTemplate = NodeTemplate() + nodeTemplate.type = dslComp.type + nodeTemplate.properties = propertyAssignments(dslComp.properties) + nodeTemplate.artifacts = dslComp.artifacts + nodeTemplate.interfaces = populateInterfaceAssignments(dslComp) + nodeTemplates[dslCompName] = nodeTemplate + + /** Populate Type **/ + nodeTypes[dslComp.type] = populateNodeType(dslComp) + } + + // For Registry Components + val registryComponents = dslBluePrint.registryComponents + registryComponents.forEach { (dslCompName, dslComp) -> + val nodeTemplate = NodeTemplate() + nodeTemplate.type = dslComp.type + nodeTemplate.properties = dslComp.properties + nodeTemplate.artifacts = dslComp.artifacts + nodeTemplate.interfaces = populateInterfaceAssignments(dslComp) + nodeTemplates[dslCompName] = nodeTemplate + } + return nodeTemplates + } + + + private fun populateWorkflow(): MutableMap<String, Workflow>? { + var workflows: MutableMap<String, Workflow>? = null + if (dslBluePrint.workflows.isNotEmpty()) { + workflows = hashMapOf() + + dslBluePrint.workflows.forEach { (dslWorkflowName, dslWorkflow) -> + val workflow = Workflow() + workflow.description = dslWorkflow.description + workflow.steps = dslWorkflow.steps + workflow.inputs = dslWorkflow.inputs + workflow.outputs = dslWorkflow.outputs + workflows[dslWorkflowName] = workflow + } + } + return workflows + } + + private fun populateNodeType(dslComponent: DSLComponent): NodeType { + val nodeType = NodeType() + nodeType.derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT + nodeType.version = dslComponent.version + nodeType.description = dslComponent.description + nodeType.interfaces = populateInterfaceDefinitions(dslComponent, nodeType) + return nodeType + } + + private fun populateInterfaceDefinitions(dslComponent: DSLComponent, nodeType: NodeType): MutableMap<String, InterfaceDefinition> { + + // Populate Node Type Attribute + nodeType.attributes = attributeDefinitions(dslComponent.attributes) + + val operationDefinition = OperationDefinition() + operationDefinition.inputs = propertyDefinitions(dslComponent.inputs) + operationDefinition.outputs = propertyDefinitions(dslComponent.outputs) + + val operations: MutableMap<String, OperationDefinition> = hashMapOf() + operations[BluePrintConstants.DEFAULT_STEP_OPERATION] = operationDefinition + + val interfaceDefinition = InterfaceDefinition() + interfaceDefinition.operations = operations + + val interfaces: MutableMap<String, InterfaceDefinition> = hashMapOf() + interfaces[BluePrintConstants.DEFAULT_STEP_INTERFACE] = interfaceDefinition + return interfaces + } + + private fun populateInterfaceAssignments(dslComponent: DSLRegistryComponent): MutableMap<String, InterfaceAssignment> { + val operationAssignment = OperationAssignment() + operationAssignment.implementation = dslComponent.implementation + operationAssignment.inputs = dslComponent.inputs + operationAssignment.outputs = dslComponent.outputs + + val operations: MutableMap<String, OperationAssignment> = hashMapOf() + operations[BluePrintConstants.DEFAULT_STEP_OPERATION] = operationAssignment + + val interfaceAssignment = InterfaceAssignment() + interfaceAssignment.operations = operations + + val interfaces: MutableMap<String, InterfaceAssignment> = hashMapOf() + interfaces[dslComponent.interfaceName] = interfaceAssignment + return interfaces + } + + private fun populateInterfaceAssignments(dslComponent: DSLComponent): MutableMap<String, InterfaceAssignment> { + val operationAssignment = OperationAssignment() + operationAssignment.implementation = dslComponent.implementation + operationAssignment.inputs = propertyAssignments(dslComponent.inputs) + operationAssignment.outputs = propertyAssignments(dslComponent.outputs) + + val operations: MutableMap<String, OperationAssignment> = hashMapOf() + operations[BluePrintConstants.DEFAULT_STEP_OPERATION] = operationAssignment + + val interfaceAssignment = InterfaceAssignment() + interfaceAssignment.operations = operations + + val interfaces: MutableMap<String, InterfaceAssignment> = hashMapOf() + interfaces[BluePrintConstants.DEFAULT_STEP_INTERFACE] = interfaceAssignment + return interfaces + } + + private fun propertyDefinitions(propertyDefinitions: Map<String, PropertyDefinition>?): MutableMap<String, PropertyDefinition>? { + val definitions: MutableMap<String, PropertyDefinition>? = propertyDefinitions?.bpClone()?.toMutableMap() + + definitions?.forEach { (_, prop) -> + prop.value = null + } + return definitions + } + + private fun attributeDefinitions(attributeDefinitions: Map<String, AttributeDefinition>?): MutableMap<String, AttributeDefinition>? { + val definitions: MutableMap<String, AttributeDefinition>? = attributeDefinitions?.bpClone()?.toMutableMap() + + definitions?.forEach { (_, prop) -> + prop.value = null + } + return definitions + } + + private fun propertyAssignments(propertyDefinitions: Map<String, PropertyDefinition>?): MutableMap<String, JsonNode>? { + var assignments: MutableMap<String, JsonNode>? = null + if (propertyDefinitions != null) { + assignments = hashMapOf() + propertyDefinitions.forEach { (propertyName, property) -> + assignments[propertyName] = property.value ?: NullNode.instance + } + } + return assignments + } +}
\ No newline at end of file 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 e7f24d629..020edc78e 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 @@ -17,6 +17,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.jsonAsJsonType import kotlin.test.assertNotNull @@ -28,9 +29,11 @@ class BluePrintDSLTest { val blueprint = blueprint("sample-bp", "1.0.0", "brindasanth@onap.com", "sample, blueprints") { + artifactType(BluePrintTypes.artifactTypeTemplateVelocity()) + // For New Component Definition - component("resource-resolution", "component-resource-resolution", "1.0.0", - "Resource Resolution Call") { + component("resource-resolution", "component-script-executor", "1.0.0", + "Resource Resolution component.") { implementation(180) // Attributes ( Properties which will be set during execution) attribute("template1-data", "string", true, "") @@ -44,10 +47,25 @@ class BluePrintDSLTest { // Outputs output("self-attribute-expression", "json", true, getAttribute("template1-data")) // Artifacts - artifacts("template1", "artifact-velocity", "Templates/template1.vtl") + artifact("template1", "artifact-template-velocity", "Templates/template1.vtl") } - workflow("resource-resolution-process", "") { + // Already definitions Registered Components + registryComponent("activate-restconf", "component-resource-resolution", "1.0.0", + "RestconfExecutor", "Resource Resolution component.") { + implementation(180) + // Properties + property("string-value1", "data") + // Inputs + input("json-content", """{ "name" : "cds"}""") + // Outputs + output("self-attribute-expression", getAttribute("template1-data")) + // Artifacts + artifact("template2", "artifact-template-velocity", "Templates/template1.vtl") + + } + + workflow("resource-resolution-process", "Resource Resolution wf") { input("json-content", "json", true, "") input("key-1", "string", true, "") output("status", "string", true, "success") @@ -57,6 +75,11 @@ class BluePrintDSLTest { assertNotNull(blueprint.components, "failed to get components") assertNotNull(blueprint.workflows, "failed to get workflows") //println(blueprint.asJsonString(true)) + + val serviceTemplateGenerator = BluePrintServiceTemplateGenerator(blueprint) + val serviceTemplate = serviceTemplateGenerator.serviceTemplate() + assertNotNull(serviceTemplate.topologyTemplate, "failed to get service topology template") + //println(serviceTemplate.asJsonString(true)) } @Test |