Bug 1411-4: MDSAL Binding2 Generator Impl
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / main / test / java / org / opendaylight / mdsal / binding2 / 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
9 package org.opendaylight.mdsal.binding2;
10
11 import com.google.common.annotations.Beta;
12 import java.io.File;
13 import java.net.URI;
14 import java.util.Set;
15 import org.opendaylight.yangtools.yang.model.api.Module;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
17 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
18 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
19 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 @Beta
26 public final class TestUtils {
27
28     private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
29
30     private TestUtils() {
31         throw new UnsupportedOperationException("Utility class");
32     }
33
34     public static Set<Module> loadModules(final URI resourceDirectory)
35             throws SourceException, ReactorException {
36         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
37                 .newBuild();
38         File[] files = new File(resourceDirectory).listFiles();
39
40         for (File file : files) {
41             if (file.getName().endsWith(".yang")) {
42                 addSources(reactor, new YangStatementSourceImpl(file.getPath(), true));
43             } else {
44                 LOG.info("Ignoring non-yang file {}", file);
45             }
46         }
47
48         EffectiveSchemaContext ctx = reactor.buildEffective();
49         return ctx.getModules();
50     }
51
52     private static void addSources(final CrossSourceStatementReactor.BuildAction reactor,
53         final YangStatementSourceImpl... sources) {
54         for (YangStatementSourceImpl source : sources) {
55             reactor.addSource(source);
56         }
57     }
58 }