From 10c2988b51c764e62d8eeed52b254d363512eb24 Mon Sep 17 00:00:00 2001 From: Brinda Santh Date: Wed, 18 Dec 2019 15:19:58 -0500 Subject: Cluster communication channels Add NATS property and library services both . NATS Messaging Services with Token Auth and TLS Auth implementation Docker Compose for NATS Streaming instance. Documentation : https://docs.nats.io/ Issue-ID: CCSDK-2007 Signed-off-by: Brinda Santh Change-Id: Ieebaa8f2b18ae89d02a4f38a8027eda495a9db43 --- .../controllerblueprints/core/CustomFunctions.kt | 29 +++++++++++++--------- .../core/utils/ClusterUtils.kt | 9 +++++++ .../core/CustomFunctionsTest.kt | 11 ++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) (limited to 'ms/blueprintsprocessor/modules/blueprints/blueprint-core/src') diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt index 5189d0758..4ab3d6f86 100644 --- a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt +++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt @@ -56,9 +56,13 @@ fun T.bpClone(): T { return ObjectUtils.clone(this) } +fun String.splitCommaAsList(): List { + return this.split(",").map { it.trim() }.toList() +} + fun String.isJson(): Boolean { return ((this.trim().startsWith("{") && this.trim().endsWith("}")) || - (this.trim().startsWith("[") && this.trim().endsWith("]"))) + (this.trim().startsWith("[") && this.trim().endsWith("]"))) } fun Any.asJsonString(intend: Boolean? = false): String { @@ -126,29 +130,25 @@ fun String.asJsonType(bpDataType: String): JsonNode { } /** - * Utility to convert Complex or Primitive object to Json Type. + * Utility to convert Complex or Primitive object or ByteArray to Json Type. */ fun T.asJsonType(): JsonNode { return if (this == null || this is MissingNode || this is NullNode) { NullNode.instance } else { when (this) { - is JsonNode -> - this + is JsonNode -> this + is ByteArray -> JacksonUtils.objectMapper.reader().readTree(this.inputStream()) is String -> { if (this.isJson()) this.jsonAsJsonType() else TextNode(this) } - is Boolean -> - BooleanNode.valueOf(this) - is Int -> - IntNode.valueOf(this.toInt()) - is Double -> - DoubleNode.valueOf(this.toDouble()) - else -> - JacksonUtils.jsonNodeFromObject(this) + is Boolean -> BooleanNode.valueOf(this) + is Int -> IntNode.valueOf(this.toInt()) + is Double -> DoubleNode.valueOf(this.toDouble()) + else -> JacksonUtils.jsonNodeFromObject(this) } } } @@ -188,6 +188,11 @@ fun ArrayNode.asListOfString(): List { return JacksonUtils.getListFromJsonNode(this, String::class.java) } +fun JsonNode.asByteArray(): ByteArray { + val writer = JacksonUtils.objectMapper.writer() + return writer.writeValueAsBytes(this) +} + fun JsonNode.asType(clazzType: Class): T { return JacksonUtils.readValue(this, clazzType) ?: throw BluePrintException("couldn't convert JsonNode of type $clazzType") diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/ClusterUtils.kt b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/ClusterUtils.kt index d3d621093..d5ffe6b7f 100644 --- a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/ClusterUtils.kt +++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/ClusterUtils.kt @@ -16,6 +16,7 @@ package org.onap.ccsdk.cds.controllerblueprints.core.utils +import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants import java.net.InetAddress object ClusterUtils { @@ -25,4 +26,12 @@ object ClusterUtils { val ip = InetAddress.getLocalHost() return ip.hostName } + + fun clusterId(): String { + return System.getProperty(BluePrintConstants.PROPERTY_CLUSTER_ID) ?: "cds-cluster" + } + + fun clusterNodeId(): String { + return System.getProperty(BluePrintConstants.PROPERTY_CLUSTER_NODE_ID) ?: "cds-controller" + } } diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctionsTest.kt b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctionsTest.kt index 3ae87b32d..7ac9b8649 100644 --- a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctionsTest.kt +++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctionsTest.kt @@ -67,6 +67,17 @@ class CustomFunctionsTest { assertFalse(returnValue.asBoolean()) } + @Test + fun testByteArrayJsonType() { + val jsonNode = """{"Name" :"Value"}""".jsonAsJsonType() + + val byteArray = jsonNode.asByteArray() + assertNotNull(byteArray, "failed to get ByteArray form Json") + + val reverseJsonNode = byteArray.asJsonType() + assertNotNull(reverseJsonNode, "failed to get Json type from ByteArray") + } + @Test fun testAsJsonType() { val nullReturnValue: JsonNode = null.asJsonType() -- cgit 1.2.3-korg