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