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