Runtime services for Relationship Type and Templates
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / 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) 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
97         this.artifactTypes!!.putAll(artifactTypes)
98     }
99
100     fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) {
101         if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
102
103         this.relationshipTypes!!.putAll(relationshipTypes)
104     }
105
106     fun policyTypes(policyTypes: MutableMap<String, PolicyType>) {
107         if (this.policyTypes == null) this.policyTypes = hashMapOf()
108
109         this.policyTypes!!.putAll(policyTypes)
110     }
111
112     fun nodeType(nodeTypes: MutableMap<String, NodeType>) {
113         if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
114
115         this.nodeTypes!!.putAll(nodeTypes)
116     }
117
118     fun dataType(dataType: DataType) {
119         if (dataTypes == null) dataTypes = hashMapOf()
120         dataTypes!![dataType.id!!] = dataType
121     }
122
123     fun artifactType(artifactType: ArtifactType) {
124         if (artifactTypes == null) artifactTypes = hashMapOf()
125         artifactTypes!![artifactType.id!!] = artifactType
126     }
127
128     fun relationshipType(relationshipType: RelationshipType) {
129         if (relationshipTypes == null) relationshipTypes = hashMapOf()
130         relationshipTypes!![relationshipType.id!!] = relationshipType
131     }
132
133     fun relationshipTypes(relationshipTypes: List<RelationshipType>) {
134         if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
135         relationshipTypes.forEach { relationshipType ->
136             this.relationshipTypes!![relationshipType.id!!] = relationshipType
137         }
138     }
139
140     fun policyType(policyType: PolicyType) {
141         if (policyTypes == null) policyTypes = hashMapOf()
142
143         policyTypes!![policyType.id!!] = policyType
144     }
145
146     fun nodeType(nodeType: NodeType) {
147         if (nodeTypes == null) nodeTypes = hashMapOf()
148         nodeTypes!![nodeType.id!!] = nodeType
149     }
150
151     fun nodeTypes(nodeTypes: List<NodeType>) {
152         if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
153         nodeTypes.forEach { nodeType ->
154             this.nodeTypes!![nodeType.id!!] = nodeType
155         }
156     }
157
158     fun dataType(
159         id: String,
160         version: String,
161         derivedFrom: String,
162         description: String,
163         block: DataTypeBuilder.() -> Unit
164     ) {
165         if (dataTypes == null) dataTypes = hashMapOf()
166         dataTypes!![id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
167     }
168
169     fun artifactType(
170         id: String,
171         version: String,
172         derivedFrom: String,
173         description: String,
174         block: ArtifactTypeBuilder.() -> Unit
175     ) {
176         if (artifactTypes == null) artifactTypes = hashMapOf()
177         artifactTypes!![id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
178     }
179
180     fun relationshipType(
181         id: String,
182         version: String,
183         derivedFrom: String,
184         description: String,
185         block: RelationshipTypeBuilder.() -> Unit
186     ) {
187         if (relationshipTypes == null) 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) policyTypes = hashMapOf()
199         policyTypes!![id] = PolicyTypeBuilder(id, version, derivedFrom, description).apply(block).build()
200     }
201
202     fun nodeType(
203         id: String,
204         version: String,
205         derivedFrom: String,
206         description: String,
207         block: NodeTypeBuilder.() -> Unit
208     ) {
209         if (nodeTypes == null) nodeTypes = hashMapOf()
210         nodeTypes!![id] = NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
211     }
212
213     fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
214         topologyTemplate = TopologyTemplateBuilder().apply(block).build()
215     }
216
217     fun build(): ServiceTemplate {
218         initMetaData()
219         serviceTemplate.metadata = metadata
220         serviceTemplate.imports = imports
221         serviceTemplate.dslDefinitions = dslDefinitions
222         serviceTemplate.nodeTypes = nodeTypes
223         serviceTemplate.artifactTypes = artifactTypes
224         serviceTemplate.dataTypes = dataTypes
225         serviceTemplate.relationshipTypes = relationshipTypes
226         serviceTemplate.policyTypes = policyTypes
227         serviceTemplate.topologyTemplate = topologyTemplate
228         return serviceTemplate
229     }
230 }