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("sample-bp", "1.0.0",
33 "brindasanth@onap.com", "sample, blueprints") {
35 artifactType(BluePrintTypes.artifactTypeTemplateVelocity())
37 // For New Component Definition
38 component("resource-resolution", "component-script-executor", "1.0.0",
39 "Resource Resolution component.") {
41 // Attributes ( Properties which will be set during execution)
42 attribute("template1-data", "string", true, "")
45 property("string-value1", "string", true, "sample")
46 property("string-value2", "string", true, getInput("key-1"))
48 input("json-content", "json", true, """{ "name" : "cds"}""")
49 input("template-content", "string", true, getArtifact("template1"))
51 output("self-attribute-expression", "json", true, getAttribute("template1-data"))
53 artifact("template1", "artifact-template-velocity", "Templates/template1.vtl")
56 // Already definitions Registered Components
57 registryComponent("activate-restconf", "component-resource-resolution", "1.0.0",
58 "RestconfExecutor", "Resource Resolution component.") {
61 property("string-value1", "data")
63 input("json-content", """{ "name" : "cds"}""")
65 output("self-attribute-expression", getAttribute("template1-data"))
67 artifact("template2", "artifact-template-velocity", "Templates/template1.vtl")
71 workflow("resource-resolution-process", "Resource Resolution wf") {
72 input("json-content", "json", true, "")
73 input("key-1", "string", true, "")
74 output("status", "string", true, "success")
75 step("resource-resolution-call", "resource-resolution", "Resource Resolution component invoke")
78 assertNotNull(blueprint.components, "failed to get components")
79 assertNotNull(blueprint.workflows, "failed to get workflows")
80 //println(blueprint.asJsonString(true))
82 val serviceTemplateGenerator = BluePrintServiceTemplateGenerator(blueprint)
83 val serviceTemplate = serviceTemplateGenerator.serviceTemplate()
84 assertNotNull(serviceTemplate.topologyTemplate, "failed to get service topology template")
85 //println(serviceTemplate.asJsonString(true))
89 fun testServiceTemplate() {
90 val serviceTemplate = serviceTemplate("sample-bp", "1.0.0",
91 "brindasanth@onap.com", "sample, blueprints") {
92 metadata("release", "1806")
93 import("Definition/data_types.json")
94 dsl("rest-endpoint", """{ "selector" : "odl-selector"}""")
95 dsl("db-endpoint", """{ "selector" : "db-selector"}""")
97 nodeTemplateOperation(nodeTemplateName = "activate", type = "sample-node-type", interfaceName = "RestconfExecutor",
98 description = "sample activation") {
99 implementation(360, "SELF") {
100 primary("Scripts/sample.py")
101 dependencies("one", "two")
104 property("json-content", """{ "name" : "cds"}""")
105 property("array-content", """["controller", "blueprints"]""")
106 property("int-value", 234)
107 property("boolean-value", true)
108 property("string-value", "sample")
109 property("input-expression", getInput("key-1"))
110 property("self-property-expression", getProperty("key-1"))
111 property("self-artifact-expression", getArtifact("key-1"))
112 property("other-artifact-expression", getNodeTemplateArtifact("node-1", "key-1"))
115 property("self-attribute-expression", getAttribute("key-1"))
118 // Other way of defining Node Template with artifacts, implementation
119 nodeTemplate("resolve", "sample-resolve-type", "Resource Resolution") {
120 operation("ResourceResolutionExecutor", "") {
123 property("boolean-value", true)
124 property("string-value", "sample")
127 property("resolve-expression", getAttribute("key-1"))
130 artifact("sample-template", "artifact-velocity", "Templates/sample-template.vtl")
133 workflow("resource-resolution", "to resolve resources") {
134 step("resource-resolution-call", "resolve", "Resource Resolution component invoke")
136 // Alternate way to define workflow
137 workflow("activate", "to resolve resources") {
138 // Alternate step definition
139 step("netconf-activate-call", "activate", "call activation component") {
144 property("request-content", "json", true)
147 property("response-content", "json", true) {
148 value(getAttribute("key-1"))
149 defaultValue("""{ "status" : "success"}""".jsonAsJsonType())
156 assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template")
157 assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates, "failed to get nodeTypes")
158 assertNotNull(serviceTemplate.topologyTemplate?.nodeTemplates!!["activate"], "failed to get nodeTypes(activate)")
159 //println(serviceTemplate.asJsonString(true))
163 fun testNodeTypePropertyConstrains() {
164 val nodeType = nodeType("data-node", "1.0.0", "tosca.Nodes.root", "") {
165 property("ip-address", "string", true, "") {
166 defaultValue("127.0.0.1")
168 validValues(arrayListOf("""127.0.0.1""".asJsonPrimitive()))
175 property("disk-space", "string", true, "") {
178 validValues("""["200KB", "400KB"]""")
180 inRange("""["100KB", "500KB" ]""")
185 validValues("""[ 200, 400]""")
186 greaterOrEqual("10KB")
193 assertNotNull(nodeType, "failed to get nodeType")
194 // println(nodeType.asJsonString(true))
198 fun testServiceTemplateWorkflow() {
199 val serviceTemplate = serviceTemplate("sample-bp", "1.0.0",
200 "brindasanth@onap.com", "sample, blueprints") {
202 workflowNodeTemplate("activate", "component-resource-resolution", "") {
203 operation("ResourceResolutionExecutor", "") {
205 property("string-value", "sample")
211 assertNotNull(serviceTemplate.topologyTemplate, "failed to get topology template")
212 assertNotNull(serviceTemplate.topologyTemplate?.workflows?.get("activate"), "failed to get workflow(activate)")
213 //println(serviceTemplate.asJsonString(true))
217 fun testNodeTemplateOperationTypes() {
219 val testNodeTemplateInstance = BluePrintTypes.nodeTemplateComponentTestExecutor(id = "test-node-template",
222 prop1("i am property1")
223 prop2("i am property2")
225 definedOperation("") {
228 request("i am request")
231 response(getAttribute("attribute1"))
235 assertNotNull(testNodeTemplateInstance, "failed to get test node template")
236 //println(testNodeTemplateInstance.asJsonString(true))
240 fun BluePrintTypes.nodeTemplateComponentTestExecutor(id: String,
242 block: TestNodeTemplateOperationImplBuilder.() -> Unit)
244 return TestNodeTemplateOperationImplBuilder(id, description).apply(block).build()
247 class TestNodeTemplateOperationImplBuilder(id: String, description: String) :
248 AbstractNodeTemplateOperationImplBuilder<TestProperty, TestInput, TestOutput>(id, "component-test-executor",
249 "ComponentTestExecutor",
252 class TestProperty : PropertiesAssignmentBuilder() {
253 fun prop1(prop1: String) {
254 property("prop1", prop1.asJsonPrimitive())
257 fun prop2(prop2: String) {
258 property("prop2", prop2.asJsonPrimitive())
262 class TestInput : PropertiesAssignmentBuilder() {
263 fun request(request: String) {
264 property("request", request.asJsonPrimitive())
268 class TestOutput : PropertiesAssignmentBuilder() {
269 fun response(response: String) {
270 response(response.asJsonPrimitive())
273 fun response(response: JsonNode) {
274 property("response", response)