Merge "Test condition to AssignVlanTagRequestInputTest"
[ccsdk/apps.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / main / kotlin / org / onap / ccsdk / apps / 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.apps.blueprintsprocessor.selfservice.api.utils
17
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 org.onap.ccsdk.apps.controllerblueprints.common.api.ActionIdentifiers
24 import org.onap.ccsdk.apps.controllerblueprints.common.api.CommonHeader
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.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 // STRUCT
35
36 fun Struct.toJava(): ObjectNode {
37     val objectNode = JsonNodeFactory.instance.objectNode()
38     return getNode(objectNode)
39 }
40
41 fun Struct.getNode(objectNode: ObjectNode): ObjectNode {
42     this.fieldsMap.forEach {
43         when (it.value.kindCase.name) {
44             "BOOL_VALUE" -> objectNode.put(it.key, it.value.boolValue)
45             "KIND_NOT_SET" -> objectNode.put(it.key, it.value.toByteArray())
46             "LIST_VALUE" -> {
47                 val arrayNode = JsonNodeFactory.instance.arrayNode()
48                 it.value.listValue.valuesList.forEach { arrayNode.addPOJO(it.getValue()) }
49                 objectNode.put(it.key, arrayNode)
50             }
51             "NULL_VALUE" -> objectNode.put(it.key, JsonNodeFactory.instance.nullNode())
52             "NUMBER_VALUE" -> objectNode.put(it.key, it.value.numberValue)
53             "STRING_VALUE" -> objectNode.put(it.key, it.value.stringValue)
54             "STRUCT_VALUE" -> objectNode.put(it.key, it.value.structValue.getNode(JsonNodeFactory.instance.objectNode()))
55         }
56     }
57     return objectNode
58 }
59
60 fun Value.getValue(): Any {
61     return when (this.kindCase.name) {
62         "BOOL_VALUE" -> this.boolValue
63         "KIND_NOT_SET" -> this.toByteArray()
64         "LIST_VALUE" -> listOf(this.listValue.valuesList.forEach { it.getValue() })
65         "NULL_VALUE" -> JsonNodeFactory.instance.nullNode()
66         "NUMBER_VALUE" -> this.numberValue
67         "STRING_VALUE" -> this.stringValue
68         "STRUCT_VALUE" -> this.structValue.getNode(JsonNodeFactory.instance.objectNode())
69         else -> {
70             "undefined"
71         }
72     }
73 }
74
75 // ACTION IDENTIFIER
76
77 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers.toProto(): ActionIdentifiers {
78     val actionIdentifier = ActionIdentifiers.newBuilder()
79     actionIdentifier.actionName = this.actionName
80     actionIdentifier.blueprintName = this.blueprintName
81     actionIdentifier.blueprintVersion = this.blueprintVersion
82     actionIdentifier.mode = this.mode
83     return actionIdentifier.build()
84 }
85
86 fun ActionIdentifiers.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers {
87     val actionIdentifier = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ActionIdentifiers()
88     actionIdentifier.actionName = this.actionName
89     actionIdentifier.blueprintName = this.blueprintName
90     actionIdentifier.blueprintVersion = this.blueprintVersion
91     actionIdentifier.mode = this.mode
92     return actionIdentifier
93 }
94
95 // COMMON HEADER
96
97 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader.toProto(): CommonHeader {
98     val commonHeader = CommonHeader.newBuilder()
99     commonHeader.originatorId = this.originatorId
100     commonHeader.requestId = this.requestId
101     commonHeader.subRequestId = this.subRequestId
102     commonHeader.timestamp = this.timestamp.toString()
103     commonHeader.flag = this.flags?.toProto()
104     return commonHeader.build()
105 }
106
107 fun CommonHeader.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader {
108     val commonHeader = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.CommonHeader()
109     commonHeader.originatorId = this.originatorId
110     commonHeader.requestId = this.requestId
111     commonHeader.subRequestId = this.subRequestId
112     commonHeader.timestamp = if (!Strings.isNullOrEmpty(this.timestamp)) {
113         formatter.parse(this.timestamp)
114     } else {
115         Date()
116     }
117     commonHeader.flags = this.flag?.toJava()
118     return commonHeader
119 }
120
121 // FLAG
122
123 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags.toProto(): Flag {
124     val flag = Flag.newBuilder()
125     flag.isForce = this.isForce
126     flag.ttl = this.ttl
127     return flag.build()
128 }
129
130 fun Flag.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags {
131     val flag = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Flags()
132     flag.isForce = this.isForce
133     flag.ttl = this.ttl
134     return flag
135 }
136
137 // STATUS
138
139 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.Status.toProto(): Status {
140     val status = Status.newBuilder()
141     status.code = this.code
142     status.errorMessage = this.errorMessage
143     status.message = this.message
144     status.timestamp = this.timestamp.toString()
145     status.eventType = this.eventType
146     return status.build()
147 }
148
149 // EXECUTION INPUT
150
151 fun ExecutionServiceInput.toJava(): org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput {
152     val executionServiceInput = org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput()
153     executionServiceInput.actionIdentifiers = this.actionIdentifiers.toJava()
154     executionServiceInput.commonHeader = this.commonHeader.toJava()
155     executionServiceInput.payload = this.payload.toJava()
156     return executionServiceInput
157 }
158
159 // EXECUTION OUPUT
160
161 fun org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceOutput.toProto(payload: Struct): ExecutionServiceOutput {
162     val executionServiceOuput = ExecutionServiceOutput.newBuilder()
163     executionServiceOuput.actionIdentifiers = this.actionIdentifiers.toProto()
164     executionServiceOuput.commonHeader = this.commonHeader.toProto()
165     executionServiceOuput.status = this.status.toProto()
166     executionServiceOuput.payload = payload
167     return executionServiceOuput.build()
168 }