From bf19a3a88eac9220051453180614edb4d146a134 Mon Sep 17 00:00:00 2001 From: "Singal, Kapil (ks220y)" Date: Thu, 10 Jan 2019 12:50:06 -0500 Subject: Resource Resoulution Service Implement Input Resource Resolution Processor Service along with Resource Resolution Utilities Change-Id: Ibb4899e415f4b79cd6cd1b190b0f4969b09c3fe4 Issue-ID: CCSDK-936 Signed-off-by: Singal, Kapil (ks220y) --- .../core/utils/JacksonUtils.kt | 364 ++++++++++----------- .../core/service/BluePrintRuntimeServiceTest.kt | 15 +- 2 files changed, 174 insertions(+), 205 deletions(-) (limited to 'components/core') diff --git a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt b/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt index 01874455a..58a820791 100644 --- a/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt +++ b/components/core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.onap.ccsdk.apps.controllerblueprints.core.utils import com.att.eelf.configuration.EELFLogger @@ -22,6 +21,9 @@ import com.att.eelf.configuration.EELFManager import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.node.ArrayNode +import com.fasterxml.jackson.databind.node.NullNode +import com.fasterxml.jackson.databind.node.ObjectNode import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import kotlinx.coroutines.Dispatchers @@ -40,241 +42,209 @@ import java.nio.charset.Charset * * @author Brinda Santh */ -object JacksonUtils { - private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString()) - - inline fun readValue(content: String): T = - jacksonObjectMapper().readValue(content, T::class.java) +class JacksonUtils { + companion object { + private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString()) + inline fun readValue(content: String): T = + jacksonObjectMapper().readValue(content, T::class.java) + + fun readValue(content: String, valueType: Class): T? { + return jacksonObjectMapper().readValue(content, valueType) + } - @JvmStatic - fun readValue(content: String, valueType: Class): T? { - return jacksonObjectMapper().readValue(content, valueType) - } + fun readValue(node: JsonNode, valueType: Class): T? { + return jacksonObjectMapper().treeToValue(node, valueType) + } - @JvmStatic - fun readValue(node: JsonNode, valueType: Class): T? { - return jacksonObjectMapper().treeToValue(node, valueType) - } + fun getContent(fileName: String): String = runBlocking { + async { + try { + File(fileName).readText(Charsets.UTF_8) + } catch (e: Exception) { + throw BluePrintException("couldn't get file ($fileName) content : ${e.message}") + } + }.await() + } - @JvmStatic - fun getContent(fileName: String): String = runBlocking { - async { - try { - File(fileName).readText(Charsets.UTF_8) - } catch (e: Exception) { - throw BluePrintException("couldn't get file ($fileName) content : ${e.message}") + fun getClassPathFileContent(fileName: String): String { + return runBlocking { + withContext(Dispatchers.Default) { + IOUtils.toString(JacksonUtils::class.java.classLoader + .getResourceAsStream(fileName), Charset.defaultCharset()) + } } - }.await() - } + } - @JvmStatic - fun getClassPathFileContent(fileName: String): String { - return runBlocking { - withContext(Dispatchers.Default) { - IOUtils.toString(JacksonUtils::class.java.classLoader - .getResourceAsStream(fileName), Charset.defaultCharset()) - } + fun readValueFromFile(fileName: String, valueType: Class): T? { + val content: String = getContent(fileName) + return readValue(content, valueType) } - } - @JvmStatic - fun readValueFromFile(fileName: String, valueType: Class): T? { - val content: String = getContent(fileName) - return readValue(content, valueType) - } + fun readValueFromClassPathFile(fileName: String, valueType: Class): T? { + val content: String = getClassPathFileContent(fileName) + return readValue(content, valueType) + } - @JvmStatic - fun readValueFromClassPathFile(fileName: String, valueType: Class): T? { - val content: String = getClassPathFileContent(fileName) - return readValue(content, valueType) - } + fun jsonNodeFromObject(from: kotlin.Any): JsonNode { + return jacksonObjectMapper().convertValue(from, JsonNode::class.java) + } - @JvmStatic - fun jsonNodeFromObject(from: kotlin.Any): JsonNode = jacksonObjectMapper().convertValue(from, JsonNode::class.java) + fun jsonNodeFromClassPathFile(fileName: String): JsonNode { + val content: String = getClassPathFileContent(fileName) + return jsonNode(content) + } - @JvmStatic - fun jsonNodeFromClassPathFile(fileName: String): JsonNode { - val content: String = getClassPathFileContent(fileName) - return jsonNode(content) - } + fun jsonNodeFromFile(fileName: String): JsonNode { + val content: String = getContent(fileName) + return jsonNode(content) + } - @JvmStatic - fun jsonNodeFromFile(fileName: String): JsonNode { - val content: String = getContent(fileName) - return jsonNode(content) - } + fun jsonNode(content: String): JsonNode { + return jacksonObjectMapper().readTree(content) + } - @JvmStatic - fun jsonNode(content: String): JsonNode { - return jacksonObjectMapper().readTree(content) - } + fun getJson(any: kotlin.Any): String { + return getJson(any, false) + } - @JvmStatic - fun getJson(any: kotlin.Any): String { - return getJson(any, false) - } + fun getWrappedJson(wrapper: String, any: kotlin.Any, pretty: Boolean = false): String { + val wrapperMap = hashMapOf() + wrapperMap[wrapper] = any + return getJson(wrapperMap, pretty) + } - @JvmStatic - fun getWrappedJson(wrapper: String, any: kotlin.Any, pretty: Boolean = false): String { - val wrapperMap = hashMapOf() - wrapperMap[wrapper] = any - return getJson(wrapperMap, pretty) - } + fun getJson(any: kotlin.Any, pretty: Boolean = false): String { + val objectMapper = jacksonObjectMapper() + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) + if (pretty) { + objectMapper.enable(SerializationFeature.INDENT_OUTPUT) + } + return objectMapper.writeValueAsString(any) + } - @JvmStatic - fun getJson(any: kotlin.Any, pretty: Boolean = false): String { - val objectMapper = jacksonObjectMapper() - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) - if (pretty) { - objectMapper.enable(SerializationFeature.INDENT_OUTPUT) + fun getJsonNode(any: kotlin.Any?, pretty: Boolean = false): JsonNode { + val objectMapper = jacksonObjectMapper() + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL) + if (pretty) { + objectMapper.enable(SerializationFeature.INDENT_OUTPUT) + } + return objectMapper.valueToTree(any) } - return objectMapper.writeValueAsString(any) - } - @JvmStatic - fun getListFromJsonNode(node: JsonNode, valueType: Class): List? { - return getListFromJson(node.toString(), valueType) - } + fun getListFromJsonNode(node: JsonNode, valueType: Class): List? { + return getListFromJson(node.toString(), valueType) + } - @JvmStatic - fun getListFromJson(content: String, valueType: Class): List? { - val objectMapper = jacksonObjectMapper() - val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType) - return objectMapper.readValue>(content, javaType) - } + fun getListFromJson(content: String, valueType: Class): List? { + val objectMapper = jacksonObjectMapper() + val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType) + return objectMapper.readValue>(content, javaType) + } - @JvmStatic - fun getListFromFile(fileName: String, valueType: Class): List? { - val content: String = getContent(fileName) - return getListFromJson(content, valueType) - } + fun getListFromFile(fileName: String, valueType: Class): List? { + val content: String = getContent(fileName) + return getListFromJson(content, valueType) + } - @JvmStatic - fun getListFromClassPathFile(fileName: String, valueType: Class): List? { - val content: String = getClassPathFileContent(fileName) - return getListFromJson(content, valueType) - } + fun getListFromClassPathFile(fileName: String, valueType: Class): List? { + val content: String = getClassPathFileContent(fileName) + return getListFromJson(content, valueType) + } - @JvmStatic - fun getMapFromJson(content: String, valueType: Class): MutableMap? { - val objectMapper = jacksonObjectMapper() - val typeRef = object : TypeReference>() {} - return objectMapper.readValue(content, typeRef) - } + fun getMapFromJson(content: String, valueType: Class): MutableMap? { + val objectMapper = jacksonObjectMapper() + val typeRef = object : TypeReference>() {} + return objectMapper.readValue(content, typeRef) + } - @JvmStatic - fun getMapFromFile(fileName: String, valueType: Class): MutableMap? { - val content: String = getContent(fileName) - return getMapFromJson(content, valueType) - } + fun getMapFromFile(fileName: String, valueType: Class): MutableMap? { + val content: String = getContent(fileName) + return getMapFromJson(content, valueType) + } - @JvmStatic - fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean { - if (BluePrintTypes.validPrimitiveTypes().contains(type)) { - return checkJsonNodeValueOfPrimitiveType(type, jsonNode) - } else if (BluePrintTypes.validCollectionTypes().contains(type)) { - return checkJsonNodeValueOfCollectionType(type, jsonNode) + fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean { + if (BluePrintTypes.validPrimitiveTypes().contains(type)) { + return checkJsonNodeValueOfPrimitiveType(type, jsonNode) + } else if (BluePrintTypes.validCollectionTypes().contains(type)) { + return checkJsonNodeValueOfCollectionType(type, jsonNode) + } + return false } - return false - } - @JvmStatic - fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean { - when (primitiveType) { - BluePrintConstants.DATA_TYPE_STRING -> return jsonNode.isTextual - BluePrintConstants.DATA_TYPE_BOOLEAN -> return jsonNode.isBoolean - BluePrintConstants.DATA_TYPE_INTEGER -> return jsonNode.isInt - BluePrintConstants.DATA_TYPE_FLOAT -> return jsonNode.isDouble - BluePrintConstants.DATA_TYPE_TIMESTAMP -> return jsonNode.isTextual - else -> return false + fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean { + when (primitiveType) { + BluePrintConstants.DATA_TYPE_STRING -> return jsonNode.isTextual + BluePrintConstants.DATA_TYPE_BOOLEAN -> return jsonNode.isBoolean + BluePrintConstants.DATA_TYPE_INTEGER -> return jsonNode.isInt + BluePrintConstants.DATA_TYPE_FLOAT -> return jsonNode.isDouble + BluePrintConstants.DATA_TYPE_TIMESTAMP -> return jsonNode.isTextual + else -> return false + } } - } - @JvmStatic - fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean { - when (type) { - BluePrintConstants.DATA_TYPE_LIST -> return jsonNode.isArray - BluePrintConstants.DATA_TYPE_MAP -> return jsonNode.isContainerNode - else -> return false + fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean { + when (type) { + BluePrintConstants.DATA_TYPE_LIST -> return jsonNode.isArray + BluePrintConstants.DATA_TYPE_MAP -> return jsonNode.isContainerNode + else -> return false + } } - } -/* - @JvmStatic - fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) { - if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) { - objectNode.put(key, value as Boolean) - } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) { - objectNode.put(key, value as Int) - } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) { - objectNode.put(key, value as Float) - } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) { - objectNode.put(key, value as String) - } else { - objectNode.put(key, value as String) + fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) { + when (primitiveType) { + BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, value as Boolean) + BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, value as Int) + BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, value as Float) + BluePrintConstants.DATA_TYPE_TIMESTAMP -> objectNode.put(key, value as String) + else -> objectNode.put(key, value as String) + } } - } - @JvmStatic - fun populatePrimitiveValues(value: Any, primitiveType: String, objectNode: ArrayNode) { - if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) { - objectNode.add(value as Boolean) - } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) { - objectNode.add(value as Int) - } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) { - objectNode.add(value as Float) - } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) { - objectNode.add(value as String) - } else { - objectNode.add(value as String) + fun populatePrimitiveValues(value: Any, primitiveType: String, arrayNode: ArrayNode) { + when (primitiveType) { + BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(value as Boolean) + BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(value as Int) + BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(value as Float) + BluePrintConstants.DATA_TYPE_TIMESTAMP -> arrayNode.add(value as String) + else -> arrayNode.add(value as String) + } } - } - @JvmStatic - fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) { - if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) { - objectNode.put(key, false) - } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) { - objectNode.put(key, 0) - } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) { - objectNode.put(key, 0.0) - } else { - objectNode.put(key, "") + fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) { + when (primitiveType) { + BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, false) + BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, 0) + BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, 0.0) + else -> objectNode.put(key, "") + } } - } - @JvmStatic - fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) { - if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) { - arrayNode.add(false) - } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) { - arrayNode.add(0) - } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) { - arrayNode.add(0.0) - } else { - arrayNode.add("") + fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) { + when (primitiveType) { + BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(false) + BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(0) + BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(0.0) + else -> arrayNode.add("") + } } - } - @JvmStatic - fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) { - if (nodeValue == null || nodeValue is NullNode) { - objectNode.set(key, nodeValue) - } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) { - if (BluePrintConstants.DATA_TYPE_BOOLEAN == type) { - objectNode.put(key, nodeValue.asBoolean()) - } else if (BluePrintConstants.DATA_TYPE_INTEGER == type) { - objectNode.put(key, nodeValue.asInt()) - } else if (BluePrintConstants.DATA_TYPE_FLOAT == type) { - objectNode.put(key, nodeValue.floatValue()) - } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == type) { - objectNode.put(key, nodeValue.asText()) + fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) { + if (nodeValue == null || nodeValue is NullNode) { + objectNode.set(key, nodeValue) + } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) { + populatePrimitiveValues(key, nodeValue, type, objectNode) } else { - objectNode.put(key, nodeValue.asText()) + objectNode.set(key, nodeValue) + } + } + + fun convertPrimitiveResourceValue(type: String, value: String): JsonNode? { + when (type) { + BluePrintConstants.DATA_TYPE_BOOLEAN -> return JacksonUtils.getJsonNode(value as Boolean) + BluePrintConstants.DATA_TYPE_INTEGER -> return JacksonUtils.getJsonNode(value as Int) + BluePrintConstants.DATA_TYPE_FLOAT -> return JacksonUtils.getJsonNode(value as Float) + else -> return JacksonUtils.getJsonNode(value) } - } else { - objectNode.set(key, nodeValue) } } - */ } \ No newline at end of file diff --git a/components/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeServiceTest.kt b/components/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeServiceTest.kt index 1dfb89a5d..c01b14b31 100644 --- a/components/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeServiceTest.kt +++ b/components/core/src/test/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintRuntimeServiceTest.kt @@ -26,8 +26,7 @@ import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.apps.controllerblueprints.core.asJsonPrimitive import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintMetadataUtils import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintRuntimeUtils -import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils.jsonNodeFromFile -import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils.jsonNodeFromObject +import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils import kotlin.test.assertEquals import kotlin.test.assertNotNull @@ -47,7 +46,7 @@ class BluePrintRuntimeServiceTest { val inputDataPath = "src/test/resources/data/default-context.json" - val inputNode: JsonNode = jsonNodeFromFile(inputDataPath) + val inputNode: JsonNode = JacksonUtils.jsonNodeFromFile(inputDataPath) bluePrintRuntimeService.assignInputs(inputNode) val propContext: MutableMap = bluePrintRuntimeService.resolveNodeTemplateProperties("activate-process") @@ -82,9 +81,9 @@ class BluePrintRuntimeServiceTest { "ResourceAssignmentComponent", "process") assertNotNull(inContext, "Failed to populate interface input property values") - assertEquals(inContext["action-name"], jsonNodeFromObject("sample-action"), "Failed to populate parameter action-name") - assertEquals(inContext["request-id"], jsonNodeFromObject("12345"), "Failed to populate parameter action-name") - } + assertEquals(inContext["action-name"], JacksonUtils.jsonNodeFromObject("sample-action"), "Failed to populate parameter action-name") + assertEquals(inContext["request-id"], JacksonUtils.jsonNodeFromObject("12345"), "Failed to populate parameter action-name") + } @Test fun testResolveNodeTemplateInterfaceOperationOutputs() { @@ -113,9 +112,9 @@ class BluePrintRuntimeServiceTest { val bluePrintRuntimeService = getBluePrintRuntimeService() bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment-ra-component", "context1", - jsonNodeFromObject("context1-value")) + JacksonUtils.jsonNodeFromObject("context1-value")) bluePrintRuntimeService.setNodeTemplateAttributeValue("resource-assignment-ra-component", "context2", - jsonNodeFromObject("context2-value")) + JacksonUtils.jsonNodeFromObject("context2-value")) val keys = listOf("context1", "context2") -- cgit 1.2.3-korg