2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.core.dsl
19 import com.fasterxml.jackson.databind.JsonNode
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
27 class BluePrintDSLTest {
30 fun testOperationDSLWorkflow() {
32 val blueprint = blueprint(
34 "brindasanth@onap.com", "sample, blueprints"
37 artifactType(BluePrintTypes.artifactTypeTemplateVelocity())
39 // For New Component Definition
41 "resource-resolution", "component-script-executor", "1.0.0",
42 "Resource Resolution component."
45 // Attributes ( Properties which will be set during execution)
46 attribute("template1-data", "string", true, "")
49 property("string-value1", "string", true, "sample")
50 property("string-value2", "string", true, getInput("key-1"))
52 input("json-content", "json", true, """{ "name" : "cds"}""")
53 input("template-content", "string", true, getArtifact("template1"))
55 output("self-attribute-expression", "json", true, getAttribute("template1-data"))
57 artifact("template1", "artifact-template-velocity", "Templates/template1.vtl")
60 // Already definitions Registered Components
62 "activate-restconf", "component-resource-resolution", "1.0.0",
63 "RestconfExecutor", "Resource Resolution component."
67 property("string-value1", "data")
69 input("json-content", """{ "name" : "cds"}""")
71 output("self-attribute-expression", getAttribute("template1-data"))
73 artifact("template2", "artifact-template-velocity", "Templates/template1.vtl")
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")
83 assertNotNull(blueprint.components, "failed to get components")
84 assertNotNull(blueprint.workflows, "failed to get workflows")
85 // println(blueprint.asJsonString(true))
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))
94 fun testServiceTemplate() {
95 val serviceTemplate = serviceTemplate(
97 "brindasanth@onap.com", "sample, blueprints"
99 metadata("release", "1806")
100 import("Definition/data_types.json")
101 dsl("rest-endpoint", """{ "selector" : "odl-selector"}""")
102 dsl("db-endpoint", """{ "selector" : "db-selector"}""")
104 nodeTemplateOperation(
105 nodeTemplateName = "activate", type = "sample-node-type", interfaceName = "RestconfExecutor",
106 description = "sample activation"
108 implementation(360, "SELF") {
109 primary("Scripts/sample.py")
110 dependencies("one", "two")
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"))
124 property("self-attribute-expression", getAttribute("key-1"))
127 // Other way of defining Node Template with artifacts, implementation
128 nodeTemplate("resolve", "sample-resolve-type", "Resource Resolution") {
129 operation("ResourceResolutionExecutor", "") {
132 property("boolean-value", true)
133 property("string-value", "sample")
136 property("resolve-expression", getAttribute("key-1"))
139 artifact("sample-template", "artifact-velocity", "Templates/sample-template.vtl")
142 workflow("resource-resolution", "to resolve resources") {
143 step("resource-resolution-call", "resolve", "Resource Resolution component invoke")
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") {
153 property("request-content", "json", true)
156 property("response-content", "json", true) {
157 value(getAttribute("key-1"))
158 defaultValue("""{ "status" : "success"}""".jsonAsJsonType())
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))
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")
177 validValues(arrayListOf("""127.0.0.1""".asJsonPrimitive()))
183 property("disk-space", "string", true, "") {
186 validValues("""["200KB", "400KB"]""")
188 inRange("""["100KB", "500KB" ]""")
193 validValues("""[ 200, 400]""")
194 greaterOrEqual("10KB")
201 assertNotNull(nodeType, "failed to get nodeType")
202 // println(nodeType.asJsonString(true))
206 fun testServiceTemplateWorkflow() {
207 val serviceTemplate = serviceTemplate(
208 "sample-bp", "1.0.0",
209 "brindasanth@onap.com", "sample, blueprints"
212 workflowNodeTemplate("activate", "component-resource-resolution", "") {
213 operation("ResourceResolutionExecutor", "") {
215 property("string-value", "sample")
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))
227 fun testNodeTemplateOperationTypes() {
229 val testNodeTemplateInstance = BluePrintTypes.nodeTemplateComponentTestExecutor(
230 id = "test-node-template",
234 prop1("i am property1")
235 prop2("i am property2")
237 definedOperation("") {
240 request("i am request")
243 response(getAttribute("attribute1"))
247 assertNotNull(testNodeTemplateInstance, "failed to get test node template")
248 // println(testNodeTemplateInstance.asJsonString(true))
252 fun BluePrintTypes.nodeTemplateComponentTestExecutor(
255 block: TestNodeTemplateOperationImplBuilder.() -> Unit
258 return TestNodeTemplateOperationImplBuilder(id, description).apply(block).build()
261 class TestNodeTemplateOperationImplBuilder(id: String, description: String) :
262 AbstractNodeTemplateOperationImplBuilder<TestProperty, TestInput, TestOutput>(
263 id, "component-test-executor",
264 "ComponentTestExecutor",
268 class TestProperty : PropertiesAssignmentBuilder() {
269 fun prop1(prop1: String) {
270 property("prop1", prop1.asJsonPrimitive())
273 fun prop2(prop2: String) {
274 property("prop2", prop2.asJsonPrimitive())
278 class TestInput : PropertiesAssignmentBuilder() {
279 fun request(request: String) {
280 property("request", request.asJsonPrimitive())
284 class TestOutput : PropertiesAssignmentBuilder() {
285 fun response(response: String) {
286 response(response.asJsonPrimitive())
289 fun response(response: JsonNode) {
290 property("response", response)