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.helpers.MessageFormatter
25 import kotlin.reflect.KClass
30 * @author Brinda Santh
33 fun <T : Any> T.bpClone(): T {
34 return ObjectUtils.clone(this)
37 fun String.isJson(): Boolean {
38 return ((this.startsWith("{") && this.endsWith("}"))
39 || (this.startsWith("[") && this.endsWith("]")))
42 fun Any.asJsonString(intend: Boolean? = false): String {
43 return JacksonUtils.getJson(this, intend!!)
46 fun String.asJsonPrimitive(): TextNode {
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())
55 fun Boolean.asJsonPrimitive(): BooleanNode {
56 return BooleanNode.valueOf(this)
59 fun Int.asJsonPrimitive(): IntNode {
60 return IntNode.valueOf(this)
63 fun Double.asJsonPrimitive(): DoubleNode {
64 return DoubleNode.valueOf(this)
67 fun <T : Any?> T.asJsonType(): JsonNode {
68 return if (this == null) {
81 BooleanNode.valueOf(this)
83 IntNode.valueOf(this.toInt())
85 DoubleNode.valueOf(this.toDouble())
87 JacksonUtils.jsonNodeFromObject(this)
92 fun Map<String, *>.asJsonNode(): JsonNode {
93 return JacksonUtils.jsonNodeFromObject(this)
96 fun Map<String, *>.asObjectNode(): ObjectNode {
97 return JacksonUtils.objectNodeFromObject(this)
100 fun format(message: String, vararg args: Any?): String {
101 if (args != null && args.isNotEmpty()) {
102 return MessageFormatter.arrayFormat(message, args).message
107 fun <T : Any> Map<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
108 return if (containsKey(key)) {
115 fun <T : Any> Map<String, *>.castValue(key: String, valueType: KClass<T>): T {
116 if (containsKey(key)) {
119 throw BluePrintException("couldn't find the key $key")
123 fun ArrayNode.asListOfString(): List<String> {
124 return JacksonUtils.getListFromJsonNode(this, String::class.java)
127 fun JsonNode.returnNullIfMissing(): JsonNode? {
128 return if (this is NullNode || this is MissingNode) {
133 fun <T : JsonNode> T?.isNull(): Boolean {
134 return if (this == null || this is NullNode || this is MissingNode) {
139 fun <T : JsonNode> T?.isNotNull(): Boolean {
140 return if (this == null || this is NullNode || this is MissingNode) {
146 * Convert Json to map of json node, the root fields will be map keys
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
156 throw BluePrintException("json node should be Object Node Type")
160 fun JsonNode.removeNullNode() {
161 val it = this.iterator()
162 while (it.hasNext()) {
163 val child = it.next()
167 child.removeNullNode()
173 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
174 val convertedValue = value.asJsonType()
175 this[key] = convertedValue
178 fun Map<String, JsonNode>.getAsString(key: String): String {
179 return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
182 fun Map<String, JsonNode>.getAsBoolean(key: String): Boolean {
183 return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
186 fun Map<String, JsonNode>.getAsInt(key: String): Int {
187 return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
190 fun Map<String, JsonNode>.getAsDouble(key: String): Double {
191 return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
196 inline fun checkEquals(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
197 if (value1.equals(value2, ignoreCase = true)) {
200 throw BluePrintException(lazyMessage().toString())
204 inline fun checkNotEmpty(value: String?, lazyMessage: () -> Any): String {
205 if (value == null || value.isEmpty()) {
206 val message = lazyMessage()
207 throw IllegalStateException(message.toString())
213 inline fun checkNotBlank(value: String?, lazyMessage: () -> Any): String {
214 if (value == null || value.isBlank()) {
215 val message = lazyMessage()
216 throw IllegalStateException(message.toString())
222 fun isNotEmpty(value: String?): Boolean {
223 return value != null && value.isNotEmpty()
226 fun isNotBlank(value: String?): Boolean {
227 return value != null && value.isNotBlank()
231 fun nullToEmpty(value: String?): String {
232 return if (isNotEmpty(value)) value!! else ""