Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / ServiceTemplateBuilder.kt
1 /*
2  *  Copyright © 2019 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core.dsl
18
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
34
35 class ServiceTemplateBuilder(
36     private val name: String,
37     private val version: String,
38     private val author: String,
39     private val tags: String
40 ) {
41
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
52
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
58     }
59
60     fun metadata(id: String, value: String) {
61         metadata[id] = value
62     }
63
64     fun import(file: String) {
65         val importDefinition = ImportDefinition().apply {
66             this.file = file
67         }
68         imports.add(importDefinition)
69     }
70
71     fun dsl(id: String, kclass: KClass<*>) {
72         dsl(id, kclass.asPropertyDefinitionMap().asJsonNode())
73     }
74
75     fun dataType(dataType: KClass<*>) {
76         dataType(dataType.asBlueprintsDataTypes())
77     }
78
79     fun dsl(id: String, content: Any) {
80         dsl(id, content.asJsonType())
81     }
82
83     fun dsl(id: String, json: JsonNode) {
84         if (dslDefinitions == null) dslDefinitions = hashMapOf()
85         dslDefinitions!![id] = json
86     }
87
88     fun dataTypes(dataTypes: MutableMap<String, DataType>) {
89         if (this.dataTypes == null) this.dataTypes = hashMapOf()
90
91         this.dataTypes!!.putAll(dataTypes)
92     }
93
94     fun artifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
95         if (this.artifactTypes == null) this.artifactTypes = hashMapOf()
96         this.artifactTypes!!.putAll(artifactTypes)
97     }
98
99     fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) {
100         if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
101         this.relationshipTypes!!.putAll(relationshipTypes)
102     }
103
104     fun policyTypes(policyTypes: MutableMap<String, PolicyType>) {
105         if (this.policyTypes == null) this.policyTypes = hashMapOf()
106         this.policyTypes!!.putAll(policyTypes)
107     }
108
109     fun nodeType(nodeTypes: MutableMap<String, NodeType>) {
110         if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
111         this.nodeTypes!!.putAll(nodeTypes)
112     }
113
114     fun dataType(dataType: DataType) {
115         if (dataTypes == null) dataTypes = hashMapOf()
116         dataTypes!![dataType.id!!] = dataType
117     }
118
119     fun artifactType(artifactType: ArtifactType) {
120         if (artifactTypes == null) artifactTypes = hashMapOf()
121         artifactTypes!![artifactType.id!!] = artifactType
122     }
123
124     fun relationshipType(relationshipType: RelationshipType) {
125         if (relationshipTypes == null) relationshipTypes = hashMapOf()
126         relationshipTypes!![relationshipType.id!!] = relationshipType
127     }
128
129     fun relationshipTypes(relationshipTypes: List<RelationshipType>) {
130         if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
131         relationshipTypes.forEach { relationshipType ->
132             this.relationshipTypes!![relationshipType.id!!] = relationshipType
133         }
134     }
135
136     fun policyType(policyType: PolicyType) {
137         if (policyTypes == null) policyTypes = hashMapOf()
138         policyTypes!![policyType.id!!] = policyType
139     }
140
141     fun nodeType(nodeType: NodeType) {
142         if (nodeTypes == null) nodeTypes = hashMapOf()
143         nodeTypes!![nodeType.id!!] = nodeType
144     }
145
146     fun nodeTypes(nodeTypes: List<NodeType>) {
147         if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
148         nodeTypes.forEach { nodeType ->
149             this.nodeTypes!![nodeType.id!!] = nodeType
150         }
151     }
152
153     fun dataType(
154         id: String,
155         version: String,
156         derivedFrom: String,
157         description: String,
158         block: DataTypeBuilder.() -> Unit
159     ) {
160         if (dataTypes == null) dataTypes = hashMapOf()
161         dataTypes!![id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
162     }
163
164     fun artifactType(
165         id: String,
166         version: String,
167         derivedFrom: String,
168         description: String,
169         block: ArtifactTypeBuilder.() -> Unit
170     ) {
171         if (artifactTypes == null) artifactTypes = hashMapOf()
172         artifactTypes!![id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
173     }
174
175     fun relationshipType(
176         id: String,
177         version: String,
178         derivedFrom: String,
179         description: String,
180         block: RelationshipTypeBuilder.() -> Unit
181     ) {
182         if (relationshipTypes == null) relationshipTypes = hashMapOf()
183         relationshipTypes!![id] = RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build()
184     }
185
186     fun policyType(
187         id: String,
188         version: String,
189         derivedFrom: String,
190         description: String,
191         block: PolicyTypeBuilder.() -> Unit
192     ) {
193         if (policyTypes == null) policyTypes = hashMapOf()
194         policyTypes!![id] = PolicyTypeBuilder(id, version, derivedFrom, description).apply(block).build()
195     }
196
197     fun nodeType(
198         id: String,
199         version: String,
200         derivedFrom: String,
201         description: String,
202         block: NodeTypeBuilder.() -> Unit
203     ) {
204         if (nodeTypes == null) nodeTypes = hashMapOf()
205         nodeTypes!![id] = NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
206     }
207
208     fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
209         topologyTemplate = TopologyTemplateBuilder().apply(block).build()
210     }
211
212     fun build(): ServiceTemplate {
213         initMetaData()
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
224     }
225 }