Allow module to be represented in different formats 15/92415/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 7 Sep 2020 09:02:42 +0000 (11:02 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 7 Sep 2020 11:01:00 +0000 (13:01 +0200)
We may need to allow for different source packaging. The default
is YangTextSchemaSource, which we always packaged. We may introduce
additional types at a later point.

Change-Id: I46dcde1052c36fbc5105a320ac3ec58186d24f61
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-maven-plugin-spi/src/main/java/org/opendaylight/yangtools/yang2sources/spi/BasicCodeGenerator.java
yang/yang-maven-plugin-spi/src/main/java/org/opendaylight/yangtools/yang2sources/spi/ModuleResourceResolver.java [new file with mode: 0644]
yang/yang-maven-plugin-spi/src/test/java/org/opendaylight/yangtools/yang2sources/spi/CodeGeneratorTestImpl.java
yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/ContextHolder.java
yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesMojo.java
yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java
yang/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/GenerateSourcesTest.java

index d6695099ef2c67625aa3712bade8685f1701dd3c..cac397db4d2a08f92ff2161d302690549557ff15 100644 (file)
@@ -11,9 +11,7 @@ import java.io.File;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Set;
-import java.util.function.Function;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
 
@@ -50,7 +48,7 @@ public interface BasicCodeGenerator {
      * @return collection of files that were generated from schema context
      */
     Collection<File> generateSources(EffectiveModelContext context, File outputBaseDir, Set<Module> currentModules,
-            Function<Module, Optional<String>> moduleResourcePathResolver) throws IOException;
+            ModuleResourceResolver moduleResourcePathResolver) throws IOException;
 
     /**
      * Provided map contains all configuration that was set in pom for code
diff --git a/yang/yang-maven-plugin-spi/src/main/java/org/opendaylight/yangtools/yang2sources/spi/ModuleResourceResolver.java b/yang/yang-maven-plugin-spi/src/main/java/org/opendaylight/yangtools/yang2sources/spi/ModuleResourceResolver.java
new file mode 100644 (file)
index 0000000..2162eeb
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang2sources.spi;
+
+import com.google.common.annotations.Beta;
+import java.util.Optional;
+import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
+
+/**
+ * An SPI-level interface to find the schema source for a particular YANG module, as packaged in the final artifact.
+ * The module must be part of the current resolution context.
+ */
+@Beta
+@FunctionalInterface
+public interface ModuleResourceResolver {
+    /**
+     * Find the path of the packaged resource which corresponds to the specified module in the specified representation.
+     *
+     * @param module Requested module
+     * @param representation Requested representation
+     * @return Path to packaged resource
+     * @throws NullPointerException if any argument is null
+     * @throws IllegalArgumentException if the requested representation is not supported by this resolver
+     */
+    Optional<String> findModuleResourcePath(Module module, Class<? extends SchemaSourceRepresentation> representation);
+}
index 1dea8426b5928446999b5a13cc96754a335f4a39..bfb5abb8bb16fcafca4001c9ae7cff898c9c7440 100644 (file)
@@ -11,9 +11,7 @@ import java.io.File;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Set;
-import java.util.function.Function;
 import org.apache.maven.project.MavenProject;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
@@ -26,7 +24,7 @@ public class CodeGeneratorTestImpl implements BasicCodeGenerator, MavenProjectAw
 
     @Override
     public Collection<File> generateSources(final EffectiveModelContext context, final File outputBaseDir,
-            final Set<Module> currentModules, final Function<Module, Optional<String>> moduleResourcePathResolver)
+            final Set<Module> currentModules, final ModuleResourceResolver moduleResourcePathResolver)
                     throws IOException {
         LOG.debug("{} generateSources:context: {}", getClass().getCanonicalName(), context);
         LOG.debug("{} generateSources:outputBaseDir: {}", getClass().getCanonicalName(), outputBaseDir);
index 83d1c4c526618dd36652c42230ce8e17c19fd5c7..a26b0fec1d06acf943f792b6cb21e9118eaf2a30 100644 (file)
@@ -7,6 +7,7 @@
  */
 package org.opendaylight.yangtools.yang2sources.plugin;
 
+import static com.google.common.base.Preconditions.checkArgument;
 import static java.util.Objects.requireNonNull;
 
 import com.google.common.collect.ImmutableSet;
@@ -15,9 +16,12 @@ import java.util.Set;
 import org.opendaylight.yangtools.concepts.Immutable;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.Module;
+import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+import org.opendaylight.yangtools.yang2sources.spi.ModuleResourceResolver;
 
-final class ContextHolder implements Immutable {
+final class ContextHolder implements Immutable, ModuleResourceResolver {
     private final EffectiveModelContext context;
     private final Set<Module> modules;
     private final Set<SourceIdentifier> sources;
@@ -28,6 +32,17 @@ final class ContextHolder implements Immutable {
         this.sources = ImmutableSet.copyOf(sources);
     }
 
+    @Override
+    public Optional<String> findModuleResourcePath(final Module module,
+            final Class<? extends SchemaSourceRepresentation> representation) {
+        checkArgument(YangTextSchemaSource.class.equals(requireNonNull(representation)),
+            "Unsupported representation %s", representation);
+        final SourceIdentifier id = Util.moduleToIdentifier(module);
+        return sources.contains(id)
+                ? Optional.of("/" + YangToSourcesProcessor.META_INF_YANG_STRING_JAR + "/" + id.toYangFilename())
+                        : Optional.empty();
+    }
+
     EffectiveModelContext getContext() {
         return context;
     }
@@ -35,11 +50,4 @@ final class ContextHolder implements Immutable {
     Set<Module> getYangModules() {
         return modules;
     }
-
-    Optional<String> moduleToResourcePath(final Module mod) {
-        final SourceIdentifier id = Util.moduleToIdentifier(mod);
-        return sources.contains(id)
-                ? Optional.of("/" + YangToSourcesProcessor.META_INF_YANG_STRING_JAR + "/" + id.toYangFilename())
-                        : Optional.empty();
-    }
-}
\ No newline at end of file
+}
index b2515e92a222949e09ece127926a7555ced5454d..0974a773e32f2fd894da2138e1ff3a4edea092f9 100644 (file)
@@ -17,7 +17,6 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Set;
-import java.util.function.Function;
 import org.apache.maven.artifact.repository.ArtifactRepository;
 import org.apache.maven.plugin.AbstractMojo;
 import org.apache.maven.plugin.MojoExecutionException;
@@ -38,13 +37,15 @@ import org.sonatype.plexus.build.incremental.BuildContext;
  * Generate sources from yang files using user provided set of
  * {@link BasicCodeGenerator}s. Steps of this process:
  * <ol>
- * <li>List yang files from {@link #yangFilesRootDir}</li>
- * <li>Process yang files using Yang Parser</li>
- * <li>For each {@link BasicCodeGenerator} from {@link #codeGenerators}:
- * <ol>
- * <li>Instantiate using default constructor</li>
- * <li>Call {@link BasicCodeGenerator#generateSources(EffectiveModelContext, File, Set, Function)}</li>
- * </ol></li>
+ *   <li>List yang files from {@link #yangFilesRootDir}</li>
+ *   <li>Process yang files using Yang Parser</li>
+ *   <li>For each {@link BasicCodeGenerator} from {@link #codeGenerators}:
+ *     <ol>
+ *       <li>Instantiate using default constructor</li>
+ *       <li>Call {@link BasicCodeGenerator#generateSources(EffectiveModelContext, File, Set,
+ *           org.opendaylight.yangtools.yang2sources.spi.ModuleResourceResolver)}</li>
+ *     </ol>
+ *   </li>
  * </ol>
  */
 @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
index ce884c15c49826751ce95cbc8cfe248c8786b207..9cf4412984279df3daff002dcb526abfcf7469e5 100644 (file)
@@ -393,7 +393,7 @@ class YangToSourcesProcessor {
         }
         final Stopwatch watch = Stopwatch.createStarted();
         Collection<File> generated = codeGenerator.generateSources(context.getContext(), outputDir,
-            context.getYangModules(), context::moduleToResourcePath);
+            context.getYangModules(), context);
 
         LOG.debug("{} Sources generated by {}: {}", LOG_PREFIX, codeGeneratorCfg.getCodeGeneratorClass(), generated);
         LOG.info("{} Sources generated by {}: {} in {}", LOG_PREFIX, codeGeneratorCfg.getCodeGeneratorClass(),
index 17299b321c650f9e36e08036aee6862ef63e24d5..a6adaf910719c6661c354aa8934e754e5f06d2a7 100644 (file)
@@ -23,9 +23,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Map;
-import java.util.Optional;
 import java.util.Set;
-import java.util.function.Function;
 import org.apache.maven.model.Build;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.project.MavenProject;
@@ -38,6 +36,7 @@ import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
 import org.opendaylight.yangtools.yang2sources.spi.BasicCodeGenerator;
 import org.opendaylight.yangtools.yang2sources.spi.MavenProjectAware;
+import org.opendaylight.yangtools.yang2sources.spi.ModuleResourceResolver;
 
 public class GenerateSourcesTest {
 
@@ -110,7 +109,7 @@ public class GenerateSourcesTest {
 
         @Override
         public Collection<File> generateSources(final EffectiveModelContext context, final File outputBaseDir,
-                final Set<Module> currentModules, final Function<Module, Optional<String>> moduleResourcePathResolver)
+                final Set<Module> currentModules, final ModuleResourceResolver moduleResourcePathResolver)
                         throws IOException {
             called++;
             outputDir = outputBaseDir;