2 * Copyright (C) 2019 Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils
18 import com.fasterxml.jackson.databind.node.JsonNodeFactory
19 import com.fasterxml.jackson.databind.node.ObjectNode
20 import com.google.common.base.Strings
21 import com.google.protobuf.Struct
22 import com.google.protobuf.Value
23 import com.google.protobuf.util.JsonFormat
24 import org.onap.ccsdk.apps.controllerblueprints.common.api.ActionIdentifiers
25 import org.onap.ccsdk.apps.controllerblueprints.common.api.CommonHeader
26 import org.onap.ccsdk.apps.controllerblueprints.common.api.Flag
27 import org.onap.ccsdk.apps.controllerblueprints.common.api.Status
28 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
29 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceInput
30 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput
31 import java.text.SimpleDateFormat
34 private val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
38 fun Struct.toJava(): ObjectNode {
39 val objectNode = JsonNodeFactory.instance.objectNode()
40 return getNode(objectNode)
43 fun Struct.getNode(objectNode: ObjectNode): ObjectNode {
44 this.fieldsMap.forEach {
45 when (it.value.kindCase.name) {
46 "BOOL_VALUE" -> objectNode.put(it.key, it.value.boolValue)
47 "KIND_NOT_SET" -> objectNode.put(it.key, it.value.toByteArray())
49 val arrayNode = JsonNodeFactory.instance.arrayNode()
50 it.value.listValue.valuesList.forEach { arrayNode.addPOJO(it.getValue()) }
51 objectNode.put(it.key, arrayNode)
53 "NULL_VALUE" -> objectNode.put(it.key, JsonNodeFactory.instance.nullNode())
54 "NUMBER_VALUE" -> objectNode.put(it.key, it.value.numberValue)
55 "STRING_VALUE" -> objectNode.put(it.key, it.value.stringValue)
56 "STRUCT_VALUE" -> objectNode.put(it.key, it.value.structValue.getNode(JsonNodeFactory.instance.objectNode()))
62 fun Value.getValue(): Any {
63 return when (this.kindCase.name) {
64 "BOOL_VALUE" -> this.boolValue
65 "KIND_NOT_SET" -> this.toByteArray()
66 "LIST_VALUE" -> listOf(this.listValue.valuesList.forEach { it.getValue() })
67 "NULL_VALUE" -> JsonNodeFactory.instance.nullNode()
68 "NUMBER_VALUE" -> this.numberValue
69 "STRING_VALUE" -> this.stringValue
70 "STRUCT_VALUE" -> this.structValue.getNode(JsonNodeFactory.instance.objectNode())
79 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers.toProto(): ActionIdentifiers {
80 val actionIdentifier = ActionIdentifiers.newBuilder()
81 actionIdentifier.actionName = this.actionName
82 actionIdentifier.blueprintName = this.blueprintName
83 actionIdentifier.blueprintVersion = this.blueprintVersion
84 actionIdentifier.mode = this.mode
85 return actionIdentifier.build()
88 fun ActionIdentifiers.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers {
89 val actionIdentifier = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers()
90 actionIdentifier.actionName = this.actionName
91 actionIdentifier.blueprintName = this.blueprintName
92 actionIdentifier.blueprintVersion = this.blueprintVersion
93 actionIdentifier.mode = this.mode
94 return actionIdentifier
99 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader.toProto(): CommonHeader {
100 val commonHeader = CommonHeader.newBuilder()
101 commonHeader.originatorId = this.originatorId
102 commonHeader.requestId = this.requestId
103 commonHeader.subRequestId = this.subRequestId
104 commonHeader.timestamp = this.timestamp.toString()
105 commonHeader.flag = this.flags?.toProto()
106 return commonHeader.build()
109 fun CommonHeader.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader {
110 val commonHeader = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader()
111 commonHeader.originatorId = this.originatorId
112 commonHeader.requestId = this.requestId
113 commonHeader.subRequestId = this.subRequestId
114 commonHeader.timestamp = if (!Strings.isNullOrEmpty(this.timestamp)) {
115 formatter.parse(this.timestamp)
119 commonHeader.flags = this.flag?.toJava()
125 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags.toProto(): Flag {
126 val flag = Flag.newBuilder()
127 flag.isForce = this.isForce
132 fun Flag.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags {
133 val flag = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags()
134 flag.isForce = this.isForce
141 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status.toProto(): Status {
142 val status = Status.newBuilder()
143 status.code = this.code
144 status.errorMessage = this.errorMessage ?: ""
145 status.message = this.message
146 status.timestamp = this.timestamp.toString()
147 status.eventType = this.eventType
148 return status.build()
153 fun ExecutionServiceInput.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput {
154 val executionServiceInput = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput()
155 executionServiceInput.actionIdentifiers = this.actionIdentifiers.toJava()
156 executionServiceInput.commonHeader = this.commonHeader.toJava()
157 executionServiceInput.payload = this.payload.toJava()
158 return executionServiceInput
163 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput.toProto(): ExecutionServiceOutput {
164 val executionServiceOuput = ExecutionServiceOutput.newBuilder()
165 executionServiceOuput.actionIdentifiers = this.actionIdentifiers.toProto()
166 executionServiceOuput.commonHeader = this.commonHeader.toProto()
167 executionServiceOuput.status = this.status.toProto()
168 val struct = Struct.newBuilder()
169 JsonFormat.parser().merge(JacksonUtils.getJson(this.payload), struct)
170 executionServiceOuput.payload = struct.build()
171 return executionServiceOuput.build()