X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-parser-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fparser%2Futil%2FTextToASTTransformer.java;h=12e486f3add4ac91f90088c48d1c288329a2a2b8;hb=da8e25edb78d9eb08303f365d41d2ae369fdce61;hp=5c43a53bb5ef1d143c524ffa68f1b877a2d05fe6;hpb=bee0e69775693323164b99a6120e21278de7b182;p=yangtools.git diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TextToASTTransformer.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TextToASTTransformer.java index 5c43a53bb5..12e486f3ad 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TextToASTTransformer.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/util/TextToASTTransformer.java @@ -1,94 +1,57 @@ /* - * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ + package org.opendaylight.yangtools.yang.parser.util; -import com.google.common.base.Function; -import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.annotations.Beta; import com.google.common.util.concurrent.Futures; -import com.google.common.util.concurrent.ListeningExecutorService; - import java.io.IOException; -import java.io.InputStream; -import java.util.concurrent.Callable; - -import org.antlr.v4.runtime.tree.ParseTreeWalker; -import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.YangContext; -import org.opendaylight.yangtools.util.concurrent.ExceptionMapper; +import java.util.Optional; +import org.antlr.v4.runtime.ParserRuleContext; import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; +import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository; +import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException; import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource; -import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceTransformationException; -import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceTransformer; -import org.opendaylight.yangtools.yang.parser.impl.YangModelBasicValidationListener; -import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl; +import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry; +import org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer; +import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * A {@link SchemaSourceTransformer} which handles translation of models from * {@link YangTextSchemaSource} representation into {@link ASTSchemaSource}. - * - * While this class is currently used explicitly, its long-term purpose is to - * be registered with a {@link org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry} - * and be invoked on demand when the processing pipeline requests the - * ASTSchemaSource representation. */ -public final class TextToASTTransformer implements SchemaSourceTransformer { +@Beta +public final class TextToASTTransformer extends SchemaSourceTransformer { private static final Logger LOG = LoggerFactory.getLogger(TextToASTTransformer.class); - private static final Function MAPPER = new ExceptionMapper("Source transformation", SchemaSourceTransformationException.class) { - @Override - protected SchemaSourceTransformationException newWithCause(final String message, final Throwable cause) { - return new SchemaSourceTransformationException(message, cause); - } - }; - - private final ListeningExecutorService executor; - private TextToASTTransformer(final ListeningExecutorService executor) { - this.executor = Preconditions.checkNotNull(executor); + private TextToASTTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) { + super(provider, YangTextSchemaSource.class, consumer, ASTSchemaSource.class, + input -> Futures.immediateFuture(transformText(input))); } - public static final TextToASTTransformer create(final ListeningExecutorService executor) { - return new TextToASTTransformer(executor); + public static TextToASTTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) { + return new TextToASTTransformer(provider, consumer); } - @Override - public Class getInputRepresentation() { - return YangTextSchemaSource.class; - } - - @Override - public Class getOutputRepresentation() { - return ASTSchemaSource.class; - } + public static ASTSchemaSource transformText(final YangTextSchemaSource text) throws SchemaSourceException, + IOException, YangSyntaxErrorException { + final YangStatementStreamSource src = YangStatementStreamSource.create(text); + final ParserRuleContext ctx = src.getYangAST(); + LOG.debug("Model {} parsed successfully", text); - @Override - public CheckedFuture transformSchemaSource(final YangTextSchemaSource source) { - return Futures.makeChecked(executor.submit(new Callable() { - @Override - public ASTSchemaSource call() throws IOException, YangSyntaxErrorException { - try (InputStream is = source.openStream()) { - final YangContext ctx = YangParserImpl.parseYangSource(is); - LOG.debug("Model {} parsed successfully", source); + // TODO: missing validation (YangModelBasicValidationListener should be re-implemented to new parser) - final ParseTreeWalker walker = new ParseTreeWalker(); - final YangModelBasicValidationListener validator = new YangModelBasicValidationListener(); - walker.walk(validator, ctx); - LOG.debug("Model {} validated successfully", source); - - return ASTSchemaSource.create(source.getIdentifier().getName(), ctx); - } - } - }), MAPPER); - } + final Optional opt = text.getSymbolicName(); + final ASTSchemaSource result = opt.isPresent() ? ASTSchemaSource.create(opt.get(), text.getIdentifier(), ctx) + : ASTSchemaSource.create(text.getIdentifier(), ctx); - @Override - public int getCost() { - // We perform a direct translation, so the cost is 1. - return 1; + return result; } }