void addSource(final @NonNull StatementStreamSource source) { sources.add(new SourceSpecificContext(this, source)); }
@Override public void addMutableStmtToSeal(final MutableStatement mutableStatement) { sourceContext.addMutableStmtToSeal(mutableStatement); }
@Override public void startStatement(final int childId, final QName name, final String argument, final StatementSourceReference ref) { final Optional<StatementContextBase<?, ?, ?>> existing = ctx.lookupDeclaredChild(current, childId); current = existing.isPresent() ? existing.get() : verifyNotNull(ctx.createDeclaredChild(current, childId, name, argument, ref)); }
@Override public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorage(final Class<N> type, final K key, final V value) { // RootStatementContext takes care of IncludedModuleContext and the rest... final V ret = getRoot().putToLocalStorage(type, key, value); // FIXME: what about duplicates? updateImportedNamespaces(type, value); return ret; }
void loadStatements() { LOG.trace("Source {} loading statements for phase {}", source, inProgressPhase); switch (inProgressPhase) { case SOURCE_PRE_LINKAGE: source.writePreLinkage(new StatementContextWriter(this, inProgressPhase), stmtDef()); break; case SOURCE_LINKAGE: source.writeLinkage(new StatementContextWriter(this, inProgressPhase), stmtDef(), preLinkagePrefixes(), getRootVersion()); break; case STATEMENT_DEFINITION: source.writeLinkageAndStatementDefinitions(new StatementContextWriter(this, inProgressPhase), stmtDef(), prefixes(), getRootVersion()); break; case FULL_DECLARATION: source.writeFull(new StatementContextWriter(this, inProgressPhase), stmtDef(), prefixes(), getRootVersion()); break; default: break; } }
private EffectiveModelContext transform() { checkState(finishedPhase == ModelProcessingPhase.EFFECTIVE_MODEL); final List<DeclaredStatement<?>> rootStatements = new ArrayList<>(sources.size()); for (final SourceSpecificContext source : sources) { rootStatements.add(source.getRoot().buildDeclared()); } return new EffectiveModelContext(rootStatements); }
SomeModifiersUnresolvedException buildFailure = null; for (final SourceSpecificContext failedSource : sourcesToProgress) { final Optional<SourceException> optSourceEx = failedSource.failModifiers(currentPhase); if (!optSourceEx.isPresent()) { continue; final SourceIdentifier sourceId = StmtContextUtils.createSourceIdentifier(failedSource.getRoot()); buildFailure = new SomeModifiersUnresolvedException(currentPhase, sourceId, sourceEx); } else {
@Override public void addRequiredSource(final SourceIdentifier dependency) { checkState(sourceContext.getInProgressPhase() == ModelProcessingPhase.SOURCE_PRE_LINKAGE, "Add required module is allowed only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase"); if (requiredSources.isEmpty()) { requiredSources = new HashSet<>(); } requiredSources.add(dependency); }
private static void removeConflictingLibSources(final SourceSpecificContext source, final Set<SourceSpecificContext> requiredLibs) { final Iterator<SourceSpecificContext> requiredLibsIter = requiredLibs.iterator(); while (requiredLibsIter.hasNext()) { final SourceSpecificContext currentReqSource = requiredLibsIter.next(); if (source.getRootIdentifier().equals(currentReqSource.getRootIdentifier())) { requiredLibsIter.remove(); } } }
private QNameToStatementDefinition stmtDef() { // regular YANG statements and extension supports added final StatementSupportBundle supportsForPhase = currentContext.getSupportsForPhase(inProgressPhase); qnameToStmtDefMap.putAll(supportsForPhase.getCommonDefinitions()); qnameToStmtDefMap.putAll(supportsForPhase.getDefinitionsSpecificForVersion(getRootVersion())); // No further actions needed if (inProgressPhase != ModelProcessingPhase.FULL_DECLARATION) { return qnameToStmtDefMap; } // We need to any and all extension statements which have been declared in the context final Map<QName, StatementSupport<?, ?, ?>> extensions = currentContext.getAllFromNamespace( StatementDefinitionNamespace.class); if (extensions != null) { extensions.forEach((qname, support) -> { final StatementSupport<?, ?, ?> existing = qnameToStmtDefMap.putIfAbsent(qname, support); if (existing != null) { LOG.debug("Source {} already defines statement {} as {}", source, qname, existing); } else { LOG.debug("Source {} defined statement {} as {}", source, qname, support); } }); } return qnameToStmtDefMap; }
private void collectRequiredSourcesFromLib( final TreeBasedTable<String, Optional<Revision>, SourceSpecificContext> libSourcesTable, final Set<SourceSpecificContext> requiredLibs, final SourceSpecificContext source) { for (final SourceIdentifier requiredSource : source.getRequiredSources()) { final SourceSpecificContext libSource = getRequiredLibSource(requiredSource, libSourcesTable); if (libSource != null && requiredLibs.add(libSource)) { collectRequiredSourcesFromLib(libSourcesTable, requiredLibs, libSource); } } }
private PrefixToModule preLinkagePrefixes() { final PrefixToModuleMap preLinkagePrefixes = new PrefixToModuleMap(true); final Map<String, URI> prefixToNamespaceMap = getAllFromLocalStorage(ImpPrefixToNamespace.class); if (prefixToNamespaceMap == null) { //:FIXME if it is a submodule without any import, the map is null. Handle also submodules and includes... return null; } prefixToNamespaceMap.forEach((key, value) -> preLinkagePrefixes.put(key, QNameModule.create(value))); return preLinkagePrefixes; }
@Override public <K, V, N extends IdentifierNamespace<K, V>> V putToLocalStorageIfAbsent(final Class<N> type, final K key, final V value) { // RootStatementContext takes care of IncludedModuleContext and the rest... final V ret = getRoot().putToLocalStorageIfAbsent(type, key, value); if (ret == null) { updateImportedNamespaces(type, value); } return ret; }
private PrefixToModule prefixes() { final Map<String, StmtContext<?, ?, ?>> allImports = getRoot().getAllFromNamespace( ImportPrefixToModuleCtx.class); if (allImports != null) { allImports.forEach((key, value) -> prefixToModuleMap.put(key, getRoot().getFromNamespace(ModuleCtxToModuleQName.class, value))); } final Map<String, StmtContext<?, ?, ?>> allBelongsTo = getRoot().getAllFromNamespace( BelongsToPrefixToModuleCtx.class); if (allBelongsTo != null) { allBelongsTo.forEach((key, value) -> prefixToModuleMap.put(key, getRoot().getFromNamespace(ModuleCtxToModuleQName.class, value))); } return prefixToModuleMap; }
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); } }
private Set<SourceSpecificContext> getRequiredSourcesFromLib() { checkState(currentPhase == ModelProcessingPhase.SOURCE_PRE_LINKAGE, "Required library sources can be collected only in ModelProcessingPhase.SOURCE_PRE_LINKAGE phase," + " but current phase was %s", currentPhase); final TreeBasedTable<String, Optional<Revision>, SourceSpecificContext> libSourcesTable = TreeBasedTable.create( String::compareTo, Revision::compare); for (final SourceSpecificContext libSource : libSources) { final SourceIdentifier libSourceIdentifier = requireNonNull(libSource.getRootIdentifier()); libSourcesTable.put(libSourceIdentifier.getName(), libSourceIdentifier.getRevision(), libSource); } final Set<SourceSpecificContext> requiredLibs = new HashSet<>(); for (final SourceSpecificContext source : sources) { collectRequiredSourcesFromLib(libSourcesTable, requiredLibs, source); removeConflictingLibSources(source, requiredLibs); } return requiredLibs; }
StatementContextBase<?, ?, ?> createDeclaredChild(final StatementContextBase<?, ?, ?> current, final int childId, final QName name, final String argument, final StatementSourceReference ref) { StatementDefinitionContext<?, ?, ?> def = currentContext.getStatementDefinition(getRootVersion(), name); if (def == null) { def = currentContext.getModelDefinedStatementDefinition(name);
private SomeModifiersUnresolvedException propagateException(final SourceSpecificContext source, final RuntimeException cause) throws SomeModifiersUnresolvedException { final SourceIdentifier sourceId = StmtContextUtils.createSourceIdentifier(source.getRoot()); if (!(cause instanceof SourceException)) { /* * This should not be happening as all our processing should provide SourceExceptions. * We will wrap the exception to provide enough information to identify the problematic model, * but also emit a warning so the offending codepath will get fixed. */ LOG.warn("Unexpected error processing source {}. Please file an issue with this model attached.", sourceId, cause); } throw new SomeModifiersUnresolvedException(currentPhase, sourceId, cause); }
/** * 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; }
void addLibSource(final @NonNull StatementStreamSource libSource) { checkState(!isEnabledSemanticVersioning(), "Library sources are not supported in semantic version mode currently."); checkState(currentPhase == ModelProcessingPhase.INIT, "Add library source is allowed in ModelProcessingPhase.INIT only"); libSources.add(new SourceSpecificContext(this, libSource)); }