2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
3 * This program and the accompanying materials are made available under the
4 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
5 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 package org.opendaylight.yangtools.yang.parser.util;
9 import com.google.common.base.Function;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.CheckedFuture;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListeningExecutorService;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.concurrent.Callable;
19 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.YangContext;
20 import org.opendaylight.yangtools.util.concurrent.ExceptionMapper;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
22 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
23 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceTransformationException;
24 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceTransformer;
25 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
28 * A {@link SchemaSourceTransformer} which handles translation of models from
29 * {@link YangTextSchemaSource} representation into {@link ASTSchemaSource}.
31 * While this class is currently used explicitly, its long-term purpose is to
32 * be registered with a {@link org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry}
33 * and be invoked on demand when the processing pipeline requests the
34 * ASTSchemaSource representation.
36 public final class TextToASTTransformer implements SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
37 private static final Function<Exception, SchemaSourceTransformationException> MAPPER = new ExceptionMapper<SchemaSourceTransformationException>("Source transformation", SchemaSourceTransformationException.class) {
39 protected SchemaSourceTransformationException newWithCause(final String message, final Throwable cause) {
40 return new SchemaSourceTransformationException(message, cause);
43 private final ListeningExecutorService executor;
45 TextToASTTransformer(final ListeningExecutorService executor) {
46 this.executor = Preconditions.checkNotNull(executor);
50 public Class<YangTextSchemaSource> getInputRepresentation() {
51 return YangTextSchemaSource.class;
55 public Class<ASTSchemaSource> getOutputRepresentation() {
56 return ASTSchemaSource.class;
60 public CheckedFuture<ASTSchemaSource, SchemaSourceTransformationException> transformSchemaSource(final YangTextSchemaSource source) {
61 return Futures.makeChecked(executor.submit(new Callable<ASTSchemaSource>() {
63 public ASTSchemaSource call() throws IOException, YangSyntaxErrorException {
64 try (InputStream is = source.openStream()) {
65 final YangContext ctx = YangParserImpl.parseYangSource(is);
66 return ASTSchemaSource.create(source.getIdentifier().getName(), ctx);
73 public int getCost() {
74 // We perform a direct translation, so the cost is 1.