From 76dc892491948adae5e5e62cf94448967e8d865b Mon Sep 17 00:00:00 2001 From: Ashlee Young Date: Sun, 6 Dec 2015 07:15:03 -0800 Subject: Fixes bad POM file with ONOS commit 8c68536972f63069c263635c9d9f4f31d7f3e9a2 Change-Id: I7adb5a2d3738d53dbc41db7577768b0e7ced5450 Signed-off-by: Ashlee Young --- framework/src/onos/tools/package/yangtools/pom.xml | 76 ++++++++++ .../java/org/onoproject/yangtool/YangLoader.java | 167 +++++++++++++++++++++ .../org/onoproject/yangtool/YangLoaderMain.java | 16 ++ .../yangtools/src/main/resources/pom-template.xml | 95 ++++++++++++ 4 files changed, 354 insertions(+) create mode 100644 framework/src/onos/tools/package/yangtools/pom.xml create mode 100644 framework/src/onos/tools/package/yangtools/src/main/java/org/onoproject/yangtool/YangLoader.java create mode 100644 framework/src/onos/tools/package/yangtools/src/main/java/org/onoproject/yangtool/YangLoaderMain.java create mode 100644 framework/src/onos/tools/package/yangtools/src/main/resources/pom-template.xml (limited to 'framework/src/onos/tools/package/yangtools') diff --git a/framework/src/onos/tools/package/yangtools/pom.xml b/framework/src/onos/tools/package/yangtools/pom.xml new file mode 100644 index 00000000..82422c0f --- /dev/null +++ b/framework/src/onos/tools/package/yangtools/pom.xml @@ -0,0 +1,76 @@ + + + 4.0.0 + org.onosproject + yangloader + 1.0-SNAPSHOT + + + commons-configuration + commons-configuration + 1.10 + + + commons-io + commons-io + 2.4 + + + com.google.guava + guava + 18.0 + + + org.onosproject + onlab-misc + 1.4.0-SNAPSHOT + + + commons-collections + commons-collections + 3.2.1 + true + + + org.slf4j + slf4j-jdk14 + 1.7.12 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-shade-plugin + 2.3 + + + package + + shade + + + + + org.onoproject.yangtool.YangLoaderMain + + + + + + + + + + diff --git a/framework/src/onos/tools/package/yangtools/src/main/java/org/onoproject/yangtool/YangLoader.java b/framework/src/onos/tools/package/yangtools/src/main/java/org/onoproject/yangtool/YangLoader.java new file mode 100644 index 00000000..37126890 --- /dev/null +++ b/framework/src/onos/tools/package/yangtools/src/main/java/org/onoproject/yangtool/YangLoader.java @@ -0,0 +1,167 @@ +package org.onoproject.yangtool; + +import com.google.common.io.Files; +import org.apache.commons.configuration.ConfigurationException; +import org.apache.commons.configuration.XMLConfiguration; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; +import org.onlab.util.Tools; + +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.io.PrintWriter; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Class that takes in input two paths, one to the yang file and one where + * to generate the interface, and a yang container name, generates the sources + * through OpenDaylight code generator plugin from yang, and modifies + * them accordingly to the needs of the ONOS behavior system. + */ +public class YangLoader { + /** + * Public method to take a yang file from the input folder and generate classes + * to the output folder. + * @param inputFolder forlder with the yang files + * @param completeOuputFolder folder where to put the desired classes + * @throws IOException + * @throws ConfigurationException + * @throws InterruptedException + */ + public void generateBehaviourInterface(String inputFolder, + String completeOuputFolder) + throws IOException, ConfigurationException, InterruptedException { + File projectDir = createTemporaryProject(inputFolder); + List containerNames = findContainerName( + new File(projectDir.getAbsolutePath() + "/src")); + System.out.println("Containers " + containerNames); + generateJava(projectDir); + //modifyClasses(containerName, projectDir, completeInterfaceOuputFolder); + copyFiles(new File(projectDir.getAbsolutePath() + "/dst"), + new File(completeOuputFolder)); + //System.out.println("Sources in " + completeOuputFolder); + + } + + private List findContainerName(File scrDir) { + List allContainers = new ArrayList<>(); + Arrays.asList(scrDir.listFiles()).stream().forEach(f -> { + try { + FileUtils.readLines(f).stream() + .filter(line -> line.matches("(.*)container(.*)\\{")) + .collect(Collectors.toList()).forEach(s -> { + s = s.replace("container", ""); + s = s.replace("{", ""); + allContainers.add(s.trim()); + }); + + } catch (IOException e) { + throw new RuntimeException(e); + } + }); + return allContainers; + } + + private File createTemporaryProject(String inputFolder) throws IOException, + ConfigurationException { + File tempDir = Files.createTempDir(); + File scrDir = new File(tempDir, "src"); + scrDir.mkdir(); + copyFiles(new File(inputFolder), scrDir); + createPomFile(tempDir, scrDir); + return tempDir; + + } + + private void copyFiles(File inputFolder, File scrDir) throws IOException { + Tools.copyDirectory(inputFolder, scrDir); + } + + private void createPomFile(File tempDir, File scrDir) throws ConfigurationException { + File pom = new File(tempDir, "pom.xml"); + File dstDir = new File(tempDir, "dst"); + dstDir.mkdir(); + XMLConfiguration cfg = new XMLConfiguration(); + cfg.load(getClass().getResourceAsStream("/pom-template.xml")); + cfg.setProperty("build.plugins.plugin.executions.execution." + + "configuration.yangFilesRootDir", scrDir.getPath()); + cfg.setProperty("build.plugins.plugin.executions." + + "execution.configuration.codeGenerators." + + "generator.outputBaseDir", dstDir.getPath()); + cfg.save(pom); + } + + private void generateJava(File projectDir) + throws IOException, InterruptedException { + Runtime rt = Runtime.getRuntime(); + Process pr = rt.exec("mvn generate-sources", null, projectDir); + String s = IOUtils.toString(pr.getInputStream(), "UTF-8"); + if (pr.waitFor() == 0) { + if (s.contains("[WARNING]")) { + System.out.println("Sources not generated, warning log: \n" + s); + } else { + System.out.println("Sources generated"); + } + } else { + System.out.println("Sources not generated. " + s + + " \nError " + pr.getInputStream().read()); + } + } + + //parsing classes part, for now is not used. + private void modifyClasses(String containerName, File projectDir, + String pathToNewInterface) throws IOException { + String odlInterfacepath = getPathWithFileName(containerName, projectDir.getPath()); + odlInterfacepath = odlInterfacepath + "/"; + parseClass(odlInterfacepath, pathToNewInterface, containerName); + System.out.println("ONOS behaviour interface generated " + + "correctly at " + pathToNewInterface); + } + + private String getPathWithFileName(String filename, String pathToGenerated) { + File[] directories = new File(pathToGenerated).listFiles(File::isDirectory); + while (directories.length != 0) { + pathToGenerated = pathToGenerated + "/" + directories[0].getName(); + directories = new File(pathToGenerated).listFiles(File::isDirectory); + } + File dir = new File(pathToGenerated); + File behaviour = (File) Arrays.asList(dir.listFiles()).stream() + .filter(f -> f.getName().equals(filename)).toArray()[0]; + return behaviour.getParentFile().getAbsolutePath(); + } + + private void parseClass(String filePath, String pathToNewInterface, + String filename) throws IOException { + InputStream fis = null; + String newFile = "package org.onosproject.net.behaviour;\n" + + "import org.onosproject.net.driver.HandlerBehaviour;\n"; + fis = new FileInputStream(filePath + filename); + InputStreamReader isr = new InputStreamReader(fis, Charset.forName("UTF-8")); + BufferedReader br = new BufferedReader(isr); + String line; + while ((line = br.readLine()) != null) { + if (!line.contains("org.opendaylight.")) { + if (line.contains("ChildOf")) { + newFile = newFile + "HandlerBehaviour\n"; + } else { + newFile = newFile + line + "\n"; + } + } + } + PrintWriter out = new PrintWriter(pathToNewInterface + + filename.replace(".java", "") + + "Interface.java"); + out.print(newFile); + out.flush(); + } + + +} diff --git a/framework/src/onos/tools/package/yangtools/src/main/java/org/onoproject/yangtool/YangLoaderMain.java b/framework/src/onos/tools/package/yangtools/src/main/java/org/onoproject/yangtool/YangLoaderMain.java new file mode 100644 index 00000000..7b137677 --- /dev/null +++ b/framework/src/onos/tools/package/yangtools/src/main/java/org/onoproject/yangtool/YangLoaderMain.java @@ -0,0 +1,16 @@ +package org.onoproject.yangtool; + +import org.apache.commons.configuration.ConfigurationException; + +import java.io.IOException; + +/** + * Main of the uangloader tool in order to be called through command line. + */ +public class YangLoaderMain { + public static void main (String args []) throws IOException, + ConfigurationException, InterruptedException { + YangLoader yl = new YangLoader(); + yl.generateBehaviourInterface(args[0], args[1]); + } +} diff --git a/framework/src/onos/tools/package/yangtools/src/main/resources/pom-template.xml b/framework/src/onos/tools/package/yangtools/src/main/resources/pom-template.xml new file mode 100644 index 00000000..5280e213 --- /dev/null +++ b/framework/src/onos/tools/package/yangtools/src/main/resources/pom-template.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + groupId + yangloader + 1.0-SNAPSHOT + + + opendaylight-release + opendaylight-release + + http://nexus.opendaylight.org/content/repositories/opendaylight.release/ + + + + opendaylight-snapshot + opendaylight-snapshot + + http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/ + + + + + + opendaylight-release + opendaylight-release + + http://nexus.opendaylight.org/content/repositories/opendaylight.release/ + + + + opendaylight-snapshot + opendaylight-snapshot + + http://nexus.opendaylight.org/content/repositories/opendaylight.snapshot/ + + + + + + org.opendaylight.yangtools + yang-binding + 0.7.2-Lithium-SR2 + + + + + + org.opendaylight.yangtools + yang-maven-plugin + 0.7.2-Lithium-SR2 + + + + generate-sources + + + + INPUTDIR + + + + org.opendaylight.yangtools.maven.sal.api.gen.plugin.CodeGeneratorImpl + + + OUTPUTDIR + + + false + + + + + + org.opendaylight.yangtools + maven-sal-api-gen-plugin + 0.7.2-Lithium-SR2 + jar + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + 1.8 + 1.8 + + + + + -- cgit 1.2.3-korg