2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.apps.controllerblueprints.core
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.*
21 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
22 import org.slf4j.helpers.MessageFormatter
24 import java.io.InputStream
25 import kotlin.reflect.KClass
30 * @author Brinda Santh
33 fun String.asJsonPrimitive(): TextNode {
37 fun Boolean.asJsonPrimitive(): BooleanNode {
38 return BooleanNode.valueOf(this)
41 fun Int.asJsonPrimitive(): IntNode {
42 return IntNode.valueOf(this)
45 fun Double.asJsonPrimitive(): DoubleNode {
46 return DoubleNode.valueOf(this)
49 fun MutableMap<String, *>.asJsonNode(): JsonNode {
50 return JacksonUtils.jsonNodeFromObject(this)
53 fun MutableMap<String, *>.asObjectNode(): ObjectNode {
54 return JacksonUtils.objectNodeFromObject(this)
57 fun format(message: String, vararg args: Any?): String {
58 if (args != null && args.isNotEmpty()) {
59 return MessageFormatter.arrayFormat(message, args).message
64 fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
65 if (containsKey(key)) {
72 fun <T : Any> MutableMap<String, *>.castValue(key: String, valueType: KClass<T>): T {
73 if (containsKey(key)) {
76 throw BluePrintException("couldn't find the key $key")
81 * Convert Json to map of json node, the root fields will be map keys
83 fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> {
84 if (this is ObjectNode) {
85 val propertyMap: MutableMap<String, JsonNode> = hashMapOf()
86 this.fields().forEach {
87 propertyMap[it.key] = it.value
91 throw BluePrintException("json node should be Object Node Type")
96 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
101 this[key] = TextNode(value)
103 this[key] = BooleanNode.valueOf(value)
105 this[key] = IntNode.valueOf(value.toInt())
107 this[key] = DoubleNode.valueOf(value.toDouble())
109 this[key] = JacksonUtils.jsonNodeFromObject(value)
113 fun MutableMap<String, JsonNode>.getAsString(key: String): String {
114 return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
117 fun MutableMap<String, JsonNode>.getAsBoolean(key: String): Boolean {
118 return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
121 fun MutableMap<String, JsonNode>.getAsInt(key: String): Int {
122 return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
125 fun MutableMap<String, JsonNode>.getAsDouble(key: String): Double {
126 return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
131 fun checkNotEmpty(value: String?): Boolean {
132 return value != null && value.isNotBlank()
135 fun checkNotEmptyOrThrow(value: String?, message: String? = value.plus(" is null/empty ")): Boolean {
136 val notEmpty = checkNotEmpty(value)
138 throw BluePrintException(message!!)
143 fun checkEqualsOrThrow(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
144 if (value1.equals(value2, ignoreCase = true)) {
147 throw BluePrintException(lazyMessage().toString())
151 fun nullToEmpty(value: String?): String {
152 return if (checkNotEmpty(value)) value!! else ""
155 fun returnNotEmptyOrThrow(value: String?, lazyMessage: () -> Any): String {
156 if (checkNotEmpty(value)) {
159 throw IllegalStateException(lazyMessage().toString())
163 fun InputStream.toFile(path: String): File {
164 val file = File(path)
165 file.outputStream().use { this.copyTo(it) }