[ONOS-5580] Bug fix for JSON encoding of augmented node names
The submission contains:
1. Fix of ONOS-5580
2. Enable RESTCONF server to register and receive notifications from YMS
Change-Id: I8d27a7ee59679de22b7f49b370b35e48909958a4
diff --git a/protocols/restconf/server/utils/src/main/java/org/onosproject/protocol/restconf/server/utils/parser/json/ParserUtils.java b/protocols/restconf/server/utils/src/main/java/org/onosproject/protocol/restconf/server/utils/parser/json/ParserUtils.java
index 5ede187..2bf7f74 100755
--- a/protocols/restconf/server/utils/src/main/java/org/onosproject/protocol/restconf/server/utils/parser/json/ParserUtils.java
+++ b/protocols/restconf/server/utils/src/main/java/org/onosproject/protocol/restconf/server/utils/parser/json/ParserUtils.java
@@ -335,4 +335,62 @@
String name = getLatterSegment(field, COLON);
return new NormalizedYangNode(namespace, name);
}
+
+
+ /**
+ * Extracts the node name from a YDT node and encodes it in JSON format.
+ * A JSON encoded node name has the following format:
+ * <p>
+ * module_name ":" node_name
+ * <p>
+ * where module_name is name of the YANG module in which the data
+ * resource is defined, and node_name is the name of the data resource.
+ * <p>
+ * If the YDT node is null or its node name field is null, then the function
+ * returns null. If the node name field is not null but module name field is,
+ * then the function returns only the node name.
+ *
+ * @param ydtContext YDT node of the target data resource
+ * @return JSON encoded name of the target data resource
+ */
+ public static String getJsonNameFromYdtNode(YdtContext ydtContext) {
+ if (ydtContext == null) {
+ return null;
+ }
+
+ String nodeName = ydtContext.getName();
+ if (nodeName == null) {
+ return null;
+ }
+
+ /*
+ * The namespace field in YDT node is a string which contains a list
+ * of identifiers separated by colon (:). e.g.,
+ *
+ * {identifier ":" identifier}+
+ *
+ * The last identifier in the string is the YANG module name.
+ */
+ String moduleName = getModuleNameFromNamespace(ydtContext.getNamespace());
+ if (moduleName == null) {
+ return nodeName;
+ } else {
+ return moduleName + COLON + nodeName;
+ }
+ }
+
+ private static String getModuleNameFromNamespace(String namespace) {
+ if (namespace == null) {
+ return null;
+ }
+
+ String moduleName = null;
+
+ if (namespace.contains(COLON)) {
+ String[] tokens = namespace.split(COLON);
+ moduleName = tokens[tokens.length - 1];
+ }
+
+ return moduleName;
+ }
}
diff --git a/protocols/restconf/server/utils/src/main/java/org/onosproject/protocol/restconf/server/utils/parser/json/YdtToJsonListener.java b/protocols/restconf/server/utils/src/main/java/org/onosproject/protocol/restconf/server/utils/parser/json/YdtToJsonListener.java
index 4ffd6f1..eeee124 100644
--- a/protocols/restconf/server/utils/src/main/java/org/onosproject/protocol/restconf/server/utils/parser/json/YdtToJsonListener.java
+++ b/protocols/restconf/server/utils/src/main/java/org/onosproject/protocol/restconf/server/utils/parser/json/YdtToJsonListener.java
@@ -23,6 +23,7 @@
import org.onosproject.yms.ydt.YdtListener;
import static com.google.common.base.Strings.isNullOrEmpty;
+import static org.onosproject.protocol.restconf.server.utils.parser.json.ParserUtils.getJsonNameFromYdtNode;
/**
* Represents implementation of codec YDT listener.
@@ -53,7 +54,7 @@
@Override
public void enterYdtNode(YdtContext ydtContext) {
- String name = ydtContext.getName();
+ String name = getJsonNameFromYdtNode(ydtContext);
if (!isBegin && name != null && name.equals(rootName)) {
isBegin = true;
@@ -69,7 +70,7 @@
break;
case MULTI_INSTANCE_NODE:
YdtContext preNode = ydtContext.getPreviousSibling();
- if (preNode == null || !preNode.getName().equals(name)) {
+ if (preNode == null || !getJsonNameFromYdtNode(preNode).equals(name)) {
jsonBuilder.addNodeTopHalf(name, JsonNodeType.ARRAY);
}
jsonBuilder.addNodeTopHalf(EMPTY, JsonNodeType.OBJECT);
@@ -94,7 +95,7 @@
return;
}
- String curName = ydtContext.getName();
+ String curName = getJsonNameFromYdtNode(ydtContext);
YdtContext nextNode = ydtContext.getNextSibling();
switch (ydtContext.getYdtType()) {
@@ -102,7 +103,7 @@
jsonBuilder.addNodeBottomHalf(JsonNodeType.OBJECT);
break;
case MULTI_INSTANCE_NODE:
- if (nextNode == null || !nextNode.getName().equals(curName)) {
+ if (nextNode == null || !getJsonNameFromYdtNode(nextNode).equals(curName)) {
jsonBuilder.addNodeBottomHalf(JsonNodeType.OBJECT);
jsonBuilder.addNodeBottomHalf(JsonNodeType.ARRAY);
} else {
@@ -120,8 +121,8 @@
ydtContext.getYdtType());
}
if (curName.equals(rootName) &&
- (nextNode == null || !nextNode.getName().equals(rootName))) {
+ (nextNode == null || !getJsonNameFromYdtNode(nextNode).equals(rootName))) {
isBegin = false;
}
}
-}
\ No newline at end of file
+}