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