2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.core.dsl
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
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
31 fun derivedFrom(derivedFrom: String) {
32 this.derivedFrom = derivedFrom
35 fun metadata(key: String, value: String) {
37 metadata = hashMapOf()
38 metadata!![key] = value
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
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
55 fun buildEntityType(entity: EntityType) {
57 entity.description = description
58 entity.version = version
59 entity.derivedFrom = derivedFrom
60 entity.metadata = metadata
61 entity.properties = properties
62 entity.attributes = attributes
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
74 fun capability(id: String, block: CapabilityDefinitionBuilder.() -> Unit) {
75 if (capabilities == null)
76 capabilities = hashMapOf()
77 capabilities!![id] = CapabilityDefinitionBuilder(id).apply(block).build()
80 fun requirement(id: String, block: RequirementDefinitionBuilder.() -> Unit) {
81 if (requirements == null)
82 requirements = hashMapOf()
83 requirements!![id] = RequirementDefinitionBuilder(id).apply(block).build()
86 fun artifact(id: String, type: String, file: String) {
87 if (artifacts == null)
88 artifacts = hashMapOf()
89 artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
92 private fun nodeInterface(id: String, block: InterfaceDefinitionBuilder.() -> Unit) {
93 if (interfaces == null)
94 interfaces = hashMapOf()
95 interfaces!![id] = InterfaceDefinitionBuilder(id).apply(block).build()
98 fun build(): NodeType {
99 buildEntityType(nodeType)
100 nodeType.capabilities = capabilities
101 nodeType.requirements = requirements
102 nodeType.interfaces = interfaces
103 nodeType.artifacts = artifacts
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()
112 fun build(): ArtifactType {
113 buildEntityType(artifactType)
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()
122 fun build(): RequirementType {
123 buildEntityType(requirementType)
124 return requirementType
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()
132 fun build(): RelationshipType {
133 buildEntityType(relationshipType)
134 return relationshipType
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()
142 fun build(): DataType {
143 buildEntityType(dataType)
148 class CapabilityDefinitionBuilder(private val id: String) {
150 private var capabilityDefinition = CapabilityDefinition()
151 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
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)
158 fun build(): CapabilityDefinition {
159 capabilityDefinition.id = id
160 capabilityDefinition.properties = properties
161 return capabilityDefinition
165 class RequirementDefinitionBuilder(private val id: String) {
166 private var requirementDefinition = RequirementDefinition()
168 fun build(): RequirementDefinition {
169 requirementDefinition.id = id
171 return requirementDefinition
175 class InterfaceDefinitionBuilder(private val id: String) {
177 private var interfaceDefinition: InterfaceDefinition = InterfaceDefinition()
178 private var operations: MutableMap<String, OperationDefinition>? = null
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()
186 fun build(): InterfaceDefinition {
187 interfaceDefinition.id = id
188 interfaceDefinition.operations = operations
189 return interfaceDefinition
193 class OperationDefinitionBuilder(private val id: String,
194 private val description: String? = "") {
195 private var operationDefinition: OperationDefinition = OperationDefinition()
197 fun inputs(block: PropertiesDefinitionBuilder.() -> Unit) {
198 operationDefinition.inputs = PropertiesDefinitionBuilder().apply(block).build()
201 fun outputs(block: PropertiesDefinitionBuilder.() -> Unit) {
202 operationDefinition.outputs = PropertiesDefinitionBuilder().apply(block).build()
205 fun build(): OperationDefinition {
206 operationDefinition.id = id
207 operationDefinition.description = description
208 return operationDefinition
212 class AttributesDefinitionBuilder {
213 private val attributes: MutableMap<String, AttributeDefinition> = hashMapOf()
215 fun property(id: String, attribute: AttributeDefinition) {
216 attributes.put(id, attribute)
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)
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)
230 fun build(): MutableMap<String, AttributeDefinition> {
235 class AttributeDefinitionBuilder(private val id: String,
236 private val type: String? = "string",
237 private val required: Boolean? = false,
238 private val description: String? = "") {
240 private var attributeDefinition: AttributeDefinition = AttributeDefinition()
242 fun entrySchema(entrySchemaType: String) {
243 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
246 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
247 attributeDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
250 // TODO("Constrains")
252 fun defaultValue(defaultValue: JsonNode) {
253 attributeDefinition.defaultValue = defaultValue
256 fun build(): AttributeDefinition {
257 attributeDefinition.id = id
258 attributeDefinition.type = type!!
259 attributeDefinition.required = required
260 attributeDefinition.description = description
261 return attributeDefinition
265 class PropertiesDefinitionBuilder {
266 private val properties: MutableMap<String, PropertyDefinition> = hashMapOf()
268 fun property(id: String, property: PropertyDefinition) {
269 properties.put(id, property)
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)
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)
283 fun build(): MutableMap<String, PropertyDefinition> {
288 class PropertyDefinitionBuilder(private val id: String,
289 private val type: String? = "string",
290 private val required: Boolean? = false,
291 private val description: String? = "") {
293 private var propertyDefinition: PropertyDefinition = PropertyDefinition()
295 fun entrySchema(entrySchemaType: String) {
296 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).build()
299 fun entrySchema(entrySchemaType: String, block: EntrySchemaBuilder.() -> Unit) {
300 propertyDefinition.entrySchema = EntrySchemaBuilder(entrySchemaType).apply(block).build()
302 // TODO("Constrains")
304 fun defaultValue(defaultValue: JsonNode) {
305 propertyDefinition.defaultValue = defaultValue
308 fun build(): PropertyDefinition {
309 propertyDefinition.id = id
310 propertyDefinition.type = type!!
311 propertyDefinition.required = required
312 propertyDefinition.description = description
313 return propertyDefinition
317 class EntrySchemaBuilder(private val type: String) {
318 private var entrySchema: EntrySchema = EntrySchema()
320 fun build(): EntrySchema {
321 entrySchema.type = type