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 ServiceTemplateBuilder(
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 var nodeTypes: MutableMap<String, NodeType>? = null
48 var artifactTypes: MutableMap<String, ArtifactType>? = null
49 var dataTypes: MutableMap<String, DataType>? = null
50 var relationshipTypes: MutableMap<String, RelationshipType>? = null
51 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) dslDefinitions = hashMapOf()
85 dslDefinitions!![id] = json
88 fun dataTypes(dataTypes: MutableMap<String, DataType>) {
89 if (this.dataTypes == null) this.dataTypes = hashMapOf()
91 this.dataTypes!!.putAll(dataTypes)
94 fun artifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
95 if (this.artifactTypes == null) this.artifactTypes = hashMapOf()
96 this.artifactTypes!!.putAll(artifactTypes)
99 fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) {
100 if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
101 this.relationshipTypes!!.putAll(relationshipTypes)
104 fun policyTypes(policyTypes: MutableMap<String, PolicyType>) {
105 if (this.policyTypes == null) this.policyTypes = hashMapOf()
106 this.policyTypes!!.putAll(policyTypes)
109 fun nodeType(nodeTypes: MutableMap<String, NodeType>) {
110 if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
111 this.nodeTypes!!.putAll(nodeTypes)
114 fun dataType(dataType: DataType) {
115 if (dataTypes == null) dataTypes = hashMapOf()
116 dataTypes!![dataType.id!!] = dataType
119 fun artifactType(artifactType: ArtifactType) {
120 if (artifactTypes == null) artifactTypes = hashMapOf()
121 artifactTypes!![artifactType.id!!] = artifactType
124 fun relationshipType(relationshipType: RelationshipType) {
125 if (relationshipTypes == null) relationshipTypes = hashMapOf()
126 relationshipTypes!![relationshipType.id!!] = relationshipType
129 fun relationshipTypes(relationshipTypes: List<RelationshipType>) {
130 if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
131 relationshipTypes.forEach { relationshipType ->
132 this.relationshipTypes!![relationshipType.id!!] = relationshipType
136 fun policyType(policyType: PolicyType) {
137 if (policyTypes == null) policyTypes = hashMapOf()
138 policyTypes!![policyType.id!!] = policyType
141 fun nodeType(nodeType: NodeType) {
142 if (nodeTypes == null) nodeTypes = hashMapOf()
143 nodeTypes!![nodeType.id!!] = nodeType
146 fun nodeTypes(nodeTypes: List<NodeType>) {
147 if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
148 nodeTypes.forEach { nodeType ->
149 this.nodeTypes!![nodeType.id!!] = nodeType
158 block: DataTypeBuilder.() -> Unit
160 if (dataTypes == null) dataTypes = hashMapOf()
161 dataTypes!![id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
169 block: ArtifactTypeBuilder.() -> Unit
171 if (artifactTypes == null) artifactTypes = hashMapOf()
172 artifactTypes!![id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
175 fun relationshipType(
180 block: RelationshipTypeBuilder.() -> Unit
182 if (relationshipTypes == null) relationshipTypes = hashMapOf()
183 relationshipTypes!![id] = RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build()
191 block: PolicyTypeBuilder.() -> Unit
193 if (policyTypes == null) policyTypes = hashMapOf()
194 policyTypes!![id] = PolicyTypeBuilder(id, version, derivedFrom, description).apply(block).build()
202 block: NodeTypeBuilder.() -> Unit
204 if (nodeTypes == null) nodeTypes = hashMapOf()
205 nodeTypes!![id] = NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
208 fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
209 topologyTemplate = TopologyTemplateBuilder().apply(block).build()
212 fun build(): ServiceTemplate {
214 serviceTemplate.metadata = metadata
215 serviceTemplate.imports = imports
216 serviceTemplate.dslDefinitions = dslDefinitions
217 serviceTemplate.nodeTypes = nodeTypes
218 serviceTemplate.artifactTypes = artifactTypes
219 serviceTemplate.dataTypes = dataTypes
220 serviceTemplate.relationshipTypes = relationshipTypes
221 serviceTemplate.policyTypes = policyTypes
222 serviceTemplate.topologyTemplate = topologyTemplate
223 return serviceTemplate