Bug 7159: Add yang-test-util artifact
[yangtools.git] / yang / yang-maven-plugin / src / main / java / org / opendaylight / yangtools / yang2sources / plugin / YangToSourcesProcessor.java
index 3de8eef5ebce7c5c2823f1a7213a5b65a966669f..604fab0d13a0c0505bf25b8910237bd09d8f28c7 100644 (file)
@@ -7,9 +7,6 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
-import java.util.HashSet;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Throwables;
 import com.google.common.collect.Maps;
@@ -20,6 +17,7 @@ import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -31,8 +29,9 @@ import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.project.MavenProject;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.parser.repo.URLSchemaContextResolver;
+import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
 import org.opendaylight.yangtools.yang2sources.plugin.Util.ContextHolder;
 import org.opendaylight.yangtools.yang2sources.plugin.Util.YangsInZipsResult;
@@ -50,6 +49,7 @@ class YangToSourcesProcessor {
     static final String LOG_PREFIX = "yang-to-sources:";
     static final String META_INF_YANG_STRING = "META-INF" + File.separator + "yang";
     static final String META_INF_YANG_STRING_JAR = "META-INF" + "/" + "yang";
+    static final String META_INF_YANG_SERVICES_STRING_JAR = "META-INF" + "/" + "services";
 
     private final File yangFilesRootDir;
     private final File[] excludedFiles;
@@ -58,17 +58,17 @@ class YangToSourcesProcessor {
     private final boolean inspectDependencies;
     private final BuildContext buildContext;
     private final YangProvider yangProvider;
-    private final URLSchemaContextResolver resolver;
+    private final YangTextSchemaContextResolver resolver;
 
     @VisibleForTesting
-    YangToSourcesProcessor(File yangFilesRootDir, File[] excludedFiles, List<CodeGeneratorArg> codeGenerators,
-            MavenProject project, boolean inspectDependencies, YangProvider yangProvider) {
+    YangToSourcesProcessor(final File yangFilesRootDir, final File[] excludedFiles, final List<CodeGeneratorArg> codeGenerators,
+            final MavenProject project, final boolean inspectDependencies, final YangProvider yangProvider) {
         this(new DefaultBuildContext(), yangFilesRootDir, excludedFiles, codeGenerators, project,
                 inspectDependencies, yangProvider);
     }
 
-    private YangToSourcesProcessor(BuildContext buildContext, File yangFilesRootDir, File[] excludedFiles,
-            List<CodeGeneratorArg> codeGenerators, MavenProject project, boolean inspectDependencies, YangProvider
+    private YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir, final File[] excludedFiles,
+            final List<CodeGeneratorArg> codeGenerators, final MavenProject project, final boolean inspectDependencies, final YangProvider
                                            yangProvider) {
         this.buildContext = Util.checkNotNull(buildContext, "buildContext");
         this.yangFilesRootDir = Util.checkNotNull(yangFilesRootDir, "yangFilesRootDir");
@@ -81,11 +81,11 @@ class YangToSourcesProcessor {
         this.project = Util.checkNotNull(project, "project");
         this.inspectDependencies = inspectDependencies;
         this.yangProvider = yangProvider;
-        this.resolver = URLSchemaContextResolver.create("maven-plugin");
+        this.resolver = YangTextSchemaContextResolver.create("maven-plugin");
     }
 
-    YangToSourcesProcessor(BuildContext buildContext, File yangFilesRootDir, File[] excludedFiles,
-                           List<CodeGeneratorArg> codeGenerators, MavenProject project, boolean inspectDependencies) {
+    YangToSourcesProcessor(final BuildContext buildContext, final File yangFilesRootDir, final File[] excludedFiles,
+                           final List<CodeGeneratorArg> codeGenerators, final MavenProject project, final boolean inspectDependencies) {
         this(yangFilesRootDir, excludedFiles, codeGenerators, project, inspectDependencies, new YangProvider());
     }
 
@@ -97,8 +97,27 @@ class YangToSourcesProcessor {
         }
     }
 
+    void conditionalExecute(final boolean skip) throws MojoExecutionException, MojoFailureException {
+        if (skip) {
+            LOG.info("Skipping YANG code generation because property yang.skip is true");
+
+            // But manually add resources
+            // add META_INF/yang
+            yangProvider.addYangsToMetaInf(project, yangFilesRootDir, excludedFiles);
+
+            // add META_INF/services
+            File generatedServicesDir = new File(project.getBasedir(), CodeGeneratorArg.YANG_SERVICES_GENERATED_DIR);
+            YangProvider.setResource(generatedServicesDir, project);
+            LOG.debug("{} Yang services files from: {} marked as resources: {}", LOG_PREFIX, generatedServicesDir,
+                    META_INF_YANG_SERVICES_STRING_JAR);
+
+
+        } else {
+            execute();
+        }
+    }
+
     private ContextHolder processYang() throws MojoExecutionException {
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
         SchemaContext resolveSchemaContext;
         List<Closeable> closeables = new ArrayList<>();
         LOG.info("{} Inspecting {}", LOG_PREFIX, yangFilesRootDir);
@@ -117,7 +136,7 @@ class YangToSourcesProcessor {
             }
 
             if (allFiles.isEmpty()) {
-               LOG.info("{} No input files found", LOG_PREFIX);
+                LOG.info("{} No input files found", LOG_PREFIX);
                 return null;
             }
 
@@ -166,7 +185,7 @@ class YangToSourcesProcessor {
                     closeables.addAll(yangStreams);
                 }
 
-                resolveSchemaContext = reactor.buildEffective(all);
+                resolveSchemaContext = YangParserTestUtils.parseYangStreams(all);
 
                 Set<Module> parsedAllYangModules = resolveSchemaContext.getModules();
                 projectYangModules = new HashSet<>();
@@ -202,13 +221,15 @@ class YangToSourcesProcessor {
         }
     }
 
-    private List<InputStream> toStreamsWithoutDuplicates(List<YangSourceFromDependency> list) throws IOException {
+    private static List<InputStream> toStreamsWithoutDuplicates(final List<YangSourceFromDependency> list) throws IOException {
         ConcurrentMap<String, YangSourceFromDependency> byContent = Maps.newConcurrentMap();
 
         for (YangSourceFromDependency yangFromDependency : list) {
             try (InputStream dataStream = yangFromDependency.openStream()) {
                 String contents = IOUtils.toString(dataStream);
                 byContent.putIfAbsent(contents, yangFromDependency);
+            } catch (IOException e) {
+                throw new IOException("Exception when reading from: " + yangFromDependency.getDescription(), e);
             }
 
         }
@@ -222,7 +243,7 @@ class YangToSourcesProcessor {
     static class YangProvider {
         private static final Logger LOG = LoggerFactory.getLogger(YangProvider.class);
 
-        void addYangsToMetaInf(MavenProject project, File yangFilesRootDir, File[] excludedFiles)
+        void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir, final File[] excludedFiles)
                 throws MojoFailureException {
 
             // copy project's src/main/yang/*.yang to target/generated-sources/yang/META-INF/yang/*.yang
@@ -235,14 +256,13 @@ class YangToSourcesProcessor {
 
             File actualGeneratedYangDir = new File(project.getBuild().getDirectory(),
                     CodeGeneratorArg.YANG_GENERATED_DIR.replace("target" + File.separator, ""));
-            if(!actualGeneratedYangDir.equals(generatedYangDir)) {
+            if (!actualGeneratedYangDir.equals(generatedYangDir)) {
                 addYangsToMetaInf(project, yangFilesRootDir, excludedFiles, actualGeneratedYangDir);
             }
         }
 
-        private void addYangsToMetaInf(MavenProject project, File yangFilesRootDir,
-                File[] excludedFiles, File generatedYangDir)
-                throws MojoFailureException {
+        private static void addYangsToMetaInf(final MavenProject project, final File yangFilesRootDir,
+                final File[] excludedFiles, final File generatedYangDir) throws MojoFailureException {
 
             File withMetaInf = new File(generatedYangDir, META_INF_YANG_STRING);
             withMetaInf.mkdirs();
@@ -263,7 +283,7 @@ class YangToSourcesProcessor {
                     META_INF_YANG_STRING_JAR);
         }
 
-        private static void setResource(File targetYangDir, MavenProject project) {
+        private static void setResource(final File targetYangDir, final MavenProject project) {
             Resource res = new Resource();
             res.setDirectory(targetYangDir.getPath());
             project.addResource(res);
@@ -273,7 +293,7 @@ class YangToSourcesProcessor {
     /**
      * Call generate on every generator from plugin configuration
      */
-    private void generateSources(ContextHolder context) throws MojoFailureException {
+    private void generateSources(final ContextHolder context) throws MojoFailureException {
         if (codeGenerators.size() == 0) {
             LOG.warn("{} No code generators provided", LOG_PREFIX);
             return;
@@ -301,7 +321,7 @@ class YangToSourcesProcessor {
     /**
      * Instantiate generator from class and call required method
      */
-    private void generateSourcesWithOneGenerator(ContextHolder context, CodeGeneratorArg codeGeneratorCfg)
+    private void generateSourcesWithOneGenerator(final ContextHolder context, final CodeGeneratorArg codeGeneratorCfg)
             throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {
 
         codeGeneratorCfg.check();