Merge "Added skeleton for yang maven plugin."
authorGiovanni Meo <gmeo@cisco.com>
Fri, 5 Apr 2013 13:19:55 +0000 (13:19 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Fri, 5 Apr 2013 13:19:55 +0000 (13:19 +0000)
31 files changed:
opendaylight/sal/yang-prototype/code-generator/pom.xml
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/.gitignore [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/CombineTest.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToResourcesPluginTest.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToSourcesPluginTest.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile1.yang [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile2.yang [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_combined/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_resources/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators_resources/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoYangFiles/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator_resources/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/YangRootNotExist/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/ConfigArg.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToResourcesMojo.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateResourcesTest.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateSourcesTest.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/UtilTest.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/resources/mock.yang [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources/pom.xml [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/CodeGenerator.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/ResourceGenerator.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/CodeGeneratorTestImpl.java [new file with mode: 0644]
opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/ResourceProviderTestImpl.java [new file with mode: 0644]

index add22412cbc9241b651701bcdbe77d134fd28292..17aa745ec0d2c2f7457066bd3b5cbcfd242dcdb9 100644 (file)
@@ -23,6 +23,9 @@
         <module>binding-generator-util</module>\r
         <module>binding-generator-impl</module>\r
         <module>binding-java-api-generator</module>\r
+        <module>yang-to-sources</module>\r
+        <module>yang-to-sources-plugin</module>\r
+        <module>yang-to-sources-plugin-it</module>\r
     </modules>\r
     <dependencies>\r
 \r
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/.gitignore b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/.gitignore
new file mode 100644 (file)
index 0000000..6bd6746
--- /dev/null
@@ -0,0 +1 @@
+log.txt
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/pom.xml
new file mode 100644 (file)
index 0000000..63a743d
--- /dev/null
@@ -0,0 +1,20 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>yang-to-sources-plugin-it</artifactId>
+
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.maven.shared</groupId>
+            <artifactId>maven-verifier</artifactId>
+            <version>1.4</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/CombineTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/CombineTest.java
new file mode 100644 (file)
index 0000000..7171ee9
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * 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/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToResourcesPluginTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToResourcesPluginTest.java
new file mode 100644 (file)
index 0000000..f7aa4f7
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToSourcesPluginTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToSourcesPluginTest.java
new file mode 100644 (file)
index 0000000..1d8f570
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * 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 static org.junit.Assert.*;
+import static org.junit.matchers.JUnitMatchers.*;
+
+import java.io.File;
+
+import org.apache.maven.it.VerificationException;
+import org.apache.maven.it.Verifier;
+import org.junit.Test;
+
+public class YangToSourcesPluginTest {
+
+    @Test
+    public void testYangRootNotExist() {
+        try {
+            setUp("YangRootNotExist/", false);
+        } catch (VerificationException e) {
+            assertVerificationException(e,
+                    "[ERROR] yang-to-sources: Unable to parse yang files from unknown");
+            assertVerificationException(
+                    e,
+                    "Caused by: org.apache.maven.plugin.MojoExecutionException: yang-to-sources: Unable to parse yang files from unknown");
+            return;
+        }
+
+        fail("Verification exception should have been thrown");
+    }
+
+    @Test
+    public void testCorrect() throws VerificationException {
+        Verifier v = setUp("Correct/", false);
+        verifyCorrectLog(v);
+    }
+
+    static void verifyCorrectLog(Verifier v) throws VerificationException {
+        v.verifyErrorFreeLog();
+        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");
+    }
+
+    @Test
+    public void testNoGenerators() throws VerificationException {
+        Verifier v = setUp("NoGenerators/", false);
+        v.verifyErrorFreeLog();
+        v.verifyTextInLog("[WARNING] yang-to-sources: No code generators provided");
+    }
+
+    @Test
+    public void testUnknownGenerator() throws VerificationException {
+        Verifier v = setUp("UnknownGenerator/", true);
+        v.verifyTextInLog("[ERROR] yang-to-sources: Unable to generate sources with unknown generator");
+        v.verifyTextInLog("java.lang.ClassNotFoundException: unknown");
+        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("[ERROR] yang-to-sources: One or more code generators failed, including failed list(generatorClass=exception) {unknown=java.lang.ClassNotFoundException}");
+    }
+
+    @Test
+    public void testNoYangFiles() throws VerificationException {
+        Verifier v = setUp("NoYangFiles/", false);
+        v.verifyTextInLog("[WARNING] yang-to-sources: No yang file found in ");
+        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");
+    }
+
+    static void assertVerificationException(VerificationException e,
+            String string) {
+        assertThat(e.getMessage(), containsString(string));
+    }
+
+    static Verifier setUp(String project, boolean ignoreF)
+            throws VerificationException {
+        Verifier verifier = new Verifier(new File("src/test/resources/"
+                + project).getAbsolutePath());
+        if (ignoreF)
+            verifier.addCliOption("-fn");
+        verifier.executeGoal("generate-resources");
+        return verifier;
+    }
+
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/pom.xml
new file mode 100644 (file)
index 0000000..d94afbd
--- /dev/null
@@ -0,0 +1,49 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>test</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-to-sources-plugin</artifactId>
+                <version>1.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>generate-sources</goal>
+                        </goals>
+                        <configuration>
+                            <yangFilesRootDir>${basedir}</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-to-sources</artifactId>
+                        <version>1.0</version>
+                        <type>test-jar</type>
+                    </dependency>
+                </dependencies>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile1.yang b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile1.yang
new file mode 100644 (file)
index 0000000..ba0c15e
--- /dev/null
@@ -0,0 +1,57 @@
+module types1 {
+       yang-version 1;
+    namespace "urn:simple.container.demo";
+    prefix "t1";
+    
+    import types2 {
+         prefix "data";
+         revision-date 2013-02-27;
+     }
+    
+    organization "Cisco";
+    contact "WILL-BE-DEFINED-LATER";
+    
+    revision "2013-02-27" {
+        reference " WILL BE DEFINED LATER";
+    }
+    
+    container interfaces {
+         list ifEntry {
+             key "ifIndex";
+
+             leaf ifIndex {
+                 type uint32;
+                 units minutes;
+             }
+             
+             leaf ifMtu {
+                 type int32;
+             }
+         }
+     }
+     
+       leaf testleaf {
+               type data:my-base-int32-type {
+                       range "min..max";
+               }
+       }
+       
+       leaf test-string-leaf {
+               type data:my-string-type-ext;
+       }
+       
+       leaf test-int-leaf {
+               type data:my-int-type-ext;
+       }
+       
+       leaf test-decimal-leaf {
+               type data:my-decimal-type {
+                       fraction-digits 4;
+               }
+       }
+       
+       leaf test-decimal-leaf2 {
+               type data:my-decimal-type-ext;
+       }
+
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile2.yang b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile2.yang
new file mode 100644 (file)
index 0000000..3e9da52
--- /dev/null
@@ -0,0 +1,100 @@
+module types2 {
+       yang-version 1;
+    namespace "urn:simple.types.data.demo";
+    prefix "t2";
+    
+    import types1 {
+         prefix "if";
+         revision-date 2013-02-27;
+     }
+
+    organization "Cisco";
+    contact "WILL-BE-DEFINED-LATER";
+    description "This is types-data test description";
+
+    revision "2013-02-27" {
+        reference " WILL BE DEFINED LATER";
+    }
+    
+    typedef my-base-int32-type {
+               type int32 {
+               range "2..20";
+        }
+       }
+
+     typedef my-type1 {
+       type my-base-int32-type {
+               range "11..max";
+       }
+       }
+       
+       typedef my-string-type {
+               type string {
+                       pattern "[a-k]*";
+               }
+       }
+       
+       typedef my-string-type2 {
+               type my-string-type {
+                       pattern "[b-u]*";
+               }
+       }
+       
+       typedef my-string-type-ext {
+               type my-string-type2 {
+                       pattern "[e-z]*";
+               }
+       }
+       
+       typedef my-int-type {
+               type int32 {
+                       range "10..20";
+               }
+       }
+       
+       typedef my-int-type2 {
+               type my-int-type {
+                       range "12..18";
+               }
+       }
+       
+       typedef my-int-type-ext {
+               type my-int-type2 {
+                       range "14..16";
+               }
+       }
+       
+       typedef my-decimal-type {
+               type decimal64 {
+                       fraction-digits 6;
+               }
+       }
+       
+       typedef my-decimal-type-ext {
+               type decimal64 {
+                       fraction-digits 5;
+               }
+       }
+
+    augment "/if:interfaces/if:ifEntry" {
+       when "if:ifType='ds0'";
+        leaf ds0ChannelNumber {
+               type string;
+        }
+       }
+
+    leaf if-name {
+       type leafref {
+               path "/interface/name";
+        }
+    }
+     
+    leaf name {
+       type string;
+    }
+     
+       leaf nested-type-leaf {
+               type my-type1;
+       }
+
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_combined/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_combined/pom.xml
new file mode 100644 (file)
index 0000000..9080f35
--- /dev/null
@@ -0,0 +1,60 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>test</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-to-sources-plugin</artifactId>
+                <version>1.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>generate-sources</goal>
+                            <goal>generate-resources</goal>
+                        </goals>
+                        <configuration>
+                            <yangFilesRootDir>${basedir}/../Correct</yangFilesRootDir>
+                            <codeGenerators>
+                                <generator>
+                                    <codeGeneratorClass>
+                                        org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl
+                                    </codeGeneratorClass>
+                                    <outputBaseDir>
+                                        outDir/src
+                                    </outputBaseDir>
+                                </generator>
+                            </codeGenerators>
+                            <resourceProviders>
+                                <provider>
+                                    <resourceProviderClass>
+                                        org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl
+                                    </resourceProviderClass>
+                                    <outputBaseDir>
+                                        outDir/resources
+                                    </outputBaseDir>
+                                </provider>
+                            </resourceProviders>
+                        </configuration>
+                    </execution>
+                </executions>
+
+                <dependencies>
+                    <dependency>
+                        <groupId>org.opendaylight.controller</groupId>
+                        <artifactId>yang-to-sources</artifactId>
+                        <version>1.0</version>
+                        <type>test-jar</type>
+                    </dependency>
+                </dependencies>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_resources/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_resources/pom.xml
new file mode 100644 (file)
index 0000000..4456fcf
--- /dev/null
@@ -0,0 +1,49 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>test</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-to-sources-plugin</artifactId>
+                <version>1.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>generate-resources</goal>
+                        </goals>
+                        <configuration>
+                            <yangFilesRootDir>${basedir}/../Correct</yangFilesRootDir>
+                            <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-to-sources</artifactId>
+                        <version>1.0</version>
+                        <type>test-jar</type>
+                    </dependency>
+                </dependencies>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators/pom.xml
new file mode 100644 (file)
index 0000000..f194e9d
--- /dev/null
@@ -0,0 +1,32 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>test</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-to-sources-plugin</artifactId>
+                <version>1.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>generate-sources</goal>
+                        </goals>
+                        <configuration>
+                            <yangFilesRootDir>${basedir}/../Correct</yangFilesRootDir>
+                            <codeGenerators>
+                            </codeGenerators>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators_resources/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators_resources/pom.xml
new file mode 100644 (file)
index 0000000..0861ca2
--- /dev/null
@@ -0,0 +1,32 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>test</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-to-sources-plugin</artifactId>
+                <version>1.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>generate-resources</goal>
+                        </goals>
+                        <configuration>
+                            <yangFilesRootDir>${basedir}/../Correct</yangFilesRootDir>
+                            <resourceProviders>
+                            </resourceProviders>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoYangFiles/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoYangFiles/pom.xml
new file mode 100644 (file)
index 0000000..4cce379
--- /dev/null
@@ -0,0 +1,49 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>test</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-to-sources-plugin</artifactId>
+                <version>1.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>generate-sources</goal>
+                        </goals>
+                        <configuration>
+                            <yangFilesRootDir>${basedir}</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-to-sources</artifactId>
+                        <version>1.0</version>
+                        <type>test-jar</type>
+                    </dependency>
+                </dependencies>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator/pom.xml
new file mode 100644 (file)
index 0000000..0a1afad
--- /dev/null
@@ -0,0 +1,57 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>test</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-to-sources-plugin</artifactId>
+                <version>1.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>generate-sources</goal>
+                        </goals>
+                        <configuration>
+                            <yangFilesRootDir>${basedir}/../Correct</yangFilesRootDir>
+                            <codeGenerators>
+                                <generator>
+                                    <codeGeneratorClass>
+                                        org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl
+                                    </codeGeneratorClass>
+                                    <outputBaseDir>
+                                        /outDir/
+                                    </outputBaseDir>
+                                </generator>
+                                <generator>
+                                    <codeGeneratorClass>
+                                        unknown
+                                    </codeGeneratorClass>
+                                    <outputBaseDir>
+                                        /outDir/
+                                    </outputBaseDir>
+                                </generator>
+                            </codeGenerators>
+                        </configuration>
+                    </execution>
+                </executions>
+
+                <dependencies>
+                    <dependency>
+                        <groupId>org.opendaylight.controller</groupId>
+                        <artifactId>yang-to-sources</artifactId>
+                        <version>1.0</version>
+                        <type>test-jar</type>
+                    </dependency>
+                </dependencies>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator_resources/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator_resources/pom.xml
new file mode 100644 (file)
index 0000000..df03f95
--- /dev/null
@@ -0,0 +1,57 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>test</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-to-sources-plugin</artifactId>
+                <version>1.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>generate-resources</goal>
+                        </goals>
+                        <configuration>
+                            <yangFilesRootDir>${basedir}/../Correct</yangFilesRootDir>
+                            <resourceProviders>
+                                <provider>
+                                    <resourceProviderClass>
+                                        org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl
+                                    </resourceProviderClass>
+                                    <outputBaseDir>
+                                        outDir/
+                                    </outputBaseDir>
+                                </provider>
+                                <provider>
+                                    <resourceProviderClass>
+                                        unknown
+                                    </resourceProviderClass>
+                                    <outputBaseDir>
+                                        outDir/
+                                    </outputBaseDir>
+                                </provider>
+                            </resourceProviders>
+                        </configuration>
+                    </execution>
+                </executions>
+
+                <dependencies>
+                    <dependency>
+                        <groupId>org.opendaylight.controller</groupId>
+                        <artifactId>yang-to-sources</artifactId>
+                        <version>1.0</version>
+                        <type>test-jar</type>
+                    </dependency>
+                </dependencies>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/YangRootNotExist/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/YangRootNotExist/pom.xml
new file mode 100644 (file)
index 0000000..3ab27e5
--- /dev/null
@@ -0,0 +1,40 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>test</artifactId>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.opendaylight.controller</groupId>
+                <artifactId>yang-to-sources-plugin</artifactId>
+                <version>1.0</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>generate-sources</goal>
+                        </goals>
+                        <configuration>
+                            <yangFilesRootDir>unknown</yangFilesRootDir>
+                            <codeGenerators>
+                                <generator>
+                                    <codeGeneratorClass>
+                                        org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl
+                                    </codeGeneratorClass>
+                                    <outputBaseDir>
+                                        /outDir/
+                                    </outputBaseDir>
+                                </generator>
+                            </codeGenerators>
+                        </configuration>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/pom.xml
new file mode 100644 (file)
index 0000000..a2d3a22
--- /dev/null
@@ -0,0 +1,79 @@
+<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>1.0</version>
+    </parent>
+
+    <artifactId>yang-to-sources-plugin</artifactId>
+    <packaging>maven-plugin</packaging>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.maven</groupId>
+            <artifactId>maven-plugin-api</artifactId>
+            <version>3.0.5</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.maven.plugin-tools</groupId>
+            <artifactId>maven-plugin-annotations</artifactId>
+            <version>3.2</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>yang-model-parser-impl</artifactId>
+            <version>1.0</version>
+        </dependency>
+        <dependency>
+            <groupId>org.opendaylight.controller</groupId>
+            <artifactId>yang-to-sources</artifactId>
+            <version>1.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.4</version>
+        </dependency>
+
+        <dependency>
+            <groupId>${project.groupId}</groupId>
+            <artifactId>yang-to-sources</artifactId>
+            <version>1.0</version>
+            <type>test-jar</type>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-all</artifactId>
+            <version>1.8.4</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-plugin-plugin</artifactId>
+                <version>3.2</version>
+                <configuration>
+                    <skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>mojo-descriptor</id>
+                        <goals>
+                            <goal>descriptor</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/ConfigArg.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/ConfigArg.java
new file mode 100644 (file)
index 0000000..f6602e6
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * 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 com.google.common.base.Preconditions;
+
+/**
+ * Complex configuration arguments
+ */
+public abstract class ConfigArg {
+
+    protected File outputBaseDir;
+
+    public ConfigArg(File outputBaseDir) {
+        this.outputBaseDir = outputBaseDir;
+    }
+
+    public ConfigArg() {
+    }
+
+    public File getOutputBaseDir() {
+        return outputBaseDir;
+    }
+
+    public abstract void check();
+
+    public static final class ResourceProviderArg extends ConfigArg {
+        private String resourceProviderClass;
+
+        public ResourceProviderArg() {
+        }
+
+        public ResourceProviderArg(String resourceProviderClass,
+                File outputBaseDir) {
+            super(outputBaseDir);
+            this.resourceProviderClass = resourceProviderClass;
+        }
+
+        @Override
+        public void check() {
+            Preconditions
+                    .checkNotNull(resourceProviderClass,
+                            "resourceProviderClass for ResourceProvider cannot be null");
+            Preconditions.checkNotNull(outputBaseDir,
+                    "outputBaseDir for ResourceProvider cannot be null, "
+                            + resourceProviderClass);
+        }
+
+        public String getResourceProviderClass() {
+            return resourceProviderClass;
+        }
+    }
+
+    /**
+     * Transfer object for code generator class and output directory.
+     */
+    public static final class CodeGeneratorArg extends ConfigArg {
+        private String codeGeneratorClass;
+
+        public CodeGeneratorArg() {
+        }
+
+        public CodeGeneratorArg(String codeGeneratorClass, File outputBaseDir) {
+            super(outputBaseDir);
+            this.codeGeneratorClass = codeGeneratorClass;
+        }
+
+        @Override
+        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() {
+            return codeGeneratorClass;
+        }
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java
new file mode 100644 (file)
index 0000000..98a5d85
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ * 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.commons.io.FileUtils;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
+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
+    // are utilized twice. First: code is generated during generate-sources
+    // phase Second: yang files are copied as resources during
+    // generate-resources phase. This cache ensures that yang files are listed
+    // only once.
+    private static Map<String, Collection<File>> cache = Maps
+            .newHashMapWithExpectedSize(10);
+
+    /**
+     * List files recursively and return as array of String paths. Use cache of
+     * size 1.
+     */
+    static Collection<File> listFiles(String rootDir) {
+
+        if (cache.get(rootDir) != null)
+            return cache.get(rootDir);
+
+        Collection<File> yangFiles = FileUtils.listFiles(new File(rootDir),
+                new String[] { YANG_SUFFIX }, true);
+
+        toCache(rootDir, yangFiles);
+        return yangFiles;
+    }
+
+    static String[] listFilesAsArrayOfPaths(String rootDir) {
+        String[] filesArray = new String[] {};
+        Collection<File> yangFiles = listFiles(rootDir);
+
+        // If collection is empty, return empty array [] rather then [null]
+        // array, that is created by default
+        return yangFiles.isEmpty() ? filesArray : Collections2.transform(
+                yangFiles, new Function<File, String>() {
+
+                    @Override
+                    public String apply(File input) {
+                        return input.getPath();
+                    }
+                }).toArray(filesArray);
+    }
+
+    private static void toCache(final String rootDir,
+            final Collection<File> yangFiles) {
+        cache.put(rootDir, yangFiles);
+    }
+
+    /**
+     * Instantiate object from fully qualified class name
+     */
+    static <T> T getInstance(String codeGeneratorClass, Class<T> baseType)
+            throws ClassNotFoundException, InstantiationException,
+            IllegalAccessException {
+        return baseType.cast(resolveClass(codeGeneratorClass, baseType)
+                .newInstance());
+    }
+
+    private static Class<?> resolveClass(String codeGeneratorClass,
+            Class<?> baseType) throws ClassNotFoundException {
+        Class<?> clazz = Class.forName(codeGeneratorClass);
+
+        if (!isImplemented(baseType, clazz))
+            throw new IllegalArgumentException("Code generator " + clazz
+                    + " has to implement " + baseType);
+        return clazz;
+    }
+
+    private static boolean isImplemented(Class<?> expectedIface,
+            Class<?> byClazz) {
+        for (Class<?> iface : byClazz.getInterfaces()) {
+            if (iface.equals(expectedIface))
+                return true;
+        }
+        return false;
+    }
+
+    static String message(String message, String logPrefix, Object... args) {
+        String innerMessage = String.format(message, args);
+        return String.format("%s %s", logPrefix, innerMessage);
+    }
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToResourcesMojo.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToResourcesMojo.java
new file mode 100644 (file)
index 0000000..7bb49dc
--- /dev/null
@@ -0,0 +1,111 @@
+/*
+ * 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;
+
+@Mojo(name = "generate-resources", defaultPhase = LifecyclePhase.GENERATE_RESOURCES)
+public final class YangToResourcesMojo extends AbstractMojo {
+
+    private static final String LOG_PREFIX = "yang-to-resources:";
+
+    @Parameter(required = true)
+    private ResourceProviderArg[] resourceProviders;
+
+    @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()));
+    }
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java
new file mode 100644 (file)
index 0000000..2928298
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ * 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.Arrays;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+
+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.yang.model.api.Module;
+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.YangModelParserImpl;
+import org.opendaylight.controller.yang2sources.plugin.ConfigArg.CodeGeneratorArg;
+import org.opendaylight.controller.yang2sources.spi.CodeGenerator;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.Maps;
+
+@Mojo(name = "generate-sources", defaultPhase = LifecyclePhase.GENERATE_SOURCES)
+public final class YangToSourcesMojo extends AbstractMojo {
+
+    private static final String LOG_PREFIX = "yang-to-sources:";
+
+    @Parameter(required = true)
+    private CodeGeneratorArg[] codeGenerators;
+
+    @Parameter(required = true)
+    private String yangFilesRootDir;
+
+    private final YangModelParser parser;
+
+    @VisibleForTesting
+    YangToSourcesMojo(CodeGeneratorArg[] codeGeneratorArgs,
+            YangModelParser parser, String yangFilesRootDir) {
+        super();
+        this.codeGenerators = codeGeneratorArgs;
+        this.yangFilesRootDir = yangFilesRootDir;
+        this.parser = parser;
+    }
+
+    public YangToSourcesMojo() {
+        super();
+        parser = new YangModelParserImpl();
+    }
+
+    @Override
+    public void execute() throws MojoExecutionException, MojoFailureException {
+        SchemaContext context = processYang();
+        generateSources(context);
+    }
+
+    /**
+     * Generate {@link SchemaContext} with {@link YangModelParserImpl}
+     */
+    private SchemaContext processYang() throws MojoExecutionException {
+        try {
+            String[] yangFiles = Util.listFilesAsArrayOfPaths(yangFilesRootDir);
+
+            if (yangFiles.length == 0)
+                getLog().warn(
+                        Util.message("No %s file found in %s", LOG_PREFIX,
+                                Util.YANG_SUFFIX, yangFilesRootDir));
+            // TODO only warning or throw exception ?
+
+            Set<Module> parsedYang = parser.parseYangModels(yangFiles);
+            SchemaContext resolveSchemaContext = parser
+                    .resolveSchemaContext(parsedYang);
+            getLog().info(
+                    Util.message("%s files parsed from %s", LOG_PREFIX,
+                            Util.YANG_SUFFIX, Arrays.toString(yangFiles)));
+            return resolveSchemaContext;
+
+            // MojoExecutionException is thrown since execution cannot continue
+        } catch (Exception e) {
+            String message = Util.message("Unable to parse %s files from %s",
+                    LOG_PREFIX, Util.YANG_SUFFIX, yangFilesRootDir);
+            getLog().error(message, e);
+            throw new MojoExecutionException(message, e);
+        }
+    }
+
+    /**
+     * Call generate on every generator from plugin configuration
+     */
+    private void generateSources(SchemaContext context)
+            throws MojoFailureException {
+        if (codeGenerators.length == 0) {
+            getLog().warn(
+                    Util.message("No code generators provided", LOG_PREFIX));
+            return;
+        }
+
+        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(
+                        Util.message(
+                                "Unable to generate sources with %s generator",
+                                LOG_PREFIX,
+                                codeGenerator.getCodeGeneratorClass()), e);
+                thrown.put(codeGenerator.getCodeGeneratorClass(), e.getClass()
+                        .getCanonicalName());
+            }
+        }
+
+        if (!thrown.isEmpty()) {
+            String message = Util
+                    .message(
+                            "One or more code generators failed, including failed list(generatorClass=exception) %s",
+                            LOG_PREFIX, thrown.toString());
+            getLog().error(message);
+            throw new MojoFailureException(message);
+        }
+    }
+
+    /**
+     * Instantiate generator from class and call required method
+     */
+    private void generateSourcesWithOneGenerator(SchemaContext context,
+            CodeGeneratorArg codeGenerator) throws ClassNotFoundException,
+            InstantiationException, IllegalAccessException {
+
+        codeGenerator.check();
+
+        CodeGenerator g = Util.getInstance(
+                codeGenerator.getCodeGeneratorClass(), CodeGenerator.class);
+        getLog().info(
+                Util.message("Code generator instantiated from %s", LOG_PREFIX,
+                        codeGenerator.getCodeGeneratorClass()));
+
+        Collection<File> generated = g.generateSources(context,
+                codeGenerator.getOutputBaseDir());
+        getLog().info(
+                Util.message("Sources generated by %s: %s", LOG_PREFIX,
+                        codeGenerator.getCodeGeneratorClass(), generated));
+    }
+
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateResourcesTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateResourcesTest.java
new file mode 100644 (file)
index 0000000..a23e3b5
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * 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;
+        }
+    }
+
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateSourcesTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateSourcesTest.java
new file mode 100644 (file)
index 0000000..be19db1
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * 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 static org.mockito.Matchers.*;
+import static org.mockito.Mockito.*;
+
+import java.io.File;
+import java.util.Collection;
+
+import org.junit.Before;
+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.spi.CodeGenerator;
+
+import com.google.common.collect.Lists;
+
+public class GenerateSourcesTest {
+
+    @Mock
+    private YangModelParser parser;
+    private String yang;
+    private YangToSourcesMojo mojo;
+    private File outDir;
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        yang = new File(getClass().getResource("/mock.yang").getFile())
+                .getParent();
+        outDir = new File("outputDir");
+        mojo = new YangToSourcesMojo(
+                new CodeGeneratorArg[] { new CodeGeneratorArg(
+                        GeneratorMock.class.getName(), outDir) }, parser, yang);
+    }
+
+    @Test
+    public void test() throws Exception {
+        mojo.execute();
+        verify(parser, times(1)).parseYangModels((String[]) anyVararg());
+        assertThat(GeneratorMock.called, is(1));
+        assertThat(GeneratorMock.outputDir, is(outDir));
+    }
+
+    public static class GeneratorMock implements CodeGenerator {
+
+        private static int called = 0;
+        private static File outputDir;
+
+        @Override
+        public Collection<File> generateSources(SchemaContext context,
+                File baseDir) {
+            called++;
+            outputDir = baseDir;
+            return Lists.newArrayList();
+        }
+
+    }
+
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/UtilTest.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/UtilTest.java
new file mode 100644 (file)
index 0000000..0a17d9f
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * 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.junit.Assert.*;
+
+import java.io.File;
+import java.util.Collection;
+
+import org.junit.Test;
+
+public class UtilTest {
+
+    @Test
+    public void testCache() {
+        String yang = new File(getClass().getResource("/mock.yang").getFile())
+                .getParent();
+        Collection<File> files = Util.listFiles(yang);
+        Collection<File> files2 = Util.listFiles(yang);
+        assertTrue(files == files2);
+    }
+
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/resources/mock.yang b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/resources/mock.yang
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/pom.xml b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/pom.xml
new file mode 100644 (file)
index 0000000..c8c74c9
--- /dev/null
@@ -0,0 +1,36 @@
+<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>1.0</version>
+    </parent>
+    <artifactId>yang-to-sources</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.opendaylight.controller</groupId>
+            <artifactId>yang-model-api</artifactId>
+            <version>1.0</version>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-jar-plugin</artifactId>
+                <version>2.4</version>
+                <executions>
+                    <execution>
+                        <phase>package</phase>
+                        <goals>
+                            <goal>test-jar</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
+</project>
\ No newline at end of file
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/CodeGenerator.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/CodeGenerator.java
new file mode 100644 (file)
index 0000000..2b39320
--- /dev/null
@@ -0,0 +1,18 @@
+/*
+ * 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.spi;
+
+import java.io.File;
+import java.util.Collection;
+
+import org.opendaylight.controller.yang.model.api.SchemaContext;
+
+public interface CodeGenerator {
+
+    Collection<File> generateSources(SchemaContext context, File outputBaseDir);
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/ResourceGenerator.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/ResourceGenerator.java
new file mode 100644 (file)
index 0000000..9095155
--- /dev/null
@@ -0,0 +1,16 @@
+/*
+ * 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.spi;
+
+import java.io.File;
+import java.util.Collection;
+
+public interface ResourceGenerator {
+
+    void generateResourceFiles(Collection<File> resources, File outputBaseDir);
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/CodeGeneratorTestImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/CodeGeneratorTestImpl.java
new file mode 100644 (file)
index 0000000..6683978
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * 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.spi;
+
+import java.io.File;
+import java.util.Collection;
+
+import org.opendaylight.controller.yang.model.api.SchemaContext;
+
+public class CodeGeneratorTestImpl implements CodeGenerator {
+
+    @Override
+    public Collection<File> generateSources(SchemaContext context,
+            File outputBaseDir) {
+        // no-op
+        return null;
+    }
+
+}
diff --git a/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/ResourceProviderTestImpl.java b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/ResourceProviderTestImpl.java
new file mode 100644 (file)
index 0000000..dc52184
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * 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.spi;
+
+import java.io.File;
+import java.util.Collection;
+
+public class ResourceProviderTestImpl implements ResourceGenerator {
+
+    @Override
+    public void generateResourceFiles(Collection<File> resources,
+            File outputBaseDir) {
+        // no-op
+    }
+
+}