Fix license header violations in yang-parser-impl
[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.base.Charsets;
13 import com.google.common.io.ByteSource;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import com.google.common.util.concurrent.Futures;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19
20 import org.antlr.v4.runtime.tree.ParseTreeWalker;
21 import org.opendaylight.yangtools.antlrv4.code.gen.YangParser.YangContext;
22 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
23 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
27 import org.opendaylight.yangtools.yang.model.repo.util.SchemaSourceTransformer;
28 import org.opendaylight.yangtools.yang.parser.impl.YangModelBasicValidationListener;
29 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * A {@link SchemaSourceTransformer} which handles translation of models from
35  * {@link YangTextSchemaSource} representation into {@link ASTSchemaSource}.
36  */
37 @Beta
38 public final class TextToASTTransformer extends SchemaSourceTransformer<YangTextSchemaSource, ASTSchemaSource> {
39     public static final class TextToASTTransformation implements Transformation<YangTextSchemaSource, ASTSchemaSource> {
40         @Override
41         public CheckedFuture<ASTSchemaSource, SchemaSourceException> apply(final YangTextSchemaSource input) throws IOException, YangSyntaxErrorException {
42             try (InputStream is = input.openStream()) {
43                 final YangContext ctx = YangParserImpl.parseYangSource(is);
44                 LOG.debug("Model {} parsed successfully", input);
45
46                 final ParseTreeWalker walker = new ParseTreeWalker();
47                 final YangModelBasicValidationListener validator = new YangModelBasicValidationListener();
48                 walker.walk(validator, ctx);
49                 LOG.debug("Model {} validated successfully", input);
50
51                 // Backwards compatibility
52                 final String text = new ByteSource() {
53                     @Override
54                     public InputStream openStream() throws IOException {
55                         return input.openStream();
56                     }
57                 }.asCharSource(Charsets.UTF_8).read();
58
59                 return Futures.immediateCheckedFuture(ASTSchemaSource.create(input.getIdentifier().getName(), ctx, text));
60             }
61         }
62     }
63
64     public static final TextToASTTransformation TRANSFORMATION = new TextToASTTransformation();
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 }