fixed sonar issue in AssignVlanTagResponse
[ccsdk/apps.git] / components / core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / utils / JacksonUtils.kt
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 <T> readValue(node: JsonNode, valueType: Class<T>): T? {\r
54         return jacksonObjectMapper().treeToValue(node, valueType)\r
55     }\r
56 \r
57     @JvmStatic\r
58     fun getContent(fileName: String): String {\r
59         return File(fileName).readText(Charsets.UTF_8)\r
60     }\r
61 \r
62     @JvmStatic\r
63     fun getClassPathFileContent(fileName: String): String {\r
64         return IOUtils.toString(JacksonUtils::class.java.classLoader\r
65                 .getResourceAsStream(fileName), Charset.defaultCharset())\r
66     }\r
67 \r
68     @JvmStatic\r
69     fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {\r
70         val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())\r
71                 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))\r
72         return readValue(content, valueType)\r
73     }\r
74 \r
75     @JvmStatic\r
76     fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {\r
77         val content: String = getClassPathFileContent(fileName)\r
78         return readValue(content, valueType)\r
79     }\r
80 \r
81     @JvmStatic\r
82     fun jsonNodeFromObject(from: kotlin.Any): JsonNode = jacksonObjectMapper().convertValue(from, JsonNode::class.java)\r
83 \r
84     @JvmStatic\r
85     fun jsonNodeFromClassPathFile(fileName: String): JsonNode {\r
86         val content: String = getClassPathFileContent(fileName)\r
87         return jsonNode(content)\r
88     }\r
89 \r
90     @JvmStatic\r
91     fun jsonNodeFromFile(fileName: String): JsonNode {\r
92         val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())\r
93                 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))\r
94         return jsonNode(content)\r
95     }\r
96 \r
97     @JvmStatic\r
98     fun jsonNode(content: String): JsonNode {\r
99         return jacksonObjectMapper().readTree(content)\r
100     }\r
101 \r
102     @JvmStatic\r
103     fun getJson(any: kotlin.Any): String {\r
104         return getJson(any, false)\r
105     }\r
106 \r
107     @JvmStatic\r
108     fun getWrappedJson(wrapper: String, any: kotlin.Any, pretty: Boolean = false): String {\r
109         val wrapperMap = hashMapOf<String, Any>()\r
110         wrapperMap[wrapper] = any\r
111         return getJson(wrapperMap, pretty)\r
112     }\r
113 \r
114     @JvmStatic\r
115     fun getJson(any: kotlin.Any, pretty: Boolean = false): String {\r
116         val objectMapper = jacksonObjectMapper()\r
117         objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)\r
118         if (pretty) {\r
119             objectMapper.enable(SerializationFeature.INDENT_OUTPUT)\r
120         }\r
121         return objectMapper.writeValueAsString(any)\r
122     }\r
123 \r
124     @JvmStatic\r
125     fun <T> getListFromJson(content: String, valueType: Class<T>): List<T>? {\r
126         val objectMapper = jacksonObjectMapper()\r
127         val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)\r
128         return objectMapper.readValue<List<T>>(content, javaType)\r
129     }\r
130 \r
131     @JvmStatic\r
132     fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T>? {\r
133         val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())\r
134                 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))\r
135         return getListFromJson(content, valueType)\r
136     }\r
137 \r
138     @JvmStatic\r
139     fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T>? {\r
140         val content: String = getClassPathFileContent(fileName)\r
141         return getListFromJson(content, valueType)\r
142     }\r
143 \r
144     @JvmStatic\r
145     fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T>? {\r
146         val objectMapper = jacksonObjectMapper()\r
147         val typeRef = object : TypeReference<MutableMap<String, T>>() {}\r
148         return objectMapper.readValue(content, typeRef)\r
149     }\r
150 \r
151     @JvmStatic\r
152     fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {\r
153         if (BluePrintTypes.validPrimitiveTypes().contains(type)) {\r
154             return checkJsonNodeValueOfPrimitiveType(type, jsonNode)\r
155         } else if (BluePrintTypes.validCollectionTypes().contains(type)) {\r
156             return checkJsonNodeValueOfCollectionType(type, jsonNode)\r
157         }\r
158         return false\r
159     }\r
160 \r
161     @JvmStatic\r
162     fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {\r
163         when (primitiveType) {\r
164             BluePrintConstants.DATA_TYPE_STRING -> return jsonNode.isTextual\r
165             BluePrintConstants.DATA_TYPE_BOOLEAN -> return jsonNode.isBoolean\r
166             BluePrintConstants.DATA_TYPE_INTEGER -> return jsonNode.isInt\r
167             BluePrintConstants.DATA_TYPE_FLOAT -> return jsonNode.isDouble\r
168             BluePrintConstants.DATA_TYPE_TIMESTAMP -> return jsonNode.isTextual\r
169             else -> return false\r
170         }\r
171     }\r
172 \r
173     @JvmStatic\r
174     fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {\r
175         when (type) {\r
176             BluePrintConstants.DATA_TYPE_LIST -> return jsonNode.isArray\r
177             BluePrintConstants.DATA_TYPE_MAP -> return jsonNode.isContainerNode\r
178             else -> return false\r
179         }\r
180 \r
181     }\r
182 /*\r
183     @JvmStatic\r
184     fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {\r
185         if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {\r
186             objectNode.put(key, value as Boolean)\r
187         } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {\r
188             objectNode.put(key, value as Int)\r
189         } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {\r
190             objectNode.put(key, value as Float)\r
191         } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {\r
192             objectNode.put(key, value as String)\r
193         } else {\r
194             objectNode.put(key, value as String)\r
195         }\r
196     }\r
197 \r
198     @JvmStatic\r
199     fun populatePrimitiveValues(value: Any, primitiveType: String, objectNode: ArrayNode) {\r
200         if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {\r
201             objectNode.add(value as Boolean)\r
202         } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {\r
203             objectNode.add(value as Int)\r
204         } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {\r
205             objectNode.add(value as Float)\r
206         } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {\r
207             objectNode.add(value as String)\r
208         } else {\r
209             objectNode.add(value as String)\r
210         }\r
211     }\r
212 \r
213     @JvmStatic\r
214     fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {\r
215         if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {\r
216             objectNode.put(key, false)\r
217         } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {\r
218             objectNode.put(key, 0)\r
219         } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {\r
220             objectNode.put(key, 0.0)\r
221         } else {\r
222             objectNode.put(key, "")\r
223         }\r
224     }\r
225 \r
226     @JvmStatic\r
227     fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {\r
228         if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {\r
229             arrayNode.add(false)\r
230         } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {\r
231             arrayNode.add(0)\r
232         } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {\r
233             arrayNode.add(0.0)\r
234         } else {\r
235             arrayNode.add("")\r
236         }\r
237     }\r
238 \r
239     @JvmStatic\r
240     fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {\r
241         if (nodeValue == null || nodeValue is NullNode) {\r
242             objectNode.set(key, nodeValue)\r
243         } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {\r
244             if (BluePrintConstants.DATA_TYPE_BOOLEAN == type) {\r
245                 objectNode.put(key, nodeValue.asBoolean())\r
246             } else if (BluePrintConstants.DATA_TYPE_INTEGER == type) {\r
247                 objectNode.put(key, nodeValue.asInt())\r
248             } else if (BluePrintConstants.DATA_TYPE_FLOAT == type) {\r
249                 objectNode.put(key, nodeValue.floatValue())\r
250             } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == type) {\r
251                 objectNode.put(key, nodeValue.asText())\r
252             } else {\r
253                 objectNode.put(key, nodeValue.asText())\r
254             }\r
255         } else {\r
256             objectNode.set(key, nodeValue)\r
257         }\r
258     }\r
259     */\r
260 }