Rework NormalizedNode type hierarchy
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / CaseEnforcer.java
index e0e3b6443660b5fad65ec0aa566fd28cdb9110ea..14dcd267e09c0959db527275c112bf08c3e3efa1 100644 (file)
  */
 package org.opendaylight.yangtools.yang.data.impl.schema.tree;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
-import java.util.Map;
+import com.google.common.collect.Sets;
 import java.util.Map.Entry;
+import java.util.Optional;
 import java.util.Set;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
-import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
+import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 
-final class CaseEnforcer implements Immutable {
-    private final Map<NodeIdentifier, DataSchemaNode> children;
-    private final MandatoryLeafEnforcer enforcer;
+class CaseEnforcer implements Immutable {
+    private static final class EnforcingMandatory extends CaseEnforcer {
+        private final MandatoryLeafEnforcer enforcer;
+
+        EnforcingMandatory(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
+                final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations,
+                final MandatoryLeafEnforcer enforcer) {
+            super(children, augmentations);
+            this.enforcer = requireNonNull(enforcer);
+        }
+
+        @Override
+        void enforceOnTreeNode(final NormalizedNode normalizedNode) {
+            enforcer.enforceOnData(normalizedNode);
+        }
+    }
 
-    private CaseEnforcer(final Map<NodeIdentifier, DataSchemaNode> children, final MandatoryLeafEnforcer enforcer) {
-        this.children = Preconditions.checkNotNull(children);
-        this.enforcer = Preconditions.checkNotNull(enforcer);
+    private final ImmutableMap<NodeIdentifier, DataSchemaNode> children;
+    private final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations;
+
+    CaseEnforcer(final ImmutableMap<NodeIdentifier, DataSchemaNode> children,
+            final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations) {
+        this.children = requireNonNull(children);
+        this.augmentations = requireNonNull(augmentations);
     }
 
-    static CaseEnforcer forTree(final ChoiceCaseNode schema, final TreeType type) {
-        final Builder<NodeIdentifier, DataSchemaNode> builder = ImmutableMap.builder();
+    static CaseEnforcer forTree(final CaseSchemaNode schema, final DataTreeConfiguration treeConfig) {
+        final TreeType type = treeConfig.getTreeType();
+        final Builder<NodeIdentifier, DataSchemaNode> childrenBuilder = ImmutableMap.builder();
+        final Builder<AugmentationIdentifier, AugmentationSchemaNode> augmentationsBuilder = ImmutableMap.builder();
         if (SchemaAwareApplyOperation.belongsToTree(type, schema)) {
-            for (DataSchemaNode child : schema.getChildNodes()) {
+            for (final DataSchemaNode child : schema.getChildNodes()) {
                 if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
-                    builder.put(NodeIdentifier.create(child.getQName()), child);
+                    childrenBuilder.put(NodeIdentifier.create(child.getQName()), child);
+                }
+            }
+            for (final AugmentationSchemaNode augment : schema.getAvailableAugmentations()) {
+                if (augment.getChildNodes().stream()
+                        .anyMatch(child -> SchemaAwareApplyOperation.belongsToTree(type, child))) {
+                    augmentationsBuilder.put(DataSchemaContextNode.augmentationIdentifierFrom(augment), augment);
                 }
             }
         }
 
-        final Map<NodeIdentifier, DataSchemaNode> children = builder.build();
-        return children.isEmpty() ? null : new CaseEnforcer(children, MandatoryLeafEnforcer.forContainer(schema, type));
+        final ImmutableMap<NodeIdentifier, DataSchemaNode> children = childrenBuilder.build();
+        if (children.isEmpty()) {
+            return null;
+        }
+        final ImmutableMap<AugmentationIdentifier, AugmentationSchemaNode> augmentations = augmentationsBuilder.build();
+        final Optional<MandatoryLeafEnforcer> enforcer = MandatoryLeafEnforcer.forContainer(schema, treeConfig);
+        return enforcer.isPresent() ? new EnforcingMandatory(children, augmentations, enforcer.get())
+                : new CaseEnforcer(children, augmentations);
     }
 
-    Set<Entry<NodeIdentifier, DataSchemaNode>> getChildEntries() {
+    final Set<Entry<NodeIdentifier, DataSchemaNode>> getChildEntries() {
         return children.entrySet();
     }
 
-    Set<NodeIdentifier> getChildIdentifiers() {
+    final Set<NodeIdentifier> getChildIdentifiers() {
         return children.keySet();
     }
 
-    void enforceOnTreeNode(final NormalizedNode<?, ?> normalizedNode) {
-        enforcer.enforceOnTreeNode(normalizedNode);
+    final Set<Entry<AugmentationIdentifier, AugmentationSchemaNode>> getAugmentationEntries() {
+        return augmentations.entrySet();
+    }
+
+    final Set<AugmentationIdentifier> getAugmentationIdentifiers() {
+        return augmentations.keySet();
+    }
+
+    final Set<PathArgument> getAllChildIdentifiers() {
+        return Sets.union(children.keySet(), augmentations.keySet());
+    }
+
+    void enforceOnTreeNode(final NormalizedNode normalizedNode) {
+        // Default is no-op
     }
 }