Use protobuf JsonFormat 57/79357/6
authorAlexis de Talhouët <adetalhouet89@gmail.com>
Thu, 28 Feb 2019 14:00:18 +0000 (09:00 -0500)
committerAlexis de Talhouët <adetalhouet89@gmail.com>
Mon, 4 Mar 2019 19:59:47 +0000 (14:59 -0500)
Instead of manually parsing Struct to ObjectNode, uses
JsonFormat

Change-Id: I5a457f10e3d106f189f9636fbcc166b12e3915fd
Issue-ID: CCSDK-947
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt
ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/BluePrintProcessingGRPCHandler.kt
ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/ExecutionServiceHandler.kt
ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/utils/BluePrintMappings.kt
ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/test/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/utils/BluePrintMappingTests.kt

index 93b93fe..1c9a905 100644 (file)
@@ -23,7 +23,13 @@ import com.fasterxml.jackson.databind.ObjectMapper
 import com.fasterxml.jackson.databind.node.NullNode
 import com.fasterxml.jackson.databind.node.ObjectNode
 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
-import org.onap.ccsdk.apps.controllerblueprints.core.*
+import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
+import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
+import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
+import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmpty
+import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow
+import org.onap.ccsdk.apps.controllerblueprints.core.nullToEmpty
+import org.onap.ccsdk.apps.controllerblueprints.core.returnNotEmptyOrThrow
 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintRuntimeService
 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
 import org.onap.ccsdk.apps.controllerblueprints.resource.dict.ResourceAssignment
@@ -41,13 +47,13 @@ class ResourceAssignmentUtils {
             val resourceProp = checkNotNull(resourceAssignment.property) { "Failed in setting resource value for resource mapping $resourceAssignment" }
             checkNotEmptyOrThrow(resourceAssignment.name, "Failed in setting resource value for resource mapping $resourceAssignment")
 
-            if (checkNotEmpty(resourceAssignment.dictionaryName)) {
+            if (resourceAssignment.dictionaryName.isNullOrEmpty()) {
                 resourceAssignment.dictionaryName = resourceAssignment.name
                 logger.warn("Missing dictionary key, setting with template key (${resourceAssignment.name}) as dictionary key (${resourceAssignment.dictionaryName})")
             }
 
             try {
-                if (checkNotEmpty(resourceProp.type)) {
+                if (resourceProp.type.isNotEmpty()) {
                     val convertedValue = convertResourceValue(resourceProp.type, value)
                     logger.info("Setting Resource Value ($convertedValue) for Resource Name (${resourceAssignment.dictionaryName}) of type (${resourceProp.type})")
                     setResourceValue(resourceAssignment, raRuntimeService, convertedValue)
index cf6776c..edb1d31 100644 (file)
@@ -37,8 +37,7 @@ class BluePrintProcessingGRPCHandler(private val bluePrintCoreConfiguration: Blu
         return object : StreamObserver<ExecutionServiceInput> {
             override fun onNext(executionServiceInput: ExecutionServiceInput) {
                 try {
-                    val inputPayload = executionServiceInput.payload
-                    executionServiceHandler.process(executionServiceInput.toJava(), responseObserver, inputPayload)
+                    executionServiceHandler.process(executionServiceInput.toJava(), responseObserver)
                 } catch (e: Exception) {
                     onError(e)
                 }
index 4447dd4..c53f039 100644 (file)
@@ -17,7 +17,6 @@
 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api
 
 import com.fasterxml.jackson.databind.node.JsonNodeFactory
-import com.google.protobuf.Struct
 import io.grpc.stub.StreamObserver
 import kotlinx.coroutines.Dispatchers
 import kotlinx.coroutines.GlobalScope
@@ -61,8 +60,7 @@ class ExecutionServiceHandler(private val bluePrintCoreConfiguration: BluePrintC
     }
 
     fun process(executionServiceInput: ExecutionServiceInput,
-                responseObserver: StreamObserver<org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput>,
-                inputPayload: Struct) {
+                responseObserver: StreamObserver<org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput>) {
         when {
             executionServiceInput.actionIdentifiers.mode == ACTION_MODE_ASYNC -> {
                 GlobalScope.launch(Dispatchers.Default) {
index b261c41..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
@@ -33,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 {
@@ -154,7 +111,7 @@ 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
 }
 
index 2e4ba27..04a2c1e 100644 (file)
@@ -1,11 +1,5 @@
 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils
 
-import com.fasterxml.jackson.databind.ObjectMapper
-import com.google.protobuf.ListValue
-import com.google.protobuf.NullValue
-import com.google.protobuf.Struct
-import com.google.protobuf.Value
-import com.google.protobuf.util.JsonFormat
 import org.junit.Assert
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -134,39 +128,4 @@ class BluePrintMappingsTest {
         Assert.assertEquals(actionIdentifiers.blueprintVersion, actionIdentifiers2.blueprintVersion)
         Assert.assertEquals(actionIdentifiers.mode, actionIdentifiers2.mode)
     }
-
-    @Test
-    fun testStructToJava() {
-        val struct = Struct.newBuilder().putAllFields(createValues()).build()
-        val struct2 = struct.toJava()
-
-        val mapper = ObjectMapper()
-
-        Assert.assertEquals(JsonFormat.printer().print(struct).replace(" ", "").replace("\r",""),
-                mapper.writerWithDefaultPrettyPrinter().writeValueAsString(struct2).replace(" ", "").replace("\r",""))
-    }
-
-    fun createValues(): Map<String, Value> {
-        val map = mutableMapOf<String, Value>()
-
-        val boolValue = Value.newBuilder().setBoolValue(true).build()
-        val stringValue = Value.newBuilder().setStringValue("string").build()
-        val doubleValue = Value.newBuilder().setNumberValue(Double.MAX_VALUE).build()
-        val jsonValue = Value.newBuilder().setStringValue("{\"bblah\": \"bbblo\"}").build()
-        val listValue = Value.newBuilder().setListValue(ListValue.newBuilder().addValues(boolValue).addValues(boolValue).build()).build()
-        val nullValue = Value.newBuilder().setNullValue(NullValue.NULL_VALUE).build()
-
-        map.put("bool", boolValue)
-        map.put("string", stringValue)
-        map.put("doublbe", doubleValue)
-        map.put("json", jsonValue)
-        map.put("list", listValue)
-        map.put("null", nullValue)
-
-        val structValue = Value.newBuilder().setStructValue(Struct.newBuilder().putAllFields(map).build()).build()
-
-        map.put("struct", structValue)
-
-        return map
-    }
 }
\ No newline at end of file