import static com.google.common.base.Preconditions.checkArgument;
-import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
-
+import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
+import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaContextFactory;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaResolutionException;
throws SchemaSourceException, IOException, YangSyntaxErrorException {
checkArgument(source != null);
- final ASTSchemaSource ast = TextToASTTransformer.TRANSFORMATION.apply(source).checkedGet();
+ final ASTSchemaSource ast = TextToASTTransformer.transformText(source);
LOG.trace("Resolved source {} to source {}", source, ast);
// AST carries an accurate identifier, check if it matches the one supplied by the source. If it
final YangTextSchemaSource text;
if (!parsedId.equals(providedId)) {
if (!parsedId.getName().equals(providedId.getName())) {
- LOG.info("Provided module name {} does not match actual text {}, corrected", providedId.toYangFilename(),
- parsedId.toYangFilename());
+ LOG.info("Provided module name {} does not match actual text {}, corrected",
+ providedId.toYangFilename(), parsedId.toYangFilename());
} else {
final String sourceRev = providedId.getRevision();
final String astRev = parsedId.getRevision();
- if (sourceRev != null) {
+ if (sourceRev != null && !SourceIdentifier.NOT_PRESENT_FORMATTED_REVISION.equals(sourceRev)) {
if (!sourceRev.equals(astRev)) {
LOG.info("Provided module revision {} does not match actual text {}, corrected",
providedId.toYangFilename(), parsedId.toYangFilename());
* @throws SchemaSourceException When parsing encounters general error
* @return a YangTextSchemaSourceRegistration for this URL
*/
- public YangTextSchemaSourceRegistration registerSource(@Nonnull final URL url) throws SchemaSourceException, IOException, YangSyntaxErrorException {
+ public YangTextSchemaSourceRegistration registerSource(@Nonnull final URL url) throws SchemaSourceException,
+ IOException, YangSyntaxErrorException {
checkArgument(url != null, "Supplied URL must not be null");
- final SourceIdentifier guessedId = RevisionSourceIdentifier.create(url.getFile(), Optional.absent());
+ final String path = url.getPath();
+ final String fileName = path.substring(path.lastIndexOf('/') + 1);
+ final SourceIdentifier guessedId = guessSourceIdentifier(fileName);
return registerSource(new YangTextSchemaSource(guessedId) {
@Override
public InputStream openStream() throws IOException {
});
}
+ private static SourceIdentifier guessSourceIdentifier(final String fileName) {
+ try {
+ return YangTextSchemaSource.identifierFromFilename(fileName);
+ } catch (IllegalArgumentException e) {
+ LOG.warn("Invalid file name format in '{}'", fileName, e);
+ return RevisionSourceIdentifier.create(fileName);
+ }
+ }
+
/**
* Try to parse all currently available yang files and build new schema context.
* @return new schema context iif there is at least 1 yang file registered and
* @return new schema context iif there is at least 1 yang file registered and
* new schema context was successfully built.
*/
- public Optional<SchemaContext> getSchemaContext(StatementParserMode statementParserMode) {
+ public Optional<SchemaContext> getSchemaContext(final StatementParserMode statementParserMode) {
final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
Optional<SchemaContext> sc;
Object v;
} while (v != version);
while (true) {
- final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources, statementParserMode);
+ final CheckedFuture<SchemaContext, SchemaResolutionException> f = factory.createSchemaContext(sources,
+ statementParserMode);
try {
sc = Optional.of(f.checkedGet());
break;
}
@Override
- public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(final SourceIdentifier sourceIdentifier) {
+ public synchronized CheckedFuture<YangTextSchemaSource, SchemaSourceException> getSource(
+ final SourceIdentifier sourceIdentifier) {
final Collection<YangTextSchemaSource> ret = texts.get(sourceIdentifier);
LOG.debug("Lookup {} result {}", sourceIdentifier, ret);
if (ret.isEmpty()) {
- return Futures.immediateFailedCheckedFuture(
- new MissingSchemaSourceException("URL for " + sourceIdentifier + " not registered", sourceIdentifier));
+ return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException(
+ "URL for " + sourceIdentifier + " not registered", sourceIdentifier));
}
return Futures.immediateCheckedFuture(ret.iterator().next());
return ImmutableSet.copyOf(texts.keySet());
}
+ @Beta
+ public synchronized Collection<YangTextSchemaSource> getSourceTexts(final SourceIdentifier sourceIdentifier) {
+ return ImmutableSet.copyOf(texts.get(sourceIdentifier));
+ }
+
+ @Beta
+ public SchemaContext trySchemaContext() throws SchemaResolutionException {
+ return trySchemaContext(StatementParserMode.DEFAULT_MODE);
+ }
+
+ @Beta
+ public SchemaContext trySchemaContext(final StatementParserMode statementParserMode)
+ throws SchemaResolutionException {
+ final SchemaContextFactory factory = repository.createSchemaContextFactory(SchemaSourceFilter.ALWAYS_ACCEPT);
+ return factory.createSchemaContext(ImmutableSet.copyOf(requiredSources), statementParserMode).checkedGet();
+ }
+
@Override
public void close() {
transReg.close();