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