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.*
21 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
22 import kotlin.reflect.KClass
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 var topologyTemplate: TopologyTemplate? = null
30 private var metadata: MutableMap<String, String> = hashMapOf()
31 private var dslDefinitions: MutableMap<String, JsonNode>? = null
32 private var imports: MutableList<ImportDefinition> = mutableListOf()
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
37 private var policyTypes: MutableMap<String, PolicyType>? = null
39 private fun initMetaData() {
40 metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name
41 metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version
42 metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author
43 metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags
46 fun metadata(id: String, value: String) {
50 fun import(file: String) {
51 val importDefinition = ImportDefinition().apply {
54 imports.add(importDefinition)
57 fun dsl(id: String, kclass: KClass<*>) {
58 dsl(id, kclass.asPropertyDefinitionMap().asJsonNode())
61 fun dataType(dataType: KClass<*>) {
62 dataType(dataType.asBluePrintsDataTypes())
65 fun dsl(id: String, content: Any) {
66 dsl(id, content.asJsonType())
69 fun dsl(id: String, json: JsonNode) {
70 if (dslDefinitions == null)
71 dslDefinitions = hashMapOf()
72 dslDefinitions!![id] = json
75 fun dataTypes(dataTypes: MutableMap<String, DataType>) {
76 if (this.dataTypes == null)
77 this.dataTypes = hashMapOf()
79 this.dataTypes!!.putAll(dataTypes)
82 fun artifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
83 if (this.artifactTypes == null)
84 this.artifactTypes = hashMapOf()
86 this.artifactTypes!!.putAll(artifactTypes)
89 fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) {
90 if (this.relationshipTypes == null)
91 this.relationshipTypes = hashMapOf()
93 this.relationshipTypes!!.putAll(relationshipTypes)
96 fun policyTypes(policyTypes: MutableMap<String, PolicyType>) {
97 if (this.policyTypes == null)
98 this.policyTypes = hashMapOf()
100 this.policyTypes!!.putAll(policyTypes)
103 fun nodeType(nodeTypes: MutableMap<String, NodeType>) {
104 if (this.nodeTypes == null)
105 this.nodeTypes = hashMapOf()
107 this.nodeTypes!!.putAll(nodeTypes)
110 fun dataType(dataType: DataType) {
111 if (dataTypes == null)
112 dataTypes = hashMapOf()
113 dataTypes!![dataType.id!!] = dataType
116 fun artifactType(artifactType: ArtifactType) {
117 if (artifactTypes == null)
118 artifactTypes = hashMapOf()
119 artifactTypes!![artifactType.id!!] = artifactType
122 fun relationshipType(relationshipType: RelationshipType) {
123 if (relationshipTypes == null)
124 relationshipTypes = hashMapOf()
125 relationshipTypes!![relationshipType.id!!] = relationshipType
128 fun policyType(policyType: PolicyType) {
129 if (policyTypes == null)
130 policyTypes = hashMapOf()
132 policyTypes!![policyType.id!!] = policyType
135 fun nodeType(nodeType: NodeType) {
136 if (nodeTypes == null)
137 nodeTypes = hashMapOf()
138 nodeTypes!![nodeType.id!!] = nodeType
141 fun dataType(id: String, version: String, derivedFrom: String, description: String,
142 block: DataTypeBuilder.() -> Unit) {
143 if (dataTypes == null)
144 dataTypes = hashMapOf()
145 dataTypes!![id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
148 fun artifactType(id: String, version: String, derivedFrom: String, description: String,
149 block: ArtifactTypeBuilder.() -> Unit) {
150 if (artifactTypes == null)
151 artifactTypes = hashMapOf()
152 artifactTypes!![id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
155 fun relationshipType(id: String, version: String, derivedFrom: String, description: String,
156 block: RelationshipTypeBuilder.() -> Unit) {
157 if (relationshipTypes == null)
158 relationshipTypes = hashMapOf()
159 relationshipTypes!![id] = RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build()
162 fun policyType(id: String, version: String, derivedFrom: String, description: String,
163 block: PolicyTypeBuilder.() -> Unit) {
164 if (policyTypes == null)
165 policyTypes = hashMapOf()
166 policyTypes!![id] = PolicyTypeBuilder(id, version, derivedFrom, description).apply(block).build()
169 fun nodeType(id: String, version: String, derivedFrom: String, description: String,
170 block: NodeTypeBuilder.() -> Unit) {
171 if (nodeTypes == null)
172 nodeTypes = hashMapOf()
173 nodeTypes!![id] = NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
176 fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
177 topologyTemplate = TopologyTemplateBuilder().apply(block).build()
180 fun build(): ServiceTemplate {
182 serviceTemplate.metadata = metadata
183 serviceTemplate.imports = imports
184 serviceTemplate.dslDefinitions = dslDefinitions
185 serviceTemplate.nodeTypes = nodeTypes
186 serviceTemplate.artifactTypes = artifactTypes
187 serviceTemplate.dataTypes = dataTypes
188 serviceTemplate.relationshipTypes = relationshipTypes
189 serviceTemplate.policyTypes = policyTypes
190 serviceTemplate.topologyTemplate = topologyTemplate
191 return serviceTemplate