Use protobuf JsonFormat
[ccsdk/apps.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / apps / blueprintsprocessor / selfservice / api / utils / BluePrintMappings.kt
index c8ce1c3..df17785 100644 (file)
  */
 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils
 
-import com.fasterxml.jackson.databind.node.JsonNodeFactory
 import com.fasterxml.jackson.databind.node.ObjectNode
 import com.google.common.base.Strings
 import com.google.protobuf.Struct
-import com.google.protobuf.Value
+import com.google.protobuf.util.JsonFormat
 import org.onap.ccsdk.apps.controllerblueprints.common.api.ActionIdentifiers
 import org.onap.ccsdk.apps.controllerblueprints.common.api.CommonHeader
 import org.onap.ccsdk.apps.controllerblueprints.common.api.Flag
 import org.onap.ccsdk.apps.controllerblueprints.common.api.Status
+import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceInput
 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput
 import java.text.SimpleDateFormat
@@ -31,47 +31,6 @@ import java.util.*
 
 private val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
 
-// STRUCT
-
-fun Struct.toJava(): ObjectNode {
-    val objectNode = JsonNodeFactory.instance.objectNode()
-    return getNode(objectNode)
-}
-
-fun Struct.getNode(objectNode: ObjectNode): ObjectNode {
-    this.fieldsMap.forEach {
-        when (it.value.kindCase.name) {
-            "BOOL_VALUE" -> objectNode.put(it.key, it.value.boolValue)
-            "KIND_NOT_SET" -> objectNode.put(it.key, it.value.toByteArray())
-            "LIST_VALUE" -> {
-                val arrayNode = JsonNodeFactory.instance.arrayNode()
-                it.value.listValue.valuesList.forEach { arrayNode.addPOJO(it.getValue()) }
-                objectNode.put(it.key, arrayNode)
-            }
-            "NULL_VALUE" -> objectNode.put(it.key, JsonNodeFactory.instance.nullNode())
-            "NUMBER_VALUE" -> objectNode.put(it.key, it.value.numberValue)
-            "STRING_VALUE" -> objectNode.put(it.key, it.value.stringValue)
-            "STRUCT_VALUE" -> objectNode.put(it.key, it.value.structValue.getNode(JsonNodeFactory.instance.objectNode()))
-        }
-    }
-    return objectNode
-}
-
-fun Value.getValue(): Any {
-    return when (this.kindCase.name) {
-        "BOOL_VALUE" -> this.boolValue
-        "KIND_NOT_SET" -> this.toByteArray()
-        "LIST_VALUE" -> listOf(this.listValue.valuesList.forEach { it.getValue() })
-        "NULL_VALUE" -> JsonNodeFactory.instance.nullNode()
-        "NUMBER_VALUE" -> this.numberValue
-        "STRING_VALUE" -> this.stringValue
-        "STRUCT_VALUE" -> this.structValue.getNode(JsonNodeFactory.instance.objectNode())
-        else -> {
-            "undefined"
-        }
-    }
-}
-
 // ACTION IDENTIFIER
 
 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers.toProto(): ActionIdentifiers {
@@ -152,17 +111,19 @@ fun ExecutionServiceInput.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core
     val executionServiceInput = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput()
     executionServiceInput.actionIdentifiers = this.actionIdentifiers.toJava()
     executionServiceInput.commonHeader = this.commonHeader.toJava()
-    executionServiceInput.payload = this.payload.toJava()
+    executionServiceInput.payload = JacksonUtils.jsonNode(JsonFormat.printer().print(this.payload)) as ObjectNode
     return executionServiceInput
 }
 
 // EXECUTION OUPUT
 
-fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput.toProto(payload: Struct): ExecutionServiceOutput {
+fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput.toProto(): ExecutionServiceOutput {
     val executionServiceOuput = ExecutionServiceOutput.newBuilder()
     executionServiceOuput.actionIdentifiers = this.actionIdentifiers.toProto()
     executionServiceOuput.commonHeader = this.commonHeader.toProto()
     executionServiceOuput.status = this.status.toProto()
-    executionServiceOuput.payload = payload
+    val struct = Struct.newBuilder()
+    JsonFormat.parser().merge(JacksonUtils.getJson(this.payload), struct)
+    executionServiceOuput.payload = struct.build()
     return executionServiceOuput.build()
 }
\ No newline at end of file