Allow fluent use of BuildAction
[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.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;
49
50 public final class TestUtils {
51     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
52
53     private TestUtils() {
54     }
55
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();
60
61         for (File file : files) {
62             if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
63                 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
64             } else {
65                 LOG.info("Ignoring non-yang file {}", file);
66             }
67         }
68
69         return reactor.buildEffective();
70     }
71
72     public static SchemaContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
73             throws IOException, ReactorException, YangSyntaxErrorException {
74         final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
75
76         for (String resourceName : resourceNames) {
77             reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
78                 resourceName)));
79         }
80
81         return reactor.buildEffective();
82     }
83
84     public static SchemaContext loadYinModules(final URI resourceDirectory) throws ReactorException, SAXException,
85             IOException {
86         final BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
87
88         for (File file : new File(resourceDirectory).listFiles()) {
89             reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
90                 YinTextSchemaSource.forFile(file))));
91         }
92
93         return reactor.buildEffective();
94     }
95
96     public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
97             IOException {
98         final SchemaContext ctx = YangInferencePipeline.RFC6020_REACTOR.newBuild()
99                 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
100                 .buildEffective();
101         return ctx.getModules().iterator().next();
102     }
103
104     public static Optional<Module> findModule(final SchemaContext context, final String moduleName) {
105         return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
106     }
107
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;
113                 break;
114             }
115         }
116         return result;
117     }
118
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)) {
123                 result = td;
124                 break;
125             }
126         }
127         return result;
128     }
129
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));
134         }
135         return SchemaPath.create(path, absolute);
136     }
137
138     /**
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.
141      *
142      * @param node
143      *            node to check
144      * @param expected
145      *            expected value
146      */
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)
151                     .getChildNodes()) {
152                 checkIsAugmenting(child, expected);
153             }
154         } else if (node instanceof ChoiceSchemaNode) {
155             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
156                 checkIsAugmenting(caseNode, expected);
157             }
158         }
159     }
160
161     /**
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.
164      *
165      * @param node
166      *            node to check
167      * @param expected
168      *            expected value
169      */
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)
174                     .getChildNodes()) {
175                 checkIsAddedByUses(child, expected);
176             }
177         } else if (node instanceof ChoiceSchemaNode) {
178             for (ChoiceCaseNode caseNode : ((ChoiceSchemaNode) node).getCases().values()) {
179                 checkIsAddedByUses(caseNode, expected);
180             }
181         }
182     }
183
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);
188         }
189     }
190
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)) {
195                 result.add(module);
196             }
197         }
198         return result;
199     }
200
201     public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws ReactorException {
202
203         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
204                 .newBuild();
205         reactor.addSources(sources);
206
207         return reactor.buildEffective();
208     }
209
210     public static SchemaContext parseYangSources(final File... files)
211             throws ReactorException, IOException, YangSyntaxErrorException {
212
213         StatementStreamSource[] sources = new StatementStreamSource[files.length];
214
215         for (int i = 0; i < files.length; i++) {
216             sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
217         }
218
219         return parseYangSources(sources);
220     }
221
222     public static SchemaContext parseYangSources(final Collection<File> files)
223             throws ReactorException, IOException, YangSyntaxErrorException {
224         return parseYangSources(files.toArray(new File[files.size()]));
225     }
226
227     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath)
228             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
229
230         URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
231         File testSourcesDir = new File(resourceDir.toURI());
232
233         return parseYangSources(testSourcesDir.listFiles());
234     }
235
236     public static SchemaContext parseYangSource(final String yangSourceFilePath)
237             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
238
239         URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
240         File testSourcesFile = new File(resourceFile.toURI());
241
242         return parseYangSources(testSourcesFile);
243     }
244 }