2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Huawei Technologies Co., Ltd. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.ccsdk.sli.plugins.yangserializers.dfserializer;
23 import com.fasterxml.jackson.databind.JsonNode;
24 import com.fasterxml.jackson.databind.node.ArrayNode;
25 import com.fasterxml.jackson.databind.node.JsonNodeType;
26 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
27 import org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType;
29 import java.util.Iterator;
32 import static com.fasterxml.jackson.databind.node.JsonNodeType.NUMBER;
33 import static com.fasterxml.jackson.databind.node.JsonNodeType.STRING;
34 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_LEAF_NODE;
35 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.MULTI_INSTANCE_NODE;
36 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_LEAF_NODE;
37 import static org.onap.ccsdk.sli.plugins.yangserializers.pnserializer.NodeType.SINGLE_INSTANCE_NODE;
40 * Implementation of JSON walker to walk through the nodes and process it.
42 public class DefaultJsonWalker implements JsonWalker {
45 public void walk(JsonListener listener, JsonNode jsonNode) throws
47 Iterator<Map.Entry<String, JsonNode>> children = jsonNode.fields();
48 while (children.hasNext()) {
49 Map.Entry<String, JsonNode> child = children.next();
50 JsonNode value = child.getValue();
51 String key = child.getKey();
52 if (value.isArray()) {
53 processMultiNodes(key, value, listener);
55 processSingleNode(key, value, listener);
61 * Processes single instance node or leaf, by adding the node to from
62 * JSON and walking through all its children recursively.
64 * @param key JSON name
65 * @param value JSON node
66 * @param listener JSON listener
67 * @throws SvcLogicException when processing the node fails
69 private void processSingleNode(String key, JsonNode value,
70 JsonListener listener)
71 throws SvcLogicException {
73 if (!value.isContainerNode()) {
74 nodeType = SINGLE_INSTANCE_LEAF_NODE;
76 nodeType = SINGLE_INSTANCE_NODE;
78 processNode(key, value, nodeType, listener);
82 * Processes multi instance node or leaf, by adding the node to from JSON
83 * and walking through all its instance recursively.
85 * @param key JSON name
86 * @param value JSON node
87 * @param listener JSON listener
88 * @throws SvcLogicException when processing a single instance fails
90 private void processMultiNodes(String key, JsonNode value,
91 JsonListener listener)
92 throws SvcLogicException {
94 Iterator<JsonNode> multiNodes = value.elements();
95 while (multiNodes.hasNext()) {
96 if (isLeafListNode((ArrayNode) value)) {
97 nodeType = MULTI_INSTANCE_LEAF_NODE;
99 nodeType = MULTI_INSTANCE_NODE;
101 JsonNode multiNode = multiNodes.next();
102 processNode(key, multiNode, nodeType, listener);
107 * Processes each node by first entering the JSON node through JSON
108 * listener, second a call back to walking the rest of the tree of the
109 * node and finally exiting the node.
111 * @param key JSON name
112 * @param node JSON node
113 * @param nodeType JSON node type
114 * @param listener JSON listener
115 * @throws SvcLogicException when entering a JSON node fails
117 private void processNode(String key, JsonNode node, NodeType nodeType,
118 JsonListener listener) throws SvcLogicException {
119 listener.enterJsonNode(key, node, nodeType);
120 walk(listener, node);
121 listener.exitJsonNode(node);
125 * Returns true if the node corresponds to a leaf-list node; false
128 * @param node JSON node
129 * @return true if node corresponds to leaf-list node; false otherwise
131 private boolean isLeafListNode(ArrayNode node) {
132 Iterator<JsonNode> children = node.elements();
133 while (children.hasNext()) {
134 JsonNodeType type = children.next().getNodeType();
135 if (type != STRING && type != NUMBER) {