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