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 java.util.concurrent.atomic.AtomicBoolean
44 import kotlin.concurrent.thread
45 import kotlin.test.assertEquals
47 data class IpAddress(val port: String, val ip: String)
48 data class Host(val name: String, val ipAddress: IpAddress)
49 data class ExpectedResponseIp(val ip: String)
50 data class ExpectedResponseIpAddress(val ipAddress: IpAddress)
52 class ResourceAssignmentUtilsTest {
54 private lateinit var resourceAssignmentRuntimeService: ResourceAssignmentRuntimeService
55 private lateinit var resourceAssignment: ResourceAssignment
57 private lateinit var inputMapToTestPrimitiveTypeWithValue: JsonNode
58 private lateinit var inputMapToTestPrimitiveTypeWithKeyValue: JsonNode
59 private lateinit var inputMapToTestCollectionOfPrimitiveType: JsonNode
60 private lateinit var inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
61 private lateinit var inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
62 private lateinit var inputMapToTestComplexTypeWithOneOutputKeyMapping: JsonNode
63 private lateinit var inputMapToTestComplexTypeWithAllOutputKeyMapping: JsonNode
64 private lateinit var expectedValueToTestPrimitiveType: JsonNode
65 private lateinit var expectedValueToTesCollectionOfPrimitiveType: JsonNode
66 private lateinit var expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping: JsonNode
67 private lateinit var expectedValueToTestComplexTypeWithOneOutputKeyMapping: JsonNode
68 private lateinit var expectedValueToTestComplexTypeWithAllOutputKeyMapping: JsonNode
69 private lateinit var expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping: JsonNode
74 val bluePrintContext = runBlocking {
75 BluePrintMetadataUtils.getBluePrintContext(
76 "./../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
80 resourceAssignmentRuntimeService = spyk(ResourceAssignmentRuntimeService("1234", bluePrintContext))
82 // Init input map and expected values for tests
83 initInputMapAndExpectedValuesForPrimitiveType()
84 initInputMapAndExpectedValuesForCollection()
85 initInputMapAndExpectedValuesForComplexType()
87 val propertiesDefinition1 = PropertyDefinition().apply {
92 val propertiesDefinition2 = PropertyDefinition().apply {
97 val propertiesDefinition3 = PropertyDefinition().apply {
102 val propertiesDefinition4 = PropertyDefinition().apply {
107 val mapOfPropertiesIpAddress = mutableMapOf<String, PropertyDefinition>()
108 mapOfPropertiesIpAddress["port"] = propertiesDefinition1
109 mapOfPropertiesIpAddress["ip"] = propertiesDefinition2
111 val mapOfPropertiesHost = mutableMapOf<String, PropertyDefinition>()
112 mapOfPropertiesHost["name"] = propertiesDefinition3
113 mapOfPropertiesHost["ipAddress"] = propertiesDefinition4
115 val myDataTypeIpaddress = DataType().apply {
117 properties = mapOfPropertiesIpAddress
120 val myDataTypeHost = DataType().apply {
122 properties = mapOfPropertiesHost
126 resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("ip-address")
127 } returns myDataTypeIpaddress
129 every { resourceAssignmentRuntimeService.bluePrintContext().dataTypeByName("host") } returns myDataTypeHost
131 every { resourceAssignmentRuntimeService.setNodeTemplateAttributeValue(any(), any(), any()) } returns Unit
135 fun `generateResourceDataForAssignments - positive test`() {
136 // given a valid resource assignment
137 val validResourceAssignment1 = createResourceAssignmentForTest("valid_value", "pnf-id")
138 val validResourceAssignment2 = createResourceAssignmentForTest("also_valid", "a1")
140 // and a list containing that resource assignment
141 val resourceAssignmentList = listOf<ResourceAssignment>(validResourceAssignment1, validResourceAssignment2)
143 // when the values of the resources are evaluated
144 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
146 // then the assignment should produce a valid result
150 "pnf-id" : "valid_value"
153 assertEquals(expected, outcome.trimIndent(), "unexpected outcome generated")
157 fun `generateResourceDataForAssignments - resource without value is not resolved as null`() {
158 // given a valid resource assignment
159 val resourceAssignmentWithNullValue = createResourceAssignmentForTest(null)
161 // and a list containing that resource assignment
162 val resourceAssignmentList = listOf<ResourceAssignment>(resourceAssignmentWithNullValue)
164 // when the values of the resources are evaluated
165 val outcome = ResourceAssignmentUtils.generateResourceDataForAssignments(resourceAssignmentList)
167 // then the assignment should produce a valid result
168 val expected = "{\n" + " \"pnf-id\" : \"\${pnf-id}\"\n" + "}"
169 assertEquals(expected, outcome.replace("\r\n", "\n"), "unexpected outcome generated")
173 fun generateResolutionSummaryDataTest() {
174 val resourceAssignment = createResourceAssignmentForTest(null)
175 val resourceDefinition = ResourceDefinition()
176 val nodeTemplate = NodeTemplate().apply {
177 properties = mutableMapOf("resolved-payload" to JacksonUtils.jsonNode("{\"mock\": true}"))
179 resourceDefinition.sources = mutableMapOf("input" to nodeTemplate)
180 resourceDefinition.property = PropertyDefinition().apply {
181 this.description = "pnf-id"
182 this.metadata = mutableMapOf("aai-path" to "//path/in/aai")
185 val result = ResourceAssignmentUtils.generateResolutionSummaryData(
186 listOf(resourceAssignment), mapOf("pnf-id" to resourceDefinition)
192 "resolution-summary":[
198 "key-identifiers":[],
199 "dictionary-description":"pnf-id",
200 "dictionary-metadata":[
201 {"name":"aai-path","value":"//path/in/aai"}
203 "dictionary-name":"pnf-id",
204 "dictionary-source":"input",
205 "request-payload":{"mock":true},
211 """.replace("\n|\\s".toRegex(), ""),
216 private fun createResourceAssignmentForTest(resourceValue: String?, resourceName: String = "pnf-id"): ResourceAssignment {
217 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
218 val resourceAssignmentForTest = ResourceAssignment().apply {
220 dictionaryName = "pnf-id"
221 dictionarySource = "input"
222 property = PropertyDefinition().apply {
227 return resourceAssignmentForTest
231 fun parseResponseNodeTestForPrimitivesTypes() {
232 var outcome = prepareResponseNodeForTest(
233 "sample-value", "string", "",
234 inputMapToTestPrimitiveTypeWithValue
237 expectedValueToTestPrimitiveType,
239 "Unexpected outcome returned for primitive type of simple String"
241 assertEquals(0, resourceAssignment.keyIdentifiers.size)
243 outcome = prepareResponseNodeForTest(
244 "sample-key-value", "string", "",
245 inputMapToTestPrimitiveTypeWithKeyValue
248 expectedValueToTestPrimitiveType,
250 "Unexpected outcome returned for primitive type of key-value String"
253 expectedValueToTestPrimitiveType,
254 resourceAssignment.keyIdentifiers[0].value
259 fun parseResponseNodeTestForCollectionsOfString() {
260 var outcome = prepareResponseNodeForTest(
261 "listOfString", "list",
262 "string", inputMapToTestCollectionOfPrimitiveType
265 expectedValueToTesCollectionOfPrimitiveType,
267 "unexpected outcome returned for list of String"
270 val expectedKeyIdentifierValue = JacksonUtils.getJsonNode(outcome.map { it["ip"] })
272 expectedKeyIdentifierValue,
273 resourceAssignment.keyIdentifiers[0].value
276 // FIXME("Map is not collection type, It is known complex type")
277 // outcome = prepareResponseNodeForTest(
278 // "mapOfString", "map", "string",
279 // inputMapToTestCollectionOfPrimitiveType
282 // expectedValueToTesCollectionOfPrimitiveType,
284 // "unexpected outcome returned for map of String"
289 fun parseResponseNodeTestForCollectionsOfComplexType() {
290 var outcome = prepareResponseNodeForTest(
291 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
292 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
295 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
297 "unexpected outcome returned for list of String"
300 outcome = prepareResponseNodeForTest(
301 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
302 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
305 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
307 "unexpected outcome returned for list of String"
312 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
313 val outcome = prepareResponseNodeForTest(
314 "complexTypeOneKeys", "host",
315 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
318 expectedValueToTestComplexTypeWithOneOutputKeyMapping,
320 "Unexpected outcome returned for complex type"
323 expectedValueToTestComplexTypeWithOneOutputKeyMapping["host"],
324 resourceAssignment.keyIdentifiers[0].value
329 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
330 val outcome = prepareResponseNodeForTest(
331 "complexTypeAllKeys", "host",
332 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
335 expectedValueToTestComplexTypeWithAllOutputKeyMapping,
337 "Unexpected outcome returned for complex type"
339 assertEquals(2, resourceAssignment.keyIdentifiers.size)
341 expectedValueToTestComplexTypeWithAllOutputKeyMapping["name"],
342 resourceAssignment.keyIdentifiers[0].value
346 expectedValueToTestComplexTypeWithAllOutputKeyMapping["ipAddress"],
347 resourceAssignment.keyIdentifiers[1].value
352 fun `transform resolved value with inline template`() {
353 resourceAssignmentRuntimeService.putResolutionStore("vnf_name", "abc-vnf".asJsonType())
354 resourceAssignment = ResourceAssignment()
355 resourceAssignment.name = "int_pktgen_private_net_id"
356 resourceAssignment.property = PropertyDefinition()
357 resourceAssignment.property!!.type = "string"
358 val value = "".asJsonType()
360 // Enable transform template
361 resourceAssignment.property!!.metadata =
362 mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_name}_private2")
364 ResourceAssignmentUtils
365 .setResourceDataValue(resourceAssignment, resourceAssignmentRuntimeService, value)
367 val valueJson = "{\"config\":{\"parameter\":\"address\",\"value\":\"0.0.0.0\"}}"
368 resourceAssignmentRuntimeService.putResolutionStore("vnf_config", JacksonUtils.objectMapper.readTree(valueJson))
369 val resourceAssignmentJson = ResourceAssignment()
370 resourceAssignmentJson.name = "vendor_vnf_configuration"
371 resourceAssignmentJson.property = PropertyDefinition()
372 resourceAssignmentJson.property!!.type = "json"
374 // Enable transform template
375 resourceAssignmentJson.property!!.metadata =
376 mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_config}")
378 ResourceAssignmentUtils
379 .setResourceDataValue(resourceAssignmentJson, resourceAssignmentRuntimeService, JacksonUtils.objectMapper.createObjectNode())
383 resourceAssignment.property!!.value!!.asText()
388 resourceAssignmentJson.property!!.value!!.toString()
393 fun `resource resolution issue of resourceStore HashMap during resource resolution`() {
394 var uncaughtExceptionOccured = AtomicBoolean(false)
397 resourceAssignmentRuntimeService.putResolutionStore("key_$i", "value_$i".asJsonType())
399 val t = thread(false) {
400 resourceAssignmentRuntimeService.getResolutionStore()
401 // often ConcurrentModificationException occurs here
403 t.uncaughtExceptionHandler =
404 Thread.UncaughtExceptionHandler { t, e -> uncaughtExceptionOccured = AtomicBoolean(true) }
407 assertEquals(uncaughtExceptionOccured.get(), false)
410 private fun initInputMapAndExpectedValuesForPrimitiveType() {
411 inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
412 val keyValue = mutableMapOf<String, String>()
413 keyValue["value"] = "1.2.3.1"
414 inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
415 expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
418 private fun initInputMapAndExpectedValuesForCollection() {
419 val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
420 val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
422 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
423 arrayChildNode.set<JsonNode>("value", it.asJsonPrimitive())
424 arrayNodeForList1.add(arrayChildNode)
426 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
428 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
429 ExpectedResponseIp("1.2.3.1"),
430 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
433 val listOfIpAddresses = arrayListOf(
434 IpAddress("1111", "1.2.3.1").asJsonType(),
435 IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
437 val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
438 listOfIpAddresses.forEach {
439 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
440 arrayChildNode.set<JsonNode>("value", it.asJsonType())
441 arrayNodeForList2.add(arrayChildNode)
443 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
445 val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
446 var childNode = JacksonUtils.objectMapper.createObjectNode()
447 childNode.set<JsonNode>("port", "1111".asJsonPrimitive())
448 childNode.set<JsonNode>("ip", "1.2.3.1".asJsonPrimitive())
449 arrayNodeForList3.add(childNode)
450 childNode = JacksonUtils.objectMapper.createObjectNode()
451 childNode.set<JsonNode>("port", "2222".asJsonPrimitive())
452 childNode.set<JsonNode>("ip", "1.2.3.2".asJsonPrimitive())
453 arrayNodeForList3.add(childNode)
454 childNode = JacksonUtils.objectMapper.createObjectNode()
455 childNode.set<JsonNode>("port", "3333".asJsonPrimitive())
456 childNode.set<JsonNode>("ip", "1.2.3.3".asJsonPrimitive())
457 arrayNodeForList3.add(childNode)
458 inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
460 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
461 ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
462 ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")),
463 ExpectedResponseIpAddress(
464 IpAddress("3333", "1.2.3.3")
467 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
468 IpAddress("1111", "1.2.3.1"),
469 IpAddress("2222", "1.2.3.2"),
470 IpAddress("3333", "1.2.3.3")
474 private fun initInputMapAndExpectedValuesForComplexType() {
475 val mapOfComplexType = mutableMapOf<String, JsonNode>()
476 mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
477 mapOfComplexType["port"] = "8888".asJsonType()
478 mapOfComplexType["something"] = "1.2.3.2".asJsonType()
479 inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
481 val objectNode = JacksonUtils.objectMapper.createObjectNode()
482 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
483 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
485 val childNode1 = JacksonUtils.objectMapper.createObjectNode()
486 childNode1.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
487 childNode1.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
488 childNode1.set<JsonNode>("port", "8888".asJsonType())
489 childNode1.set<JsonNode>("something", IpAddress("2222", "1.2.3.1").asJsonType())
490 inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
492 val childNode2 = JacksonUtils.objectMapper.createObjectNode()
493 childNode2.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
494 childNode2.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
495 expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
498 private fun prepareResponseNodeForTest(
499 dictionary_source: String,
505 resourceAssignment = when (sourceType) {
507 prepareRAResourceDictionaryCollection(dictionary_source, sourceType, entrySchema)
510 prepareRAResourceDictionaryOfPrimaryType(dictionary_source)
513 prepareRAResourceDictionaryComplexType(dictionary_source, sourceType, entrySchema)
517 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
518 "Failed to get database query result into Json node."
521 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
523 return ResourceAssignmentUtils.parseResponseNode(
526 resourceAssignmentRuntimeService,
531 private fun prepareRAResourceDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
532 return ResourceAssignment().apply {
534 dictionaryName = "sample-ip"
535 dictionarySource = "$dictionary_source"
536 property = PropertyDefinition().apply {
542 private fun prepareRAResourceDictionaryCollection(
543 dictionary_source: String,
546 ): ResourceAssignment {
547 return ResourceAssignment().apply {
548 name = "ipAddress-list"
549 dictionaryName = "sample-licenses"
550 dictionarySource = "$dictionary_source"
551 property = PropertyDefinition().apply {
553 entrySchema = EntrySchema().apply {
560 private fun prepareRAResourceDictionaryComplexType(
561 dictionary_source: String,
564 ): ResourceAssignment {
565 return ResourceAssignment().apply {
566 name = "ipAddress-complexType"
567 dictionaryName = "sample-licenses"
568 dictionarySource = "$dictionary_source"
569 property = PropertyDefinition().apply {
575 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
576 val outputMapping = mutableMapOf<String, String>()
578 when (dictionary_source) {
579 "sample-key-value", "sample-value" -> {
581 if (dictionary_source == "sample-key-value")
582 outputMapping["sample-ip"] = "value"
584 "listOfString", "mapOfString" -> {
586 outputMapping["ip"] = "value"
588 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
589 // List or map of complex Type
590 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
591 outputMapping["ipAddress"] = "value"
593 outputMapping["port"] = "port"
594 outputMapping["ip"] = "ip"
599 if (dictionary_source == "complexTypeOneKeys")
600 outputMapping["host"] = "value"
602 outputMapping["name"] = "name"
603 outputMapping["ipAddress"] = "ipAddress"