Improve StreamingContext diagnostics 49/98049/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 22 Oct 2021 15:59:43 +0000 (17:59 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 22 Oct 2021 16:00:32 +0000 (18:00 +0200)
When we encounter a wrong nested child we end up throwing an opaque
ClassCastException. Add an explicit check to improve diagnostics.

JIRA: NETCONF-820
Change-Id: I3c5a24c1ff4d12a56dfd5fd6d8cd3f940af330a0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
netconf/netconf-util/src/main/java/org/opendaylight/netconf/util/StreamingContext.java

index 2f1fc44bda006402e37544c1040a3889568a894b..eb02e2b72ec661144e4a8b3f2ba88c385c3c650a 100644 (file)
@@ -249,8 +249,8 @@ abstract class StreamingContext<T extends PathArgument> implements Identifiable<
 
         AbstractMapMixin(final ListSchemaNode list) {
             super(NodeIdentifier.create(list.getQName()));
-            this.innerNode = new ListEntry(NodeIdentifierWithPredicates.of(list.getQName()), list);
-            this.keyLeaves = list.getKeyDefinition();
+            innerNode = new ListEntry(NodeIdentifierWithPredicates.of(list.getQName()), list);
+            keyLeaves = list.getKeyDefinition();
         }
 
         @Override
@@ -265,7 +265,12 @@ abstract class StreamingContext<T extends PathArgument> implements Identifiable<
 
         @Override
         final void emitChildTreeNode(final NormalizedNodeStreamWriter writer, final PathNode node) throws IOException {
-            final NodeIdentifierWithPredicates childPath = (NodeIdentifierWithPredicates) node.element();
+            final PathArgument element = node.element();
+            if (!(element instanceof NodeIdentifierWithPredicates)) {
+                throw new IOException("Child identifier " + element + " is invalid in parent " + getIdentifier());
+            }
+
+            final NodeIdentifierWithPredicates childPath = (NodeIdentifierWithPredicates) element;
             final StreamingContext<?> childOp = getChildOperation(childPath);
             if (childPath.size() == 0 && node.isEmpty() || childPath.keySet().containsAll(keyLeaves)) {
                 // This is a query for the entire list, or the query specifies everything we need
@@ -529,7 +534,7 @@ abstract class StreamingContext<T extends PathArgument> implements Identifiable<
 
         UnkeyedListMixin(final ListSchemaNode list) {
             super(NodeIdentifier.create(list.getQName()));
-            this.innerNode = new UnkeyedListItem(list);
+            innerNode = new UnkeyedListItem(list);
         }
 
         @Override