fc6df96c8eb928bfd2a5bfef6e82f3ca1f90e91f
[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.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.*
23
24 class ServiceTemplateBuilder(private val name: String,
25                              private val version: String,
26                              private val author: String,
27                              private val tags: String) {
28     private var serviceTemplate = ServiceTemplate()
29     private lateinit var topologyTemplate: TopologyTemplate
30     private var metadata: MutableMap<String, String> = hashMapOf()
31     private var dslDefinitions: MutableMap<String, JsonNode>? = null
32     private var imports: MutableList<ImportDefinition>? = null
33     private var nodeTypes: MutableMap<String, NodeType>? = null
34     private var artifactTypes: MutableMap<String, ArtifactType>? = null
35     private var dataTypes: MutableMap<String, DataType>? = null
36     private var relationshipTypes: MutableMap<String, RelationshipType>? = null
37
38     private fun initMetaData() {
39         metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name
40         metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version
41         metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author
42         metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags
43     }
44
45     fun metadata(id: String, value: String) {
46         metadata[id] = value
47     }
48
49     fun dsl(id: String, json: String) {
50        dsl(id, json.asJsonType())
51     }
52
53     fun dsl(id: String, json: JsonNode) {
54         if (dslDefinitions == null)
55             dslDefinitions = hashMapOf()
56         dslDefinitions!![id] = json.asJsonType()
57     }
58
59     // TODO("Imports")
60
61     fun dataType(id: String, version: String, description: String, block: DataTypeBuilder.() -> Unit) {
62         if (dataTypes == null)
63             dataTypes = hashMapOf()
64         dataTypes!![id] = DataTypeBuilder(id, version, description).apply(block).build()
65     }
66
67     fun artifactType(id: String, version: String, description: String, block: ArtifactTypeBuilder.() -> Unit) {
68         if (artifactTypes == null)
69             artifactTypes = hashMapOf()
70         artifactTypes!![id] = ArtifactTypeBuilder(id, version, description).apply(block).build()
71     }
72
73     fun relationshipType(id: String, version: String, description: String, block: RelationshipTypeBuilder.() -> Unit) {
74         if (relationshipTypes == null)
75             relationshipTypes = hashMapOf()
76         relationshipTypes!![id] = RelationshipTypeBuilder(id, version, description).apply(block).build()
77     }
78
79     fun nodeType(id: String, version: String, description: String, block: NodeTypeBuilder.() -> Unit) {
80         if (nodeTypes == null)
81             nodeTypes = hashMapOf()
82         nodeTypes!![id] = NodeTypeBuilder(id, version, description).apply(block).build()
83     }
84
85     fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
86         topologyTemplate = TopologyTemplateBuilder().apply(block).build()
87     }
88
89     fun build(): ServiceTemplate {
90         initMetaData()
91         serviceTemplate.metadata = metadata
92         serviceTemplate.imports = imports
93         serviceTemplate.dslDefinitions = dslDefinitions
94         serviceTemplate.nodeTypes = nodeTypes
95         serviceTemplate.artifactTypes = artifactTypes
96         serviceTemplate.dataTypes = dataTypes
97         serviceTemplate.relationshipTypes = relationshipTypes
98         serviceTemplate.topologyTemplate = topologyTemplate
99         return serviceTemplate
100     }
101 }