final void copyTo(final StatementContextBase<?, ?, ?> target, final CopyType typeOfCopy, @Nullable final QNameModule targetModule) { final Collection<Mutable<?, ?, ?>> buffer = new ArrayList<>(substatements.size() + effective.size()); for (final Mutable<?, ?, ?> stmtContext : substatements.values()) { if (stmtContext.isSupportedByFeatures()) { copySubstatement(stmtContext, target, typeOfCopy, targetModule, buffer); } } for (final Mutable<?, ?, ?> stmtContext : effective) { copySubstatement(stmtContext, target, typeOfCopy, targetModule, buffer); } target.addEffectiveSubstatements(buffer); }
final <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceItemAddedAction(final Class<N> type, final ModelProcessingPhase phase, final NamespaceKeyCriterion<K> criterion, final OnNamespaceItemAdded listener) { final Optional<Entry<K, V>> existing = getFromNamespace(type, criterion); if (existing.isPresent()) { final Entry<K, V> entry = existing.get(); LOG.debug("Listener on {} criterion {} found a pre-existing match: {}", type, criterion, entry); waitForPhase(entry.getValue(), type, phase, criterion, listener); return; } final NamespaceBehaviourWithListeners<K, V, N> behaviour = getBehaviour(type); behaviour.addListener(new PredicateValueAddedListener<K, V>(this) { @Override boolean onValueAdded(final K key, final V value) { if (criterion.match(key)) { LOG.debug("Listener on {} criterion {} matched added key {}", type, criterion, key); waitForPhase(value, type, phase, criterion, listener); return true; } return false; } }); }
@Override public @NonNull StatementDefinition getDefinition() { return getPublicDefinition(); }
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); }
private static void copyStatement(final Mutable<?, ?, ?> stmtCtxToBeCopied, final StatementContextBase<?, ?, ?> targetCtx) { // we need to make a copy of the statement context only if it is an unknown statement, otherwise // we can reuse the original statement context if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeCopied)) { targetCtx.addEffectiveSubstatement(stmtCtxToBeCopied); } else { targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeCopied, CopyType.ORIGINAL)); } }
SubstatementContext(final StatementContextBase<A, D, E> original, final StatementContextBase<?, ?, ?> parent, final CopyType copyType, final QNameModule targetModule) { super(original, copyType); this.parent = requireNonNull(parent); this.argument = targetModule == null ? original.getStatementArgument() : original.definition().adaptArgumentValue(original, targetModule); }
&& YangStmtMapping.LEAF_LIST.equals(targetCtx.getPublicDefinition())) { 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()); return; for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) { if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) { targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced); copyStatement(stmtCtxToBeReplaced, targetCtx); return; for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) { if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) { targetCtxSubstatement.setIsSupportedToBuildEffective(false); stmtToBeReplaced.getStatementName(), targetCtx.getStatementArgument());
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); }
private static void deleteStatement(final StmtContext<?, ?, ?> stmtCtxToBeDeleted, final StatementContextBase<?, ?, ?> targetCtx) { final StatementDefinition stmtToBeDeleted = stmtCtxToBeDeleted.getPublicDefinition(); final String stmtArgument = stmtCtxToBeDeleted.rawStatementArgument(); for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableEffectiveSubstatements()) { if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.getPublicDefinition(), targetCtxSubstatement.rawStatementArgument())) { targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeDeleted, stmtArgument); return; } } for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) { if (statementsAreEqual(stmtToBeDeleted, stmtArgument, targetCtxSubstatement.getPublicDefinition(), targetCtxSubstatement.rawStatementArgument())) { targetCtxSubstatement.setIsSupportedToBuildEffective(false); return; } } 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()); }
@Override public void apply(final InferenceContext ctx) { final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.resolve(ctx); if (!isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) { augmentNode.setIsSupportedToBuildEffective(false); return; } /** * Marks case short hand in augment */ if (augmentTargetCtx.getPublicDefinition() == YangStmtMapping.CHOICE) { augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, Boolean.TRUE); } // 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. final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode; 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); } }
/** * Create a new substatement at the specified offset. * * @param offset Substatement offset * @param def definition context * @param ref source reference * @param argument statement argument * @param <X> new substatement argument type * @param <Y> new substatement declared type * @param <Z> new substatement effective type * @return A new substatement */ @SuppressWarnings("checkstyle:methodTypeParameterName") public final <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> StatementContextBase<X, Y, Z> createSubstatement(final int offset, final StatementDefinitionContext<X, Y, Z> def, final StatementSourceReference ref, final String argument) { final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase(); checkState(inProgressPhase != ModelProcessingPhase.EFFECTIVE_MODEL, "Declared statement cannot be added in effective phase at: %s", getStatementSourceReference()); final Optional<StatementSupport<?, ?, ?>> implicitParent = definition.getImplicitParentFor(def.getPublicView()); if (implicitParent.isPresent()) { return createImplicitParent(offset, implicitParent.get(), ref, argument).createSubstatement(offset, def, ref, argument); } final StatementContextBase<X, Y, Z> ret = new SubstatementContext<>(this, def, ref, argument); substatements = substatements.put(offset, ret); def.onStatementAdded(ret); return ret; }
private static void addOrReplaceNode(final Mutable<?, ?, ?> refineSubstatementCtx, final StatementContextBase<?, ?, ?> refineTargetNodeCtx) { 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.coerceParentContext().getStatementArgument(), refineSubstatementCtx.getPublicDefinition(), refineTargetNodeCtx.getPublicDefinition()); if (isAllowedToAddByRefine(refineSubstatementDef)) { refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx); } else { refineTargetNodeCtx.removeStatementFromEffectiveSubstatements(refineSubstatementDef); refineTargetNodeCtx.addEffectiveSubstatement(refineSubstatementCtx); } }
private static void addStatement(final Mutable<?, ?, ?> stmtCtxToBeAdded, final StatementContextBase<?, ?, ?> targetCtx) { if (!StmtContextUtils.isUnknownStatement(stmtCtxToBeAdded)) { final StatementDefinition stmtToBeAdded = stmtCtxToBeAdded.getPublicDefinition(); if (SINGLETON_STATEMENTS.contains(stmtToBeAdded) || YangStmtMapping.DEFAULT.equals(stmtToBeAdded) && YangStmtMapping.LEAF.equals(targetCtx.getPublicDefinition())) { for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.allSubstatements()) { InferenceException.throwIf(stmtToBeAdded.equals(targetCtxSubstatement.getPublicDefinition()), stmtCtxToBeAdded.getStatementSourceReference(), "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()); } } } copyStatement(stmtCtxToBeAdded, targetCtx); }
if (isIgnoringIfFeatures()) { supportedByFeatures = OptionalBoolean.of(true); return true; final boolean isParentSupported = isParentSupportedByFeatures(); final Set<QName> supportedFeatures = getFromNamespace(SupportedFeaturesNamespace.class, SupportedFeatures.SUPPORTED_FEATURES); final boolean ret = supportedFeatures == null ? true
/** * Adds a {@link ContextMutation} to a {@link ModelProcessingPhase}. * * @throws IllegalStateException * when the mutation was registered after phase was completed */ void addMutation(final ModelProcessingPhase phase, final ContextMutation mutation) { ModelProcessingPhase finishedPhase = completedPhase; while (finishedPhase != null) { checkState(!phase.equals(finishedPhase), "Mutation registered after phase was completed at: %s", getStatementSourceReference()); finishedPhase = finishedPhase.getPreviousPhase(); } if (phaseMutation.isEmpty()) { phaseMutation = newMultimap(); } phaseMutation.put(phase, mutation); }
private void beforeAddEffectiveStatement(final int toAdd) { final ModelProcessingPhase inProgressPhase = getRoot().getSourceContext().getInProgressPhase(); checkState(inProgressPhase == ModelProcessingPhase.FULL_DECLARATION || inProgressPhase == ModelProcessingPhase.EFFECTIVE_MODEL, "Effective statement cannot be added in declared phase at: %s", getStatementSourceReference()); if (effective.isEmpty()) { effective = new ArrayList<>(toAdd); } }
final <K, V, N extends IdentifierNamespace<K, V>> void onNamespaceItemAddedAction(final Class<N> type, final K key, final OnNamespaceItemAdded listener) { final Object potential = getFromNamespace(type, key); if (potential != null) { LOG.trace("Listener on {} key {} satisfied immediately", type, key); listener.namespaceItemAdded(this, type, key, potential); return; } getBehaviour(type).addListener(new KeyedValueAddedListener<K>(this, key) { @Override void onValueAdded(final Object value) { listener.namespaceItemAdded(StatementContextBase.this, type, key, value); } }); }
public void appendImplicitStatement(final StatementSupport<?, ?, ?> statementToAdd) { createSubstatement(substatements.capacity(), new StatementDefinitionContext<>(statementToAdd), ImplicitSubstatement.of(getStatementSourceReference()), null); }
/** * Removes a statement context from the effective substatements based on its statement definition (i.e statement * keyword) and raw (in String form) statement argument. The statement context is removed only if both statement * definition and statement argument match with one of the effective substatements' statement definition * and argument. * * <p> * If the statementArg parameter is null, the statement context is removed based only on its statement definition. * * @param statementDef statement definition of the statement context to remove * @param statementArg statement argument of the statement context to remove */ public void removeStatementFromEffectiveSubstatements(final StatementDefinition statementDef, final String statementArg) { if (statementArg == null) { removeStatementFromEffectiveSubstatements(statementDef); } if (effective.isEmpty()) { return; } final Iterator<Mutable<?, ?, ?>> iterator = effective.iterator(); while (iterator.hasNext()) { final Mutable<?, ?, ?> next = iterator.next(); if (statementDef.equals(next.getPublicDefinition()) && statementArg.equals(next.rawStatementArgument())) { iterator.remove(); } } shrinkEffective(); }
private SchemaPath createSchemaPath() { final Optional<SchemaPath> maybeParentPath = parent.getSchemaPath(); Verify.verify(maybeParentPath.isPresent(), "Parent %s does not have a SchemaPath", parent); final SchemaPath parentPath = maybeParentPath.get(); || Boolean.TRUE.equals(parent.getFromNamespace(AugmentToChoiceNamespace.class, parent))) && isSupportedAsShorthandCase()) { path = parentPath.createChild(qname);