0522e1f5e8378ecc680aeaac0c1899a1e1066572
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */
16
17 package org.onap.ccsdk.apps.controllerblueprints.core.utils
18
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
23 import reactor.core.publisher.Mono
24 import reactor.core.publisher.toMono
25
26 @Deprecated("Reactor will be replaced by coroutines by default")
27 object JacksonReactorUtils {
28     private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
29
30     @JvmStatic
31     fun getContent(fileName: String): Mono<String> {
32         return JacksonUtils.getContent(fileName).toMono()
33     }
34
35     @JvmStatic
36     fun getClassPathFileContent(fileName: String): Mono<String> {
37         return JacksonUtils.getClassPathFileContent(fileName).toMono()
38     }
39
40     @JvmStatic
41     fun <T> readValue(content: String, valueType: Class<T>): Mono<T> {
42         return Mono.just(jacksonObjectMapper().readValue(content, valueType))
43     }
44
45     @JvmStatic
46     fun jsonNode(content: String): Mono<JsonNode> {
47         return Mono.just(jacksonObjectMapper().readTree(content))
48     }
49
50     @JvmStatic
51     fun getJson(any: kotlin.Any, pretty: Boolean = false): Mono<String> {
52         return Mono.just(JacksonUtils.getJson(any, pretty))
53     }
54
55     @JvmStatic
56     fun <T> getListFromJson(content: String, valueType: Class<T>): Mono<List<T>> {
57         val objectMapper = jacksonObjectMapper()
58         val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)
59         return objectMapper.readValue<List<T>>(content, javaType).toMono()
60     }
61
62     @JvmStatic
63     fun <T> readValueFromFile(fileName: String, valueType: Class<T>): Mono<T> {
64         return getContent(fileName)
65                 .flatMap { content ->
66                     readValue(content, valueType)
67                 }
68     }
69
70     @JvmStatic
71     fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): Mono<T> {
72         return getClassPathFileContent(fileName)
73                 .flatMap { content ->
74                     readValue(content, valueType)
75                 }
76     }
77
78     @JvmStatic
79     fun jsonNodeFromFile(fileName: String): Mono<JsonNode> {
80         return getContent(fileName)
81                 .flatMap { content ->
82                     jsonNode(content)
83                 }
84     }
85
86     @JvmStatic
87     fun jsonNodeFromClassPathFile(fileName: String): Mono<JsonNode> {
88         return getClassPathFileContent(fileName)
89                 .flatMap { content ->
90                     jsonNode(content)
91                 }
92     }
93
94     @JvmStatic
95     fun <T> getListFromFile(fileName: String, valueType: Class<T>): Mono<List<T>> {
96         return getContent(fileName)
97                 .flatMap { content ->
98                     getListFromJson(content, valueType)
99                 }
100     }
101
102     @JvmStatic
103     fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): Mono<List<T>> {
104         return getClassPathFileContent(fileName)
105                 .flatMap { content ->
106                     getListFromJson(content, valueType)
107                 }
108     }
109 }