2 * Copyright © 2019 IBM.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.controllerblueprints.core.dsl
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
24 class ServiceTemplateBuilder(private val name: String,
25 private val version: String,
26 private val author: String,
27 private val tags: String) {
28 private var serviceTemplate = ServiceTemplate()
29 private lateinit var topologyTemplate: TopologyTemplate
30 private var metadata: MutableMap<String, String> = hashMapOf()
31 private var dslDefinitions: MutableMap<String, JsonNode>? = null
32 private var imports: MutableList<ImportDefinition>? = null
33 private var nodeTypes: MutableMap<String, NodeType>? = null
34 private var artifactTypes: MutableMap<String, ArtifactType>? = null
35 private var dataTypes: MutableMap<String, DataType>? = null
36 private var relationshipTypes: MutableMap<String, RelationshipType>? = null
38 private fun initMetaData() {
39 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name
40 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version
41 metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author
42 metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags
45 fun metadata(id: String, value: String) {
49 fun dsl(id: String, json: String) {
50 dsl(id, json.asJsonType())
53 fun dsl(id: String, json: JsonNode) {
54 if (dslDefinitions == null)
55 dslDefinitions = hashMapOf()
56 dslDefinitions!![id] = json.asJsonType()
61 fun dataType(id: String, version: String, description: String, block: DataTypeBuilder.() -> Unit) {
62 if (dataTypes == null)
63 dataTypes = hashMapOf()
64 dataTypes!![id] = DataTypeBuilder(id, version, description).apply(block).build()
67 fun artifactType(id: String, version: String, description: String, block: ArtifactTypeBuilder.() -> Unit) {
68 if (artifactTypes == null)
69 artifactTypes = hashMapOf()
70 artifactTypes!![id] = ArtifactTypeBuilder(id, version, description).apply(block).build()
73 fun relationshipType(id: String, version: String, description: String, block: RelationshipTypeBuilder.() -> Unit) {
74 if (relationshipTypes == null)
75 relationshipTypes = hashMapOf()
76 relationshipTypes!![id] = RelationshipTypeBuilder(id, version, description).apply(block).build()
79 fun nodeType(id: String, version: String, description: String, block: NodeTypeBuilder.() -> Unit) {
80 if (nodeTypes == null)
81 nodeTypes = hashMapOf()
82 nodeTypes!![id] = NodeTypeBuilder(id, version, description).apply(block).build()
85 fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
86 topologyTemplate = TopologyTemplateBuilder().apply(block).build()
89 fun build(): ServiceTemplate {
91 serviceTemplate.metadata = metadata
92 serviceTemplate.imports = imports
93 serviceTemplate.dslDefinitions = dslDefinitions
94 serviceTemplate.nodeTypes = nodeTypes
95 serviceTemplate.artifactTypes = artifactTypes
96 serviceTemplate.dataTypes = dataTypes
97 serviceTemplate.relationshipTypes = relationshipTypes
98 serviceTemplate.topologyTemplate = topologyTemplate
99 return serviceTemplate