- final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
- ModelProcessingPhase.EFFECTIVE_MODEL);
- final Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement,
- EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq =
- augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
- final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target =
- augmentAction.mutatesEffectiveCtxPath(getSearchRoot(augmentNode),
- ChildSchemaNodeNamespace.class, augmentNode.coerceStatementArgument().getPathFromRoot());
-
- augmentAction.apply(new InferenceAction() {
- @Override
- public void apply(final InferenceContext ctx) {
- final StatementContextBase<?, ?, ?> augmentTargetCtx =
- (StatementContextBase<?, ?, ?>) target.resolve(ctx);
- if (!isSupportedAugmentTarget(augmentTargetCtx)
- || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
- augmentNode.setIsSupportedToBuildEffective(false);
- return;
- }
-
- // We are targeting a context which is creating implicit nodes. In order to keep things consistent,
- // we will need to circle back when creating effective statements.
- if (augmentTargetCtx.hasImplicitParentSupport()) {
- augmentNode.addToNs(AugmentImplicitHandlingNamespace.class, augmentNode, augmentTargetCtx);
- }
-
- final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
- // FIXME: this is a workaround for models which augment a node which is added via an extension
- // which we do not handle. This needs to be reworked in terms of unknown schema nodes.
- try {
- copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
- augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
- updateAugmentOrder(augmentSourceCtx);
- } catch (final SourceException e) {
- LOG.warn("Failed to add augmentation {} defined at {}",
- augmentTargetCtx.getStatementSourceReference(),
- augmentSourceCtx.getStatementSourceReference(), e);
- }
- }
-
- private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
- Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
- YangStmtMapping.AUGMENT);
- if (currentOrder == null) {
- currentOrder = 1;
- } else {
- currentOrder++;
- }
-
- augmentSourceCtx.addToNs(StmtOrderingNamespace.class, YangStmtMapping.AUGMENT, currentOrder);
- }
-
- @Override
- public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
- /*
- * Do not fail, if it is an uses-augment to an unknown node.
- */
- if (YangStmtMapping.USES == augmentNode.coerceParentContext().getPublicDefinition()) {
- final SchemaNodeIdentifier augmentArg = augmentNode.coerceStatementArgument();
- final Optional<StmtContext<?, ?, ?>> targetNode = ChildSchemaNodeNamespace.findNode(
- getSearchRoot(augmentNode), augmentArg);
- if (targetNode.isPresent() && StmtContextUtils.isUnknownStatement(targetNode.get())) {
- augmentNode.setIsSupportedToBuildEffective(false);
- LOG.warn("Uses-augment to unknown node {}. Augmentation has not been performed. At line: {}",
- augmentArg, augmentNode.getStatementSourceReference());
- return;
- }
- }
-
- throw new InferenceException(augmentNode.getStatementSourceReference(),
- "Augment target '%s' not found", augmentNode.getStatementArgument());
- }
- });
- }
-
- private static StmtContext<?, ?, ?> getSearchRoot(final StmtContext<?, ?, ?> augmentContext) {
- // Augment is in uses - we need to augment instantiated nodes in parent.
- final StmtContext<?, ?, ?> parent = augmentContext.coerceParentContext();
- if (YangStmtMapping.USES == parent.getPublicDefinition()) {
- return parent.getParentContext();
- }
- return parent;
- }
-
- static void copyFromSourceToTarget(final StatementContextBase<?, ?, ?> sourceCtx,
- final StatementContextBase<?, ?, ?> targetCtx) {
- final CopyType typeOfCopy = UsesStatement.class.equals(sourceCtx.coerceParentContext().getPublicDefinition()
- .getDeclaredRepresentationClass()) ? CopyType.ADDED_BY_USES_AUGMENTATION
- : CopyType.ADDED_BY_AUGMENTATION;
- /*
- * Since Yang 1.1, if an augmentation is made conditional with a
- * "when" statement, it is allowed to add mandatory nodes.
- */
- final boolean skipCheckOfMandatoryNodes = YangVersion.VERSION_1_1.equals(sourceCtx.getRootVersion())
- && isConditionalAugmentStmt(sourceCtx);
-
- final Collection<? extends Mutable<?, ?, ?>> declared = sourceCtx.mutableDeclaredSubstatements();
- final Collection<? extends Mutable<?, ?, ?>> effective = sourceCtx.mutableEffectiveSubstatements();
- final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(declared.size() + effective.size());
-
- for (final Mutable<?, ?, ?> originalStmtCtx : declared) {
- if (originalStmtCtx.isSupportedByFeatures()) {
- copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes);
- }
- }
- for (final Mutable<?, ?, ?> originalStmtCtx : effective) {
- copyStatement(originalStmtCtx, targetCtx, typeOfCopy, buffer, skipCheckOfMandatoryNodes);
- }
-
- targetCtx.addEffectiveSubstatements(buffer);
- }