72acef86ac112d0dcbcec47dcae55b18bde5a019
[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     public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
88             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
89         return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, supportedFeatures);
90     }
91
92     public static EffectiveModelContext parseYangSource(final String yangSourcePath,
93             final YangParserConfiguration config, final Set<QName> supportedFeatures)
94                     throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
95         return parseYangSources(config, supportedFeatures,
96             new File(StmtTestUtils.class.getResource(yangSourcePath).toURI()));
97     }
98
99     public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
100             throws ReactorException {
101         return parseYangSources(YangParserConfiguration.DEFAULT, null, sources);
102     }
103
104     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
105             final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
106         return parseYangSources(config, supportedFeatures, Arrays.asList(sources));
107     }
108
109     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
110             final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
111             throws ReactorException {
112         final BuildAction build = getReactor(config).newBuild().addSources(sources);
113         if (supportedFeatures != null) {
114             build.setSupportedFeatures(supportedFeatures);
115         }
116         return build.buildEffective();
117     }
118
119     public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
120             YangSyntaxErrorException {
121         return parseYangSources(YangParserConfiguration.DEFAULT, null, files);
122     }
123
124     public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
125             final Set<QName> supportedFeatures, final File... files) throws  ReactorException, IOException,
126             YangSyntaxErrorException {
127
128         final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
129         for (File file : files) {
130             sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
131         }
132
133         return parseYangSources(config, supportedFeatures, sources);
134     }
135
136     public static EffectiveModelContext parseYangSources(final Collection<File> files) throws ReactorException,
137             IOException, YangSyntaxErrorException {
138         return parseYangSources(files, YangParserConfiguration.DEFAULT);
139     }
140
141     public static EffectiveModelContext parseYangSources(final Collection<File> files,
142             final YangParserConfiguration config) throws ReactorException, IOException, YangSyntaxErrorException {
143         return parseYangSources(config, null, files.toArray(new File[0]));
144     }
145
146     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
147             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
148         return parseYangSources(yangSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
149     }
150
151     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
152             final YangParserConfiguration config) throws ReactorException, URISyntaxException, IOException,
153             YangSyntaxErrorException {
154         return parseYangSources(yangSourcesDirectoryPath, null, config);
155     }
156
157     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
158             final Set<QName> supportedFeatures, final YangParserConfiguration config) throws ReactorException,
159             URISyntaxException, IOException, YangSyntaxErrorException {
160
161         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
162         final File testSourcesDir = new File(resourceDir.toURI());
163
164         return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
165     }
166
167     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
168             throws URISyntaxException, SAXException, IOException, ReactorException {
169         return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
170     }
171
172     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
173             final YangParserConfiguration config) throws URISyntaxException, SAXException, IOException,
174             ReactorException {
175         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
176         final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
177         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
178         for (int i = 0; i < files.length; i++) {
179             final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
180
181             sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
182                 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
183         }
184
185         return parseYinSources(config, sources);
186     }
187
188     public static EffectiveModelContext parseYinSources(final YangParserConfiguration config,
189             final StatementStreamSource... sources) throws ReactorException {
190         return getReactor(config)
191             .newBuild()
192             .addSources(sources)
193             .buildEffective();
194     }
195
196     public static Module findImportedModule(final SchemaContext context, final Module rootModule,
197             final String importedModuleName) {
198         ModuleImport requestedModuleImport = null;
199         for (final ModuleImport moduleImport : rootModule.getImports()) {
200             if (moduleImport.getModuleName().equals(importedModuleName)) {
201                 requestedModuleImport = moduleImport;
202                 break;
203             }
204         }
205
206         return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
207                 .orElse(null);
208     }
209
210     private static CrossSourceStatementReactor getReactor(final YangParserConfiguration config) {
211         return YangParserConfiguration.DEFAULT.equals(config) ? RFC7950Reactors.defaultReactor()
212             : RFC7950Reactors.defaultReactorBuilder(config).build();
213     }
214 }