Remove MandatoryDescendant 61/106061/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 18 May 2023 22:12:02 +0000 (00:12 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 18 May 2023 22:19:40 +0000 (00:19 +0200)
We no longer have to deal with augmentations: hence we just need to
keep YangInstanceIdentifiers to descendants.

JIRA: YANGTOOLS-568
Change-Id: Ica2ca74660f5e5a0367bb9f67e3a2550eed9f883
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/MandatoryDescendant.java [deleted file]
data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/MandatoryLeafEnforcer.java

diff --git a/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/MandatoryDescendant.java b/data/yang-data-tree-ri/src/main/java/org/opendaylight/yangtools/yang/data/tree/impl/MandatoryDescendant.java
deleted file mode 100644 (file)
index 1bba513..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-/*
- * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.data.tree.impl;
-
-import static com.google.common.base.Verify.verify;
-import static java.util.Objects.requireNonNull;
-
-import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.yangtools.concepts.Immutable;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
-import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.AugmentationTarget;
-import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * A path to a descendant which must exist. This really should be equivalent to YangInstanceIdentifier, but for now we
- * need to deal with up to two possible paths -- one with AugmentationIdentifiers and one without.
- */
-// FIXME: 8.0.0: remove this structure and just keep the 'path' YangInstanceIdentifier
-final class MandatoryDescendant implements Immutable {
-    private static final Logger LOG = LoggerFactory.getLogger(MandatoryDescendant.class);
-
-    // Correctly-nested path, mirroring Augmentation and choice/case structure
-    private final @NonNull YangInstanceIdentifier path;
-    // Legacy trivial path,
-    private final @Nullable YangInstanceIdentifier legacyPath;
-
-    private MandatoryDescendant(final YangInstanceIdentifier path, final YangInstanceIdentifier legacyPath) {
-        this.path = requireNonNull(path);
-        this.legacyPath = legacyPath;
-    }
-
-    static @NonNull MandatoryDescendant create(final YangInstanceIdentifier parentId,
-            final DataNodeContainer parentSchema, final DataSchemaNode childSchema) {
-        return new MandatoryDescendant(parentId.node(NodeIdentifier.create(childSchema.getQName())).toOptimized(),
-            null);
-    }
-
-    void enforceOnData(final NormalizedNode data) {
-        // Find try primary path first ...
-        if (NormalizedNodes.findNode(data, path).isPresent()) {
-            return;
-        }
-        // ... if we have a legacy path, try that as well ...
-        if (legacyPath != null && NormalizedNodes.findNode(data, legacyPath).isPresent()) {
-            // .. this should not really be happening ...
-            LOG.debug("Found {} at alternate path {}", path, legacyPath);
-            return;
-        }
-
-        // ... not found, report the error
-        throw new IllegalArgumentException(String.format("Node %s is missing mandatory descendant %s",
-            data.name(), path));
-    }
-
-    static AugmentationSchemaNode getAugIdentifierOfChild(final DataNodeContainer parent, final DataSchemaNode child) {
-        verify(parent instanceof AugmentationTarget,
-            "Unexpected augmenting child %s in non-target parent %s", child, parent);
-        return ((AugmentationTarget) parent).getAvailableAugmentations().stream()
-            .filter(augment -> augment.findDataChildByName(child.getQName()).isPresent())
-            .findFirst()
-            .orElseThrow(() -> new IllegalArgumentException(String.format(
-                "Node %s is marked as augmenting but is not present in the schema of %s", child.getQName(), parent)));
-    }
-
-    @Override
-    public String toString() {
-        return legacyPath == null ? path.toString() : "(" + path + " || " + legacyPath + ")";
-    }
-}
index 3ab1934b15fd0b8a32dea57bce14fb9cec5d61b0..b2d23af2a321c4cf0a409f6dd1c78538d6c5bccc 100644 (file)
@@ -16,12 +16,12 @@ import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
 import org.opendaylight.yangtools.yang.data.tree.api.TreeType;
 import org.opendaylight.yangtools.yang.data.tree.impl.node.TreeNode;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
-import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraintAware;
 import org.opendaylight.yangtools.yang.model.api.MandatoryAware;
 import org.slf4j.Logger;
@@ -31,9 +31,9 @@ import org.slf4j.LoggerFactory;
 final class MandatoryLeafEnforcer implements Immutable {
     private static final Logger LOG = LoggerFactory.getLogger(MandatoryLeafEnforcer.class);
 
-    private final ImmutableList<MandatoryDescendant> mandatoryNodes;
+    private final ImmutableList<YangInstanceIdentifier> mandatoryNodes;
 
-    private MandatoryLeafEnforcer(final ImmutableList<MandatoryDescendant> mandatoryNodes) {
+    private MandatoryLeafEnforcer(final ImmutableList<YangInstanceIdentifier> mandatoryNodes) {
         this.mandatoryNodes = requireNonNull(mandatoryNodes);
     }
 
@@ -43,23 +43,28 @@ final class MandatoryLeafEnforcer implements Immutable {
             return null;
         }
 
-        final var builder = ImmutableList.<MandatoryDescendant>builder();
+        final var builder = ImmutableList.<YangInstanceIdentifier>builder();
         findMandatoryNodes(builder, YangInstanceIdentifier.empty(), schema, treeConfig.getTreeType());
         final var mandatoryNodes = builder.build();
         return mandatoryNodes.isEmpty() ? null : new MandatoryLeafEnforcer(mandatoryNodes);
     }
 
     void enforceOnData(final NormalizedNode data) {
-        mandatoryNodes.forEach(node -> node.enforceOnData(data));
+        for (var path : mandatoryNodes) {
+            if (!NormalizedNodes.findNode(data, path).isPresent()) {
+                throw new IllegalArgumentException(String.format("Node %s is missing mandatory descendant %s",
+                    data.name(), path));
+            }
+        }
     }
 
     void enforceOnTreeNode(final TreeNode tree) {
         enforceOnData(tree.getData());
     }
 
-    private static void findMandatoryNodes(final Builder<MandatoryDescendant> builder, final YangInstanceIdentifier id,
-            final DataNodeContainer schema, final TreeType type) {
-        for (final DataSchemaNode child : schema.getChildNodes()) {
+    private static void findMandatoryNodes(final Builder<YangInstanceIdentifier> builder,
+            final YangInstanceIdentifier id, final DataNodeContainer schema, final TreeType type) {
+        for (var child : schema.getChildNodes()) {
             if (SchemaAwareApplyOperation.belongsToTree(type, child)) {
                 if (child instanceof ContainerSchemaNode container) {
                     if (!container.isPresenceContainer()) {
@@ -81,7 +86,7 @@ final class MandatoryLeafEnforcer implements Immutable {
                             .orElse(Boolean.FALSE);
                     }
                     if (needEnforce) {
-                        final MandatoryDescendant desc = MandatoryDescendant.create(id, schema, child);
+                        final var desc = id.node(NodeIdentifier.create(child.getQName())).toOptimized();
                         LOG.debug("Adding mandatory child {}", desc);
                         builder.add(desc);
                     }