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