Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / utils / BluePrintMappings.kt
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.cds.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.cds.controllerblueprints.common.api.ActionIdentifiers
23 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
24 import org.onap.ccsdk.cds.controllerblueprints.common.api.EventType
25 import org.onap.ccsdk.cds.controllerblueprints.common.api.Flag
26 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
29 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
30 import java.text.SimpleDateFormat
31 import java.util.*
32
33 private val formatter = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
34
35 // ACTION IDENTIFIER
36
37 fun org.onap.ccsdk.cds.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()
44 }
45
46 fun ActionIdentifiers.toJava(): org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ActionIdentifiers {
47     val actionIdentifier = org.onap.ccsdk.cds.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
53 }
54
55 // COMMON HEADER
56
57 fun org.onap.ccsdk.cds.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()
65 }
66
67 fun CommonHeader.toJava(): org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.CommonHeader {
68     val commonHeader = org.onap.ccsdk.cds.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)
74     } else {
75         Date()
76     }
77     commonHeader.flags = this.flag?.toJava()
78     return commonHeader
79 }
80
81 // FLAG
82
83 fun org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.Flags.toProto(): Flag {
84     val flag = Flag.newBuilder()
85     flag.isForce = this.isForce
86     flag.ttl = this.ttl
87     return flag.build()
88 }
89
90 fun Flag.toJava(): org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.Flags {
91     val flag = org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.Flags()
92     flag.isForce = this.isForce
93     flag.ttl = this.ttl
94     return flag
95 }
96
97 // STATUS
98
99 fun org.onap.ccsdk.cds.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()
107 }
108
109 // EXECUTION INPUT
110
111 fun ExecutionServiceInput.toJava(): org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput {
112     val executionServiceInput = org.onap.ccsdk.cds.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
117 }
118
119 // EXECUTION OUPUT
120
121 fun org.onap.ccsdk.cds.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()
130 }