+++ /dev/null
-/*
- * 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);
- }
-
-}
+++ /dev/null
-/*
- * 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}");
- }
-
-}
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;
public void testCorrect() throws VerificationException {
Verifier v = setUp("Correct/", false);
verifyCorrectLog(v);
+
}
static void verifyCorrectLog(Verifier v) throws VerificationException {
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
+ 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);
+ }
+
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <artifactId>binding-generator</artifactId>
- <groupId>org.opendaylight.controller</groupId>
- <version>0.5-SNAPSHOT</version>
- </parent>
- <artifactId>test</artifactId>
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>binding-generator</artifactId>
+ <groupId>org.opendaylight.controller</groupId>
+ <version>0.5-SNAPSHOT</version>
+ </parent>
+ <artifactId>test</artifactId>
- <build>
- <plugins>
- <plugin>
- <groupId>org.opendaylight.controller</groupId>
- <artifactId>yang-maven-plugin</artifactId>
- <version>0.5-SNAPSHOT</version>
- <executions>
- <execution>
- <goals>
- <goal>generate-sources</goal>
- </goals>
- <configuration>
- <yangFilesRootDir>${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model</yangFilesRootDir>
- <codeGenerators>
- <generator>
- <codeGeneratorClass>
- org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl
- </codeGeneratorClass>
- <outputBaseDir>
- outDir/
- </outputBaseDir>
- </generator>
- </codeGenerators>
- </configuration>
- </execution>
- </executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>yang-maven-plugin-spi</artifactId>
+ <version>0.5-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
- <dependencies>
- <dependency>
- <groupId>org.opendaylight.controller</groupId>
- <artifactId>yang-maven-plugin-spi</artifactId>
- <version>0.5-SNAPSHOT</version>
- <type>test-jar</type>
- </dependency>
- </dependencies>
- </plugin>
- </plugins>
- </build>
-</project>
\ No newline at end of file
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>yang-maven-plugin</artifactId>
+ <version>0.5-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>generate-sources</goal>
+ </goals>
+ <configuration>
+ <yangFilesRootDir>${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model</yangFilesRootDir>
+ <codeGenerators>
+ <generator>
+ <codeGeneratorClass>
+ org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl
+ </codeGeneratorClass>
+ <outputBaseDir>
+ outDir/
+ </outputBaseDir>
+ </generator>
+ </codeGenerators>
+ <resourceProviders>
+ <provider>
+ <resourceProviderClass>
+ org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl
+ </resourceProviderClass>
+ <outputBaseDir>
+ outDir/
+ </outputBaseDir>
+ </provider>
+ </resourceProviders>
+ </configuration>
+ </execution>
+ </executions>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>yang-maven-plugin-spi</artifactId>
+ <version>0.5-SNAPSHOT</version>
+ <type>test-jar</type>
+ </dependency>
+ </dependencies>
+ </plugin>
+ </plugins>
+ </build>
+</project>
<executions>
<execution>
<goals>
- <goal>generate-resources</goal>
+ <goal>generate-sources</goal>
</goals>
<configuration>
<yangFilesRootDir>${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model</yangFilesRootDir>
<execution>
<goals>
<goal>generate-sources</goal>
- <goal>generate-resources</goal>
</goals>
<configuration>
<yangFilesRootDir>${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model</yangFilesRootDir>
org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl
</codeGeneratorClass>
<outputBaseDir>
- outDir/src
+ outDir/
</outputBaseDir>
</generator>
</codeGenerators>
org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl
</resourceProviderClass>
<outputBaseDir>
- outDir/resources
+ outDir/
</outputBaseDir>
</provider>
</resourceProviders>
<yangFilesRootDir>${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model</yangFilesRootDir>
<codeGenerators>
</codeGenerators>
+ <resourceProviders>
+ <provider>
+ <resourceProviderClass>
+ org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl
+ </resourceProviderClass>
+ <outputBaseDir>
+ outDir/
+ </outputBaseDir>
+ </provider>
+ </resourceProviders>
</configuration>
</execution>
</executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>yang-maven-plugin-spi</artifactId>
+ <version>0.5-SNAPSHOT</version>
+ <type>test-jar</type>
+ </dependency>
+ </dependencies>
</plugin>
</plugins>
</build>
<executions>
<execution>
<goals>
- <goal>generate-resources</goal>
+ <goal>generate-sources</goal>
</goals>
<configuration>
<yangFilesRootDir>${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model</yangFilesRootDir>
+ <codeGenerators>
+ <generator>
+ <codeGeneratorClass>
+ org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl
+ </codeGeneratorClass>
+ <outputBaseDir>
+ outDir/
+ </outputBaseDir>
+ </generator>
+ </codeGenerators>
<resourceProviders>
</resourceProviders>
</configuration>
</execution>
</executions>
+ <dependencies>
+ <dependency>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>yang-maven-plugin-spi</artifactId>
+ <version>0.5-SNAPSHOT</version>
+ <type>test-jar</type>
+ </dependency>
+ </dependencies>
</plugin>
</plugins>
</build>
--- /dev/null
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+ <parent>
+ <artifactId>binding-generator</artifactId>
+ <groupId>org.opendaylight.controller</groupId>
+ <version>0.5-SNAPSHOT</version>
+ </parent>
+ <artifactId>test</artifactId>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>yang-maven-plugin</artifactId>
+ <version>0.5-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>generate-sources</goal>
+ </goals>
+ <configuration>
+ <yangFilesRootDir>${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model</yangFilesRootDir>
+ <codeGenerators>
+ <generator>
+ <codeGeneratorClass>
+ org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl
+ </codeGeneratorClass>
+ </generator>
+ </codeGenerators>
+ <resourceProviders>
+ <provider>
+ <resourceProviderClass>
+ org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl
+ </resourceProviderClass>
+ </provider>
+ </resourceProviders>
+ </configuration>
+ </execution>
+ </executions>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.opendaylight.controller</groupId>
+ <artifactId>yang-maven-plugin-spi</artifactId>
+ <version>0.5-SNAPSHOT</version>
+ <type>test-jar</type>
+ </dependency>
+ </dependencies>
+ </plugin>
+ </plugins>
+ </build>
+</project>
\ No newline at end of file
</outputBaseDir>
</generator>
</codeGenerators>
+ <resourceProviders>
+ <provider>
+ <resourceProviderClass>
+ org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl
+ </resourceProviderClass>
+ <outputBaseDir>
+ outDir/
+ </outputBaseDir>
+ </provider>
+ </resourceProviders>
</configuration>
</execution>
</executions>
</outputBaseDir>
</generator>
</codeGenerators>
+ <resourceProviders>
+ <provider>
+ <resourceProviderClass>
+ org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl
+ </resourceProviderClass>
+ <outputBaseDir>
+ outDir/
+ </outputBaseDir>
+ </provider>
+ </resourceProviders>
</configuration>
</execution>
</executions>
<executions>
<execution>
<goals>
- <goal>generate-resources</goal>
+ <goal>generate-sources</goal>
</goals>
<configuration>
<yangFilesRootDir>${basedir}/../../../../../yang-model-parser-impl/src/test/resources/model</yangFilesRootDir>
+ <codeGenerators>
+ <generator>
+ <codeGeneratorClass>
+ org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl
+ </codeGeneratorClass>
+ <outputBaseDir>
+ outDir/
+ </outputBaseDir>
+ </generator>
+ </codeGenerators>
<resourceProviders>
<provider>
<resourceProviderClass>
</outputBaseDir>
</generator>
</codeGenerators>
+ <resourceProviders>
+ <provider>
+ <resourceProviderClass>
+ org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl
+ </resourceProviderClass>
+ <outputBaseDir>
+ outDir/
+ </outputBaseDir>
+ </provider>
+ </resourceProviders>
</configuration>
</execution>
</executions>
<version>3.2</version>
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-artifact</artifactId>
+ <version>2.0</version>
+ </dependency>
<dependency>
<groupId>${project.groupId}</groupId>
public ResourceProviderArg() {
}
+ public ResourceProviderArg(String resourceProviderClass) {
+ this(resourceProviderClass, new File("outDir/"));
+ }
+
public ResourceProviderArg(String resourceProviderClass,
File outputBaseDir) {
super(outputBaseDir);
Preconditions
.checkNotNull(resourceProviderClass,
"resourceProviderClass for ResourceProvider cannot be null");
- Preconditions.checkNotNull(outputBaseDir,
- "outputBaseDir for ResourceProvider cannot be null, "
- + resourceProviderClass);
}
public String getResourceProviderClass() {
public CodeGeneratorArg() {
}
+ public CodeGeneratorArg(String codeGeneratorClass) {
+ this(codeGeneratorClass, new File("outDir/"));
+ }
+
public CodeGeneratorArg(String codeGeneratorClass, File outputBaseDir) {
super(outputBaseDir);
this.codeGeneratorClass = codeGeneratorClass;
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() {
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
* size 1.
*/
static Collection<File> listFiles(String rootDir) {
-
if (cache.get(rootDir) != null)
return cache.get(rootDir);
return yangFiles;
}
+ static Collection<InputStream> listFilesAsStream(String rootDir) throws FileNotFoundException {
+ Collection<InputStream> is = new ArrayList<InputStream>();
+
+ Collection<File> files = listFiles(rootDir);
+ for(File f : files) {
+ is.add(new FileInputStream(f));
+ }
+
+ return is;
+ }
+
static String[] listFilesAsArrayOfPaths(String rootDir) {
String[] filesArray = new String[] {};
Collection<File> yangFiles = listFiles(rootDir);
String innerMessage = String.format(message, args);
return String.format("%s %s", logPrefix, innerMessage);
}
+
+ public static List<File> getClassPath(MavenProject project)
+ throws DependencyResolutionRequiredException {
+ List<File> 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<File> getFilesFromClasspath(
+ List<File> jarsOnClasspath, List<String> classPathFilter)
+ throws ZipException, IOException {
+ List<File> yangs = Lists.newArrayList();
+
+ for (File file : jarsOnClasspath) {
+ ZipFile zip = new ZipFile(file);
+ Enumeration<? extends ZipEntry> 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<String> filter) {
+ for(String f : filter) {
+ if(name.endsWith(f)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
}
+++ /dev/null
-/*
- * 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:
- * <ol>
- * <li>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)</li>
- * <li>For each {@link ResourceGenerator} from {@link #resourceProviders}:</li>
- * <ol>
- * <li>Instantiate using default constructor</li>
- * <li>Call {@link ResourceGenerator#generateResourceFiles(Collection, File)}</li>
- * </ol>
- * </ol>
- */
-@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<String, String> thrown = Maps.newHashMap();
- Collection<File> 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<File> 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()));
- }
-}
*/
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;
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;
/**
*/
@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
@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;
public void execute() throws MojoExecutionException, MojoFailureException {
SchemaContext context = processYang();
generateSources(context);
+ generateResources();
+
+ closeResources();
}
/**
*/
private SchemaContext processYang() throws MojoExecutionException {
try {
- Collection<File> yangFiles = Util.listFiles(yangFilesRootDir);
+ Collection<InputStream> yangFiles = Util
+ .listFilesAsStream(yangFilesRootDir);
+ yangFiles.addAll(getFilesFromDependenciesAsStream());
if (yangFiles.isEmpty()) {
getLog().warn(
}
Set<Module> parsedYang = parser
- .parseYangModels(new ArrayList<File>(yangFiles));
+ .parseYangModelsFromStreams(new ArrayList<InputStream>(
+ yangFiles));
SchemaContext resolveSchemaContext = parser
.resolveSchemaContext(parsedYang);
getLog().info(
}
}
+ 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<String, String> thrown = Maps.newHashMap();
+
+ Collection<File> yangFiles = new ArrayList<File>();
+
+ // 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<File> getFilesFromYangRoot() {
+ Collection<File> yangFilesLoaded = Util.listFiles(yangFilesRootDir);
+ Collection<File> yangFiles = new ArrayList<File>(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<File> getFilesFromDependencies() {
+ Collection<File> yangFiles = new ArrayList<File>();
+
+ try {
+ List<File> filesOnCp = Util.getClassPath(project);
+ List<String> filter = Lists.newArrayList(".yang");
+ for (File file : filesOnCp) {
+ ZipFile zip = new ZipFile(file);
+ Enumeration<? extends ZipEntry> 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<File> 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
*/
}
Map<String, String> thrown = Maps.newHashMap();
-
for (CodeGeneratorArg codeGenerator : codeGenerators) {
try {
-
generateSourcesWithOneGenerator(context, codeGenerator);
-
} catch (Exception e) {
// try other generators, exception will be thrown after
getLog().error(
codeGeneratorCfg.getCodeGeneratorClass(), generated));
}
+ /**
+ * Collection of resources which should be closed after use.
+ */
+ private final List<Closeable> resources = new ArrayList<Closeable>();
+
+ /**
+ * Search for yang files in dependent projects.
+ *
+ * @return files found as List of InputStream
+ */
+ private List<InputStream> getFilesFromDependenciesAsStream() {
+ final List<InputStream> yangsFromDependencies = new ArrayList<InputStream>();
+ try {
+ List<File> filesOnCp = Util.getClassPath(project);
+
+ List<String> filter = Lists.newArrayList(".yang");
+ for (File file : filesOnCp) {
+ ZipFile zip = new ZipFile(file);
+ Enumeration<? extends ZipEntry> 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);
+ }
+ }
+ }
+
}
+++ /dev/null
-/*
- * 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<File> 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<File> resources,
- File outputDir) {
- called++;
- baseDir = outputDir;
- }
- }
-
-}
*/
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
.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);
}
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;
return Lists.newArrayList();
}
}
+
+ public static class ProviderMock implements ResourceGenerator {
+
+ private static int called = 0;
+ private static File baseDir;
+
+ @Override
+ public void generateResourceFiles(Collection<File> 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<File> resources,
+ File outputDir) {
+ called++;
+ baseDir = outputDir;
+ }
+ }
}