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