Refactor Netconf script component parent.
[ccsdk/cds.git] / ms / controllerblueprints / modules / 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
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.jayway.jsonpath.Configuration
23 import com.jayway.jsonpath.JsonPath
24 import com.jayway.jsonpath.Option
25 import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider
26 import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider
27 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString
28
29 class JsonParserUtils {
30     companion object {
31
32         //TODO("Optimise this")
33         val JACKSON_JSON_NODE_CONFIGURATION = Configuration.builder()
34                 .mappingProvider(JacksonMappingProvider()).jsonProvider(JacksonJsonNodeJsonProvider()).build()
35
36         val PATH_CONFIGURATION = Configuration.builder().options(Option.AS_PATH_LIST).build()
37
38         fun paths(jsonContent: String, expression: String): List<String> {
39             return JsonPath.using(PATH_CONFIGURATION).parse(jsonContent).read(expression)
40         }
41
42         fun paths(jsonNode: JsonNode, expression: String): List<String> {
43             return paths(jsonNode.asJsonString(), expression)
44         }
45
46         fun parse(jsonContent: String, expression: String): JsonNode {
47             return JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).read(expression)
48         }
49
50         fun parse(jsonNode: JsonNode, expression: String): JsonNode {
51             return parse(jsonNode.asJsonString(), expression)
52         }
53
54         fun parseNSet(jsonContent: String, expression: String, value: JsonNode): JsonNode {
55             return JsonPath.using(JACKSON_JSON_NODE_CONFIGURATION).parse(jsonContent).set(expression, value).json()
56         }
57
58         fun parseNSet(jsonNode: JsonNode, expression: String, valueNode: JsonNode): JsonNode {
59
60             return parseNSet(jsonNode.asJsonString(), expression, valueNode)
61         }
62     }
63 }