4d1d9b6533eb09e4f39ca318171de4a7a7a6a5b6
[ccsdk/cds.git] / components / core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / CustomFunctions.kt
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 package org.onap.ccsdk.apps.controllerblueprints.core\r
18 \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
26 import java.io.File\r
27 import java.io.InputStream\r
28 import kotlin.reflect.KClass\r
29 \r
30 /**\r
31  *\r
32  *\r
33  * @author Brinda Santh\r
34  */\r
35 \r
36 fun String.asJsonPrimitive(): TextNode {\r
37     return TextNode(this)\r
38 }\r
39 \r
40 fun Boolean.asJsonPrimitive(): BooleanNode {\r
41     return BooleanNode.valueOf(this)\r
42 }\r
43 \r
44 fun Int.asJsonPrimitive(): IntNode {\r
45     return IntNode.valueOf(this)\r
46 }\r
47 \r
48 fun Double.asJsonPrimitive(): DoubleNode {\r
49     return DoubleNode.valueOf(this)\r
50 }\r
51 \r
52 fun MutableMap<String, *>.asJsonNode(): JsonNode {\r
53     return JacksonUtils.jsonNodeFromObject(this)\r
54 }\r
55 \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
59     }\r
60     return message\r
61 }\r
62 \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
66     } else {\r
67         return null\r
68     }\r
69 }\r
70 \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
74     } else {\r
75         throw BluePrintException("couldn't find the key $key")\r
76     }\r
77 }\r
78 \r
79 fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {\r
80     when (value) {\r
81         is JsonNode ->\r
82             this[key] = value\r
83         is String ->\r
84             this[key] = TextNode(value)\r
85         is Boolean ->\r
86             this[key] = BooleanNode.valueOf(value)\r
87         is Int ->\r
88             this[key] = IntNode.valueOf(value.toInt())\r
89         is Double ->\r
90             this[key] = DoubleNode.valueOf(value.toDouble())\r
91         else ->\r
92             this[key] = JacksonUtils.jsonNodeFromObject(value)\r
93     }\r
94 }\r
95 \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
98 }\r
99 \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
102 }\r
103 \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
106 }\r
107 \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
110 }\r
111 \r
112 // Checks\r
113 \r
114 fun checkNotEmpty(value: String?): Boolean {\r
115     return value != null && value.isNotEmpty()\r
116 }\r
117 \r
118 fun checkNotEmptyNThrow(value: String?, message: String? = value.plus(" is null/empty ")): Boolean {\r
119     val notEmpty = value != null && value.isNotEmpty()\r
120     if (!notEmpty) {\r
121         throw BluePrintException(message!!)\r
122     }\r
123     return notEmpty\r
124 }\r
125 \r
126 fun InputStream.toFile(path: String): File {\r
127     val file = File(path)\r
128     file.outputStream().use { this.copyTo(it) }\r
129     return file\r
130 }\r
131 \r
132 \r
133 \r
134 \r
135 \r
136 \r