Refractor controller blueprint modules
[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             topologyTemplate {
104                 nodeTemplateOperation(
105                     nodeTemplateName = "activate", type = "sample-node-type", interfaceName = "RestconfExecutor",
106                     description = "sample activation"
107                 ) {
108                     implementation(360, "SELF") {
109                         primary("Scripts/sample.py")
110                         dependencies("one", "two")
111                     }
112                     inputs {
113                         property("json-content", """{ "name" : "cds"}""")
114                         property("array-content", """["controller", "blueprints"]""")
115                         property("int-value", 234)
116                         property("boolean-value", true)
117                         property("string-value", "sample")
118                         property("input-expression", getInput("key-1"))
119                         property("self-property-expression", getProperty("key-1"))
120                         property("self-artifact-expression", getArtifact("key-1"))
121                         property("other-artifact-expression", getNodeTemplateArtifact("node-1", "key-1"))
122                     }
123                     outputs {
124                         property("self-attribute-expression", getAttribute("key-1"))
125                     }
126                 }
127                 // Other way of defining Node Template with artifacts, implementation
128                 nodeTemplate("resolve", "sample-resolve-type", "Resource Resolution") {
129                     operation("ResourceResolutionExecutor", "") {
130                         implementation(180)
131                         inputs {
132                             property("boolean-value", true)
133                             property("string-value", "sample")
134                         }
135                         outputs {
136                             property("resolve-expression", getAttribute("key-1"))
137                         }
138                     }
139                     artifact("sample-template", "artifact-velocity", "Templates/sample-template.vtl")
140                 }
141
142                 workflow("resource-resolution", "to resolve resources") {
143                     step("resource-resolution-call", "resolve", "Resource Resolution component invoke")
144                 }
145                 // Alternate way to define workflow
146                 workflow("activate", "to resolve resources") {
147                     // Alternate step definition
148                     step("netconf-activate-call", "activate", "call activation component") {
149                         success("END")
150                         failure("END")
151                     }
152                     inputs {
153                         property("request-content", "json", true)
154                     }
155                     outputs {
156                         property("response-content", "json", true) {
157                             value(getAttribute("key-1"))
158                             defaultValue("""{ "status" : "success"}""".jsonAsJsonType())
159                         }
160                     }
161                 }
162             }
163         }
164
165         assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template")
166         assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates, "failed to get nodeTypes")
167         assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates!!["activate"], "failed to get nodeTypes(activate)")
168         // println(serviceTemplate.asJsonString(true))
169     }
170
171     @Test
172     fun testNodeTypePropertyConstrains() {
173         val nodeType = nodeType("data-node", "1.0.0", "tosca.Nodes.root", "") {
174             property("ip-address", "string", true, "") {
175                 defaultValue("127.0.0.1")
176                 constrain {
177                     validValues(arrayListOf("""127.0.0.1""".asJsonPrimitive()))
178                     length(10)
179                     maxLength(20)
180                     minLength(10)
181                 }
182             }
183             property("disk-space", "string", true, "") {
184                 defaultValue(10)
185                 constrain {
186                     validValues("""["200KB", "400KB"]""")
187                     equal("200KB")
188                     inRange("""["100KB", "500KB" ]""")
189                     maxLength("10MB")
190                     minLength("10KB")
191                 }
192                 constrain {
193                     validValues("""[ 200, 400]""")
194                     greaterOrEqual("10KB")
195                     greaterThan("20KB")
196                     lessOrEqual("200KB")
197                     lessThan("190KB")
198                 }
199             }
200         }
201         assertNotNull(nodeType, "failed to get nodeType")
202         // println(nodeType.asJsonString(true))
203     }
204
205     @Test
206     fun testServiceTemplateWorkflow() {
207         val serviceTemplate = serviceTemplate(
208             "sample-bp", "1.0.0",
209             "brindasanth@onap.com", "sample, blueprints"
210         ) {
211             topologyTemplate {
212                 workflowNodeTemplate("activate", "component-resource-resolution", "") {
213                     operation("ResourceResolutionExecutor", "") {
214                         inputs {
215                             property("string-value", "sample")
216                         }
217                     }
218                 }
219             }
220         }
221         assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template")
222         assertNotNull(serviceTemplate.topologyTemplate?.workflows?.get("activate"), "failed to get workflow(activate)")
223         // println(serviceTemplate.asJsonString(true))
224     }
225
226     @Test
227     fun testNodeTemplateOperationTypes() {
228
229         val testNodeTemplateInstance = BluePrintTypes.nodeTemplateComponentTestExecutor(
230             id = "test-node-template",
231             description = ""
232         ) {
233             definedProperties {
234                 prop1("i am property1")
235                 prop2("i am property2")
236             }
237             definedOperation("") {
238                 implementation(360)
239                 inputs {
240                     request("i am request")
241                 }
242                 outputs {
243                     response(getAttribute("attribute1"))
244                 }
245             }
246         }
247         assertNotNull(testNodeTemplateInstance, "failed to get test node template")
248         // println(testNodeTemplateInstance.asJsonString(true))
249     }
250 }
251
252 fun BluePrintTypes.nodeTemplateComponentTestExecutor(
253     id: String,
254     description: String,
255     block: TestNodeTemplateOperationImplBuilder.() -> Unit
256 ):
257         NodeTemplate {
258     return TestNodeTemplateOperationImplBuilder(id, description).apply(block).build()
259 }
260
261 class TestNodeTemplateOperationImplBuilder(id: String, description: String) :
262     AbstractNodeTemplateOperationImplBuilder<TestProperty, TestInput, TestOutput>(
263         id, "component-test-executor",
264         "ComponentTestExecutor",
265         description
266     )
267
268 class TestProperty : PropertiesAssignmentBuilder() {
269     fun prop1(prop1: String) {
270         property("prop1", prop1.asJsonPrimitive())
271     }
272
273     fun prop2(prop2: String) {
274         property("prop2", prop2.asJsonPrimitive())
275     }
276 }
277
278 class TestInput : PropertiesAssignmentBuilder() {
279     fun request(request: String) {
280         property("request", request.asJsonPrimitive())
281     }
282 }
283
284 class TestOutput : PropertiesAssignmentBuilder() {
285     fun response(response: String) {
286         response(response.asJsonPrimitive())
287     }
288
289     fun response(response: JsonNode) {
290         property("response", response)
291     }
292 }