Improve service template access through cache.
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / CustomFunctions.kt
index a508c8f..0b640d9 100644 (file)
@@ -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,13 +31,22 @@ import kotlin.reflect.KClass
  * @author Brinda Santh
  */
 
+fun <T : Any> logger(clazz: T) = LoggerFactory.getLogger(clazz.javaClass)!!
+
+fun <T : KClass<*>> logger(clazz: T) = LoggerFactory.getLogger(clazz.java)!!
+
+
+fun <T : Any> 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, true)
+    return JacksonUtils.getJson(this, intend!!)
 }
 
 fun String.asJsonPrimitive(): TextNode {
@@ -59,8 +70,33 @@ fun Double.asJsonPrimitive(): DoubleNode {
     return DoubleNode.valueOf(this)
 }
 
+/**
+ * Utility to convert Primitive object to Json Type Primitive.
+ */
+fun <T : Any?> 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 : Any?> T.asJsonType(): JsonNode {
-    return if (this == null) {
+    return if (this == null || this is MissingNode || this is NullNode) {
         NullNode.instance
     } else {
         when (this) {
@@ -100,10 +136,10 @@ fun format(message: String, vararg args: Any?): String {
 }
 
 fun <T : Any> Map<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
-    if (containsKey(key)) {
-        return get(key) as? T
+    return if (containsKey(key)) {
+        get(key) as? T
     } else {
-        return null
+        null
     }
 }
 
@@ -125,12 +161,20 @@ fun JsonNode.returnNullIfMissing(): JsonNode? {
     } else this
 }
 
+fun <T : JsonNode> T?.isNull(): Boolean {
+    return this == null || this is NullNode || this is MissingNode
+}
+
+fun <T : JsonNode> 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<String, JsonNode> {
     if (this is ObjectNode) {
-        val propertyMap: MutableMap<String, JsonNode> = hashMapOf()
+        val propertyMap: MutableMap<String, JsonNode> = linkedMapOf()
         this.fields().forEach {
             propertyMap[it.key] = it.value
         }