2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package org.onap.ccsdk.apps.controllerblueprints.core
\r
19 import com.fasterxml.jackson.databind.JsonNode
\r
20 import com.fasterxml.jackson.databind.node.BooleanNode
\r
21 import com.fasterxml.jackson.databind.node.DoubleNode
\r
22 import com.fasterxml.jackson.databind.node.IntNode
\r
23 import com.fasterxml.jackson.databind.node.TextNode
\r
24 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
\r
25 import org.slf4j.helpers.MessageFormatter
\r
27 import java.io.InputStream
\r
28 import kotlin.reflect.KClass
\r
33 * @author Brinda Santh
\r
36 fun String.asJsonPrimitive(): TextNode {
\r
37 return TextNode(this)
\r
40 fun Boolean.asJsonPrimitive(): BooleanNode {
\r
41 return BooleanNode.valueOf(this)
\r
44 fun Int.asJsonPrimitive(): IntNode {
\r
45 return IntNode.valueOf(this)
\r
48 fun Double.asJsonPrimitive(): DoubleNode {
\r
49 return DoubleNode.valueOf(this)
\r
52 fun MutableMap<String, *>.asJsonNode(): JsonNode {
\r
53 return JacksonUtils.jsonNodeFromObject(this)
\r
56 fun format(message: String, vararg args: Any?): String {
\r
57 if (args != null && args.isNotEmpty()) {
\r
58 return MessageFormatter.arrayFormat(message, args).message
\r
63 fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
\r
64 if (containsKey(key)) {
\r
65 return get(key) as? T
\r
71 fun <T : Any> MutableMap<String, *>.castValue(key: String, valueType: KClass<T>): T {
\r
72 if (containsKey(key)) {
\r
73 return get(key) as T
\r
75 throw BluePrintException("couldn't find the key $key")
\r
79 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
\r
84 this[key] = TextNode(value)
\r
86 this[key] = BooleanNode.valueOf(value)
\r
88 this[key] = IntNode.valueOf(value.toInt())
\r
90 this[key] = DoubleNode.valueOf(value.toDouble())
\r
92 this[key] = JacksonUtils.jsonNodeFromObject(value)
\r
96 fun MutableMap<String, JsonNode>.getAsString(key: String): String {
\r
97 return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
\r
100 fun MutableMap<String, JsonNode>.getAsBoolean(key: String): Boolean {
\r
101 return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
\r
104 fun MutableMap<String, JsonNode>.getAsInt(key: String): Int {
\r
105 return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
\r
108 fun MutableMap<String, JsonNode>.getAsDouble(key: String): Double {
\r
109 return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
\r
114 fun checkNotEmpty(value: String?): Boolean {
\r
115 return value != null && value.isNotBlank()
\r
118 fun checkNotEmptyOrThrow(value: String?, message: String? = value.plus(" is null/empty ")): Boolean {
\r
119 val notEmpty = checkNotEmpty(value)
\r
121 throw BluePrintException(message!!)
\r
126 fun checkEqualsOrThrow(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
\r
127 if (value1.equals(value2, ignoreCase = true)) {
\r
130 throw BluePrintException(lazyMessage().toString())
\r
134 fun nullToEmpty(value: String?): String {
\r
135 return if (checkNotEmpty(value)) value!! else ""
\r
138 fun returnNotEmptyOrThrow(value: String?, lazyMessage: () -> Any): String {
\r
139 if (checkNotEmpty(value)) {
\r
142 throw IllegalStateException(lazyMessage().toString())
\r
146 fun InputStream.toFile(path: String): File {
\r
147 val file = File(path)
\r
148 file.outputStream().use { this.copyTo(it) }
\r