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