Fix checkstyle in yang-parser-impl and enable enforcement
[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 com.google.common.io.Files;
12 import java.io.File;
13 import java.io.FileFilter;
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.SourceIdentifier;
31 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
34 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinStatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.rfc6020.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.rfc6020.YangInferencePipeline;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.xml.sax.SAXException;
44
45 public 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 Set<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 StatementStreamSource sourceForResource(final String resourceName) {
79         try {
80             return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
81         } catch (IOException | YangSyntaxErrorException e) {
82             throw new IllegalArgumentException("Failed to create source", e);
83         }
84     }
85
86     public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
87         LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
88         final Set<Module> submodules = module.getSubmodules();
89         for (final Module submodule : submodules) {
90             printReferences(submodule, true, indent + "      ");
91             printChilds(submodule.getChildNodes(), indent + "            ");
92         }
93     }
94
95     public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
96
97         for (final DataSchemaNode child : childNodes) {
98             LOG.debug(indent + "Child " + child.getQName().getLocalName());
99             if (child instanceof DataNodeContainer) {
100                 printChilds(((DataNodeContainer) child).getChildNodes(), indent + "      ");
101             }
102         }
103     }
104
105     public static SchemaContext parseYangSource(final String yangSourcePath) throws ReactorException,
106             URISyntaxException, IOException, YangSyntaxErrorException {
107         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
108     }
109
110     public static SchemaContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
111             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
112         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, supportedFeatures);
113     }
114
115     public static SchemaContext parseYangSource(final String yangSourcePath,
116             final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
117                     throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
118         final URL source = StmtTestUtils.class.getResource(yangSourcePath);
119         final File sourceFile = new File(source.toURI());
120         return parseYangSources(statementParserMode, supportedFeatures, sourceFile);
121     }
122
123     public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws ReactorException {
124         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
125     }
126
127     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
128             final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
129         return parseYangSources(statementParserMode, supportedFeatures, Arrays.asList(sources));
130     }
131
132     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
133             final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
134             throws ReactorException {
135         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
136             statementParserMode);
137         reactor.addSources(sources);
138         if (supportedFeatures != null) {
139             reactor.setSupportedFeatures(supportedFeatures);
140         }
141
142         return reactor.buildEffective();
143     }
144
145     public static SchemaContext parseYangSources(final File... files) throws ReactorException, IOException,
146             YangSyntaxErrorException {
147         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
148     }
149
150     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
151             final Set<QName> supportedFeatures, final File... files) throws  ReactorException, IOException,
152             YangSyntaxErrorException {
153
154         final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
155         for (File file : files) {
156             sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
157         }
158
159         return parseYangSources(statementParserMode, supportedFeatures, sources);
160     }
161
162     public static SchemaContext parseYangSources(final Collection<File> files) throws ReactorException, IOException,
163             YangSyntaxErrorException {
164         return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
165     }
166
167     public static SchemaContext parseYangSources(final Collection<File> files,
168             final StatementParserMode statementParserMode) throws ReactorException, IOException,
169             YangSyntaxErrorException {
170         return parseYangSources(statementParserMode, null, files.toArray(new File[files.size()]));
171     }
172
173     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws
174             ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
175         return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
176     }
177
178     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
179             final StatementParserMode statementParserMode) throws ReactorException, URISyntaxException, IOException,
180             YangSyntaxErrorException {
181         return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
182     }
183
184     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
185             final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws ReactorException,
186             URISyntaxException, IOException, YangSyntaxErrorException {
187
188         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
189         final File testSourcesDir = new File(resourceDir.toURI());
190
191         return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
192     }
193
194     public static SchemaContext parseYangSources(final String yangFilesDirectoryPath,
195             final String yangLibsDirectoryPath)
196             throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
197         return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
198     }
199
200     public static SchemaContext parseYangSources(final String yangFilesDirectoryPath,
201             final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
202             ReactorException, IOException, YangSyntaxErrorException {
203         final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
204         final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
205
206         return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
207                 supportedFeatures);
208     }
209
210     private static SchemaContext parseYangSources(final File[] yangFiles, final File[] libFiles,
211             final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
212         final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
213         for (int i = 0; i < yangFiles.length; i++) {
214             yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
215         }
216
217         final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
218         for (int i = 0; i < libFiles.length; i++) {
219             libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
220         }
221
222         return parseYangSources(yangSources, libSources, supportedFeatures);
223     }
224
225     private static SchemaContext parseYangSources(final StatementStreamSource[] yangSources,
226             final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
227
228         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
229         reactor.addSources(yangSources);
230         reactor.addLibSources(libSources);
231         if (supportedFeatures != null) {
232             reactor.setSupportedFeatures(supportedFeatures);
233         }
234
235         return reactor.buildEffective();
236     }
237
238     public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath,
239             final StatementParserMode statementParserMode) throws URISyntaxException, SAXException, IOException,
240             ReactorException {
241         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
242         final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
243         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
244         for (int i = 0; i < files.length; i++) {
245             final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
246
247             sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
248                 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
249         }
250
251         return parseYinSources(statementParserMode, sources);
252     }
253
254     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode,
255             final StatementStreamSource... sources) throws ReactorException {
256
257         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
258                 .newBuild(statementParserMode);
259         reactor.addSources(sources);
260
261         return reactor.buildEffective();
262     }
263
264     public static Module findImportedModule(final SchemaContext context, final Module rootModule,
265             final String importedModuleName) {
266         ModuleImport requestedModuleImport = null;
267         final Set<ModuleImport> rootImports = rootModule.getImports();
268         for (final ModuleImport moduleImport : rootImports) {
269             if (moduleImport.getModuleName().equals(importedModuleName)) {
270                 requestedModuleImport = moduleImport;
271                 break;
272             }
273         }
274
275         return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
276                 .orElse(null);
277     }
278 }