Graduate ChildSchemaNodeNamespace as SchemaTreeNamespace
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / deviate / AbstractDeviateStatementSupport.java
index ce3630580dfca3b33b41671e91663876fab21f42..0ce5325f848f971f04407e14e38b9785717a435a 100644 (file)
@@ -7,25 +7,30 @@
  */
 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.deviate;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableMap.Builder;
 import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Maps;
 import com.google.common.collect.SetMultimap;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
+import java.util.Arrays;
 import java.util.Collection;
-import java.util.Map;
 import java.util.Objects;
 import java.util.Set;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.YangVersion;
 import org.opendaylight.yangtools.yang.model.api.DeviateKind;
 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
+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.meta.StatementDefinition;
+import org.opendaylight.yangtools.yang.model.api.stmt.DeviateEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.DeviateStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
-import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.SchemaNodeIdentifierBuildNamespace;
 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.YangValidationBundles;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
+import org.opendaylight.yangtools.yang.parser.spi.SchemaTreeNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
@@ -45,8 +50,8 @@ import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<DeviateKind, DeviateStatement,
-        EffectiveStatement<DeviateKind, DeviateStatement>> {
+abstract class AbstractDeviateStatementSupport
+        extends BaseStatementSupport<DeviateKind, DeviateStatement, DeviateEffectiveStatement> {
     private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviateStatementSupport.class);
 
     private static final SubstatementValidator DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR =
@@ -83,21 +88,14 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<
                 .addOptional(YangStmtMapping.UNITS)
                 .build();
 
-    private static final Map<String, DeviateKind> KEYWORD_TO_DEVIATE_MAP;
+    private static final ImmutableMap<String, DeviateKind> KEYWORD_TO_DEVIATE_MAP =
+            Maps.uniqueIndex(Arrays.asList(DeviateKind.values()), DeviateKind::getKeyword);
 
-    static {
-        final Builder<String, DeviateKind> keywordToDeviateMapBuilder = ImmutableMap.builder();
-        for (final DeviateKind deviate : DeviateKind.values()) {
-            keywordToDeviateMapBuilder.put(deviate.getKeyword(), deviate);
-        }
-        KEYWORD_TO_DEVIATE_MAP = keywordToDeviateMapBuilder.build();
-    }
-
-    private static final Set<YangStmtMapping> SINGLETON_STATEMENTS = ImmutableSet.of(
+    private static final ImmutableSet<YangStmtMapping> SINGLETON_STATEMENTS = ImmutableSet.of(
             YangStmtMapping.UNITS, YangStmtMapping.CONFIG, YangStmtMapping.MANDATORY,
             YangStmtMapping.MIN_ELEMENTS, YangStmtMapping.MAX_ELEMENTS);
 
-    private static final Set<YangStmtMapping> IMPLICIT_STATEMENTS = ImmutableSet.of(YangStmtMapping.CONFIG,
+    private static final ImmutableSet<YangStmtMapping> IMPLICIT_STATEMENTS = ImmutableSet.of(YangStmtMapping.CONFIG,
             YangStmtMapping.MANDATORY, YangStmtMapping.MAX_ELEMENTS, YangStmtMapping.MIN_ELEMENTS);
 
     AbstractDeviateStatementSupport() {
@@ -111,25 +109,13 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<
     }
 
     @Override
-    public final DeviateStatement createDeclared(final StmtContext<DeviateKind, DeviateStatement, ?> ctx) {
-        return new DeviateStatementImpl(ctx);
-    }
-
-    @Override
-    public final EffectiveStatement<DeviateKind, DeviateStatement> createEffective(
-            final StmtContext<DeviateKind, DeviateStatement, EffectiveStatement<DeviateKind,
-                    DeviateStatement>> ctx) {
-        return new DeviateEffectiveStatementImpl(ctx);
-    }
-
-    @Override
-    public final void onFullDefinitionDeclared(final Mutable<DeviateKind, DeviateStatement,
-            EffectiveStatement<DeviateKind, DeviateStatement>> deviateStmtCtx) {
+    public final void onFullDefinitionDeclared(
+            final Mutable<DeviateKind, DeviateStatement, DeviateEffectiveStatement> deviateStmtCtx) {
         final DeviateKind deviateKind = deviateStmtCtx.getStatementArgument();
         getSubstatementValidatorForDeviate(deviateKind).validate(deviateStmtCtx);
 
         final SchemaNodeIdentifier deviationTarget =
-                (SchemaNodeIdentifier) deviateStmtCtx.getParentContext().getStatementArgument();
+                (SchemaNodeIdentifier) deviateStmtCtx.coerceParentContext().getStatementArgument();
 
         if (!isDeviationSupported(deviateStmtCtx, deviationTarget)) {
             return;
@@ -138,17 +124,17 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<
         final ModelActionBuilder deviateAction = deviateStmtCtx.newInferenceAction(
                 ModelProcessingPhase.EFFECTIVE_MODEL);
 
-        final Prerequisite<StmtContext<DeviateKind, DeviateStatement, EffectiveStatement<DeviateKind,
-                DeviateStatement>>> sourceCtxPrerequisite =
+        final Prerequisite<StmtContext<DeviateKind, DeviateStatement,
+            DeviateEffectiveStatement>> sourceCtxPrerequisite =
                 deviateAction.requiresCtx(deviateStmtCtx, ModelProcessingPhase.EFFECTIVE_MODEL);
 
         final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> targetCtxPrerequisite =
-                deviateAction.mutatesEffectiveCtx(deviateStmtCtx.getRoot(),
-                    SchemaNodeIdentifierBuildNamespace.class,  deviationTarget);
+                deviateAction.mutatesEffectiveCtxPath(deviateStmtCtx.getRoot(),
+                    SchemaTreeNamespace.class, deviationTarget.getNodeIdentifiers());
 
         deviateAction.apply(new InferenceAction() {
             @Override
-            public void apply(final InferenceContext ctx) throws InferenceException {
+            public void apply(final InferenceContext ctx) {
                 // FIXME once BUG-7760 gets fixed, there will be no need for these dirty casts
                 final StatementContextBase<?, ?, ?> sourceNodeStmtCtx =
                         (StatementContextBase<?, ?, ?>) sourceCtxPrerequisite.resolve(ctx);
@@ -175,17 +161,59 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<
 
             @Override
             public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
-                throw new InferenceException(deviateStmtCtx.getParentContext().getStatementSourceReference(),
+                throw new InferenceException(deviateStmtCtx.coerceParentContext().getStatementSourceReference(),
                     "Deviation target '%s' not found.", deviationTarget);
             }
         });
     }
 
+    @Override
+    public String internArgument(final String rawArgument) {
+        if ("add".equals(rawArgument)) {
+            return "add";
+        } else if ("delete".equals(rawArgument)) {
+            return "delete";
+        } else if ("replace".equals(rawArgument)) {
+            return "replace";
+        } else if ("not-supported".equals(rawArgument)) {
+            return "not-supported";
+        } else {
+            return rawArgument;
+        }
+    }
+
     @Override
     protected final SubstatementValidator getSubstatementValidator() {
         return null;
     }
 
+    @Override
+    protected final DeviateStatement createDeclared(final StmtContext<DeviateKind, DeviateStatement, ?> ctx,
+            final ImmutableList<? extends DeclaredStatement<?>> substatements) {
+        return new DeviateStatementImpl(ctx, substatements);
+    }
+
+    @Override
+    protected final DeviateStatement createEmptyDeclared(final StmtContext<DeviateKind, DeviateStatement, ?> ctx) {
+        // This is exceedingly unlikely, just reuse the implementation
+        return createDeclared(ctx, ImmutableList.of());
+    }
+
+    @Override
+    protected final DeviateEffectiveStatement createEffective(
+            final StmtContext<DeviateKind, DeviateStatement, DeviateEffectiveStatement> ctx,
+            final DeviateStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+        return new DeviateEffectiveStatementImpl(declared, substatements);
+    }
+
+    @Override
+    protected final DeviateEffectiveStatement createEmptyEffective(
+            final StmtContext<DeviateKind, DeviateStatement, DeviateEffectiveStatement> ctx,
+            final DeviateStatement declared) {
+        // This is exceedingly unlikely, just reuse the implementation
+        return createEffective(ctx, declared, ImmutableList.of());
+    }
+
     protected SubstatementValidator getSubstatementValidatorForDeviate(final DeviateKind deviateKind) {
         switch (deviateKind) {
             case NOT_SUPPORTED:
@@ -202,8 +230,8 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<
         }
     }
 
-    private static boolean isDeviationSupported(final Mutable<DeviateKind, DeviateStatement,
-            EffectiveStatement<DeviateKind, DeviateStatement>> deviateStmtCtx,
+    private static boolean isDeviationSupported(
+            final Mutable<DeviateKind, DeviateStatement, DeviateEffectiveStatement> deviateStmtCtx,
             final SchemaNodeIdentifier deviationTarget) {
         final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules = deviateStmtCtx.getFromNamespace(
                 ModulesDeviatedByModules.class, SupportedModules.SUPPORTED_MODULES);
@@ -213,7 +241,7 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<
 
         final QNameModule currentModule = deviateStmtCtx.getFromNamespace(ModuleCtxToModuleQName.class,
                 deviateStmtCtx.getRoot());
-        final QNameModule targetModule = deviationTarget.getLastComponent().getModule();
+        final QNameModule targetModule = Iterables.getLast(deviationTarget.getNodeIdentifiers()).getModule();
 
         final Set<QNameModule> deviationModulesSupportedByTargetModule = modulesDeviatedByModules.get(targetModule);
         if (deviationModulesSupportedByTargetModule != null) {
@@ -223,6 +251,8 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<
         return false;
     }
 
+    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+            justification = "https://github.com/spotbugs/spotbugs/issues/811")
     private static void performDeviateAdd(final StatementContextBase<?, ?, ?> deviateStmtCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
         for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
@@ -250,6 +280,8 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<
         copyStatement(stmtCtxToBeAdded, targetCtx);
     }
 
+    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+            justification = "https://github.com/spotbugs/spotbugs/issues/811")
     private static void performDeviateReplace(final StatementContextBase<?, ?, ?> deviateStmtCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
         for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {
@@ -299,6 +331,8 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport<
                 stmtToBeReplaced.getStatementName(), targetCtx.getStatementArgument());
     }
 
+    @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
+            justification = "https://github.com/spotbugs/spotbugs/issues/811")
     private static void performDeviateDelete(final StatementContextBase<?, ?, ?> deviateStmtCtx,
             final StatementContextBase<?, ?, ?> targetCtx) {
         for (Mutable<?, ?, ?> originalStmtCtx : deviateStmtCtx.mutableDeclaredSubstatements()) {