From ece17d743438390fb4d0d41ea9dec6715ef604fe Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Fri, 5 Apr 2013 14:03:55 +0200 Subject: [PATCH] Added skeleton for yang maven plugin. Added implementation of yang-to-sources maven plugin which is responsible for parsing YANG files and invoking configured code providers to generate sources from YANG during maven generate-sources and generate-resources phase. Added JUNIT tests testing the integration with maven and following scenarios: No generators for generate-sources and generate-resources Unknown generators for generate-sources and generate-resources Non-existant root folder with yang sources No yang sources under yang root folder Correct configuration for both generate-sources and generate-resources Change-Id: Ib4d2a2c004ba19d3fda79d6a3d6da41050059ce0 Signed-off-by: Maros Marsalek --- .../sal/yang-prototype/code-generator/pom.xml | 3 + .../yang-to-sources-plugin-it/.gitignore | 1 + .../yang-to-sources-plugin-it/pom.xml | 20 +++ .../yang2sources/plugin/it/CombineTest.java | 23 +++ .../plugin/it/YangToResourcesPluginTest.java | 47 ++++++ .../plugin/it/YangToSourcesPluginTest.java | 91 ++++++++++ .../src/test/resources/Correct/pom.xml | 49 ++++++ .../Correct/resources/model/testfile1.yang | 57 +++++++ .../Correct/resources/model/testfile2.yang | 100 +++++++++++ .../test/resources/Correct_combined/pom.xml | 60 +++++++ .../test/resources/Correct_resources/pom.xml | 49 ++++++ .../src/test/resources/NoGenerators/pom.xml | 32 ++++ .../resources/NoGenerators_resources/pom.xml | 32 ++++ .../src/test/resources/NoYangFiles/pom.xml | 49 ++++++ .../test/resources/UnknownGenerator/pom.xml | 57 +++++++ .../UnknownGenerator_resources/pom.xml | 57 +++++++ .../test/resources/YangRootNotExist/pom.xml | 40 +++++ .../yang-to-sources-plugin/pom.xml | 79 +++++++++ .../yang2sources/plugin/ConfigArg.java | 88 ++++++++++ .../controller/yang2sources/plugin/Util.java | 102 ++++++++++++ .../plugin/YangToResourcesMojo.java | 111 +++++++++++++ .../plugin/YangToSourcesMojo.java | 157 ++++++++++++++++++ .../plugin/GenerateResourcesTest.java | 75 +++++++++ .../plugin/GenerateSourcesTest.java | 72 ++++++++ .../yang2sources/plugin/UtilTest.java | 28 ++++ .../src/test/resources/mock.yang | 0 .../code-generator/yang-to-sources/pom.xml | 36 ++++ .../yang2sources/spi/CodeGenerator.java | 18 ++ .../yang2sources/spi/ResourceGenerator.java | 16 ++ .../spi/CodeGeneratorTestImpl.java | 24 +++ .../spi/ResourceProviderTestImpl.java | 21 +++ 31 files changed, 1594 insertions(+) create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/.gitignore create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/CombineTest.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToResourcesPluginTest.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToSourcesPluginTest.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile1.yang create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile2.yang create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_combined/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_resources/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators_resources/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoYangFiles/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator_resources/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/YangRootNotExist/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/ConfigArg.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToResourcesMojo.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateResourcesTest.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateSourcesTest.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/UtilTest.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/resources/mock.yang create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources/pom.xml create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/CodeGenerator.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/ResourceGenerator.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/CodeGeneratorTestImpl.java create mode 100644 opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/ResourceProviderTestImpl.java diff --git a/opendaylight/sal/yang-prototype/code-generator/pom.xml b/opendaylight/sal/yang-prototype/code-generator/pom.xml index add22412cb..17aa745ec0 100644 --- a/opendaylight/sal/yang-prototype/code-generator/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/pom.xml @@ -23,6 +23,9 @@ binding-generator-util binding-generator-impl binding-java-api-generator + yang-to-sources + yang-to-sources-plugin + yang-to-sources-plugin-it 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 index 0000000000..6bd6746426 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/.gitignore @@ -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 index 0000000000..63a743d00d --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/pom.xml @@ -0,0 +1,20 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + yang-to-sources-plugin-it + + + + + org.apache.maven.shared + maven-verifier + 1.4 + test + + + \ 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 index 0000000000..7171ee92b0 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/CombineTest.java @@ -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 index 0000000000..f7aa4f7c4e --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToResourcesPluginTest.java @@ -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 index 0000000000..1d8f570853 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/java/org/opendaylight/controller/yang2sources/plugin/it/YangToSourcesPluginTest.java @@ -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 index 0000000000..d94afbdd95 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + test + + + + + org.opendaylight.controller + yang-to-sources-plugin + 1.0 + + + + generate-sources + + + ${basedir} + + + + org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl + + + outDir/ + + + + + + + + + + org.opendaylight.controller + yang-to-sources + 1.0 + test-jar + + + + + + \ 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 index 0000000000..ba0c15eb4e --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile1.yang @@ -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 index 0000000000..3e9da5256b --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct/resources/model/testfile2.yang @@ -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 index 0000000000..9080f351d5 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_combined/pom.xml @@ -0,0 +1,60 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + test + + + + + org.opendaylight.controller + yang-to-sources-plugin + 1.0 + + + + generate-sources + generate-resources + + + ${basedir}/../Correct + + + + org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl + + + outDir/src + + + + + + + org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl + + + outDir/resources + + + + + + + + + + org.opendaylight.controller + yang-to-sources + 1.0 + test-jar + + + + + + \ 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 index 0000000000..4456fcf3da --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/Correct_resources/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + test + + + + + org.opendaylight.controller + yang-to-sources-plugin + 1.0 + + + + generate-resources + + + ${basedir}/../Correct + + + + org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl + + + outDir/ + + + + + + + + + + org.opendaylight.controller + yang-to-sources + 1.0 + test-jar + + + + + + \ 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 index 0000000000..f194e9dde6 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + test + + + + + org.opendaylight.controller + yang-to-sources-plugin + 1.0 + + + + generate-sources + + + ${basedir}/../Correct + + + + + + + + + \ 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 index 0000000000..0861ca21ab --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoGenerators_resources/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + test + + + + + org.opendaylight.controller + yang-to-sources-plugin + 1.0 + + + + generate-resources + + + ${basedir}/../Correct + + + + + + + + + \ 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 index 0000000000..4cce379210 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/NoYangFiles/pom.xml @@ -0,0 +1,49 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + test + + + + + org.opendaylight.controller + yang-to-sources-plugin + 1.0 + + + + generate-sources + + + ${basedir} + + + + org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl + + + /outDir/ + + + + + + + + + + org.opendaylight.controller + yang-to-sources + 1.0 + test-jar + + + + + + \ 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 index 0000000000..0a1afade58 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + test + + + + + org.opendaylight.controller + yang-to-sources-plugin + 1.0 + + + + generate-sources + + + ${basedir}/../Correct + + + + org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl + + + /outDir/ + + + + + unknown + + + /outDir/ + + + + + + + + + + org.opendaylight.controller + yang-to-sources + 1.0 + test-jar + + + + + + \ 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 index 0000000000..df03f95c26 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/UnknownGenerator_resources/pom.xml @@ -0,0 +1,57 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + test + + + + + org.opendaylight.controller + yang-to-sources-plugin + 1.0 + + + + generate-resources + + + ${basedir}/../Correct + + + + org.opendaylight.controller.yang2sources.spi.ResourceProviderTestImpl + + + outDir/ + + + + + unknown + + + outDir/ + + + + + + + + + + org.opendaylight.controller + yang-to-sources + 1.0 + test-jar + + + + + + \ 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 index 0000000000..3ab27e5740 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin-it/src/test/resources/YangRootNotExist/pom.xml @@ -0,0 +1,40 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + test + + + + + org.opendaylight.controller + yang-to-sources-plugin + 1.0 + + + + generate-sources + + + unknown + + + + org.opendaylight.controller.yang2sources.spi.CodeGeneratorTestImpl + + + /outDir/ + + + + + + + + + + \ 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 index 0000000000..a2d3a2206e --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/pom.xml @@ -0,0 +1,79 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + + yang-to-sources-plugin + maven-plugin + + + + org.apache.maven + maven-plugin-api + 3.0.5 + + + org.apache.maven.plugin-tools + maven-plugin-annotations + 3.2 + provided + + + + ${project.groupId} + yang-model-parser-impl + 1.0 + + + org.opendaylight.controller + yang-to-sources + 1.0 + + + + commons-io + commons-io + 2.4 + + + + ${project.groupId} + yang-to-sources + 1.0 + test-jar + test + + + org.mockito + mockito-all + 1.8.4 + test + + + + + + + org.apache.maven.plugins + maven-plugin-plugin + 3.2 + + true + + + + mojo-descriptor + + descriptor + + + + + + + + \ 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 index 0000000000..f6602e6aae --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/ConfigArg.java @@ -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 index 0000000000..98a5d8531b --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/Util.java @@ -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> cache = Maps + .newHashMapWithExpectedSize(10); + + /** + * List files recursively and return as array of String paths. Use cache of + * size 1. + */ + static Collection listFiles(String rootDir) { + + if (cache.get(rootDir) != null) + return cache.get(rootDir); + + Collection 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 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() { + + @Override + public String apply(File input) { + return input.getPath(); + } + }).toArray(filesArray); + } + + private static void toCache(final String rootDir, + final Collection yangFiles) { + cache.put(rootDir, yangFiles); + } + + /** + * Instantiate object from fully qualified class name + */ + static T getInstance(String codeGeneratorClass, Class 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 index 0000000000..7bb49dc0df --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToResourcesMojo.java @@ -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 thrown = Maps.newHashMap(); + Collection yangFiles = Util.listFiles(yangFilesRootDir); + + for (ResourceProviderArg resourceProvider : resourceProviders) { + try { + + provideResourcesWithOneProvider(yangFiles, resourceProvider); + + } catch (Exception e) { + // try other generators, exception will be thrown after + getLog().error( + Util.message( + "Unable to provide resources with %s resource provider", + LOG_PREFIX, + resourceProvider.getResourceProviderClass()), e); + thrown.put(resourceProvider.getResourceProviderClass(), e + .getClass().getCanonicalName()); + } + } + + if (!thrown.isEmpty()) { + String message = Util + .message( + "One or more code resource provider failed, including failed list(resourceProviderClass=exception) %s", + LOG_PREFIX, thrown.toString()); + getLog().error(message); + throw new MojoFailureException(message); + } + } + + /** + * Instantiate provider from class and call required method + */ + private void provideResourcesWithOneProvider(Collection yangFiles, + ResourceProviderArg resourceProvider) + throws ClassNotFoundException, InstantiationException, + IllegalAccessException { + + resourceProvider.check(); + + ResourceGenerator g = Util.getInstance( + resourceProvider.getResourceProviderClass(), + ResourceGenerator.class); + getLog().info( + Util.message("Resource provider instantiated from %s", + LOG_PREFIX, resourceProvider.getResourceProviderClass())); + + g.generateResourceFiles(yangFiles, resourceProvider.getOutputBaseDir()); + getLog().info( + Util.message("Resource provider %s call successful", + LOG_PREFIX, resourceProvider.getResourceProviderClass())); + } +} diff --git a/opendaylight/sal/yang-prototype/code-generator/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 index 0000000000..29282988d6 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/main/java/org/opendaylight/controller/yang2sources/plugin/YangToSourcesMojo.java @@ -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 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 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 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 index 0000000000..a23e3b5a90 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateResourcesTest.java @@ -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 resources, + File outputDir) { + called++; + baseDir = outputDir; + } + } + + public static class ProviderMock2 implements ResourceGenerator { + + private static int called = 0; + private static File baseDir; + + @Override + public void generateResourceFiles(Collection resources, + File outputDir) { + called++; + baseDir = outputDir; + } + } + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/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 index 0000000000..be19db1423 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/GenerateSourcesTest.java @@ -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 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 index 0000000000..0a17d9f13d --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources-plugin/src/test/java/org/opendaylight/controller/yang2sources/plugin/UtilTest.java @@ -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 files = Util.listFiles(yang); + Collection 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 index 0000000000..e69de29bb2 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 index 0000000000..c8c74c92b7 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + + binding-generator + org.opendaylight.controller + 1.0 + + yang-to-sources + + + + org.opendaylight.controller + yang-model-api + 1.0 + + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + package + + test-jar + + + + + + + \ 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 index 0000000000..2b39320989 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/CodeGenerator.java @@ -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 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 index 0000000000..9095155b0c --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/main/java/org/opendaylight/controller/yang2sources/spi/ResourceGenerator.java @@ -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 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 index 0000000000..6683978988 --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/CodeGeneratorTestImpl.java @@ -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 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 index 0000000000..dc521841ef --- /dev/null +++ b/opendaylight/sal/yang-prototype/code-generator/yang-to-sources/src/test/java/org/opendaylight/controller/yang2sources/spi/ResourceProviderTestImpl.java @@ -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 resources, + File outputBaseDir) { + // no-op + } + +} -- 2.36.6