Merge "Created media folders for ResourceDictionary"
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BluePrintTemplateDSLBuilder.kt
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 class TopologyTemplateBuilder {
25     private var topologyTemplate = TopologyTemplate()
26     private var nodeTemplates: MutableMap<String, NodeTemplate>? = null
27     private var workflows: MutableMap<String, Workflow>? = null
28
29     fun nodeTemplate(id: String, type: String, description: String, block: NodeTemplateBuilder.() -> Unit) {
30         if (nodeTemplates == null)
31             nodeTemplates = hashMapOf()
32         nodeTemplates!![id] = NodeTemplateBuilder(id, type, description).apply(block).build()
33     }
34
35     fun nodeTemplateOperation(nodeTemplateName: String, type: String, interfaceName: String, description: String,
36                               operationBlock: OperationAssignmentBuilder.() -> Unit) {
37         if (nodeTemplates == null)
38             nodeTemplates = hashMapOf()
39
40         val nodeTemplateBuilder = NodeTemplateBuilder(nodeTemplateName, type, description)
41         nodeTemplateBuilder.operation(interfaceName, "$description operation", operationBlock)
42         nodeTemplates!![nodeTemplateName] = nodeTemplateBuilder.build()
43     }
44
45     fun workflow(id: String, description: String, block: WorkflowBuilder.() -> Unit) {
46         if (workflows == null)
47             workflows = hashMapOf()
48         workflows!![id] = WorkflowBuilder(id, description).apply(block).build()
49     }
50
51     //TODO("populate inputs, outputs")
52     fun workflowNodeTemplate(actionName: String,
53                              nodeTemplateType: String, description: String, block: NodeTemplateBuilder.() -> Unit) {
54         if (nodeTemplates == null)
55             nodeTemplates = hashMapOf()
56
57         if (workflows == null)
58             workflows = hashMapOf()
59
60         val workflowBuilder = WorkflowBuilder(actionName, description)
61         workflowBuilder.nodeTemplateStep(actionName, description)
62         // Workflow name is NodeTemplate name
63         workflows!![actionName] = workflowBuilder.build()
64
65         nodeTemplates!![actionName] = NodeTemplateBuilder(actionName, nodeTemplateType, description).apply(block).build()
66     }
67
68     fun build(): TopologyTemplate {
69         topologyTemplate.nodeTemplates = nodeTemplates
70         topologyTemplate.workflows = workflows
71         return topologyTemplate
72     }
73 }
74
75 class NodeTemplateBuilder(private val id: String,
76                           private val type: String,
77                           private val description: String? = "") {
78     private var nodeTemplate: NodeTemplate = NodeTemplate()
79     private var interfaces: MutableMap<String, InterfaceAssignment>? = null
80     private var artifacts: MutableMap<String, ArtifactDefinition>? = null
81     private var capabilities: MutableMap<String, CapabilityAssignment>? = null
82     private var requirements: MutableMap<String, RequirementAssignment>? = null
83
84     fun operation(interfaceName: String, description: String? = "",
85                   block: OperationAssignmentBuilder.() -> Unit) {
86         if (interfaces == null)
87             interfaces = hashMapOf()
88
89         val interfaceAssignment = InterfaceAssignment()
90         val defaultOperationName = BluePrintConstants.DEFAULT_STEP_OPERATION
91         interfaceAssignment.operations = hashMapOf()
92         interfaceAssignment.operations!![defaultOperationName] =
93                 OperationAssignmentBuilder(defaultOperationName, description).apply(block).build()
94         interfaces!![interfaceName] = interfaceAssignment
95     }
96
97     fun artifact(id: String, type: String, file: String) {
98         if (artifacts == null)
99             artifacts = hashMapOf()
100         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).build()
101     }
102
103     fun artifact(id: String, type: String, file: String, block: ArtifactDefinitionBuilder.() -> Unit) {
104         if (artifacts == null)
105             artifacts = hashMapOf()
106         artifacts!![id] = ArtifactDefinitionBuilder(id, type, file).apply(block).build()
107     }
108
109     fun capability(id: String, block: CapabilityAssignmentBuilder.() -> Unit) {
110         if (capabilities == null)
111             capabilities = hashMapOf()
112         capabilities!![id] = CapabilityAssignmentBuilder(id).apply(block).build()
113     }
114
115     fun requirement(id: String, capability: String, node: String, relationship: String) {
116         if (requirements == null)
117             requirements = hashMapOf()
118         requirements!![id] = RequirementAssignmentBuilder(id, capability, node, relationship).build()
119     }
120
121     fun build(): NodeTemplate {
122         nodeTemplate.id = id
123         nodeTemplate.type = type
124         nodeTemplate.description = description
125         nodeTemplate.interfaces = interfaces
126         nodeTemplate.artifacts = artifacts
127         nodeTemplate.capabilities = capabilities
128         nodeTemplate.requirements = requirements
129         return nodeTemplate
130     }
131 }
132
133 class ArtifactDefinitionBuilder(private val id: String, private val type: String, private val file: String) {
134
135     private var artifactDefinition: ArtifactDefinition = ArtifactDefinition()
136     // TODO()
137
138     fun build(): ArtifactDefinition {
139         artifactDefinition.id = id
140         artifactDefinition.type = type
141         artifactDefinition.file = file
142         return artifactDefinition
143     }
144 }
145
146 class CapabilityAssignmentBuilder(private val id: String) {
147     private var capabilityAssignment: CapabilityAssignment = CapabilityAssignment()
148     private var attributes: MutableMap<String, JsonNode>? = null
149     private var properties: MutableMap<String, JsonNode>? = null
150
151     fun attributes(block: AttributesAssignmentBuilder.() -> Unit) {
152         if (attributes == null)
153             attributes = hashMapOf()
154         attributes = AttributesAssignmentBuilder().apply(block).build()
155     }
156
157     fun properties(block: PropertiesAssignmentBuilder.() -> Unit) {
158         if (properties == null)
159             properties = hashMapOf()
160         properties = PropertiesAssignmentBuilder().apply(block).build()
161     }
162
163     fun build(): CapabilityAssignment {
164         capabilityAssignment.properties = properties
165         capabilityAssignment.attributes = attributes
166         return capabilityAssignment
167     }
168 }
169
170 class RequirementAssignmentBuilder(private val id: String, private val capability: String,
171                                    private val node: String,
172                                    private val relationship: String) {
173     private var requirementAssignment: RequirementAssignment = RequirementAssignment()
174
175     fun build(): RequirementAssignment {
176         requirementAssignment.id = id
177         requirementAssignment.capability = capability
178         requirementAssignment.node = node
179         requirementAssignment.relationship = relationship
180         return requirementAssignment
181     }
182 }
183
184 class InterfaceAssignmentBuilder(private val id: String) {
185
186     private var interfaceAssignment: InterfaceAssignment = InterfaceAssignment()
187     private var operations: MutableMap<String, OperationAssignment>? = null
188
189     fun operation(id: String, description: String? = "", block: OperationAssignmentBuilder.() -> Unit) {
190         if (operations == null)
191             operations = hashMapOf()
192         operations!![id] = OperationAssignmentBuilder(id, description).apply(block).build()
193     }
194
195     fun build(): InterfaceAssignment {
196         interfaceAssignment.id = id
197         interfaceAssignment.operations = operations
198         return interfaceAssignment
199     }
200 }
201
202 class OperationAssignmentBuilder(private val id: String,
203                                  private val description: String? = "") {
204
205     private var operationAssignment: OperationAssignment = OperationAssignment()
206
207     fun implementation(timeout: Int, operationHost: String? = BluePrintConstants.PROPERTY_SELF) {
208         val implementation = Implementation().apply {
209             this.operationHost = operationHost!!
210             this.timeout = timeout
211         }
212         operationAssignment.implementation = implementation
213     }
214
215     fun inputs(block: PropertiesAssignmentBuilder.() -> Unit) {
216         operationAssignment.inputs = PropertiesAssignmentBuilder().apply(block).build()
217     }
218
219     fun outputs(block: PropertiesAssignmentBuilder.() -> Unit) {
220         operationAssignment.outputs = PropertiesAssignmentBuilder().apply(block).build()
221     }
222
223     fun build(): OperationAssignment {
224         operationAssignment.id = id
225         operationAssignment.description = description
226         return operationAssignment
227     }
228 }
229
230 class PropertiesAssignmentBuilder {
231     private var properties: MutableMap<String, JsonNode> = hashMapOf()
232
233     fun property(id: String, value: Any) {
234         property(id, value.asJsonType())
235     }
236
237     fun property(id: String, value: JsonNode) {
238         properties[id] = value
239     }
240
241     fun build(): MutableMap<String, JsonNode> {
242         return properties
243     }
244 }
245
246 class AttributesAssignmentBuilder {
247     private var attributes: MutableMap<String, JsonNode> = hashMapOf()
248
249     fun attribute(id: String, value: String) {
250         attribute(id, value.asJsonType())
251     }
252
253     fun attribute(id: String, value: JsonNode) {
254         attributes[id] = value
255     }
256
257     fun build(): MutableMap<String, JsonNode> {
258         return attributes
259     }
260 }