df17785c39011ee57628277418e73cdafae36881
[ccsdk/cds.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils
17
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.Flag
25 import org.onap.ccsdk.apps.controllerblueprints.common.api.Status
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
27 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceInput
28 import org.onap.ccsdk.apps.controllerblueprints.processing.api.ExecutionServiceOutput
29 import java.text.SimpleDateFormat
30 import java.util.*
31
32 private val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
33
34 // ACTION IDENTIFIER
35
36 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers.toProto(): ActionIdentifiers {
37     val actionIdentifier = ActionIdentifiers.newBuilder()
38     actionIdentifier.actionName = this.actionName
39     actionIdentifier.blueprintName = this.blueprintName
40     actionIdentifier.blueprintVersion = this.blueprintVersion
41     actionIdentifier.mode = this.mode
42     return actionIdentifier.build()
43 }
44
45 fun ActionIdentifiers.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers {
46     val actionIdentifier = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers()
47     actionIdentifier.actionName = this.actionName
48     actionIdentifier.blueprintName = this.blueprintName
49     actionIdentifier.blueprintVersion = this.blueprintVersion
50     actionIdentifier.mode = this.mode
51     return actionIdentifier
52 }
53
54 // COMMON HEADER
55
56 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader.toProto(): CommonHeader {
57     val commonHeader = CommonHeader.newBuilder()
58     commonHeader.originatorId = this.originatorId
59     commonHeader.requestId = this.requestId
60     commonHeader.subRequestId = this.subRequestId
61     commonHeader.timestamp = this.timestamp.toString()
62     commonHeader.flag = this.flags?.toProto()
63     return commonHeader.build()
64 }
65
66 fun CommonHeader.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader {
67     val commonHeader = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader()
68     commonHeader.originatorId = this.originatorId
69     commonHeader.requestId = this.requestId
70     commonHeader.subRequestId = this.subRequestId
71     commonHeader.timestamp = if (!Strings.isNullOrEmpty(this.timestamp)) {
72         formatter.parse(this.timestamp)
73     } else {
74         Date()
75     }
76     commonHeader.flags = this.flag?.toJava()
77     return commonHeader
78 }
79
80 // FLAG
81
82 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags.toProto(): Flag {
83     val flag = Flag.newBuilder()
84     flag.isForce = this.isForce
85     flag.ttl = this.ttl
86     return flag.build()
87 }
88
89 fun Flag.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags {
90     val flag = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags()
91     flag.isForce = this.isForce
92     flag.ttl = this.ttl
93     return flag
94 }
95
96 // STATUS
97
98 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status.toProto(): Status {
99     val status = Status.newBuilder()
100     status.code = this.code
101     status.errorMessage = this.errorMessage ?: ""
102     status.message = this.message
103     status.timestamp = this.timestamp.toString()
104     status.eventType = this.eventType
105     return status.build()
106 }
107
108 // EXECUTION INPUT
109
110 fun ExecutionServiceInput.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput {
111     val executionServiceInput = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput()
112     executionServiceInput.actionIdentifiers = this.actionIdentifiers.toJava()
113     executionServiceInput.commonHeader = this.commonHeader.toJava()
114     executionServiceInput.payload = JacksonUtils.jsonNode(JsonFormat.printer().print(this.payload)) as ObjectNode
115     return executionServiceInput
116 }
117
118 // EXECUTION OUPUT
119
120 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput.toProto(): ExecutionServiceOutput {
121     val executionServiceOuput = ExecutionServiceOutput.newBuilder()
122     executionServiceOuput.actionIdentifiers = this.actionIdentifiers.toProto()
123     executionServiceOuput.commonHeader = this.commonHeader.toProto()
124     executionServiceOuput.status = this.status.toProto()
125     val struct = Struct.newBuilder()
126     JsonFormat.parser().merge(JacksonUtils.getJson(this.payload), struct)
127     executionServiceOuput.payload = struct.build()
128     return executionServiceOuput.build()
129 }