Merge "Fixed validation of resource-assignment-params.config-assign"
[ccsdk/cds.git] / ms / blueprintsprocessor / application / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / JsonNormalizer.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.ccsdk.cds.blueprintsprocessor
21
22 import com.fasterxml.jackson.databind.JsonNode
23 import com.fasterxml.jackson.databind.ObjectMapper
24 import com.fasterxml.jackson.databind.node.ContainerNode
25 import com.fasterxml.jackson.databind.node.MissingNode
26 import com.fasterxml.jackson.databind.node.ObjectNode
27 import com.schibsted.spt.data.jslt.Parser
28
29 class JsonNormalizer {
30
31     companion object {
32
33         fun getNormalizer(mapper: ObjectMapper, jsltSpec: JsonNode): (String) -> String {
34             if (jsltSpec is MissingNode) {
35                 return { it }
36             }
37             return { s: String ->
38                 val input = mapper.readTree(s)
39                 val expandedJstlSpec = expandJstlSpec(jsltSpec)
40                 val jslt = Parser.compileString(expandedJstlSpec)
41                 val output = jslt.apply(input)
42                 output.toString()
43             }
44         }
45
46         /**
47          * Creates an extended JSTL spec by appending the "*: ." wildcard pattern to every inner JSON object, and
48          * removing the extra quotes added by the standard YAML/JSON converters on fields prefixed by "?".
49          *
50          * @param jstlSpec the JSTL spec as a structured JSON object.
51          * @return the string representation of the extended JSTL spec.
52          */
53         private fun expandJstlSpec(jstlSpec: JsonNode): String {
54             val extendedJstlSpec = updateObjectNodes(jstlSpec, "*", ".")
55             return extendedJstlSpec.toString()
56                     // Handle the "?" as a prefix to literal/non-quoted values
57                     .replace("\"\\?([^\"]+)\"".toRegex(), "$1")
58                     // Also, remove the quotes added by Jackson for key and value of the wildcard matcher
59                     .replace("\"([.*])\"".toRegex(), "$1")
60         }
61
62         /**
63          * Expands a structured JSON object, by adding the given key and value to every nested ObjectNode.
64          *
65          * @param jsonNode the root node.
66          * @param fieldName the fixed field name.
67          * @param fieldValue the fixed field value.
68          */
69         private fun updateObjectNodes(jsonNode: JsonNode, fieldName: String, fieldValue: String): JsonNode {
70             if (jsonNode is ContainerNode<*>) {
71                 (jsonNode as? ObjectNode)?.put(fieldName, fieldValue)
72                 jsonNode.forEach { child ->
73                     updateObjectNodes(child, fieldName, fieldValue)
74                 }
75             }
76             return jsonNode
77         }
78     }
79 }