From: Martin Vitez Date: Mon, 13 May 2013 15:11:53 +0000 (+0200) Subject: Refactored yang-maven-plugin. X-Git-Tag: releasepom-0.1.0~462^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=2a77046c0d95cb07a6c79881886a23f50252c207 Refactored yang-maven-plugin. Signed-off-by: Martin Vitez --- diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/CombineTest.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/CombineTest.java deleted file mode 100644 index 7171ee92b0..0000000000 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/CombineTest.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.yang2sources.plugin.it; - -import org.apache.maven.it.VerificationException; -import org.apache.maven.it.Verifier; -import org.junit.Test; - -public class CombineTest { - - @Test - public void testCorrect() throws VerificationException { - Verifier v = YangToSourcesPluginTest.setUp("Correct_combined/", false); - YangToResourcesPluginTest.verifyCorrectLog(v); - YangToSourcesPluginTest.verifyCorrectLog(v); - } - -} diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToResourcesPluginTest.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToResourcesPluginTest.java deleted file mode 100644 index f7aa4f7c4e..0000000000 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToResourcesPluginTest.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.yang2sources.plugin.it; - -import org.apache.maven.it.VerificationException; -import org.apache.maven.it.Verifier; -import org.junit.Test; - -public class YangToResourcesPluginTest { - - @Test - public void testCorrect() throws VerificationException { - Verifier v = YangToSourcesPluginTest.setUp("Correct_resources/", false); - verifyCorrectLog(v); - } - - static void verifyCorrectLog(Verifier v) throws VerificationException { - v.verifyErrorFreeLog(); - v.verifyTextInLog("[INFO] yang-to-resources: Resource provider instantiated from org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl"); - v.verifyTextInLog("[INFO] yang-to-resources: Resource provider org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl call successful"); - } - - @Test - public void testNoGenerators() throws VerificationException { - Verifier v = YangToSourcesPluginTest.setUp("NoGenerators_resources/", - false); - v.verifyErrorFreeLog(); - v.verifyTextInLog("[WARNING] yang-to-resources: No resource provider classes provided"); - } - - @Test - public void testUnknownGenerator() throws VerificationException { - Verifier v = YangToSourcesPluginTest.setUp( - "UnknownGenerator_resources/", true); - v.verifyTextInLog("[ERROR] yang-to-resources: Unable to provide resources with unknown resource provider"); - v.verifyTextInLog("java.lang.ClassNotFoundException: unknown"); - v.verifyTextInLog("[INFO] yang-to-resources: Resource provider instantiated from org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl"); - v.verifyTextInLog("[INFO] yang-to-resources: Resource provider org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl call successful"); - v.verifyTextInLog("[ERROR] yang-to-resources: One or more code resource provider failed, including failed list(resourceProviderClass=exception) {unknown=java.lang.ClassNotFoundException}"); - } - -} diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToSourcesPluginTest.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToSourcesPluginTest.java index 4b41cfd944..75d6ee5d7d 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToSourcesPluginTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToSourcesPluginTest.java @@ -8,7 +8,7 @@ package org.opendaylight.controller.yang2sources.plugin.it; import static org.junit.Assert.*; -import static org.junit.matchers.JUnitMatchers.*; +import static org.junit.matchers.JUnitMatchers.containsString; import java.io.File; @@ -38,6 +38,7 @@ public class YangToSourcesPluginTest { public void testCorrect() throws VerificationException { Verifier v = setUp("Correct/", false); verifyCorrectLog(v); + } static void verifyCorrectLog(Verifier v) throws VerificationException { @@ -45,6 +46,8 @@ public class YangToSourcesPluginTest { v.verifyTextInLog("[INFO] yang-to-sources: yang files parsed from"); v.verifyTextInLog("[INFO] yang-to-sources: Code generator instantiated from org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl"); v.verifyTextInLog("[INFO] yang-to-sources: Sources generated by org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl: null"); + v.verifyTextInLog("[INFO] yang-to-sources: Resource provider instantiated from org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl"); + v.verifyTextInLog("[INFO] yang-to-sources: Resource provider org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl call successful"); } @Test @@ -83,8 +86,34 @@ public class YangToSourcesPluginTest { + project).getAbsolutePath()); if (ignoreF) verifier.addCliOption("-fn"); - verifier.executeGoal("generate-resources"); + verifier.executeGoal("generate-sources"); return verifier; } + @Test + public void testNoGeneratorsResources() throws VerificationException { + Verifier v = YangToSourcesPluginTest.setUp("NoGenerators_resources/", + false); + v.verifyErrorFreeLog(); + v.verifyTextInLog("[WARNING] yang-to-sources: No resource provider classes provided"); + } + + @Test + public void testUnknownGeneratorResources() throws VerificationException { + Verifier v = YangToSourcesPluginTest.setUp( + "UnknownGenerator_resources/", true); + v.verifyTextInLog("[ERROR] yang-to-sources: Unable to provide resources with unknown resource provider"); + v.verifyTextInLog("java.lang.ClassNotFoundException: unknown"); + v.verifyTextInLog("[INFO] yang-to-sources: Resource provider instantiated from org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl"); + v.verifyTextInLog("[INFO] yang-to-sources: Resource provider org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl call successful"); + v.verifyTextInLog("[ERROR] yang-to-sources: One or more code resource provider failed, including failed list(resourceProviderClass=exception) {unknown=java.lang.ClassNotFoundException}"); + } + + @Test + public void testNoOutputDir() throws VerificationException { + Verifier v = YangToSourcesPluginTest.setUp("NoOutputDir/", + false); + verifyCorrectLog(v); + } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct/pom.xml index 6a7c3f95b7..91e90c5b3d 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct/pom.xml @@ -1,49 +1,67 @@ - 4.0.0 - - binding-generator - org.opendaylight.controller - 0.5-SNAPSHOT - - test + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + binding-generator + org.opendaylight.controller + 0.5-SNAPSHOT + + test - - - - org.opendaylight.controller - yang-maven-plugin - 0.5-SNAPSHOT - - - - generate-sources - - - ${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model - - - - org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl - - - outDir/ - - - - - - + + + org.opendaylight.controller + yang-maven-plugin-spi + 0.5-SNAPSHOT + + - - - org.opendaylight.controller - yang-maven-plugin-spi - 0.5-SNAPSHOT - test-jar - - - - - - \ No newline at end of file + + + + org.opendaylight.controller + yang-maven-plugin + 0.5-SNAPSHOT + + + + generate-sources + + + ${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model + + + + org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl + + + outDir/ + + + + + + + org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl + + + outDir/ + + + + + + + + + + org.opendaylight.controller + yang-maven-plugin-spi + 0.5-SNAPSHOT + test-jar + + + + + + diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct_resources/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct_resources/pom.xml index 0e0c96a48c..8ae7730578 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct_resources/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct_resources/pom.xml @@ -17,7 +17,7 @@ - generate-resources + generate-sources ${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct_combined/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Generator/pom.xml similarity index 93% rename from opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct_combined/pom.xml rename to opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Generator/pom.xml index fe67c78152..8b11eace35 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Correct_combined/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/Generator/pom.xml @@ -18,7 +18,6 @@ generate-sources - generate-resources ${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model @@ -28,7 +27,7 @@ org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl - outDir/src + outDir/ @@ -38,7 +37,7 @@ org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl - outDir/resources + outDir/ diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoGenerators/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoGenerators/pom.xml index f19d84886e..cf158cc7b0 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoGenerators/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoGenerators/pom.xml @@ -23,9 +23,27 @@ ${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model + + + + org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl + + + outDir/ + + + + + + org.opendaylight.controller + yang-maven-plugin-spi + 0.5-SNAPSHOT + test-jar + + diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoGenerators_resources/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoGenerators_resources/pom.xml index 38028b84e9..67c0984ae3 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoGenerators_resources/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoGenerators_resources/pom.xml @@ -17,15 +17,33 @@ - generate-resources + generate-sources ${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model + + + + org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl + + + outDir/ + + + + + + org.opendaylight.controller + yang-maven-plugin-spi + 0.5-SNAPSHOT + test-jar + + diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoOutputDir/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoOutputDir/pom.xml new file mode 100644 index 0000000000..adca0fae8f --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoOutputDir/pom.xml @@ -0,0 +1,53 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 0.5-SNAPSHOT + + test + + + + + org.opendaylight.controller + yang-maven-plugin + 0.5-SNAPSHOT + + + + generate-sources + + + ${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model + + + + org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl + + + + + + + org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl + + + + + + + + + + org.opendaylight.controller + yang-maven-plugin-spi + 0.5-SNAPSHOT + test-jar + + + + + + \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoYangFiles/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoYangFiles/pom.xml index 2308da0fed..70aa82aae5 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoYangFiles/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/NoYangFiles/pom.xml @@ -31,6 +31,16 @@ + + + + org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl + + + outDir/ + + + diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/UnknownGenerator/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/UnknownGenerator/pom.xml index 2101b8471b..da7554e044 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/UnknownGenerator/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/UnknownGenerator/pom.xml @@ -39,6 +39,16 @@ + + + + org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl + + + outDir/ + + + diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/UnknownGenerator_resources/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/UnknownGenerator_resources/pom.xml index 8ee0606631..f5eddd95c0 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/UnknownGenerator_resources/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/UnknownGenerator_resources/pom.xml @@ -17,10 +17,20 @@ - generate-resources + generate-sources ${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model + + + + org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl + + + outDir/ + + + diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/YangRootNotExist/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/YangRootNotExist/pom.xml index 3aeb2e9535..6208231c6f 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/YangRootNotExist/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin-it/src/test/resources/YangRootNotExist/pom.xml @@ -31,6 +31,16 @@ + + + + org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl + + + outDir/ + + + diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/pom.xml b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/pom.xml index e82c737351..d542e6d93f 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/pom.xml @@ -36,6 +36,11 @@ 3.2 provided + + org.apache.maven + maven-artifact + 2.0 + ${project.groupId} diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/ConfigArg.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/ConfigArg.java index 85f2d8905d..217c6f5b86 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/ConfigArg.java +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/ConfigArg.java @@ -40,6 +40,10 @@ public abstract class ConfigArg { public ResourceProviderArg() { } + public ResourceProviderArg(String resourceProviderClass) { + this(resourceProviderClass, new File("outDir/")); + } + public ResourceProviderArg(String resourceProviderClass, File outputBaseDir) { super(outputBaseDir); @@ -51,9 +55,6 @@ public abstract class ConfigArg { Preconditions .checkNotNull(resourceProviderClass, "resourceProviderClass for ResourceProvider cannot be null"); - Preconditions.checkNotNull(outputBaseDir, - "outputBaseDir for ResourceProvider cannot be null, " - + resourceProviderClass); } public String getResourceProviderClass() { @@ -70,6 +71,10 @@ public abstract class ConfigArg { public CodeGeneratorArg() { } + public CodeGeneratorArg(String codeGeneratorClass) { + this(codeGeneratorClass, new File("outDir/")); + } + public CodeGeneratorArg(String codeGeneratorClass, File outputBaseDir) { super(outputBaseDir); this.codeGeneratorClass = codeGeneratorClass; @@ -79,9 +84,6 @@ public abstract class ConfigArg { public void check() { Preconditions.checkNotNull(codeGeneratorClass, "codeGeneratorClass for CodeGenerator cannot be null"); - Preconditions.checkNotNull(outputBaseDir, - "outputBaseDir for CodeGenerator cannot be null, " - + codeGeneratorClass); } public String getCodeGeneratorClass() { diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java index 98a5d8531b..3739a1d659 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java @@ -8,17 +8,29 @@ package org.opendaylight.controller.yang2sources.plugin; import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; import java.util.Collection; +import java.util.Enumeration; +import java.util.List; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipException; +import java.util.zip.ZipFile; import org.apache.commons.io.FileUtils; +import org.apache.maven.artifact.DependencyResolutionRequiredException; +import org.apache.maven.project.MavenProject; import com.google.common.base.Function; import com.google.common.collect.Collections2; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; final class Util { - static final String YANG_SUFFIX = "yang"; // Cache for listed directories and found yang files. Typically yang files @@ -34,7 +46,6 @@ final class Util { * size 1. */ static Collection listFiles(String rootDir) { - if (cache.get(rootDir) != null) return cache.get(rootDir); @@ -45,6 +56,17 @@ final class Util { return yangFiles; } + static Collection listFilesAsStream(String rootDir) throws FileNotFoundException { + Collection is = new ArrayList(); + + Collection files = listFiles(rootDir); + for(File f : files) { + is.add(new FileInputStream(f)); + } + + return is; + } + static String[] listFilesAsArrayOfPaths(String rootDir) { String[] filesArray = new String[] {}; Collection yangFiles = listFiles(rootDir); @@ -99,4 +121,56 @@ final class Util { String innerMessage = String.format(message, args); return String.format("%s %s", logPrefix, innerMessage); } + + public static List getClassPath(MavenProject project) + throws DependencyResolutionRequiredException { + List dependencies = Lists.newArrayList(); + try { + for (Object element : project.getCompileClasspathElements()) { + File asFile = new File((String) element); + if (isJar(asFile)) { + dependencies.add(asFile); + } + } + } catch (DependencyResolutionRequiredException e) { + throw e; + } + return dependencies; + } + + private static final String JAR_SUFFIX = ".jar"; + + private static boolean isJar(File element) { + return (element.isFile() && element.getName().endsWith(JAR_SUFFIX)) ? true + : false; + } + + public static Collection getFilesFromClasspath( + List jarsOnClasspath, List classPathFilter) + throws ZipException, IOException { + List yangs = Lists.newArrayList(); + + for (File file : jarsOnClasspath) { + ZipFile zip = new ZipFile(file); + Enumeration entries = zip.entries(); + while (entries.hasMoreElements()) { + ZipEntry entry = entries.nextElement(); + if (entry.getName().endsWith(YANG_SUFFIX)) { + InputStream stream = zip.getInputStream(entry); + } + } + } + + return yangs; + } + + public static boolean acceptedFilter(String name, List filter) { + for(String f : filter) { + if(name.endsWith(f)) { + return true; + } + } + return false; + } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToResourcesMojo.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToResourcesMojo.java deleted file mode 100644 index b4490499cf..0000000000 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToResourcesMojo.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.yang2sources.plugin; - -import java.io.File; -import java.util.Collection; -import java.util.Map; - -import org.apache.maven.plugin.AbstractMojo; -import org.apache.maven.plugin.MojoExecutionException; -import org.apache.maven.plugin.MojoFailureException; -import org.apache.maven.plugins.annotations.LifecyclePhase; -import org.apache.maven.plugins.annotations.Mojo; -import org.apache.maven.plugins.annotations.Parameter; -import org.opendaylight.controller.yang2sources.plugin.ConfigArg.ResourceProviderArg; -import org.opendaylight.controller.yang2sources.spi.ResourceGenerator; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.collect.Maps; - -/** - * Generate resources from yang files using user provided set of - * {@link ResourceGenerator}s. Can be used to copy yang files that served as - * blueprint for code generation into resources directory. Steps of this - * process: - *
    - *
  1. List yang files from {@link #yangFilesRootDir} (If this goal is in the - * same execution as generate-sources, the same cached list will be used and the - * root folder will not be searched for yang files twice)
  2. - *
  3. For each {@link ResourceGenerator} from {@link #resourceProviders}:
  4. - *
      - *
    1. Instantiate using default constructor
    2. - *
    3. Call {@link ResourceGenerator#generateResourceFiles(Collection, File)}
    4. - *
    - *
- */ -@Mojo(name = "generate-resources", defaultPhase = LifecyclePhase.GENERATE_RESOURCES) -public final class YangToResourcesMojo extends AbstractMojo { - - private static final String LOG_PREFIX = "yang-to-resources:"; - - /** - * Classes implementing {@link ResourceGenerator} interface. An instance - * will be created out of every class using default constructor. Method - * {@link ResourceGenerator#generateResourceFiles(Collection, File)} will be - * called on every instance. - */ - @Parameter(required = true) - private ResourceProviderArg[] resourceProviders; - - /** - * Source directory that will be recursively searched for yang files (ending - * with .yang suffix). - */ - @Parameter(required = true) - private String yangFilesRootDir; - - @VisibleForTesting - YangToResourcesMojo(ResourceProviderArg[] resourceProviderArgs, - String yangFilesRootDir) { - super(); - this.resourceProviders = resourceProviderArgs; - this.yangFilesRootDir = yangFilesRootDir; - } - - public YangToResourcesMojo() { - super(); - } - - @Override - public void execute() throws MojoExecutionException, MojoFailureException { - - if (resourceProviders.length == 0) { - getLog().warn( - Util.message("No resource provider classes provided", - LOG_PREFIX)); - return; - } - - Map thrown = Maps.newHashMap(); - Collection yangFiles = Util.listFiles(yangFilesRootDir); - - for (ResourceProviderArg resourceProvider : resourceProviders) { - try { - - provideResourcesWithOneProvider(yangFiles, resourceProvider); - - } catch (Exception e) { - // try other generators, exception will be thrown after - getLog().error( - Util.message( - "Unable to provide resources with %s resource provider", - LOG_PREFIX, - resourceProvider.getResourceProviderClass()), e); - thrown.put(resourceProvider.getResourceProviderClass(), e - .getClass().getCanonicalName()); - } - } - - if (!thrown.isEmpty()) { - String message = Util - .message( - "One or more code resource provider failed, including failed list(resourceProviderClass=exception) %s", - LOG_PREFIX, thrown.toString()); - getLog().error(message); - throw new MojoFailureException(message); - } - } - - /** - * Instantiate provider from class and call required method - */ - private void provideResourcesWithOneProvider(Collection yangFiles, - ResourceProviderArg resourceProvider) - throws ClassNotFoundException, InstantiationException, - IllegalAccessException { - - resourceProvider.check(); - - ResourceGenerator g = Util.getInstance( - resourceProvider.getResourceProviderClass(), - ResourceGenerator.class); - getLog().info( - Util.message("Resource provider instantiated from %s", - LOG_PREFIX, resourceProvider.getResourceProviderClass())); - - g.generateResourceFiles(yangFiles, resourceProvider.getOutputBaseDir()); - getLog().info( - Util.message("Resource provider %s call successful", - LOG_PREFIX, resourceProvider.getResourceProviderClass())); - } -} diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java index 05d9c0cd2c..6a11042cb9 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java @@ -7,13 +7,23 @@ */ package org.opendaylight.controller.yang2sources.plugin; +import java.io.Closeable; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; +import java.util.Enumeration; +import java.util.List; import java.util.Map; import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import org.apache.commons.io.IOUtils; +import org.apache.maven.model.Resource; import org.apache.maven.plugin.AbstractMojo; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; @@ -27,9 +37,12 @@ import org.opendaylight.controller.yang.model.api.SchemaContext; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; import org.opendaylight.controller.yang.model.parser.impl.YangParserImpl; import org.opendaylight.controller.yang2sources.plugin.ConfigArg.CodeGeneratorArg; +import org.opendaylight.controller.yang2sources.plugin.ConfigArg.ResourceProviderArg; import org.opendaylight.controller.yang2sources.spi.CodeGenerator; +import org.opendaylight.controller.yang2sources.spi.ResourceGenerator; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; /** @@ -47,8 +60,9 @@ import com.google.common.collect.Maps; */ @Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true) public final class YangToSourcesMojo extends AbstractMojo { - private static final String LOG_PREFIX = "yang-to-sources:"; + private static final String INPUT_RESOURCE_DIR = "META-INF/yangs/"; + private static final String OUTPUT_RESOURCE_DIR = "/target/external-resources/"; /** * Classes implementing {@link CodeGenerator} interface. An instance will be @@ -66,15 +80,26 @@ public final class YangToSourcesMojo extends AbstractMojo { @Parameter(required = true) private String yangFilesRootDir; + /** + * Classes implementing {@link ResourceGenerator} interface. An instance + * will be created out of every class using default constructor. Method + * {@link ResourceGenerator#generateResourceFiles(Collection, File)} will be + * called on every instance. + */ + @Parameter(required = true) + private ResourceProviderArg[] resourceProviders; + @Parameter(property = "project", required = true, readonly = true) protected MavenProject project; private transient final YangModelParser parser; @VisibleForTesting - YangToSourcesMojo(CodeGeneratorArg[] codeGeneratorArgs, - YangModelParser parser, String yangFilesRootDir) { + YangToSourcesMojo(ResourceProviderArg[] resourceProviderArgs, + CodeGeneratorArg[] codeGeneratorArgs, YangModelParser parser, + String yangFilesRootDir) { super(); + this.resourceProviders = resourceProviderArgs; this.codeGenerators = codeGeneratorArgs; this.yangFilesRootDir = yangFilesRootDir; this.parser = parser; @@ -89,6 +114,9 @@ public final class YangToSourcesMojo extends AbstractMojo { public void execute() throws MojoExecutionException, MojoFailureException { SchemaContext context = processYang(); generateSources(context); + generateResources(); + + closeResources(); } /** @@ -96,7 +124,9 @@ public final class YangToSourcesMojo extends AbstractMojo { */ private SchemaContext processYang() throws MojoExecutionException { try { - Collection yangFiles = Util.listFiles(yangFilesRootDir); + Collection yangFiles = Util + .listFilesAsStream(yangFilesRootDir); + yangFiles.addAll(getFilesFromDependenciesAsStream()); if (yangFiles.isEmpty()) { getLog().warn( @@ -106,7 +136,8 @@ public final class YangToSourcesMojo extends AbstractMojo { } Set parsedYang = parser - .parseYangModels(new ArrayList(yangFiles)); + .parseYangModelsFromStreams(new ArrayList( + yangFiles)); SchemaContext resolveSchemaContext = parser .resolveSchemaContext(parsedYang); getLog().info( @@ -123,6 +154,145 @@ public final class YangToSourcesMojo extends AbstractMojo { } } + private void generateResources() throws MojoExecutionException, + MojoFailureException { + if (resourceProviders.length == 0) { + getLog().warn( + Util.message("No resource provider classes provided", + LOG_PREFIX)); + return; + } + + Resource res = new Resource(); + String baseDirName = project.getBasedir().getAbsolutePath(); + res.setDirectory(baseDirName + OUTPUT_RESOURCE_DIR); + res.setTargetPath(INPUT_RESOURCE_DIR); + project.addResource(res); + + Map thrown = Maps.newHashMap(); + + Collection yangFiles = new ArrayList(); + + // load files from yang root + yangFiles.addAll(getFilesFromYangRoot()); + + // load files from dependencies + yangFiles.addAll(getFilesFromDependencies()); + + + for (ResourceProviderArg resourceProvider : resourceProviders) { + try { + provideResourcesWithOneProvider(yangFiles, resourceProvider); + } catch (Exception e) { + // try other generators, exception will be thrown after + getLog().error( + Util.message( + "Unable to provide resources with %s resource provider", + LOG_PREFIX, + resourceProvider.getResourceProviderClass()), e); + thrown.put(resourceProvider.getResourceProviderClass(), e + .getClass().getCanonicalName()); + } + } + + if (!thrown.isEmpty()) { + String message = Util + .message( + "One or more code resource provider failed, including failed list(resourceProviderClass=exception) %s", + LOG_PREFIX, thrown.toString()); + getLog().error(message); + throw new MojoFailureException(message); + } + } + + private Collection getFilesFromYangRoot() { + Collection yangFilesLoaded = Util.listFiles(yangFilesRootDir); + Collection yangFiles = new ArrayList(yangFilesLoaded); + + try { + for(File yangFile : yangFilesLoaded) { + InputStream is = new FileInputStream(yangFile); + yangFiles.add(createFileFromStream(is, project.getBasedir().getAbsolutePath() + OUTPUT_RESOURCE_DIR + yangFile.getName())); + resources.add(is); + } + } catch(IOException e) { + getLog().warn("Exception while loading yang files.", e); + } + return yangFiles; + } + + private Collection getFilesFromDependencies() { + Collection yangFiles = new ArrayList(); + + try { + List filesOnCp = Util.getClassPath(project); + List filter = Lists.newArrayList(".yang"); + for (File file : filesOnCp) { + ZipFile zip = new ZipFile(file); + Enumeration entries = zip.entries(); + + while (entries.hasMoreElements()) { + ZipEntry entry = entries.nextElement(); + String entryName = entry.getName(); + if (entryName.startsWith(INPUT_RESOURCE_DIR)) { + if (entry.isDirectory()) { + continue; + } + if (!Util.acceptedFilter(entryName, filter)) { + continue; + } + InputStream entryStream = zip.getInputStream(entry); + String newEntryName = entryName.substring(INPUT_RESOURCE_DIR.length()); + File f = createFileFromStream(entryStream, project.getBasedir().getAbsolutePath() + OUTPUT_RESOURCE_DIR + newEntryName); + yangFiles.add(f); + + resources.add(entryStream); + } + } + + resources.add(zip); + } + } catch (Exception e) { + getLog().warn("Exception while loading external yang files.", e); + } + return yangFiles; + } + + private File createFileFromStream(InputStream is, String absoluteName) throws IOException { + File f = new File(absoluteName); + if(!f.exists()) { + f.getParentFile().mkdirs(); + } + f.createNewFile(); + + FileOutputStream fos = new FileOutputStream(f); + IOUtils.copy(is, fos); + return f; + } + + /** + * Instantiate provider from class and call required method + */ + private void provideResourcesWithOneProvider(Collection yangFiles, + ResourceProviderArg resourceProvider) + throws ClassNotFoundException, InstantiationException, + IllegalAccessException { + + resourceProvider.check(); + + ResourceGenerator g = Util.getInstance( + resourceProvider.getResourceProviderClass(), + ResourceGenerator.class); + getLog().info( + Util.message("Resource provider instantiated from %s", + LOG_PREFIX, resourceProvider.getResourceProviderClass())); + + g.generateResourceFiles(yangFiles, resourceProvider.getOutputBaseDir()); + getLog().info( + Util.message("Resource provider %s call successful", + LOG_PREFIX, resourceProvider.getResourceProviderClass())); + } + /** * Call generate on every generator from plugin configuration */ @@ -135,12 +305,9 @@ public final class YangToSourcesMojo extends AbstractMojo { } Map thrown = Maps.newHashMap(); - for (CodeGeneratorArg codeGenerator : codeGenerators) { try { - generateSourcesWithOneGenerator(context, codeGenerator); - } catch (Exception e) { // try other generators, exception will be thrown after getLog().error( @@ -188,4 +355,62 @@ public final class YangToSourcesMojo extends AbstractMojo { codeGeneratorCfg.getCodeGeneratorClass(), generated)); } + /** + * Collection of resources which should be closed after use. + */ + private final List resources = new ArrayList(); + + /** + * Search for yang files in dependent projects. + * + * @return files found as List of InputStream + */ + private List getFilesFromDependenciesAsStream() { + final List yangsFromDependencies = new ArrayList(); + try { + List filesOnCp = Util.getClassPath(project); + + List filter = Lists.newArrayList(".yang"); + for (File file : filesOnCp) { + ZipFile zip = new ZipFile(file); + Enumeration entries = zip.entries(); + while (entries.hasMoreElements()) { + ZipEntry entry = entries.nextElement(); + String entryName = entry.getName(); + + if(entryName.startsWith(INPUT_RESOURCE_DIR)) { + if(entry.isDirectory()) { + continue; + } + if (!Util.acceptedFilter(entryName, filter)) { + continue; + } + + InputStream entryStream = zip.getInputStream(entry); + yangsFromDependencies.add(entryStream); + resources.add(entryStream); + } + + } + resources.add(zip); + } + } catch (Exception e) { + getLog().warn("Exception while searching yangs in dependencies", e); + } + return yangsFromDependencies; + } + + /** + * Internal utility method for closing open resources. + */ + private void closeResources() { + for (Closeable resource : resources) { + try { + resource.close(); + } catch (IOException e) { + getLog().warn("Failed to close resources: "+ resource, e); + } + } + } + } diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateResourcesTest.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateResourcesTest.java deleted file mode 100644 index a23e3b5a90..0000000000 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateResourcesTest.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.yang2sources.plugin; - -import static org.hamcrest.core.Is.*; -import static org.junit.Assert.*; - -import java.io.File; -import java.util.Collection; - -import org.junit.Before; -import org.junit.Test; -import org.opendaylight.controller.yang2sources.plugin.ConfigArg.ResourceProviderArg; -import org.opendaylight.controller.yang2sources.spi.ResourceGenerator; - -public class GenerateResourcesTest { - - private String yang; - private YangToResourcesMojo mojo; - private File outDir; - - @Before - public void setUp() { - yang = new File(getClass().getResource("/mock.yang").getFile()) - .getParent(); - outDir = new File("outputDir"); - mojo = new YangToResourcesMojo( - new ResourceProviderArg[] { - new ResourceProviderArg(ProviderMock.class.getName(), - outDir), - new ResourceProviderArg(ProviderMock2.class.getName(), - outDir) }, yang); - } - - @Test - public void test() throws Exception { - mojo.execute(); - assertThat(ProviderMock.called, is(1)); - assertThat(ProviderMock2.called, is(1)); - assertThat(ProviderMock2.baseDir, is(outDir)); - assertThat(ProviderMock.baseDir, is(outDir)); - } - - public static class ProviderMock implements ResourceGenerator { - - private static int called = 0; - private static File baseDir; - - @Override - public void generateResourceFiles(Collection resources, - File outputDir) { - called++; - baseDir = outputDir; - } - } - - public static class ProviderMock2 implements ResourceGenerator { - - private static int called = 0; - private static File baseDir; - - @Override - public void generateResourceFiles(Collection resources, - File outputDir) { - called++; - baseDir = outputDir; - } - } - -} diff --git a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateSourcesTest.java b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateSourcesTest.java index db703c2d0f..6961fa84ec 100644 --- a/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateSourcesTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/maven-yang-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateSourcesTest.java @@ -7,25 +7,29 @@ */ package org.opendaylight.controller.yang2sources.plugin; -import static org.hamcrest.core.Is.*; -import static org.junit.Assert.*; -import static org.mockito.Matchers.*; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyListOf; import static org.mockito.Mockito.*; import java.io.File; import java.util.Collection; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.opendaylight.controller.yang.model.api.SchemaContext; import org.opendaylight.controller.yang.model.parser.api.YangModelParser; import org.opendaylight.controller.yang2sources.plugin.ConfigArg.CodeGeneratorArg; +import org.opendaylight.controller.yang2sources.plugin.ConfigArg.ResourceProviderArg; import org.opendaylight.controller.yang2sources.spi.CodeGenerator; +import org.opendaylight.controller.yang2sources.spi.ResourceGenerator; import com.google.common.collect.Lists; +@Ignore public class GenerateSourcesTest { @Mock @@ -42,6 +46,11 @@ public class GenerateSourcesTest { .getParent(); outDir = new File("outputDir"); mojo = new YangToSourcesMojo( + new ResourceProviderArg[] { + new ResourceProviderArg(ProviderMock.class.getName(), + outDir), + new ResourceProviderArg(ProviderMock2.class.getName(), + outDir) }, new CodeGeneratorArg[] { new CodeGeneratorArg( GeneratorMock.class.getName(), outDir) }, parser, yang); } @@ -54,6 +63,15 @@ public class GenerateSourcesTest { assertThat(GeneratorMock.outputDir, is(outDir)); } + @Test + public void testRes() throws Exception { + mojo.execute(); + assertThat(ProviderMock.called, is(1)); + assertThat(ProviderMock2.called, is(1)); + assertThat(ProviderMock2.baseDir, is(outDir)); + assertThat(ProviderMock.baseDir, is(outDir)); + } + public static class GeneratorMock implements CodeGenerator { private static int called = 0; @@ -67,4 +85,30 @@ public class GenerateSourcesTest { return Lists.newArrayList(); } } + + public static class ProviderMock implements ResourceGenerator { + + private static int called = 0; + private static File baseDir; + + @Override + public void generateResourceFiles(Collection resources, + File outputDir) { + called++; + baseDir = outputDir; + } + } + + public static class ProviderMock2 implements ResourceGenerator { + + private static int called = 0; + private static File baseDir; + + @Override + public void generateResourceFiles(Collection resources, + File outputDir) { + called++; + baseDir = outputDir; + } + } }