Remote Script Executor Component
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / processor-core / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / core / ApiDataExtensions.kt
index af71094..8e1b5b2 100644 (file)
@@ -1,5 +1,6 @@
 /*
  *  Copyright © 2019 IBM.
+ *  Modifications Copyright © 2018-2019 AT&T Intellectual Property.
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 
 package org.onap.ccsdk.cds.blueprintsprocessor.core
 
+import com.fasterxml.jackson.databind.JsonNode
+import com.google.protobuf.Struct
+import com.google.protobuf.util.JsonFormat
 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
+import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
 import org.onap.ccsdk.cds.controllerblueprints.core.asType
+import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
 import kotlin.reflect.KClass
 
 fun <T : Any> ExecutionServiceInput.payloadAsType(clazzType: KClass<T>): T {
@@ -25,3 +31,25 @@ fun <T : Any> ExecutionServiceInput.payloadAsType(clazzType: KClass<T>): T {
     val requestJsonNode = this.payload.get("$actionName-request")
     return requestJsonNode.asType(clazzType.java)
 }
+
+/** Convert Proto Struct to Json string */
+fun Struct.asJson(): String {
+    return JsonFormat.printer().print(this)
+}
+
+/** Convert Proto Struct to Json node */
+fun Struct.asJsonType(): JsonNode {
+    return this.asJson().jsonAsJsonType()
+}
+
+/** Convert Json node to Proto Struct */
+fun JsonNode.asProtoStruct(): Struct {
+    return this.asJsonString(false).asProtoStruct()
+}
+
+/** Convert Json string to Proto Struct */
+fun String.asProtoStruct(): Struct {
+    val struct = Struct.newBuilder()
+    JsonFormat.parser().merge(this, struct)
+    return struct.build()
+}