chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.onosproject.protocol.restconf.server.utils.parser.json; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.JsonNodeType; |
| 22 | import org.onosproject.protocol.restconf.server.utils.exceptions.JsonParseException; |
| 23 | import org.onosproject.protocol.restconf.server.utils.parser.api.JsonListener; |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 24 | import org.onosproject.protocol.restconf.server.utils.parser.api.NormalizedYangNode; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 25 | import org.onosproject.yms.ydt.YdtBuilder; |
| 26 | import org.onosproject.yms.ydt.YdtContext; |
| 27 | import org.slf4j.Logger; |
| 28 | |
| 29 | import java.util.HashSet; |
| 30 | import java.util.Iterator; |
| 31 | import java.util.Set; |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 32 | import java.util.Stack; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 33 | |
| 34 | import static com.fasterxml.jackson.databind.node.JsonNodeType.ARRAY; |
| 35 | import static com.google.common.base.Strings.isNullOrEmpty; |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 36 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.buildNormalizedNode; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 37 | import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_NODE; |
| 38 | import static org.onosproject.yms.ydt.YdtType.SINGLE_INSTANCE_NODE; |
| 39 | import static org.slf4j.LoggerFactory.getLogger; |
| 40 | |
| 41 | /** |
| 42 | * Represents default implementation of codec JSON listener. |
| 43 | */ |
| 44 | public class JsonToYdtListener implements JsonListener { |
| 45 | |
| 46 | private static final String INPUT_FIELD_NAME = "input"; |
| 47 | private static final String COLON = ":"; |
| 48 | private static final int INPUT_FIELD_LENGTH = 2; |
| 49 | private static final String E_UNSUP_TYPE = "Unsupported node type %s " + |
| 50 | "field name is %s fieldName"; |
| 51 | |
| 52 | private Logger log = getLogger(getClass()); |
| 53 | |
| 54 | private YdtBuilder ydtBuilder; |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 55 | private NormalizedYangNode defaultMultiInsNode; |
| 56 | private Stack<NormalizedYangNode> nameStack = new Stack<>(); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 57 | private YdtContext rpcModule; |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 58 | private boolean isListArray = false; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Creates a listener for the process of a Json Object, the listener will |
| 62 | * try to transfer the JSON object to a Ydt builder. |
| 63 | * |
| 64 | * @param ydtBuilder the YDT builder to build a YDT object |
| 65 | */ |
| 66 | public JsonToYdtListener(YdtBuilder ydtBuilder) { |
| 67 | this.ydtBuilder = ydtBuilder; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void enterJsonNode(String fieldName, JsonNode node) { |
| 72 | if (isNullOrEmpty(fieldName)) { |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 73 | if (defaultMultiInsNode != null) { |
| 74 | ydtBuilder.addChild(defaultMultiInsNode.getName(), |
| 75 | defaultMultiInsNode.getNamespace(), |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 76 | MULTI_INSTANCE_NODE); |
| 77 | } |
| 78 | return; |
| 79 | } |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 80 | NormalizedYangNode normalizedNode = buildNormalizedNode(fieldName); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 81 | JsonNodeType nodeType = node.getNodeType(); |
| 82 | switch (nodeType) { |
| 83 | case OBJECT: |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 84 | processObjectNode(normalizedNode); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 85 | break; |
| 86 | |
| 87 | case ARRAY: |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 88 | processArrayNode(normalizedNode, node); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 89 | break; |
| 90 | |
| 91 | //TODO for now, just process the following three node type |
| 92 | case STRING: |
| 93 | case NUMBER: |
| 94 | case BOOLEAN: |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 95 | processLeafNode(normalizedNode, node.asText()); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 96 | break; |
| 97 | |
| 98 | case BINARY: |
| 99 | case MISSING: |
| 100 | case NULL: |
| 101 | case POJO: |
| 102 | log.debug("Unimplemented node type {}", nodeType); |
| 103 | break; |
| 104 | |
| 105 | default: |
| 106 | throw new JsonParseException(String.format(E_UNSUP_TYPE, |
| 107 | nodeType, fieldName)); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void exitJsonNode(JsonNode jsonNode) { |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 113 | if (jsonNode.getNodeType() == ARRAY && nameStack.empty()) { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 114 | return; |
| 115 | } |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 116 | |
| 117 | if (jsonNode.getNodeType() == ARRAY && !isListArray) { |
| 118 | nameStack.pop(); |
| 119 | if (nameStack.empty()) { |
| 120 | return; |
| 121 | } |
| 122 | defaultMultiInsNode = nameStack.get(nameStack.size() - 1); |
| 123 | return; |
| 124 | } |
| 125 | if (isListArray) { |
| 126 | isListArray = false; |
| 127 | } |
| 128 | |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 129 | ydtBuilder.traverseToParent(); |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 130 | } |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 131 | |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 132 | private void processObjectNode(NormalizedYangNode node) { |
| 133 | ydtBuilder.addChild(node.getName(), node.getNamespace(), |
| 134 | SINGLE_INSTANCE_NODE); |
| 135 | } |
| 136 | |
| 137 | private void processLeafNode(NormalizedYangNode node, String value) { |
| 138 | ydtBuilder.addLeaf(node.getName(), node.getNamespace(), value); |
| 139 | } |
| 140 | |
| 141 | private void processArrayNode(NormalizedYangNode normalizedNode, |
| 142 | JsonNode node) { |
| 143 | ArrayNode arrayNode = (ArrayNode) node; |
| 144 | if (arrayNode.size() == 0) { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 145 | return; |
| 146 | } |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 147 | Set<String> sets = new HashSet<>(); |
| 148 | Iterator<JsonNode> elements = arrayNode.elements(); |
| 149 | boolean isLeafList = true; |
| 150 | while (elements.hasNext()) { |
| 151 | JsonNode element = elements.next(); |
| 152 | JsonNodeType eleType = element.getNodeType(); |
| 153 | |
| 154 | if (eleType == JsonNodeType.STRING || |
| 155 | eleType == JsonNodeType.NUMBER || |
| 156 | eleType == JsonNodeType.BOOLEAN) { |
| 157 | sets.add(element.asText()); |
| 158 | } else { |
| 159 | isLeafList = false; |
| 160 | } |
| 161 | } |
| 162 | if (isLeafList) { |
| 163 | //leaf-list |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 164 | ydtBuilder.addLeaf(normalizedNode.getName(), |
| 165 | normalizedNode.getNamespace(), sets); |
| 166 | isListArray = true; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 167 | } else { |
chengfan | 7b2a60b | 2016-10-08 20:27:15 +0800 | [diff] [blame^] | 168 | defaultMultiInsNode = normalizedNode; |
| 169 | nameStack.push(defaultMultiInsNode); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | } |