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 819dfff3be7d15b44ec43d79106b6b0bab848ee2..3b8b11007f4d5320106f99646aca88889387c151 100644 (file)
@@ -7,6 +7,21 @@
  */
 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;
 import java.util.EnumMap;
 import java.util.Map;
@@ -19,9 +34,16 @@ import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
 public class CrossSourceStatementReactor {
 
     private final Map<ModelProcessingPhase,StatementSupportBundle> supportedTerminology;
+    private final Map<ValidationBundleType,Collection<?>> supportedValidation;
 
     CrossSourceStatementReactor(Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
+        this.supportedValidation = ImmutableMap.of();
+    }
+
+    CrossSourceStatementReactor(Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology, Map<ValidationBundleType,Collection<?>> supportedValidation) {
+        this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
+        this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
     }
 
     public static final Builder builder() {
@@ -35,15 +57,24 @@ public class CrossSourceStatementReactor {
     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor>{
 
         final Map<ModelProcessingPhase,StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
+        final Map<ValidationBundleType,Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
 
         public Builder setBundle(ModelProcessingPhase phase,StatementSupportBundle bundle) {
             bundles.put(phase, bundle);
             return this;
         }
 
+
+        public Builder setValidationBundle(
+                ValidationBundleType type,
+                Collection<?> validationBundle) {
+            validationBundles.put(type,validationBundle);
+            return this;
+        }
+
         @Override
         public CrossSourceStatementReactor build() {
-            return new CrossSourceStatementReactor(bundles);
+            return new CrossSourceStatementReactor(bundles, validationBundles);
         }
 
     }
@@ -53,20 +84,61 @@ public class CrossSourceStatementReactor {
         private final BuildGlobalContext context;
 
         public BuildAction() {
-            this.context = new BuildGlobalContext(supportedTerminology);
+            this.context = new BuildGlobalContext(supportedTerminology, supportedValidation);
         }
 
         public void addSource(StatementStreamSource source) {
             context.addSource(source);
         }
 
+        public void addSources(StatementStreamSource... sources) {
+            for (StatementStreamSource source : sources) {
+                context.addSource(source);
+            }
+        }
+
         public EffectiveModelContext build() throws SourceException, ReactorException {
             return context.build();
         }
 
+        public EffectiveSchemaContext buildEffective() throws SourceException, ReactorException {
+            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;
+        }
+    }
 }