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