42ff8827d87be73cd743618944c2f884dcb913f2
[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.apache.commons.lang3.ObjectUtils
23 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
24 import org.slf4j.helpers.MessageFormatter
25 import kotlin.reflect.KClass
26
27 /**
28  *
29  *
30  * @author Brinda Santh
31  */
32
33 fun <T : Any> T.bpClone(): T {
34     return ObjectUtils.clone(this)
35 }
36
37 fun String.isJson(): Boolean {
38     return ((this.startsWith("{") && this.endsWith("}"))
39             || (this.startsWith("[") && this.endsWith("]")))
40 }
41
42 fun Any.asJsonString(intend: Boolean? = false): String {
43     return JacksonUtils.getJson(this, intend!!)
44 }
45
46 fun String.asJsonPrimitive(): TextNode {
47     return TextNode(this)
48 }
49
50 // If you know the string is json content, then use the function directly
51 fun String.jsonAsJsonType(): JsonNode {
52     return JacksonUtils.jsonNode(this.trim())
53 }
54
55 fun Boolean.asJsonPrimitive(): BooleanNode {
56     return BooleanNode.valueOf(this)
57 }
58
59 fun Int.asJsonPrimitive(): IntNode {
60     return IntNode.valueOf(this)
61 }
62
63 fun Double.asJsonPrimitive(): DoubleNode {
64     return DoubleNode.valueOf(this)
65 }
66
67 fun <T : Any?> T.asJsonType(): JsonNode {
68     return if (this == null) {
69         NullNode.instance
70     } else {
71         when (this) {
72             is JsonNode ->
73                 this
74             is String -> {
75                 if (this.isJson())
76                     this.jsonAsJsonType()
77                 else
78                     TextNode(this)
79             }
80             is Boolean ->
81                 BooleanNode.valueOf(this)
82             is Int ->
83                 IntNode.valueOf(this.toInt())
84             is Double ->
85                 DoubleNode.valueOf(this.toDouble())
86             else ->
87                 JacksonUtils.jsonNodeFromObject(this)
88         }
89     }
90 }
91
92 fun Map<String, *>.asJsonNode(): JsonNode {
93     return JacksonUtils.jsonNodeFromObject(this)
94 }
95
96 fun Map<String, *>.asObjectNode(): ObjectNode {
97     return JacksonUtils.objectNodeFromObject(this)
98 }
99
100 fun format(message: String, vararg args: Any?): String {
101     if (args != null && args.isNotEmpty()) {
102         return MessageFormatter.arrayFormat(message, args).message
103     }
104     return message
105 }
106
107 fun <T : Any> Map<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
108     return if (containsKey(key)) {
109         get(key) as? T
110     } else {
111         null
112     }
113 }
114
115 fun <T : Any> Map<String, *>.castValue(key: String, valueType: KClass<T>): T {
116     if (containsKey(key)) {
117         return get(key) as T
118     } else {
119         throw BluePrintException("couldn't find the key $key")
120     }
121 }
122
123 fun ArrayNode.asListOfString(): List<String> {
124     return JacksonUtils.getListFromJsonNode(this, String::class.java)
125 }
126
127 fun JsonNode.returnNullIfMissing(): JsonNode? {
128     return if (this is NullNode || this is MissingNode) {
129         null
130     } else this
131 }
132
133 fun <T : JsonNode> T?.isNull(): Boolean {
134     return if (this == null || this is NullNode || this is MissingNode) {
135         true
136     } else false
137 }
138
139 fun <T : JsonNode> T?.isNotNull(): Boolean {
140     return if (this == null || this is NullNode || this is MissingNode) {
141         false
142     } else true
143 }
144
145 /**
146  * Convert Json to map of json node, the root fields will be map keys
147  */
148 fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> {
149     if (this is ObjectNode) {
150         val propertyMap: MutableMap<String, JsonNode> = hashMapOf()
151         this.fields().forEach {
152             propertyMap[it.key] = it.value
153         }
154         return propertyMap
155     } else {
156         throw BluePrintException("json node should be Object Node Type")
157     }
158 }
159
160 fun JsonNode.removeNullNode() {
161     val it = this.iterator()
162     while (it.hasNext()) {
163         val child = it.next()
164         if (child.isNull) {
165             it.remove()
166         } else {
167             child.removeNullNode()
168         }
169     }
170 }
171
172
173 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
174     val convertedValue = value.asJsonType()
175     this[key] = convertedValue
176 }
177
178 fun Map<String, JsonNode>.getAsString(key: String): String {
179     return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
180 }
181
182 fun Map<String, JsonNode>.getAsBoolean(key: String): Boolean {
183     return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
184 }
185
186 fun Map<String, JsonNode>.getAsInt(key: String): Int {
187     return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
188 }
189
190 fun Map<String, JsonNode>.getAsDouble(key: String): Double {
191     return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
192 }
193
194 // Checks
195
196 inline fun checkEquals(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
197     if (value1.equals(value2, ignoreCase = true)) {
198         return true
199     } else {
200         throw BluePrintException(lazyMessage().toString())
201     }
202 }
203
204 inline fun checkNotEmpty(value: String?, lazyMessage: () -> Any): String {
205     if (value == null || value.isEmpty()) {
206         val message = lazyMessage()
207         throw IllegalStateException(message.toString())
208     } else {
209         return value
210     }
211 }
212
213 inline fun checkNotBlank(value: String?, lazyMessage: () -> Any): String {
214     if (value == null || value.isBlank()) {
215         val message = lazyMessage()
216         throw IllegalStateException(message.toString())
217     } else {
218         return value
219     }
220 }
221
222 fun isNotEmpty(value: String?): Boolean {
223     return value != null && value.isNotEmpty()
224 }
225
226 fun isNotBlank(value: String?): Boolean {
227     return value != null && value.isNotBlank()
228 }
229
230
231 fun nullToEmpty(value: String?): String {
232     return if (isNotEmpty(value)) value!! else ""
233 }
234
235