b9b48ac2dade1feacbd0f04006c59795d71cc37b
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / util / TextToASTTransformer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.yang.parser.util;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.util.concurrent.CheckedFuture;
13 import com.google.common.util.concurrent.Futures;
14 import java.io.IOException;
15 import java.util.Optional;
16 import javax.annotation.Nonnull;
17 import org.antlr.v4.runtime.ParserRuleContext;
18 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
19 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
20 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
21 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
22 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
23 import org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer;
24 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * A {@link SchemaSourceTransformer} which handles translation of models from
30  * {@link YangTextSchemaSource} representation into {@link ASTSchemaSource}.
31  */
32 @Beta
33 public final class TextToASTTransformer extends SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
34
35     /**
36      * @deprecated Use {@link TextToASTTransformer#transformText(YangTextSchemaSource)} instead.
37      */
38     @Deprecated
39     public static final class TextToASTTransformation implements Transformation<YangTextSchemaSource, ASTSchemaSource> {
40         @Override
41         public CheckedFuture<ASTSchemaSource, SchemaSourceException> apply(@Nonnull final YangTextSchemaSource input)
42                 throws IOException, YangSyntaxErrorException {
43             final YangStatementStreamSource src = YangStatementStreamSource.create(input);
44
45             final ParserRuleContext ctx = src.getYangAST();
46             LOG.debug("Model {} parsed successfully", input);
47
48             //:TODO missing validation (YangModelBasicValidationListener should be re-implemented to new parser)
49
50             final Optional<String> opt = input.getSymbolicName();
51             final ASTSchemaSource result = opt.isPresent()
52                     ? ASTSchemaSource.create(opt.get(), input.getIdentifier(), ctx)
53                             : ASTSchemaSource.create(input.getIdentifier(), ctx);
54
55             return Futures.immediateCheckedFuture(result);
56         }
57     }
58
59     /**
60      * @deprecated Use {@link TextToASTTransformer#transformText(YangTextSchemaSource)} instead.
61      */
62     @Deprecated
63     public static final TextToASTTransformation TRANSFORMATION = new TextToASTTransformation();
64
65     private static final Logger LOG = LoggerFactory.getLogger(TextToASTTransformer.class);
66
67     private TextToASTTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
68         super(provider, YangTextSchemaSource.class, consumer, ASTSchemaSource.class, TRANSFORMATION);
69     }
70
71     public static TextToASTTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
72         return new TextToASTTransformer(provider, consumer);
73     }
74
75     public static ASTSchemaSource transformText(final YangTextSchemaSource text) throws SchemaSourceException,
76             IOException, YangSyntaxErrorException {
77         return TRANSFORMATION.apply(text).checkedGet();
78     }
79 }