c9f7d507c136c517676142ca79bc942a9814e233
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BluePrintServiceDSLBuilder.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 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> = mutableListOf()
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     private var policyTypes: MutableMap<String, PolicyType>? = null
38
39     private fun initMetaData() {
40         metadata[BluePrintConstants.METADATA_TEMPLATE_NAME] = name
41         metadata[BluePrintConstants.METADATA_TEMPLATE_VERSION] = version
42         metadata[BluePrintConstants.METADATA_TEMPLATE_AUTHOR] = author
43         metadata[BluePrintConstants.METADATA_TEMPLATE_TAGS] = tags
44     }
45
46     fun metadata(id: String, value: String) {
47         metadata[id] = value
48     }
49
50     fun import(file: String) {
51         val importDefinition = ImportDefinition().apply {
52             this.file = file
53         }
54         imports.add(importDefinition)
55     }
56
57     fun dsl(id: String, content: Any) {
58         dsl(id, content.asJsonType())
59     }
60
61     fun dsl(id: String, json: JsonNode) {
62         if (dslDefinitions == null)
63             dslDefinitions = hashMapOf()
64         dslDefinitions!![id] = json
65     }
66
67     fun dataTypes(dataTypes: MutableMap<String, DataType>) {
68         if (this.dataTypes == null)
69             this.dataTypes = hashMapOf()
70
71         this.dataTypes!!.putAll(dataTypes)
72     }
73
74     fun artifactTypes(artifactTypes: MutableMap<String, ArtifactType>) {
75         if (this.artifactTypes == null)
76             this.artifactTypes = hashMapOf()
77
78         this.artifactTypes!!.putAll(artifactTypes)
79     }
80
81     fun relationshipTypes(relationshipTypes: MutableMap<String, RelationshipType>) {
82         if (this.relationshipTypes == null)
83             this.relationshipTypes = hashMapOf()
84
85         this.relationshipTypes!!.putAll(relationshipTypes)
86     }
87
88     fun policyTypes(policyTypes: MutableMap<String, PolicyType>) {
89         if (this.policyTypes == null)
90             this.policyTypes = hashMapOf()
91
92         this.policyTypes!!.putAll(policyTypes)
93     }
94
95     fun nodeType(nodeTypes: MutableMap<String, NodeType>) {
96         if (this.nodeTypes == null)
97             this.nodeTypes = hashMapOf()
98
99         this.nodeTypes!!.putAll(nodeTypes)
100     }
101
102     fun dataType(dataType: DataType) {
103         if (dataTypes == null)
104             dataTypes = hashMapOf()
105         dataTypes!![dataType.id!!] = dataType
106     }
107
108     fun artifactType(artifactType: ArtifactType) {
109         if (artifactTypes == null)
110             artifactTypes = hashMapOf()
111         artifactTypes!![artifactType.id!!] = artifactType
112     }
113
114     fun relationshipType(relationshipType: RelationshipType) {
115         if (relationshipTypes == null)
116             relationshipTypes = hashMapOf()
117         relationshipTypes!![relationshipType.id!!] = relationshipType
118     }
119
120     fun policyType(policyType: PolicyType) {
121         if (policyTypes == null)
122             policyTypes = hashMapOf()
123
124         policyTypes!![policyType.id!!] = policyType
125     }
126
127     fun nodeType(nodeType: NodeType) {
128         if (nodeTypes == null)
129             nodeTypes = hashMapOf()
130         nodeTypes!![nodeType.id!!] = nodeType
131     }
132
133     fun dataType(id: String, version: String, derivedFrom: String, description: String,
134                  block: DataTypeBuilder.() -> Unit) {
135         if (dataTypes == null)
136             dataTypes = hashMapOf()
137         dataTypes!![id] = DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
138     }
139
140     fun artifactType(id: String, version: String, derivedFrom: String, description: String,
141                      block: ArtifactTypeBuilder.() -> Unit) {
142         if (artifactTypes == null)
143             artifactTypes = hashMapOf()
144         artifactTypes!![id] = ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
145     }
146
147     fun relationshipType(id: String, version: String, derivedFrom: String, description: String,
148                          block: RelationshipTypeBuilder.() -> Unit) {
149         if (relationshipTypes == null)
150             relationshipTypes = hashMapOf()
151         relationshipTypes!![id] = RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build()
152     }
153
154     fun policyType(id: String, version: String, derivedFrom: String, description: String,
155                    block: PolicyTypeBuilder.() -> Unit) {
156         if (policyTypes == null)
157             policyTypes = hashMapOf()
158         policyTypes!![id] = PolicyTypeBuilder(id, version, derivedFrom, description).apply(block).build()
159     }
160
161     fun nodeType(id: String, version: String, derivedFrom: String, description: String,
162                  block: NodeTypeBuilder.() -> Unit) {
163         if (nodeTypes == null)
164             nodeTypes = hashMapOf()
165         nodeTypes!![id] = NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
166     }
167
168     fun topologyTemplate(block: TopologyTemplateBuilder.() -> Unit) {
169         topologyTemplate = TopologyTemplateBuilder().apply(block).build()
170     }
171
172     fun build(): ServiceTemplate {
173         initMetaData()
174         serviceTemplate.metadata = metadata
175         serviceTemplate.imports = imports
176         serviceTemplate.dslDefinitions = dslDefinitions
177         serviceTemplate.nodeTypes = nodeTypes
178         serviceTemplate.artifactTypes = artifactTypes
179         serviceTemplate.dataTypes = dataTypes
180         serviceTemplate.relationshipTypes = relationshipTypes
181         serviceTemplate.policyTypes = policyTypes
182         serviceTemplate.topologyTemplate = topologyTemplate
183         return serviceTemplate
184     }
185 }