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.ObjectNode
19 import com.google.common.base.Strings
20 import com.google.protobuf.Struct
21 import com.google.protobuf.util.JsonFormat
22 import org.onap.ccsdk.apps.controllerblueprints.common.api.ActionIdentifiers
23 import org.onap.ccsdk.apps.controllerblueprints.common.api.CommonHeader
24 import org.onap.ccsdk.apps.controllerblueprints.common.api.EventType
25 import org.onap.ccsdk.apps.controllerblueprints.common.api.Flag
26 import org.onap.ccsdk.apps.controllerblueprints.common.api.Status
27 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceInput
29 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput
30 import java.text.SimpleDateFormat
33 private val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
37 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers.toProto(): ActionIdentifiers {
38 val actionIdentifier = ActionIdentifiers.newBuilder()
39 actionIdentifier.actionName = this.actionName
40 actionIdentifier.blueprintName = this.blueprintName
41 actionIdentifier.blueprintVersion = this.blueprintVersion
42 actionIdentifier.mode = this.mode
43 return actionIdentifier.build()
46 fun ActionIdentifiers.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers {
47 val actionIdentifier = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers()
48 actionIdentifier.actionName = this.actionName
49 actionIdentifier.blueprintName = this.blueprintName
50 actionIdentifier.blueprintVersion = this.blueprintVersion
51 actionIdentifier.mode = this.mode
52 return actionIdentifier
57 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader.toProto(): CommonHeader {
58 val commonHeader = CommonHeader.newBuilder()
59 commonHeader.originatorId = this.originatorId
60 commonHeader.requestId = this.requestId
61 commonHeader.subRequestId = this.subRequestId
62 commonHeader.timestamp = this.timestamp.toString()
63 commonHeader.flag = this.flags?.toProto()
64 return commonHeader.build()
67 fun CommonHeader.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader {
68 val commonHeader = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader()
69 commonHeader.originatorId = this.originatorId
70 commonHeader.requestId = this.requestId
71 commonHeader.subRequestId = this.subRequestId
72 commonHeader.timestamp = if (!Strings.isNullOrEmpty(this.timestamp)) {
73 formatter.parse(this.timestamp)
77 commonHeader.flags = this.flag?.toJava()
83 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags.toProto(): Flag {
84 val flag = Flag.newBuilder()
85 flag.isForce = this.isForce
90 fun Flag.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags {
91 val flag = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags()
92 flag.isForce = this.isForce
99 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status.toProto(): Status {
100 val status = Status.newBuilder()
101 status.code = this.code
102 status.errorMessage = this.errorMessage ?: ""
103 status.message = this.message
104 status.timestamp = this.timestamp.toString()
105 status.eventType = EventType.valueOf(this.eventType)
106 return status.build()
111 fun ExecutionServiceInput.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput {
112 val executionServiceInput = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput()
113 executionServiceInput.actionIdentifiers = this.actionIdentifiers.toJava()
114 executionServiceInput.commonHeader = this.commonHeader.toJava()
115 executionServiceInput.payload = JacksonUtils.jsonNode(JsonFormat.printer().print(this.payload)) as ObjectNode
116 return executionServiceInput
121 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput.toProto(): ExecutionServiceOutput {
122 val executionServiceOuput = ExecutionServiceOutput.newBuilder()
123 executionServiceOuput.actionIdentifiers = this.actionIdentifiers.toProto()
124 executionServiceOuput.commonHeader = this.commonHeader.toProto()
125 executionServiceOuput.status = this.status.toProto()
126 val struct = Struct.newBuilder()
127 JsonFormat.parser().merge(JacksonUtils.getJson(this.payload), struct)
128 executionServiceOuput.payload = struct.build()
129 return executionServiceOuput.build()