fceb6ed379625ab2b2d55f7992bf02edac37478c
[yangtools.git] / yang / yang-parser-impl / 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 java.util.Set;
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.rfc7950.reactor.RFC7950Reactors;
39 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
40 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
41 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
43 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
44 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.xml.sax.SAXException;
48
49 public final class TestUtils {
50     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
51
52     private TestUtils() {
53     }
54
55     public static SchemaContext loadModules(final URI resourceDirectory)
56             throws ReactorException, IOException, YangSyntaxErrorException {
57         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
58         File[] files = new File(resourceDirectory).listFiles();
59
60         for (File file : files) {
61             if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
62                 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
63             } else {
64                 LOG.info("Ignoring non-yang file {}", file);
65             }
66         }
67
68         return reactor.buildEffective();
69     }
70
71     public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
72             throws IOException, ReactorException, YangSyntaxErrorException {
73         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
74
75         for (String resourceName : resourceNames) {
76             reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
77                 resourceName)));
78         }
79
80         return reactor.buildEffective();
81     }
82
83     public static SchemaContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException,
84             IOException {
85         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
86
87         for (File file : new File(resourceDirectory).listFiles()) {
88             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
89                 YinTextSchemaSource.forFile(file))));
90         }
91
92         return reactor.buildEffective();
93     }
94
95     public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
96             IOException {
97         final SchemaContext ctx = RFC7950Reactors.defaultReactor().newBuild()
98                 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
99                 .buildEffective();
100         return ctx.getModules().iterator().next();
101     }
102
103     public static Optional<Module> findModule(final SchemaContext context, final String moduleName) {
104         return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
105     }
106
107     public static ModuleImport findImport(final Set<ModuleImport> imports, final String prefix) {
108         ModuleImport result = null;
109         for (ModuleImport moduleImport : imports) {
110             if (moduleImport.getPrefix().equals(prefix)) {
111                 result = moduleImport;
112                 break;
113             }
114         }
115         return result;
116     }
117
118     public static TypeDefinition<?> findTypedef(final Set<TypeDefinition<?>> typedefs, final String name) {
119         TypeDefinition<?> result = null;
120         for (TypeDefinition<?> td : typedefs) {
121             if (td.getQName().getLocalName().equals(name)) {
122                 result = td;
123                 break;
124             }
125         }
126         return result;
127     }
128
129     public static SchemaPath createPath(final boolean absolute, final QNameModule module, final String... names) {
130         List<QName> path = new ArrayList<>(names.length);
131         for (String name : names) {
132             path.add(QName.create(module, name));
133         }
134         return SchemaPath.create(path, absolute);
135     }
136
137     /**
138      * Test if node has augmenting flag set to expected value. In case this is
139      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
140      *
141      * @param node
142      *            node to check
143      * @param expected
144      *            expected value
145      */
146     public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
147         assertEquals(expected, node.isAugmenting());
148         if (node instanceof DataNodeContainer) {
149             for (DataSchemaNode child : ((DataNodeContainer) node)
150                     .getChildNodes()) {
151                 checkIsAugmenting(child, expected);
152             }
153         } else if (node instanceof ChoiceSchemaNode) {
154             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
155                 checkIsAugmenting(caseNode, expected);
156             }
157         }
158     }
159
160     /**
161      * Check if node has addedByUses flag set to expected value. In case this is
162      * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
163      *
164      * @param node
165      *            node to check
166      * @param expected
167      *            expected value
168      */
169     public static void checkIsAddedByUses(final DataSchemaNode node, final boolean expected) {
170         assertEquals(expected, node.isAddedByUses());
171         if (node instanceof DataNodeContainer) {
172             for (DataSchemaNode child : ((DataNodeContainer) node)
173                     .getChildNodes()) {
174                 checkIsAddedByUses(child, expected);
175             }
176         } else if (node instanceof ChoiceSchemaNode) {
177             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
178                 checkIsAddedByUses(caseNode, expected);
179             }
180         }
181     }
182
183     public static void checkIsAddedByUses(final GroupingDefinition node, final boolean expected) {
184         assertEquals(expected, node.isAddedByUses());
185         for (DataSchemaNode child : node.getChildNodes()) {
186             checkIsAddedByUses(child, expected);
187         }
188     }
189
190     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
191         List<Module> result = new ArrayList<>();
192         for (Module module : modules) {
193             if (module.getName().equals(moduleName)) {
194                 result.add(module);
195             }
196         }
197         return result;
198     }
199
200     public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws ReactorException {
201         return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
202     }
203
204     public static SchemaContext parseYangSources(final File... files)
205             throws ReactorException, IOException, YangSyntaxErrorException {
206
207         StatementStreamSource[] sources = new StatementStreamSource[files.length];
208
209         for (int i = 0; i < files.length; i++) {
210             sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
211         }
212
213         return parseYangSources(sources);
214     }
215
216     public static SchemaContext parseYangSources(final Collection<File> files)
217             throws ReactorException, IOException, YangSyntaxErrorException {
218         return parseYangSources(files.toArray(new File[files.size()]));
219     }
220
221     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
222             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
223
224         URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
225         File testSourcesDir = new File(resourceDir.toURI());
226
227         return parseYangSources(testSourcesDir.listFiles());
228     }
229
230     public static SchemaContext parseYangSource(final String yangSourceFilePath)
231             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
232
233         URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
234         File testSourcesFile = new File(resourceFile.toURI());
235
236         return parseYangSources(testSourcesFile);
237     }
238 }