Refractor controller blueprint modules
[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: BluePrintServiceDSLBuilder.() -> Unit
50 ): ServiceTemplate {
51     return BluePrintServiceDSLBuilder(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().jsonAsJsonType()
170 }
171
172 /** Blueprint Type Extensions */
173
174 fun BluePrintTypes.nodeTypeComponent(): NodeType {
175     return nodeType(
176         id = BluePrintConstants.MODEL_TYPE_NODE_COMPONENT,
177         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
178         derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT,
179         description = "This is default Component Node"
180     ) {
181     }
182 }
183
184 fun BluePrintTypes.nodeTypeWorkflow(): NodeType {
185     return nodeType(
186         id = BluePrintConstants.MODEL_TYPE_NODE_WORKFLOW,
187         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
188         derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT,
189         description = "This is default Workflow Node"
190     ) {
191     }
192 }
193
194 fun BluePrintTypes.nodeTypeVnf(): NodeType {
195     return nodeType(
196         id = BluePrintConstants.MODEL_TYPE_NODE_VNF,
197         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
198         derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT,
199         description = "This is default VNF Node"
200     ) {
201     }
202 }
203
204 fun BluePrintTypes.nodeTypeResourceSource(): NodeType {
205     return nodeType(
206         id = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE,
207         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
208         derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT,
209         description = "This is default Resource Source Node"
210     ) {
211     }
212 }
213
214 /** Artifacts */
215
216 fun BluePrintTypes.artifactTypeTemplateVelocity(): ArtifactType {
217     return artifactType(
218         id = BluePrintConstants.MODEL_TYPE_ARTIFACT_TEMPLATE_VELOCITY,
219         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
220         derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
221         description = "Velocity Artifact"
222     ) {
223         fileExt("vtl")
224     }
225 }
226
227 fun BluePrintTypes.artifactTypeTempleJinja(): ArtifactType {
228     return artifactType(
229         id = BluePrintConstants.MODEL_TYPE_ARTIFACT_TEMPLATE_JINJA,
230         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
231         derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
232         description = "Jinja Artifact"
233     ) {
234         fileExt("jinja")
235     }
236 }
237
238 fun BluePrintTypes.artifactTypeMappingResource(): ArtifactType {
239     return artifactType(
240         id = BluePrintConstants.MODEL_TYPE_ARTIFACT_MAPPING_RESOURCE,
241         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
242         derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
243         description = "Mapping Resource Artifact"
244     ) {
245         fileExt("json")
246     }
247 }
248
249 fun BluePrintTypes.artifactTypeScriptJython(): ArtifactType {
250     return artifactType(
251         id = BluePrintConstants.MODEL_TYPE_ARTIFACT_SCRIPT_JYTHON,
252         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
253         derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
254         description = "Jython Script Artifact"
255     ) {
256         fileExt("py")
257     }
258 }
259
260 fun BluePrintTypes.artifactTypeScriptKotlin(): ArtifactType {
261     return artifactType(
262         id = BluePrintConstants.MODEL_TYPE_ARTIFACT_SCRIPT_KOTLIN,
263         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
264         derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
265         description = "Kotlin Script Artifact"
266     ) {
267         fileExt("kts")
268     }
269 }
270
271 fun BluePrintTypes.artifactTypeDirectedGraph(): ArtifactType {
272     return artifactType(
273         id = BluePrintConstants.MODEL_TYPE_ARTIFACT_DIRECTED_GRAPH,
274         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
275         derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
276         description = "Directed Graph Artifact"
277     ) {
278         fileExt("xml", "json")
279     }
280 }
281
282 fun BluePrintTypes.artifactTypeComponentJar(): ArtifactType {
283     return artifactType(
284         id = BluePrintConstants.MODEL_TYPE_ARTIFACT_COMPONENT_JAR,
285         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
286         derivedFrom = BluePrintConstants.MODEL_TYPE_ARTIFACT_TYPE_IMPLEMENTATION,
287         description = "Component Artifact"
288     ) {
289         fileExt("jar")
290     }
291 }
292
293 /** Relationship Types */
294
295 fun BluePrintTypes.relationshipTypeConnectsTo(): RelationshipType {
296     return relationshipType(
297         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_CONNECTS_TO,
298         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
299         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_ROOT,
300         description = "Relationship connects to"
301     ) {
302     }
303 }
304
305 fun BluePrintTypes.relationshipTypeDependsOn(): RelationshipType {
306     return relationshipType(
307         id = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_DEPENDS_ON,
308         version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
309         derivedFrom = BluePrintConstants.MODEL_TYPE_RELATIONSHIPS_ROOT,
310         description = "Relationship depends on"
311     ) {
312     }
313 }