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