Bug 8307: Add the option for activating deviation statements
[yangtools.git] / yang / yang-parser-impl / 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
9 package org.opendaylight.yangtools.yang.stmt;
10
11 import java.io.File;
12 import java.io.FileFilter;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.net.URL;
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.Module;
27 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
30 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
31 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
32 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
38 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YinStatementSourceImpl;
39 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public 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 e, final String indent) {
58         LOG.debug(indent + e.getMessage());
59
60         final Throwable[] suppressed = e.getSuppressed();
61         for (final Throwable throwable : suppressed) {
62             log(throwable, indent + "        ");
63         }
64     }
65
66     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
67         final List<Module> result = new ArrayList<>();
68         for (final Module module : modules) {
69             if (module.getName().equals(moduleName)) {
70                 result.add(module);
71             }
72         }
73         return result;
74     }
75
76     public static StatementStreamSource sourceForResource(final String resourceName) {
77         try {
78             return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
79         } catch (IOException | YangSyntaxErrorException e) {
80             throw new IllegalArgumentException("Failed to create source", e);
81         }
82     }
83
84     public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
85         LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
86         final Set<Module> submodules = module.getSubmodules();
87         for (final Module submodule : submodules) {
88             printReferences(submodule, true, indent + "      ");
89             printChilds(submodule.getChildNodes(), indent + "            ");
90         }
91     }
92
93     public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
94
95         for (final DataSchemaNode child : childNodes) {
96             LOG.debug(indent + "Child " + child.getQName().getLocalName());
97             if (child instanceof DataNodeContainer) {
98                 printChilds(((DataNodeContainer) child).getChildNodes(), indent + "      ");
99             }
100         }
101     }
102
103     public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
104             ReactorException {
105         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
106     }
107
108     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
109             final Set<QName> supportedFeatures, final StatementStreamSource... sources)
110             throws SourceException, ReactorException {
111         return parseYangSources(statementParserMode, supportedFeatures, Arrays.asList(sources));
112     }
113
114     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
115             final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
116             throws SourceException, ReactorException {
117         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
118             statementParserMode);
119         reactor.addSources(sources);
120         if (supportedFeatures != null) {
121             reactor.setSupportedFeatures(supportedFeatures);
122         }
123
124         return reactor.buildEffective();
125     }
126
127     public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
128             IOException, YangSyntaxErrorException {
129         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
130     }
131
132     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
133             final Set<QName> supportedFeatures, final File... files) throws SourceException,
134             ReactorException, IOException, YangSyntaxErrorException {
135
136         final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
137         for (File file : files) {
138             sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
139         }
140
141         return parseYangSources(statementParserMode, supportedFeatures, sources);
142     }
143
144     public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
145             IOException, YangSyntaxErrorException {
146         return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
147     }
148
149     public static SchemaContext parseYangSources(final Collection<File> files, final StatementParserMode statementParserMode)
150             throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
151         return parseYangSources(statementParserMode, null, files.toArray(new File[files.size()]));
152     }
153
154     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
155             ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
156         return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
157     }
158
159     public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
160             URISyntaxException, IOException, YangSyntaxErrorException {
161         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
162     }
163
164     public static SchemaContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
165             throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
166         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, supportedFeatures);
167     }
168
169     public static SchemaContext parseYangSource(final String yangSourcePath,
170             final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
171             throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
172         final URL source = StmtTestUtils.class.getResource(yangSourcePath);
173         final File sourceFile = new File(source.toURI());
174         return parseYangSources(statementParserMode, supportedFeatures, sourceFile);
175     }
176
177     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
178             final StatementParserMode statementParserMode) throws SourceException, ReactorException,
179             URISyntaxException, IOException, YangSyntaxErrorException {
180         return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
181     }
182
183     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
184             final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws SourceException,
185             ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
186
187         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
188         final File testSourcesDir = new File(resourceDir.toURI());
189
190         return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
191     }
192
193     public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
194             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
195
196         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
197         final File testSourcesDir = new File(resourceDir.toURI());
198
199         return parseYinSources(statementParserMode, testSourcesDir.listFiles(YIN_FILE_FILTER));
200     }
201
202     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final File... files)
203             throws SourceException, ReactorException, FileNotFoundException {
204
205         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
206
207         for (int i = 0; i < files.length; i++) {
208             sources[i] = new YinStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
209         }
210
211         return parseYinSources(statementParserMode, sources);
212     }
213
214     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
215             throws SourceException, ReactorException {
216
217         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
218         reactor.addSources(sources);
219
220         return reactor.buildEffective();
221     }
222
223     public static Module findImportedModule(final SchemaContext context, final Module rootModule, final String importedModuleName) {
224         ModuleImport requestedModuleImport = null;
225         final Set<ModuleImport> rootImports = rootModule.getImports();
226         for (final ModuleImport moduleImport : rootImports) {
227             if (moduleImport.getModuleName().equals(importedModuleName)) {
228                 requestedModuleImport = moduleImport;
229                 break;
230             }
231         }
232
233         final Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
234                 requestedModuleImport.getRevision());
235         return importedModule;
236     }
237
238     public static SchemaContext parseYangSources(final String yangFilesDirectoryPath, final String yangLibsDirectoryPath)
239             throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
240         return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
241     }
242
243     public static SchemaContext parseYangSources(final String yangFilesDirectoryPath,
244             final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
245             ReactorException, IOException, YangSyntaxErrorException {
246         final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
247         final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
248
249         return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
250                 supportedFeatures);
251     }
252
253     private static SchemaContext parseYangSources(final File[] yangFiles, final File[] libFiles,
254             final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
255         final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
256         for (int i = 0; i < yangFiles.length; i++) {
257             yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
258         }
259
260         final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
261         for (int i = 0; i < libFiles.length; i++) {
262             libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
263         }
264
265         return parseYangSources(yangSources, libSources, supportedFeatures);
266     }
267
268     private static SchemaContext parseYangSources(final StatementStreamSource[] yangSources,
269             final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
270
271         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
272         reactor.addSources(yangSources);
273         reactor.addLibSources(libSources);
274         if (supportedFeatures != null) {
275             reactor.setSupportedFeatures(supportedFeatures);
276         }
277
278         return reactor.buildEffective();
279     }
280 }