259efbf0bbe60b69526f820f4a7e7ca2101223d7
[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.*
21 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
22 import kotlin.reflect.KClass
23
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
38
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
44     }
45
46     fun metadata(id: String, value: String) {
47         metadata[id] = value
48     }
49
50     fun import(file: String) {
51         val importDefinition = ImportDefinition().apply {
52             this.file = file
53         }
54         imports.add(importDefinition)
55     }
56
57     fun dsl(id: String, kclass: KClass<*>) {
58         dsl(id, kclass.asPropertyDefinitionMap().asJsonNode())
59     }
60
61     fun dataType(dataType: KClass<*>) {
62         dataType(dataType.asBluePrintsDataTypes())
63     }
64
65     fun dsl(id: String, content: Any) {
66         dsl(id, content.asJsonType())
67     }
68
69     fun dsl(id: String, json: JsonNode) {
70         if (dslDefinitions == null)
71             dslDefinitions = hashMapOf()
72         dslDefinitions!![id] = json
73     }
74
75     fun dataTypes(dataTypes: MutableMap<String, DataType>) {
76         if (this.dataTypes == null)
77             this.dataTypes = hashMapOf()
78
79         this.dataTypes!!.putAll(dataTypes)
80     }
81
82     fun artifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
83         if (this.artifactTypes == null)
84             this.artifactTypes = hashMapOf()
85
86         this.artifactTypes!!.putAll(artifactTypes)
87     }
88
89     fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) {
90         if (this.relationshipTypes == null)
91             this.relationshipTypes = hashMapOf()
92
93         this.relationshipTypes!!.putAll(relationshipTypes)
94     }
95
96     fun policyTypes(policyTypes: MutableMap<String, PolicyType>) {
97         if (this.policyTypes == null)
98             this.policyTypes = hashMapOf()
99
100         this.policyTypes!!.putAll(policyTypes)
101     }
102
103     fun nodeType(nodeTypes: MutableMap<String, NodeType>) {
104         if (this.nodeTypes == null)
105             this.nodeTypes = hashMapOf()
106
107         this.nodeTypes!!.putAll(nodeTypes)
108     }
109
110     fun dataType(dataType: DataType) {
111         if (dataTypes == null)
112             dataTypes = hashMapOf()
113         dataTypes!![dataType.id!!] = dataType
114     }
115
116     fun artifactType(artifactType: ArtifactType) {
117         if (artifactTypes == null)
118             artifactTypes = hashMapOf()
119         artifactTypes!![artifactType.id!!] = artifactType
120     }
121
122     fun relationshipType(relationshipType: RelationshipType) {
123         if (relationshipTypes == null)
124             relationshipTypes = hashMapOf()
125         relationshipTypes!![relationshipType.id!!] = relationshipType
126     }
127
128     fun policyType(policyType: PolicyType) {
129         if (policyTypes == null)
130             policyTypes = hashMapOf()
131
132         policyTypes!![policyType.id!!] = policyType
133     }
134
135     fun nodeType(nodeType: NodeType) {
136         if (nodeTypes == null)
137             nodeTypes = hashMapOf()
138         nodeTypes!![nodeType.id!!] = nodeType
139     }
140
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()
146     }
147
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()
153     }
154
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()
160     }
161
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()
167     }
168
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()
174     }
175
176     fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
177         topologyTemplate = TopologyTemplateBuilder().apply(block).build()
178     }
179
180     fun build(): ServiceTemplate {
181         initMetaData()
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
192     }
193 }