Bug 5200: Yang parser doesn't fill error-app-tag and error-message in constraints
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContextUtils.java
index 59863bdd7cd93c24d319f83283be3aa2c0fe07e6..6b5f8439df66415bf61a180ffbdd23af8080f1f3 100644 (file)
@@ -8,11 +8,11 @@
 package org.opendaylight.yangtools.yang.parser.spi.meta;
 
 import com.google.common.base.Function;
-import com.google.common.base.Predicate;
 import com.google.common.base.Splitter;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.ImmutableSet.Builder;
 import java.util.Collection;
-import java.util.LinkedHashSet;
-import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
@@ -22,9 +22,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
 
 public final class StmtContextUtils {
-
-    public static final char LIST_KEY_SEPARATOR = ' ';
-    private static final Splitter KEY_SPLITTER = Splitter.on(LIST_KEY_SEPARATOR).omitEmptyStrings().trimResults();
+    public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
 
     private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
             new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
@@ -42,14 +40,6 @@ public final class StmtContextUtils {
         }
     };
 
-    public static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
-            new Predicate<StmtContext<?,?,?>>() {
-        @Override
-        public boolean apply(final StmtContext<?, ?, ?> input) {
-            return input.isSupportedToBuildEffective();
-        }
-    };
-
     private StmtContextUtils() {
         throw new UnsupportedOperationException("Utility class");
     }
@@ -81,6 +71,12 @@ public final class StmtContextUtils {
         return producesDeclared(ctx, declaredType) ? (AT) ctx.getStatementArgument() : null;
     }
 
+    public static <AT, DT extends DeclaredStatement<AT>> AT firstSubstatementAttributeOf(
+            final StmtContext<?, ?, ?> ctx, final Class<DT> declaredType) {
+        AT firstAttribute = firstAttributeOf(ctx.effectiveSubstatements(), declaredType);
+        return firstAttribute != null ? firstAttribute : firstAttributeOf(ctx.declaredSubstatements(), declaredType);
+    }
+
     @SuppressWarnings("unchecked")
     public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
@@ -92,6 +88,55 @@ public final class StmtContextUtils {
         return null;
     }
 
+    @SuppressWarnings("unchecked")
+    public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllDeclaredSubstatements(
+            final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+        ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
+        for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+            if (producesDeclared(subStmtContext, declaredType)) {
+                listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
+            }
+        }
+        return listBuilder.build();
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllEffectiveSubstatements(
+            final StmtContext<?, ?, ?> stmtContext, final Class<DT> type) {
+        ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
+        for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
+            if (producesDeclared(subStmtContext, type)) {
+                listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
+            }
+        }
+        return listBuilder.build();
+    }
+
+    public static <AT, DT extends DeclaredStatement<AT>> Collection<StmtContext<AT, DT, ?>> findAllSubstatements(
+            final StmtContext<?, ?, ?> stmtContext, final Class<DT> type) {
+        ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
+        listBuilder.addAll(findAllDeclaredSubstatements(stmtContext, type));
+        listBuilder.addAll(findAllEffectiveSubstatements(stmtContext, type));
+        return listBuilder.build();
+    }
+
+    @SuppressWarnings("unchecked")
+    public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstEffectiveSubstatement(
+            final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+        for (StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
+            if (producesDeclared(subStmtContext,declaredType)) {
+                return (StmtContext<AT, ?, ?>) subStmtContext;
+            }
+        }
+        return null;
+    }
+
+    public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
+            final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
+        StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
+        return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext, declaredType);
+    }
+
     @SafeVarargs
     public static StmtContext<?, ?, ?> findFirstDeclaredSubstatement(final StmtContext<?, ?, ?> stmtContext,
             int startIndex, final Class<? extends DeclaredStatement<?>>... types) {
@@ -167,12 +212,21 @@ public final class StmtContextUtils {
             final StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?> keyStmtCtx,
             final QNameModule newQNameModule) {
 
-        Set<SchemaNodeIdentifier> newKeys = new LinkedHashSet<>();
-        for (String keyToken : KEY_SPLITTER.split(keyStmtCtx.rawStatementArgument())) {
-            final QName keyQName = QName.create(newQNameModule, keyToken);
-            newKeys.add(SchemaNodeIdentifier.create(false, keyQName));
+        final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
+        boolean replaced = false;
+        for (SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) {
+            final QName qname = arg.getLastComponent();
+            if (!newQNameModule.equals(qname)) {
+                final QName newQname = keyStmtCtx.getFromNamespace(QNameCacheNamespace.class,
+                    QName.create(newQNameModule, qname.getLocalName()));
+                builder.add(SchemaNodeIdentifier.create(false, newQname));
+                replaced = true;
+            } else {
+                builder.add(arg);
+            }
         }
 
-        return newKeys;
+        // This makes sure we reuse the collection when a grouping is instantiated in the same module
+        return replaced ? builder.build() : keyStmtCtx.getStatementArgument();
     }
 }