Populate parser/ hierarchy
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / stmt / TestUtils.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 package org.opendaylight.yangtools.yang.stmt;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.net.URI;
15 import java.net.URISyntaxException;
16 import java.net.URL;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.yang.common.YangConstants;
23 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
34 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
40 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
41 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.xml.sax.SAXException;
45
46 public final class TestUtils {
47     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
48
49     private TestUtils() {
50     }
51
52     public static EffectiveModelContext loadModules(final URI resourceDirectory)
53             throws ReactorException, IOException, YangSyntaxErrorException {
54         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
55         File[] files = new File(resourceDirectory).listFiles();
56
57         for (File file : files) {
58             if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
59                 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
60             } else {
61                 LOG.info("Ignoring non-yang file {}", file);
62             }
63         }
64
65         return reactor.buildEffective();
66     }
67
68     public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
69             throws IOException, ReactorException, YangSyntaxErrorException {
70         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
71
72         for (String resourceName : resourceNames) {
73             reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
74                 resourceName)));
75         }
76
77         return reactor.buildEffective();
78     }
79
80     public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
81             throws ReactorException, SAXException, IOException {
82         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
83
84         for (File file : new File(resourceDirectory).listFiles()) {
85             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
86                 YinTextSchemaSource.forFile(file))));
87         }
88
89         return reactor.buildEffective();
90     }
91
92     public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
93             IOException {
94         return RFC7950Reactors.defaultReactor().newBuild()
95                 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
96                 .buildEffective()
97                 .getModules().iterator().next();
98     }
99
100     @Deprecated(forRemoval = true)
101     public static Optional<? extends @NonNull Module> findModule(final SchemaContext context, final String moduleName) {
102         return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
103     }
104
105     public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
106         for (ModuleImport moduleImport : imports) {
107             if (moduleImport.getPrefix().equals(prefix)) {
108                 return moduleImport;
109             }
110         }
111         return null;
112     }
113
114     public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
115             final String name) {
116         for (TypeDefinition<?> td : typedefs) {
117             if (td.getQName().getLocalName().equals(name)) {
118                 return td;
119             }
120         }
121         return null;
122     }
123
124     /**
125      * Test if node has augmenting flag set to expected value. In case this is
126      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
127      *
128      * @param node
129      *            node to check
130      * @param expected
131      *            expected value
132      */
133     public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
134         assertEquals(expected, node.isAugmenting());
135         if (node instanceof DataNodeContainer) {
136             for (DataSchemaNode child : ((DataNodeContainer) node)
137                     .getChildNodes()) {
138                 checkIsAugmenting(child, expected);
139             }
140         } else if (node instanceof ChoiceSchemaNode) {
141             for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
142                 checkIsAugmenting(caseNode, expected);
143             }
144         }
145     }
146
147     public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
148         List<Module> result = new ArrayList<>();
149         for (Module module : modules) {
150             if (module.getName().equals(moduleName)) {
151                 result.add(module);
152             }
153         }
154         return result;
155     }
156
157     public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
158             throws ReactorException {
159         return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
160     }
161
162     public static EffectiveModelContext parseYangSources(final File... files)
163             throws ReactorException, IOException, YangSyntaxErrorException {
164
165         StatementStreamSource[] sources = new StatementStreamSource[files.length];
166
167         for (int i = 0; i < files.length; i++) {
168             sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
169         }
170
171         return parseYangSources(sources);
172     }
173
174     public static EffectiveModelContext parseYangSources(final Collection<File> files)
175             throws ReactorException, IOException, YangSyntaxErrorException {
176         return parseYangSources(files.toArray(new File[files.size()]));
177     }
178
179     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
180             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
181
182         URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
183         File testSourcesDir = new File(resourceDir.toURI());
184
185         return parseYangSources(testSourcesDir.listFiles(
186             (dir, name) -> name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)));
187     }
188
189     public static EffectiveModelContext parseYangSource(final String yangSourceFilePath)
190             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
191
192         URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
193         File testSourcesFile = new File(resourceFile.toURI());
194
195         return parseYangSources(testSourcesFile);
196     }
197 }