2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.onap.ccsdk.cds.controllerblueprints.core
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.LoggerFactory
25 import org.slf4j.helpers.MessageFormatter
26 import kotlin.reflect.KClass
31 * @author Brinda Santh
34 fun <T : Any> logger(clazz: T) = LoggerFactory.getLogger(clazz.javaClass)!!
36 fun <T : KClass<*>> logger(clazz: T) = LoggerFactory.getLogger(clazz.java)!!
39 fun <T : Any> T.bpClone(): T {
40 return ObjectUtils.clone(this)
43 fun String.isJson(): Boolean {
44 return ((this.startsWith("{") && this.endsWith("}"))
45 || (this.startsWith("[") && this.endsWith("]")))
48 fun Any.asJsonString(intend: Boolean? = false): String {
49 return JacksonUtils.getJson(this, intend!!)
52 fun String.asJsonPrimitive(): TextNode {
56 // If you know the string is json content, then use the function directly
57 fun String.jsonAsJsonType(): JsonNode {
58 return JacksonUtils.jsonNode(this.trim())
61 fun Boolean.asJsonPrimitive(): BooleanNode {
62 return BooleanNode.valueOf(this)
65 fun Int.asJsonPrimitive(): IntNode {
66 return IntNode.valueOf(this)
69 fun Double.asJsonPrimitive(): DoubleNode {
70 return DoubleNode.valueOf(this)
74 * Utility to convert Primitive object to Json Type Primitive.
76 fun <T : Any?> T.asJsonPrimitive(): JsonNode {
77 return if (this == null || this is MissingNode || this is NullNode) {
82 this.asJsonPrimitive()
84 this.asJsonPrimitive()
86 this.asJsonPrimitive()
88 this.asJsonPrimitive()
90 throw BluePrintException("$this type is not supported")
96 * Utility to convert Complex or Primitive object to Json Type.
98 fun <T : Any?> T.asJsonType(): JsonNode {
99 return if (this == null || this is MissingNode || this is NullNode) {
107 this.jsonAsJsonType()
112 BooleanNode.valueOf(this)
114 IntNode.valueOf(this.toInt())
116 DoubleNode.valueOf(this.toDouble())
118 JacksonUtils.jsonNodeFromObject(this)
123 fun Map<String, *>.asJsonNode(): JsonNode {
124 return JacksonUtils.jsonNodeFromObject(this)
127 fun Map<String, *>.asObjectNode(): ObjectNode {
128 return JacksonUtils.objectNodeFromObject(this)
131 fun format(message: String, vararg args: Any?): String {
132 if (args != null && args.isNotEmpty()) {
133 return MessageFormatter.arrayFormat(message, args).message
138 fun <T : Any> Map<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
139 return if (containsKey(key)) {
146 fun <T : Any> Map<String, *>.castValue(key: String, valueType: KClass<T>): T {
147 if (containsKey(key)) {
150 throw BluePrintException("couldn't find the key $key")
154 fun ArrayNode.asListOfString(): List<String> {
155 return JacksonUtils.getListFromJsonNode(this, String::class.java)
158 fun JsonNode.returnNullIfMissing(): JsonNode? {
159 return if (this is NullNode || this is MissingNode) {
164 fun <T : JsonNode> T?.isNull(): Boolean {
165 return this == null || this is NullNode || this is MissingNode
168 fun <T : JsonNode> T?.isNotNull(): Boolean {
169 return !(this == null || this is NullNode || this is MissingNode)
173 * Convert Json to map of json node, the root fields will be map keys
175 fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> {
176 if (this is ObjectNode) {
177 val propertyMap: MutableMap<String, JsonNode> = linkedMapOf()
178 this.fields().forEach {
179 propertyMap[it.key] = it.value
183 throw BluePrintException("json node should be Object Node Type")
187 fun JsonNode.removeNullNode() {
188 val it = this.iterator()
189 while (it.hasNext()) {
190 val child = it.next()
194 child.removeNullNode()
200 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
201 val convertedValue = value.asJsonType()
202 this[key] = convertedValue
205 fun Map<String, JsonNode>.getAsString(key: String): String {
206 return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
209 fun Map<String, JsonNode>.getAsBoolean(key: String): Boolean {
210 return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
213 fun Map<String, JsonNode>.getAsInt(key: String): Int {
214 return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
217 fun Map<String, JsonNode>.getAsDouble(key: String): Double {
218 return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
223 inline fun checkEquals(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
224 if (value1.equals(value2, ignoreCase = true)) {
227 throw BluePrintException(lazyMessage().toString())
231 inline fun checkNotEmpty(value: String?, lazyMessage: () -> Any): String {
232 if (value == null || value.isEmpty()) {
233 val message = lazyMessage()
234 throw IllegalStateException(message.toString())
240 inline fun checkNotBlank(value: String?, lazyMessage: () -> Any): String {
241 if (value == null || value.isBlank()) {
242 val message = lazyMessage()
243 throw IllegalStateException(message.toString())
249 fun isNotEmpty(value: String?): Boolean {
250 return value != null && value.isNotEmpty()
253 fun isNotBlank(value: String?): Boolean {
254 return value != null && value.isNotBlank()
258 fun nullToEmpty(value: String?): String {
259 return if (isNotEmpty(value)) value!! else ""
262 inline fun <reified T : JsonNode> T.isComplexType(): Boolean {
263 return this is ObjectNode || this is ArrayNode