2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
10 import static org.junit.Assert.assertEquals;
13 import java.io.IOException;
15 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Optional;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.YangConstants;
25 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
26 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
28 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
36 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
37 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
38 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
39 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinStatementStreamSource;
40 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinTextToDomTransformer;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
42 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
43 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
44 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
45 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.xml.sax.SAXException;
51 public final class TestUtils {
52 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
57 public static SchemaContext loadModules(final URI resourceDirectory)
58 throws ReactorException, IOException, YangSyntaxErrorException {
59 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
61 File[] files = new File(resourceDirectory).listFiles();
63 for (File file : files) {
64 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
65 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
67 LOG.info("Ignoring non-yang file {}", file);
71 return reactor.buildEffective();
74 public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
75 throws IOException, ReactorException, YangSyntaxErrorException {
76 final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
78 for (String resourceName : resourceNames) {
79 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass, resourceName)));
82 return reactor.buildEffective();
85 public static SchemaContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException,
87 final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
89 for (File file : new File(resourceDirectory).listFiles()) {
90 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
91 YinTextSchemaSource.forFile(file))));
94 return reactor.buildEffective();
97 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException, IOException {
98 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
99 reactor.addSources(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)));
100 SchemaContext ctx = reactor.buildEffective();
101 return ctx.getModules().iterator().next();
104 public static Optional<Module> findModule(final SchemaContext context, final String moduleName) {
105 return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
108 public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
109 ModuleImport result = null;
110 for (ModuleImport moduleImport : imports) {
111 if (moduleImport.getPrefix().equals(prefix)) {
112 result = moduleImport;
119 public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
120 TypeDefinition<?> result = null;
121 for (TypeDefinition<?> td : typedefs) {
122 if (td.getQName().getLocalName().equals(name)) {
130 public static SchemaPath createPath(final boolean absolute, final QNameModule module, final String... names) {
131 List<QName> path = new ArrayList<>(names.length);
132 for (String name : names) {
133 path.add(QName.create(module, name));
135 return SchemaPath.create(path, absolute);
139 * Test if node has augmenting flag set to expected value. In case this is
140 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
147 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
148 assertEquals(expected, node.isAugmenting());
149 if (node instanceof DataNodeContainer) {
150 for (DataSchemaNode child : ((DataNodeContainer) node)
152 checkIsAugmenting(child, expected);
154 } else if (node instanceof ChoiceSchemaNode) {
155 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
156 checkIsAugmenting(caseNode, expected);
162 * Check if node has addedByUses flag set to expected value. In case this is
163 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
170 public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
171 assertEquals(expected, node.isAddedByUses());
172 if (node instanceof DataNodeContainer) {
173 for (DataSchemaNode child : ((DataNodeContainer) node)
175 checkIsAddedByUses(child, expected);
177 } else if (node instanceof ChoiceSchemaNode) {
178 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
179 checkIsAddedByUses(caseNode, expected);
184 public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
185 assertEquals(expected, node.isAddedByUses());
186 for (DataSchemaNode child : node.getChildNodes()) {
187 checkIsAddedByUses(child, expected);
191 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
192 List<Module> result = new ArrayList<>();
193 for (Module module : modules) {
194 if (module.getName().equals(moduleName)) {
201 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
204 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
206 reactor.addSources(sources);
208 return reactor.buildEffective();
211 public static SchemaContext parseYangSources(final File... files)
212 throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
214 StatementStreamSource[] sources = new StatementStreamSource[files.length];
216 for (int i = 0; i < files.length; i++) {
217 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
220 return parseYangSources(sources);
223 public static SchemaContext parseYangSources(final Collection<File> files)
224 throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
225 return parseYangSources(files.toArray(new File[files.size()]));
228 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
229 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
231 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
232 File testSourcesDir = new File(resourceDir.toURI());
234 return parseYangSources(testSourcesDir.listFiles());
237 public static SchemaContext parseYangSource(final String yangSourceFilePath)
238 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
240 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
241 File testSourcesFile = new File(resourceFile.toURI());
243 return parseYangSources(testSourcesFile);