From: Milos Fabian Date: Tue, 3 Dec 2013 08:30:38 +0000 (+0100) Subject: Added yang-test-plugin maven plugin. X-Git-Tag: jenkins-controller-bulk-release-prepare-only-2-1~273^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=dbab0ae40879d014e5c909f2e339b443890f5848 Added yang-test-plugin maven plugin. Generated source files are deleted before new ones are genarated, after that implemantaion code from stub.txt is inserted. Change-Id: I24d0748bd9ed10908785ba88341e8a4f4cde6958 Signed-off-by: Milos Fabian --- diff --git a/opendaylight/config/pom.xml b/opendaylight/config/pom.xml index c9ed19dacb..675e5cf80d 100644 --- a/opendaylight/config/pom.xml +++ b/opendaylight/config/pom.xml @@ -39,6 +39,7 @@ netty-event-executor-config netty-timer-config config-persister-directory-adapter + yang-test-plugin diff --git a/opendaylight/config/yang-test-plugin/pom.xml b/opendaylight/config/yang-test-plugin/pom.xml new file mode 100644 index 0000000000..30ac8554fe --- /dev/null +++ b/opendaylight/config/yang-test-plugin/pom.xml @@ -0,0 +1,28 @@ + + + + org.opendaylight.controller + config-plugin-parent + 0.2.3-SNAPSHOT + ../config-plugin-parent + + 4.0.0 + maven-plugin + yang-test-plugin + + Remove generated source files, after new files generation, implementation is inserted. + ${project.artifactId} + + 3.0.4 + + + + + org.apache.maven + maven-plugin-api + 3.0.5 + + + \ No newline at end of file diff --git a/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/DeleteSources.java b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/DeleteSources.java new file mode 100644 index 0000000000..461066215f --- /dev/null +++ b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/DeleteSources.java @@ -0,0 +1,48 @@ +/* + * 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.config.yang.test.plugin; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; + +import java.io.File; + +/** + * Delete all Module/ModuleFactory sources + * + * @goal delete-sources + * + * @phase initialize + */ +public class DeleteSources extends AbstractMojo{ + /** + * @parameter expression="${project.build.sourceDirectory}" + * @readOnly + * @required + */ + private File directory; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + if (directory == null || !directory.exists()) { + super.getLog().error("Directory does not exists."); + } + File sourceDirectory = new File(directory.getPath() + Util.replaceDots(".org.opendaylight.controller.config.yang.test.impl")); + if (sourceDirectory == null || !sourceDirectory.exists()) { + super.getLog().error("Source directory does not exists " + sourceDirectory.getPath()); + } + File[] sourceFiles = sourceDirectory.listFiles(); + for (File sourceFile: sourceFiles) { + if(sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) { + super.getLog().debug("Source file deleted: " + sourceFile.getName()); + sourceFile.delete(); + } + } + } +} diff --git a/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java new file mode 100644 index 0000000000..dbb9ddb363 --- /dev/null +++ b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/ProcessSources.java @@ -0,0 +1,99 @@ +/* + * 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.config.yang.test.plugin; + +import org.apache.maven.plugin.AbstractMojo; +import org.apache.maven.plugin.MojoExecutionException; +import org.apache.maven.plugin.MojoFailureException; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; + +/** + * Add implementation code from stub.txt + * + * @goal process-sources + * + * @phase process-sources + * + */ +public class ProcessSources extends AbstractMojo{ + /** + * @parameter expression="${project.build.sourceDirectory}" + * @readOnly + * @required + */ + private File directory; + + @Override + public void execute() throws MojoExecutionException, MojoFailureException { + if (directory == null || !directory.exists()) { + super.getLog().error("Directory does not exists."); + } + File sourceDirectory = new File(directory.getPath() + Util.replaceDots(".org.opendaylight.controller.config.yang.test.impl")); + if (!sourceDirectory.exists()) { + super.getLog().error("Source directory does not exists " + sourceDirectory.getPath()); + } + String header = ""; + try { + header = Util.loadHeader(); + } catch (IOException e) { + super.getLog().error("Header.txt not found."); + } + File[] sourceFiles = sourceDirectory.listFiles(); + for (File sourceFile: sourceFiles) { + if(sourceFile.getName().endsWith("Module.java") || sourceFile.getName().endsWith("ModuleFactory.java")) { + File stubFile = new File(sourceFile.getPath().replace(".java", "Stub.txt")); + String stubLines = null; + try { + if (stubFile.exists()) { + stubLines = Util.loadStubFile(stubFile.getPath()); + } + + InputStream javaIn = new FileInputStream(sourceFile.getPath()); + BufferedReader javaBuf = new BufferedReader(new InputStreamReader(javaIn)); + StringBuffer output = new StringBuffer(); + String line = javaBuf.readLine(); + boolean writeLine = false; + while ((line = javaBuf.readLine()) != null) { + if(!writeLine && line.contains("*/")) { + line = header; + writeLine = true; + } else { + if (line.contains("TODO")) { + writeLine = false; + } else { + if (stubLines != null && line.contains("throw new")) { + line = stubLines.toString(); + writeLine = true; + } + } + } + if(writeLine) { + output.append(line).append(System.lineSeparator()); + } + } + javaBuf.close(); + + OutputStream javaOut = new FileOutputStream(sourceFile.getPath()); + javaOut.write(output.toString().getBytes()); + javaOut.close(); + } catch (IOException e) { + getLog().error("Error while reading/writing to files.", e); + } + + } + } + } +} diff --git a/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/Util.java b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/Util.java new file mode 100644 index 0000000000..16a41d9adb --- /dev/null +++ b/opendaylight/config/yang-test-plugin/src/main/java/org/opendaylight/controller/config/yang/test/plugin/Util.java @@ -0,0 +1,50 @@ +/* + * 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.config.yang.test.plugin; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.regex.Matcher; + +public class Util { + + public static String replaceDots(String path) { + path = path.replace(".", Matcher.quoteReplacement(File.separator)); + return path; + } + + public static String loadHeader() throws IOException { + StringBuffer header = new StringBuffer(); + InputStream headIn = Util.class.getClassLoader().getResourceAsStream("Header.txt"); + BufferedReader headBuf = new BufferedReader(new InputStreamReader(headIn)); + String line = null; + while ((line = headBuf.readLine()) != null) { + header.append(line).append(System.lineSeparator()); + } + headBuf.close(); + return header.toString(); + } + + public static String loadStubFile(String fileName) throws IOException { + InputStream stubIn = new FileInputStream(fileName); + BufferedReader stubBuf = new BufferedReader(new InputStreamReader(stubIn)); + + StringBuffer stubLines = new StringBuffer(); + String stubLine = null; + while ((stubLine = stubBuf.readLine()) != null) { + stubLines.append(stubLine).append(System.lineSeparator()); + } + stubBuf.close(); + return stubLines.toString(); + } +} diff --git a/opendaylight/config/yang-test-plugin/src/main/resources/Header.txt b/opendaylight/config/yang-test-plugin/src/main/resources/Header.txt new file mode 100644 index 0000000000..068fd26844 --- /dev/null +++ b/opendaylight/config/yang-test-plugin/src/main/resources/Header.txt @@ -0,0 +1,7 @@ +/* + * 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 + */ \ No newline at end of file diff --git a/opendaylight/config/yang-test/pom.xml b/opendaylight/config/yang-test/pom.xml index e3737c78f3..149348df4b 100644 --- a/opendaylight/config/yang-test/pom.xml +++ b/opendaylight/config/yang-test/pom.xml @@ -101,6 +101,19 @@ + + org.opendaylight.controller + yang-test-plugin + ${config.version} + + + + delete-sources + process-sources + + + + diff --git a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModule.java b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModule.java index 1122c1ffa4..c64f824b15 100644 --- a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModule.java +++ b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModule.java @@ -1,3 +1,11 @@ +/* + * 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.config.yang.test.impl; /** @@ -28,5 +36,6 @@ public final class DepTestImplModule extends org.opendaylight.controller.config. public void close() throws Exception { } }; + } } diff --git a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModuleFactory.java b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModuleFactory.java index 4152736768..c26c29ed60 100644 --- a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModuleFactory.java +++ b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/DepTestImplModuleFactory.java @@ -1,3 +1,11 @@ +/* + * 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.config.yang.test.impl; /** diff --git a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModule.java b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModule.java index 7e1848dd6a..3594ee0353 100644 --- a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModule.java +++ b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModule.java @@ -1,3 +1,11 @@ +/* + * 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.config.yang.test.impl; /** @@ -23,6 +31,7 @@ public final class NetconfTestImplModule extends org.opendaylight.controller.con @Override public java.lang.AutoCloseable createInstance() { - return NetconfTestImplModuleUtil.registerRuntimeBeans(this); +return NetconfTestImplModuleUtil.registerRuntimeBeans(this); + } } diff --git a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleFactory.java b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleFactory.java index 7cab528868..accc1db76e 100644 --- a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleFactory.java +++ b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleFactory.java @@ -1,3 +1,11 @@ +/* + * 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.config.yang.test.impl; /** diff --git a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleUtil.java b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleUtil.java index 58943c9df2..4de9804337 100644 --- a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleUtil.java +++ b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/NetconfTestImplModuleUtil.java @@ -1,10 +1,9 @@ -/** - * @author Tomas Olvecky +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * - * 11 2013 - * - * Copyright (c) 2013 by Cisco Systems, Inc. - * 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.config.yang.test.impl; diff --git a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModule.java b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModule.java index 52a71620bf..9ba1db4eed 100644 --- a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModule.java +++ b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModule.java @@ -1,3 +1,11 @@ +/* + * 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.config.yang.test.impl; /** @@ -28,5 +36,6 @@ public final class TestImplModule extends org.opendaylight.controller.config.yan public void close() throws Exception { } }; + } } diff --git a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModuleFactory.java b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModuleFactory.java index ce9aa92b64..a6ce734f96 100644 --- a/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModuleFactory.java +++ b/opendaylight/config/yang-test/src/main/java/org/opendaylight/controller/config/yang/test/impl/TestImplModuleFactory.java @@ -1,3 +1,11 @@ +/* + * 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.config.yang.test.impl; /**