YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / repo / 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.rfc7950.repo;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.util.concurrent.Futures;
13 import java.io.IOException;
14 import org.antlr.v4.runtime.ParserRuleContext;
15 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
16 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
17 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
18 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
19 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
20 import org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * A {@link SchemaSourceTransformer} which handles translation of models from
26  * {@link YangTextSchemaSource} representation into {@link ASTSchemaSource}.
27  */
28 @Beta
29 public final class TextToASTTransformer extends SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
30     private static final Logger LOG = LoggerFactory.getLogger(TextToASTTransformer.class);
31
32     private TextToASTTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
33         super(provider, YangTextSchemaSource.class, consumer, ASTSchemaSource.class,
34             input -> Futures.immediateFuture(transformText(input)));
35     }
36
37     public static TextToASTTransformer create(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
38         return new TextToASTTransformer(provider, consumer);
39     }
40
41     public static ASTSchemaSource transformText(final YangTextSchemaSource text) throws SchemaSourceException,
42             IOException, YangSyntaxErrorException {
43         final YangStatementStreamSource src = YangStatementStreamSource.create(text);
44         final ParserRuleContext ctx = src.getYangAST();
45         LOG.debug("Model {} parsed successfully", text);
46
47         // TODO: missing validation (YangModelBasicValidationListener should be re-implemented to new parser)
48
49         return ASTSchemaSource.create(text.getIdentifier(), text.getSymbolicName().orElse(null), ctx);
50     }
51 }