Fixing Blueprint Typo's and docs
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BlueprintDSL.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.BlueprintTypes
22 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.ArtifactType
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.RelationshipType
28 import org.onap.ccsdk.cds.controllerblueprints.core.data.ServiceTemplate
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.Workflow
30 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
31
32 // CDS DSLs
33 fun blueprint(
34     name: String,
35     version: String,
36     author: String,
37     tags: String,
38     block: DSLBlueprintBuilder.() -> Unit
39 ): DSLBlueprint {
40     return DSLBlueprintBuilder(name, version, author, tags).apply(block).build()
41 }
42
43 // TOSCA DSLs
44 fun serviceTemplate(
45     name: String,
46     version: String,
47     author: String,
48     tags: String,
49     block: ServiceTemplateBuilder.() -> Unit
50 ): ServiceTemplate {
51     return ServiceTemplateBuilder(name, version, author, tags).apply(block).build()
52 }
53
54 fun workflow(id: String, description: String, block: WorkflowBuilder.() -> Unit): Workflow {
55     return WorkflowBuilder(id, description).apply(block).build()
56 }
57
58 fun nodeTemplate(
59     id: String,
60     type: String,
61     description: String,
62     block: NodeTemplateBuilder.() -> Unit
63 ): NodeTemplate {
64     return NodeTemplateBuilder(id, type, description).apply(block).build()
65 }
66
67 fun nodeType(
68     id: String,
69     version: String,
70     derivedFrom: String,
71     description: String,
72     block: NodeTypeBuilder.() -> Unit
73 ): NodeType {
74     return NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
75 }
76
77 fun dataType(
78     id: String,
79     version: String,
80     derivedFrom: String,
81     description: String,
82     block: DataTypeBuilder.() -> Unit
83 ): DataType {
84     return DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
85 }
86
87 fun artifactType(
88     id: String,
89     version: String,
90     derivedFrom: String,
91     description: String,
92     block: ArtifactTypeBuilder.() -> Unit
93 ): ArtifactType {
94     return ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
95 }
96
97 fun relationshipType(
98     id: String,
99     version: String,
100     derivedFrom: String,
101     description: String,
102     block: RelationshipTypeBuilder.() -> Unit
103 ): RelationshipType {
104     return RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build()
105 }
106
107 // DSL Function
108 fun dslExpression(key: String): JsonNode {
109     return ("*$key").asJsonPrimitive()
110 }
111 // Input Function
112
113 fun getInput(inputKey: String, jsonPath: String? = null): JsonNode {
114     return """{"get_input": "$inputKey"}""".jsonAsJsonType()
115 }
116
117 fun getAttribute(attributeId: String, jsonPath: String? = null): JsonNode {
118     return getNodeTemplateAttribute("SELF", attributeId, jsonPath)
119 }
120
121 fun getNodeTemplateAttribute(nodeTemplateId: String, attributeId: String): JsonNode {
122     return getNodeTemplateAttribute(nodeTemplateId, attributeId, null)
123 }
124
125 fun getNodeTemplateAttribute(nodeTemplateId: String, attributeId: String, jsonPath: String?): JsonNode {
126     return if (jsonPath.isNullOrEmpty() || jsonPath.isNullOrBlank()) {
127         """{"get_attribute": ["$nodeTemplateId", "$attributeId"]}""".jsonAsJsonType()
128     } else {
129         """{"get_attribute": ["$nodeTemplateId", "$attributeId", "$jsonPath"]}""".jsonAsJsonType()
130     }
131 }
132
133 // Property Function
134
135 fun getProperty(propertyId: String, jsonPath: String? = null): JsonNode {
136     return getNodeTemplateProperty("SELF", propertyId, jsonPath)
137 }
138
139 fun getNodeTemplateProperty(nodeTemplateName: String, propertyId: String): JsonNode {
140     return getNodeTemplateProperty(nodeTemplateName, propertyId, null)
141 }
142
143 fun getNodeTemplateProperty(nodeTemplateName: String, propertyId: String, jsonPath: String?): JsonNode {
144     return if (jsonPath.isNullOrEmpty() || jsonPath.isNullOrBlank()) {
145         """{"get_property": ["$nodeTemplateName", "$propertyId"]}""".jsonAsJsonType()
146     } else {
147         """{"get_property": ["$nodeTemplateName", "$propertyId", "$jsonPath"]}""".jsonAsJsonType()
148     }
149 }
150
151 // Artifact Function
152
153 fun getArtifact(artifactId: String): JsonNode {
154     return getNodeTemplateArtifact("SELF", artifactId)
155 }
156
157 fun getNodeTemplateArtifact(nodeTemplateName: String, artifactId: String): JsonNode {
158     return """{"get_artifact": ["$nodeTemplateName", "$artifactId"]}""".jsonAsJsonType()
159 }
160
161 // Operation Function
162
163 fun getNodeTemplateOperationOutput(
164     nodeTemplateName: String,
165     interfaceName: String,
166     propertyId: String,
167     jsonPath: String? = null
168 ): JsonNode {
169     return """{"get_operation_output": ["$nodeTemplateName", "$interfaceName", "process","$propertyId","$jsonPath" ]}""".trimMargin()
170         .jsonAsJsonType()
171 }
172
173 /** Blueprint Type Extensions */
174 fun ServiceTemplateBuilder.nodeTypeComponent() {
175     val nodeType = BlueprintTypes.nodeTypeComponent()
176     if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
177     this.nodeTypes!![nodeType.id!!] = nodeType
178 }
179
180 fun BlueprintTypes.nodeTypeComponent(): NodeType {
181     return nodeType(
182         id = BlueprintConstants.MODEL_TYPE_NODE_COMPONENT,
183         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
184         derivedFrom = BlueprintConstants.MODEL_TYPE_NODES_ROOT,
185         description = "This is default Component Node"
186     ) {
187     }
188 }
189
190 @Deprecated("CDS won't support, use implerative workflow definitions.")
191 fun BlueprintTypes.nodeTypeWorkflow(): NodeType {
192     return nodeType(
193         id = BlueprintConstants.MODEL_TYPE_NODE_WORKFLOW,
194         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
195         derivedFrom = BlueprintConstants.MODEL_TYPE_NODES_ROOT,
196         description = "This is default Workflow Node"
197     ) {
198     }
199 }
200
201 fun ServiceTemplateBuilder.nodeTypeVnf() {
202     val nodeType = BlueprintTypes.nodeTypeVnf()
203     if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
204     this.nodeTypes!![nodeType.id!!] = nodeType
205 }
206
207 fun BlueprintTypes.nodeTypeVnf(): NodeType {
208     return nodeType(
209         id = BlueprintConstants.MODEL_TYPE_NODE_VNF,
210         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
211         derivedFrom = BlueprintConstants.MODEL_TYPE_NODES_ROOT,
212         description = "This is default VNF Node"
213     ) {
214     }
215 }
216
217 fun ServiceTemplateBuilder.nodeTypeResourceSource() {
218     val nodeType = BlueprintTypes.nodeTypeResourceSource()
219     if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
220     this.nodeTypes!![nodeType.id!!] = nodeType
221 }
222
223 fun BlueprintTypes.nodeTypeResourceSource(): NodeType {
224     return nodeType(
225         id = BlueprintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE,
226         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
227         derivedFrom = BlueprintConstants.MODEL_TYPE_NODES_ROOT,
228         description = "This is default Resource Source Node"
229     ) {
230     }
231 }
232
233 /** Artifacts */
234 fun ServiceTemplateBuilder.artifactTypeTemplateVelocity() {
235     val artifactType = BlueprintTypes.artifactTypeTemplateVelocity()
236     if (this.artifactTypes == null) this.artifactTypes = hashMapOf()
237     this.artifactTypes!![artifactType.id!!] = artifactType
238 }
239
240 fun BlueprintTypes.artifactTypeTemplateVelocity(): ArtifactType {
241     return artifactType(
242         id = BlueprintConstants.MODEL_TYPE_ARTIFACT_TEMPLATE_VELOCITY,
243         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
244         derivedFrom = BlueprintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
245         description = "Velocity Artifact"
246     ) {
247         fileExt("vtl")
248     }
249 }
250
251 fun ServiceTemplateBuilder.artifactTypeTempleJinja() {
252     val artifactType = BlueprintTypes.artifactTypeTempleJinja()
253     if (this.artifactTypes == null) this.artifactTypes = hashMapOf()
254     this.artifactTypes!![artifactType.id!!] = artifactType
255 }
256
257 fun BlueprintTypes.artifactTypeTempleJinja(): ArtifactType {
258     return artifactType(
259         id = BlueprintConstants.MODEL_TYPE_ARTIFACT_TEMPLATE_JINJA,
260         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
261         derivedFrom = BlueprintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
262         description = "Jinja Artifact"
263     ) {
264         fileExt("jinja")
265     }
266 }
267
268 fun ServiceTemplateBuilder.artifactTypeMappingResource() {
269     val artifactType = BlueprintTypes.artifactTypeMappingResource()
270     if (this.artifactTypes == null) this.artifactTypes = hashMapOf()
271     this.artifactTypes!![artifactType.id!!] = artifactType
272 }
273
274 fun BlueprintTypes.artifactTypeMappingResource(): ArtifactType {
275     return artifactType(
276         id = BlueprintConstants.MODEL_TYPE_ARTIFACT_MAPPING_RESOURCE,
277         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
278         derivedFrom = BlueprintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
279         description = "Mapping Resource Artifact"
280     ) {
281         fileExt("json")
282     }
283 }
284
285 @Deprecated("CDS won't support", replaceWith = ReplaceWith("artifactTypeScriptKotlin"))
286 fun BlueprintTypes.artifactTypeScriptJython(): ArtifactType {
287     return artifactType(
288         id = BlueprintConstants.MODEL_TYPE_ARTIFACT_SCRIPT_JYTHON,
289         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
290         derivedFrom = BlueprintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
291         description = "Jython Script Artifact"
292     ) {
293         fileExt("py")
294     }
295 }
296
297 fun ServiceTemplateBuilder.artifactTypeScriptKotlin() {
298     val artifactType = BlueprintTypes.artifactTypeScriptKotlin()
299     if (this.artifactTypes == null) this.artifactTypes = hashMapOf()
300     this.artifactTypes!![artifactType.id!!] = artifactType
301 }
302
303 fun BlueprintTypes.artifactTypeScriptKotlin(): ArtifactType {
304     return artifactType(
305         id = BlueprintConstants.MODEL_TYPE_ARTIFACT_SCRIPT_KOTLIN,
306         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
307         derivedFrom = BlueprintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
308         description = "Kotlin Script Artifact"
309     ) {
310         fileExt("kts")
311     }
312 }
313
314 fun ServiceTemplateBuilder.artifactTypeK8sProfileFolder() {
315     val artifactType = BlueprintTypes.artifactTypeK8sProfileFolder()
316     if (this.artifactTypes == null) this.artifactTypes = hashMapOf()
317     this.artifactTypes!![artifactType.id!!] = artifactType
318 }
319
320 fun BlueprintTypes.artifactTypeK8sProfileFolder(): ArtifactType {
321     return artifactType(
322         id = BlueprintConstants.MODEL_TYPE_ARTIFACT_K8S_PROFILE,
323         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
324         derivedFrom = BlueprintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
325         description = "K8s Profile Folder Artifact"
326     ) {
327     }
328 }
329
330 @Deprecated("CDS won't support, use implerative workflow definitions.")
331 fun BlueprintTypes.artifactTypeDirectedGraph(): ArtifactType {
332     return artifactType(
333         id = BlueprintConstants.MODEL_TYPE_ARTIFACT_DIRECTED_GRAPH,
334         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
335         derivedFrom = BlueprintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
336         description = "Directed Graph Artifact"
337     ) {
338         fileExt("xml", "json")
339     }
340 }
341
342 fun ServiceTemplateBuilder.artifactTypeComponentJar() {
343     val artifactType = BlueprintTypes.artifactTypeComponentJar()
344     if (this.artifactTypes == null) this.artifactTypes = hashMapOf()
345     this.artifactTypes!![artifactType.id!!] = artifactType
346 }
347
348 fun BlueprintTypes.artifactTypeComponentJar(): ArtifactType {
349     return artifactType(
350         id = BlueprintConstants.MODEL_TYPE_ARTIFACT_COMPONENT_JAR,
351         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
352         derivedFrom = BlueprintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
353         description = "Component Artifact"
354     ) {
355         fileExt("jar")
356     }
357 }
358
359 /** Relationship Types */
360
361 fun ServiceTemplateBuilder.relationshipTypeConnectsTo() {
362     val relationshipType = BlueprintTypes.relationshipTypeConnectsTo()
363     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
364     this.relationshipTypes!![relationshipType.id!!] = relationshipType
365 }
366
367 fun BlueprintTypes.relationshipTypeConnectsTo(): RelationshipType {
368     return relationshipType(
369         id = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
370         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
371         derivedFrom = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_ROOT,
372         description = "Relationship connects to"
373     ) {
374         validTargetTypes(arrayListOf(BlueprintConstants.MODEL_TYPE_CAPABILITY_TYPE_ENDPOINT))
375     }
376 }
377
378 fun ServiceTemplateBuilder.relationshipTypeDependsOn() {
379     val relationshipType = BlueprintTypes.relationshipTypeDependsOn()
380     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
381     this.relationshipTypes!![relationshipType.id!!] = relationshipType
382 }
383
384 fun BlueprintTypes.relationshipTypeDependsOn(): RelationshipType {
385     return relationshipType(
386         id = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_DEPENDS_ON,
387         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
388         derivedFrom = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_ROOT,
389         description = "Relationship depends on"
390     ) {
391     }
392 }
393
394 fun ServiceTemplateBuilder.relationshipTypeHostedOn() {
395     val relationshipType = BlueprintTypes.relationshipTypeHostedOn()
396     if (this.relationshipTypes == null) this.relationshipTypes = hashMapOf()
397     this.relationshipTypes!![relationshipType.id!!] = relationshipType
398 }
399
400 fun BlueprintTypes.relationshipTypeHostedOn(): RelationshipType {
401     return relationshipType(
402         id = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_HOSTED_ON,
403         version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
404         derivedFrom = BlueprintConstants.MODEL_TYPE_RELATIONSHIPS_ROOT,
405         description = "Relationship hosted on"
406     ) {
407     }
408 }