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.StatementStreamSource;
43 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
44 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
45 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48 import org.xml.sax.SAXException;
50 public final class TestUtils {
51 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
56 public static SchemaContext loadModules(final URI resourceDirectory)
57 throws ReactorException, IOException, YangSyntaxErrorException {
58 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
59 File[] files = new File(resourceDirectory).listFiles();
61 for (File file : files) {
62 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
63 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
65 LOG.info("Ignoring non-yang file {}", file);
69 return reactor.buildEffective();
72 public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
73 throws IOException, ReactorException, YangSyntaxErrorException {
74 final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
76 for (String resourceName : resourceNames) {
77 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
81 return reactor.buildEffective();
84 public static SchemaContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException,
86 final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
88 for (File file : new File(resourceDirectory).listFiles()) {
89 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
90 YinTextSchemaSource.forFile(file))));
93 return reactor.buildEffective();
96 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
98 final SchemaContext ctx = YangInferencePipeline.RFC6020_REACTOR.newBuild()
99 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
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().values()) {
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().values()) {
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 ReactorException {
203 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
205 reactor.addSources(sources);
207 return reactor.buildEffective();
210 public static SchemaContext parseYangSources(final File... files)
211 throws ReactorException, IOException, YangSyntaxErrorException {
213 StatementStreamSource[] sources = new StatementStreamSource[files.length];
215 for (int i = 0; i < files.length; i++) {
216 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
219 return parseYangSources(sources);
222 public static SchemaContext parseYangSources(final Collection<File> files)
223 throws ReactorException, IOException, YangSyntaxErrorException {
224 return parseYangSources(files.toArray(new File[files.size()]));
227 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
228 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
230 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
231 File testSourcesDir = new File(resourceDir.toURI());
233 return parseYangSources(testSourcesDir.listFiles());
236 public static SchemaContext parseYangSource(final String yangSourceFilePath)
237 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
239 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
240 File testSourcesFile = new File(resourceFile.toURI());
242 return parseYangSources(testSourcesFile);