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.asBluePrintsDataTypes
22 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonNode
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
24 import org.onap.ccsdk.cds.controllerblueprints.core.asPropertyDefinitionMap
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.ImportDefinition
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.PolicyType
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
31 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
32 import org.onap.ccsdk.cds.controllerblueprints.core.data.TopologyTemplate
33 import kotlin.reflect.KClass
35 class BluePrintServiceDSLBuilder(
36 private val name: String,
37 private val version: String,
38 private val author: String,
39 private val tags: String
42 private var serviceTemplate = ServiceTemplate()
43 private var topologyTemplate: TopologyTemplate? = null
44 private var metadata: MutableMap<String, String> = hashMapOf()
45 private var dslDefinitions: MutableMap<String, JsonNode>? = null
46 private var imports: MutableList<ImportDefinition> = mutableListOf()
47 private var nodeTypes: MutableMap<String, NodeType>? = null
48 private var artifactTypes: MutableMap<String, ArtifactType>? = null
49 private var dataTypes: MutableMap<String, DataType>? = null
50 private var relationshipTypes: MutableMap<String, RelationshipType>? = null
51 private var policyTypes: MutableMap<String, PolicyType>? = null
53 private fun initMetaData() {
54 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name
55 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version
56 metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author
57 metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags
60 fun metadata(id: String, value: String) {
64 fun import(file: String) {
65 val importDefinition = ImportDefinition().apply {
68 imports.add(importDefinition)
71 fun dsl(id: String, kclass: KClass<*>) {
72 dsl(id, kclass.asPropertyDefinitionMap().asJsonNode())
75 fun dataType(dataType: KClass<*>) {
76 dataType(dataType.asBluePrintsDataTypes())
79 fun dsl(id: String, content: Any) {
80 dsl(id, content.asJsonType())
83 fun dsl(id: String, json: JsonNode) {
84 if (dslDefinitions == null)
85 dslDefinitions = hashMapOf()
86 dslDefinitions!![id] = json
89 fun dataTypes(dataTypes: MutableMap<String, DataType>) {
90 if (this.dataTypes == null)
91 this.dataTypes = hashMapOf()
93 this.dataTypes!!.putAll(dataTypes)
96 fun artifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
97 if (this.artifactTypes == null)
98 this.artifactTypes = hashMapOf()
100 this.artifactTypes!!.putAll(artifactTypes)
103 fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) {
104 if (this.relationshipTypes == null)
105 this.relationshipTypes = hashMapOf()
107 this.relationshipTypes!!.putAll(relationshipTypes)
110 fun policyTypes(policyTypes: MutableMap<String, PolicyType>) {
111 if (this.policyTypes == null)
112 this.policyTypes = hashMapOf()
114 this.policyTypes!!.putAll(policyTypes)
117 fun nodeType(nodeTypes: MutableMap<String, NodeType>) {
118 if (this.nodeTypes == null)
119 this.nodeTypes = hashMapOf()
121 this.nodeTypes!!.putAll(nodeTypes)
124 fun dataType(dataType: DataType) {
125 if (dataTypes == null)
126 dataTypes = hashMapOf()
127 dataTypes!![dataType.id!!] = dataType
130 fun artifactType(artifactType: ArtifactType) {
131 if (artifactTypes == null)
132 artifactTypes = hashMapOf()
133 artifactTypes!![artifactType.id!!] = artifactType
136 fun relationshipType(relationshipType: RelationshipType) {
137 if (relationshipTypes == null)
138 relationshipTypes = hashMapOf()
139 relationshipTypes!![relationshipType.id!!] = relationshipType
142 fun policyType(policyType: PolicyType) {
143 if (policyTypes == null)
144 policyTypes = hashMapOf()
146 policyTypes!![policyType.id!!] = policyType
149 fun nodeType(nodeType: NodeType) {
150 if (nodeTypes == null)
151 nodeTypes = hashMapOf()
152 nodeTypes!![nodeType.id!!] = nodeType
160 block: DataTypeBuilder.() -> Unit
162 if (dataTypes == null)
163 dataTypes = hashMapOf()
164 dataTypes!![id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
172 block: ArtifactTypeBuilder.() -> Unit
174 if (artifactTypes == null)
175 artifactTypes = hashMapOf()
176 artifactTypes!![id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
179 fun relationshipType(
184 block: RelationshipTypeBuilder.() -> Unit
186 if (relationshipTypes == null)
187 relationshipTypes = hashMapOf()
188 relationshipTypes!![id] = RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build()
196 block: PolicyTypeBuilder.() -> Unit
198 if (policyTypes == null)
199 policyTypes = hashMapOf()
200 policyTypes!![id] = PolicyTypeBuilder(id, version, derivedFrom, description).apply(block).build()
208 block: NodeTypeBuilder.() -> Unit
210 if (nodeTypes == null)
211 nodeTypes = hashMapOf()
212 nodeTypes!![id] = NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
215 fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
216 topologyTemplate = TopologyTemplateBuilder().apply(block).build()
219 fun build(): ServiceTemplate {
221 serviceTemplate.metadata = metadata
222 serviceTemplate.imports = imports
223 serviceTemplate.dslDefinitions = dslDefinitions
224 serviceTemplate.nodeTypes = nodeTypes
225 serviceTemplate.artifactTypes = artifactTypes
226 serviceTemplate.dataTypes = dataTypes
227 serviceTemplate.relationshipTypes = relationshipTypes
228 serviceTemplate.policyTypes = policyTypes
229 serviceTemplate.topologyTemplate = topologyTemplate
230 return serviceTemplate