7d98c42d152da66c9d465f06fcdfc27b49fff4e2
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.core
18
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
23 import java.io.File
24 import java.io.InputStream
25 import kotlin.reflect.KClass
26
27 /**
28  *
29  *
30  * @author Brinda Santh
31  */
32
33 fun String.asJsonPrimitive(): TextNode {
34     return TextNode(this)
35 }
36
37 fun Boolean.asJsonPrimitive(): BooleanNode {
38     return BooleanNode.valueOf(this)
39 }
40
41 fun Int.asJsonPrimitive(): IntNode {
42     return IntNode.valueOf(this)
43 }
44
45 fun Double.asJsonPrimitive(): DoubleNode {
46     return DoubleNode.valueOf(this)
47 }
48
49 fun MutableMap<String, *>.asJsonNode(): JsonNode {
50     return JacksonUtils.jsonNodeFromObject(this)
51 }
52
53 fun format(message: String, vararg args: Any?): String {
54     if (args != null && args.isNotEmpty()) {
55         return MessageFormatter.arrayFormat(message, args).message
56     }
57     return message
58 }
59
60 fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
61     if (containsKey(key)) {
62         return get(key) as? T
63     } else {
64         return null
65     }
66 }
67
68 fun <T : Any> MutableMap<String, *>.castValue(key: String, valueType: KClass<T>): T {
69     if (containsKey(key)) {
70         return get(key) as T
71     } else {
72         throw BluePrintException("couldn't find the key $key")
73     }
74 }
75
76 /**
77  * Convert Json to map of json node, the root fields will be map keys
78  */
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
84         }
85         return propertyMap
86     } else {
87         throw BluePrintException("json node should be Object Node Type")
88     }
89 }
90
91
92 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
93     when (value) {
94         is JsonNode ->
95             this[key] = value
96         is String ->
97             this[key] = TextNode(value)
98         is Boolean ->
99             this[key] = BooleanNode.valueOf(value)
100         is Int ->
101             this[key] = IntNode.valueOf(value.toInt())
102         is Double ->
103             this[key] = DoubleNode.valueOf(value.toDouble())
104         else ->
105             this[key] = JacksonUtils.jsonNodeFromObject(value)
106     }
107 }
108
109 fun MutableMap<String, JsonNode>.getAsString(key: String): String {
110     return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
111 }
112
113 fun MutableMap<String, JsonNode>.getAsBoolean(key: String): Boolean {
114     return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
115 }
116
117 fun MutableMap<String, JsonNode>.getAsInt(key: String): Int {
118     return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
119 }
120
121 fun MutableMap<String, JsonNode>.getAsDouble(key: String): Double {
122     return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
123 }
124
125 // Checks
126
127 fun checkNotEmpty(value: String?): Boolean {
128     return value != null && value.isNotBlank()
129 }
130
131 fun checkNotEmptyOrThrow(value: String?, message: String? = value.plus(" is null/empty ")): Boolean {
132     val notEmpty = checkNotEmpty(value)
133     if (!notEmpty) {
134         throw BluePrintException(message!!)
135     }
136     return notEmpty
137 }
138
139 fun checkEqualsOrThrow(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
140     if (value1.equals(value2, ignoreCase = true)) {
141         return true
142     } else {
143         throw BluePrintException(lazyMessage().toString())
144     }
145 }
146
147 fun nullToEmpty(value: String?): String {
148     return if (checkNotEmpty(value)) value!! else ""
149 }
150
151 fun returnNotEmptyOrThrow(value: String?, lazyMessage: () -> Any): String {
152     if (checkNotEmpty(value)) {
153         return value!!
154     } else {
155         throw IllegalStateException(lazyMessage().toString())
156     }
157 }
158
159 fun InputStream.toFile(path: String): File {
160     val file = File(path)
161     file.outputStream().use { this.copyTo(it) }
162     return file
163 }
164