BUG-9043: Remove use of CheckedFuture from YANG components
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / TextToASTTransformer.java
index 5c43a53bb5ef1d143c524ffa68f1b877a2d05fe6..12e486f3add4ac91f90088c48d1c288329a2a2b8 100644 (file)
@@ -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<YangTextSchemaSource, ASTSchemaSource> {
+@Beta
+public final class TextToASTTransformer extends SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
     private static final Logger LOG = LoggerFactory.getLogger(TextToASTTransformer.class);
-    private static final Function<Exception, SchemaSourceTransformationException> MAPPER = new ExceptionMapper<SchemaSourceTransformationException>("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<YangTextSchemaSource> getInputRepresentation() {
-        return YangTextSchemaSource.class;
-    }
-
-    @Override
-    public Class<ASTSchemaSource> 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<ASTSchemaSource, SchemaSourceTransformationException> transformSchemaSource(final YangTextSchemaSource source) {
-        return Futures.makeChecked(executor.submit(new Callable<ASTSchemaSource>() {
-            @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<String> 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;
     }
 }