Add notification support to SchemaTracker
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / SchemaTracker.java
index cb7747fc6c70ea2451629b943d14fc680f5f40db..725c115afb76ab167624c0deb60ab65a326df5fb 100644 (file)
@@ -8,12 +8,14 @@
 package org.opendaylight.yangtools.yang.data.impl.codec;
 
 import com.google.common.annotations.Beta;
+import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
 import java.io.IOException;
 import java.util.ArrayDeque;
 import java.util.Deque;
 import java.util.HashSet;
-import javax.xml.stream.XMLStreamWriter;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -32,6 +34,7 @@ import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
@@ -49,15 +52,20 @@ public final class SchemaTracker {
     private final DataNodeContainer root;
 
     private SchemaTracker(final SchemaContext context, final SchemaPath path) {
-        DataSchemaNode current = Preconditions.checkNotNull(context);
-        for (QName qname : path.getPathFromRoot()) {
-            final DataSchemaNode child;
+        SchemaNode current = Preconditions.checkNotNull(context);
+        for (final QName qname : path.getPathFromRoot()) {
+            SchemaNode child;
             if(current instanceof DataNodeContainer) {
                 child = ((DataNodeContainer) current).getDataChildByName(qname);
+
+                if(child == null && current instanceof SchemaContext) {
+                    child = tryFindNotification((SchemaContext) current, qname)
+                            .orNull();
+                }
             } else if (current instanceof ChoiceNode) {
                 child = ((ChoiceNode) current).getCaseNodeByName(qname);
             } else {
-                throw new IllegalArgumentException(String.format("Schema node %s does not allow children.",current));
+                throw new IllegalArgumentException(String.format("Schema node %s does not allow children.", current));
             }
             current = child;
         }
@@ -65,10 +73,13 @@ public final class SchemaTracker {
         this.root = (DataNodeContainer) current;
     }
 
+    private Optional<SchemaNode> tryFindNotification(final SchemaContext ctx, final QName qname) {
+        return Optional.<SchemaNode>fromNullable(Iterables.find(ctx.getNotifications(), new SchemaNodePredicate(qname), null));
+    }
+
     /**
      * Create a new writer with the specified context as its root.
      *
-     * @param writer Output {@link XMLStreamWriter}
      * @param context Associated {@link SchemaContext}.
      * @return A new {@link NormalizedNodeStreamWriter}
      */
@@ -79,8 +90,8 @@ public final class SchemaTracker {
     /**
      * Create a new writer with the specified context and rooted in the specified schema path
      *
-     * @param writer Output {@link XMLStreamWriter}
-     * @param context Associated {@link SchemaContext}.
+     * @param context Associated {@link SchemaContext}
+     * @param path schema path
      *
      * @return A new {@link NormalizedNodeStreamWriter}
      */
@@ -102,6 +113,9 @@ public final class SchemaTracker {
         if(parent instanceof DataNodeContainer) {
             schema = ((DataNodeContainer)parent).getDataChildByName(qname);
 
+            if(schema == null && parent instanceof NotificationDefinition) {
+                schema = ((NotificationDefinition) parent);
+            }
         } else if(parent instanceof ChoiceNode) {
             for(ChoiceCaseNode caze : ((ChoiceNode) parent).getCases()) {
                 DataSchemaNode potential = caze.getDataChildByName(qname);
@@ -117,7 +131,7 @@ public final class SchemaTracker {
         return schema;
     }
 
-    public void startList(final NodeIdentifier name) {
+    public void startList(final PathArgument name) {
         final SchemaNode schema = getSchema(name);
         Preconditions.checkArgument(schema instanceof ListSchemaNode, "Node %s is not a list", schema.getPath());
         schemaStack.push(schema);
@@ -136,12 +150,12 @@ public final class SchemaTracker {
         return (LeafSchemaNode) schema;
     }
 
-    public SchemaNode startLeafSet(final NodeIdentifier name) {
+    public LeafListSchemaNode startLeafSet(final NodeIdentifier name) {
         final SchemaNode schema = getSchema(name);
 
         Preconditions.checkArgument(schema instanceof LeafListSchemaNode, "Node %s is not a leaf-list", schema.getPath());
         schemaStack.push(schema);
-        return schema;
+        return (LeafListSchemaNode)schema;
     }
 
     public LeafListSchemaNode leafSetEntryNode() {
@@ -151,20 +165,23 @@ public final class SchemaTracker {
         return (LeafListSchemaNode) parent;
     }
 
-    public SchemaNode startChoiceNode(final NodeIdentifier name) {
+    public ChoiceNode startChoiceNode(final NodeIdentifier name) {
         LOG.debug("Enter choice {}", name);
         final SchemaNode schema = getSchema(name);
 
         Preconditions.checkArgument(schema instanceof ChoiceNode, "Node %s is not a choice", schema.getPath());
         schemaStack.push(schema);
-        return schema;
+        return (ChoiceNode)schema;
     }
 
     public SchemaNode startContainerNode(final NodeIdentifier name) {
         LOG.debug("Enter container {}", name);
         final SchemaNode schema = getSchema(name);
 
-        Preconditions.checkArgument(schema instanceof ContainerSchemaNode, "Node %s is not a container", schema.getPath());
+        boolean isAllowed = schema instanceof ContainerSchemaNode;
+        isAllowed |= schema instanceof NotificationDefinition;
+
+        Preconditions.checkArgument(isAllowed, "Node %s is not a container nor a notification", schema.getPath());
         schemaStack.push(schema);
         return schema;
     }
@@ -195,4 +212,17 @@ public final class SchemaTracker {
     public Object endNode() {
         return schemaStack.pop();
     }
+
+    private static final class SchemaNodePredicate implements Predicate<SchemaNode> {
+        private final QName qname;
+
+        public SchemaNodePredicate(final QName qname) {
+            this.qname = qname;
+        }
+
+        @Override
+        public boolean apply(final SchemaNode input) {
+            return input.getQName().equals(qname);
+        }
+    }
 }