Removed redundant timeout handling for executeCommand
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / dsl / BlueprintDSLTest.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.junit.Test
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.NodeTemplate
24 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
25 import kotlin.test.assertNotNull
26
27 class BlueprintDSLTest {
28
29     @Test
30     fun testOperationDSLWorkflow() {
31
32         val blueprint = blueprint(
33             "sample-bp", "1.0.0",
34             "brindasanth@onap.com", "sample, blueprints"
35         ) {
36
37             artifactType(BlueprintTypes.artifactTypeTemplateVelocity())
38
39             // For New Component Definition
40             component(
41                 "resource-resolution", "component-script-executor", "1.0.0",
42                 "Resource Resolution component."
43             ) {
44                 implementation(180)
45                 // Attributes ( Properties which will be set during execution)
46                 attribute("template1-data", "string", true, "")
47
48                 // Properties
49                 property("string-value1", "string", true, "sample")
50                 property("string-value2", "string", true, getInput("key-1"))
51                 // Inputs
52                 input("json-content", "json", true, """{ "name" : "cds"}""")
53                 input("template-content", "string", true, getArtifact("template1"))
54                 // Outputs
55                 output("self-attribute-expression", "json", true, getAttribute("template1-data"))
56                 // Artifacts
57                 artifact("template1", "artifact-template-velocity", "Templates/template1.vtl")
58             }
59
60             // Already definitions Registered Components
61             registryComponent(
62                 "activate-restconf", "component-resource-resolution", "1.0.0",
63                 "RestconfExecutor", "Resource Resolution component."
64             ) {
65                 implementation(180)
66                 // Properties
67                 property("string-value1", "data")
68                 // Inputs
69                 input("json-content", """{ "name" : "cds"}""")
70                 // Outputs
71                 output("self-attribute-expression", getAttribute("template1-data"))
72                 // Artifacts
73                 artifact("template2", "artifact-template-velocity", "Templates/template1.vtl")
74             }
75
76             workflow("resource-resolution-process", "Resource Resolution wf") {
77                 input("json-content", "json", true, "")
78                 input("key-1", "string", true, "")
79                 output("status", "string", true, "success")
80                 step("resource-resolution-call", "resource-resolution", "Resource Resolution component invoke")
81             }
82         }
83         assertNotNull(blueprint.components, "failed to get components")
84         assertNotNull(blueprint.workflows, "failed to get workflows")
85         // println(blueprint.asJsonString(true))
86
87         val serviceTemplateGenerator = BlueprintServiceTemplateGenerator(blueprint)
88         val serviceTemplate = serviceTemplateGenerator.serviceTemplate()
89         assertNotNull(serviceTemplate.topologyTemplate, "failed to get service topology template")
90         // println(serviceTemplate.asJsonString(true))
91     }
92
93     @Test
94     fun testServiceTemplate() {
95         val serviceTemplate = serviceTemplate(
96             "sample-bp", "1.0.0",
97             "brindasanth@onap.com", "sample, blueprints"
98         ) {
99             metadata("release", "1806")
100             import("Definition/data_types.json")
101             dsl("rest-endpoint", """{ "selector" : "odl-selector"}""")
102             dsl("db-endpoint", """{ "selector" : "db-selector"}""")
103
104             nodeTypeComponent()
105             nodeTypeResourceSource()
106             nodeTypeVnf()
107
108             artifactTypeTemplateVelocity()
109             artifactTypeTempleJinja()
110             artifactTypeScriptKotlin()
111             artifactTypeMappingResource()
112             artifactTypeComponentJar()
113             artifactTypeK8sProfileFolder()
114             artifactTypeK8sConfigFolder()
115
116             relationshipTypeConnectsTo()
117             relationshipTypeDependsOn()
118             relationshipTypeHostedOn()
119
120             topologyTemplate {
121                 nodeTemplateOperation(
122                     nodeTemplateName = "activate", type = "sample-node-type", interfaceName = "RestconfExecutor",
123                     description = "sample activation"
124                 ) {
125                     implementation(360, "SELF") {
126                         primary("Scripts/sample.py")
127                         dependencies("one", "two")
128                     }
129                     inputs {
130                         property("json-content", """{ "name" : "cds"}""")
131                         property("array-content", """["controller", "blueprints"]""")
132                         property("int-value", 234)
133                         property("boolean-value", true)
134                         property("string-value", "sample")
135                         property("input-expression", getInput("key-1"))
136                         property("self-property-expression", getProperty("key-1"))
137                         property("self-artifact-expression", getArtifact("key-1"))
138                         property("other-artifact-expression", getNodeTemplateArtifact("node-1", "key-1"))
139                     }
140                     outputs {
141                         property("self-attribute-expression", getAttribute("key-1"))
142                     }
143                 }
144                 // Other way of defining Node Template with artifacts, implementation
145                 nodeTemplate("resolve", "sample-resolve-type", "Resource Resolution") {
146                     operation("ResourceResolutionExecutor", "") {
147                         implementation(180)
148                         inputs {
149                             property("boolean-value", true)
150                             property("string-value", "sample")
151                         }
152                         outputs {
153                             property("resolve-expression", getAttribute("key-1"))
154                         }
155                     }
156                     artifact("sample-template", "artifact-velocity", "Templates/sample-template.vtl")
157                 }
158
159                 workflow("resource-resolution", "to resolve resources") {
160                     step("resource-resolution-call", "resolve", "Resource Resolution component invoke")
161                 }
162                 // Alternate way to define workflow
163                 workflow("activate", "to resolve resources") {
164                     // Alternate step definition
165                     step("netconf-activate-call", "activate", "call activation component") {
166                         success("END")
167                         failure("END")
168                     }
169                     inputs {
170                         property("request-content", "json", true)
171                     }
172                     outputs {
173                         property("response-content", "json", true) {
174                             value(getAttribute("key-1"))
175                             defaultValue("""{ "status" : "success"}""".jsonAsJsonType())
176                         }
177                     }
178                 }
179             }
180         }
181
182         // println(serviceTemplate.asJsonString(true))
183         assertNotNull(serviceTemplate.artifactTypes, "failed to get artifactTypes")
184         assertNotNull(serviceTemplate.nodeTypes, "failed to get nodeTypes")
185         assertNotNull(serviceTemplate.relationshipTypes, "failed to get relationshipTypes")
186         assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template")
187         assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates, "failed to get nodeTypes")
188         assertNotNull(
189             serviceTemplate.topologyTemplate?.nodeTemplates!!["activate"],
190             "failed to get nodeTypes(activate)"
191         )
192     }
193
194     @Test
195     fun testNodeTypePropertyConstrains() {
196         val nodeType = nodeType("data-node", "1.0.0", "tosca.Nodes.root", "") {
197             property("ip-address", "string", true, "") {
198                 defaultValue("127.0.0.1")
199                 constrain {
200                     validValues(arrayListOf("""127.0.0.1""".asJsonPrimitive()))
201                     length(10)
202                     maxLength(20)
203                     minLength(10)
204                 }
205             }
206             property("disk-space", "string", true, "") {
207                 defaultValue(10)
208                 constrain {
209                     validValues("""["200KB", "400KB"]""")
210                     equal("200KB")
211                     inRange("""["100KB", "500KB" ]""")
212                     maxLength("10MB")
213                     minLength("10KB")
214                 }
215                 constrain {
216                     validValues("""[ 200, 400]""")
217                     greaterOrEqual("10KB")
218                     greaterThan("20KB")
219                     lessOrEqual("200KB")
220                     lessThan("190KB")
221                 }
222             }
223         }
224         assertNotNull(nodeType, "failed to get nodeType")
225         // println(nodeType.asJsonString(true))
226     }
227
228     @Test
229     fun testServiceTemplateWorkflow() {
230         val serviceTemplate = serviceTemplate(
231             "sample-bp", "1.0.0",
232             "brindasanth@onap.com", "sample, blueprints"
233         ) {
234             topologyTemplate {
235                 workflowNodeTemplate("activate", "component-resource-resolution", "") {
236                     operation("ResourceResolutionExecutor", "") {
237                         inputs {
238                             property("string-value", "sample")
239                         }
240                     }
241                 }
242             }
243         }
244         assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template")
245         assertNotNull(serviceTemplate.topologyTemplate?.workflows?.get("activate"), "failed to get workflow(activate)")
246         // println(serviceTemplate.asJsonString(true))
247     }
248
249     @Test
250     fun testNodeTemplateOperationTypes() {
251
252         val testNodeTemplateInstance = BlueprintTypes.nodeTemplateComponentTestExecutor(
253             id = "test-node-template",
254             description = ""
255         ) {
256             definedProperties {
257                 prop1("i am property1")
258                 prop2("i am property2")
259             }
260             definedOperation("") {
261                 implementation(360)
262                 inputs {
263                     request("i am request")
264                 }
265                 outputs {
266                     response(getAttribute("attribute1"))
267                 }
268             }
269         }
270         assertNotNull(testNodeTemplateInstance, "failed to get test node template")
271         // println(testNodeTemplateInstance.asJsonString(true))
272     }
273 }
274
275 fun BlueprintTypes.nodeTemplateComponentTestExecutor(
276     id: String,
277     description: String,
278     block: TestNodeTemplateOperationImplBuilder.() -> Unit
279 ):
280     NodeTemplate {
281         return TestNodeTemplateOperationImplBuilder(id, description).apply(block).build()
282     }
283
284 class TestNodeTemplateOperationImplBuilder(id: String, description: String) :
285     AbstractNodeTemplateOperationImplBuilder<TestProperty, TestInput, TestOutput>(
286         id, "component-test-executor",
287         "ComponentTestExecutor",
288         description
289     )
290
291 class TestProperty : PropertiesAssignmentBuilder() {
292
293     fun prop1(prop1: String) {
294         property("prop1", prop1.asJsonPrimitive())
295     }
296
297     fun prop2(prop2: String) {
298         property("prop2", prop2.asJsonPrimitive())
299     }
300 }
301
302 class TestInput : PropertiesAssignmentBuilder() {
303
304     fun request(request: String) {
305         property("request", request.asJsonPrimitive())
306     }
307 }
308
309 class TestOutput : PropertiesAssignmentBuilder() {
310
311     fun response(response: String) {
312         response(response.asJsonPrimitive())
313     }
314
315     fun response(response: JsonNode) {
316         property("response", response)
317     }
318 }