Use simple index for feature/extension/identity 16/94116/3
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 8 Dec 2020 15:28:01 +0000 (16:28 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 8 Dec 2020 15:54:51 +0000 (16:54 +0100)
Modules expose these three contructs in their user-facing namespaces.

While the same is available through inference's view of things, and
it is tempting to reuse those, this is a DTO contract over provided
substatements, hence use a simple filter to construct our own view
of the contents.

This reduces the dependency on NamespaceStmtCtx, making it easier
to separate the logic the DTO logic and the inference logic.

Change-Id: I544dd0d2b7f3cf07ea71f1075571056a0c62682d
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/module/ModuleEffectiveStatementImpl.java

index 6c4e8586c4b17783e47d5f707913a52b2a7596d0..b14eed83ce3cfa6b503d98da2d8e01e6e968a7e1 100644 (file)
@@ -17,6 +17,7 @@ import java.util.Collection;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Optional;
+import java.util.function.Function;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
@@ -27,21 +28,15 @@ import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionEffectiveStatementNamespace;
-import org.opendaylight.yangtools.yang.model.api.stmt.ExtensionStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureEffectiveStatementNamespace;
-import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.IdentityEffectiveStatementNamespace;
-import org.opendaylight.yangtools.yang.model.api.stmt.IdentityStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.PrefixEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveModule;
-import org.opendaylight.yangtools.yang.parser.spi.ExtensionNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.FeatureNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.IdentityNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx;
@@ -85,27 +80,27 @@ final class ModuleEffectiveStatementImpl extends AbstractEffectiveModule<ModuleS
                 : ImmutableMap.copyOf(Maps.transformValues(includedSubmodules,
                     submodule -> (SubmoduleEffectiveStatement) submodule.buildEffective()));
 
-        final Map<QName, StmtContext<?, ExtensionStatement, ExtensionEffectiveStatement>> extensions =
-                stmt.localNamespacePortion(ExtensionNamespace.class);
-        qnameToExtension = extensions == null ? ImmutableMap.of()
-                : ImmutableMap.copyOf(Maps.transformValues(extensions, StmtContext::buildEffective));
-        final Map<QName, StmtContext<?, FeatureStatement, FeatureEffectiveStatement>> features =
-                stmt.localNamespacePortion(FeatureNamespace.class);
-        qnameToFeature = features == null ? ImmutableMap.of()
-                : ImmutableMap.copyOf(Maps.transformValues(features, StmtContext::buildEffective));
-        final Map<QName, StmtContext<?, IdentityStatement, IdentityEffectiveStatement>> identities =
-                stmt.localNamespacePortion(IdentityNamespace.class);
-        qnameToIdentity = identities == null ? ImmutableMap.of()
-                : ImmutableMap.copyOf(Maps.transformValues(identities, StmtContext::buildEffective));
+        qnameToExtension = substatements.stream()
+            .filter(ExtensionEffectiveStatement.class::isInstance)
+            .map(ExtensionEffectiveStatement.class::cast)
+            .collect(ImmutableMap.toImmutableMap(ExtensionEffectiveStatement::argument, Function.identity()));
+        qnameToFeature = substatements.stream()
+            .filter(FeatureEffectiveStatement.class::isInstance)
+            .map(FeatureEffectiveStatement.class::cast)
+            .collect(ImmutableMap.toImmutableMap(FeatureEffectiveStatement::argument, Function.identity()));
+        qnameToIdentity = substatements.stream()
+            .filter(IdentityEffectiveStatement.class::isInstance)
+            .map(IdentityEffectiveStatement.class::cast)
+            .collect(ImmutableMap.toImmutableMap(IdentityEffectiveStatement::argument, Function.identity()));
     }
 
     @Override
-    public @NonNull QNameModule localQNameModule() {
+    public QNameModule localQNameModule() {
         return qnameModule;
     }
 
     @Override
-    public @NonNull QNameModule getQNameModule() {
+    public QNameModule getQNameModule() {
         return qnameModule;
     }