X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=ms%2Fcontrollerblueprints%2Fmodules%2Fblueprint-core%2Fsrc%2Fmain%2Fkotlin%2Forg%2Fonap%2Fccsdk%2Fcds%2Fcontrollerblueprints%2Fcore%2FCustomFunctions.kt;h=0b640d9e9dabceb4656ab98eed599296ae8fe65b;hb=257dff5659f752d98dfe9421e9761aa5d84bd294;hp=72c2c1df3d02f6a1372e6a58d3b768febe21e959;hpb=81c79a84c39eb0de385ce327c4835466ccf4577b;p=ccsdk%2Fcds.git diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt index 72c2c1df3..0b640d9e9 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt @@ -19,7 +19,9 @@ package org.onap.ccsdk.cds.controllerblueprints.core import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.node.* +import org.apache.commons.lang3.ObjectUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.slf4j.LoggerFactory import org.slf4j.helpers.MessageFormatter import kotlin.reflect.KClass @@ -29,10 +31,33 @@ import kotlin.reflect.KClass * @author Brinda Santh */ +fun logger(clazz: T) = LoggerFactory.getLogger(clazz.javaClass)!! + +fun > logger(clazz: T) = LoggerFactory.getLogger(clazz.java)!! + + +fun T.bpClone(): T { + return ObjectUtils.clone(this) +} + +fun String.isJson(): Boolean { + return ((this.startsWith("{") && this.endsWith("}")) + || (this.startsWith("[") && this.endsWith("]"))) +} + +fun Any.asJsonString(intend: Boolean? = false): String { + return JacksonUtils.getJson(this, intend!!) +} + fun String.asJsonPrimitive(): TextNode { return TextNode(this) } +// If you know the string is json content, then use the function directly +fun String.jsonAsJsonType(): JsonNode { + return JacksonUtils.jsonNode(this.trim()) +} + fun Boolean.asJsonPrimitive(): BooleanNode { return BooleanNode.valueOf(this) } @@ -45,15 +70,44 @@ fun Double.asJsonPrimitive(): DoubleNode { return DoubleNode.valueOf(this) } +/** + * Utility to convert Primitive object to Json Type Primitive. + */ +fun T.asJsonPrimitive(): JsonNode { + return if (this == null || this is MissingNode || this is NullNode) { + NullNode.instance + } else { + when (this) { + is String -> + this.asJsonPrimitive() + is Boolean -> + this.asJsonPrimitive() + is Int -> + this.asJsonPrimitive() + is Double -> + this.asJsonPrimitive() + else -> + throw BluePrintException("$this type is not supported") + } + } +} + +/** + * Utility to convert Complex or Primitive object to Json Type. + */ fun T.asJsonType(): JsonNode { - return if (this == null) { + return if (this == null || this is MissingNode || this is NullNode) { NullNode.instance } else { when (this) { is JsonNode -> this - is String -> - TextNode(this) + is String -> { + if (this.isJson()) + this.jsonAsJsonType() + else + TextNode(this) + } is Boolean -> BooleanNode.valueOf(this) is Int -> @@ -82,10 +136,10 @@ fun format(message: String, vararg args: Any?): String { } fun Map.castOptionalValue(key: String, valueType: KClass): T? { - if (containsKey(key)) { - return get(key) as? T + return if (containsKey(key)) { + get(key) as? T } else { - return null + null } } @@ -101,12 +155,26 @@ fun ArrayNode.asListOfString(): List { return JacksonUtils.getListFromJsonNode(this, String::class.java) } +fun JsonNode.returnNullIfMissing(): JsonNode? { + return if (this is NullNode || this is MissingNode) { + null + } else this +} + +fun T?.isNull(): Boolean { + return this == null || this is NullNode || this is MissingNode +} + +fun T?.isNotNull(): Boolean { + return !(this == null || this is NullNode || this is MissingNode) +} + /** * Convert Json to map of json node, the root fields will be map keys */ fun JsonNode.rootFieldsToMap(): MutableMap { if (this is ObjectNode) { - val propertyMap: MutableMap = hashMapOf() + val propertyMap: MutableMap = linkedMapOf() this.fields().forEach { propertyMap[it.key] = it.value } @@ -116,6 +184,18 @@ fun JsonNode.rootFieldsToMap(): MutableMap { } } +fun JsonNode.removeNullNode() { + val it = this.iterator() + while (it.hasNext()) { + val child = it.next() + if (child.isNull) { + it.remove() + } else { + child.removeNullNode() + } + } +} + fun MutableMap.putJsonElement(key: String, value: Any) { val convertedValue = value.asJsonType()