X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-rfc7950%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Frfc7950%2Fstmt%2Fdeviate%2FAbstractDeviateStatementSupport.java;h=f2a16708b087fe264bbc67007d924939076e2027;hb=5db72610e7358baa1f5c9dd85abc9e93f3c8b020;hp=2c13541396c0959d0acc6ad2e539f58ceec86dfb;hpb=1cc6359e5bae1459582e9262a24ca56f23bcc70f;p=yangtools.git diff --git a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/deviate/AbstractDeviateStatementSupport.java b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/deviate/AbstractDeviateStatementSupport.java index 2c13541396..f2a16708b0 100644 --- a/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/deviate/AbstractDeviateStatementSupport.java +++ b/yang/yang-parser-rfc7950/src/main/java/org/opendaylight/yangtools/yang/parser/rfc7950/stmt/deviate/AbstractDeviateStatementSupport.java @@ -7,25 +7,32 @@ */ 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.EffectiveStmtCtx.Current; import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException; import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder; import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction; @@ -44,9 +51,9 @@ import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport> { - private static final Logger LOG = LoggerFactory.getLogger(DeviateStatementImpl.class); +abstract class AbstractDeviateStatementSupport + extends BaseStatementSupport { + private static final Logger LOG = LoggerFactory.getLogger(AbstractDeviateStatementSupport.class); private static final SubstatementValidator DEVIATE_NOT_SUPPORTED_SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping.DEVIATE).build(); @@ -82,21 +89,14 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< .addOptional(YangStmtMapping.UNITS) .build(); - private static final Map KEYWORD_TO_DEVIATE_MAP; + private static final ImmutableMap KEYWORD_TO_DEVIATE_MAP = + Maps.uniqueIndex(Arrays.asList(DeviateKind.values()), DeviateKind::getKeyword); - static { - final Builder keywordToDeviateMapBuilder = ImmutableMap.builder(); - for (final DeviateKind deviate : DeviateKind.values()) { - keywordToDeviateMapBuilder.put(deviate.getKeyword(), deviate); - } - KEYWORD_TO_DEVIATE_MAP = keywordToDeviateMapBuilder.build(); - } - - private static final Set SINGLETON_STATEMENTS = ImmutableSet.of( + private static final ImmutableSet SINGLETON_STATEMENTS = ImmutableSet.of( YangStmtMapping.UNITS, YangStmtMapping.CONFIG, YangStmtMapping.MANDATORY, YangStmtMapping.MIN_ELEMENTS, YangStmtMapping.MAX_ELEMENTS); - private static final Set IMPLICIT_STATEMENTS = ImmutableSet.of(YangStmtMapping.CONFIG, + private static final ImmutableSet IMPLICIT_STATEMENTS = ImmutableSet.of(YangStmtMapping.CONFIG, YangStmtMapping.MANDATORY, YangStmtMapping.MAX_ELEMENTS, YangStmtMapping.MIN_ELEMENTS); AbstractDeviateStatementSupport() { @@ -105,30 +105,18 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< @Override public final DeviateKind parseArgumentValue(final StmtContext ctx, final String value) { - return SourceException.throwIfNull(KEYWORD_TO_DEVIATE_MAP.get(value), - ctx.getStatementSourceReference(), "String '%s' is not valid deviate argument", value); - } - - @Override - public final DeviateStatement createDeclared(final StmtContext ctx) { - return new DeviateStatementImpl(ctx); + return SourceException.throwIfNull(KEYWORD_TO_DEVIATE_MAP.get(value), ctx.sourceReference(), + "String '%s' is not valid deviate argument", value); } @Override - public final EffectiveStatement createEffective( - final StmtContext> ctx) { - return new DeviateEffectiveStatementImpl(ctx); - } - - @Override - public final void onFullDefinitionDeclared(final Mutable> deviateStmtCtx) { + public final void onFullDefinitionDeclared( + final Mutable 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; @@ -137,17 +125,17 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< final ModelActionBuilder deviateAction = deviateStmtCtx.newInferenceAction( ModelProcessingPhase.EFFECTIVE_MODEL); - final Prerequisite>> sourceCtxPrerequisite = + final Prerequisite> sourceCtxPrerequisite = deviateAction.requiresCtx(deviateStmtCtx, ModelProcessingPhase.EFFECTIVE_MODEL); final Prerequisite>> 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); @@ -174,17 +162,50 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< @Override public void prerequisiteFailed(final Collection> failed) { - throw new InferenceException(deviateStmtCtx.getParentContext().getStatementSourceReference(), + throw new InferenceException(deviateStmtCtx.coerceParentContext().sourceReference(), "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 ctx, + final ImmutableList> substatements) { + return new DeviateStatementImpl(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(), substatements); + } + + @Override + protected final DeviateStatement createEmptyDeclared(final StmtContext ctx) { + // This is exceedingly unlikely, just reuse the implementation + return createDeclared(ctx, ImmutableList.of()); + } + + @Override + protected DeviateEffectiveStatement createEffective(final Current stmt, + final ImmutableList> substatements) { + return new DeviateEffectiveStatementImpl(stmt.declared(), substatements); + } + protected SubstatementValidator getSubstatementValidatorForDeviate(final DeviateKind deviateKind) { switch (deviateKind) { case NOT_SUPPORTED: @@ -201,10 +222,10 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< } } - private static boolean isDeviationSupported(final Mutable> deviateStmtCtx, + private static boolean isDeviationSupported( + final Mutable deviateStmtCtx, final SchemaNodeIdentifier deviationTarget) { - final Map> modulesDeviatedByModules = deviateStmtCtx.getFromNamespace( + final SetMultimap modulesDeviatedByModules = deviateStmtCtx.getFromNamespace( ModulesDeviatedByModules.class, SupportedModules.SUPPORTED_MODULES); if (modulesDeviatedByModules == null) { return true; @@ -212,7 +233,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 deviationModulesSupportedByTargetModule = modulesDeviatedByModules.get(targetModule); if (deviationModulesSupportedByTargetModule != null) { @@ -222,6 +243,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()) { @@ -233,12 +256,12 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< private static void addStatement(final Mutable stmtCtxToBeAdded, final StatementContextBase targetCtx) { if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeAdded)) { - final StatementDefinition stmtToBeAdded = stmtCtxToBeAdded.getPublicDefinition(); + final StatementDefinition stmtToBeAdded = stmtCtxToBeAdded.publicDefinition(); if (SINGLETON_STATEMENTS.contains(stmtToBeAdded) || YangStmtMapping.DEFAULT.equals(stmtToBeAdded) - && YangStmtMapping.LEAF.equals(targetCtx.getPublicDefinition())) { + && YangStmtMapping.LEAF.equals(targetCtx.publicDefinition())) { for (final StmtContext targetCtxSubstatement : targetCtx.allSubstatements()) { - InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.getPublicDefinition()), - stmtCtxToBeAdded.getStatementSourceReference(), + InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.publicDefinition()), + stmtCtxToBeAdded.sourceReference(), "Deviation cannot add substatement %s to target node %s because it is already defined " + "in target and can appear only once.", stmtToBeAdded.getStatementName(), targetCtx.getStatementArgument()); @@ -249,6 +272,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()) { @@ -259,18 +284,18 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< private static void replaceStatement(final Mutable stmtCtxToBeReplaced, final StatementContextBase targetCtx) { - final StatementDefinition stmtToBeReplaced = stmtCtxToBeReplaced.getPublicDefinition(); + final StatementDefinition stmtToBeReplaced = stmtCtxToBeReplaced.publicDefinition(); if (YangStmtMapping.DEFAULT.equals(stmtToBeReplaced) - && YangStmtMapping.LEAF_LIST.equals(targetCtx.getPublicDefinition())) { + && YangStmtMapping.LEAF_LIST.equals(targetCtx.publicDefinition())) { LOG.error("Deviation cannot replace substatement {} in target leaf-list {} because a leaf-list can " + "have multiple default statements. At line: {}", stmtToBeReplaced.getStatementName(), - targetCtx.getStatementArgument(), stmtCtxToBeReplaced.getStatementSourceReference()); + targetCtx.getStatementArgument(), stmtCtxToBeReplaced.sourceReference()); return; } for (final StmtContext targetCtxSubstatement : targetCtx.effectiveSubstatements()) { - if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) { + if (stmtToBeReplaced.equals(targetCtxSubstatement.publicDefinition())) { targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced); copyStatement(stmtCtxToBeReplaced, targetCtx); return; @@ -278,7 +303,7 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< } for (final Mutable targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) { - if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) { + if (stmtToBeReplaced.equals(targetCtxSubstatement.publicDefinition())) { targetCtxSubstatement.setIsSupportedToBuildEffective(false); copyStatement(stmtCtxToBeReplaced, targetCtx); return; @@ -293,11 +318,13 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< return; } - throw new InferenceException(stmtCtxToBeReplaced.getStatementSourceReference(), "Deviation cannot replace " + throw new InferenceException(stmtCtxToBeReplaced.sourceReference(), "Deviation cannot replace " + "substatement %s in target node %s because it does not exist in target node.", 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()) { @@ -308,20 +335,20 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< private static void deleteStatement(final StmtContext stmtCtxToBeDeleted, final StatementContextBase targetCtx) { - final StatementDefinition stmtToBeDeleted = stmtCtxToBeDeleted.getPublicDefinition(); - final String stmtArgument = stmtCtxToBeDeleted.rawStatementArgument(); + final StatementDefinition stmtToBeDeleted = stmtCtxToBeDeleted.publicDefinition(); + final String stmtArgument = stmtCtxToBeDeleted.rawArgument(); for (final Mutable targetCtxSubstatement : targetCtx.mutableEffectiveSubstatements()) { - if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.getPublicDefinition(), - targetCtxSubstatement.rawStatementArgument())) { + if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(), + targetCtxSubstatement.rawArgument())) { targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeDeleted, stmtArgument); return; } } for (final Mutable targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) { - if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.getPublicDefinition(), - targetCtxSubstatement.rawStatementArgument())) { + if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.publicDefinition(), + targetCtxSubstatement.rawArgument())) { targetCtxSubstatement.setIsSupportedToBuildEffective(false); return; } @@ -329,7 +356,7 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< LOG.error("Deviation cannot delete substatement {} with argument '{}' in target node {} because it does " + "not exist in the target node. At line: {}", stmtToBeDeleted.getStatementName(), stmtArgument, - targetCtx.getStatementArgument(), stmtCtxToBeDeleted.getStatementSourceReference()); + targetCtx.getStatementArgument(), stmtCtxToBeDeleted.sourceReference()); } private static void copyStatement(final Mutable stmtCtxToBeCopied, @@ -351,24 +378,24 @@ abstract class AbstractDeviateStatementSupport extends AbstractStatementSupport< private static void validateDeviationTarget(final StmtContext deviateSubStmtCtx, final StmtContext targetCtx) { InferenceException.throwIf(!isSupportedDeviationTarget(deviateSubStmtCtx, targetCtx, - targetCtx.getRootVersion()), deviateSubStmtCtx.getStatementSourceReference(), - "%s is not a valid deviation target for substatement %s.", - targetCtx.getStatementArgument(), deviateSubStmtCtx.getPublicDefinition().getStatementName()); + targetCtx.getRootVersion()), deviateSubStmtCtx.sourceReference(), + "%s is not a valid deviation target for substatement %s.", targetCtx.argument(), + deviateSubStmtCtx.publicDefinition().getStatementName()); } private static boolean isSupportedDeviationTarget(final StmtContext deviateSubstatementCtx, final StmtContext deviateTargetCtx, final YangVersion yangVersion) { Set supportedDeviationTargets = YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(deviateTargetCtx.getRootVersion(), - deviateSubstatementCtx.getPublicDefinition()); + deviateSubstatementCtx.publicDefinition()); if (supportedDeviationTargets == null) { supportedDeviationTargets = YangValidationBundles.SUPPORTED_DEVIATION_TARGETS.get(YangVersion.VERSION_1, - deviateSubstatementCtx.getPublicDefinition()); + deviateSubstatementCtx.publicDefinition()); } // if supportedDeviationTargets is null, it means that the deviate substatement is an unknown statement return supportedDeviationTargets == null || supportedDeviationTargets.contains( - deviateTargetCtx.getPublicDefinition()); + deviateTargetCtx.publicDefinition()); } -} \ No newline at end of file +}