diff options
author | Singal, Kapil (ks220y) <ks220y@att.com> | 2019-09-23 16:11:17 -0400 |
---|---|---|
committer | KAPIL SINGAL <ks220y@att.com> | 2019-09-29 03:12:56 +0000 |
commit | 7a04ac414b690983d863e96b487e314780572c48 (patch) | |
tree | 7df2d84fd1c28f96416c620ee82207be7d0b2945 /ms/blueprintsprocessor/functions | |
parent | f2dde8ba531cd4783fbc0872c406ec6e0d097c54 (diff) |
Refactoring ResourceAssignmentUtils
Refactoring ResourceAssignmentUtils parseResponseNodeForPrimitiveTypes API to remove cyclic value assignments
Issue-ID: CCSDK-1748
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
Change-Id: I27b5a7d3ed2df38cf4e3e44686aec094ebdb5f25
Refactoring ResourceAssignmentUtils
Changing isNull condition to isNullOrEmpty to make sure Empty value doesn't get assigned to resource
Issue-ID: CCSDK-1748
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
Change-Id: I0744537c7ddec80f20ffd7e6545b947439f63743
Resource Resolution Refactoring
Refactoring some logging statements and adding new loggers
Issue-ID: CCSDK-1748
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
Change-Id: I5676659eea01056a7d29206f13473a4361516755
Diffstat (limited to 'ms/blueprintsprocessor/functions')
8 files changed, 63 insertions, 60 deletions
diff --git a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt index 25bb3c938..3a655ded7 100644 --- a/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt +++ b/ms/blueprintsprocessor/functions/ansible-awx-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/ansible/executor/ComponentRemoteAnsibleExecutor.kt @@ -296,20 +296,20 @@ open class ComponentRemoteAnsibleExecutor(private val blueprintRestLibPropertySe val skipTagsProp = getOptionalOperationInput(INPUT_SKIP_TAGS) val askLimitOnLaunch = jtLaunchReqs.at("/ask_limit_on_launch").asBoolean() - if (askLimitOnLaunch && limitProp.isNotNull()) { + if (askLimitOnLaunch && !limitProp.isNullOrMissing()) { payload.set(INPUT_LIMIT_TO_HOST, limitProp) } val askTagsOnLaunch = jtLaunchReqs.at("/ask_tags_on_launch").asBoolean() - if (askTagsOnLaunch && tagsProp.isNotNull()) { + if (askTagsOnLaunch && !tagsProp.isNullOrMissing()) { payload.set(INPUT_TAGS, tagsProp) } - if (askTagsOnLaunch && skipTagsProp.isNotNull()) { + if (askTagsOnLaunch && !skipTagsProp.isNullOrMissing()) { payload.set("skip_tags", skipTagsProp) } } val askInventoryOnLaunch = jtLaunchReqs.at("/ask_inventory_on_launch").asBoolean() - if (askInventoryOnLaunch && inventoryProp.isNotNull()) { + if (askInventoryOnLaunch && !inventoryProp.isNullOrMissing()) { var inventoryKeyId = if (inventoryProp is TextNode) { resolveInventoryIdByName(awxClient, inventoryProp.textValue())?.asJsonPrimitive() } else { diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt index 51170a9b2..8651e2620 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionService.kt @@ -230,7 +230,7 @@ open class ResourceResolutionServiceImpl(private var applicationContext: Applica blueprintRuntimeService, artifactPrefix, resourceAssignment) - log.info("Resource resolution saved into database successfully : ($resourceAssignment)") + log.info("Resource resolution saved into database successfully : (${resourceAssignment.name})") } // Set errors from RA diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt index 8d21e9a70..0f5d91415 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/DatabaseResourceAssignmentProcessor.kt @@ -91,7 +91,8 @@ open class DatabaseResourceAssignmentProcessor( "failed to get input-key-mappings for $dName under $dSource properties" } - logger.info("$dSource dictionary information : ($sql), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})") + logger.info("DatabaseResource ($dSource) dictionary information: " + + "Query:($sql), input-key-mapping:($inputKeyMapping), output-key-mapping:(${sourceProperties.outputKeyMapping})") val jdbcTemplate = blueprintDBLibService(sourceProperties) val rows = jdbcTemplate.query(sql, populateNamedParameter(inputKeyMapping)) @@ -135,7 +136,9 @@ open class DatabaseResourceAssignmentProcessor( logger.trace("Reference dictionary key (${it.key}) resulted in value ($expressionValue)") namedParameters[it.key] = expressionValue } - logger.info("Parameter information : ($namedParameters)") + if (namedParameters.isNotEmpty()) { + logger.info("Parameter information : ($namedParameters)") + } return namedParameters } @@ -152,7 +155,7 @@ open class DatabaseResourceAssignmentProcessor( val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) { "failed to get output-key-mappings for $dName under $dSource properties" } - logger.info("Response processing type($type)") + logger.info("Response processing type ($type)") val responseNode = checkNotNull(JacksonUtils.getJsonNode(rows)) { "Failed to get database query result into Json node." diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt index af89bcef6..e513170a8 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/ResourceAssignmentProcessor.kt @@ -51,7 +51,9 @@ abstract class ResourceAssignmentProcessor : BlueprintFunctionNode<ResourceAssig open fun setFromInput(resourceAssignment: ResourceAssignment): Boolean { try { val value = raRuntimeService.getInputValue(resourceAssignment.name) - if (value.returnNullIfMissing() != null) { + if (!value.isNullOrMissing()) { + log.debug("For Resource:(${resourceAssignment.name}) found value:({}) in input-data.", + ResourceAssignmentUtils.getValueToLog(resourceAssignment.property?.metadata, value)) ResourceAssignmentUtils.setResourceDataValue(resourceAssignment, raRuntimeService, value) return true } diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt index dab6ff79f..2b9239f4c 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/processor/RestResourceResolutionProcessor.kt @@ -81,7 +81,8 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping) val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping) - logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})") + logger.info("RestResource ($dSource) dictionary information: " + + "URL:($urlPath), input-key-mapping:($inputKeyMapping), output-key-mapping:(${sourceProperties.outputKeyMapping})") // Get the Rest Client Service val restClientService = blueprintWebClientService(resourceAssignment, sourceProperties) @@ -133,7 +134,7 @@ open class RestResourceResolutionProcessor(private val blueprintRestLibPropertyS val outputKeyMapping = checkNotNull(sourceProperties.outputKeyMapping) { "failed to get output-key-mappings for $dName under $dSource properties" } - logger.info("Response processing type($type)") + logger.info("Response processing type ($type)") val responseNode = checkNotNull(JacksonUtils.jsonNode(restResponse).at(path)) { "Failed to find path ($path) in response ($restResponse)" diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt index 2a3820f07..688713469 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt @@ -124,9 +124,7 @@ class ResourceAssignmentUtils { val resourceProp = checkNotNull(resourceAssignment.property) { "Failed to populate mandatory resource resource mapping $resourceAssignment" } - if (resourceProp.required != null && resourceProp.required!! - && (resourceProp.value == null || resourceProp.value!!.returnNullIfMissing() == null) - ) { + if (resourceProp.required != null && resourceProp.required!! && resourceProp.value.isNullOrMissing()) { logger.error("failed to populate mandatory resource mapping ($resourceAssignment)") throw BluePrintProcessorException("failed to populate mandatory resource mapping ($resourceAssignment)") } @@ -196,8 +194,10 @@ class ResourceAssignmentUtils { } } - fun transformToRARuntimeService(blueprintRuntimeService: BluePrintRuntimeService<*>, - templateArtifactName: String): ResourceAssignmentRuntimeService { + fun transformToRARuntimeService( + blueprintRuntimeService: BluePrintRuntimeService<*>, + templateArtifactName: String + ): ResourceAssignmentRuntimeService { val resourceAssignmentRuntimeService = ResourceAssignmentRuntimeService( blueprintRuntimeService.id(), @@ -210,8 +210,10 @@ class ResourceAssignmentUtils { } @Throws(BluePrintProcessorException::class) - fun getPropertyType(raRuntimeService: ResourceAssignmentRuntimeService, dataTypeName: String, - propertyName: String): String { + fun getPropertyType( + raRuntimeService: ResourceAssignmentRuntimeService, dataTypeName: String, + propertyName: String + ): String { lateinit var type: String try { val dataTypeProps = @@ -228,8 +230,10 @@ class ResourceAssignmentUtils { } @Throws(BluePrintProcessorException::class) - fun parseResponseNode(responseNode: JsonNode, resourceAssignment: ResourceAssignment, - raRuntimeService: ResourceAssignmentRuntimeService, outputKeyMapping: MutableMap<String, String>): JsonNode { + fun parseResponseNode( + responseNode: JsonNode, resourceAssignment: ResourceAssignment, + raRuntimeService: ResourceAssignmentRuntimeService, outputKeyMapping: MutableMap<String, String> + ): JsonNode { val metadata = resourceAssignment.property!!.metadata try { if ((resourceAssignment.property?.type).isNullOrEmpty()) { @@ -238,9 +242,10 @@ class ResourceAssignmentUtils { val type = resourceAssignment.property!!.type val valueToPrint = getValueToLog(metadata, responseNode) - logger.info("For template key (${resourceAssignment.name}) setting value as ($valueToPrint)") + logger.info("For template key (${resourceAssignment.name}) trying to get value from responseNode ($valueToPrint)") return when (type) { in BluePrintTypes.validPrimitiveTypes() -> { + // Primitive Types parseResponseNodeForPrimitiveTypes(responseNode, outputKeyMapping) } in BluePrintTypes.validCollectionTypes() -> { @@ -258,46 +263,37 @@ class ResourceAssignmentUtils { } } - //TODO: Need to Refactor - private fun parseResponseNodeForPrimitiveTypes(responseNode: JsonNode, - outputKeyMapping: MutableMap<String, String>): JsonNode { - var result: JsonNode? = responseNode - - if (responseNode.isComplexType()) { - val key = outputKeyMapping.keys.firstOrNull() - var returnNode: JsonNode? - if (responseNode is ArrayNode) { - val arrayNode = responseNode.toList() - val firstElement = if (key.isNullOrEmpty()) { - arrayNode.first() - } else { - arrayNode.firstOrNull { element -> - element.isComplexType() && element.has(outputKeyMapping[key]) - } - } - returnNode = firstElement - } else { - returnNode = responseNode - } + private fun parseResponseNodeForPrimitiveTypes( + responseNode: JsonNode, + outputKeyMapping: MutableMap<String, String> + ): JsonNode { + // Return responseNode if is not a Complex Type + if (!responseNode.isComplexType()) { + return responseNode + } - if (returnNode.isNull() || (returnNode!!.isComplexType() && !returnNode.has(outputKeyMapping[key]))) { - if (key.isNullOrEmpty()) { - throw BluePrintProcessorException("Fail to find mapping in the responseNode.") - } else { - throw BluePrintProcessorException("Fail to find response with output key mapping ($key) in result.") - } - } - result = if (returnNode.isComplexType()) { - returnNode[outputKeyMapping[key]] + val outputKey = outputKeyMapping.keys.firstOrNull() + var returnNode = if (responseNode is ArrayNode) { + val arrayNode = responseNode.toList() + if (outputKey.isNullOrEmpty()) { + arrayNode.first() } else { - responseNode + arrayNode.firstOrNull { element -> + element.isComplexType() && element.has(outputKeyMapping[outputKey]) + } } } else { - if (outputKeyMapping.isNotEmpty()) { - throw BluePrintProcessorException("Fail to find key-value in response node to map output-key-mapping.") - } + responseNode + } + + if (returnNode.isNullOrMissing() || returnNode!!.isComplexType() && !returnNode.has(outputKeyMapping[outputKey])) { + throw BluePrintProcessorException("Fail to find output key mapping ($outputKey) in the responseNode.") + } + return if (returnNode.isComplexType()) { + returnNode[outputKeyMapping[outputKey]] + } else { + returnNode } - return result!! } private fun parseResponseNodeForCollection( @@ -568,7 +564,7 @@ class ResourceAssignmentUtils { fun getValueToLog(metadata: MutableMap<String, String>?, value: Any): Any { return if (checkIfLogIsProtected(metadata)) { - "*************" + "******REDACTED******" } else { value } diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/mock/MockRestResourceResolutionProcessor.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/mock/MockRestResourceResolutionProcessor.kt index 203b7ea30..e5b559826 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/mock/MockRestResourceResolutionProcessor.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/mock/MockRestResourceResolutionProcessor.kt @@ -77,7 +77,8 @@ class MockRestResourceResolutionProcessor(private val blueprintRestLibPropertySe resolveFromInputKeyMapping(checkNotNull(sourceProperties.urlPath), resolvedInputKeyMapping) val verb = resolveFromInputKeyMapping(nullToEmpty(sourceProperties.verb), resolvedInputKeyMapping) - logger.info("$dSource dictionary information : ($urlPath), ($inputKeyMapping), (${sourceProperties.outputKeyMapping})") + logger.info("MockRestResource ($dSource) dictionary information: " + + "URL:($urlPath), input-key-mapping:($inputKeyMapping), output-key-mapping:(${sourceProperties.outputKeyMapping})") // Get the Rest Client Service val restClientService = blueprintWebClientService(executionRequest) diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtilsTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtilsTest.kt index 728e1165a..d7a696848 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtilsTest.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtilsTest.kt @@ -131,7 +131,7 @@ class ResourceAssignmentUtilsTest { //then the assignment should produce a valid result val expected = "{\n" + " \"pnf-id\" : \"valid_value\"\n" + "}" - assertEquals(expected, outcome, "unexpected outcome generated") + assertEquals(expected, outcome.replace("\r\n","\n"), "unexpected outcome generated") } @Test @@ -147,7 +147,7 @@ class ResourceAssignmentUtilsTest { //then the assignment should produce a valid result val expected = "{\n" + " \"pnf-id\" : \"\${pnf-id}\"\n" + "}" - assertEquals(expected, outcome, "unexpected outcome generated") + assertEquals(expected, outcome.replace("\r\n","\n"), "unexpected outcome generated") } |