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.ObjectNode
26 import com.fasterxml.jackson.databind.node.TextNode
29 import kotlinx.coroutines.runBlocking
30 import org.junit.Before
32 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
33 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.METADATA_TRANSFORM_TEMPLATE
34 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
35 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
36 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
37 import org.onap.ccsdk.cds.controllerblueprints.core.data.EntrySchema
38 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
39 import org.onap.ccsdk.cds.controllerblueprints.core.data.PropertyDefinition
40 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
41 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
42 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
43 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
44 import kotlin.test.assertEquals
46 data class IpAddress(val port: String, val ip: String)
47 data class Host(val name: String, val ipAddress: IpAddress)
48 data class ExpectedResponseIp(val ip: String)
49 data class ExpectedResponseIpAddress(val ipAddress: IpAddress)
51 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))
188 "resolution-summary":[
194 "key-identifiers":[],
195 "dictionary-description":"pnf-id",
196 "dictionary-metadata":[
197 {"name":"aai-path","value":"//path/in/aai"}
199 "dictionary-name":"pnf-id",
200 "dictionary-source":"input",
201 "request-payload":{"mock":true},
207 """.replace("\n|\\s".toRegex(), ""), result)
211 fun generateAssignmentMapTest() {
212 val artifactPrefix = "vdns"
213 val resourceAssignments = mutableListOf(
214 createResourceAssignmentForTest("abc-123", "vnf-id"),
215 createResourceAssignmentForTest(null, "vf-module-name")
218 val result: ObjectNode = ResourceAssignmentUtils.generateAssignmentMap(artifactPrefix, resourceAssignments)
220 assertEquals("abc-123", result["vdns"]["vnf-id"].textValue())
221 assertEquals(JacksonUtils.getJsonNode(null), result["vdns"]["vf-module-name"])
224 private fun createResourceAssignmentForTest(resourceValue: String?, resourceName: String = "pnf-id"): ResourceAssignment {
225 val valueForTest = if (resourceValue == null) null else TextNode(resourceValue)
226 val resourceAssignmentForTest = ResourceAssignment().apply {
228 dictionaryName = "pnf-id"
229 dictionarySource = "input"
230 property = PropertyDefinition().apply {
235 return resourceAssignmentForTest
239 fun parseResponseNodeTestForPrimitivesTypes() {
240 var outcome = prepareResponseNodeForTest(
241 "sample-value", "string", "",
242 inputMapToTestPrimitiveTypeWithValue
245 expectedValueToTestPrimitiveType,
247 "Unexpected outcome returned for primitive type of simple String"
249 assertEquals(0, resourceAssignment.keyIdentifiers.size)
251 outcome = prepareResponseNodeForTest(
252 "sample-key-value", "string", "",
253 inputMapToTestPrimitiveTypeWithKeyValue
256 expectedValueToTestPrimitiveType,
258 "Unexpected outcome returned for primitive type of key-value String"
261 expectedValueToTestPrimitiveType,
262 resourceAssignment.keyIdentifiers[0].value
267 fun parseResponseNodeTestForCollectionsOfString() {
268 var outcome = prepareResponseNodeForTest(
269 "listOfString", "list",
270 "string", inputMapToTestCollectionOfPrimitiveType
273 expectedValueToTesCollectionOfPrimitiveType,
275 "unexpected outcome returned for list of String"
278 val expectedKeyIdentifierValue = JacksonUtils.getJsonNode(outcome.map { it["ip"] })
280 expectedKeyIdentifierValue,
281 resourceAssignment.keyIdentifiers[0].value
284 // FIXME("Map is not collection type, It is known complex type")
285 // outcome = prepareResponseNodeForTest(
286 // "mapOfString", "map", "string",
287 // inputMapToTestCollectionOfPrimitiveType
290 // expectedValueToTesCollectionOfPrimitiveType,
292 // "unexpected outcome returned for map of String"
297 fun parseResponseNodeTestForCollectionsOfComplexType() {
298 var outcome = prepareResponseNodeForTest(
299 "listOfMyDataTypeWithOneOutputKeyMapping", "list",
300 "ip-address", inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping
303 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping,
305 "unexpected outcome returned for list of String"
308 outcome = prepareResponseNodeForTest(
309 "listOfMyDataTypeWithAllOutputKeyMapping", "list",
310 "ip-address", inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping
313 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping,
315 "unexpected outcome returned for list of String"
320 fun `parseResponseNodeTestForComplexType find one output key mapping`() {
321 val outcome = prepareResponseNodeForTest(
322 "complexTypeOneKeys", "host",
323 "", inputMapToTestComplexTypeWithOneOutputKeyMapping
326 expectedValueToTestComplexTypeWithOneOutputKeyMapping,
328 "Unexpected outcome returned for complex type"
331 expectedValueToTestComplexTypeWithOneOutputKeyMapping["host"],
332 resourceAssignment.keyIdentifiers[0].value)
336 fun `parseResponseNodeTestForComplexType find all output key mapping`() {
337 val outcome = prepareResponseNodeForTest(
338 "complexTypeAllKeys", "host",
339 "", inputMapToTestComplexTypeWithAllOutputKeyMapping
342 expectedValueToTestComplexTypeWithAllOutputKeyMapping,
344 "Unexpected outcome returned for complex type"
346 assertEquals(2, resourceAssignment.keyIdentifiers.size)
348 expectedValueToTestComplexTypeWithAllOutputKeyMapping["name"],
349 resourceAssignment.keyIdentifiers[0].value
353 expectedValueToTestComplexTypeWithAllOutputKeyMapping["ipAddress"],
354 resourceAssignment.keyIdentifiers[1].value
359 fun `transform resolved value with inline template`() {
360 resourceAssignmentRuntimeService.putResolutionStore("vnf_name", "abc-vnf".asJsonType())
361 resourceAssignment = ResourceAssignment()
362 resourceAssignment.name = "int_pktgen_private_net_id"
363 resourceAssignment.property = PropertyDefinition()
364 resourceAssignment.property!!.type = "string"
365 val value = "".asJsonType()
367 // Enable transform template
368 resourceAssignment.property!!.metadata =
369 mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_name}_private2")
371 ResourceAssignmentUtils
372 .setResourceDataValue(resourceAssignment, resourceAssignmentRuntimeService, value)
374 assertEquals("abc-vnf_private2",
375 resourceAssignment.property!!.value!!.asText())
378 private fun initInputMapAndExpectedValuesForPrimitiveType() {
379 inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
380 val keyValue = mutableMapOf<String, String>()
381 keyValue["value"] = "1.2.3.1"
382 inputMapToTestPrimitiveTypeWithKeyValue = keyValue.asJsonType()
383 expectedValueToTestPrimitiveType = TextNode("1.2.3.1")
386 private fun initInputMapAndExpectedValuesForCollection() {
387 val listOfIps = arrayListOf("1.2.3.1", "1.2.3.2", "1.2.3.3")
388 val arrayNodeForList1 = JacksonUtils.objectMapper.createArrayNode()
390 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
391 arrayChildNode.set<JsonNode>("value", it.asJsonPrimitive())
392 arrayNodeForList1.add(arrayChildNode)
394 inputMapToTestCollectionOfPrimitiveType = arrayNodeForList1
396 expectedValueToTesCollectionOfPrimitiveType = arrayListOf(
397 ExpectedResponseIp("1.2.3.1"),
398 ExpectedResponseIp("1.2.3.2"), ExpectedResponseIp("1.2.3.3")
401 val listOfIpAddresses = arrayListOf(
402 IpAddress("1111", "1.2.3.1").asJsonType(),
403 IpAddress("2222", "1.2.3.2").asJsonType(), IpAddress("3333", "1.2.3.3").asJsonType()
405 val arrayNodeForList2 = JacksonUtils.objectMapper.createArrayNode()
406 listOfIpAddresses.forEach {
407 val arrayChildNode = JacksonUtils.objectMapper.createObjectNode()
408 arrayChildNode.set<JsonNode>("value", it.asJsonType())
409 arrayNodeForList2.add(arrayChildNode)
411 inputMapToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayNodeForList2
413 val arrayNodeForList3 = JacksonUtils.objectMapper.createArrayNode()
414 var childNode = JacksonUtils.objectMapper.createObjectNode()
415 childNode.set<JsonNode>("port", "1111".asJsonPrimitive())
416 childNode.set<JsonNode>("ip", "1.2.3.1".asJsonPrimitive())
417 arrayNodeForList3.add(childNode)
418 childNode = JacksonUtils.objectMapper.createObjectNode()
419 childNode.set<JsonNode>("port", "2222".asJsonPrimitive())
420 childNode.set<JsonNode>("ip", "1.2.3.2".asJsonPrimitive())
421 arrayNodeForList3.add(childNode)
422 childNode = JacksonUtils.objectMapper.createObjectNode()
423 childNode.set<JsonNode>("port", "3333".asJsonPrimitive())
424 childNode.set<JsonNode>("ip", "1.2.3.3".asJsonPrimitive())
425 arrayNodeForList3.add(childNode)
426 inputMapToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayNodeForList3
428 expectedValueToTestCollectionOfComplexTypeWithOneOutputKeyMapping = arrayListOf(
429 ExpectedResponseIpAddress(IpAddress("1111", "1.2.3.1")),
430 ExpectedResponseIpAddress(IpAddress("2222", "1.2.3.2")), ExpectedResponseIpAddress(
431 IpAddress("3333", "1.2.3.3")
434 expectedValueToTestCollectionOfComplexTypeWithAllOutputKeyMapping = arrayListOf(
435 IpAddress("1111", "1.2.3.1"),
436 IpAddress("2222", "1.2.3.2"),
437 IpAddress("3333", "1.2.3.3")
441 private fun initInputMapAndExpectedValuesForComplexType() {
442 val mapOfComplexType = mutableMapOf<String, JsonNode>()
443 mapOfComplexType["value"] = Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType()
444 mapOfComplexType["port"] = "8888".asJsonType()
445 mapOfComplexType["something"] = "1.2.3.2".asJsonType()
446 inputMapToTestComplexTypeWithOneOutputKeyMapping = mapOfComplexType.asJsonType()
448 val objectNode = JacksonUtils.objectMapper.createObjectNode()
449 expectedValueToTestComplexTypeWithOneOutputKeyMapping =
450 objectNode.set("host", Host("my-ipAddress", IpAddress("1111", "1.2.3.1")).asJsonType())
452 val childNode1 = JacksonUtils.objectMapper.createObjectNode()
453 childNode1.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
454 childNode1.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
455 childNode1.set<JsonNode>("port", "8888".asJsonType())
456 childNode1.set<JsonNode>("something", IpAddress("2222", "1.2.3.1").asJsonType())
457 inputMapToTestComplexTypeWithAllOutputKeyMapping = childNode1
459 val childNode2 = JacksonUtils.objectMapper.createObjectNode()
460 childNode2.set<JsonNode>("name", "my-ipAddress".asJsonPrimitive())
461 childNode2.set<JsonNode>("ipAddress", IpAddress("1111", "1.2.3.1").asJsonType())
462 expectedValueToTestComplexTypeWithAllOutputKeyMapping = childNode2
465 private fun prepareResponseNodeForTest(
466 dictionary_source: String,
472 resourceAssignment = when (sourceType) {
474 prepareRADataDictionaryCollection(dictionary_source, sourceType, entrySchema)
477 prepareRADataDictionaryOfPrimaryType(dictionary_source)
480 prepareRADataDictionaryComplexType(dictionary_source, sourceType, entrySchema)
484 val responseNode = checkNotNull(JacksonUtils.getJsonNode(response)) {
485 "Failed to get database query result into Json node."
488 val outputKeyMapping = prepareOutputKeyMapping(dictionary_source)
490 return ResourceAssignmentUtils.parseResponseNode(
493 resourceAssignmentRuntimeService,
498 private fun prepareRADataDictionaryOfPrimaryType(dictionary_source: String): ResourceAssignment {
499 return ResourceAssignment().apply {
501 dictionaryName = "sample-ip"
502 dictionarySource = "$dictionary_source"
503 property = PropertyDefinition().apply {
509 private fun prepareRADataDictionaryCollection(
510 dictionary_source: String,
513 ): ResourceAssignment {
514 return ResourceAssignment().apply {
515 name = "ipAddress-list"
516 dictionaryName = "sample-licenses"
517 dictionarySource = "$dictionary_source"
518 property = PropertyDefinition().apply {
520 entrySchema = EntrySchema().apply {
527 private fun prepareRADataDictionaryComplexType(
528 dictionary_source: String,
531 ): ResourceAssignment {
532 return ResourceAssignment().apply {
533 name = "ipAddress-complexType"
534 dictionaryName = "sample-licenses"
535 dictionarySource = "$dictionary_source"
536 property = PropertyDefinition().apply {
542 private fun prepareOutputKeyMapping(dictionary_source: String): MutableMap<String, String> {
543 val outputMapping = mutableMapOf<String, String>()
545 when (dictionary_source) {
546 "sample-key-value", "sample-value" -> {
548 if (dictionary_source == "sample-key-value")
549 outputMapping["sample-ip"] = "value"
551 "listOfString", "mapOfString" -> {
553 outputMapping["ip"] = "value"
555 "listOfMyDataTypeWithOneOutputKeyMapping", "listOfMyDataTypeWithAllOutputKeyMapping" -> {
556 // List or map of complex Type
557 if (dictionary_source == "listOfMyDataTypeWithOneOutputKeyMapping")
558 outputMapping["ipAddress"] = "value"
560 outputMapping["port"] = "port"
561 outputMapping["ip"] = "ip"
566 if (dictionary_source == "complexTypeOneKeys")
567 outputMapping["host"] = "value"
569 outputMapping["name"] = "name"
570 outputMapping["ipAddress"] = "ipAddress"