Bug 6869: [Yang 1.1] Allow if-feature in bit, enum, refine & identity
[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.net.URISyntaxException;
15 import java.net.URL;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Set;
20 import java.util.function.Predicate;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.repo.api.IfFeaturePredicates;
28 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YinStatementSourceImpl;
36 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class StmtTestUtils {
41
42     final public static FileFilter YANG_FILE_FILTER = file -> {
43         final String name = file.getName().toLowerCase();
44         return name.endsWith(".yang") && file.isFile();
45     };
46
47     final public static FileFilter YIN_FILE_FILTER = file -> {
48         final String name = file.getName().toLowerCase();
49         return name.endsWith(".xml") && file.isFile();
50     };
51
52     private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
53
54     private StmtTestUtils() {
55
56     }
57
58     public static void log(final Throwable e, final String indent) {
59         LOG.debug(indent + e.getMessage());
60
61         final Throwable[] suppressed = e.getSuppressed();
62         for (final Throwable throwable : suppressed) {
63             log(throwable, indent + "        ");
64         }
65     }
66
67     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
68         final List<Module> result = new ArrayList<>();
69         for (final Module module : modules) {
70             if (module.getName().equals(moduleName)) {
71                 result.add(module);
72             }
73         }
74         return result;
75     }
76
77     public static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final YangStatementSourceImpl... sources) {
78         for (final YangStatementSourceImpl source : sources) {
79             reactor.addSource(source);
80         }
81     }
82
83     public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
84         LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
85         final Set<Module> submodules = module.getSubmodules();
86         for (final Module submodule : submodules) {
87             printReferences(submodule, true, indent + "      ");
88             printChilds(submodule.getChildNodes(), indent + "            ");
89         }
90     }
91
92     public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
93
94         for (final DataSchemaNode child : childNodes) {
95             LOG.debug(indent + "Child " + child.getQName().getLocalName());
96             if (child instanceof DataNodeContainer) {
97                 printChilds(((DataNodeContainer) child).getChildNodes(), indent + "      ");
98             }
99         }
100     }
101
102     public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
103             ReactorException {
104         return parseYangSources(StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES, sources);
105     }
106
107     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final Predicate<QName> ifFeaturePredicate, final StatementStreamSource... sources)
108             throws SourceException, ReactorException {
109
110         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode, ifFeaturePredicate);
111         reactor.addSources(sources);
112
113         return reactor.buildEffective();
114     }
115
116     public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
117             FileNotFoundException {
118         return parseYangSources(StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES, files);
119     }
120
121     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final Predicate<QName> ifFeaturePredicate, final File... files) throws SourceException,
122             ReactorException, FileNotFoundException {
123
124         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
125
126         for (int i = 0; i < files.length; i++) {
127             sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
128         }
129
130         return parseYangSources(statementParserMode, ifFeaturePredicate, sources);
131     }
132
133     public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
134             FileNotFoundException {
135         return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
136     }
137
138     public static SchemaContext parseYangSources(final Collection<File> files, final StatementParserMode statementParserMode)
139             throws SourceException, ReactorException, FileNotFoundException {
140         return parseYangSources(statementParserMode, IfFeaturePredicates.ALL_FEATURES, files.toArray(new File[files.size()]));
141     }
142
143     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
144             ReactorException, FileNotFoundException, URISyntaxException {
145         return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
146     }
147
148     public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
149             FileNotFoundException, URISyntaxException {
150         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES);
151     }
152
153     public static SchemaContext parseYangSource(final String yangSourcePath, final Predicate<QName> ifFeaturesPredicate)
154             throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
155         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, ifFeaturesPredicate);
156     }
157
158     public static SchemaContext parseYangSource(final String yangSourcePath,
159             final StatementParserMode statementParserMode, final Predicate<QName> ifFeaturePredicate)
160             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
161         final URL source = StmtTestUtils.class.getResource(yangSourcePath);
162         final File sourceFile = new File(source.toURI());
163         return parseYangSources(statementParserMode, ifFeaturePredicate, sourceFile);
164     }
165
166     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath, final StatementParserMode statementParserMode)
167             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
168
169         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
170         final File testSourcesDir = new File(resourceDir.toURI());
171
172         return parseYangSources(statementParserMode, IfFeaturePredicates.ALL_FEATURES, testSourcesDir.listFiles(YANG_FILE_FILTER));
173     }
174
175     public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
176             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
177
178         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
179         final File testSourcesDir = new File(resourceDir.toURI());
180
181         return parseYinSources(statementParserMode, testSourcesDir.listFiles(YIN_FILE_FILTER));
182     }
183
184     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final File... files) throws SourceException,
185             ReactorException, FileNotFoundException {
186
187         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
188
189         for (int i = 0; i < files.length; i++) {
190             sources[i] = new YinStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
191         }
192
193         return parseYinSources(statementParserMode, sources);
194     }
195
196     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
197             throws SourceException, ReactorException {
198
199         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
200         reactor.addSources(sources);
201
202         return reactor.buildEffective();
203     }
204
205     public static Module findImportedModule(final SchemaContext context, final Module rootModule, final String importedModuleName) {
206         ModuleImport requestedModuleImport = null;
207         final Set<ModuleImport> rootImports = rootModule.getImports();
208         for (final ModuleImport moduleImport : rootImports) {
209             if (moduleImport.getModuleName().equals(importedModuleName)) {
210                 requestedModuleImport = moduleImport;
211                 break;
212             }
213         }
214
215         final Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
216                 requestedModuleImport.getRevision());
217         return importedModule;
218     }
219 }