ca4d5633855269c1a5af97ad5d0bb90792ed087a
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BluePrintServiceDSLBuilder.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 BluePrintServiceDSLBuilder(
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     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
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)
85             dslDefinitions = hashMapOf()
86         dslDefinitions!![id] = json
87     }
88
89     fun dataTypes(dataTypes: MutableMap<String, DataType>) {
90         if (this.dataTypes == null)
91             this.dataTypes = hashMapOf()
92
93         this.dataTypes!!.putAll(dataTypes)
94     }
95
96     fun artifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
97         if (this.artifactTypes == null)
98             this.artifactTypes = hashMapOf()
99
100         this.artifactTypes!!.putAll(artifactTypes)
101     }
102
103     fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) {
104         if (this.relationshipTypes == null)
105             this.relationshipTypes = hashMapOf()
106
107         this.relationshipTypes!!.putAll(relationshipTypes)
108     }
109
110     fun policyTypes(policyTypes: MutableMap<String, PolicyType>) {
111         if (this.policyTypes == null)
112             this.policyTypes = hashMapOf()
113
114         this.policyTypes!!.putAll(policyTypes)
115     }
116
117     fun nodeType(nodeTypes: MutableMap<String, NodeType>) {
118         if (this.nodeTypes == null)
119             this.nodeTypes = hashMapOf()
120
121         this.nodeTypes!!.putAll(nodeTypes)
122     }
123
124     fun dataType(dataType: DataType) {
125         if (dataTypes == null)
126             dataTypes = hashMapOf()
127         dataTypes!![dataType.id!!] = dataType
128     }
129
130     fun artifactType(artifactType: ArtifactType) {
131         if (artifactTypes == null)
132             artifactTypes = hashMapOf()
133         artifactTypes!![artifactType.id!!] = artifactType
134     }
135
136     fun relationshipType(relationshipType: RelationshipType) {
137         if (relationshipTypes == null)
138             relationshipTypes = hashMapOf()
139         relationshipTypes!![relationshipType.id!!] = relationshipType
140     }
141
142     fun policyType(policyType: PolicyType) {
143         if (policyTypes == null)
144             policyTypes = hashMapOf()
145
146         policyTypes!![policyType.id!!] = policyType
147     }
148
149     fun nodeType(nodeType: NodeType) {
150         if (nodeTypes == null)
151             nodeTypes = hashMapOf()
152         nodeTypes!![nodeType.id!!] = nodeType
153     }
154
155     fun dataType(
156         id: String,
157         version: String,
158         derivedFrom: String,
159         description: String,
160         block: DataTypeBuilder.() -> Unit
161     ) {
162         if (dataTypes == null)
163             dataTypes = hashMapOf()
164         dataTypes!![id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
165     }
166
167     fun artifactType(
168         id: String,
169         version: String,
170         derivedFrom: String,
171         description: String,
172         block: ArtifactTypeBuilder.() -> Unit
173     ) {
174         if (artifactTypes == null)
175             artifactTypes = hashMapOf()
176         artifactTypes!![id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
177     }
178
179     fun relationshipType(
180         id: String,
181         version: String,
182         derivedFrom: String,
183         description: String,
184         block: RelationshipTypeBuilder.() -> Unit
185     ) {
186         if (relationshipTypes == null)
187             relationshipTypes = hashMapOf()
188         relationshipTypes!![id] = RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build()
189     }
190
191     fun policyType(
192         id: String,
193         version: String,
194         derivedFrom: String,
195         description: String,
196         block: PolicyTypeBuilder.() -> Unit
197     ) {
198         if (policyTypes == null)
199             policyTypes = hashMapOf()
200         policyTypes!![id] = PolicyTypeBuilder(id, version, derivedFrom, description).apply(block).build()
201     }
202
203     fun nodeType(
204         id: String,
205         version: String,
206         derivedFrom: String,
207         description: String,
208         block: NodeTypeBuilder.() -> Unit
209     ) {
210         if (nodeTypes == null)
211             nodeTypes = hashMapOf()
212         nodeTypes!![id] = NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
213     }
214
215     fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
216         topologyTemplate = TopologyTemplateBuilder().apply(block).build()
217     }
218
219     fun build(): ServiceTemplate {
220         initMetaData()
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
231     }
232 }