5075e72618ef189c71cfe0a3cba7ba57d2ea3860
[ccsdk/apps.git] /
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  *\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *     http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.apps.controllerblueprints.core.utils\r
19 \r
20 import com.att.eelf.configuration.EELFLogger\r
21 import com.att.eelf.configuration.EELFManager\r
22 import com.fasterxml.jackson.annotation.JsonInclude\r
23 import com.fasterxml.jackson.core.type.TypeReference\r
24 import com.fasterxml.jackson.databind.JsonNode\r
25 import com.fasterxml.jackson.databind.SerializationFeature\r
26 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper\r
27 import org.apache.commons.io.FileUtils\r
28 import org.apache.commons.io.IOUtils\r
29 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException\r
31 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes\r
32 import org.onap.ccsdk.apps.controllerblueprints.core.format\r
33 import java.io.File\r
34 import java.nio.charset.Charset\r
35 \r
36 /**\r
37  *\r
38  *\r
39  * @author Brinda Santh\r
40  */\r
41 object JacksonUtils {\r
42     private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())\r
43 \r
44     inline fun <reified T : Any> readValue(content: String): T =\r
45             jacksonObjectMapper().readValue(content, T::class.java)\r
46 \r
47     @JvmStatic\r
48     fun <T> readValue(content: String, valueType: Class<T>): T? {\r
49         return jacksonObjectMapper().readValue(content, valueType)\r
50     }\r
51 \r
52     @JvmStatic\r
53     fun getContent(fileName: String): String {\r
54         return File(fileName).readText(Charsets.UTF_8)\r
55     }\r
56 \r
57     @JvmStatic\r
58     fun getClassPathFileContent(fileName: String): String {\r
59         return IOUtils.toString(JacksonUtils::class.java.classLoader\r
60                 .getResourceAsStream(fileName), Charset.defaultCharset())\r
61     }\r
62 \r
63     @JvmStatic\r
64     fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {\r
65         val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())\r
66                 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))\r
67         return readValue(content, valueType)\r
68     }\r
69 \r
70     @JvmStatic\r
71     fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {\r
72         val content: String = getClassPathFileContent(fileName)\r
73         return readValue(content, valueType)\r
74     }\r
75 \r
76     @JvmStatic\r
77     fun jsonNodeFromObject(from: kotlin.Any): JsonNode = jacksonObjectMapper().convertValue(from, JsonNode::class.java)\r
78 \r
79     @JvmStatic\r
80     fun jsonNodeFromClassPathFile(fileName: String): JsonNode {\r
81         val content: String = getClassPathFileContent(fileName)\r
82         return jsonNode(content)\r
83     }\r
84 \r
85     @JvmStatic\r
86     fun jsonNodeFromFile(fileName: String): JsonNode {\r
87         val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())\r
88                 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))\r
89         return jsonNode(content)\r
90     }\r
91 \r
92     @JvmStatic\r
93     fun jsonNode(content: String): JsonNode {\r
94         return jacksonObjectMapper().readTree(content)\r
95     }\r
96 \r
97     @JvmStatic\r
98     fun getJson(any: kotlin.Any): String {\r
99         return getJson(any, false)\r
100     }\r
101 \r
102     @JvmStatic\r
103     fun getJson(any: kotlin.Any, pretty: Boolean = false): String {\r
104         val objectMapper = jacksonObjectMapper()\r
105         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)\r
106         if (pretty) {\r
107             objectMapper.enable(SerializationFeature.INDENT_OUTPUT)\r
108         }\r
109         return objectMapper.writeValueAsString(any)\r
110     }\r
111 \r
112     @JvmStatic\r
113     fun <T> getListFromJson(content: String, valueType: Class<T>): List<T>? {\r
114         val objectMapper = jacksonObjectMapper()\r
115         val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)\r
116         return objectMapper.readValue<List<T>>(content, javaType)\r
117     }\r
118 \r
119     @JvmStatic\r
120     fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T>? {\r
121         val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())\r
122                 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))\r
123         return getListFromJson(content, valueType)\r
124     }\r
125 \r
126     @JvmStatic\r
127     fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T>? {\r
128         val content: String = getClassPathFileContent(fileName)\r
129         return getListFromJson(content, valueType)\r
130     }\r
131 \r
132     @JvmStatic\r
133     fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T>? {\r
134         val objectMapper = jacksonObjectMapper()\r
135         val typeRef = object : TypeReference<MutableMap<String, T>>() {}\r
136         return objectMapper.readValue(content, typeRef)\r
137     }\r
138 \r
139     @JvmStatic\r
140     fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {\r
141         if (BluePrintTypes.validPrimitiveTypes().contains(type)) {\r
142             return checkJsonNodeValueOfPrimitiveType(type, jsonNode)\r
143         } else if (BluePrintTypes.validCollectionTypes().contains(type)) {\r
144             return checkJsonNodeValueOfCollectionType(type, jsonNode)\r
145         }\r
146         return false\r
147     }\r
148 \r
149     @JvmStatic\r
150     fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {\r
151         when (primitiveType) {\r
152             BluePrintConstants.DATA_TYPE_STRING -> return jsonNode.isTextual\r
153             BluePrintConstants.DATA_TYPE_BOOLEAN -> return jsonNode.isBoolean\r
154             BluePrintConstants.DATA_TYPE_INTEGER -> return jsonNode.isInt\r
155             BluePrintConstants.DATA_TYPE_FLOAT -> return jsonNode.isDouble\r
156             BluePrintConstants.DATA_TYPE_TIMESTAMP -> return jsonNode.isTextual\r
157             else -> return false\r
158         }\r
159     }\r
160 \r
161     @JvmStatic\r
162     fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {\r
163         when (type) {\r
164             BluePrintConstants.DATA_TYPE_LIST -> return jsonNode.isArray\r
165             BluePrintConstants.DATA_TYPE_MAP -> return jsonNode.isContainerNode\r
166             else -> return false\r
167         }\r
168 \r
169     }\r
170 /*\r
171     @JvmStatic\r
172     fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {\r
173         if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {\r
174             objectNode.put(key, value as Boolean)\r
175         } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {\r
176             objectNode.put(key, value as Int)\r
177         } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {\r
178             objectNode.put(key, value as Float)\r
179         } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {\r
180             objectNode.put(key, value as String)\r
181         } else {\r
182             objectNode.put(key, value as String)\r
183         }\r
184     }\r
185 \r
186     @JvmStatic\r
187     fun populatePrimitiveValues(value: Any, primitiveType: String, objectNode: ArrayNode) {\r
188         if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {\r
189             objectNode.add(value as Boolean)\r
190         } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {\r
191             objectNode.add(value as Int)\r
192         } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {\r
193             objectNode.add(value as Float)\r
194         } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {\r
195             objectNode.add(value as String)\r
196         } else {\r
197             objectNode.add(value as String)\r
198         }\r
199     }\r
200 \r
201     @JvmStatic\r
202     fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {\r
203         if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {\r
204             objectNode.put(key, false)\r
205         } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {\r
206             objectNode.put(key, 0)\r
207         } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {\r
208             objectNode.put(key, 0.0)\r
209         } else {\r
210             objectNode.put(key, "")\r
211         }\r
212     }\r
213 \r
214     @JvmStatic\r
215     fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {\r
216         if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {\r
217             arrayNode.add(false)\r
218         } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {\r
219             arrayNode.add(0)\r
220         } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {\r
221             arrayNode.add(0.0)\r
222         } else {\r
223             arrayNode.add("")\r
224         }\r
225     }\r
226 \r
227     @JvmStatic\r
228     fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {\r
229         if (nodeValue == null || nodeValue is NullNode) {\r
230             objectNode.set(key, nodeValue)\r
231         } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {\r
232             if (BluePrintConstants.DATA_TYPE_BOOLEAN == type) {\r
233                 objectNode.put(key, nodeValue.asBoolean())\r
234             } else if (BluePrintConstants.DATA_TYPE_INTEGER == type) {\r
235                 objectNode.put(key, nodeValue.asInt())\r
236             } else if (BluePrintConstants.DATA_TYPE_FLOAT == type) {\r
237                 objectNode.put(key, nodeValue.floatValue())\r
238             } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == type) {\r
239                 objectNode.put(key, nodeValue.asText())\r
240             } else {\r
241                 objectNode.put(key, nodeValue.asText())\r
242             }\r
243         } else {\r
244             objectNode.set(key, nodeValue)\r
245         }\r
246     }\r
247     */\r
248 }