462935d61bc1f24a6744001b0966f8228ed5eb43
[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 MutableMap<String, *>.asObjectNode(): ObjectNode {
54     return JacksonUtils.objectNodeFromObject(this)
55 }
56
57 fun format(message: String, vararg args: Any?): String {
58     if (args != null && args.isNotEmpty()) {
59         return MessageFormatter.arrayFormat(message, args).message
60     }
61     return message
62 }
63
64 fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
65     if (containsKey(key)) {
66         return get(key) as? T
67     } else {
68         return null
69     }
70 }
71
72 fun <T : Any> MutableMap<String, *>.castValue(key: String, valueType: KClass<T>): T {
73     if (containsKey(key)) {
74         return get(key) as T
75     } else {
76         throw BluePrintException("couldn't find the key $key")
77     }
78 }
79
80 /**
81  * Convert Json to map of json node, the root fields will be map keys
82  */
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
88         }
89         return propertyMap
90     } else {
91         throw BluePrintException("json node should be Object Node Type")
92     }
93 }
94
95
96 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
97     when (value) {
98         is JsonNode ->
99             this[key] = value
100         is String ->
101             this[key] = TextNode(value)
102         is Boolean ->
103             this[key] = BooleanNode.valueOf(value)
104         is Int ->
105             this[key] = IntNode.valueOf(value.toInt())
106         is Double ->
107             this[key] = DoubleNode.valueOf(value.toDouble())
108         else ->
109             this[key] = JacksonUtils.jsonNodeFromObject(value)
110     }
111 }
112
113 fun MutableMap<String, JsonNode>.getAsString(key: String): String {
114     return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
115 }
116
117 fun MutableMap<String, JsonNode>.getAsBoolean(key: String): Boolean {
118     return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
119 }
120
121 fun MutableMap<String, JsonNode>.getAsInt(key: String): Int {
122     return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
123 }
124
125 fun MutableMap<String, JsonNode>.getAsDouble(key: String): Double {
126     return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
127 }
128
129 // Checks
130
131 fun checkNotEmpty(value: String?): Boolean {
132     return value != null && value.isNotBlank()
133 }
134
135 fun checkNotEmptyOrThrow(value: String?, message: String? = value.plus(" is null/empty ")): Boolean {
136     val notEmpty = checkNotEmpty(value)
137     if (!notEmpty) {
138         throw BluePrintException(message!!)
139     }
140     return notEmpty
141 }
142
143 fun checkEqualsOrThrow(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
144     if (value1.equals(value2, ignoreCase = true)) {
145         return true
146     } else {
147         throw BluePrintException(lazyMessage().toString())
148     }
149 }
150
151 fun nullToEmpty(value: String?): String {
152     return if (checkNotEmpty(value)) value!! else ""
153 }
154
155 fun returnNotEmptyOrThrow(value: String?, lazyMessage: () -> Any): String {
156     if (checkNotEmpty(value)) {
157         return value!!
158     } else {
159         throw IllegalStateException(lazyMessage().toString())
160     }
161 }
162
163 fun InputStream.toFile(path: String): File {
164     val file = File(path)
165     file.outputStream().use { this.copyTo(it) }
166     return file
167 }
168