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.node.JsonNodeType; |
| 20 | import org.onosproject.protocol.restconf.server.utils.exceptions.YdtParseException; |
| 21 | import org.onosproject.protocol.restconf.server.utils.parser.api.JsonBuilder; |
| 22 | import org.onosproject.yms.ydt.YdtContext; |
| 23 | import org.onosproject.yms.ydt.YdtListener; |
| 24 | |
| 25 | import static com.google.common.base.Strings.isNullOrEmpty; |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 26 | import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.getJsonNameFromYdtNode; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 27 | |
| 28 | /** |
| 29 | * Represents implementation of codec YDT listener. |
| 30 | */ |
| 31 | public class YdtToJsonListener implements YdtListener { |
| 32 | |
| 33 | private static final String EMPTY = ""; |
| 34 | private JsonBuilder jsonBuilder; |
| 35 | //the root name of the json |
| 36 | //the input YdtContext is usually a total tree of a YANG resource |
| 37 | //this property is used to mark the start of the request node. |
| 38 | private String rootName; |
| 39 | //the parse state |
| 40 | private boolean isBegin; |
| 41 | |
| 42 | /** |
| 43 | * Creates a listener for the process of a Ydt, the listener will try to |
| 44 | * transfer the Ydt to a JSON Object. |
| 45 | * |
| 46 | * @param rootName the name of a specific YdtContext begin to process |
| 47 | * @param jsonBuilder the JSON builder to build a JSON object |
| 48 | */ |
| 49 | public YdtToJsonListener(String rootName, JsonBuilder jsonBuilder) { |
| 50 | this.jsonBuilder = jsonBuilder; |
| 51 | this.rootName = rootName; |
| 52 | this.isBegin = isNullOrEmpty(rootName); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void enterYdtNode(YdtContext ydtContext) { |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 57 | String name = getJsonNameFromYdtNode(ydtContext); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 58 | |
Henry Yu | 7dbfc20 | 2016-10-24 16:00:37 -0400 | [diff] [blame] | 59 | if (!isBegin && name != null && name.equals(rootName)) { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 60 | isBegin = true; |
| 61 | } |
| 62 | if (!isBegin) { |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | switch (ydtContext.getYdtType()) { |
| 67 | |
| 68 | case SINGLE_INSTANCE_NODE: |
| 69 | jsonBuilder.addNodeTopHalf(name, JsonNodeType.OBJECT); |
| 70 | break; |
| 71 | case MULTI_INSTANCE_NODE: |
| 72 | YdtContext preNode = ydtContext.getPreviousSibling(); |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 73 | if (preNode == null || !getJsonNameFromYdtNode(preNode).equals(name)) { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 74 | jsonBuilder.addNodeTopHalf(name, JsonNodeType.ARRAY); |
| 75 | } |
| 76 | jsonBuilder.addNodeTopHalf(EMPTY, JsonNodeType.OBJECT); |
| 77 | break; |
| 78 | case SINGLE_INSTANCE_LEAF_VALUE_NODE: |
| 79 | jsonBuilder.addNodeWithValueTopHalf(name, ydtContext.getValue()); |
| 80 | break; |
| 81 | case MULTI_INSTANCE_LEAF_VALUE_NODE: |
| 82 | jsonBuilder.addNodeWithSetTopHalf(name, ydtContext.getValueSet()); |
| 83 | break; |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 84 | case LOGICAL_ROOT_NODE: |
| 85 | break; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 86 | default: |
| 87 | throw new YdtParseException("unknown Ydt type " + |
| 88 | ydtContext.getYdtType()); |
| 89 | } |
| 90 | |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void exitYdtNode(YdtContext ydtContext) { |
| 95 | |
| 96 | if (!isBegin) { |
| 97 | return; |
| 98 | } |
| 99 | |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 100 | String curName = getJsonNameFromYdtNode(ydtContext); |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 101 | YdtContext nextNode = ydtContext.getNextSibling(); |
| 102 | switch (ydtContext.getYdtType()) { |
| 103 | |
| 104 | case SINGLE_INSTANCE_NODE: |
| 105 | jsonBuilder.addNodeBottomHalf(JsonNodeType.OBJECT); |
| 106 | break; |
| 107 | case MULTI_INSTANCE_NODE: |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 108 | if (nextNode == null || !getJsonNameFromYdtNode(nextNode).equals(curName)) { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 109 | jsonBuilder.addNodeBottomHalf(JsonNodeType.OBJECT); |
| 110 | jsonBuilder.addNodeBottomHalf(JsonNodeType.ARRAY); |
| 111 | } else { |
| 112 | jsonBuilder.addNodeBottomHalf(JsonNodeType.OBJECT); |
| 113 | } |
| 114 | break; |
| 115 | case SINGLE_INSTANCE_LEAF_VALUE_NODE: |
| 116 | jsonBuilder.addNodeBottomHalf(JsonNodeType.STRING); |
| 117 | break; |
| 118 | case MULTI_INSTANCE_LEAF_VALUE_NODE: |
| 119 | jsonBuilder.addNodeBottomHalf(JsonNodeType.ARRAY); |
| 120 | break; |
Henry Yu | 52a8a72 | 2016-12-19 14:50:50 -0500 | [diff] [blame^] | 121 | case LOGICAL_ROOT_NODE: |
| 122 | break; |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 123 | default: |
| 124 | throw new YdtParseException("Unknown Ydt type " + |
| 125 | ydtContext.getYdtType()); |
| 126 | } |
| 127 | if (curName.equals(rootName) && |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 128 | (nextNode == null || !getJsonNameFromYdtNode(nextNode).equals(rootName))) { |
chengfan | c58d4be | 2016-09-20 10:33:12 +0800 | [diff] [blame] | 129 | isBegin = false; |
| 130 | } |
| 131 | } |
Henry Yu | 528007c | 2016-11-01 15:49:59 -0400 | [diff] [blame] | 132 | } |