Expose FileYangTextSource
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / StmtTestUtils.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.yangtools.yang.stmt;
9
10 import java.io.File;
11 import java.io.FileFilter;
12 import java.io.IOException;
13 import java.net.URISyntaxException;
14 import java.net.URL;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Set;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.YangConstants;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
26 import org.opendaylight.yangtools.yang.model.spi.source.FileYangTextSource;
27 import org.opendaylight.yangtools.yang.model.spi.source.FileYinTextSource;
28 import org.opendaylight.yangtools.yang.model.spi.source.URLYangTextSource;
29 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
30 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
36 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
37 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.xml.sax.SAXException;
42
43 public final class StmtTestUtils {
44
45     public static final FileFilter YANG_FILE_FILTER =
46         file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
47
48     public static final FileFilter YIN_FILE_FILTER =
49         file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
50
51     private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
52
53     private StmtTestUtils() {
54
55     }
56
57     public static void log(final Throwable exception, final String indent) {
58         LOG.debug("{}{}", indent, exception.getMessage());
59
60         final Throwable[] suppressed = exception.getSuppressed();
61         for (final Throwable throwable : suppressed) {
62             log(throwable, indent + "        ");
63         }
64     }
65
66     public static YangStatementStreamSource sourceForResource(final String resourceName) {
67         try {
68             return YangStatementStreamSource.create(new URLYangTextSource(
69                 StmtTestUtils.class.getResource(resourceName)));
70         } catch (IOException | YangSyntaxErrorException e) {
71             throw new IllegalArgumentException("Failed to create source", e);
72         }
73     }
74
75     public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
76             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
77         return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, supportedFeatures);
78     }
79
80     public static EffectiveModelContext parseYangSource(final String yangSourcePath,
81             final YangParserConfiguration config, final Set<QName> supportedFeatures)
82                     throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
83         return parseYangSources(config, supportedFeatures,
84             new File(StmtTestUtils.class.getResource(yangSourcePath).toURI()));
85     }
86
87     public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
88             throws ReactorException {
89         return parseYangSources(YangParserConfiguration.DEFAULT, null, sources);
90     }
91
92     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
93             final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
94         return parseYangSources(config, supportedFeatures, Arrays.asList(sources));
95     }
96
97     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
98             final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
99             throws ReactorException {
100         final BuildAction build = getReactor(config).newBuild().addSources(sources);
101         if (supportedFeatures != null) {
102             build.setSupportedFeatures(FeatureSet.of(supportedFeatures));
103         }
104         return build.buildEffective();
105     }
106
107     public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
108             YangSyntaxErrorException {
109         return parseYangSources(YangParserConfiguration.DEFAULT, null, files);
110     }
111
112     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
113             final Set<QName> supportedFeatures, final File... files)
114                 throws ReactorException, IOException, YangSyntaxErrorException {
115         final var sources = new ArrayList<YangStatementStreamSource>(files.length);
116         for (var file : files) {
117             sources.add(YangStatementStreamSource.create(new FileYangTextSource(file.toPath())));
118         }
119         return parseYangSources(config, supportedFeatures, sources);
120     }
121
122     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
123             final YangParserConfiguration config) throws ReactorException, URISyntaxException, IOException,
124             YangSyntaxErrorException {
125         return parseYangSources(yangSourcesDirectoryPath, null, config);
126     }
127
128     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
129             final Set<QName> supportedFeatures, final YangParserConfiguration config) throws ReactorException,
130             URISyntaxException, IOException, YangSyntaxErrorException {
131
132         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
133         final File testSourcesDir = new File(resourceDir.toURI());
134
135         return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
136     }
137
138     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
139             throws URISyntaxException, SAXException, IOException, ReactorException {
140         return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
141     }
142
143     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
144             final YangParserConfiguration config)
145                 throws URISyntaxException, SAXException, IOException, ReactorException {
146         final var resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
147         final var files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
148         final var sources = new StatementStreamSource[files.length];
149         for (int i = 0; i < files.length; i++) {
150             sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
151                 new FileYinTextSource(files[i].toPath())));
152         }
153
154         return parseYinSources(config, sources);
155     }
156
157     public static EffectiveModelContext parseYinSources(final YangParserConfiguration config,
158             final StatementStreamSource... sources) throws ReactorException {
159         return getReactor(config)
160             .newBuild()
161             .addSources(sources)
162             .buildEffective();
163     }
164
165     public static Module findImportedModule(final SchemaContext context, final Module rootModule,
166             final String importedModuleName) {
167         ModuleImport requestedModuleImport = null;
168         for (final ModuleImport moduleImport : rootModule.getImports()) {
169             if (moduleImport.getModuleName().equals(importedModuleName)) {
170                 requestedModuleImport = moduleImport;
171                 break;
172             }
173         }
174
175         return context.findModule(requestedModuleImport.getModuleName().getLocalName(),
176                     requestedModuleImport.getRevision())
177                 .orElse(null);
178     }
179
180     private static CrossSourceStatementReactor getReactor(final YangParserConfiguration config) {
181         return YangParserConfiguration.DEFAULT.equals(config) ? RFC7950Reactors.defaultReactor()
182             : RFC7950Reactors.defaultReactorBuilder(config).build();
183     }
184 }