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.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
23 import org.slf4j.helpers.MessageFormatter
24 import kotlin.reflect.KClass
29 * @author Brinda Santh
32 fun String.asJsonPrimitive(): TextNode {
36 fun Boolean.asJsonPrimitive(): BooleanNode {
37 return BooleanNode.valueOf(this)
40 fun Int.asJsonPrimitive(): IntNode {
41 return IntNode.valueOf(this)
44 fun Double.asJsonPrimitive(): DoubleNode {
45 return DoubleNode.valueOf(this)
48 fun <T : Any?> T.asJsonType(): JsonNode {
49 return if (this == null) {
58 BooleanNode.valueOf(this)
60 IntNode.valueOf(this.toInt())
62 DoubleNode.valueOf(this.toDouble())
64 JacksonUtils.jsonNodeFromObject(this)
69 fun Map<String, *>.asJsonNode(): JsonNode {
70 return JacksonUtils.jsonNodeFromObject(this)
73 fun Map<String, *>.asObjectNode(): ObjectNode {
74 return JacksonUtils.objectNodeFromObject(this)
77 fun format(message: String, vararg args: Any?): String {
78 if (args != null && args.isNotEmpty()) {
79 return MessageFormatter.arrayFormat(message, args).message
84 fun <T : Any> Map<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
85 if (containsKey(key)) {
92 fun <T : Any> Map<String, *>.castValue(key: String, valueType: KClass<T>): T {
93 if (containsKey(key)) {
96 throw BluePrintException("couldn't find the key $key")
101 * Convert Json to map of json node, the root fields will be map keys
103 fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> {
104 if (this is ObjectNode) {
105 val propertyMap: MutableMap<String, JsonNode> = hashMapOf()
106 this.fields().forEach {
107 propertyMap[it.key] = it.value
111 throw BluePrintException("json node should be Object Node Type")
116 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
117 val convertedValue = value.asJsonType()
118 this[key] = convertedValue
121 fun Map<String, JsonNode>.getAsString(key: String): String {
122 return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
125 fun Map<String, JsonNode>.getAsBoolean(key: String): Boolean {
126 return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
129 fun Map<String, JsonNode>.getAsInt(key: String): Int {
130 return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
133 fun Map<String, JsonNode>.getAsDouble(key: String): Double {
134 return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
139 inline fun checkEquals(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
140 if (value1.equals(value2, ignoreCase = true)) {
143 throw BluePrintException(lazyMessage().toString())
147 inline fun checkNotEmpty(value: String?, lazyMessage: () -> Any): String {
148 if (value == null || value.isEmpty()) {
149 val message = lazyMessage()
150 throw IllegalStateException(message.toString())
156 inline fun checkNotBlank(value: String?, lazyMessage: () -> Any): String {
157 if (value == null || value.isBlank()) {
158 val message = lazyMessage()
159 throw IllegalStateException(message.toString())
165 fun isNotEmpty(value: String?): Boolean {
166 return value != null && value.isNotEmpty()
169 fun isNotBlank(value: String?): Boolean {
170 return value != null && value.isNotBlank()
174 fun nullToEmpty(value: String?): String {
175 return if (isNotEmpty(value)) value!! else ""