340894f1b40171e2397880fc0ffb53636c072729
[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.blueprintsprocessor.functions.resource.resolution
18
19 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
20 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
21 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertyDefinitionBuilder
22 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
23
24 /** Resource Definition DSL **/
25 fun BluePrintTypes.resourceDefinition(name: String,
26                                       block: ResourceDefinitionBuilder.() -> Unit): ResourceDefinition {
27     return ResourceDefinitionBuilder(name).apply(block).build()
28 }
29
30 class ResourceDefinitionBuilder(private val name: String) {
31     private val resourceDefinition = ResourceDefinition()
32
33     fun updatedBy(updatedBy: String) {
34         resourceDefinition.updatedBy = updatedBy
35     }
36
37     fun tags(tags: String) {
38         resourceDefinition.tags = tags
39     }
40
41     fun property(id: String, type: String, required: Boolean, description: String? = "") {
42         resourceDefinition.property = PropertyDefinitionBuilder(id, type, required, description).build()
43     }
44
45     fun property(id: String, type: String, required: Boolean, description: String? = "",
46                  block: PropertyDefinitionBuilder.() -> Unit) {
47         resourceDefinition.property = PropertyDefinitionBuilder(id, type, required, description).apply(block).build()
48     }
49
50     fun sources(block: ResourceDefinitionSourcesBuilder.() -> Unit) {
51         resourceDefinition.sources = ResourceDefinitionSourcesBuilder().apply(block).build()
52     }
53
54     fun sources(sources: MutableMap<String, NodeTemplate>) {
55         resourceDefinition.sources = sources
56     }
57
58     fun build(): ResourceDefinition {
59         resourceDefinition.name = name
60         return resourceDefinition
61     }
62 }
63
64 class ResourceDefinitionSourcesBuilder {
65     var sources: MutableMap<String, NodeTemplate> = hashMapOf()
66
67     fun source(source: NodeTemplate) {
68         sources[source.id!!] = source
69     }
70
71     fun sourceInput(id: String, description: String, block: SourceInputNodeTemplateBuilder.() -> Unit) {
72         sources[id] = SourceInputNodeTemplateBuilder(id, description).apply(block).build()
73     }
74
75     fun sourceDefault(id: String, description: String, block: SourceDefaultNodeTemplateBuilder.() -> Unit) {
76         sources[id] = SourceDefaultNodeTemplateBuilder(id, description).apply(block).build()
77     }
78
79     fun sourceDb(id: String, description: String, block: SourceDbNodeTemplateBuilder.() -> Unit) {
80         sources[id] = SourceDbNodeTemplateBuilder(id, description).apply(block).build()
81     }
82
83     fun sourceRest(id: String, description: String, block: SourceRestNodeTemplateBuilder.() -> Unit) {
84         sources[id] = SourceRestNodeTemplateBuilder(id, description).apply(block).build()
85     }
86
87     fun sourceCapability(id: String, description: String, block: SourceCapabilityNodeTemplateBuilder.() -> Unit) {
88         sources[id] = SourceCapabilityNodeTemplateBuilder(id, description).apply(block).build()
89     }
90
91     fun build(): MutableMap<String, NodeTemplate> {
92         return sources
93     }
94 }