Formatting Code base with ktlint
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / utils / JacksonReactorUtils.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.utils
19
20 import com.fasterxml.jackson.databind.JsonNode
21 import kotlinx.coroutines.Dispatchers
22 import kotlinx.coroutines.withContext
23 import org.apache.commons.io.IOUtils
24 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
25 import org.slf4j.LoggerFactory
26 import java.io.File
27 import java.nio.charset.Charset
28
29 class JacksonReactorUtils {
30     companion object {
31
32         private val log = LoggerFactory.getLogger(this::class.toString())
33
34         suspend fun getContent(fileName: String): String {
35             // log.info("Reading File($fileName)")
36             return getContent(normalizedFile(fileName))
37         }
38
39         suspend fun getContent(file: File): String = withContext(Dispatchers.IO) {
40             // log.info("Reading File(${file.absolutePath})")
41             file.readText(Charsets.UTF_8)
42         }
43
44         suspend fun getClassPathFileContent(fileName: String): String = withContext(Dispatchers.IO) {
45             // log.trace("Reading Classpath File($fileName)")
46             IOUtils.toString(
47                 JacksonUtils::class.java.classLoader
48                     .getResourceAsStream(fileName), Charset.defaultCharset()
49             )
50         }
51
52         suspend fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {
53             val content: String = getContent(fileName)
54             return JacksonUtils.readValue(content, valueType)
55         }
56
57         suspend fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {
58             val content: String = getClassPathFileContent(fileName)
59             return JacksonUtils.readValue(content, valueType)
60         }
61
62         suspend fun jsonNodeFromClassPathFile(fileName: String): JsonNode {
63             val content: String = getClassPathFileContent(fileName)
64             return JacksonUtils.jsonNode(content)
65         }
66
67         suspend fun jsonNodeFromFile(fileName: String): JsonNode {
68             val content: String = getContent(fileName)
69             return JacksonUtils.jsonNode(content)
70         }
71
72         suspend fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T> {
73             val content: String = getContent(fileName)
74             return JacksonUtils.getListFromJson(content, valueType)
75         }
76
77         suspend fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T> {
78             val content: String = getClassPathFileContent(fileName)
79             return JacksonUtils.getListFromJson(content, valueType)
80         }
81
82         suspend fun <T> getMapFromFile(file: File, valueType: Class<T>): MutableMap<String, T> {
83             val content: String = getContent(file)
84             return JacksonUtils.getMapFromJson(content, valueType)
85         }
86
87         suspend fun <T> getMapFromClassPathFile(fileName: String, valueType: Class<T>): MutableMap<String, T> {
88             val content: String = getClassPathFileContent(fileName)
89             return JacksonUtils.getMapFromJson(content, valueType)
90         }
91     }
92 }