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