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 format(message: String, vararg args: Any?): String {
54 if (args != null && args.isNotEmpty()) {
55 return MessageFormatter.arrayFormat(message, args).message
60 fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
61 if (containsKey(key)) {
68 fun <T : Any> MutableMap<String, *>.castValue(key: String, valueType: KClass<T>): T {
69 if (containsKey(key)) {
72 throw BluePrintException("couldn't find the key $key")
77 * Convert Json to map of json node, the root fields will be map keys
79 fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> {
80 if (this is ObjectNode) {
81 val propertyMap: MutableMap<String, JsonNode> = hashMapOf()
82 this.fields().forEach {
83 propertyMap[it.key] = it.value
87 throw BluePrintException("json node should be Object Node Type")
92 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
97 this[key] = TextNode(value)
99 this[key] = BooleanNode.valueOf(value)
101 this[key] = IntNode.valueOf(value.toInt())
103 this[key] = DoubleNode.valueOf(value.toDouble())
105 this[key] = JacksonUtils.jsonNodeFromObject(value)
109 fun MutableMap<String, JsonNode>.getAsString(key: String): String {
110 return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
113 fun MutableMap<String, JsonNode>.getAsBoolean(key: String): Boolean {
114 return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
117 fun MutableMap<String, JsonNode>.getAsInt(key: String): Int {
118 return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
121 fun MutableMap<String, JsonNode>.getAsDouble(key: String): Double {
122 return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
127 fun checkNotEmpty(value: String?): Boolean {
128 return value != null && value.isNotBlank()
131 fun checkNotEmptyOrThrow(value: String?, message: String? = value.plus(" is null/empty ")): Boolean {
132 val notEmpty = checkNotEmpty(value)
134 throw BluePrintException(message!!)
139 fun checkEqualsOrThrow(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
140 if (value1.equals(value2, ignoreCase = true)) {
143 throw BluePrintException(lazyMessage().toString())
147 fun nullToEmpty(value: String?): String {
148 return if (checkNotEmpty(value)) value!! else ""
151 fun returnNotEmptyOrThrow(value: String?, lazyMessage: () -> Any): String {
152 if (checkNotEmpty(value)) {
155 throw IllegalStateException(lazyMessage().toString())
159 fun InputStream.toFile(path: String): File {
160 val file = File(path)
161 file.outputStream().use { this.copyTo(it) }