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> = 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, content: Any) {
58 dsl(id, content.asJsonType())
61 fun dsl(id: String, json: JsonNode) {
62 if (dslDefinitions == null)
63 dslDefinitions = hashMapOf()
64 dslDefinitions!![id] = json
67 fun dataTypes(dataTypes: MutableMap<String, DataType>) {
68 if (this.dataTypes == null)
69 this.dataTypes = hashMapOf()
71 this.dataTypes!!.putAll(dataTypes)
74 fun artifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
75 if (this.artifactTypes == null)
76 this.artifactTypes = hashMapOf()
78 this.artifactTypes!!.putAll(artifactTypes)
81 fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) {
82 if (this.relationshipTypes == null)
83 this.relationshipTypes = hashMapOf()
85 this.relationshipTypes!!.putAll(relationshipTypes)
88 fun policyTypes(policyTypes: MutableMap<String, PolicyType>) {
89 if (this.policyTypes == null)
90 this.policyTypes = hashMapOf()
92 this.policyTypes!!.putAll(policyTypes)
95 fun nodeType(nodeTypes: MutableMap<String, NodeType>) {
96 if (this.nodeTypes == null)
97 this.nodeTypes = hashMapOf()
99 this.nodeTypes!!.putAll(nodeTypes)
102 fun dataType(dataType: DataType) {
103 if (dataTypes == null)
104 dataTypes = hashMapOf()
105 dataTypes!![dataType.id!!] = dataType
108 fun artifactType(artifactType: ArtifactType) {
109 if (artifactTypes == null)
110 artifactTypes = hashMapOf()
111 artifactTypes!![artifactType.id!!] = artifactType
114 fun relationshipType(relationshipType: RelationshipType) {
115 if (relationshipTypes == null)
116 relationshipTypes = hashMapOf()
117 relationshipTypes!![relationshipType.id!!] = relationshipType
120 fun policyType(policyType: PolicyType) {
121 if (policyTypes == null)
122 policyTypes = hashMapOf()
124 policyTypes!![policyType.id!!] = policyType
127 fun nodeType(nodeType: NodeType) {
128 if (nodeTypes == null)
129 nodeTypes = hashMapOf()
130 nodeTypes!![nodeType.id!!] = nodeType
133 fun dataType(id: String, version: String, derivedFrom: String, description: String,
134 block: DataTypeBuilder.() -> Unit) {
135 if (dataTypes == null)
136 dataTypes = hashMapOf()
137 dataTypes!![id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
140 fun artifactType(id: String, version: String, derivedFrom: String, description: String,
141 block: ArtifactTypeBuilder.() -> Unit) {
142 if (artifactTypes == null)
143 artifactTypes = hashMapOf()
144 artifactTypes!![id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
147 fun relationshipType(id: String, version: String, derivedFrom: String, description: String,
148 block: RelationshipTypeBuilder.() -> Unit) {
149 if (relationshipTypes == null)
150 relationshipTypes = hashMapOf()
151 relationshipTypes!![id] = RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build()
154 fun policyType(id: String, version: String, derivedFrom: String, description: String,
155 block: PolicyTypeBuilder.() -> Unit) {
156 if (policyTypes == null)
157 policyTypes = hashMapOf()
158 policyTypes!![id] = PolicyTypeBuilder(id, version, derivedFrom, description).apply(block).build()
161 fun nodeType(id: String, version: String, derivedFrom: String, description: String,
162 block: NodeTypeBuilder.() -> Unit) {
163 if (nodeTypes == null)
164 nodeTypes = hashMapOf()
165 nodeTypes!![id] = NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
168 fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
169 topologyTemplate = TopologyTemplateBuilder().apply(block).build()
172 fun build(): ServiceTemplate {
174 serviceTemplate.metadata = metadata
175 serviceTemplate.imports = imports
176 serviceTemplate.dslDefinitions = dslDefinitions
177 serviceTemplate.nodeTypes = nodeTypes
178 serviceTemplate.artifactTypes = artifactTypes
179 serviceTemplate.dataTypes = dataTypes
180 serviceTemplate.relationshipTypes = relationshipTypes
181 serviceTemplate.policyTypes = policyTypes
182 serviceTemplate.topologyTemplate = topologyTemplate
183 return serviceTemplate