1a6ddb260ad694b3eb56ba8a78431a96319a5175
[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 com.google.common.io.Files;
11 import java.io.File;
12 import java.io.FileFilter;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.nio.file.Path;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Set;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.YangConstants;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
31 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
32 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
40 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.xml.sax.SAXException;
44
45 public final class StmtTestUtils {
46
47     public static final FileFilter YANG_FILE_FILTER =
48         file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
49
50     public static final FileFilter YIN_FILE_FILTER =
51         file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
52
53     private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
54
55     private StmtTestUtils() {
56
57     }
58
59     public static void log(final Throwable exception, final String indent) {
60         LOG.debug("{}{}", indent, exception.getMessage());
61
62         final Throwable[] suppressed = exception.getSuppressed();
63         for (final Throwable throwable : suppressed) {
64             log(throwable, indent + "        ");
65         }
66     }
67
68     public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
69         final List<Module> result = new ArrayList<>();
70         for (final Module module : modules) {
71             if (module.getName().equals(moduleName)) {
72                 result.add(module);
73             }
74         }
75         return result;
76     }
77
78     public static YangStatementStreamSource sourceForResource(final String resourceName) {
79         try {
80             return YangStatementStreamSource.create(YangTextSchemaSource.forPath(Path.of(
81                 StmtTestUtils.class.getResource(resourceName).toURI())));
82         } catch (IOException | YangSyntaxErrorException | URISyntaxException e) {
83             throw new IllegalArgumentException("Failed to create source", e);
84         }
85     }
86
87     @Deprecated(forRemoval = true)
88     // TestUtils.parseYangSource() instead, but callers need also further cleanup
89     public static EffectiveModelContext parseYangSource(final String yangSourcePath) throws ReactorException,
90             URISyntaxException, IOException, YangSyntaxErrorException {
91         return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, null);
92     }
93
94     public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
95             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
96         return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, supportedFeatures);
97     }
98
99     public static EffectiveModelContext parseYangSource(final String yangSourcePath,
100             final YangParserConfiguration config, final Set<QName> supportedFeatures)
101                     throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
102         return parseYangSources(config, supportedFeatures,
103             new File(StmtTestUtils.class.getResource(yangSourcePath).toURI()));
104     }
105
106     public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
107             throws ReactorException {
108         return parseYangSources(YangParserConfiguration.DEFAULT, null, sources);
109     }
110
111     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
112             final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
113         return parseYangSources(config, supportedFeatures, Arrays.asList(sources));
114     }
115
116     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
117             final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
118             throws ReactorException {
119         final BuildAction build = getReactor(config).newBuild().addSources(sources);
120         if (supportedFeatures != null) {
121             build.setSupportedFeatures(supportedFeatures);
122         }
123         return build.buildEffective();
124     }
125
126     public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
127             YangSyntaxErrorException {
128         return parseYangSources(YangParserConfiguration.DEFAULT, null, files);
129     }
130
131     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
132             final Set<QName> supportedFeatures, final File... files) throws  ReactorException, IOException,
133             YangSyntaxErrorException {
134
135         final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
136         for (File file : files) {
137             sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
138         }
139
140         return parseYangSources(config, supportedFeatures, sources);
141     }
142
143     public static EffectiveModelContext parseYangSources(final Collection<File> files) throws ReactorException,
144             IOException, YangSyntaxErrorException {
145         return parseYangSources(files, YangParserConfiguration.DEFAULT);
146     }
147
148     public static EffectiveModelContext parseYangSources(final Collection<File> files,
149             final YangParserConfiguration config) throws ReactorException, IOException, YangSyntaxErrorException {
150         return parseYangSources(config, null, files.toArray(new File[0]));
151     }
152
153     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
154             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
155         return parseYangSources(yangSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
156     }
157
158     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
159             final YangParserConfiguration config) throws ReactorException, URISyntaxException, IOException,
160             YangSyntaxErrorException {
161         return parseYangSources(yangSourcesDirectoryPath, null, config);
162     }
163
164     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
165             final Set<QName> supportedFeatures, final YangParserConfiguration config) throws ReactorException,
166             URISyntaxException, IOException, YangSyntaxErrorException {
167
168         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
169         final File testSourcesDir = new File(resourceDir.toURI());
170
171         return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
172     }
173
174     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
175             throws URISyntaxException, SAXException, IOException, ReactorException {
176         return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
177     }
178
179     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
180             final YangParserConfiguration config) throws URISyntaxException, SAXException, IOException,
181             ReactorException {
182         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
183         final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
184         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
185         for (int i = 0; i < files.length; i++) {
186             final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
187
188             sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
189                 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
190         }
191
192         return parseYinSources(config, sources);
193     }
194
195     public static EffectiveModelContext parseYinSources(final YangParserConfiguration config,
196             final StatementStreamSource... sources) throws ReactorException {
197         return getReactor(config)
198             .newBuild()
199             .addSources(sources)
200             .buildEffective();
201     }
202
203     public static Module findImportedModule(final SchemaContext context, final Module rootModule,
204             final String importedModuleName) {
205         ModuleImport requestedModuleImport = null;
206         for (final ModuleImport moduleImport : rootModule.getImports()) {
207             if (moduleImport.getModuleName().equals(importedModuleName)) {
208                 requestedModuleImport = moduleImport;
209                 break;
210             }
211         }
212
213         return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
214                 .orElse(null);
215     }
216
217     private static CrossSourceStatementReactor getReactor(final YangParserConfiguration config) {
218         return YangParserConfiguration.DEFAULT.equals(config) ? RFC7950Reactors.defaultReactor()
219             : RFC7950Reactors.defaultReactorBuilder(config).build();
220     }
221 }