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