+ return ImmutableList.copyOf(filtered.values());
+ }
+ }
+
+ private static final class AssembleSources implements AsyncFunction<List<ASTSchemaSource>, SchemaContext> {
+
+ private final Optional<Set<QName>> supportedFeatures;
+ private final StatementParserMode statementParserMode;
+ private final Function<ASTSchemaSource, SourceIdentifier> getIdentifier;
+
+ private AssembleSources(final Optional<Set<QName>> supportedFeatures,
+ final StatementParserMode statementParserMode) {
+ this.supportedFeatures = supportedFeatures;
+ this.statementParserMode = Preconditions.checkNotNull(statementParserMode);
+ switch (statementParserMode) {
+ case SEMVER_MODE:
+ this.getIdentifier = ASTSchemaSource::getSemVerIdentifier;
+ break;
+ default:
+ this.getIdentifier = ASTSchemaSource::getIdentifier;
+ }
+ }
+
+ @Override
+ public ListenableFuture<SchemaContext> apply(@Nonnull final List<ASTSchemaSource> sources)
+ throws SchemaResolutionException, ReactorException {
+ final Map<SourceIdentifier, ASTSchemaSource> srcs = Maps.uniqueIndex(sources, getIdentifier);
+ final Map<SourceIdentifier, YangModelDependencyInfo> deps =
+ Maps.transformValues(srcs, ASTSchemaSource::getDependencyInformation);
+
+ LOG.debug("Resolving dependency reactor {}", deps);
+
+ final DependencyResolver res = this.statementParserMode == StatementParserMode.SEMVER_MODE
+ ? SemVerDependencyResolver.create(deps) : RevisionDependencyResolver.create(deps);
+ if (!res.getUnresolvedSources().isEmpty()) {
+ LOG.debug("Omitting models {} due to unsatisfied imports {}", res.getUnresolvedSources(),
+ res.getUnsatisfiedImports());
+ throw new SchemaResolutionException("Failed to resolve required models",
+ res.getResolvedSources(), res.getUnsatisfiedImports());
+ }
+
+ final BuildAction reactor = DefaultReactors.defaultReactor().newBuild(statementParserMode);
+ if (supportedFeatures.isPresent()) {
+ reactor.setSupportedFeatures(supportedFeatures.get());
+ }
+
+ for (final Entry<SourceIdentifier, ASTSchemaSource> e : srcs.entrySet()) {
+ final ASTSchemaSource ast = e.getValue();
+ final ParserRuleContext parserRuleCtx = ast.getAST();
+ Preconditions.checkArgument(parserRuleCtx instanceof StatementContext,
+ "Unsupported context class %s for source %s", parserRuleCtx.getClass(), e.getKey());
+
+ reactor.addSource(YangStatementStreamSource.create(e.getKey(), (StatementContext) parserRuleCtx,
+ ast.getSymbolicName().orElse(null)));
+ }
+
+ final SchemaContext schemaContext;
+ try {
+ schemaContext = reactor.buildEffective();
+ } catch (final ReactorException ex) {
+ throw new SchemaResolutionException("Failed to resolve required models", ex.getSourceIdentifier(), ex);
+ }
+
+ return Futures.immediateFuture(schemaContext);