From: lsedlak Date: Thu, 2 May 2013 13:06:12 +0000 (+0200) Subject: Added generate To File for specified directory. X-Git-Tag: releasepom-0.1.0~500^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=193bf712a646cd9464d4af055f637ba33bf30cdd Added generate To File for specified directory. Added overloading method generateToFile with File as input parameter and List of Files as result; Removed unused import in BaseTypeProvider test and BaseYangTypes; Changed binding-generator, yang and sal maven pom files to use java 1.7; Signed-off-by: Lukas Sedlak --- diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/generator/impl/BindingGeneratorImpl.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/generator/impl/BindingGeneratorImpl.java index dcee5fee33..3c0d9ed35a 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/generator/impl/BindingGeneratorImpl.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/generator/impl/BindingGeneratorImpl.java @@ -290,28 +290,15 @@ public class BindingGeneratorImpl implements BindingGenerator { } return null; } - - // private String resolveGeneratedTypePackageName(final SchemaPath - // schemaPath) { - // final StringBuilder builder = new StringBuilder(); - // builder.append(basePackageName); - // if ((schemaPath != null) && (schemaPath.getPath() != null)) { - // final List pathToNode = schemaPath.getPath(); - // final int traversalSteps = (pathToNode.size() - 1); - // for (int i = 0; i < traversalSteps; ++i) { - // builder.append("."); - // String nodeLocalName = pathToNode.get(i).getLocalName(); - // - // // TODO: refactor with use of BindingGeneratorUtil class - // nodeLocalName = nodeLocalName.replace(":", "."); - // nodeLocalName = nodeLocalName.replace("-", "."); - // builder.append(nodeLocalName); - // } - // return validatePackage(builder.toString()); - // } - // return null; - // } - + + private List augmentationToGenTypes(final Module module) { + final List augmentGenTypes = new ArrayList(); + + //TODO: add implementation + + return augmentGenTypes; + } + private GeneratedType containerToGenType(ContainerSchemaNode container) { if (container == null) { return null; diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java index b050ce65f1..a6425df693 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/main/java/org/opendaylight/controller/sal/binding/yang/types/BaseYangTypes.java @@ -1,67 +1,66 @@ -/* - * 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.sal.binding.yang.types; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.HashMap; -import java.util.Map; - -import org.opendaylight.controller.binding.generator.util.Types; -import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider; -import org.opendaylight.controller.sal.binding.model.api.Type; -import org.opendaylight.controller.yang.model.api.TypeDefinition; - -public class BaseYangTypes { - - private static Map typeMap = new HashMap(); - - public static final Type BOOLEAN_TYPE = Types.typeForClass(Boolean.class); - public static final Type INT8_TYPE = Types.typeForClass(Byte.class); - public static final Type INT16_TYPE = Types.typeForClass(Short.class); - public static final Type INT32_TYPE = Types.typeForClass(Integer.class); - public static final Type INT64_TYPE = Types.typeForClass(Long.class); - public static final Type STRING_TYPE = Types.typeForClass(String.class); - public static final Type DECIMAL64_TYPE = Types.typeForClass(Double.class); - public static final Type UINT8_TYPE = Types.typeForClass(Short.class); - public static final Type UINT16_TYPE = Types.typeForClass(Integer.class); - public static final Type UINT32_TYPE = Types.typeForClass(Long.class); - public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class); - - static { - typeMap.put("boolean", BOOLEAN_TYPE); - typeMap.put("int8", INT8_TYPE); - typeMap.put("int16", INT16_TYPE); - typeMap.put("int32", INT32_TYPE); - typeMap.put("int64", INT64_TYPE); - typeMap.put("string", STRING_TYPE); - typeMap.put("decimal64", DECIMAL64_TYPE); - typeMap.put("uint8", UINT8_TYPE); - typeMap.put("uint16", UINT16_TYPE); - typeMap.put("uint32", UINT32_TYPE); - typeMap.put("uint64", UINT64_TYPE); - } - - public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() { - - @Override - public Type javaTypeForYangType(String type) { - return typeMap.get(type); - } - - @Override - public Type javaTypeForSchemaDefinitionType(TypeDefinition type) { - if (type != null) { - return typeMap.get(type.getQName().getLocalName()); - } - - return null; - } - }; - -} +/* + * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.sal.binding.yang.types; + +import java.math.BigInteger; +import java.util.HashMap; +import java.util.Map; + +import org.opendaylight.controller.binding.generator.util.Types; +import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider; +import org.opendaylight.controller.sal.binding.model.api.Type; +import org.opendaylight.controller.yang.model.api.TypeDefinition; + +public class BaseYangTypes { + + private static Map typeMap = new HashMap(); + + public static final Type BOOLEAN_TYPE = Types.typeForClass(Boolean.class); + public static final Type INT8_TYPE = Types.typeForClass(Byte.class); + public static final Type INT16_TYPE = Types.typeForClass(Short.class); + public static final Type INT32_TYPE = Types.typeForClass(Integer.class); + public static final Type INT64_TYPE = Types.typeForClass(Long.class); + public static final Type STRING_TYPE = Types.typeForClass(String.class); + public static final Type DECIMAL64_TYPE = Types.typeForClass(Double.class); + public static final Type UINT8_TYPE = Types.typeForClass(Short.class); + public static final Type UINT16_TYPE = Types.typeForClass(Integer.class); + public static final Type UINT32_TYPE = Types.typeForClass(Long.class); + public static final Type UINT64_TYPE = Types.typeForClass(BigInteger.class); + + static { + typeMap.put("boolean", BOOLEAN_TYPE); + typeMap.put("int8", INT8_TYPE); + typeMap.put("int16", INT16_TYPE); + typeMap.put("int32", INT32_TYPE); + typeMap.put("int64", INT64_TYPE); + typeMap.put("string", STRING_TYPE); + typeMap.put("decimal64", DECIMAL64_TYPE); + typeMap.put("uint8", UINT8_TYPE); + typeMap.put("uint16", UINT16_TYPE); + typeMap.put("uint32", UINT32_TYPE); + typeMap.put("uint64", UINT64_TYPE); + } + + public static final TypeProvider BASE_YANG_TYPES_PROVIDER = new TypeProvider() { + + @Override + public Type javaTypeForYangType(String type) { + return typeMap.get(type); + } + + @Override + public Type javaTypeForSchemaDefinitionType(TypeDefinition type) { + if (type != null) { + return typeMap.get(type.getQName().getLocalName()); + } + + return null; + } + }; + +} diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/BaseTypeProvider.java b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/BaseTypeProvider.java index c54ac0613f..1ea5d16c02 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/BaseTypeProvider.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-generator-impl/src/test/java/org/opendaylight/controller/sal/binding/generator/impl/BaseTypeProvider.java @@ -10,7 +10,6 @@ package org.opendaylight.controller.sal.binding.generator.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import org.junit.Ignore; import org.junit.Test; import org.opendaylight.controller.binding.generator.util.Types; import org.opendaylight.controller.sal.binding.generator.spi.TypeProvider; diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorJavaFile.java b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorJavaFile.java index be115c9a45..474e4ad0f9 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorJavaFile.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/controller/sal/java/api/generator/GeneratorJavaFile.java @@ -20,18 +20,23 @@ import java.util.Set; import org.opendaylight.controller.sal.binding.model.api.CodeGenerator; import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject; import org.opendaylight.controller.sal.binding.model.api.GeneratedType; +import org.opendaylight.controller.sal.binding.model.api.Type; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class GeneratorJavaFile { + private static final Logger log = LoggerFactory + .getLogger(GeneratorJavaFile.class); private final CodeGenerator interfaceGenerator; private final ClassCodeGenerator classGenerator; - private final Set types; + private final Set genTypes; private final Set genTransferObjects; public GeneratorJavaFile(final CodeGenerator codeGenerator, final Set types) { this.interfaceGenerator = codeGenerator; - this.types = types; + this.genTypes = types; this.genTransferObjects = new HashSet(); classGenerator = new ClassCodeGenerator(); } @@ -40,72 +45,87 @@ public class GeneratorJavaFile { final Set genTransferObjects) { this.interfaceGenerator = new InterfaceGenerator(); this.classGenerator = new ClassCodeGenerator(); - this.types = types; + this.genTypes = types; this.genTransferObjects = genTransferObjects; } - public List generateToFile() throws IOException { - return generateToFile(null); - } - + @Deprecated public List generateToFile(String path) throws IOException { final List result = new ArrayList(); - for (GeneratedType type : types) { - String parentPath = generateParentPath(path, - type.getPackageName()); + for (GeneratedType genType : genTypes) { + final String parentPath = generateParentPath(path, + genType.getPackageName()); - File file = new File(parentPath, type.getName() + ".java"); - File parent = file.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); + final File directory = new File(parentPath); + final File genFile = generateTypeToJavaFile(directory, genType, + interfaceGenerator); + + if (genFile != null) { + result.add(genFile); } + } - if (!file.exists()) { - FileWriter fw = null; - BufferedWriter bw = null; + for (GeneratedTransferObject transferObject : genTransferObjects) { + final String parentPath = generateParentPath(path, + transferObject.getPackageName()); - file.createNewFile(); - fw = new FileWriter(file); - bw = new BufferedWriter(fw); - Writer writer = interfaceGenerator.generate(type); - bw.write(writer.toString()); + final File directory = new File(parentPath); + final File genFile = generateTypeToJavaFile(directory, + transferObject, classGenerator); - if (bw != null) { - bw.close(); - } - result.add(file); + if (genFile != null) { + result.add(genFile); } } + return result; + } + public List generateToFile(final File directory) throws IOException { + final List result = new ArrayList(); + for (GeneratedType type : genTypes) { + final File genFile = generateTypeToJavaFile(directory, type, + interfaceGenerator); + + if (genFile != null) { + result.add(genFile); + } + } for (GeneratedTransferObject transferObject : genTransferObjects) { - String parentPath = generateParentPath(path, - transferObject.getPackageName()); + final File genFile = generateTypeToJavaFile(directory, + transferObject, classGenerator); - File file = new File(parentPath, transferObject.getName() + ".java"); - File parent = file.getParentFile(); - if (!parent.exists()) { - parent.mkdirs(); + if (genFile != null) { + result.add(genFile); } + } + return result; + } - if (!file.exists()) { - FileWriter fw = null; - BufferedWriter bw = null; + private File generateTypeToJavaFile(final File directory, final Type type, + final CodeGenerator generator) throws IOException { + if ((directory != null) && (type != null) && (generator != null)) { + if (!directory.exists()) { + directory.mkdirs(); + } + + final File file = new File(directory, type.getName() + ".java"); + try (final FileWriter fw = new FileWriter(file)) { file.createNewFile(); - fw = new FileWriter(file); - bw = new BufferedWriter(fw); - Writer writer = classGenerator.generate(transferObject); - bw.write(writer.toString()); - if (bw != null) { - bw.close(); + try (final BufferedWriter bw = new BufferedWriter(fw)) { + Writer writer = generator.generate(type); + bw.write(writer.toString()); } - result.add(file); + } catch (IOException e) { + log.error(e.getMessage()); + throw new IOException(e.getMessage()); } - } - return result; + return file; + } + return null; } private String generateParentPath(String path, String pkg) { @@ -142,5 +162,4 @@ public class GeneratorJavaFile { } return fullPath; } - } diff --git a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/controller/sal/java/api/generator/test/GeneratorJavaFileTest.java b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/controller/sal/java/api/generator/test/GeneratorJavaFileTest.java index 4ae8cc1275..90fbb6f605 100644 --- a/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/controller/sal/java/api/generator/test/GeneratorJavaFileTest.java +++ b/opendaylight/sal/yang-prototype/code-generator/binding-java-api-generator/src/test/java/org/opendaylight/controller/sal/java/api/generator/test/GeneratorJavaFileTest.java @@ -14,7 +14,6 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -80,7 +79,8 @@ public class GeneratorJavaFileTest { new InterfaceGenerator(), types); generator.generateToFile(PATH); - String[] files = new File(PATH + FS + "org" + FS + "opendaylight" + FS + "controller" + FS + "gen").list(); + String[] files = new File(PATH + FS + "org" + FS + "opendaylight" + FS + + "controller" + FS + "gen").list(); List filesList = Arrays.asList(files); assertEquals(3, files.length); @@ -88,7 +88,7 @@ public class GeneratorJavaFileTest { assertTrue(filesList.contains("Type2.java")); assertTrue(filesList.contains("Type3.java")); } - + @Test public void compilationTest() throws Exception { final YangModelParserImpl parser = new YangModelParserImpl(); @@ -97,17 +97,12 @@ public class GeneratorJavaFileTest { final File sourcesDir = new File("src/test/resources/yang"); final List sourceFiles = new ArrayList(); final File[] fileArray = sourcesDir.listFiles(); - + for (int i = 0; i < fileArray.length; ++i) { sourceFiles.add(fileArray[i]); } -// String[] sourcesDirPaths = new String[sourceFiles.length]; -// for (int i = 0; i < sourceFiles.length; i++) { -// sourcesDirPaths[i] = sourceFiles[i].getAbsolutePath(); -// } - final Set modulesToBuild = parser - .parseYangModels(sourceFiles); + final Set modulesToBuild = parser.parseYangModels(sourceFiles); final SchemaContext context = parser .resolveSchemaContext(modulesToBuild); @@ -115,7 +110,8 @@ public class GeneratorJavaFileTest { final Set typesToGenerate = new HashSet(); final Set tosToGenerate = new HashSet(); for (Type type : types) { - if (type instanceof GeneratedType && !(type instanceof GeneratedTransferObject)) { + if (type instanceof GeneratedType + && !(type instanceof GeneratedTransferObject)) { typesToGenerate.add((GeneratedType) type); } @@ -162,7 +158,7 @@ public class GeneratorJavaFileTest { /** * Search recursively given directory for *.java files. - * + * * @param directory * directory to search * @return List of java files found diff --git a/opendaylight/sal/yang-prototype/code-generator/pom.xml b/opendaylight/sal/yang-prototype/code-generator/pom.xml index dafba87168..89e2794fd1 100644 --- a/opendaylight/sal/yang-prototype/code-generator/pom.xml +++ b/opendaylight/sal/yang-prototype/code-generator/pom.xml @@ -1,100 +1,100 @@ - - 4.0.0 - org.opendaylight.controller - binding-generator - 1.0 - pom - binding-generator - - - UTF-8 - - - - ../yang - ../sal/sal-schema-repository-api - code-generator-demo - yang-model-parser-api - yang-model-parser-impl - binding-model-api - binding-generator-api - binding-generator-spi - binding-generator-util - binding-generator-impl - binding-java-api-generator - maven-yang - maven-yang-plugin - maven-yang-plugin-it - maven-sal-api-gen-plugin - - - - - junit - junit - 4.10 - test - true - - - org.slf4j - slf4j-api - 1.7.2 - - - org.slf4j - slf4j-simple - 1.7.2 - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.0 - true - - 1.6 - 1.6 - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.8.1 - - maven - - - - - aggregate - - site - - - - - - - - - org.codehaus.mojo - findbugs-maven-plugin - 2.4.0 - - Max - Low - site - - - - org.codehaus.mojo - jdepend-maven-plugin - 2.0-beta-2 - - - - + + 4.0.0 + org.opendaylight.controller + binding-generator + 1.0 + pom + binding-generator + + + UTF-8 + + + + ../yang + ../sal/sal-schema-repository-api + code-generator-demo + yang-model-parser-api + yang-model-parser-impl + binding-model-api + binding-generator-api + binding-generator-spi + binding-generator-util + binding-generator-impl + binding-java-api-generator + maven-yang + maven-yang-plugin + maven-yang-plugin-it + maven-sal-api-gen-plugin + + + + + junit + junit + 4.10 + test + true + + + org.slf4j + slf4j-api + 1.7.2 + + + org.slf4j + slf4j-simple + 1.7.2 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0 + true + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.8.1 + + maven + + + + + aggregate + + site + + + + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 2.4.0 + + Max + Low + site + + + + org.codehaus.mojo + jdepend-maven-plugin + 2.0-beta-2 + + + + diff --git a/opendaylight/sal/yang-prototype/sal/pom.xml b/opendaylight/sal/yang-prototype/sal/pom.xml index 9f77613b89..2604a45acf 100644 --- a/opendaylight/sal/yang-prototype/sal/pom.xml +++ b/opendaylight/sal/yang-prototype/sal/pom.xml @@ -1,107 +1,107 @@ - - 4.0.0 - org.opendaylight.controller - sal - 1.0-SNAPSHOT - pom - - - sal-common - sal-common-util - sal-core-api - sal-data-api - sal-binding-api - sal-binding-spi - sal-binding-broker-impl - sal-schema-repository-api - sal-core-spi - sal-broker-impl - sal-core-demo - - - - - - com.google.guava - guava - 14.0.1 - jar - - - org.slf4j - slf4j-api - 1.7.2 - - - junit - junit - 4.10 - - - - - - - junit - junit - test - true - - - org.mockito - mockito-all - 1.9.5 - test - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.0 - true - - 1.6 - 1.6 - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.8.1 - - maven - - - - - aggregate - - site - - - - - - - - - org.codehaus.mojo - findbugs-maven-plugin - 2.4.0 - - Max - Low - site - - - - org.codehaus.mojo - jdepend-maven-plugin - 2.0-beta-2 - - + + 4.0.0 + org.opendaylight.controller + sal + 1.0-SNAPSHOT + pom + + + sal-common + sal-common-util + sal-core-api + sal-data-api + sal-binding-api + sal-binding-spi + sal-binding-broker-impl + sal-schema-repository-api + sal-core-spi + sal-broker-impl + sal-core-demo + + + + + + com.google.guava + guava + 14.0.1 + jar + + + org.slf4j + slf4j-api + 1.7.2 + + + junit + junit + 4.10 + + + + + + + junit + junit + test + true + + + org.mockito + mockito-all + 1.9.5 + test + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0 + true + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.8.1 + + maven + + + + + aggregate + + site + + + + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 2.4.0 + + Max + Low + site + + + + org.codehaus.mojo + jdepend-maven-plugin + 2.0-beta-2 + + \ No newline at end of file diff --git a/opendaylight/sal/yang-prototype/yang/pom.xml b/opendaylight/sal/yang-prototype/yang/pom.xml index d10366c2cf..68134d4ac8 100644 --- a/opendaylight/sal/yang-prototype/yang/pom.xml +++ b/opendaylight/sal/yang-prototype/yang/pom.xml @@ -1,84 +1,84 @@ - - 4.0.0 - org.opendaylight.controller - yang - 1.0 - pom - - yang-common - yang-data-api - yang-data-util - yang-model-api - yang-model-util - yang-binding - - - - - junit - junit - 4.10 - test - true - - - org.slf4j - slf4j-api - 1.7.2 - - - org.slf4j - slf4j-simple - 1.7.2 - - - - - - org.apache.maven.plugins - maven-compiler-plugin - 2.0 - true - - 1.6 - 1.6 - - - - org.apache.maven.plugins - maven-javadoc-plugin - 2.8.1 - - maven - - - - - aggregate - - site - - - - - - - - - org.codehaus.mojo - findbugs-maven-plugin - 2.4.0 - - Max - Low - site - - - - org.codehaus.mojo - jdepend-maven-plugin - 2.0-beta-2 - - - - + + 4.0.0 + org.opendaylight.controller + yang + 1.0 + pom + + yang-common + yang-data-api + yang-data-util + yang-model-api + yang-model-util + yang-binding + + + + + junit + junit + 4.10 + test + true + + + org.slf4j + slf4j-api + 1.7.2 + + + org.slf4j + slf4j-simple + 1.7.2 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 2.0 + true + + 1.7 + 1.7 + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.8.1 + + maven + + + + + aggregate + + site + + + + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 2.4.0 + + Max + Low + site + + + + org.codehaus.mojo + jdepend-maven-plugin + 2.0-beta-2 + + + +