Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BluePrintRuntimeServiceTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018-2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core.service
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.node.NullNode
22 import org.junit.Test
23 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.TestConstants
25 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
27 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintRuntimeUtils
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.slf4j.LoggerFactory
32 import kotlin.test.assertEquals
33 import kotlin.test.assertNotNull
34
35 /**
36  *
37  *
38  * @author Brinda Santh
39  */
40 class BluePrintRuntimeServiceTest {
41
42     private val log = LoggerFactory.getLogger(this::class.toString())
43
44     @Test
45     fun `test Resolve NodeTemplate Properties`() {
46         log.info("************************ testResolveNodeTemplateProperties **********************")
47
48         val bluePrintRuntimeService = getBluePrintRuntimeService()
49
50         val inputDataPath = "src/test/resources/data/default-context.json"
51
52         val inputNode: JsonNode = JacksonUtils.jsonNodeFromFile(inputDataPath)
53         bluePrintRuntimeService.assignInputs(inputNode)
54
55         val propContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
56             .resolveNodeTemplateProperties("activate-process")
57
58         assertNotNull(propContext, "Failed to populate interface property values")
59     }
60
61     @Test
62     fun `test Resolve Relationship Properties`() {
63         log.info("************************ testResolveRelationshipTemplateProperties **********************")
64
65         val bluePrintRuntimeService = getBluePrintRuntimeService()
66
67         val inputDataPath = "src/test/resources/data/default-context.json"
68
69         val inputNode: JsonNode = JacksonUtils.jsonNodeFromFile(inputDataPath)
70         bluePrintRuntimeService.assignInputs(inputNode)
71
72         val propContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
73             .resolveRelationshipTemplateProperties("cli-device-properties")
74
75         assertNotNull(propContext, "Failed to populate relationship property values")
76         assertEquals(
77             "localhost".asJsonPrimitive(),
78             propContext["connection-config"]!!.get("host"),
79             "failed to resolve expression"
80         )
81     }
82
83     @Test
84     fun `test resolve NodeTemplate Capability Properties`() {
85         log.info("************************ testResolveNodeTemplateRequirementProperties **********************")
86         val bluePrintRuntimeService = getBluePrintRuntimeService()
87
88         val executionContext = bluePrintRuntimeService.getExecutionContext()
89
90         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
91             bluePrintRuntimeService.bluePrintContext(),
92             "data/default-context.json", executionContext
93         )
94
95         val assignmentParams = "{\n" +
96             "            \"ipAddress\": \"127.0.0.1\",\n" +
97             "            \"hostName\": \"vnf-host\"\n" +
98             "          }"
99
100         bluePrintRuntimeService.setNodeTemplateAttributeValue(
101             "resource-assignment", "assignment-params",
102             JacksonUtils.jsonNode(assignmentParams)
103         )
104
105         val capProperties = bluePrintRuntimeService.resolveNodeTemplateCapabilityProperties(
106             "sample-netconf-device",
107             "netconf"
108         )
109         assertNotNull(capProperties, "Failed to populate capability property values")
110         assertEquals(
111             capProperties["target-ip-address"],
112             "127.0.0.1".asJsonPrimitive(),
113             "Failed to populate parameter target-ip-address"
114         )
115         assertEquals(
116             capProperties["port-number"],
117             JacksonUtils.jsonNodeFromObject(830),
118             "Failed to populate parameter port-number"
119         )
120     }
121
122     @Test
123     fun `test Resolve NodeTemplate Interface Operation Inputs`() {
124         log.info("************************ testResolveNodeTemplateInterfaceOperationInputs **********************")
125
126         val bluePrintRuntimeService = getBluePrintRuntimeService()
127
128         val executionContext = bluePrintRuntimeService.getExecutionContext()
129
130         BluePrintRuntimeUtils.assignInputsFromClassPathFile(
131             bluePrintRuntimeService.bluePrintContext(),
132             "data/default-context.json", executionContext
133         )
134
135         val inContext: MutableMap<String, JsonNode> = bluePrintRuntimeService
136             .resolveNodeTemplateInterfaceOperationInputs(
137                 "resource-assignment",
138                 "ResourceResolutionComponent", "process"
139             )
140
141         assertNotNull(inContext, "Failed to populate interface input property values")
142         assertEquals(
143             inContext["action-name"],
144             JacksonUtils.jsonNodeFromObject("sample-action"),
145             "Failed to populate parameter action-name"
146         )
147         assertEquals(
148             inContext["request-id"],
149             JacksonUtils.jsonNodeFromObject("12345"),
150             "Failed to populate parameter action-name"
151         )
152     }
153
154     @Test
155     fun `test Resolve NodeTemplate Interface Operation Outputs`() {
156         log.info("************************ testResolveNodeTemplateInterfaceOperationOutputs **********************")
157
158         val bluePrintRuntimeService = getBluePrintRuntimeService()
159
160         bluePrintRuntimeService.setNodeTemplateAttributeValue(
161             "resource-assignment",
162             "assignment-params",
163             NullNode.getInstance()
164         )
165
166         bluePrintRuntimeService.resolveNodeTemplateInterfaceOperationOutputs(
167             "resource-assignment",
168             "ResourceResolutionComponent", "process"
169         )
170
171         val outputStatus = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
172             "resource-assignment",
173             "ResourceResolutionComponent", "process", "status"
174         )
175         assertEquals("success".asJsonPrimitive(), outputStatus, "Failed to get operation property status")
176
177         val outputParams = bluePrintRuntimeService.getNodeTemplateOperationOutputValue(
178             "resource-assignment",
179             "ResourceResolutionComponent", "process", "resource-assignment-params"
180         )
181         assertEquals(
182             NullNode.getInstance(),
183             outputParams,
184             "Failed to get operation property resource-assignment-params"
185         )
186     }
187
188     @Test
189     fun `test NodeTemplate Context Property`() {
190         log.info("************************ testNodeTemplateContextProperty **********************")
191         val bluePrintRuntimeService = getBluePrintRuntimeService()
192
193         bluePrintRuntimeService.setNodeTemplateAttributeValue(
194             "resource-assignment-ra-component", "context1",
195             JacksonUtils.jsonNodeFromObject("context1-value")
196         )
197         bluePrintRuntimeService.setNodeTemplateAttributeValue(
198             "resource-assignment-ra-component", "context2",
199             JacksonUtils.jsonNodeFromObject("context2-value")
200         )
201
202         val keys = listOf("context1", "context2")
203
204         val jsonValueNode =
205             bluePrintRuntimeService.getJsonForNodeTemplateAttributeProperties("resource-assignment-ra-component", keys)
206         assertNotNull(jsonValueNode, "Failed to get Json for Node Template Context Properties")
207         log.info("JSON Prepared Value Context {}", jsonValueNode)
208     }
209
210     @Test
211     fun `test Resolve DSL Properties`() {
212         log.info("************************ resolveDSLExpression **********************")
213
214         val bluePrintRuntimeService = getBluePrintRuntimeService()
215
216         bluePrintRuntimeService.setInputValue("rest-user-name", "sample-username".asJsonPrimitive())
217
218         val resolvedJsonNode: JsonNode = bluePrintRuntimeService.resolveDSLExpression("dynamic-rest-source")
219         assertNotNull(resolvedJsonNode, "Failed to populate dsl property values")
220     }
221
222     @Test
223     fun `test Resolve Workflow Outputs`() {
224         log.info("************************ resolvePropertyAssignments **********************")
225         val bluePrintRuntimeService = getBluePrintRuntimeService()
226
227         val assignmentParams = "{\"ipAddress\": \"127.0.0.1\", \"hostName\": \"vnf-host\"}"
228
229         bluePrintRuntimeService.setNodeTemplateAttributeValue(
230             "resource-assignment", "assignment-params",
231             JacksonUtils.jsonNode(assignmentParams)
232         )
233
234         val resolvedJsonNode = bluePrintRuntimeService.resolveWorkflowOutputs("resource-assignment")
235         assertNotNull(resolvedJsonNode, "Failed to populate workflow output property values")
236     }
237
238     @Test
239     fun `test resolvePropertyDefinitions using sub attributes`() {
240         val bluePrintRuntimeService = getBluePrintRuntimeService()
241
242         bluePrintRuntimeService.setNodeTemplateAttributeValue(
243             "resource-assignment", "assignment-map",
244             JacksonUtils.jsonNode(
245                 """
246                     {
247                       "a-prefix":{
248                         "an-object":{
249                           "a-key":123
250                         }
251                       }
252                     }
253                 """.trimIndent()
254             )
255         )
256
257         val propertyDefinitions = mutableMapOf<String, PropertyDefinition>(
258             "resolution" to PropertyDefinition().apply {
259                 this.type = "json"
260                 this.value = JacksonUtils.jsonNode(
261                     """
262                         {
263                           "get_attribute":[
264                             "resource-assignment",
265                             "",
266                             "assignment-map",
267                             "a-prefix",
268                             "an-object",
269                             "a-key"
270                             ]
271                         }
272                     """.trimIndent()
273                 )
274             }
275         )
276
277         val result = bluePrintRuntimeService.resolvePropertyDefinitions("workflow", "WORKFLOW", propertyDefinitions)
278
279         assertEquals("123", result["resolution"]!!.asText())
280     }
281
282     private fun getBluePrintRuntimeService(): BluePrintRuntimeService<MutableMap<String, JsonNode>> {
283         val blueprintBasePath = normalizedPathName(TestConstants.PATH_TEST_BLUEPRINTS_BASECONFIG)
284         val blueprintRuntime = BluePrintMetadataUtils.bluePrintRuntime("1234", blueprintBasePath)
285         val checkProcessId = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID)
286         val checkBasePath = blueprintRuntime.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)
287
288         assertEquals(
289             "1234".asJsonPrimitive(),
290             checkProcessId, "Failed to get process id after runtime creation"
291         )
292         assertEquals(
293             blueprintBasePath.asJsonPrimitive(),
294             checkBasePath, "Failed to get base path after runtime creation"
295         )
296
297         return blueprintRuntime
298     }
299 }