Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / CustomFunctions.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.controllerblueprints.core
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.node.*
22 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
23 import org.slf4j.helpers.MessageFormatter
24 import kotlin.reflect.KClass
25
26 /**
27  *
28  *
29  * @author Brinda Santh
30  */
31
32 fun String.asJsonPrimitive(): TextNode {
33     return TextNode(this)
34 }
35
36 fun Boolean.asJsonPrimitive(): BooleanNode {
37     return BooleanNode.valueOf(this)
38 }
39
40 fun Int.asJsonPrimitive(): IntNode {
41     return IntNode.valueOf(this)
42 }
43
44 fun Double.asJsonPrimitive(): DoubleNode {
45     return DoubleNode.valueOf(this)
46 }
47
48 fun MutableMap<String, *>.asJsonNode(): JsonNode {
49     return JacksonUtils.jsonNodeFromObject(this)
50 }
51
52 fun MutableMap<String, *>.asObjectNode(): ObjectNode {
53     return JacksonUtils.objectNodeFromObject(this)
54 }
55
56 fun format(message: String, vararg args: Any?): String {
57     if (args != null && args.isNotEmpty()) {
58         return MessageFormatter.arrayFormat(message, args).message
59     }
60     return message
61 }
62
63 fun <T : Any> MutableMap<String, *>.castOptionalValue(key: String, valueType: KClass<T>): T? {
64     if (containsKey(key)) {
65         return get(key) as? T
66     } else {
67         return null
68     }
69 }
70
71 fun <T : Any> MutableMap<String, *>.castValue(key: String, valueType: KClass<T>): T {
72     if (containsKey(key)) {
73         return get(key) as T
74     } else {
75         throw BluePrintException("couldn't find the key $key")
76     }
77 }
78
79 /**
80  * Convert Json to map of json node, the root fields will be map keys
81  */
82 fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> {
83     if (this is ObjectNode) {
84         val propertyMap: MutableMap<String, JsonNode> = hashMapOf()
85         this.fields().forEach {
86             propertyMap[it.key] = it.value
87         }
88         return propertyMap
89     } else {
90         throw BluePrintException("json node should be Object Node Type")
91     }
92 }
93
94
95 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
96     when (value) {
97         is JsonNode ->
98             this[key] = value
99         is String ->
100             this[key] = TextNode(value)
101         is Boolean ->
102             this[key] = BooleanNode.valueOf(value)
103         is Int ->
104             this[key] = IntNode.valueOf(value.toInt())
105         is Double ->
106             this[key] = DoubleNode.valueOf(value.toDouble())
107         else ->
108             this[key] = JacksonUtils.jsonNodeFromObject(value)
109     }
110 }
111
112 fun MutableMap<String, JsonNode>.getAsString(key: String): String {
113     return this[key]?.asText() ?: throw BluePrintException("couldn't find value for key($key)")
114 }
115
116 fun MutableMap<String, JsonNode>.getAsBoolean(key: String): Boolean {
117     return this[key]?.asBoolean() ?: throw BluePrintException("couldn't find value for key($key)")
118 }
119
120 fun MutableMap<String, JsonNode>.getAsInt(key: String): Int {
121     return this[key]?.asInt() ?: throw BluePrintException("couldn't find value for key($key)")
122 }
123
124 fun MutableMap<String, JsonNode>.getAsDouble(key: String): Double {
125     return this[key]?.asDouble() ?: throw BluePrintException("couldn't find value for key($key)")
126 }
127
128 // Checks
129
130 fun checkNotEmpty(value: String?): Boolean {
131     return value != null && value.isNotBlank()
132 }
133
134 fun checkNotEmptyOrThrow(value: String?, message: String? = value.plus(" is null/empty ")): Boolean {
135     val notEmpty = checkNotEmpty(value)
136     if (!notEmpty) {
137         throw BluePrintException(message!!)
138     }
139     return notEmpty
140 }
141
142 fun checkEqualsOrThrow(value1: String?, value2: String?, lazyMessage: () -> Any): Boolean {
143     if (value1.equals(value2, ignoreCase = true)) {
144         return true
145     } else {
146         throw BluePrintException(lazyMessage().toString())
147     }
148 }
149
150 fun nullToEmpty(value: String?): String {
151     return if (checkNotEmpty(value)) value!! else ""
152 }
153
154 fun returnNotEmptyOrThrow(value: String?, lazyMessage: () -> Any): String {
155     if (checkNotEmpty(value)) {
156         return value!!
157     } else {
158         throw IllegalStateException(lazyMessage().toString())
159     }
160 }
161