Do not issue duplicate warnings for lists missing keys
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / list / AbstractListStatementSupport.java
index 3a1ec687587e1d05437e3684a3e5189a434a2253..2b02e0fd0e9b272fba47bdbc27345fe8f377a938 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.list;
 
+import static com.google.common.base.Verify.verify;
+
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
 import java.util.ArrayList;
@@ -14,6 +16,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Optional;
 import java.util.Set;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
@@ -37,7 +40,6 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
-import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -75,7 +77,6 @@ abstract class AbstractListStatementSupport extends BaseQNameStatementSupport<Li
     protected final ListEffectiveStatement createEffective(
             final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx,
             final ListStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
-        final StatementSourceReference ref = ctx.getStatementSourceReference();
         final SchemaPath path = ctx.getSchemaPath().get();
         final ListSchemaNode original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
                 .orElse(null);
@@ -94,8 +95,9 @@ abstract class AbstractListStatementSupport extends BaseQNameStatementSupport<Li
                 final QName keyQName = key.getLastComponent();
 
                 if (!possibleLeafQNamesForKey.contains(keyQName)) {
-                    throw new InferenceException(ref, "Key '%s' misses node '%s' in list '%s'",
-                        keyStmt.getDeclared().rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument());
+                    throw new InferenceException(ctx.getStatementSourceReference(),
+                        "Key '%s' misses node '%s' in list '%s'", keyStmt.getDeclared().rawArgument(),
+                        keyQName.getLocalName(), ctx.getStatementArgument());
                 }
                 keyDefinitionInit.add(keyQName);
             }
@@ -114,9 +116,7 @@ abstract class AbstractListStatementSupport extends BaseQNameStatementSupport<Li
                     .equals("user"))
                 .toFlags();
         if (configuration && keyDefinition.isEmpty() && isInstantied(ctx)) {
-            LOG.info("Configuration list {} does not define any keys in violation of RFC7950 section 7.8.2. While "
-                    + " this is fine with OpenDaylight, it can cause interoperability issues with other systems "
-                    + "[at {}]", ctx.getStatementArgument(), ref);
+            warnConfigList(ctx);
         }
 
         final Optional<ElementCountConstraint> elementCountConstraint =
@@ -127,6 +127,19 @@ abstract class AbstractListStatementSupport extends BaseQNameStatementSupport<Li
                             elementCountConstraint.orElse(null), original);
     }
 
+    private static void warnConfigList(final @NonNull StmtContext<QName, ListStatement, ListEffectiveStatement> ctx) {
+        final StmtContext<?, ?, ?> warnCtx = ctx.getOriginalCtx().orElse(ctx);
+        final Boolean warned = warnCtx.getFromNamespace(ConfigListWarningNamespace.class, Boolean.TRUE);
+        // Hacky check if we have issued a warning for the original statement
+        if (warned == null) {
+            verify(warnCtx instanceof Mutable, "Unexpected context %s", warnCtx);
+            ((Mutable<?, ?, ?>) warnCtx).addToNs(ConfigListWarningNamespace.class, Boolean.TRUE, Boolean.TRUE);
+            LOG.info("Configuration list {} does not define any keys in violation of RFC7950 section 7.8.2. While "
+                    + "this is fine with OpenDaylight, it can cause interoperability issues with other systems "
+                    + "[defined at {}]", ctx.getStatementArgument(), warnCtx.getStatementSourceReference());
+        }
+    }
+
     private static boolean isInstantied(final StmtContext<?, ?, ?> ctx) {
         for (StmtContext<?, ?, ?> parent = ctx.getParentContext(); parent != null; parent = parent.getParentContext()) {
             if (UNINSTANTIATED_DATATREE_STATEMENTS.contains(parent.getPublicDefinition())) {