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
60 File[] files = new File(resourceDirectory).listFiles();
62 for (File file : files) {
63 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
64 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
66 LOG.info("Ignoring non-yang file {}", file);
70 return reactor.buildEffective();
73 public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
74 throws IOException, ReactorException, YangSyntaxErrorException {
75 final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
77 for (String resourceName : resourceNames) {
78 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
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,
99 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
100 reactor.addSources(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)));
101 SchemaContext ctx = reactor.buildEffective();
102 return ctx.getModules().iterator().next();
105 public static Optional<Module> findModule(final SchemaContext context, final String moduleName) {
106 return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
109 public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
110 ModuleImport result = null;
111 for (ModuleImport moduleImport : imports) {
112 if (moduleImport.getPrefix().equals(prefix)) {
113 result = moduleImport;
120 public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
121 TypeDefinition<?> result = null;
122 for (TypeDefinition<?> td : typedefs) {
123 if (td.getQName().getLocalName().equals(name)) {
131 public static SchemaPath createPath(final boolean absolute, final QNameModule module, final String... names) {
132 List<QName> path = new ArrayList<>(names.length);
133 for (String name : names) {
134 path.add(QName.create(module, name));
136 return SchemaPath.create(path, absolute);
140 * Test if node has augmenting flag set to expected value. In case this is
141 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
148 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
149 assertEquals(expected, node.isAugmenting());
150 if (node instanceof DataNodeContainer) {
151 for (DataSchemaNode child : ((DataNodeContainer) node)
153 checkIsAugmenting(child, expected);
155 } else if (node instanceof ChoiceSchemaNode) {
156 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
157 checkIsAugmenting(caseNode, expected);
163 * Check if node has addedByUses flag set to expected value. In case this is
164 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
171 public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
172 assertEquals(expected, node.isAddedByUses());
173 if (node instanceof DataNodeContainer) {
174 for (DataSchemaNode child : ((DataNodeContainer) node)
176 checkIsAddedByUses(child, expected);
178 } else if (node instanceof ChoiceSchemaNode) {
179 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
180 checkIsAddedByUses(caseNode, expected);
185 public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
186 assertEquals(expected, node.isAddedByUses());
187 for (DataSchemaNode child : node.getChildNodes()) {
188 checkIsAddedByUses(child, expected);
192 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
193 List<Module> result = new ArrayList<>();
194 for (Module module : modules) {
195 if (module.getName().equals(moduleName)) {
202 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws ReactorException {
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 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 ReactorException, IOException, YangSyntaxErrorException {
225 return parseYangSources(files.toArray(new File[files.size()]));
228 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
229 throws 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 ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
240 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
241 File testSourcesFile = new File(resourceFile.toURI());
243 return parseYangSources(testSourcesFile);