Merge "Fix missing format placeholder"
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / transform / base / parser / BaseDispatcherParser.java
index 9de1b823acaa4399266e393832c45d6a511453d7..a594e5d763a8dbe51c8a33fd03d808b97d605d97 100644 (file)
@@ -7,11 +7,13 @@
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.transform.base.parser;
 
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.LinkedListMultimap;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
@@ -19,14 +21,10 @@ import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.AttributesBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.transform.ToNormalizedNodeParser;
-import org.opendaylight.yangtools.yang.data.impl.schema.transform.base.AugmentationSchemaProxy;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchema;
-import org.opendaylight.yangtools.yang.model.api.ChoiceNode;
+import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.LinkedListMultimap;
+import org.opendaylight.yangtools.yang.model.util.EffectiveAugmentationSchema;
 
 /**
  * Abstract(base) Parser for DataContainerNodes e.g. ContainerNode, AugmentationNode.
@@ -62,7 +60,7 @@ public abstract class BaseDispatcherParser<E, N extends DataContainerNode<?>, S>
      * @return map from QName to ChoiceNode schema of child nodes that are
      *         contained within a choice statement under current schema.
      */
-    protected abstract Map<QName, ChoiceNode> mapChildElementsFromChoices(S schema);
+    protected abstract Map<QName, ChoiceSchemaNode> mapChildElementsFromChoices(S schema);
 
     /**
      *
@@ -91,7 +89,7 @@ public abstract class BaseDispatcherParser<E, N extends DataContainerNode<?>, S>
     protected abstract NodeParserDispatcher<E> getDispatcher();
 
     @Override
-    public N parse(Iterable<E> elements, S schema) {
+    public N parse(final Iterable<E> elements, final S schema) {
 
         checkAtLeastOneNode(schema, elements);
 
@@ -105,12 +103,16 @@ public abstract class BaseDispatcherParser<E, N extends DataContainerNode<?>, S>
         LinkedListMultimap<AugmentationSchema, E> augmentsToElements = LinkedListMultimap.create();
 
         // Map child nodes from choices
-        Map<QName, ChoiceNode> mappedChoiceChildNodes = mapChildElementsFromChoices(schema);
-        LinkedListMultimap<ChoiceNode, E> choicesToElements = LinkedListMultimap.create();
+        Map<QName, ChoiceSchemaNode> mappedChoiceChildNodes = mapChildElementsFromChoices(schema);
+        LinkedListMultimap<ChoiceSchemaNode, E> choicesToElements = LinkedListMultimap.create();
 
         // process Child nodes
         for (QName childPartialQName : mappedChildElements.keySet()) {
             DataSchemaNode childSchema = getSchemaForChild(schema, childPartialQName);
+            //with strict parsing an exception would be already thrown, with nonstrict we want to ignore this node
+            if (childSchema == null) {
+                continue;
+            }
             List<E> childrenForQName = mappedChildElements.get(childPartialQName);
 
             // Augment
@@ -119,7 +121,7 @@ public abstract class BaseDispatcherParser<E, N extends DataContainerNode<?>, S>
                 augmentsToElements.putAll(augmentationSchema, childrenForQName);
                 // Choices
             } else if (isMarkedAs(mappedChoiceChildNodes, childSchema.getQName())) {
-                ChoiceNode choiceSchema = mappedChoiceChildNodes.get(childSchema.getQName());
+                ChoiceSchemaNode choiceSchema = mappedChoiceChildNodes.get(childSchema.getQName());
                 choicesToElements.putAll(choiceSchema, childrenForQName);
                 // Regular child nodes
             } else {
@@ -130,14 +132,14 @@ public abstract class BaseDispatcherParser<E, N extends DataContainerNode<?>, S>
         }
 
         // TODO ordering is not preserved for choice and augment elements
-        for (ChoiceNode choiceSchema : choicesToElements.keySet()) {
+        for (ChoiceSchemaNode choiceSchema : choicesToElements.keySet()) {
             containerBuilder.withChild(getDispatcher().dispatchChildElement(choiceSchema,
                     choicesToElements.get(choiceSchema)));
         }
 
         for (AugmentationSchema augmentSchema : augmentsToElements.keySet()) {
             Set<DataSchemaNode> realChildSchemas = getRealSchemasForAugment(schema, augmentSchema);
-            AugmentationSchemaProxy augSchemaProxy = new AugmentationSchemaProxy(augmentSchema, realChildSchemas);
+            EffectiveAugmentationSchema augSchemaProxy = new EffectiveAugmentationSchema(augmentSchema, realChildSchemas);
             containerBuilder.withChild(getDispatcher().dispatchChildElement(augSchemaProxy, augmentsToElements.get(augmentSchema)));
         }
 
@@ -151,22 +153,26 @@ public abstract class BaseDispatcherParser<E, N extends DataContainerNode<?>, S>
         return containerBuilder.build();
     }
 
-    protected Map<QName, String> getAttributes(E e) {
+    protected Map<QName, String> getAttributes(final E e) {
         return Collections.emptyMap();
     }
 
-    private boolean isMarkedAs(Map<QName, ?> mappedAugmentChildNodes, QName qName) {
+    protected boolean strictParsing() {
+        return true;
+    }
+
+    private boolean isMarkedAs(final Map<QName, ?> mappedAugmentChildNodes, final QName qName) {
         return mappedAugmentChildNodes.containsKey(qName);
     }
 
-    protected void checkOnlyOneNode(S schema, Iterable<E> childNodes) {
+    protected void checkOnlyOneNode(final S schema, final Iterable<E> childNodes) {
         final int size = Iterables.size(childNodes);
         Preconditions.checkArgument(size == 1,
                 "Node detected multiple times, should be 1, identified by: %s, found: %s", schema, childNodes);
     }
 
-    private void checkAtLeastOneNode(S schema, Iterable<E> childNodes) {
-        Preconditions.checkArgument(Iterables.isEmpty(childNodes) == false,
+    private void checkAtLeastOneNode(final S schema, final Iterable<E> childNodes) {
+        Preconditions.checkArgument(!Iterables.isEmpty(childNodes),
                 "Node detected 0 times, should be at least 1, identified by: %s, found: %s", schema, childNodes);
     }
 }