Merge "Resource resolution should return a string"
[ccsdk/cds.git] / ms / controllerblueprints / modules / 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.*
24 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
25
26 // CDS DSLs
27 fun blueprint(name: String, version: String, author: String, tags: String,
28               block: DSLBluePrintBuilder.() -> Unit): DSLBluePrint {
29     return DSLBluePrintBuilder(name, version, author, tags).apply(block).build()
30 }
31
32 // TOSCA DSLs
33 fun serviceTemplate(name: String, version: String, author: String, tags: String,
34                     block: ServiceTemplateBuilder.() -> Unit): ServiceTemplate {
35     return ServiceTemplateBuilder(name, version, author, tags).apply(block).build()
36 }
37
38 fun workflow(id: String, description: String, block: WorkflowBuilder.() -> Unit): Workflow {
39     return WorkflowBuilder(id, description).apply(block).build()
40 }
41
42 fun nodeTemplate(id: String, type: String, description: String,
43                  block: NodeTemplateBuilder.() -> Unit): NodeTemplate {
44     return NodeTemplateBuilder(id, type, description).apply(block).build()
45 }
46
47 fun nodeType(id: String, version: String, derivedFrom: String, description: String,
48              block: NodeTypeBuilder.() -> Unit): NodeType {
49     return NodeTypeBuilder(id, version, derivedFrom, description).apply(block).build()
50 }
51
52 fun dataType(id: String, version: String, derivedFrom: String, description: String,
53              block: DataTypeBuilder.() -> Unit): DataType {
54     return DataTypeBuilder(id, version, derivedFrom, description).apply(block).build()
55 }
56
57 fun artifactType(id: String, version: String, derivedFrom: String, description: String,
58                  block: ArtifactTypeBuilder.() -> Unit): ArtifactType {
59     return ArtifactTypeBuilder(id, version, derivedFrom, description).apply(block).build()
60 }
61
62 fun relationshipType(id: String, version: String, derivedFrom: String, description: String,
63                      block: RelationshipTypeBuilder.() -> Unit): RelationshipType {
64     return RelationshipTypeBuilder(id, version, derivedFrom, description).apply(block).build()
65 }
66
67 // DSL Function
68 fun dslExpression(key: String): JsonNode {
69     return ("*$key").asJsonPrimitive()
70 }
71 // Input Function
72
73 fun getInput(inputKey: String, jsonPath: String? = null): JsonNode {
74     return """{"get_input": "$inputKey"}""".jsonAsJsonType()
75 }
76
77 fun getAttribute(attributeId: String, jsonPath: String? = null): JsonNode {
78     return getNodeTemplateAttribute("SELF", attributeId, jsonPath)
79 }
80
81 fun getNodeTemplateAttribute(nodeTemplateId: String, attributeId: String): JsonNode {
82     return getNodeTemplateAttribute(nodeTemplateId, attributeId, null)
83 }
84
85 fun getNodeTemplateAttribute(nodeTemplateId: String, attributeId: String, jsonPath: String?): JsonNode {
86     return if (jsonPath.isNullOrEmpty() || jsonPath.isNullOrBlank()) {
87         """{"get_attribute": ["$nodeTemplateId", "$attributeId"]}""".jsonAsJsonType()
88     } else {
89         """{"get_attribute": ["$nodeTemplateId", "$attributeId", "$jsonPath"]}""".jsonAsJsonType()
90     }
91 }
92
93 // Property Function
94
95 fun getProperty(propertyId: String, jsonPath: String? = null): JsonNode {
96     return getNodeTemplateProperty("SELF", propertyId, jsonPath)
97 }
98
99 fun getNodeTemplateProperty(nodeTemplateName: String, propertyId: String): JsonNode {
100     return getNodeTemplateProperty(nodeTemplateName, propertyId, null)
101 }
102
103 fun getNodeTemplateProperty(nodeTemplateName: String, propertyId: String, jsonPath: String?): JsonNode {
104     return if (jsonPath.isNullOrEmpty() || jsonPath.isNullOrBlank()) {
105         """{"get_property": ["$nodeTemplateName", "$propertyId"]}""".jsonAsJsonType()
106     } else {
107         """{"get_property": ["$nodeTemplateName", "$propertyId", "$jsonPath"]}""".jsonAsJsonType()
108     }
109 }
110
111 // Artifact Function
112
113 fun getArtifact(artifactId: String): JsonNode {
114     return getNodeTemplateArtifact("SELF", artifactId)
115 }
116
117 fun getNodeTemplateArtifact(nodeTemplateName: String, artifactId: String): JsonNode {
118     return """{"get_artifact": ["$nodeTemplateName", "$artifactId"]}""".jsonAsJsonType()
119 }
120
121 // Operation Function
122
123 fun getNodeTemplateOperationOutput(nodeTemplateName: String, interfaceName: String, propertyId: String,
124                                    jsonPath: String? = null): JsonNode {
125     return """{"get_operation_output": ["$nodeTemplateName", "$interfaceName", "process","$propertyId","$jsonPath" ]}""".trimMargin().jsonAsJsonType()
126 }
127
128 /** Blueprint Type Extensions */
129
130 fun BluePrintTypes.nodeTypeComponent(): NodeType {
131     return nodeType(id = BluePrintConstants.MODEL_TYPE_NODE_COMPONENT,
132             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
133             derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT,
134             description = "This is default Component Node") {
135     }
136 }
137
138 fun BluePrintTypes.nodeTypeWorkflow(): NodeType {
139     return nodeType(id = BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW,
140             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
141             derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT,
142             description = "This is default Workflow Node") {
143     }
144 }
145
146 fun BluePrintTypes.nodeTypeVnf(): NodeType {
147     return nodeType(id = BluePrintConstants.MODEL_TYPE_NODE_VNF,
148             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
149             derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT,
150             description = "This is default VNF Node") {
151     }
152 }
153
154 fun BluePrintTypes.nodeTypeResourceSource(): NodeType {
155     return nodeType(id = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE,
156             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
157             derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT,
158             description = "This is default Resource Source Node") {
159     }
160 }
161
162 /** Artifacts */
163
164 fun BluePrintTypes.artifactTypeTemplateVelocity(): ArtifactType {
165     return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_TEMPLATE_VELOCITY,
166             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
167             derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
168             description = "Velocity Artifact") {
169         fileExt("vtl")
170     }
171 }
172
173 fun BluePrintTypes.artifactTypeTempleJinja(): ArtifactType {
174     return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_TEMPLATE_JINJA,
175             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
176             derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
177             description = "Jinja Artifact") {
178         fileExt("jinja")
179     }
180 }
181
182 fun BluePrintTypes.artifactTypeMappingResource(): ArtifactType {
183     return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_MAPPING_RESOURCE,
184             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
185             derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
186             description = "Mapping Resource Artifact") {
187         fileExt("json")
188     }
189 }
190
191 fun BluePrintTypes.artifactTypeScriptJython(): ArtifactType {
192     return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_SCRIPT_JYTHON,
193             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
194             derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
195             description = "Jython Script Artifact") {
196         fileExt("py")
197     }
198 }
199
200 fun BluePrintTypes.artifactTypeScriptKotlin(): ArtifactType {
201     return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_SCRIPT_KOTLIN,
202             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
203             derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
204             description = "Kotlin Script Artifact") {
205         fileExt("kts")
206     }
207 }
208
209 fun BluePrintTypes.artifactTypeDirectedGraph(): ArtifactType {
210     return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_DIRECTED_GRAPH,
211             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
212             derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
213             description = "Directed Graph Artifact") {
214         fileExt("xml", "json")
215     }
216 }
217
218 fun BluePrintTypes.artifactTypeComponentJar(): ArtifactType {
219     return artifactType(id = BluePrintConstants.MODEL_TYPE_ARTIFACT_COMPONENT_JAR,
220             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
221             derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
222             description = "Component Artifact") {
223         fileExt("jar")
224     }
225 }
226
227 /** Relationship Types */
228
229 fun BluePrintTypes.relationshipTypeConnectsTo(): RelationshipType {
230     return relationshipType(id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
231             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
232             derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_ROOT,
233             description = "Relationship connects to") {
234     }
235 }
236
237 fun BluePrintTypes.relationshipTypeDependsOn(): RelationshipType {
238     return relationshipType(id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_DEPENDS_ON,
239             version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
240             derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_ROOT,
241             description = "Relationship depends on") {
242     }
243 }