72c2c1df3d02f6a1372e6a58d3b768febe21e959
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.node.*
22 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
23 import org.slf4j.helpers.MessageFormatter
24 import kotlin.reflect.KClass
25
26 /**
27  *
28  *
29  * @author Brinda Santh
30  */
31
32 fun String.asJsonPrimitive(): TextNode {
33     return TextNode(this)
34 }
35
36 fun Boolean.asJsonPrimitive(): BooleanNode {
37     return BooleanNode.valueOf(this)
38 }
39
40 fun Int.asJsonPrimitive(): IntNode {
41     return IntNode.valueOf(this)
42 }
43
44 fun Double.asJsonPrimitive(): DoubleNode {
45     return DoubleNode.valueOf(this)
46 }
47
48 fun <T : Any?> T.asJsonType(): JsonNode {
49     return if (this == null) {
50         NullNode.instance
51     } else {
52         when (this) {
53             is JsonNode ->
54                 this
55             is String ->
56                 TextNode(this)
57             is Boolean ->
58                 BooleanNode.valueOf(this)
59             is Int ->
60                 IntNode.valueOf(this.toInt())
61             is Double ->
62                 DoubleNode.valueOf(this.toDouble())
63             else ->
64                 JacksonUtils.jsonNodeFromObject(this)
65         }
66     }
67 }
68
69 fun Map<String, *>.asJsonNode(): JsonNode {
70     return JacksonUtils.jsonNodeFromObject(this)
71 }
72
73 fun Map<String, *>.asObjectNode(): ObjectNode {
74     return JacksonUtils.objectNodeFromObject(this)
75 }
76
77 fun format(message: String, vararg args: Any?): String {
78     if (args != null && args.isNotEmpty()) {
79         return MessageFormatter.arrayFormat(message, args).message
80     }
81     return message
82 }
83
84 fun <T : Any> Map<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
85     if (containsKey(key)) {
86         return get(key) as? T
87     } else {
88         return null
89     }
90 }
91
92 fun <T : Any> Map<String, *>.castValue(key: String, valueType: KClass<T>): T {
93     if (containsKey(key)) {
94         return get(key) as T
95     } else {
96         throw BluePrintException("couldn't find the key $key")
97     }
98 }
99
100 fun ArrayNode.asListOfString(): List<String> {
101     return JacksonUtils.getListFromJsonNode(this, String::class.java)
102 }
103
104 /**
105  * Convert Json to map of json node, the root fields will be map keys
106  */
107 fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> {
108     if (this is ObjectNode) {
109         val propertyMap: MutableMap<String, JsonNode> = hashMapOf()
110         this.fields().forEach {
111             propertyMap[it.key] = it.value
112         }
113         return propertyMap
114     } else {
115         throw BluePrintException("json node should be Object Node Type")
116     }
117 }
118
119
120 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
121     val convertedValue = value.asJsonType()
122     this[key] = convertedValue
123 }
124
125 fun Map<String, JsonNode>.getAsString(key: String): String {
126     return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
127 }
128
129 fun Map<String, JsonNode>.getAsBoolean(key: String): Boolean {
130     return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
131 }
132
133 fun Map<String, JsonNode>.getAsInt(key: String): Int {
134     return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
135 }
136
137 fun Map<String, JsonNode>.getAsDouble(key: String): Double {
138     return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
139 }
140
141 // Checks
142
143 inline fun checkEquals(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
144     if (value1.equals(value2, ignoreCase = true)) {
145         return true
146     } else {
147         throw BluePrintException(lazyMessage().toString())
148     }
149 }
150
151 inline fun checkNotEmpty(value: String?, lazyMessage: () -> Any): String {
152     if (value == null || value.isEmpty()) {
153         val message = lazyMessage()
154         throw IllegalStateException(message.toString())
155     } else {
156         return value
157     }
158 }
159
160 inline fun checkNotBlank(value: String?, lazyMessage: () -> Any): String {
161     if (value == null || value.isBlank()) {
162         val message = lazyMessage()
163         throw IllegalStateException(message.toString())
164     } else {
165         return value
166     }
167 }
168
169 fun isNotEmpty(value: String?): Boolean {
170     return value != null && value.isNotEmpty()
171 }
172
173 fun isNotBlank(value: String?): Boolean {
174     return value != null && value.isNotBlank()
175 }
176
177
178 fun nullToEmpty(value: String?): String {
179     return if (isNotEmpty(value)) value!! else ""
180 }
181
182