Remote Script Executor Component
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / utils / JsonParserUtils.kt
1 /*
2  *  Copyright © 2018 IBM.
3  *  Modifications Copyright © 2018 - 2019 IBM, Bell Canada.
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 com.jayway.jsonpath.Configuration
22 import com.jayway.jsonpath.JsonPath
23 import com.jayway.jsonpath.Option
24 import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider
25 import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
27
28 class JsonParserUtils {
29     companion object {
30
31         // TODO("Optimise this")
32         val JACKSON_JSON_NODE_CONFIGURATION = Configuration.builder()
33             .mappingProvider(JacksonMappingProvider()).jsonProvider(JacksonJsonNodeJsonProvider()).build()
34
35         val PATH_CONFIGURATION = Configuration.builder().options(Option.AS_PATH_LIST).build()
36
37         fun paths(jsonContent: String, expression: String): List<String> {
38             return JsonPath.using(PATH_CONFIGURATION).parse(jsonContent).read(expression)
39         }
40
41         fun paths(jsonNode: JsonNode, expression: String): List<String> {
42             return paths(jsonNode.asJsonString(), expression)
43         }
44
45         fun parse(jsonContent: String, expression: String): JsonNode {
46             return JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).read(expression)
47         }
48
49         fun parse(jsonNode: JsonNode, expression: String): JsonNode {
50             return parse(jsonNode.asJsonString(), expression)
51         }
52
53         fun parseNSet(jsonContent: String, expression: String, value: JsonNode): JsonNode {
54             return JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).set(expression, value).json()
55         }
56
57         fun parseNSet(jsonNode: JsonNode, expression: String, valueNode: JsonNode): JsonNode {
58
59             return parseNSet(jsonNode.asJsonString(), expression, valueNode)
60         }
61     }
62 }