66d36a5b13fb5176cd0b5030d88fe78ba56b6103
[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.asJsonType
21 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
22
23 class TopologyTemplateBuilder() {
24     private var topologyTemplate = TopologyTemplate()
25     private var nodeTemplates: MutableMap<String, NodeTemplate>? = null
26     private var workflows: MutableMap<String, Workflow>? = null
27
28     fun nodeTemplate(id: String, type: String, description: String, block: NodeTemplateBuilder.() -> Unit) {
29         if (nodeTemplates == null)
30             nodeTemplates = hashMapOf()
31         nodeTemplates!![id] = NodeTemplateBuilder(id, type, description).apply(block).build()
32     }
33
34     fun nodeTemplateOperation(nodeTemplateName: String, type: String,
35                               interfaceName: String, operationName: String,
36                               description: String, operationBlock: OperationAssignmentBuilder.() -> Unit) {
37         if (nodeTemplates == null)
38             nodeTemplates = hashMapOf()
39
40         val nodeTemplateBuilder = NodeTemplateBuilder(nodeTemplateName, type, description)
41         nodeTemplateBuilder.operation(interfaceName, operationName, "$description operation", operationBlock)
42         nodeTemplates!![nodeTemplateName] = nodeTemplateBuilder.build()
43     }
44     //TODO("workflow")
45
46     fun build(): TopologyTemplate {
47         topologyTemplate.nodeTemplates = nodeTemplates
48         topologyTemplate.workflows = workflows
49         return topologyTemplate
50     }
51 }
52
53 class NodeTemplateBuilder(private val id: String,
54                           private val type: String,
55                           private val description: String? = "") {
56     private var nodeTemplate: NodeTemplate = NodeTemplate()
57     private var interfaces: MutableMap<String, InterfaceAssignment>? = null
58     private var artifacts: MutableMap<String, ArtifactDefinition>? = null
59     private var capabilities: MutableMap<String, CapabilityAssignment>? = null
60     private var requirements: MutableMap<String, RequirementAssignment>? = null
61
62     fun operation(interfaceName: String, operationName: String, description: String? = "",
63                   block: OperationAssignmentBuilder.() -> Unit) {
64         if (interfaces == null)
65             interfaces = hashMapOf()
66
67         val interfaceAssignment = InterfaceAssignment()
68         interfaceAssignment.operations = hashMapOf()
69         interfaceAssignment.operations!![operationName] = OperationAssignmentBuilder(operationName, description).apply(block).build()
70         interfaces!![interfaceName] = interfaceAssignment
71     }
72
73     fun artifact(id: String, type: String, file: String) {
74         if (artifacts == null)
75             artifacts = hashMapOf()
76         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
77     }
78
79     fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
80         if (artifacts == null)
81             artifacts = hashMapOf()
82         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
83     }
84
85     fun capability(id: String, block: CapabilityAssignmentBuilder.() -> Unit) {
86         if (capabilities == null)
87             capabilities = hashMapOf()
88         capabilities!![id] = CapabilityAssignmentBuilder(id).apply(block).build()
89     }
90
91     fun requirement(id: String, capability: String, node: String, relationship: String) {
92         if (requirements == null)
93             requirements = hashMapOf()
94         requirements!![id] = RequirementAssignmentBuilder(id, capability, node, relationship).build()
95     }
96
97     fun build(): NodeTemplate {
98         nodeTemplate.id = id
99         nodeTemplate.type = type
100         nodeTemplate.description = description
101         nodeTemplate.interfaces = interfaces
102         nodeTemplate.artifacts = artifacts
103         nodeTemplate.capabilities = capabilities
104         nodeTemplate.requirements = requirements
105         return nodeTemplate
106     }
107 }
108
109 class ArtifactDefinitionBuilder(private val id: String, private val type: String, private val file: String) {
110
111     private var artifactDefinition: ArtifactDefinition = ArtifactDefinition()
112     // TODO()
113
114     fun build(): ArtifactDefinition {
115         artifactDefinition.id = id
116         artifactDefinition.type = type
117         artifactDefinition.file = file
118         return artifactDefinition
119     }
120 }
121
122 class CapabilityAssignmentBuilder(private val id: String) {
123     private var capabilityAssignment: CapabilityAssignment = CapabilityAssignment()
124     private var attributes: MutableMap<String, JsonNode>? = null
125     private var properties: MutableMap<String, JsonNode>? = null
126
127     fun attributes(block: AttributesAssignmentBuilder.() -> Unit) {
128         if (attributes == null)
129             attributes = hashMapOf()
130         attributes = AttributesAssignmentBuilder().apply(block).build()
131     }
132
133     fun properties(block: PropertiesAssignmentBuilder.() -> Unit) {
134         if (properties == null)
135             properties = hashMapOf()
136         properties = PropertiesAssignmentBuilder().apply(block).build()
137     }
138
139     fun build(): CapabilityAssignment {
140         capabilityAssignment.properties = properties
141         capabilityAssignment.attributes = attributes
142         return capabilityAssignment
143     }
144 }
145
146 class RequirementAssignmentBuilder(private val id: String, private val capability: String,
147                                    private val node: String,
148                                    private val relationship: String) {
149     private var requirementAssignment: RequirementAssignment = RequirementAssignment()
150
151     fun build(): RequirementAssignment {
152         requirementAssignment.id = id
153         requirementAssignment.capability = capability
154         requirementAssignment.node = node
155         requirementAssignment.relationship = relationship
156         return requirementAssignment
157     }
158 }
159
160 class InterfaceAssignmentBuilder(private val id: String) {
161
162     private var interfaceAssignment: InterfaceAssignment = InterfaceAssignment()
163     private var operations: MutableMap<String, OperationAssignment>? = null
164
165     fun operation(id: String, description: String? = "", block: OperationAssignmentBuilder.() -> Unit) {
166         if (operations == null)
167             operations = hashMapOf()
168         operations!![id] = OperationAssignmentBuilder(id, description).apply(block).build()
169     }
170
171     fun build(): InterfaceAssignment {
172         interfaceAssignment.id = id
173         interfaceAssignment.operations = operations
174         return interfaceAssignment
175     }
176 }
177
178 class OperationAssignmentBuilder(private val id: String,
179                                  private val description: String? = "") {
180
181     private var operationAssignment: OperationAssignment = OperationAssignment()
182
183     fun inputs(block: PropertiesAssignmentBuilder.() -> Unit) {
184         operationAssignment.inputs = PropertiesAssignmentBuilder().apply(block).build()
185     }
186
187     fun build(): OperationAssignment {
188         operationAssignment.id = id
189         operationAssignment.description = description
190         return operationAssignment
191     }
192 }
193
194 class PropertiesAssignmentBuilder {
195     private var properties: MutableMap<String, JsonNode> = hashMapOf()
196
197     fun property(id: String, value: Any) {
198         property(id, value.asJsonType())
199     }
200
201     fun property(id: String, value: JsonNode) {
202         properties[id] = value
203     }
204
205     fun build(): MutableMap<String, JsonNode> {
206         return properties
207     }
208 }
209
210 class AttributesAssignmentBuilder {
211     private var attributes: MutableMap<String, JsonNode> = hashMapOf()
212
213     fun attribute(id: String, value: String) {
214         attribute(id, value.asJsonType())
215     }
216
217     fun attribute(id: String, value: JsonNode) {
218         attributes[id] = value
219     }
220
221     fun build(): MutableMap<String, JsonNode> {
222         return attributes
223     }
224 }