Bug 3670 (part 3/5): Use of new statement parser in yang-maven-plugin
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / CrossSourceStatementReactor.java
index c8040809941da736e6b7928e629f8dd2a83cf421..3b8b11007f4d5320106f99646aca88889387c151 100644 (file)
@@ -7,8 +7,19 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
+import java.util.Set;
+
+import java.io.FileNotFoundException;
+import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
+import java.util.HashMap;
+import java.util.Collections;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import java.io.File;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import java.io.InputStream;
+import java.util.List;
 import java.util.Collection;
-
 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
 import com.google.common.collect.ImmutableMap;
@@ -94,7 +105,40 @@ public class CrossSourceStatementReactor {
             return context.buildEffective();
         }
 
-    }
+        public SchemaContext buildEffective(List<InputStream> yangInputStreams) throws SourceException, ReactorException {
+
+            for(InputStream yangInputStream : yangInputStreams) {
+                addSource(new YangStatementSourceImpl(yangInputStream));
+            }
 
+            return buildEffective();
+        }
+
+        public Map<File, Module> buildEffectiveMappedToSource(
+                List<File> yangFiles) throws SourceException, ReactorException,
+                FileNotFoundException {
+
+            if (yangFiles == null || yangFiles.isEmpty()) {
+                return Collections.emptyMap();
+            }
 
+            Map<String, File> pathToFile = new HashMap<>();
+            Map<File, Module> sourceFileToModule = new HashMap<>();
+
+            for (File yangFile : yangFiles) {
+                addSource(new YangStatementSourceImpl(new NamedFileInputStream(
+                        yangFile, yangFile.getPath())));
+                pathToFile.put(yangFile.getPath(), yangFile);
+            }
+
+            EffectiveSchemaContext schema = buildEffective();
+            Set<Module> modules = schema.getModules();
+            for (Module module : modules) {
+                sourceFileToModule.put(
+                        pathToFile.get(module.getModuleSourcePath()), module);
+            }
+
+            return sourceFileToModule;
+        }
+    }
 }