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