Bug 6158: Optimization of if-feaures resolution during SchemaContext assembly 71/41471/7
authorPeter Kajsa <pkajsa@cisco.com>
Thu, 7 Jul 2016 08:31:49 +0000 (10:31 +0200)
committerRobert Varga <nite@hq.sk>
Mon, 11 Jul 2016 10:51:51 +0000 (10:51 +0000)
It is unnecessary to check both declared and effective substatements everytime,
because effective substatements are empty at the beginning and they are filled
subsequently by copying of declared. Therefore this patch check whether
a feature is supported only for declarted statements during their build and
also creation of their copy.
In addition this patch introduces ALL_FEATURES predicate constant. The constant
is passed as argument into default SchemaContext build, and so we can simply
return true and skip rest of areFeaturesSupported method.
Moreover the result of areFeaturesSupported method is cached and so it is
not computed more times for any statement.

Change-Id: If8c87975712b65f9f28b640ac0b634576e08c191
Signed-off-by: Peter Kajsa <pkajsa@cisco.com>
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/IfFeaturePredicates.java [new file with mode: 0644]
yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/SchemaContextFactory.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContext.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/spi/meta/StmtContextUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/CrossSourceStatementReactor.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/RootStatementContext.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/StatementContextBase.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/reactor/SubstatementContext.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/AugmentUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/GroupingUtils.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/EffectiveStatementBase.java

diff --git a/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/IfFeaturePredicates.java b/yang/yang-model-api/src/main/java/org/opendaylight/yangtools/yang/model/repo/api/IfFeaturePredicates.java
new file mode 100644 (file)
index 0000000..e381be6
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. 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.model.repo.api;
+
+import com.google.common.annotations.Beta;
+import java.util.function.Predicate;
+import org.opendaylight.yangtools.yang.common.QName;
+
+@Beta
+/**
+ * If-feature predicate constants.
+ */
+public final class IfFeaturePredicates {
+    /**
+     * All features predicate constant. Using of this constant improves
+     * performance of schema context resolution.
+     */
+    public static final Predicate<QName> ALL_FEATURES = t -> true;
+
+    private IfFeaturePredicates() {
+        throw new UnsupportedOperationException("Utility class");
+    }
+}
index ddf9c53770ed546811c330c3c67ea5b4ef3730c8..eaa06ce6ad8b71e1427b80ea9d4bc6feee81b12f 100644 (file)
@@ -34,7 +34,7 @@ public interface SchemaContextFactory {
      */
     default CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
             @Nonnull Collection<SourceIdentifier> requiredSources) {
-        return createSchemaContext(requiredSources, StatementParserMode.DEFAULT_MODE, t -> true);
+        return createSchemaContext(requiredSources, StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES);
     }
 
     /**
@@ -51,7 +51,7 @@ public interface SchemaContextFactory {
      */
     default CheckedFuture<SchemaContext, SchemaResolutionException> createSchemaContext(
             Collection<SourceIdentifier> requiredSources, StatementParserMode statementParserMode) {
-        return createSchemaContext(requiredSources, statementParserMode, t -> true);
+        return createSchemaContext(requiredSources, statementParserMode, IfFeaturePredicates.ALL_FEATURES);
     }
 
     /**
index de95986d476a35ab93c79113c40c693f3c383d6b..fd44a86e0d4a06951d2a943ee2505839d0648fd0 100644 (file)
@@ -101,6 +101,12 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
 
     List<TypeOfCopy> getCopyHistory();
 
+    enum SupportedByFeatures {
+        UNDEFINED, SUPPORTED, NOT_SUPPORTED
+    }
+
+    SupportedByFeatures getSupportedByFeatures();
+
     void addAllToCopyHistory(List<TypeOfCopy> typeOfCopyList);
 
     void addToCopyHistory(TypeOfCopy typeOfCopy);
@@ -137,6 +143,7 @@ public interface StmtContext<A, D extends DeclaredStatement<A>, E extends Effect
         <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(
                 Class<N> namespace, KT key, StmtContext<?, ?, ?> stmt);
 
+        void setSupportedByFeatures(boolean isSupported);
     }
 
 }
index c9d389f05cd4e7eb61ea74edc7936587646392a4..35b1d20dee7a1337047b71b64052d7f0d00de780 100644 (file)
@@ -12,7 +12,6 @@ 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.ArrayList;
 import java.util.Collection;
 import java.util.function.Predicate;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -22,6 +21,7 @@ import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.KeyStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.IfFeaturePredicates;
 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.source.SupportedFeaturesNamespace.SupportedFeatures;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
@@ -30,16 +30,14 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
 public final class StmtContextUtils {
     public static final Splitter LIST_KEY_SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
 
-    private static final Function<StmtContext<?, ?,?>, DeclaredStatement<?>> BUILD_DECLARED =
-            new Function<StmtContext<?,?,?>, DeclaredStatement<?>>() {
+    private static final Function<StmtContext<?, ?, ?>, DeclaredStatement<?>> BUILD_DECLARED = new Function<StmtContext<?, ?, ?>, DeclaredStatement<?>>() {
         @Override
         public DeclaredStatement<?> apply(final StmtContext<?, ?, ?> input) {
             return input.buildDeclared();
         }
     };
 
-    private static final Function<StmtContext<?, ?,?>, EffectiveStatement<?,?>> BUILD_EFFECTIVE =
-            new Function<StmtContext<?,?,?>, EffectiveStatement<?,?>>() {
+    private static final Function<StmtContext<?, ?, ?>, EffectiveStatement<?, ?>> BUILD_EFFECTIVE = new Function<StmtContext<?, ?, ?>, EffectiveStatement<?, ?>>() {
         @Override
         public EffectiveStatement<?, ?> apply(final StmtContext<?, ?, ?> input) {
             return input.buildEffective();
@@ -63,7 +61,7 @@ public final class StmtContextUtils {
     @SuppressWarnings("unchecked")
     public static <AT, DT extends DeclaredStatement<AT>> AT firstAttributeOf(
             final Iterable<? extends StmtContext<?, ?, ?>> contexts, final Class<DT> declaredType) {
-        for (StmtContext<?, ?, ?> ctx : contexts) {
+        for (final StmtContext<?, ?, ?> ctx : contexts) {
             if (producesDeclared(ctx, declaredType)) {
                 return (AT) ctx.getStatementArgument();
             }
@@ -79,15 +77,15 @@ public final class StmtContextUtils {
 
     public static <AT, DT extends DeclaredStatement<AT>> AT firstSubstatementAttributeOf(
             final StmtContext<?, ?, ?> ctx, final Class<DT> declaredType) {
-        AT firstAttribute = firstAttributeOf(ctx.effectiveSubstatements(), declaredType);
+        final 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(
+    public static <AT, DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstDeclaredSubstatement(
             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
-        for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
-            if (producesDeclared(subStmtContext,declaredType)) {
+        for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+            if (producesDeclared(subStmtContext, declaredType)) {
                 return (StmtContext<AT, ?, ?>) subStmtContext;
             }
         }
@@ -97,8 +95,8 @@ public final class StmtContextUtils {
     @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()) {
+        final ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
+        for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
             if (producesDeclared(subStmtContext, declaredType)) {
                 listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
             }
@@ -109,8 +107,8 @@ public final class StmtContextUtils {
     @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()) {
+        final ImmutableList.Builder<StmtContext<AT, DT, ?>> listBuilder = ImmutableList.builder();
+        for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
             if (producesDeclared(subStmtContext, type)) {
                 listBuilder.add((StmtContext<AT, DT, ?>) subStmtContext);
             }
@@ -120,27 +118,28 @@ public final class StmtContextUtils {
 
     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();
+        final 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(
+    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)) {
+        for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.effectiveSubstatements()) {
+            if (producesDeclared(subStmtContext, declaredType)) {
                 return (StmtContext<AT, ?, ?>) subStmtContext;
             }
         }
         return null;
     }
 
-    public static <AT,DT extends DeclaredStatement<AT>> StmtContext<AT, ?, ?> findFirstSubstatement(
+    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);
+        final StmtContext<AT, ?, ?> declaredSubstatement = findFirstDeclaredSubstatement(stmtContext, declaredType);
+        return declaredSubstatement != null ? declaredSubstatement : findFirstEffectiveSubstatement(stmtContext,
+                declaredType);
     }
 
     @SafeVarargs
@@ -150,10 +149,10 @@ public final class StmtContextUtils {
             return null;
         }
 
-        for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
-            if (producesDeclared(subStmtContext,types[startIndex])) {
-                return startIndex + 1 == types.length ? subStmtContext
-                        : findFirstDeclaredSubstatement(subStmtContext, ++startIndex, types);
+        for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+            if (producesDeclared(subStmtContext, types[startIndex])) {
+                return startIndex + 1 == types.length ? subStmtContext : findFirstDeclaredSubstatement(subStmtContext,
+                        ++startIndex, types);
             }
         }
         return null;
@@ -161,13 +160,13 @@ public final class StmtContextUtils {
 
     public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findFirstDeclaredSubstatementOnSublevel(
             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType, int sublevel) {
-        for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+        for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
             if (sublevel == 1 && producesDeclared(subStmtContext, declaredType)) {
                 return subStmtContext;
             }
             if (sublevel > 1) {
-                final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(
-                    subStmtContext, declaredType, --sublevel);
+                final StmtContext<?, ?, ?> result = findFirstDeclaredSubstatementOnSublevel(subStmtContext,
+                        declaredType, --sublevel);
                 if (result != null) {
                     return result;
                 }
@@ -179,7 +178,7 @@ public final class StmtContextUtils {
 
     public static <DT extends DeclaredStatement<?>> StmtContext<?, ?, ?> findDeepFirstDeclaredSubstatement(
             final StmtContext<?, ?, ?> stmtContext, final Class<DT> declaredType) {
-        for (StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
+        for (final StmtContext<?, ?, ?> subStmtContext : stmtContext.declaredSubstatements()) {
             if (producesDeclared(subStmtContext, declaredType)) {
                 return subStmtContext;
             }
@@ -198,8 +197,8 @@ public final class StmtContextUtils {
         return type.isAssignableFrom(ctx.getPublicDefinition().getDeclaredRepresentationClass());
     }
 
-    public static boolean isInExtensionBody(final StmtContext<?,?,?> stmtCtx) {
-        StmtContext<?,?,?> current = stmtCtx;
+    public static boolean isInExtensionBody(final StmtContext<?, ?, ?> stmtCtx) {
+        StmtContext<?, ?, ?> current = stmtCtx;
         while (!current.getParentContext().isRootContext()) {
             current = current.getParentContext();
             if (producesDeclared(current, UnknownStatementImpl.class)) {
@@ -220,11 +219,11 @@ public final class StmtContextUtils {
 
         final Builder<SchemaNodeIdentifier> builder = ImmutableSet.builder();
         boolean replaced = false;
-        for (SchemaNodeIdentifier arg : keyStmtCtx.getStatementArgument()) {
+        for (final 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()));
+                        QName.create(newQNameModule, qname.getLocalName()));
                 builder.add(SchemaNodeIdentifier.create(false, newQname));
                 replaced = true;
             } else {
@@ -232,20 +231,40 @@ public final class StmtContextUtils {
             }
         }
 
-        // This makes sure we reuse the collection when a grouping is instantiated in the same module
+        // This makes sure we reuse the collection when a grouping is
+        // instantiated in the same module
         return replaced ? builder.build() : keyStmtCtx.getStatementArgument();
     }
 
-    public static boolean areFeaturesSupported(final StmtContext<?, ?, ?> stmtContext) {
-        Predicate<QName> isFeatureSupported = stmtContext.getFromNamespace(SupportedFeaturesNamespace.class,
+    public static boolean areFeaturesSupported(final StmtContext.Mutable<?, ?, ?> stmtContext) {
+        switch (stmtContext.getSupportedByFeatures()) {
+        case SUPPORTED:
+            return true;
+        case NOT_SUPPORTED:
+            return false;
+        default:
+            break;
+        }
+
+        final Predicate<QName> isFeatureSupported = stmtContext.getFromNamespace(SupportedFeaturesNamespace.class,
                 SupportedFeatures.SUPPORTED_FEATURES);
-        Collection<StatementContextBase<?, ?, ?>> substatements = new ArrayList<>();
-        substatements.addAll(stmtContext.declaredSubstatements());
-        substatements.addAll(stmtContext.effectiveSubstatements());
+        if (IfFeaturePredicates.ALL_FEATURES.equals(isFeatureSupported)) {
+            stmtContext.setSupportedByFeatures(true);
+            return true;
+        }
+
+        final boolean result = checkFeatureSupport(stmtContext, isFeatureSupported);
+        stmtContext.setSupportedByFeatures(result);
+        return result;
+    }
+
+    private static boolean checkFeatureSupport(final StmtContext.Mutable<?, ?, ?> stmtContext,
+            final Predicate<QName> isFeatureSupported) {
+        final Collection<StatementContextBase<?, ?, ?>> substatements = stmtContext.declaredSubstatements();
 
         boolean isSupported = false;
         boolean containsIfFeature = false;
-        for (StatementContextBase<?, ?, ?> stmt: substatements) {
+        for (final StatementContextBase<?, ?, ?> stmt : substatements) {
             if (stmt.getPublicDefinition().equals(Rfc6020Mapping.IF_FEATURE)) {
                 containsIfFeature = true;
                 if (isFeatureSupported.test((QName) stmt.getStatementArgument())) {
index fac33f5e1da30793fdd10483de5dfbf70a489729..eee66de03e140fc9fe37062bd232566dd0c940a8 100644 (file)
@@ -24,6 +24,7 @@ import java.util.function.Predicate;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.repo.api.IfFeaturePredicates;
 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
@@ -55,7 +56,7 @@ public class CrossSourceStatementReactor {
     }
 
     public final BuildAction newBuild() {
-        return newBuild(StatementParserMode.DEFAULT_MODE, t -> true);
+        return newBuild(StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES);
     }
 
     public final BuildAction newBuild(final Predicate<QName> isFeatureSupported) {
@@ -63,7 +64,7 @@ public class CrossSourceStatementReactor {
     }
 
     public final BuildAction newBuild(final StatementParserMode statementParserMode) {
-        return new BuildAction(statementParserMode, t -> true);
+        return new BuildAction(statementParserMode, IfFeaturePredicates.ALL_FEATURES);
     }
 
     public final BuildAction newBuild(final StatementParserMode statementParserMode,
@@ -96,18 +97,18 @@ public class CrossSourceStatementReactor {
         private final BuildGlobalContext context;
 
         public BuildAction() {
-            this(StatementParserMode.DEFAULT_MODE, t -> true);
+            this(StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES);
         }
 
-        public BuildAction(StatementParserMode statementParserMode) {
-            this(statementParserMode, t -> true);
+        public BuildAction(final StatementParserMode statementParserMode) {
+            this(statementParserMode, IfFeaturePredicates.ALL_FEATURES);
         }
 
-        public BuildAction(Predicate<QName> isFeatureSupported) {
+        public BuildAction(final Predicate<QName> isFeatureSupported) {
             this(StatementParserMode.DEFAULT_MODE, isFeatureSupported);
         }
 
-        public BuildAction(StatementParserMode statementParserMode, Predicate<QName> isFeatureSupported) {
+        public BuildAction(final StatementParserMode statementParserMode, final Predicate<QName> isFeatureSupported) {
             this.context = new BuildGlobalContext(supportedTerminology, supportedValidation, statementParserMode,
                     isFeatureSupported);
         }
@@ -117,7 +118,7 @@ public class CrossSourceStatementReactor {
         }
 
         public void addSources(final StatementStreamSource... sources) {
-            for (StatementStreamSource source : sources) {
+            for (final StatementStreamSource source : sources) {
                 context.addSource(source);
             }
         }
@@ -136,7 +137,7 @@ public class CrossSourceStatementReactor {
 
         public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws ReactorException,
                 IOException {
-            for (ByteSource yangByteSource : yangByteSources) {
+            for (final ByteSource yangByteSource : yangByteSources) {
                 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
             }
 
@@ -144,7 +145,7 @@ public class CrossSourceStatementReactor {
         }
 
         public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws ReactorException {
-            for (InputStream yangInputStream : yangInputStreams) {
+            for (final InputStream yangInputStream : yangInputStreams) {
                 addSource(new YangStatementSourceImpl(yangInputStream));
             }
 
@@ -162,17 +163,17 @@ public class CrossSourceStatementReactor {
                 return Collections.emptyMap();
             }
 
-            Map<String, File> pathToFile = new HashMap<>();
-            Map<File, Module> sourceFileToModule = new HashMap<>();
+            final Map<String, File> pathToFile = new HashMap<>();
+            final Map<File, Module> sourceFileToModule = new HashMap<>();
 
-            for (File yangFile : yangFiles) {
+            for (final File yangFile : yangFiles) {
                 addSource(new YangStatementSourceImpl(new NamedFileInputStream(yangFile, yangFile.getPath())));
                 pathToFile.put(yangFile.getPath(), yangFile);
             }
 
-            EffectiveSchemaContext schema = buildEffective();
-            Set<Module> modules = schema.getModules();
-            for (Module module : modules) {
+            final EffectiveSchemaContext schema = buildEffective();
+            final Set<Module> modules = schema.getModules();
+            for (final Module module : modules) {
                 sourceFileToModule.put(pathToFile.get(module.getModuleSourcePath()), module);
             }
 
index 4fc299a573a86b26d5ad850b02c3118db1b4d5e9..045be493784c639bcffac5043deb1d6b9f0520e3 100644 (file)
@@ -17,6 +17,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Namesp
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.Registry;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 
 /**
  * root statement class for a Yang source
@@ -55,8 +56,11 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
      */
     private void copyDeclaredStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
             final TypeOfCopy typeOfCopy) {
-        Collection<? extends StmtContext<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
-        for (StmtContext<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
+        final Collection<StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original.declaredSubstatements();
+        for (final StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
+            if (!StmtContextUtils.areFeaturesSupported(stmtContext)) {
+                continue;
+            }
             this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
         }
     }
@@ -70,8 +74,8 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
      */
     private void copyEffectiveStmts(final RootStatementContext<A, D, E> original, final QNameModule newQNameModule,
             final TypeOfCopy typeOfCopy) {
-        Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
-        for (StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
+        final Collection<? extends StmtContext<?, ?, ?>> originalEffectiveSubstatements = original.effectiveSubstatements();
+        for (final StmtContext<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
             this.addEffectiveSubstatement(stmtContext.createCopy(newQNameModule, this, typeOfCopy));
         }
     }
@@ -140,7 +144,7 @@ public class RootStatementContext<A, D extends DeclaredStatement<A>, E extends E
     @Override
     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
             final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) {
-        RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
+        final RootStatementContext<A, D, E> copy = new RootStatementContext<>(this, newQNameModule, typeOfCopy);
 
         copy.addAllToCopyHistory(this.getCopyHistory());
         copy.addToCopyHistory(typeOfCopy);
index b537046eb03fd728a47c2b5fb3600dbd739ffe5d..870057e3fe55d5736680a132586f4cb324682c76 100644 (file)
@@ -102,6 +102,17 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     private final List<TypeOfCopy> copyHistory = new ArrayList<>(1);
 
     private boolean isSupportedToBuildEffective = true;
+    private SupportedByFeatures supportedByFeatures = SupportedByFeatures.UNDEFINED;
+
+    @Override
+    public SupportedByFeatures getSupportedByFeatures() {
+        return supportedByFeatures;
+    }
+
+    @Override
+    public void setSupportedByFeatures(final boolean isSupported) {
+        this.supportedByFeatures = isSupported ? SupportedByFeatures.SUPPORTED : SupportedByFeatures.NOT_SUPPORTED;
+    }
 
     @Override
     public boolean isSupportedToBuildEffective() {
@@ -251,9 +262,9 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
     }
 
     public void removeStatementFromEffectiveSubstatements(final StatementDefinition refineSubstatementDef) {
-        Iterator<StatementContextBase<?, ?, ?>> iterator = effective.iterator();
+        final Iterator<StatementContextBase<?, ?, ?>> iterator = effective.iterator();
         while (iterator.hasNext()) {
-            StatementContextBase<?, ?, ?> next = iterator.next();
+            final StatementContextBase<?, ?, ?> next = iterator.next();
             if (next.getPublicDefinition().equals(refineSubstatementDef)) {
                 iterator.remove();
             }
@@ -318,7 +329,7 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
             public StatementContextBase build() throws SourceException {
                 StatementContextBase<?, ?, ?> potential = null;
 
-                StatementDefinition stmtDef = getDefinition().getPublicView();
+                final StatementDefinition stmtDef = getDefinition().getPublicView();
                 if (stmtDef != Rfc6020Mapping.AUGMENT && stmtDef != Rfc6020Mapping.DEVIATION
                         && stmtDef != Rfc6020Mapping.TYPE) {
                     potential = substatements.get(createIdentifier());
@@ -399,20 +410,20 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
      *             when an error occured in source parsing
      */
     boolean tryToCompletePhase(final ModelProcessingPhase phase) {
-        Iterator<ContextMutation> openMutations = phaseMutation.get(phase).iterator();
+        final Iterator<ContextMutation> openMutations = phaseMutation.get(phase).iterator();
         boolean finished = true;
         while (openMutations.hasNext()) {
-            ContextMutation current = openMutations.next();
+            final ContextMutation current = openMutations.next();
             if (current.isFinished()) {
                 openMutations.remove();
             } else {
                 finished = false;
             }
         }
-        for (StatementContextBase<?, ?, ?> child : declared) {
+        for (final StatementContextBase<?, ?, ?> child : declared) {
             finished &= child.tryToCompletePhase(phase);
         }
-        for (StatementContextBase<?, ?, ?> child : effective) {
+        for (final StatementContextBase<?, ?, ?> child : effective) {
             finished &= child.tryToCompletePhase(phase);
         }
 
@@ -433,9 +444,9 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
      */
     private void onPhaseCompleted(final ModelProcessingPhase phase) {
         completedPhase = phase;
-        Iterator<OnPhaseFinished> listener = phaseListeners.get(completedPhase).iterator();
+        final Iterator<OnPhaseFinished> listener = phaseListeners.get(completedPhase).iterator();
         while (listener.hasNext()) {
-            OnPhaseFinished next = listener.next();
+            final OnPhaseFinished next = listener.next();
             if (next.phaseFinished(this, phase)) {
                 listener.remove();
             }
@@ -476,20 +487,20 @@ public abstract class StatementContextBase<A, D extends DeclaredStatement<A>, E
 
     <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceItemAddedAction(final Class<N> type, final K key,
             final OnNamespaceItemAdded listener) throws SourceException {
-        Object potential = getFromNamespace(type, key);
+        final Object potential = getFromNamespace(type, key);
         if (potential != null) {
             listener.namespaceItemAdded(this, type, key, potential);
             return;
         }
-        NamespaceBehaviour<K, V, N> behaviour = getBehaviourRegistry().getNamespaceBehaviour(type);
+        final NamespaceBehaviour<K, V, N> behaviour = getBehaviourRegistry().getNamespaceBehaviour(type);
         if (behaviour instanceof NamespaceBehaviourWithListeners) {
-            NamespaceBehaviourWithListeners<K, V, N> casted = (NamespaceBehaviourWithListeners<K, V, N>) behaviour;
+            final NamespaceBehaviourWithListeners<K, V, N> casted = (NamespaceBehaviourWithListeners<K, V, N>) behaviour;
             casted.addValueListener(new ValueAddedListener<K>(this, key) {
                 @Override
                 void onValueAdded(final Object key, final Object value) {
                     try {
                         listener.namespaceItemAdded(StatementContextBase.this, type, key, value);
-                    } catch (SourceException e) {
+                    } catch (final SourceException e) {
                         throw Throwables.propagate(e);
                     }
                 }
index a6553fc893683c9793a8e5ab008b79ca7d9df6fe..c8129a5e5eb646c51e28cdadde74ad77d815cf13 100644 (file)
@@ -36,8 +36,8 @@ import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNa
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.GroupingUtils;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
 
-final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
-        extends StatementContextBase<A, D, E> {
+final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> extends
+        StatementContextBase<A, D, E> {
 
     private final StatementContextBase<?, ?, ?> parent;
     private final A argument;
@@ -50,22 +50,19 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
     }
 
     @SuppressWarnings("unchecked")
-    SubstatementContext(final SubstatementContext<A, D, E> original,
-            final QNameModule newQNameModule,
+    SubstatementContext(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
             final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) {
         super(original);
         this.parent = newParent;
 
         if (newQNameModule != null) {
             if (original.argument instanceof QName) {
-                QName originalQName = (QName) original.argument;
-                this.argument = (A)
-                        getFromNamespace(QNameCacheNamespace.class,
-                            QName.create(newQNameModule, originalQName.getLocalName()));
+                final QName originalQName = (QName) original.argument;
+                this.argument = (A) getFromNamespace(QNameCacheNamespace.class,
+                        QName.create(newQNameModule, originalQName.getLocalName()));
             } else if (StmtContextUtils.producesDeclared(original, KeyStatement.class)) {
                 this.argument = (A) StmtContextUtils.replaceModuleQNameForKey(
-                                (StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?>) original,
-                                newQNameModule);
+                        (StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?>) original, newQNameModule);
             } else {
                 this.argument = original.argument;
             }
@@ -74,15 +71,16 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         }
     }
 
-
-    private void copyDeclaredStmts(final SubstatementContext<A, D, E> original,
-            final QNameModule newQNameModule, final TypeOfCopy typeOfCopy) {
-        Collection<? extends StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original
+    private void copyDeclaredStmts(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
+            final TypeOfCopy typeOfCopy) {
+        final Collection<? extends StatementContextBase<?, ?, ?>> originalDeclaredSubstatements = original
                 .declaredSubstatements();
-        for (StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
+        for (final StatementContextBase<?, ?, ?> stmtContext : originalDeclaredSubstatements) {
+            if (!StmtContextUtils.areFeaturesSupported(stmtContext)) {
+                continue;
+            }
             if (GroupingUtils.needToCopyByUses(stmtContext)) {
-                StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(
-                        newQNameModule, this, typeOfCopy);
+                final StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
                 this.addEffectiveSubstatement(copy);
             } else if (GroupingUtils.isReusedByUses(stmtContext)) {
                 this.addEffectiveSubstatement(stmtContext);
@@ -90,14 +88,13 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         }
     }
 
-    private void copyEffectiveStmts(final SubstatementContext<A, D, E> original,
-            final QNameModule newQNameModule, final TypeOfCopy typeOfCopy) {
-        Collection<? extends StatementContextBase<?, ?, ?>> originalEffectiveSubstatements = original
+    private void copyEffectiveStmts(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
+            final TypeOfCopy typeOfCopy) {
+        final Collection<? extends StatementContextBase<?, ?, ?>> originalEffectiveSubstatements = original
                 .effectiveSubstatements();
-        for (StatementContextBase<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
+        for (final StatementContextBase<?, ?, ?> stmtContext : originalEffectiveSubstatements) {
             if (GroupingUtils.needToCopyByUses(stmtContext)) {
-                StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(
-                        newQNameModule, this, typeOfCopy);
+                final StatementContextBase<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
                 this.addEffectiveSubstatement(copy);
             } else if (GroupingUtils.isReusedByUses(stmtContext)) {
                 this.addEffectiveSubstatement(stmtContext);
@@ -131,15 +128,15 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
     }
 
     @Override
-    public StatementContextBase<?, ?, ?> createCopy(
-            final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) {
+    public StatementContextBase<?, ?, ?> createCopy(final StatementContextBase<?, ?, ?> newParent,
+            final TypeOfCopy typeOfCopy) {
         return createCopy(null, newParent, typeOfCopy);
     }
 
     @Override
     public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
             final StatementContextBase<?, ?, ?> newParent, final TypeOfCopy typeOfCopy) {
-        SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newQNameModule, newParent, typeOfCopy);
+        final SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newQNameModule, newParent, typeOfCopy);
 
         copy.addAllToCopyHistory(this.getCopyHistory());
         copy.addToCopyHistory(typeOfCopy);
@@ -169,7 +166,7 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         final SchemaPath parentPath = maybeParentPath.get();
 
         if (argument instanceof QName) {
-            QName qname = (QName) argument;
+            final QName qname = (QName) argument;
             if (StmtContextUtils.producesDeclared(this, UsesStatement.class)) {
                 return maybeParentPath.orNull();
             }
@@ -187,13 +184,13 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         if (argument instanceof String) {
             // FIXME: This may yield illegal argument exceptions
             final StatementContextBase<?, ?, ?> originalCtx = getOriginalCtx();
-            final QName qname = (originalCtx != null) ? Utils.qNameFromArgument(originalCtx, (String) argument)
-                    : Utils.qNameFromArgument(this, (String) argument);
+            final QName qname = (originalCtx != null) ? Utils.qNameFromArgument(originalCtx, (String) argument) : Utils
+                    .qNameFromArgument(this, (String) argument);
             return parentPath.createChild(qname);
         }
-        if (argument instanceof SchemaNodeIdentifier &&
-                (StmtContextUtils.producesDeclared(this, AugmentStatement.class)
-                   || StmtContextUtils.producesDeclared(this, RefineStatement.class))) {
+        if (argument instanceof SchemaNodeIdentifier
+                && (StmtContextUtils.producesDeclared(this, AugmentStatement.class) || StmtContextUtils
+                        .producesDeclared(this, RefineStatement.class))) {
 
             return parentPath.createChild(((SchemaNodeIdentifier) argument).getPathFromRoot());
         }
@@ -229,8 +226,8 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
 
     @Override
     public boolean isConfiguration() {
-        StmtContext<Boolean, ?, ?> configStatement = StmtContextUtils
-                .findFirstSubstatement(this, ConfigStatement.class);
+        final StmtContext<Boolean, ?, ?> configStatement = StmtContextUtils.findFirstSubstatement(this,
+                ConfigStatement.class);
 
         /*
          * If "config" statement is not specified, the default is the same as
@@ -241,8 +238,8 @@ final class SubstatementContext<A, D extends DeclaredStatement<A>, E extends Eff
         }
 
         /*
-         * If a parent node has "config" set to "true", the node underneath it can
-         * have "config" set to "true" or "false".
+         * If a parent node has "config" set to "true", the node underneath it
+         * can have "config" set to "true" or "false".
          */
         if (parent.isConfiguration()) {
             return configStatement.getStatementArgument();
index c171a7a1f308bd277f7bf4f264b3cebd5e8e9979..2ae751d4aad45127afb44068ba2f78eb20fa1170 100644 (file)
@@ -23,6 +23,7 @@ import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
 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.TypeOfCopy;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
@@ -43,14 +44,18 @@ public final class AugmentUtils {
     private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
 
-        final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
-                .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
+        final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
+                .getDeclaredRepresentationClass().equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION
+                : TypeOfCopy.ADDED_BY_AUGMENTATION;
 
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
+        for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
+            if (!StmtContextUtils.areFeaturesSupported(originalStmtCtx)) {
+                continue;
+            }
             if (needToCopyByAugment(originalStmtCtx)) {
                 validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx, typeOfCopy);
 
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
+                final StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
                 targetCtx.addEffectiveSubstatement(copy);
             } else if (isReusedByAugment(originalStmtCtx)) {
                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
@@ -60,14 +65,15 @@ public final class AugmentUtils {
 
     private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
-        final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
-                .equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
+        final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
+                .getDeclaredRepresentationClass().equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION
+                : TypeOfCopy.ADDED_BY_AUGMENTATION;
 
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
+        for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
             if (needToCopyByAugment(originalStmtCtx)) {
                 validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx, typeOfCopy);
 
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
+                final StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
                 targetCtx.addEffectiveSubstatement(copy);
             } else if (isReusedByAugment(originalStmtCtx)) {
                 targetCtx.addEffectiveSubstatement(originalStmtCtx);
@@ -103,31 +109,31 @@ public final class AugmentUtils {
                     .getPublicDefinition().getDeclaredRepresentationClass());
             final boolean targetIsDataNode = DataDefinitionStatement.class.isAssignableFrom(subStatement
                     .getPublicDefinition().getDeclaredRepresentationClass());
-            boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
+            final boolean qNamesEqual = sourceIsDataNode && targetIsDataNode
                     && Objects.equals(sourceCtx.getStatementArgument(), subStatement.getStatementArgument());
 
             InferenceException.throwIf(qNamesEqual, sourceCtx.getStatementSourceReference(),
-                "An augment cannot add node named '%s' because this name is already used in target",
-                sourceCtx.rawStatementArgument());
+                    "An augment cannot add node named '%s' because this name is already used in target",
+                    sourceCtx.rawStatementArgument());
         }
     }
 
     private static boolean reguiredCheckOfMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx,
             StatementContextBase<?, ?, ?> targetCtx) {
         /*
-         * If the statement argument is not QName, it cannot be mandatory statement,
-         * therefore return false and skip mandatory nodes validation
+         * If the statement argument is not QName, it cannot be mandatory
+         * statement, therefore return false and skip mandatory nodes validation
          */
         if (!(sourceCtx.getStatementArgument() instanceof QName)) {
             return false;
         }
-        QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
+        final QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
 
-        RootStatementContext<?, ?, ?> root = targetCtx.getRoot();
+        final RootStatementContext<?, ?, ?> root = targetCtx.getRoot();
         do {
             Verify.verify(targetCtx.getStatementArgument() instanceof QName,
                     "Argument of augment target statement must be QName.");
-            QName targetStmtQName = (QName) targetCtx.getStatementArgument();
+            final QName targetStmtQName = (QName) targetCtx.getStatementArgument();
             /*
              * If target is from another module, return true and perform
              * mandatory nodes validation
@@ -169,13 +175,15 @@ public final class AugmentUtils {
     static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
 
         /*
-         * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must not be for example
-         * grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
-         * example Grouping and Container are siblings and they have the same QName. We must find the Container and the
-         * Grouping must be ignored as disallowed augment target.
+         * :TODO Substatement must be allowed augment target type e.g.
+         * Container, etc... and must not be for example grouping, identity etc.
+         * It is problem in case when more than one substatements have the same
+         * QName, for example Grouping and Container are siblings and they have
+         * the same QName. We must find the Container and the Grouping must be
+         * ignored as disallowed augment target.
          */
 
-        Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
+        final Collection<?> allowedAugmentTargets = substatementCtx.getFromNamespace(ValidationBundlesNamespace.class,
                 ValidationBundleType.SUPPORTED_AUGMENT_TARGETS);
 
         // if no allowed target is returned we consider all targets allowed
index acea0fe085d2ec1c8e434f8d436b3f90ffafae29..b16054ee05976a03bd0779f04527afb860d9f178 100644 (file)
@@ -40,30 +40,35 @@ public final class GroupingUtils {
     }
 
     /**
-     * @param sourceGrpStmtCtx source grouping statement context
-     * @param targetCtx target context
-     * @param usesNode uses node
-     * @throws SourceException instance of SourceException
+     * @param sourceGrpStmtCtx
+     *            source grouping statement context
+     * @param targetCtx
+     *            target context
+     * @param usesNode
+     *            uses node
+     * @throws SourceException
+     *             instance of SourceException
      */
     public static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
             final StatementContextBase<?, ?, ?> targetCtx,
             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode) {
 
-        QNameModule newQNameModule = getNewQNameModule(targetCtx,
-                sourceGrpStmtCtx);
+        final QNameModule newQNameModule = getNewQNameModule(targetCtx, sourceGrpStmtCtx);
         copyDeclaredStmts(sourceGrpStmtCtx, targetCtx, usesNode, newQNameModule);
-        copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, usesNode,
-                newQNameModule);
+        copyEffectiveStmts(sourceGrpStmtCtx, targetCtx, usesNode, newQNameModule);
     }
 
     public static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceGrpStmtCtx,
             final StatementContextBase<?, ?, ?> targetCtx,
             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
             final QNameModule newQNameModule) {
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.declaredSubstatements()) {
+        for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.declaredSubstatements()) {
+            if (!StmtContextUtils.areFeaturesSupported(originalStmtCtx)) {
+                continue;
+            }
             if (needToCopyByUses(originalStmtCtx)) {
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
-                    TypeOfCopy.ADDED_BY_USES);
+                final StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
+                        TypeOfCopy.ADDED_BY_USES);
                 targetCtx.addEffectiveSubstatement(copy);
                 usesNode.addAsEffectOfStatement(copy);
             } else if (isReusedByUsesOnTop(originalStmtCtx)) {
@@ -77,10 +82,10 @@ public final class GroupingUtils {
             final StatementContextBase<?, ?, ?> targetCtx,
             final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
             final QNameModule newQNameModule) {
-        for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.effectiveSubstatements()) {
+        for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceGrpStmtCtx.effectiveSubstatements()) {
             if (needToCopyByUses(originalStmtCtx)) {
-                StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
-                    TypeOfCopy.ADDED_BY_USES);
+                final StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(newQNameModule, targetCtx,
+                        TypeOfCopy.ADDED_BY_USES);
                 targetCtx.addEffectiveSubstatement(copy);
                 usesNode.addAsEffectOfStatement(copy);
             } else if (isReusedByUsesOnTop(originalStmtCtx)) {
@@ -100,8 +105,8 @@ public final class GroupingUtils {
                 return targetCtx.getFromNamespace(ModuleCtxToModuleQName.class, targetCtx.getRoot());
             }
 
-            Object targetStmtArgument = targetCtx.getStatementArgument();
-            Object sourceStmtArgument = stmtContext.getStatementArgument();
+            final Object targetStmtArgument = targetCtx.getStatementArgument();
+            final Object sourceStmtArgument = stmtContext.getStatementArgument();
             if (targetStmtArgument instanceof QName && sourceStmtArgument instanceof QName) {
                 return ((QName) targetStmtArgument).getModule();
             } else {
@@ -128,8 +133,8 @@ public final class GroupingUtils {
     public static boolean needToCopyByUses(final StmtContext<?, ?, ?> stmtContext) {
         final StatementDefinition def = stmtContext.getPublicDefinition();
 
-        return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def)
-                && Rfc6020Mapping.GROUPING.equals(stmtContext.getParentContext().getPublicDefinition())));
+        return !(NOCOPY_DEF_SET.contains(def) || (NOCOPY_FROM_GROUPING_SET.contains(def) && Rfc6020Mapping.GROUPING
+                .equals(stmtContext.getParentContext().getPublicDefinition())));
     }
 
     public static boolean isReusedByUses(final StmtContext<?, ?, ?> stmtContext) {
@@ -143,7 +148,7 @@ public final class GroupingUtils {
     public static void resolveUsesNode(
             final Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode,
             final StatementContextBase<?, ?, ?> targetNodeStmtCtx) {
-        for (StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
+        for (final StatementContextBase<?, ?, ?> subStmtCtx : usesNode.declaredSubstatements()) {
             if (StmtContextUtils.producesDeclared(subStmtCtx, RefineStatement.class)) {
                 performRefine(subStmtCtx, targetNodeStmtCtx);
             }
@@ -153,19 +158,21 @@ public final class GroupingUtils {
     private static void performRefine(final StatementContextBase<?, ?, ?> refineCtx,
             final StatementContextBase<?, ?, ?> usesParentCtx) {
 
-        Object refineArgument = refineCtx.getStatementArgument();
+        final Object refineArgument = refineCtx.getStatementArgument();
         Preconditions.checkArgument(refineArgument instanceof SchemaNodeIdentifier,
-            "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
+                "Invalid refine argument %s. It must be instance of SchemaNodeIdentifier. At %s", refineArgument,
                 refineCtx.getStatementSourceReference());
 
-        SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
-        StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx, refineTargetNodeIdentifier);
+        final SchemaNodeIdentifier refineTargetNodeIdentifier = (SchemaNodeIdentifier) refineArgument;
+        final StatementContextBase<?, ?, ?> refineTargetNodeCtx = Utils.findNode(usesParentCtx,
+                refineTargetNodeIdentifier);
         Preconditions.checkArgument(refineTargetNodeCtx != null, "Refine target node %s not found. At %s",
                 refineTargetNodeIdentifier, refineCtx.getStatementSourceReference());
         if (StmtContextUtils.isUnknownStatement(refineTargetNodeCtx)) {
-            LOG.debug("Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
-                    refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(), refineTargetNodeCtx.getStatementArgument(),
-                    refineCtx.getStatementSourceReference());
+            LOG.debug(
+                    "Refine node '{}' in uses '{}' has target node unknown statement '{}'. Refine has been skipped. At line: {}",
+                    refineCtx.getStatementArgument(), refineCtx.getParentContext().getStatementArgument(),
+                    refineTargetNodeCtx.getStatementArgument(), refineCtx.getStatementSourceReference());
             refineCtx.addAsEffectOfStatement(refineTargetNodeCtx);
             return;
         }
@@ -176,7 +183,7 @@ public final class GroupingUtils {
 
     private static void addOrReplaceNodes(final StatementContextBase<?, ?, ?> refineCtx,
             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
-        for (StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
+        for (final StatementContextBase<?, ?, ?> refineSubstatementCtx : refineCtx.declaredSubstatements()) {
             if (isSupportedRefineSubstatement(refineSubstatementCtx)) {
                 addOrReplaceNode(refineSubstatementCtx, refineTargetNodeCtx);
             }
@@ -186,14 +193,14 @@ public final class GroupingUtils {
     private static void addOrReplaceNode(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
 
-        StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
+        final StatementDefinition refineSubstatementDef = refineSubstatementCtx.getPublicDefinition();
 
         SourceException.throwIf(!isSupportedRefineTarget(refineSubstatementCtx, refineTargetNodeCtx),
-            refineSubstatementCtx.getStatementSourceReference(),
-            "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
-                    refineSubstatementCtx.getRoot().getStatementArgument(),
-                    refineSubstatementCtx.getParentContext().getStatementArgument(),
-                    refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx.getPublicDefinition());
+                refineSubstatementCtx.getStatementSourceReference(),
+                "Error in module '%s' in the refine of uses '%s': can not perform refine of '%s' for the target '%s'.",
+                refineSubstatementCtx.getRoot().getStatementArgument(), refineSubstatementCtx.getParentContext()
+                        .getStatementArgument(), refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx
+                        .getPublicDefinition());
 
         if (isAllowedToAddByRefine(refineSubstatementDef)) {
             refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx);
@@ -210,8 +217,8 @@ public final class GroupingUtils {
     }
 
     private static boolean isSupportedRefineSubstatement(final StatementContextBase<?, ?, ?> refineSubstatementCtx) {
-        Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
-            ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
+        final Collection<?> supportedRefineSubstatements = refineSubstatementCtx.getFromNamespace(
+                ValidationBundlesNamespace.class, ValidationBundleType.SUPPORTED_REFINE_SUBSTATEMENTS);
 
         return supportedRefineSubstatements == null || supportedRefineSubstatements.isEmpty()
                 || supportedRefineSubstatements.contains(refineSubstatementCtx.getPublicDefinition())
@@ -221,8 +228,8 @@ public final class GroupingUtils {
     private static boolean isSupportedRefineTarget(final StatementContextBase<?, ?, ?> refineSubstatementCtx,
             final StatementContextBase<?, ?, ?> refineTargetNodeCtx) {
 
-        Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS.get(
-            refineSubstatementCtx.getPublicDefinition());
+        final Collection<?> supportedRefineTargets = YangValidationBundles.SUPPORTED_REFINE_TARGETS
+                .get(refineSubstatementCtx.getPublicDefinition());
 
         return supportedRefineTargets == null || supportedRefineTargets.isEmpty()
                 || supportedRefineTargets.contains(refineTargetNodeCtx.getPublicDefinition());
index 7b35dad07ce68c6bcb99d9619998388c8f4182e2..c841a54d2b48faba467763d145000f38b9d3e7a8 100644 (file)
@@ -29,30 +29,27 @@ import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 
 public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>> implements EffectiveStatement<A, D> {
 
-    private static final Predicate<StmtContext<?, ?,?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE =
-            new Predicate<StmtContext<?,?,?>>() {
+    private static final Predicate<StmtContext<?, ?, ?>> IS_SUPPORTED_TO_BUILD_EFFECTIVE = new Predicate<StmtContext<?, ?, ?>>() {
         @Override
         public boolean apply(final StmtContext<?, ?, ?> input) {
             return input.isSupportedToBuildEffective();
         }
     };
 
-    private static final Predicate<StmtContext<?, ?,?>> IS_UNKNOWN_STATEMENT_CONTEXT =
-            new Predicate<StmtContext<?,?,?>>() {
+    private static final Predicate<StmtContext<?, ?, ?>> IS_UNKNOWN_STATEMENT_CONTEXT = new Predicate<StmtContext<?, ?, ?>>() {
         @Override
         public boolean apply(final StmtContext<?, ?, ?> input) {
             return StmtContextUtils.isUnknownStatement(input);
         }
     };
 
-    private static final Predicate<StmtContext<?, ?, ?>> ARE_FEATURES_SUPPORTED =
-            new Predicate<StmtContext<?, ?, ?>>() {
+    private static final Predicate<StatementContextBase<?, ?, ?>> ARE_FEATURES_SUPPORTED = new Predicate<StatementContextBase<?, ?, ?>>() {
 
-                @Override
-                public boolean apply(StmtContext<?, ?, ?> input) {
-                    return StmtContextUtils.areFeaturesSupported(input);
-                }
-            };
+        @Override
+        public boolean apply(final StatementContextBase<?, ?, ?> input) {
+            return StmtContextUtils.areFeaturesSupported(input);
+        }
+    };
 
     private final List<? extends EffectiveStatement<?, ?>> substatements;
     private final List<StatementContextBase<?, ?, ?>> unknownSubstatementsToBuild;
@@ -73,12 +70,14 @@ public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
      *            method. The main purpose of this is to allow the build of
      *            recursive extension definitions.
      */
-    protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, boolean buildUnknownSubstatements) {
+    protected EffectiveStatementBase(final StmtContext<A, D, ?> ctx, final boolean buildUnknownSubstatements) {
 
         final Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements = ctx.effectiveSubstatements();
         final Collection<StatementContextBase<?, ?, ?>> substatementsInit = new ArrayList<>();
 
-        for (StatementContextBase<?, ?, ?> declaredSubstatement : ctx.declaredSubstatements()) {
+        final Collection<StatementContextBase<?, ?, ?>> supportedDeclaredSubStmts = Collections2.filter(
+                ctx.declaredSubstatements(), ARE_FEATURES_SUPPORTED);
+        for (final StatementContextBase<?, ?, ?> declaredSubstatement : supportedDeclaredSubStmts) {
             if (declaredSubstatement.getPublicDefinition().equals(Rfc6020Mapping.USES)) {
                 substatementsInit.add(declaredSubstatement);
                 substatementsInit.addAll(declaredSubstatement.getEffectOfStatement());
@@ -101,14 +100,14 @@ public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
             this.unknownSubstatementsToBuild = ImmutableList.of();
         }
 
-        substatementsToBuild = Collections2.filter(substatementsToBuild, ARE_FEATURES_SUPPORTED);
-
-        Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
+        final Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils
+                .buildEffective();
         this.substatements = ImmutableList.copyOf(Collections2.transform(substatementsToBuild, buildEffective));
     }
 
     Collection<EffectiveStatement<?, ?>> getOmittedUnknownSubstatements() {
-        Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils.buildEffective();
+        final Function<StmtContext<?, ?, ? extends EffectiveStatement<?, ?>>, EffectiveStatement<?, ?>> buildEffective = StmtContextUtils
+                .buildEffective();
         return Collections2.transform(unknownSubstatementsToBuild, buildEffective);
     }
 
@@ -132,13 +131,13 @@ public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
     }
 
     protected final <S extends EffectiveStatement<?, ?>> S firstEffective(final Class<S> type) {
-        Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
+        final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
                 Predicates.instanceOf(type));
         return possible.isPresent() ? type.cast(possible.get()) : null;
     }
 
     protected final <S extends SchemaNode> S firstSchemaNode(final Class<S> type) {
-        Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
+        final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
                 Predicates.instanceOf(type));
         return possible.isPresent() ? type.cast(possible.get()) : null;
     }
@@ -149,13 +148,13 @@ public abstract class EffectiveStatementBase<A, D extends DeclaredStatement<A>>
     }
 
     protected final <T> T firstSubstatementOfType(final Class<T> type) {
-        Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
+        final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
                 Predicates.instanceOf(type));
         return possible.isPresent() ? type.cast(possible.get()) : null;
     }
 
     protected final <R> R firstSubstatementOfType(final Class<?> type, final Class<R> returnType) {
-        Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
+        final Optional<? extends EffectiveStatement<?, ?>> possible = Iterables.tryFind(substatements,
                 Predicates.and(Predicates.instanceOf(type), Predicates.instanceOf(returnType)));
         return possible.isPresent() ? returnType.cast(possible.get()) : null;
     }