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