07cc20ebdc677e591305ff54d090df5c9979fb4c
[ccsdk/cds.git] /
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.data.*
22
23
24 open class EntityTypeBuilder(private val id: String,
25                              private val version: String,
26                              private val derivedFrom: String,
27                              private val description: String? = "") {
28     var metadata: MutableMap<String, String>? = null
29     var properties: MutableMap<String, PropertyDefinition>? = null
30     var attributes: MutableMap<String, AttributeDefinition>? = null
31
32     fun metadata(key: String, value: String) {
33         if (metadata == null)
34             metadata = hashMapOf()
35         metadata!![key] = value
36     }
37
38     fun attribute(id: String, type: String, required: Boolean, description: String? = "") {
39         if (attributes == null)
40             attributes = hashMapOf()
41         val attribute = AttributeDefinitionBuilder(id, type, required, description).build()
42         attributes!![id] = attribute
43     }
44
45     fun attribute(id: String, type: String, required: Boolean, description: String? = "",
46                   block: AttributeDefinitionBuilder.() -> Unit) {
47         if (attributes == null)
48             attributes = hashMapOf()
49         val attribute = AttributeDefinitionBuilder(id, type, required, description).apply(block).build()
50         attributes!![id] = attribute
51     }
52
53     fun property(id: String, type: String, required: Boolean, description: String? = "") {
54         if (properties == null)
55             properties = hashMapOf()
56         val property = PropertyDefinitionBuilder(id, type, required, description).build()
57         properties!![id] = property
58     }
59
60     fun property(id: String, type: String, required: Boolean, description: String? = "",
61                  block: PropertyDefinitionBuilder.() -> Unit) {
62         if (properties == null)
63             properties = hashMapOf()
64         val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
65         properties!![id] = property
66     }
67
68     fun buildEntityType(entity: EntityType) {
69         entity.id = id
70         entity.description = description
71         entity.version = version
72         entity.derivedFrom = derivedFrom
73         entity.metadata = metadata
74         entity.properties = properties
75         entity.attributes = attributes
76     }
77 }
78
79 class NodeTypeBuilder(id: String, version: String, derivedFrom: String,
80                       description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
81     private var nodeType = NodeType()
82     private var capabilities: MutableMap<String, CapabilityDefinition>? = null
83     private var requirements: MutableMap<String, RequirementDefinition>? = null
84     private var interfaces: MutableMap<String, InterfaceDefinition>? = null
85     private var artifacts: MutableMap<String, ArtifactDefinition>? = null
86
87     fun capability(id: String, type: String, description: String, block: CapabilityDefinitionBuilder.() -> Unit) {
88         if (capabilities == null)
89             capabilities = hashMapOf()
90         capabilities!![id] = CapabilityDefinitionBuilder(id, type, description).apply(block).build()
91     }
92
93     fun requirement(id: String, capability: String, node: String, relationship: String, description: String) {
94         if (requirements == null)
95             requirements = hashMapOf()
96         requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description).build()
97     }
98
99     fun requirement(id: String, capability: String, node: String, relationship: String, description: String,
100                     block: RequirementDefinitionBuilder.() -> Unit) {
101         if (requirements == null)
102             requirements = hashMapOf()
103         requirements!![id] = RequirementDefinitionBuilder(id, capability, node, relationship, description)
104                 .apply(block).build()
105     }
106
107     fun artifact(id: String, type: String, file: String) {
108         if (artifacts == null)
109             artifacts = hashMapOf()
110         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
111     }
112
113     private fun nodeInterface(id: String, block: InterfaceDefinitionBuilder.() -> Unit) {
114         if (interfaces == null)
115             interfaces = hashMapOf()
116         interfaces!![id] = InterfaceDefinitionBuilder(id).apply(block).build()
117     }
118
119     fun operation(interfaceName: String, description: String?, block: OperationDefinitionBuilder.() -> Unit) {
120         if (interfaces == null)
121             interfaces = hashMapOf()
122
123         val interfaceDefinition = InterfaceDefinition()
124         val defaultOperationName = "process"
125         interfaceDefinition.operations = hashMapOf()
126         interfaceDefinition.operations!![defaultOperationName] =
127                 OperationDefinitionBuilder(defaultOperationName, description).apply(block).build()
128         interfaces!![interfaceName] = interfaceDefinition
129     }
130
131     fun build(): NodeType {
132         buildEntityType(nodeType)
133         nodeType.capabilities = capabilities
134         nodeType.requirements = requirements
135         nodeType.interfaces = interfaces
136         nodeType.artifacts = artifacts
137         return nodeType
138     }
139 }
140
141 class ArtifactTypeBuilder(id: String, version: String, derivedFrom: String,
142                           description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
143     private var artifactType = ArtifactType()
144     private var fileExt: MutableList<String>? = null
145
146     fun fileExt(vararg fileExts: String) {
147         if (fileExt == null)
148             fileExt = arrayListOf()
149         fileExts.forEach {
150             fileExt!!.add(it)
151         }
152     }
153
154     fun build(): ArtifactType {
155         buildEntityType(artifactType)
156         artifactType.fileExt = fileExt
157         return artifactType
158     }
159 }
160
161 class PolicyTypeBuilder(id: String, version: String, derivedFrom: String,
162                         description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
163     private var policyType = PolicyType()
164     // TODO()
165     fun build(): PolicyType {
166         buildEntityType(policyType)
167         return policyType
168     }
169 }
170
171 class RelationshipTypeBuilder(private val id: String, private val version: String,
172                               derivedFrom: String, private val description: String?)
173     : EntityTypeBuilder(id, version, derivedFrom, description) {
174
175     private var relationshipType = RelationshipType()
176     // TODO()
177     fun build(): RelationshipType {
178         buildEntityType(relationshipType)
179         relationshipType.id = id
180         relationshipType.version = version
181         relationshipType.description = description
182         return relationshipType
183     }
184 }
185
186 class DataTypeBuilder(id: String, version: String, derivedFrom: String,
187                       description: String?) : EntityTypeBuilder(id, version, derivedFrom, description) {
188     private var dataType = DataType()
189     // TODO()
190     fun build(): DataType {
191         buildEntityType(dataType)
192         return dataType
193     }
194 }
195
196 class CapabilityDefinitionBuilder(private val id: String, private val type: String, private val description: String? = "") {
197
198     private var capabilityDefinition = CapabilityDefinition()
199     private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
200
201     fun property(id: String, type: String? = BluePrintConstants.DATA_TYPE_STRING, required: Boolean? = false, description: String? = "") {
202         val property = PropertyDefinitionBuilder(id, type, required, description).build()
203         properties[id] = property
204     }
205
206     fun build(): CapabilityDefinition {
207         capabilityDefinition.id = id
208         capabilityDefinition.description = description
209         capabilityDefinition.type = type
210         capabilityDefinition.properties = properties
211         return capabilityDefinition
212     }
213 }
214
215 class RequirementDefinitionBuilder(private val id: String, private val capability: String, private val node: String,
216                                    private val relationship: String, private val description: String? = "") {
217     private var requirementDefinition = RequirementDefinition()
218
219     fun build(): RequirementDefinition {
220         requirementDefinition.id = id
221         requirementDefinition.description = description
222         requirementDefinition.capability = capability
223         requirementDefinition.node = node
224         requirementDefinition.relationship = relationship
225         return requirementDefinition
226     }
227 }
228
229 class InterfaceDefinitionBuilder(private val id: String) {
230
231     private var interfaceDefinition: InterfaceDefinition = InterfaceDefinition()
232     private var operations: MutableMap<String, OperationDefinition>? = null
233
234     fun operation(id: String, description: String? = "", block: OperationDefinitionBuilder.() -> Unit) {
235         if (operations == null)
236             operations = hashMapOf()
237         operations!![id] = OperationDefinitionBuilder(id, description).apply(block).build()
238     }
239
240     fun build(): InterfaceDefinition {
241         interfaceDefinition.id = id
242         interfaceDefinition.operations = operations
243         return interfaceDefinition
244     }
245 }
246
247 class OperationDefinitionBuilder(private val id: String,
248                                  private val description: String? = "") {
249     private var operationDefinition: OperationDefinition = OperationDefinition()
250
251     fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
252         operationDefinition.inputs = PropertiesDefinitionBuilder().apply(block).build()
253     }
254
255     fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
256         operationDefinition.outputs = PropertiesDefinitionBuilder().apply(block).build()
257     }
258
259     fun build(): OperationDefinition {
260         operationDefinition.id = id
261         operationDefinition.description = description
262         return operationDefinition
263     }
264 }
265
266 class AttributesDefinitionBuilder {
267     private val attributes: MutableMap<String, AttributeDefinition> = hashMapOf()
268
269     fun property(id: String, attribute: AttributeDefinition) {
270         attributes[id] = attribute
271     }
272
273     fun property(id: String, type: String?, required: Boolean?, description: String?) {
274         val attribute = AttributeDefinitionBuilder(id, type, required, description).build()
275         attributes[id] = attribute
276     }
277
278     fun property(id: String, type: String?, required: Boolean?, description: String?,
279                  block: AttributeDefinitionBuilder.() -> Unit) {
280         val attribute = AttributeDefinitionBuilder(id, type, required, description).apply(block).build()
281         attributes[id] = attribute
282     }
283
284     fun build(): MutableMap<String, AttributeDefinition> {
285         return attributes
286     }
287 }
288
289 class AttributeDefinitionBuilder(private val id: String,
290                                  private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
291                                  private val required: Boolean? = false,
292                                  private val description: String? = "") {
293
294     private var attributeDefinition: AttributeDefinition = AttributeDefinition()
295
296     fun entrySchema(entrySchemaType: String) {
297         attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
298     }
299
300     fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
301         attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
302     }
303
304     // TODO("Constrains")
305
306     fun defaultValue(defaultValue: JsonNode) {
307         attributeDefinition.defaultValue = defaultValue
308     }
309
310     fun build(): AttributeDefinition {
311         attributeDefinition.id = id
312         attributeDefinition.type = type!!
313         attributeDefinition.required = required
314         attributeDefinition.description = description
315         return attributeDefinition
316     }
317 }
318
319 class PropertiesDefinitionBuilder {
320     private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
321
322     fun property(id: String, property: PropertyDefinition) {
323         properties[id] = property
324     }
325
326     fun property(id: String, type: String?, required: Boolean?, description: String? = "") {
327         val property = PropertyDefinitionBuilder(id, type, required, description).build()
328         properties[id] = property
329     }
330
331     fun property(id: String, type: String?, required: Boolean?, description: String? = "",
332                  block: PropertyDefinitionBuilder.() -> Unit) {
333         val property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
334         properties[id] = property
335     }
336
337     fun build(): MutableMap<String, PropertyDefinition> {
338         return properties
339     }
340 }
341
342 class PropertyDefinitionBuilder(private val id: String,
343                                 private val type: String? = BluePrintConstants.DATA_TYPE_STRING,
344                                 private val required: Boolean? = false,
345                                 private val description: String? = "") {
346
347     private var propertyDefinition: PropertyDefinition = PropertyDefinition()
348
349     fun entrySchema(entrySchemaType: String) {
350         propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
351     }
352
353     fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
354         propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
355     }
356     // TODO("Constrains")
357
358     fun defaultValue(defaultValue: JsonNode) {
359         propertyDefinition.defaultValue = defaultValue
360     }
361
362     fun value(value: JsonNode) {
363         propertyDefinition.value = value
364     }
365
366     fun build(): PropertyDefinition {
367         propertyDefinition.id = id
368         propertyDefinition.type = type!!
369         propertyDefinition.required = required
370         propertyDefinition.description = description
371         return propertyDefinition
372     }
373 }
374
375 class EntrySchemaBuilder(private val type: String) {
376     private var entrySchema: EntrySchema = EntrySchema()
377
378     fun build(): EntrySchema {
379         entrySchema.type = type
380         return entrySchema
381     }
382 }