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;
14 import java.io.InputStream;
16 import java.net.URISyntaxException;
18 import java.text.DateFormat;
19 import java.text.ParseException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.Date;
23 import java.util.List;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
27 import org.opendaylight.yangtools.yang.common.YangConstants;
28 import org.opendaylight.yangtools.yang.model.api.ChoiceCaseNode;
29 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
39 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
40 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
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.rfc6020.YangInferencePipeline;
46 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
47 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YinStatementSourceImpl;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public final class TestUtils {
52 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
57 public static Set<Module> loadModules(final URI resourceDirectory)
58 throws SourceException, 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 SchemaContext ctx = reactor.buildEffective();
72 return ctx.getModules();
75 public static Set<Module> loadModules(final List<InputStream> streams)
76 throws SourceException, ReactorException {
77 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
79 for (InputStream inputStream : streams) {
80 reactor.addSource(new YangStatementSourceImpl(inputStream));
83 SchemaContext ctx = reactor.buildEffective();
84 return ctx.getModules();
87 public static Set<Module> loadYinModules(final URI resourceDirectory) throws ReactorException {
88 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
90 for (File file : new File(resourceDirectory).listFiles()) {
91 reactor.addSource(new YinStatementSourceImpl(file.getPath(), true));
94 SchemaContext ctx = reactor.buildEffective();
95 return ctx.getModules();
98 public static Set<Module> loadYinModules(final List<InputStream> streams) throws SourceException, ReactorException {
99 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
100 for (InputStream inputStream : streams) {
101 reactor.addSource(new YinStatementSourceImpl(inputStream));
104 SchemaContext ctx = reactor.buildEffective();
105 return ctx.getModules();
108 public static Module loadModule(final InputStream stream)
109 throws SourceException, ReactorException {
110 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
112 reactor.addSource(new YangStatementSourceImpl(stream));
113 SchemaContext ctx = reactor.buildEffective();
114 return ctx.getModules().iterator().next();
117 public static Module loadYinModule(final InputStream stream) throws SourceException, ReactorException {
118 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
119 reactor.addSources(new YinStatementSourceImpl(stream));
120 SchemaContext ctx = reactor.buildEffective();
121 return ctx.getModules().iterator().next();
124 public static Module findModule(final Set<Module> modules,
125 final String moduleName) {
126 Module result = null;
127 for (Module module : modules) {
128 if (module.getName().equals(moduleName)) {
136 public static ModuleImport findImport(final Set<ModuleImport> imports,
137 final String prefix) {
138 ModuleImport result = null;
139 for (ModuleImport moduleImport : imports) {
140 if (moduleImport.getPrefix().equals(prefix)) {
141 result = moduleImport;
148 public static TypeDefinition<?> findTypedef(
149 final Set<TypeDefinition<?>> typedefs, final String name) {
150 TypeDefinition<?> result = null;
151 for (TypeDefinition<?> td : typedefs) {
152 if (td.getQName().getLocalName().equals(name)) {
160 public static SchemaPath createPath(final boolean absolute,
161 final URI namespace, final Date revision, final String prefix,
162 final String... names) {
163 List<QName> path = new ArrayList<>();
164 for (String name : names) {
165 path.add(QName.create(namespace, revision, name));
167 return SchemaPath.create(path, absolute);
170 public static Date createDate(final String date) {
172 final DateFormat simpleDateFormat = SimpleDateFormatUtil.getRevisionFormat();
174 result = simpleDateFormat.parse(date);
175 } catch (ParseException e) {
182 * Test if node has augmenting flag set to expected value. In case this is
183 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
190 public static void checkIsAugmenting(final DataSchemaNode node,
191 final boolean expected) {
192 assertEquals(expected, node.isAugmenting());
193 if (node instanceof DataNodeContainer) {
194 for (DataSchemaNode child : ((DataNodeContainer) node)
196 checkIsAugmenting(child, expected);
198 } else if (node instanceof ChoiceSchemaNode) {
199 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
200 checkIsAugmenting(caseNode, expected);
206 * Check if node has addedByUses flag set to expected value. In case this is
207 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
214 public static void checkIsAddedByUses(final DataSchemaNode node,
215 final boolean expected) {
216 assertEquals(expected, node.isAddedByUses());
217 if (node instanceof DataNodeContainer) {
218 for (DataSchemaNode child : ((DataNodeContainer) node)
220 checkIsAddedByUses(child, expected);
222 } else if (node instanceof ChoiceSchemaNode) {
223 for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
224 checkIsAddedByUses(caseNode, expected);
229 public static void checkIsAddedByUses(final GroupingDefinition node,
230 final boolean expected) {
231 assertEquals(expected, node.isAddedByUses());
232 for (DataSchemaNode child : node.getChildNodes()) {
233 checkIsAddedByUses(child, expected);
237 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
238 List<Module> result = new ArrayList<>();
239 for (Module module : modules) {
240 if (module.getName().equals(moduleName)) {
247 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
250 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
252 reactor.addSources(sources);
254 return reactor.buildEffective();
257 public static SchemaContext parseYangSources(final File... files)
258 throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
260 StatementStreamSource[] sources = new StatementStreamSource[files.length];
262 for (int i = 0; i < files.length; i++) {
263 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
266 return parseYangSources(sources);
269 public static SchemaContext parseYangSources(final Collection<File> files)
270 throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
271 return parseYangSources(files.toArray(new File[files.size()]));
274 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
275 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
277 URL resourceDir = StmtTestUtils.class
278 .getResource(yangSourcesDirectoryPath);
279 File testSourcesDir = new File(resourceDir.toURI());
281 return parseYangSources(testSourcesDir.listFiles());
284 public static SchemaContext parseYangSource(final String yangSourceFilePath)
285 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
287 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
288 File testSourcesFile = new File(resourceFile.toURI());
290 return parseYangSources(testSourcesFile);