Remove EffectiveAugmentationSchema.create()
[yangtools.git] / model / yang-model-util / src / main / java / org / opendaylight / yangtools / yang / model / util / EffectiveAugmentationSchema.java
index 0813e676d7b468933b9a4dba6f3b85ad77e800d9..30f54f7c07609a092b1a5d2b17034ced5802cf4d 100644 (file)
@@ -10,13 +10,8 @@ package org.opendaylight.yangtools.yang.model.util;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
 import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
 import java.util.Optional;
-import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.ActionDefinition;
 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
@@ -32,41 +27,48 @@ import org.opendaylight.yangtools.yang.model.api.stmt.AugmentEffectiveStatement;
 import org.opendaylight.yangtools.yang.xpath.api.YangXPathExpression.QualifiedBound;
 
 /**
- * Proxy for AugmentationSchema. Child node schemas are replaced with actual schemas from parent.
+ * Proxy for AugmentationSchema. Child node schemas are replaced with actual schemas from parent. This is needed to
+ * correctly interpret constructs like this:
+ * <pre>
+ *   <code>
+ *     container foo;
+ *
+ *     augment /foo {
+ *       container bar;
+ *     }
+ *
+ *     augment /foo/bar {
+ *       container baz;
+ *     }
+ *   </code>
+ * </pre>
+ * The {@link AugmentationSchemaNode} returned for {@code augment /foo} contains bare {@code container bar}, e.g. it
+ * does not show {@code augment /foo/bar} as an available augmentation -- this is only visible in {@code foo}'s schema
+ * nodes.
+ *
+ * <p>
+ * Note this class only handles {@link DataSchemaNode}s, not all {@code schema tree} statements, as it strictly should.
  */
-// FIXME: 7.0.0: re-evaluate the need for this class and potentially its effective statement replacement
+// FIXME: YANGTOOLS-1403: this functionality should be integrated into EffectiveAugmentStatement/AugmentationSchemaNode
 public final class EffectiveAugmentationSchema implements AugmentationSchemaNode {
+    private final ImmutableMap<QName, DataSchemaNode> children;
     private final AugmentationSchemaNode delegate;
-    private final ImmutableSet<DataSchemaNode> realChildSchemas;
-    private final ImmutableMap<QName, DataSchemaNode> mappedChildSchemas;
-
-    public EffectiveAugmentationSchema(final AugmentationSchemaNode augmentSchema,
-            final Collection<? extends DataSchemaNode> realChildSchemas) {
-        this.delegate = requireNonNull(augmentSchema);
-        this.realChildSchemas = ImmutableSet.copyOf(realChildSchemas);
-
-        final Map<QName, DataSchemaNode> m = new HashMap<>(realChildSchemas.size());
-        for (DataSchemaNode realChildSchema : realChildSchemas) {
-            m.put(realChildSchema.getQName(), realChildSchema);
-        }
 
-        this.mappedChildSchemas = ImmutableMap.copyOf(m);
-    }
-
-    /**
-     * Returns an AugmentationSchemaNode as effective in a parent node.
-     *
-     * @param schema Augmentation schema
-     * @param parent Parent schema
-     * @return Adjusted Augmentation schema
-     * @throws NullPointerException if any of the arguments is null
-     */
-    public static AugmentationSchemaNode create(final AugmentationSchemaNode schema, final DataNodeContainer parent) {
-        Set<DataSchemaNode> children = new HashSet<>();
-        for (DataSchemaNode augNode : schema.getChildNodes()) {
-            children.add(parent.getDataChildByName(augNode.getQName()));
+    public EffectiveAugmentationSchema(final AugmentationSchemaNode augment, final DataNodeContainer target) {
+        delegate = requireNonNull(augment);
+
+        final var augmentChildren = augment.getChildNodes();
+        final var builder = ImmutableMap.<QName, DataSchemaNode>builderWithExpectedSize(augmentChildren.size());
+        for (var augChild : augmentChildren) {
+            // parent may have the corresponding child removed via 'deviate unsupported', i.e. the child is effectively
+            // not present at the target site
+            final var qname = augChild.getQName();
+            final var targetChild = target.dataChildByName(qname);
+            if (targetChild != null) {
+                builder.put(qname, targetChild);
+            }
         }
-        return new EffectiveAugmentationSchema(schema, children);
+        children = builder.build();
     }
 
     @Override
@@ -101,7 +103,7 @@ public final class EffectiveAugmentationSchema implements AugmentationSchemaNode
 
     @Override
     public Collection<? extends DataSchemaNode> getChildNodes() {
-        return realChildSchemas;
+        return children.values();
     }
 
     @Override
@@ -111,7 +113,7 @@ public final class EffectiveAugmentationSchema implements AugmentationSchemaNode
 
     @Override
     public DataSchemaNode dataChildByName(final QName name) {
-        return mappedChildSchemas.get(requireNonNull(name));
+        return children.get(requireNonNull(name));
     }
 
     @Override