2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (c) 2019 IBM, Bell Canada.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.utils
24 import com.fasterxml.jackson.databind.JsonNode
25 import com.fasterxml.jackson.databind.node.TextNode
28 import kotlinx.coroutines.runBlocking
29 import org.junit.Before
31 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
32 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.METADATA_TRANSFORM_TEMPLATE
33 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
34 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
35 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
36 import org.onap.ccsdk.cds.controllerblueprints.core.data.EntrySchema
37 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
38 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
40 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
41 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
42 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
43 import kotlin.test.assertEquals
45 data class IpAddress(val port: String, val ip: String)
46 data class Host(val name: String, val ipAddress: IpAddress)
47 data class ExpectedResponseIp(val ip: String)
48 data class ExpectedResponseIpAddress(val ipAddress: IpAddress)
50 class ResourceAssignmentUtilsTest {
52 private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
53 private lateinit var resourceAssignment: ResourceAssignment
55 private lateinit var inputMapToTestPrimitiveTypeWithValue: JsonNode
56 private lateinit var inputMapToTestPrimitiveTypeWithKeyValue: JsonNode
57 private lateinit var inputMapToTestCollectionOfPrimitiveType: JsonNode
58 private lateinit var inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
59 private lateinit var inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
60 private lateinit var inputMapToTestComplexTypeWithOneOutputKeyMapping: JsonNode
61 private lateinit var inputMapToTestComplexTypeWithAllOutputKeyMapping: JsonNode
62 private lateinit var expectedValueToTestPrimitiveType: JsonNode
63 private lateinit var expectedValueToTesCollectionOfPrimitiveType: JsonNode
64 private lateinit var expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
65 private lateinit var expectedValueToTestComplexTypeWithOneOutputKeyMapping: JsonNode
66 private lateinit var expectedValueToTestComplexTypeWithAllOutputKeyMapping: JsonNode
67 private lateinit var expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
72 val bluePrintContext = runBlocking {
73 BluePrintMetadataUtils.getBluePrintContext(
74 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
78 resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
80 // Init input map and expected values for tests
81 initInputMapAndExpectedValuesForPrimitiveType()
82 initInputMapAndExpectedValuesForCollection()
83 initInputMapAndExpectedValuesForComplexType()
85 val propertiesDefinition1 = PropertyDefinition().apply {
90 val propertiesDefinition2 = PropertyDefinition().apply {
95 val propertiesDefinition3 = PropertyDefinition().apply {
100 val propertiesDefinition4 = PropertyDefinition().apply {
105 val mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
106 mapOfPropertiesIpAddress["port"] = propertiesDefinition1
107 mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
109 val mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
110 mapOfPropertiesHost["name"] = propertiesDefinition3
111 mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
113 val myDataTypeIpaddress = DataType().apply {
115 properties = mapOfPropertiesIpAddress
118 val myDataTypeHost = DataType().apply {
120 properties = mapOfPropertiesHost
124 resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
125 } returns myDataTypeIpaddress
127 every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
129 every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
133 fun `generateResourceDataForAssignments - positive test`() {
134 // given a valid resource assignment
135 val validResourceAssignment1 = createResourceAssignmentForTest("valid_value", "pnf-id")
136 val validResourceAssignment2 = createResourceAssignmentForTest("also_valid", "a1")
138 // and a list containing that resource assignment
139 val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment1, validResourceAssignment2)
141 // when the values of the resources are evaluated
142 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
144 // then the assignment should produce a valid result
148 "pnf-id" : "valid_value"
151 assertEquals(expected, outcome.trimIndent(), "unexpected outcome generated")
155 fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
156 // given a valid resource assignment
157 val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
159 // and a list containing that resource assignment
160 val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
162 // when the values of the resources are evaluated
163 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
165 // then the assignment should produce a valid result
166 val expected = "{\n" + " \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
167 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
171 fun generateResolutionSummaryDataTest() {
172 val resourceAssignment = createResourceAssignmentForTest(null)
173 val resourceDefinition = ResourceDefinition()
174 val nodeTemplate = NodeTemplate().apply {
175 properties = mutableMapOf("resolved-payload" to JacksonUtils.jsonNode("{\"mock\": true}"))
177 resourceDefinition.sources = mutableMapOf("input" to nodeTemplate)
178 resourceDefinition.property = PropertyDefinition().apply {
179 this.description = "pnf-id"
180 this.metadata = mutableMapOf("aai-path" to "//path/in/aai")
183 val result = ResourceAssignmentUtils.generateResolutionSummaryData(
184 listOf(resourceAssignment), mapOf("pnf-id" to resourceDefinition)
190 "resolution-summary":[
196 "key-identifiers":[],
197 "dictionary-description":"pnf-id",
198 "dictionary-metadata":[
199 {"name":"aai-path","value":"//path/in/aai"}
201 "dictionary-name":"pnf-id",
202 "dictionary-source":"input",
203 "request-payload":{"mock":true},
209 """.replace("\n|\\s".toRegex(), ""),
214 private fun createResourceAssignmentForTest(resourceValue: String?, resourceName: String = "pnf-id"): ResourceAssignment {
215 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
216 val resourceAssignmentForTest = ResourceAssignment().apply {
218 dictionaryName = "pnf-id"
219 dictionarySource = "input"
220 property = PropertyDefinition().apply {
225 return resourceAssignmentForTest
229 fun parseResponseNodeTestForPrimitivesTypes() {
230 var outcome = prepareResponseNodeForTest(
231 "sample-value", "string", "",
232 inputMapToTestPrimitiveTypeWithValue
235 expectedValueToTestPrimitiveType,
237 "Unexpected outcome returned for primitive type of simple String"
239 assertEquals(0, resourceAssignment.keyIdentifiers.size)
241 outcome = prepareResponseNodeForTest(
242 "sample-key-value", "string", "",
243 inputMapToTestPrimitiveTypeWithKeyValue
246 expectedValueToTestPrimitiveType,
248 "Unexpected outcome returned for primitive type of key-value String"
251 expectedValueToTestPrimitiveType,
252 resourceAssignment.keyIdentifiers[0].value
257 fun parseResponseNodeTestForCollectionsOfString() {
258 var outcome = prepareResponseNodeForTest(
259 "listOfString", "list",
260 "string", inputMapToTestCollectionOfPrimitiveType
263 expectedValueToTesCollectionOfPrimitiveType,
265 "unexpected outcome returned for list of String"
268 val expectedKeyIdentifierValue = JacksonUtils.getJsonNode(outcome.map { it["ip"] })
270 expectedKeyIdentifierValue,
271 resourceAssignment.keyIdentifiers[0].value
274 // FIXME("Map is not collection type, It is known complex type")
275 // outcome = prepareResponseNodeForTest(
276 // "mapOfString", "map", "string",
277 // inputMapToTestCollectionOfPrimitiveType
280 // expectedValueToTesCollectionOfPrimitiveType,
282 // "unexpected outcome returned for map of String"
287 fun parseResponseNodeTestForCollectionsOfComplexType() {
288 var outcome = prepareResponseNodeForTest(
289 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
290 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
293 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
295 "unexpected outcome returned for list of String"
298 outcome = prepareResponseNodeForTest(
299 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
300 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
303 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
305 "unexpected outcome returned for list of String"
310 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
311 val outcome = prepareResponseNodeForTest(
312 "complexTypeOneKeys", "host",
313 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
316 expectedValueToTestComplexTypeWithOneOutputKeyMapping,
318 "Unexpected outcome returned for complex type"
321 expectedValueToTestComplexTypeWithOneOutputKeyMapping["host"],
322 resourceAssignment.keyIdentifiers[0].value
327 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
328 val outcome = prepareResponseNodeForTest(
329 "complexTypeAllKeys", "host",
330 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
333 expectedValueToTestComplexTypeWithAllOutputKeyMapping,
335 "Unexpected outcome returned for complex type"
337 assertEquals(2, resourceAssignment.keyIdentifiers.size)
339 expectedValueToTestComplexTypeWithAllOutputKeyMapping["name"],
340 resourceAssignment.keyIdentifiers[0].value
344 expectedValueToTestComplexTypeWithAllOutputKeyMapping["ipAddress"],
345 resourceAssignment.keyIdentifiers[1].value
350 fun `transform resolved value with inline template`() {
351 resourceAssignmentRuntimeService.putResolutionStore("vnf_name", "abc-vnf".asJsonType())
352 resourceAssignment = ResourceAssignment()
353 resourceAssignment.name = "int_pktgen_private_net_id"
354 resourceAssignment.property = PropertyDefinition()
355 resourceAssignment.property!!.type = "string"
356 val value = "".asJsonType()
358 // Enable transform template
359 resourceAssignment.property!!.metadata =
360 mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_name}_private2")
362 ResourceAssignmentUtils
363 .setResourceDataValue(resourceAssignment, resourceAssignmentRuntimeService, value)
365 val valueJson = "{\"config\":{\"parameter\":\"address\",\"value\":\"0.0.0.0\"}}"
366 resourceAssignmentRuntimeService.putResolutionStore("vnf_config", JacksonUtils.objectMapper.readTree(valueJson))
367 val resourceAssignmentJson = ResourceAssignment()
368 resourceAssignmentJson.name = "vendor_vnf_configuration"
369 resourceAssignmentJson.property = PropertyDefinition()
370 resourceAssignmentJson.property!!.type = "json"
372 // Enable transform template
373 resourceAssignmentJson.property!!.metadata =
374 mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_config}")
376 ResourceAssignmentUtils
377 .setResourceDataValue(resourceAssignmentJson, resourceAssignmentRuntimeService, JacksonUtils.objectMapper.createObjectNode())
381 resourceAssignment.property!!.value!!.asText()
386 resourceAssignmentJson.property!!.value!!.toString()
390 private fun initInputMapAndExpectedValuesForPrimitiveType() {
391 inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
392 val keyValue = mutableMapOf<String, String>()
393 keyValue["value"] = "1.2.3.1"
394 inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
395 expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
398 private fun initInputMapAndExpectedValuesForCollection() {
399 val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
400 val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
402 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
403 arrayChildNode.set<JsonNode>("value", it.asJsonPrimitive())
404 arrayNodeForList1.add(arrayChildNode)
406 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
408 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
409 ExpectedResponseIp("1.2.3.1"),
410 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
413 val listOfIpAddresses = arrayListOf(
414 IpAddress("1111", "1.2.3.1").asJsonType(),
415 IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
417 val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
418 listOfIpAddresses.forEach {
419 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
420 arrayChildNode.set<JsonNode>("value", it.asJsonType())
421 arrayNodeForList2.add(arrayChildNode)
423 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
425 val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
426 var childNode = JacksonUtils.objectMapper.createObjectNode()
427 childNode.set<JsonNode>("port", "1111".asJsonPrimitive())
428 childNode.set<JsonNode>("ip", "1.2.3.1".asJsonPrimitive())
429 arrayNodeForList3.add(childNode)
430 childNode = JacksonUtils.objectMapper.createObjectNode()
431 childNode.set<JsonNode>("port", "2222".asJsonPrimitive())
432 childNode.set<JsonNode>("ip", "1.2.3.2".asJsonPrimitive())
433 arrayNodeForList3.add(childNode)
434 childNode = JacksonUtils.objectMapper.createObjectNode()
435 childNode.set<JsonNode>("port", "3333".asJsonPrimitive())
436 childNode.set<JsonNode>("ip", "1.2.3.3".asJsonPrimitive())
437 arrayNodeForList3.add(childNode)
438 inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
440 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
441 ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
442 ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")),
443 ExpectedResponseIpAddress(
444 IpAddress("3333", "1.2.3.3")
447 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
448 IpAddress("1111", "1.2.3.1"),
449 IpAddress("2222", "1.2.3.2"),
450 IpAddress("3333", "1.2.3.3")
454 private fun initInputMapAndExpectedValuesForComplexType() {
455 val mapOfComplexType = mutableMapOf<String, JsonNode>()
456 mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
457 mapOfComplexType["port"] = "8888".asJsonType()
458 mapOfComplexType["something"] = "1.2.3.2".asJsonType()
459 inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
461 val objectNode = JacksonUtils.objectMapper.createObjectNode()
462 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
463 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
465 val childNode1 = JacksonUtils.objectMapper.createObjectNode()
466 childNode1.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
467 childNode1.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
468 childNode1.set<JsonNode>("port", "8888".asJsonType())
469 childNode1.set<JsonNode>("something", IpAddress("2222", "1.2.3.1").asJsonType())
470 inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
472 val childNode2 = JacksonUtils.objectMapper.createObjectNode()
473 childNode2.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
474 childNode2.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
475 expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
478 private fun prepareResponseNodeForTest(
479 dictionary_source: String,
485 resourceAssignment = when (sourceType) {
487 prepareRAResourceDictionaryCollection(dictionary_source, sourceType, entrySchema)
490 prepareRAResourceDictionaryOfPrimaryType(dictionary_source)
493 prepareRAResourceDictionaryComplexType(dictionary_source, sourceType, entrySchema)
497 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
498 "Failed to get database query result into Json node."
501 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
503 return ResourceAssignmentUtils.parseResponseNode(
506 resourceAssignmentRuntimeService,
511 private fun prepareRAResourceDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
512 return ResourceAssignment().apply {
514 dictionaryName = "sample-ip"
515 dictionarySource = "$dictionary_source"
516 property = PropertyDefinition().apply {
522 private fun prepareRAResourceDictionaryCollection(
523 dictionary_source: String,
526 ): ResourceAssignment {
527 return ResourceAssignment().apply {
528 name = "ipAddress-list"
529 dictionaryName = "sample-licenses"
530 dictionarySource = "$dictionary_source"
531 property = PropertyDefinition().apply {
533 entrySchema = EntrySchema().apply {
540 private fun prepareRAResourceDictionaryComplexType(
541 dictionary_source: String,
544 ): ResourceAssignment {
545 return ResourceAssignment().apply {
546 name = "ipAddress-complexType"
547 dictionaryName = "sample-licenses"
548 dictionarySource = "$dictionary_source"
549 property = PropertyDefinition().apply {
555 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
556 val outputMapping = mutableMapOf<String, String>()
558 when (dictionary_source) {
559 "sample-key-value", "sample-value" -> {
561 if (dictionary_source == "sample-key-value")
562 outputMapping["sample-ip"] = "value"
564 "listOfString", "mapOfString" -> {
566 outputMapping["ip"] = "value"
568 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
569 // List or map of complex Type
570 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
571 outputMapping["ipAddress"] = "value"
573 outputMapping["port"] = "port"
574 outputMapping["ip"] = "ip"
579 if (dictionary_source == "complexTypeOneKeys")
580 outputMapping["host"] = "value"
582 outputMapping["name"] = "name"
583 outputMapping["ipAddress"] = "ipAddress"