Bharat saraswal
Committed by Gerrit Code Review

[ONOS-4149],[ONOS-3909] YANG list translator impl and bug fixes.

Change-Id: Ia1a94142a3a114815766f661ed850bf9cacde66f
Showing 29 changed files with 526 additions and 665 deletions
......@@ -16,7 +16,6 @@
package org.onosproject.yangutils.plugin.manager;
import java.io.File;
import java.util.Iterator;
import java.util.List;
......@@ -24,31 +23,34 @@ import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.parser.YangUtilsParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
import org.sonatype.plexus.build.incremental.BuildContext;
import static org.apache.maven.plugins.annotations.LifecyclePhase.GENERATE_SOURCES;
import static org.apache.maven.plugins.annotations.ResolutionScope.COMPILE;
import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addToSource;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPkgToPath;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.copyYangFilesToTarget;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
/**
* ONOS YANG utility maven plugin. Goal of plugin is yang2java Execution phase
* in generate-sources requiresDependencyResolution at compile time.
* ONOS YANG utility maven plugin.
* Goal of plugin is yang2java Execution phase in generate-sources requiresDependencyResolution at compile time.
*/
@Mojo(name = "yang2java", defaultPhase = LifecyclePhase.GENERATE_SOURCES,
requiresDependencyResolution = ResolutionScope.COMPILE, requiresProject = true)
@Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE,
requiresProject = true)
public class YangUtilManager extends AbstractMojo {
/**
......@@ -87,8 +89,7 @@ public class YangUtilManager extends AbstractMojo {
@Component
private BuildContext context;
private static final String DEFAULT_PKG = File.separator
+ UtilConstants.DEFAULT_BASE_PKG.replace(UtilConstants.PERIOD, UtilConstants.SLASH);
private static final String DEFAULT_PKG = SLASH + convertPkgToPath(DEFAULT_BASE_PKG);
private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
private String searchDir;
......@@ -116,7 +117,7 @@ public class YangUtilManager extends AbstractMojo {
clean(getDirectory(baseDir, outputDirectory));
searchDir = getDirectory(baseDir, yangFilesDir);
codeGenDir = getDirectory(baseDir, genFilesDir) + File.separator;
codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
List<String> yangFiles = YangFileScanner.getYangFiles(searchDir);
Iterator<String> yangFileIterator = yangFiles.iterator();
......@@ -133,7 +134,7 @@ public class YangUtilManager extends AbstractMojo {
}
if (e.getMessage() != null) {
logInfo = logInfo + UtilConstants.NEW_LINE + e.getMessage();
logInfo = logInfo + NEW_LINE + e.getMessage();
}
getLog().info(logInfo);
}
......@@ -143,7 +144,7 @@ public class YangUtilManager extends AbstractMojo {
copyYangFilesToTarget(yangFiles, getDirectory(baseDir, outputDirectory), project);
} catch (Exception e) {
getLog().info(e);
//throw new MojoExecutionException("Exception occured due to " + e.getLocalizedMessage());
throw new MojoExecutionException("Exception occured due to " + e.getLocalizedMessage());
}
}
}
......
......@@ -18,6 +18,7 @@ package org.onosproject.yangutils.translator.tojava;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
......@@ -246,4 +247,40 @@ public final class JavaAttributeInfo {
return newAttr;
}
/**
* Create an attribute info object corresponding to the passed type def attribute
* information and return it.
*
* @param curNode current data model node for which the java file is being
* generated
* @param attributeType leaf data type
* @param attributeName leaf name
* @param isListAttribute is the current added attribute needs to be a list
* @return AttributeInfo attribute details required to add in temporary
* files
*/
public static JavaAttributeInfo getAttributeInfoOfTypeDef(YangNode curNode,
YangType<?> attributeType, String attributeName,
boolean isListAttribute) {
JavaAttributeInfo newAttr = new JavaAttributeInfo();
/*
* Get the import info corresponding to the attribute for import in
* generated java files or qualified access
*/
JavaQualifiedTypeInfo importInfo = getQualifiedTypeInfoOfLeafAttribute(curNode,
attributeType, attributeName, isListAttribute);
AttributesJavaDataType.addImportInfo(importInfo);
newAttr.setImportInfo(importInfo);
newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(
curNode, importInfo));
newAttr.setAttributeName(getCamelCase(attributeName));
newAttr.setListAttr(isListAttribute);
newAttr.setImportInfo(importInfo);
newAttr.setAttributeType(attributeType);
return newAttr;
}
}
......
......@@ -15,11 +15,26 @@
*/
package org.onosproject.yangutils.translator.tojava;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;
import org.onosproject.yangutils.datamodel.YangNode;
import static org.onosproject.yangutils.utils.UtilConstants.COLLECTION_IMPORTS;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
/**
* Generated Java file can contain imports.
*/
......@@ -49,6 +64,7 @@ public class JavaImportData {
* @return true if any of the attribute needs to be maintained as a list.
*/
public boolean getIfListImported() {
return isListToImport;
}
......@@ -58,6 +74,7 @@ public class JavaImportData {
* @param isList status to mention list is bing imported.
*/
public void setIfListImported(boolean isList) {
isListToImport = isList;
}
......@@ -67,6 +84,7 @@ public class JavaImportData {
* @return the set containing the imported class/interface info
*/
public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
return importSet;
}
......@@ -76,6 +94,7 @@ public class JavaImportData {
* @param importSet the set containing the imported class/interface info
*/
private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
this.importSet = importSet;
}
......@@ -95,6 +114,7 @@ public class JavaImportData {
* @return status of new addition of class/interface to the import set
*/
public boolean addImportInfo(YangNode curNode, JavaQualifiedTypeInfo newImportInfo) {
if (!(curNode instanceof HasJavaImportData)) {
throw new RuntimeException("missing import info in data model node");
}
......@@ -108,4 +128,62 @@ public class JavaImportData {
((HasJavaImportData) curNode).getJavaImportData().getImportSet().add(newImportInfo);
return true;
}
/**
* Returns import for class.
*
* @param attr java attribute info
* @return imports for class
*/
public List<String> getImports(JavaAttributeInfo attr) {
String importString;
List<String> imports = new ArrayList<>();
for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
importString = IMPORT;
if (importInfo.getPkgInfo() != EMPTY_STRING && importInfo.getClassInfo() != null
&& importInfo.getPkgInfo() != JAVA_LANG) {
importString = importString + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
+ NEW_LINE;
imports.add(importString);
}
}
if (attr.isListAttr()) {
imports.add(setImportForList());
}
java.util.Collections.sort(imports);
return imports;
}
/**
* Gets import for hash and equals method.
*
* @return import for hash and equals method
*/
public String getImportForHashAndEquals() {
return IMPORT + JAVA_UTIL_OBJECTS_IMPORT_PKG + PERIOD + JAVA_UTIL_OBJECTS_IMPORT_CLASS;
}
/**
* Gets import for to string method.
*
* @return import for to string method
*/
public String getImportForToString() {
return IMPORT + GOOGLE_MORE_OBJECT_IMPORT_PKG + PERIOD + GOOGLE_MORE_OBJECT_IMPORT_CLASS;
}
/**
* Sets import for to list.
*/
private static String setImportForList() {
return IMPORT + COLLECTION_IMPORTS + PERIOD + LIST + SEMI_COLAN + NEW_LINE;
}
}
......
......@@ -27,12 +27,10 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
/**
......@@ -162,7 +160,7 @@ public class YangJavaContainer extends YangContainer
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
createPackage(absloutePath, getName());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
......@@ -180,8 +178,8 @@ public class YangJavaContainer extends YangContainer
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -17,6 +17,7 @@
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangInput;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
......@@ -27,7 +28,6 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
......@@ -180,7 +180,7 @@ public class YangJavaInput extends YangInput
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -27,7 +27,6 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
......@@ -155,9 +154,11 @@ public class YangJavaList extends YangList
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
......@@ -180,7 +181,7 @@ public class YangJavaList extends YangList
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -27,12 +27,10 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
/**
......@@ -163,7 +161,6 @@ public class YangJavaModule extends YangModule
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
createPackage(absloutePath, getName());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
......@@ -174,8 +171,8 @@ public class YangJavaModule extends YangModule
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
return;
}
}
......
......@@ -17,6 +17,7 @@
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangOutput;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
......@@ -27,7 +28,6 @@ import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.utils.GenerateJavaCodeExitBuilder.generateJavaFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
......@@ -180,7 +180,7 @@ public class YangJavaOutput extends YangOutput
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -75,6 +75,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
......@@ -88,6 +89,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
......@@ -98,6 +100,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
......@@ -109,6 +112,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public void setJavaImportData(JavaImportData javaImportData) {
this.javaImportData = javaImportData;
}
......@@ -119,6 +123,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
if (tempFileHandle == null) {
throw new RuntimeException("missing temp file hand for current node "
+ getJavaFileInfo().getJavaName());
......@@ -133,6 +138,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
tempFileHandle = fileHandle;
}
......@@ -144,6 +150,7 @@ public class YangJavaSubModule extends YangSubModule
* @return the name space string of the module.
*/
private String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
// TODO Auto-generated method stub
return "";
}
......@@ -157,6 +164,7 @@ public class YangJavaSubModule extends YangSubModule
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getRootPackage(getVersion(),
getNameSpaceFromModule(getBelongsTo()),
......
......@@ -15,24 +15,29 @@
*/
package org.onosproject.yangutils.translator.tojava.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
import org.onosproject.yangutils.translator.tojava.HasTempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCurNodePackage;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
/**
* Type define information extended to support java code generation.
*/
public class YangJavaTypeDef extends YangTypeDef
implements JavaCodeGenerator, HasJavaFileInfo, HasJavaImportData {
implements JavaCodeGenerator, HasJavaFileInfo, HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
......@@ -46,13 +51,19 @@ public class YangJavaTypeDef extends YangTypeDef
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Default constructor.
*/
public YangJavaTypeDef() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
getJavaFileInfo().setGeneratedFileTypes(GENERATE_TYPEDEF_CLASS);
}
/**
......@@ -62,6 +73,7 @@ public class YangJavaTypeDef extends YangTypeDef
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
......@@ -75,6 +87,7 @@ public class YangJavaTypeDef extends YangTypeDef
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
......@@ -85,6 +98,7 @@ public class YangJavaTypeDef extends YangTypeDef
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
......@@ -96,32 +110,73 @@ public class YangJavaTypeDef extends YangTypeDef
*/
@Override
public void setJavaImportData(JavaImportData javaImportData) {
this.javaImportData = javaImportData;
}
/**
* Get the temporary file handle.
*
* @return temporary file handle
*/
@Override
public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
if (tempFileHandle == null) {
throw new RuntimeException("missing temp file hand for current node "
+ getJavaFileInfo().getJavaName());
}
return tempFileHandle;
}
/**
* Set temporary file handle.
*
* @param fileHandle temporary file handle
*/
@Override
public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
tempFileHandle = fileHandle;
}
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
*
* @param codeGenDir code generation directory
* @throws IOException IO operations fails
*/
@Override
public void generateCodeEntry(String codeGenDir) {
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
// TODO: generate type define temporary files
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addTypeDefAttributeToTempFiles(this);
}
/**
* Create a java file using the YANG grouping info.
* @throws IOException IO operations fails
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().setCurYangNode(this);
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_TYPEDEF_CLASS, this);
}
}
......
......@@ -16,8 +16,22 @@
package org.onosproject.yangutils.translator.tojava.utils;
import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPLEMENTS;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
/**
* Generates class definition for generated files.
......@@ -44,19 +58,19 @@ public final class ClassDefinitionGenerator {
* based on the file type and the YANG name of the file, generate the
* class / interface definition start.
*/
if ((genFileTypes & GeneratedJavaFileType.INTERFACE_MASK) != 0) {
if ((genFileTypes & INTERFACE_MASK) != 0) {
return getInterfaceDefinition(yangName);
} else if ((genFileTypes & GeneratedJavaFileType.BUILDER_CLASS_MASK) != 0) {
} else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) {
return getBuilderClassDefinition(yangName);
} else if ((genFileTypes & GeneratedJavaFileType.IMPL_CLASS_MASK) != 0) {
} else if ((genFileTypes & IMPL_CLASS_MASK) != 0) {
return getImplClassDefinition(yangName);
} else if ((genFileTypes & GeneratedJavaFileType.BUILDER_INTERFACE_MASK) != 0) {
} else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) {
return getBuilderInterfaceDefinition(yangName);
} else if ((genFileTypes & GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS) != 0) {
} else if ((genFileTypes & GENERATE_TYPEDEF_CLASS) != 0) {
return getTypeDefClassDefinition(yangName);
}
......@@ -71,8 +85,7 @@ public final class ClassDefinitionGenerator {
*/
private static String getInterfaceDefinition(String yangName) {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE + UtilConstants.SPACE + yangName
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -83,8 +96,8 @@ public final class ClassDefinitionGenerator {
* @return definition
*/
private static String getBuilderInterfaceDefinition(String yangName) {
return UtilConstants.INTERFACE + UtilConstants.SPACE + yangName + UtilConstants.BUILDER + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.NEW_LINE;
return INTERFACE + SPACE + yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + NEW_LINE;
}
/**
......@@ -95,10 +108,8 @@ public final class ClassDefinitionGenerator {
*/
private static String getBuilderClassDefinition(String yangName) {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.CLASS + UtilConstants.SPACE + yangName
+ UtilConstants.BUILDER + UtilConstants.SPACE + UtilConstants.IMPLEMENTS + UtilConstants.SPACE
+ yangName + UtilConstants.PERIOD + yangName + UtilConstants.BUILDER + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
+ yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -109,10 +120,8 @@ public final class ClassDefinitionGenerator {
*/
private static String getImplClassDefinition(String yangName) {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE
+ UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.IMPL + UtilConstants.SPACE
+ UtilConstants.IMPLEMENTS + UtilConstants.SPACE + yangName + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + IMPL + SPACE + IMPLEMENTS + SPACE + yangName
+ SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -123,9 +132,6 @@ public final class ClassDefinitionGenerator {
*/
private static String getTypeDefClassDefinition(String yangName) {
return UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.FINAL + UtilConstants.SPACE
+ UtilConstants.CLASS + UtilConstants.SPACE + yangName + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE;
return PUBLIC + SPACE + FINAL + SPACE + CLASS + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
}
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator.tojava.utils;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaImportData;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.appendFileContents;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.clean;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.closeFileHandles;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateImplClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateInterfaceFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.getFileObject;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.insert;
/**
* Provides generate java code exit.
*/
public final class GenerateJavaCodeExitBuilder {
private static final String JAVA_FILE_EXTENSION = ".java";
/**
* Default constructor.
*/
private GenerateJavaCodeExitBuilder() {
}
/**
* Sets import for hash and equals method.
*
* @return import string for implementing hash and equals
*/
private static String setImportForHashAndEquals() {
return UtilConstants.IMPORT + UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_PKG + UtilConstants.PERIOD
+ UtilConstants.JAVA_UTIL_OBJECTS_IMPORT_CLASS;
}
/**
* Sets import for to string method.
*
* @return import string for implementing to string
*/
private static String setImportForToString() {
return UtilConstants.IMPORT + UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_PKG + UtilConstants.PERIOD
+ UtilConstants.GOOGLE_MORE_OBJECT_IMPORT_CLASS;
}
/**
* Sets import for to list.
*
* @return import string for list collection
*/
private static String setImportForList() {
return UtilConstants.IMPORT + UtilConstants.COLLECTION_IMPORTS + UtilConstants.PERIOD
+ UtilConstants.LIST + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
}
/**
* Construct java code exit.
*
* @param fileType generated file type
* @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
public static void generateJavaFile(int fileType, YangNode curNode) throws IOException {
JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
String className = JavaIdentifierSyntax.getCaptialCase(javaFileInfo.getJavaName());
String pkg = javaFileInfo.getPackageFilePath();
List<String> imports = getImports(((HasJavaImportData) curNode).getJavaImportData());
/**
* Start generation of files.
*/
if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0
| fileType == GENERATE_INTERFACE_WITH_BUILDER) {
/**
* Create interface file.
*/
String interfaceFileName = className;
File interfaceFile = getFileObject(pkg, interfaceFileName, JAVA_FILE_EXTENSION, javaFileInfo);
interfaceFile = generateInterfaceFile(interfaceFile, imports, curNode);
/**
* Create temp builder interface file.
*/
String builderInterfaceFileName = className
+ UtilConstants.BUILDER + UtilConstants.INTERFACE;
File builderInterfaceFile = getFileObject(pkg, builderInterfaceFileName, JAVA_FILE_EXTENSION, javaFileInfo);
builderInterfaceFile = generateBuilderInterfaceFile(builderInterfaceFile, curNode);
/**
* Append builder interface file to interface file and close it.
*/
appendFileContents(builderInterfaceFile, interfaceFile);
insert(interfaceFile, JavaCodeSnippetGen.getJavaClassDefClose());
/**
* Close file handle for interface files.
*/
closeFileHandles(builderInterfaceFile);
closeFileHandles(interfaceFile);
/**
* Remove temp files.
*/
clean(builderInterfaceFile);
}
imports.add(setImportForHashAndEquals());
imports.add(setImportForToString());
java.util.Collections.sort(imports);
if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0
| fileType == GENERATE_INTERFACE_WITH_BUILDER) {
/**
* Create builder class file.
*/
String builderFileName = className
+ UtilConstants.BUILDER;
File builderFile = getFileObject(pkg, builderFileName, JAVA_FILE_EXTENSION, javaFileInfo);
builderFile = generateBuilderClassFile(builderFile, imports, curNode);
/**
* Create temp impl class file.
*/
String implFileName = className + UtilConstants.IMPL;
File implTempFile = getFileObject(pkg, implFileName, JAVA_FILE_EXTENSION, javaFileInfo);
implTempFile = generateImplClassFile(implTempFile, curNode);
/**
* Append impl class to builder class and close it.
*/
appendFileContents(implTempFile, builderFile);
insert(builderFile, JavaCodeSnippetGen.getJavaClassDefClose());
/**
* Close file handle for classes files.
*/
closeFileHandles(implTempFile);
closeFileHandles(builderFile);
/**
* Remove temp files.
*/
clean(implTempFile);
}
/**
* if ((fileType & GENERATE_TYPEDEF_CLASS) != 0) {
*
* /** Create builder class file. //
*/
//String typeDefFileName = className;
//File typeDefFile = JavaFileGenerator.getFileObject(path, typeDefFileName, JAVA_FILE_EXTENSION,
// ((HasJavaFileInfo) curNode).getJavaFileInfo());
//typeDefFile = JavaFileGenerator.generateTypeDefClassFile(typeDefFile, className, imports,
// path.replace('/', '.'), attrList, ((HasJavaFileInfo) curNode).getJavaFileInfo());
// JavaFileGenerator.insert(typeDefFile, JavaCodeSnippetGen.getJavaClassDefClose());
// /**
// * Close file handle for classes files.
// */
// JavaFileGenerator.closeFileHandles(typeDefFile);
// }
//
}
/**
* Returns import for class.
*
* @param javaImportData import data
* @return imports for class
*/
private static List<String> getImports(JavaImportData javaImportData) {
String importString;
List<String> imports = new ArrayList<>();
for (JavaQualifiedTypeInfo importInfo : javaImportData.getImportSet()) {
importString = UtilConstants.IMPORT;
if (importInfo.getPkgInfo() != "" && importInfo.getClassInfo() != null
&& importInfo.getPkgInfo() != UtilConstants.JAVA_LANG) {
importString = importString + importInfo.getPkgInfo() + ".";
importString = importString + importInfo.getClassInfo() + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE;
imports.add(importString);
}
}
if (javaImportData.getIfListImported()) {
imports.add(setImportForList());
}
java.util.Collections.sort(imports);
return imports;
}
}
......@@ -17,7 +17,19 @@
package org.onosproject.yangutils.translator.tojava.utils;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
/**
* Utility class to generate the java snippet.
......@@ -52,8 +64,7 @@ public final class JavaCodeSnippetGen {
*/
public static String getImportText(JavaQualifiedTypeInfo importInfo) {
return UtilConstants.IMPORT + importInfo.getPkgInfo() + UtilConstants.PERIOD + importInfo.getClassInfo()
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
return IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN + NEW_LINE;
}
/**
......@@ -69,8 +80,8 @@ public final class JavaCodeSnippetGen {
/*
* get the camel case name for java class / interface.
*/
yangName = JavaIdentifierSyntax.getCamelCase(yangName);
return ClassDefinitionGenerator.generateClassDefinition(genFileTypes, yangName);
yangName = getCamelCase(yangName);
return generateClassDefinition(genFileTypes, yangName);
}
/**
......@@ -85,30 +96,23 @@ public final class JavaCodeSnippetGen {
public static String getJavaAttributeDefination(String javaAttributeTypePkg, String javaAttributeType,
String javaAttributeName, boolean isList) {
String attributeDefination = UtilConstants.PRIVATE
+ UtilConstants.SPACE;
String attributeDefination = PRIVATE + SPACE;
if (!isList) {
if (javaAttributeTypePkg != null) {
attributeDefination = attributeDefination
+ javaAttributeTypePkg + UtilConstants.PERIOD;
attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD;
}
attributeDefination = attributeDefination
+ javaAttributeType
+ UtilConstants.SPACE
+ javaAttributeName
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
attributeDefination = attributeDefination + javaAttributeType + SPACE + javaAttributeName + SEMI_COLAN
+ NEW_LINE;
} else {
attributeDefination = attributeDefination + UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET;
attributeDefination = attributeDefination + LIST + DIAMOND_OPEN_BRACKET;
if (javaAttributeTypePkg != null) {
attributeDefination = attributeDefination
+ javaAttributeTypePkg + UtilConstants.PERIOD;
attributeDefination = attributeDefination + javaAttributeTypePkg + PERIOD;
}
attributeDefination = attributeDefination
+ javaAttributeType + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE
+ javaAttributeName + UtilConstants.SUFIX_S + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
attributeDefination = attributeDefination + javaAttributeType + DIAMOND_CLOSE_BRACKET + SPACE
+ javaAttributeName + SEMI_COLAN + NEW_LINE;
}
return attributeDefination;
}
......@@ -121,7 +125,7 @@ public final class JavaCodeSnippetGen {
*/
public static String getListAttribute(String type) {
return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + type + UtilConstants.DIAMOND_CLOSE_BRACKET;
return LIST + DIAMOND_OPEN_BRACKET + type + DIAMOND_CLOSE_BRACKET;
}
/**
......@@ -132,7 +136,6 @@ public final class JavaCodeSnippetGen {
*/
public static String getJavaClassDefClose() {
return UtilConstants.CLOSE_CURLY_BRACKET;
return CLOSE_CURLY_BRACKET;
}
}
......
......@@ -16,14 +16,24 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.io.File;
import java.util.ArrayList;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.utils.UtilConstants.COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT_BASE_PKG;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_KEY_WORDS;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_FIRST_DIGIT;
import static org.onosproject.yangutils.utils.UtilConstants.REGEX_WITH_SPECIAL_CHAR;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.UNDER_SCORE;
/**
* Utility Class for translating the name from YANG to java convention.
......@@ -53,12 +63,12 @@ public final class JavaIdentifierSyntax {
public static String getRootPackage(byte version, String nameSpace, String revision) {
String pkg;
pkg = UtilConstants.DEFAULT_BASE_PKG;
pkg = pkg + UtilConstants.PERIOD;
pkg = DEFAULT_BASE_PKG;
pkg = pkg + PERIOD;
pkg = pkg + getYangVersion(version);
pkg = pkg + UtilConstants.PERIOD;
pkg = pkg + PERIOD;
pkg = pkg + getPkgFromNameSpace(nameSpace);
pkg = pkg + UtilConstants.PERIOD;
pkg = pkg + PERIOD;
pkg = pkg + getYangRevisionStr(revision);
return pkg.toLowerCase();
......@@ -99,7 +109,7 @@ public final class JavaIdentifierSyntax {
throw new RuntimeException("missing parent java node to get current node's package");
}
JavaFileInfo parentJavaFileHandle = ((HasJavaFileInfo) parentNode).getJavaFileInfo();
pkg = parentJavaFileHandle.getPackage() + UtilConstants.PERIOD + parentJavaFileHandle.getJavaName();
pkg = parentJavaFileHandle.getPackage() + PERIOD + parentJavaFileHandle.getJavaName();
return pkg.toLowerCase();
}
......@@ -123,9 +133,9 @@ public final class JavaIdentifierSyntax {
public static String getPkgFromNameSpace(String nameSpace) {
ArrayList<String> pkgArr = new ArrayList<String>();
nameSpace = nameSpace.replace(UtilConstants.QUOTES, UtilConstants.EMPTY_STRING);
String properNameSpace = nameSpace.replaceAll(UtilConstants.REGEX_WITH_SPECIAL_CHAR, UtilConstants.COLAN);
String[] nameSpaceArr = properNameSpace.split(UtilConstants.COLAN);
nameSpace = nameSpace.replace(QUOTES, EMPTY_STRING);
String properNameSpace = nameSpace.replaceAll(REGEX_WITH_SPECIAL_CHAR, COLAN);
String[] nameSpaceArr = properNameSpace.split(COLAN);
for (String nameSpaceString : nameSpaceArr) {
pkgArr.add(nameSpaceString);
......@@ -142,7 +152,7 @@ public final class JavaIdentifierSyntax {
*/
public static String getYangRevisionStr(String date) throws TranslatorException {
String[] revisionArr = date.split(UtilConstants.HYPHEN);
String[] revisionArr = date.split(HYPHEN);
String rev = "rev";
rev = rev + revisionArr[INDEX_ZERO];
......@@ -172,17 +182,17 @@ public final class JavaIdentifierSyntax {
*/
public static String getPkgFrmArr(ArrayList<String> pkgArr) {
String pkg = UtilConstants.EMPTY_STRING;
String pkg = EMPTY_STRING;
int size = pkgArr.size();
int i = 0;
for (String member : pkgArr) {
boolean presenceOfKeyword = UtilConstants.JAVA_KEY_WORDS.contains(member);
if (presenceOfKeyword || member.matches(UtilConstants.REGEX_FOR_FIRST_DIGIT)) {
member = UtilConstants.UNDER_SCORE + member;
boolean presenceOfKeyword = JAVA_KEY_WORDS.contains(member);
if (presenceOfKeyword || member.matches(REGEX_FOR_FIRST_DIGIT)) {
member = UNDER_SCORE + member;
}
pkg = pkg + member;
if (i != size - 1) {
pkg = pkg + UtilConstants.PERIOD;
pkg = pkg + PERIOD;
}
i++;
}
......@@ -198,7 +208,7 @@ public final class JavaIdentifierSyntax {
public static String getSubPkgFromName(String name) {
ArrayList<String> pkgArr = new ArrayList<String>();
String[] nameArr = name.split(UtilConstants.COLAN);
String[] nameArr = name.split(COLAN);
for (String nameString : nameArr) {
pkgArr.add(nameString);
......@@ -214,7 +224,7 @@ public final class JavaIdentifierSyntax {
*/
public static String getCamelCase(String yangIdentifier) {
String[] strArray = yangIdentifier.split(UtilConstants.HYPHEN);
String[] strArray = yangIdentifier.split(HYPHEN);
String camelCase = strArray[0];
for (int i = 1; i < strArray.length; i++) {
camelCase = camelCase + strArray[i].substring(0, 1).toUpperCase() + strArray[i].substring(1);
......@@ -254,7 +264,7 @@ public final class JavaIdentifierSyntax {
*/
public static String getJavaPackageFromPackagePath(String packagePath) {
return packagePath.replace(File.separator, UtilConstants.PERIOD);
return packagePath.replace(SLASH, PERIOD);
}
/**
......@@ -265,6 +275,6 @@ public final class JavaIdentifierSyntax {
*/
public static String getPackageDirPathFromJavaJPackage(String packagePath) {
return packagePath.replace(UtilConstants.PERIOD, File.separator);
return packagePath.replace(PERIOD, SLASH);
}
}
......
......@@ -83,7 +83,7 @@ public final class UtilConstants {
public static final String INSTANCE_OF = " instanceof ";
public static final String VALUE = "value";
public static final String SUFIX_S = "s";
public static final String SUFFIX_S = "s";
public static final String IF = "if";
public static final String FOR = "for";
......@@ -152,7 +152,7 @@ public final class UtilConstants {
/**
* String built in java type.
*/
public static final String STRING = "String";
public static final String STRING_DATA_TYPE = "String";
/**
* java.lang.* packages.
*/
......@@ -161,7 +161,7 @@ public final class UtilConstants {
/**
* boolean built in java type.
*/
public static final String BOOLEAN = "boolean";
public static final String BOOLEAN_DATA_TYPE = "boolean";
/**
* byte java built in type.
......@@ -229,8 +229,7 @@ public final class UtilConstants {
public static final String DOUBLE_WRAPPER = "Double";
/**
* List of keywords in java, this is used for checking if the input does not
* contain these keywords.
* List of keywords in java, this is used for checking if the input does not contain these keywords.
*/
public static final List JAVA_KEY_WORDS = Arrays.asList("abstract", "assert", "boolean", "break", "byte", "case",
"catch", "char", "class", "const", "continue", "default", "do", "double", "else", "extends", "false",
......
......@@ -24,12 +24,16 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
/**
* Provides the license header for the generated files.
*/
public final class CopyrightHeader {
private static final int EOF = -1;
private static final String COPYRIGHT_HEADER_FILE = "CopyrightHeader.txt";
private static final String TEMP_FILE = "temp.txt";
private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader();
private static String copyrightHeader;
......@@ -47,6 +51,7 @@ public final class CopyrightHeader {
* @throws IOException when fails to parse copyright header
*/
public static String getCopyrightHeader() throws IOException {
if (copyrightHeader == null) {
parseCopyrightHeader();
}
......@@ -59,6 +64,7 @@ public final class CopyrightHeader {
* @param header copyright header
*/
private static void setCopyrightHeader(String header) {
copyrightHeader = header;
}
......@@ -69,11 +75,12 @@ public final class CopyrightHeader {
*/
public static void parseCopyrightHeader() throws IOException {
File temp = new File("temp.txt");
File temp = new File(TEMP_FILE);
try {
InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt");
InputStream stream = classLoader.getResourceAsStream(COPYRIGHT_HEADER_FILE);
OutputStream out = new FileOutputStream(temp);
int index;
while ((index = stream.read()) != EOF) {
out.write(index);
......@@ -98,18 +105,20 @@ public final class CopyrightHeader {
*/
private static String getStringFileContent(File toAppend) throws IOException {
BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
FileReader fileReader = new FileReader(toAppend);
BufferedReader bufferReader = new BufferedReader(fileReader);
try {
StringBuilder stringBuilder = new StringBuilder();
String line = bufferReader.readLine();
while (line != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
stringBuilder.append(NEW_LINE);
line = bufferReader.readLine();
}
return stringBuilder.toString();
} finally {
fileReader.close();
bufferReader.close();
}
}
......
......@@ -23,7 +23,17 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.MULTIPLE_NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPathToPkg;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.convertPkgToPath;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
/**
* Utility to handle file system operations.
......@@ -44,8 +54,8 @@ public final class FileSystemUtil {
*/
public static boolean doesPackageExist(String pkg) {
File pkgDir = new File(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
File pkgWithFile = new File(pkgDir + File.separator + "package-info.java");
File pkgDir = new File(convertPkgToPath(pkg));
File pkgWithFile = new File(pkgDir + SLASH + "package-info.java");
if (pkgDir.exists() && pkgWithFile.isFile()) {
return true;
}
......@@ -63,8 +73,8 @@ public final class FileSystemUtil {
if (!doesPackageExist(pkg)) {
try {
File pack = YangIoUtils.createDirectories(pkg);
YangIoUtils.addPackageInfo(pack, pkgInfo, pkg.replace(UtilConstants.SLASH, UtilConstants.PERIOD));
File pack = createDirectories(pkg);
addPackageInfo(pack, pkgInfo, convertPathToPkg(pkg));
} catch (IOException e) {
throw new IOException("failed to create package-info file");
}
......@@ -83,9 +93,7 @@ public final class FileSystemUtil {
*/
public static void appendFileContents(File toAppend, File srcFile) throws IOException {
updateFileHandle(srcFile,
UtilConstants.NEW_LINE + readAppendFile(toAppend.toString(), UtilConstants.FOUR_SPACE_INDENTATION),
false);
updateFileHandle(srcFile, NEW_LINE + readAppendFile(toAppend.toString(), FOUR_SPACE_INDENTATION), false);
return;
}
......@@ -106,15 +114,14 @@ public final class FileSystemUtil {
String line = bufferReader.readLine();
while (line != null) {
if (line.equals(UtilConstants.SPACE) | line.equals(UtilConstants.EMPTY_STRING)
| line.equals(UtilConstants.EIGHT_SPACE_INDENTATION)
| line.equals(UtilConstants.MULTIPLE_NEW_LINE)) {
stringBuilder.append(UtilConstants.NEW_LINE);
} else if (line.equals(UtilConstants.FOUR_SPACE_INDENTATION)) {
stringBuilder.append(UtilConstants.EMPTY_STRING);
if (line.equals(SPACE) | line.equals(EMPTY_STRING) | line.equals(EIGHT_SPACE_INDENTATION)
| line.equals(MULTIPLE_NEW_LINE)) {
stringBuilder.append(NEW_LINE);
} else if (line.equals(FOUR_SPACE_INDENTATION)) {
stringBuilder.append(EMPTY_STRING);
} else {
stringBuilder.append(spaces + line);
stringBuilder.append(UtilConstants.NEW_LINE);
stringBuilder.append(NEW_LINE);
}
line = bufferReader.readLine();
}
......@@ -131,9 +138,8 @@ public final class FileSystemUtil {
* @param inputFile input file
* @param contentTobeAdded content to be appended to the file
* @param isClose when close of file is called.
* @throws IOException if the named file exists but is a directory rather
* than a regular file, does not exist but cannot be created, or
* cannot be opened for any other reason
* @throws IOException if the named file exists but is a directory rather than a regular file,
* does not exist but cannot be created, or cannot be opened for any other reason
*/
public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose) throws IOException {
......
......@@ -23,10 +23,13 @@ import java.util.List;
import java.util.Stack;
/**
* Provides the IO services for Yangutils-maven-Plugin.
* Provides utility for searching the files in a directory.
*/
public final class YangFileScanner {
private static final String JAVA_FILE_EXTENTION = ".java";
private static final String YANG_FILE_EXTENTION = ".yang";
/**
* Default constructor.
*/
......@@ -44,7 +47,7 @@ public final class YangFileScanner {
*/
public static List<String> getJavaFiles(String root) throws NullPointerException, IOException {
return getFiles(root, ".java");
return getFiles(root, JAVA_FILE_EXTENTION);
}
/**
......@@ -58,7 +61,7 @@ public final class YangFileScanner {
*/
public static List<String> getYangFiles(String root) throws NullPointerException, IOException {
return getFiles(root, ".yang");
return getFiles(root, YANG_FILE_EXTENTION);
}
/**
......@@ -68,8 +71,7 @@ public final class YangFileScanner {
* @param extension file extension
* @return list of required files
* @throws NullPointerException when no file is there
* @throws IOException when files get deleted while performing the
* operations
* @throws IOException when files get deleted while performing the operations
*/
public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException {
......
......@@ -25,13 +25,27 @@ import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Resource;
import org.apache.maven.project.MavenProject;
import org.onosproject.yangutils.utils.UtilConstants;
import org.slf4j.Logger;
import org.sonatype.plexus.build.incremental.BuildContext;
import static org.apache.commons.io.FileUtils.deleteDirectory;
import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.ORG;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
import static org.slf4j.LoggerFactory.getLogger;
/**
......@@ -40,8 +54,7 @@ import static org.slf4j.LoggerFactory.getLogger;
public final class YangIoUtils {
private static final Logger log = getLogger(YangIoUtils.class);
private static final String TARGET_RESOURCE_PATH = UtilConstants.SLASH + UtilConstants.TEMP + UtilConstants.SLASH
+ UtilConstants.YANG_RESOURCES + UtilConstants.SLASH;
private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
/**
* Default constructor.
......@@ -72,22 +85,24 @@ public final class YangIoUtils {
*/
public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
if (pack.contains(UtilConstants.ORG)) {
String[] strArray = pack.split(UtilConstants.ORG);
pack = UtilConstants.ORG + strArray[1];
if (pack.contains(ORG)) {
String[] strArray = pack.split(ORG);
pack = ORG + strArray[1];
}
try {
File packageInfo = new File(path + File.separator + "package-info.java");
File packageInfo = new File(path + SLASH + "package-info.java");
packageInfo.createNewFile();
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
fileWriter = new FileWriter(packageInfo);
bufferedWriter = new BufferedWriter(fileWriter);
FileWriter fileWriter = new FileWriter(packageInfo);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo, false));
bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
bufferedWriter.write(JavaDocGen.getJavaDoc(PACKAGE_INFO, classInfo, false));
bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
bufferedWriter.close();
fileWriter.close();
} catch (IOException e) {
throw new IOException("Exception occured while creating package info file.");
}
......@@ -97,15 +112,16 @@ public final class YangIoUtils {
* Cleans the generated directory if already exist in source folder.
*
* @param dir generated directory in previous build
* @throws IOException when failed to delete directory
*/
public static void clean(String dir) {
public static void clean(String dir) throws IOException {
File generatedDirectory = new File(dir);
if (generatedDirectory.exists()) {
try {
FileUtils.deleteDirectory(generatedDirectory);
deleteDirectory(generatedDirectory);
} catch (IOException e) {
log.info("Failed to delete the generated files in " + generatedDirectory + " directory");
throw new IOException("Failed to delete the generated files in " + generatedDirectory + " directory");
}
}
}
......@@ -147,18 +163,17 @@ public final class YangIoUtils {
*/
public static String partString(String partString) {
String[] strArray = partString.split(UtilConstants.COMMA);
String newString = "";
String[] strArray = partString.split(COMMA);
String newString = EMPTY_STRING;
for (int i = 0; i < strArray.length; i++) {
if (i % 4 != 0 || i == 0) {
newString = newString + strArray[i] + UtilConstants.COMMA;
newString = newString + strArray[i] + COMMA;
} else {
newString = newString + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION
+ strArray[i]
+ UtilConstants.COMMA;
newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
+ strArray[i] + COMMA;
}
}
return trimAtLast(newString, UtilConstants.COMMA);
return trimAtLast(newString, COMMA);
}
/**
......@@ -183,13 +198,13 @@ public final class YangIoUtils {
public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
pathOfJavaPkg = trimAtLast(pathOfJavaPkg, UtilConstants.SLASH);
pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
}
String[] strArray = pathOfJavaPkg.split(UtilConstants.SLASH);
if (strArray[0].equals(UtilConstants.EMPTY_STRING)) {
String[] strArray = pathOfJavaPkg.split(SLASH);
if (strArray[0].equals(EMPTY_STRING)) {
return pathOfJavaPkg;
} else {
return baseCodeGenPath + File.separator + pathOfJavaPkg;
return baseCodeGenPath + SLASH + pathOfJavaPkg;
}
}
......@@ -212,8 +227,7 @@ public final class YangIoUtils {
* @param yangFiles list of YANG files
* @param outputDir project's output directory
* @param project maven project
* @throws IOException when fails to copy files to destination resource
* directory
* @throws IOException when fails to copy files to destination resource directory
*/
public static void copyYangFilesToTarget(List<String> yangFiles, String outputDir, MavenProject project)
throws IOException {
......@@ -226,11 +240,11 @@ public final class YangIoUtils {
for (File file : files) {
Files.copy(file.toPath(),
new File(path + file.getName()).toPath(),
(new File(path + file.getName())).toPath(),
StandardCopyOption.REPLACE_EXISTING);
}
Resource rsc = new Resource();
rsc.setDirectory(outputDir + UtilConstants.SLASH + UtilConstants.TEMP + UtilConstants.SLASH);
rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
project.addResource(rsc);
}
......@@ -248,4 +262,58 @@ public final class YangIoUtils {
}
return files;
}
/**
* Merge the temp java files to main java files.
*
* @param appendFile temp file
* @param srcFile main file
* @throws IOException when fails to append contents
*/
public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
try {
appendFileContents(appendFile, srcFile);
} catch (IOException e) {
throw new IOException("Failed to append " + appendFile + " in " + srcFile);
}
}
/**
* Insert data in the generated file.
*
* @param file file in which need to be inserted
* @param data data which need to be inserted
* @throws IOException when fails to insert into file
*/
public static void insertDataIntoJavaFile(File file, String data) throws IOException {
try {
updateFileHandle(file, data, false);
} catch (IOException e) {
throw new IOException("Failed to insert in " + file + "file");
}
}
/**
* Convert directory path in java package format.
*
* @param path directory path
* @return java package
*/
public static String convertPathToPkg(String path) {
return path.replace(SLASH, PERIOD);
}
/**
* Convert java package in directory path format.
*
* @param pkg java package
* @return directory path
*/
public static String convertPkgToPath(String pkg) {
return pkg.replace(PERIOD, SLASH);
}
}
......
......@@ -16,18 +16,36 @@
package org.onosproject.yangutils.translator.tojava.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType;
import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getImportText;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefStart;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getListAttribute;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.IMPORT;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
import static org.onosproject.yangutils.utils.UtilConstants.LIST;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
/**
* Unit test cases for java code snippet generator.
......@@ -36,10 +54,8 @@ public class JavaCodeSnippetGenTest {
private static final String PKG_INFO = "org.onosproject.unittest";
private static final String CLASS_INFO = "JavaCodeSnippetGenTest";
private static final int FILE_GEN_TYPE = GeneratedJavaFileType.INTERFACE_MASK;
private static final GeneratedMethodTypes METHOD_GEN_TYPE = GeneratedMethodTypes.GETTER;
private static final int FILE_GEN_TYPE = INTERFACE_MASK;
private static final String YANG_NAME = "Test";
private static final String STRING = "String";
/**
* Unit test for private constructor.
......@@ -54,6 +70,7 @@ public class JavaCodeSnippetGenTest {
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {JavaCodeSnippetGen.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
......@@ -67,14 +84,14 @@ public class JavaCodeSnippetGenTest {
*/
@Test
public void testForImportText() {
JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
importInfo.setPkgInfo(PKG_INFO);
importInfo.setClassInfo(CLASS_INFO);
String imports = JavaCodeSnippetGen.getImportText(importInfo);
String imports = getImportText(importInfo);
assertThat(true, is(imports.equals(UtilConstants.IMPORT + PKG_INFO + UtilConstants.PERIOD + CLASS_INFO
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
assertThat(true, is(imports.equals(IMPORT + PKG_INFO + PERIOD + CLASS_INFO + SEMI_COLAN + NEW_LINE)));
}
/**
......@@ -82,11 +99,10 @@ public class JavaCodeSnippetGenTest {
*/
@Test
public void testForJavaClassDefStart() {
String classDef = JavaCodeSnippetGen.getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME);
assertThat(true,
is(classDef.equals(UtilConstants.PUBLIC + UtilConstants.SPACE + UtilConstants.INTERFACE
+ UtilConstants.SPACE + YANG_NAME + UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET
+ UtilConstants.NEW_LINE)));
String classDef = getJavaClassDefStart(FILE_GEN_TYPE, YANG_NAME);
assertThat(true, is(classDef
.equals(PUBLIC + SPACE + INTERFACE + SPACE + YANG_NAME + SPACE + OPEN_CURLY_BRACKET + NEW_LINE)));
}
......@@ -95,36 +111,20 @@ public class JavaCodeSnippetGenTest {
*/
@Test
public void testForListAttribute() {
String listAttribute = JavaCodeSnippetGen.getListAttribute(STRING);
assertThat(true, is(listAttribute.equals(UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + STRING
+ UtilConstants.DIAMOND_CLOSE_BRACKET)));
}
/**
* Unit test case for java class interface definition close.
*/
@Test
public void testForJavaClassDefInterfaceClose() {
String interfaceDef = JavaCodeSnippetGen.getJavaClassDefClose();
assertThat(true, is(interfaceDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
String listAttribute = getListAttribute(STRING_DATA_TYPE);
assertThat(true,
is(listAttribute.equals(LIST + DIAMOND_OPEN_BRACKET + STRING_DATA_TYPE + DIAMOND_CLOSE_BRACKET)));
}
/**
* Unit test case for java class builder class definition close.
* Unit test case for java class interface definition close.
*/
@Test
public void testForJavaClassDefBuilderClassClose() {
String builderClassDef = JavaCodeSnippetGen.getJavaClassDefClose();
assertThat(true, is(builderClassDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
}
public void testForJavaClassDefClose() {
/**
* Unit test case for java class typedef definition close.
*/
@Test
public void testForJavaClassDefTypeDefClose() {
String typeDef = JavaCodeSnippetGen.getJavaClassDefClose();
assertThat(true, is(typeDef.equals(UtilConstants.CLOSE_CURLY_BRACKET)));
String interfaceDef = getJavaClassDefClose();
assertThat(true, is(interfaceDef.equals(CLOSE_CURLY_BRACKET)));
}
/**
......@@ -133,30 +133,22 @@ public class JavaCodeSnippetGenTest {
@Test
public void testForJavaAttributeInfo() {
String attributeWithoutTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, UtilConstants.STRING,
YANG_NAME, false);
assertThat(true,
is(attributeWithoutTypePkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.STRING
+ UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
String attributeWithTypePkg = JavaCodeSnippetGen.getJavaAttributeDefination(
UtilConstants.JAVA_LANG, UtilConstants.STRING, YANG_NAME, false);
assertThat(true, is(attributeWithTypePkg
.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.JAVA_LANG + UtilConstants.PERIOD
+ UtilConstants.STRING + UtilConstants.SPACE + YANG_NAME + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE)));
String attributeWithListPkg = JavaCodeSnippetGen.getJavaAttributeDefination(
UtilConstants.JAVA_LANG, UtilConstants.STRING, YANG_NAME, true);
assertThat(true,
is(attributeWithListPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
+ UtilConstants.DIAMOND_OPEN_BRACKET + UtilConstants.JAVA_LANG + UtilConstants.PERIOD
+ UtilConstants.STRING + UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME
+ UtilConstants.SUFIX_S + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
String attributeWithListWithoutPkg = JavaCodeSnippetGen.getJavaAttributeDefination(null, UtilConstants.STRING,
YANG_NAME, true);
assertThat(true,
is(attributeWithListWithoutPkg.equals(UtilConstants.PRIVATE + UtilConstants.SPACE + UtilConstants.LIST
+ UtilConstants.DIAMOND_OPEN_BRACKET + UtilConstants.STRING
+ UtilConstants.DIAMOND_CLOSE_BRACKET + UtilConstants.SPACE + YANG_NAME + UtilConstants.SUFIX_S
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE)));
String attributeWithoutTypePkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, false);
assertThat(true, is(attributeWithoutTypePkg.equals(
PRIVATE + SPACE + STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
String attributeWithTypePkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, false);
assertThat(true, is(attributeWithTypePkg.equals(PRIVATE + SPACE + JAVA_LANG + PERIOD
+ STRING_DATA_TYPE + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
String attributeWithListPkg = getJavaAttributeDefination(JAVA_LANG, STRING_DATA_TYPE, YANG_NAME, true);
assertThat(true, is(attributeWithListPkg.equals(
PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + JAVA_LANG + PERIOD + STRING_DATA_TYPE
+ DIAMOND_CLOSE_BRACKET + SPACE + YANG_NAME + SEMI_COLAN + NEW_LINE)));
String attributeWithListWithoutPkg = getJavaAttributeDefination(null, STRING_DATA_TYPE, YANG_NAME, true);
assertThat(true, is(attributeWithListWithoutPkg.equals(
PRIVATE + SPACE + LIST + DIAMOND_OPEN_BRACKET + STRING_DATA_TYPE + DIAMOND_CLOSE_BRACKET + SPACE
+ YANG_NAME + SEMI_COLAN + NEW_LINE)));
}
}
......
/*
* Copyright 2016 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator.tojava.utils;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
/**
* Unit tests for generated methods from the file type.
*/
public final class MethodsGeneratorTest {
public static JavaAttributeInfo testAttr;
public static YangType<?> attrType = new YangType<>();
/**
* Unit test for private constructor.
*
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the
* private constructor
* @throws IllegalAccessException if instance is provoked or a method is
* provoked
* @throws InvocationTargetException when an exception occurs by the method
* or constructor
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {
MethodsGenerator.class
};
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
assertNotNull(constructor.newInstance());
}
}
// /**
// * Unit test case for checking the parse builder and typedef constructor.
// */
// @Test
// public void getParseBuilderInterfaceMethodConstructorTest() {
//
// JavaQualifiedTypeInfo forSetter = new JavaQualifiedTypeInfo();
// attrType.setDataTypeName("binary");
// attrType.getDataTypeName();
// attrType.setDataType(YangDataTypes.BINARY);
// attrType.getDataType();
// testAttr.setAttributeName("attributeTest");
// testAttr.setAttributeType(attrType);
// forSetter.setPkgInfo("test1/test3");
// forSetter.setClassInfo("This class contains");
// testAttr.setImportInfo(forSetter);
// String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname");
// }
/**
* Unit test case for checking the values received from constructor, default
* constructor and build string formation.
*/
@Test
public void getValuesTest() {
String stringConstructor = MethodsGenerator.getConstructorString("testname");
assertThat(stringConstructor.contains(UtilConstants.JAVA_DOC_CONSTRUCTOR)
&& stringConstructor.contains(UtilConstants.JAVA_DOC_PARAM)
&& stringConstructor.contains(UtilConstants.BUILDER_OBJECT), is(true));
String stringDefaultConstructor = MethodsGenerator.getDefaultConstructorString("testnameBuilder", "public");
assertThat(stringDefaultConstructor.contains(UtilConstants.JAVA_DOC_DEFAULT_CONSTRUCTOR)
&& stringDefaultConstructor.contains(UtilConstants.BUILDER)
&& stringDefaultConstructor.contains("testname"), is(true));
String stringBuild = MethodsGenerator.getBuildString("testname");
assertThat(stringBuild.contains(UtilConstants.OVERRIDE) && stringBuild.contains(UtilConstants.BUILD)
&& stringBuild.contains(UtilConstants.RETURN), is(true));
}
/**
* Unit test for checking the values received for class getter, class and
* typedef setters with list data type.
*/
// @Test
// public void getGetterSetterTest() {
//
// JavaQualifiedTypeInfo forGetterSetter = new JavaQualifiedTypeInfo();
// attrType.setDataTypeName("int");
// attrType.getDataTypeName();
// attrType.setDataType(YangDataTypes.UINT8);
// attrType.getDataType();
// testAttr.setAttributeName("AttributeTest1");
// testAttr.setAttributeType(attrType);
// forGetterSetter.setPkgInfo("null");
// forGetterSetter.setClassInfo("This class contains");
// testAttr.setImportInfo(forGetterSetter);
// testAttr.setListAttr(true);
// String getterForClass = MethodsGenerator.getGetterForClass(testAttr);
// assertThat(getterForClass.contains(UtilConstants.GET_METHOD_PREFIX) && getterForClass.contains("List<")
// && getterForClass.contains("attributeTest1"), is(true));
// String setterForClass = MethodsGenerator.getSetterForClass(testAttr, "TestThis");
// assertThat(setterForClass.contains(UtilConstants.SET_METHOD_PREFIX) && setterForClass.contains("List<")
// && setterForClass.contains("attributeTest1"), is(true));
// String typeDefSetter = MethodsGenerator.getSetterForTypeDefClass(testAttr);
// assertThat(typeDefSetter.contains(UtilConstants.SET_METHOD_PREFIX) && typeDefSetter.contains("List<")
// && typeDefSetter.contains("attributeTest1") && typeDefSetter.contains("this."), is(true));
// }
/**
* Unit test case for checking the parse builder and typedef constructor
* with list data type.
*/
// @Test
// public void getConstructorWithListTypeTest() {
//
// JavaQualifiedTypeInfo forSetter = new JavaQualifiedTypeInfo();
// attrType.setDataTypeName("binary");
// attrType.getDataTypeName();
// attrType.setDataType(YangDataTypes.BINARY);
// attrType.getDataType();
// testAttr.setAttributeName("attributeTest");
// testAttr.setAttributeType(attrType);
// forSetter.setPkgInfo("null");
// forSetter.setClassInfo("This class contains");
// testAttr.setImportInfo(forSetter);
// testAttr.setListAttr(true);
// String stringTypeDef = MethodsGenerator.getTypeDefConstructor(testAttr, "Testname");
// assertThat(stringTypeDef.contains("(List<") && stringTypeDef.contains("Testname")
// && stringTypeDef.contains(UtilConstants.THIS), is(true));
// }
}
......@@ -21,9 +21,7 @@ import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.onosproject.yangutils.utils.UtilConstants;
import static org.junit.Assert.assertNotNull;
......@@ -34,12 +32,12 @@ import static org.junit.Assert.assertTrue;
*/
public final class FileSystemUtilTest {
public static final String BASE_DIR_PKG = "target.UnitTestCase.";
public static final String PKG_INFO_CONTENT = "testGeneration6";
public static final String BASE_PKG = "target/UnitTestCase";
@Rule
public ExpectedException thrown = ExpectedException.none();
private static final String BASE_DIR_PKG = "target.UnitTestCase.";
private static final String PKG_INFO_CONTENT = "testGeneration6";
private static final String BASE_PKG = "target/UnitTestCase";
private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
private static final String TEST_DATA_2 = "This is next second line\n";
private static final String TEST_DATA_3 = "This is next third line in the file";
/**
* A private constructor is tested.
......@@ -47,20 +45,15 @@ public final class FileSystemUtilTest {
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the
* private constructor
* @throws IllegalAccessException if instance is provoked or a method is
* provoked
* @throws InvocationTargetException when an exception occurs by the method
* or constructor
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
public void callPrivateConstructors() throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {
FileSystemUtil.class
};
Class<?>[] classesToConstruct = {FileSystemUtil.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
......@@ -70,6 +63,8 @@ public final class FileSystemUtilTest {
/**
* This test case checks the contents to be written in the file.
*
* @throws IOException when fails to create a test file
*/
@Test
public void updateFileHandleTest() throws IOException {
......@@ -80,15 +75,17 @@ public final class FileSystemUtilTest {
createFile.createNewFile();
File createSourceFile = new File(dir + "sourceTestFile");
createSourceFile.createNewFile();
FileSystemUtil.updateFileHandle(createFile, "This is to append a text to the file first1\n", false);
FileSystemUtil.updateFileHandle(createFile, "This is next second line\n", false);
FileSystemUtil.updateFileHandle(createFile, "This is next third line in the file", false);
FileSystemUtil.updateFileHandle(createFile, TEST_DATA_1, false);
FileSystemUtil.updateFileHandle(createFile, TEST_DATA_2, false);
FileSystemUtil.updateFileHandle(createFile, TEST_DATA_3, false);
FileSystemUtil.appendFileContents(createFile, createSourceFile);
FileSystemUtil.updateFileHandle(createFile, null, true);
}
/**
* This test case checks whether the package is existing.
*
* @throws IOException when failed to create a test file
*/
@Test
public void packageExistTest() throws IOException {
......@@ -104,21 +101,4 @@ public final class FileSystemUtilTest {
createDir.delete();
}
/**
* This test case checks the package does not exist.
*/
// @Test
// public void packageNotExistTest() throws IOException {
// String dirPath = "notexist1.notexist2";
// String strPath = BASE_DIR_PKG + dirPath;
// File createDir = new File(strPath.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
// assertFalse(FileSystemUtil.doesPackageExist(strPath));
// createDir.mkdirs();
// assertFalse(FileSystemUtil.doesPackageExist(strPath));
// CopyrightHeader.parseCopyrightHeader();
// FileSystemUtil.createPackage(strPath, PKG_INFO_CONTENT);
// assertTrue(FileSystemUtil.doesPackageExist(strPath));
// createDir.delete();
// }
}
......
......@@ -62,7 +62,7 @@ public final class JavaDocGenTest {
@Test
public void buildGenerationTest() {
String buildDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILD, "testGeneration1", false);
String buildDoc = JavaDocGen.getJavaDoc(JavaDocType.BUILD_METHOD, "testGeneration1", false);
assertTrue(buildDoc.contains("Builds object of") && buildDoc.contains(" */\n"));
}
......@@ -117,7 +117,7 @@ public final class JavaDocGenTest {
@Test
public void getterGenerationTest() {
String getterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.GETTER, "testGeneration1", false);
String getterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.GETTER_METHOD, "testGeneration1", false);
assertTrue(getterJavaDoc.contains("Returns the attribute") && getterJavaDoc.contains(" */\n"));
}
......@@ -157,7 +157,7 @@ public final class JavaDocGenTest {
@Test
public void setterGenerationTest() {
String setterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.SETTER, "testGeneration1", false);
String setterJavaDoc = JavaDocGen.getJavaDoc(JavaDocType.SETTER_METHOD, "testGeneration1", false);
assertTrue(setterJavaDoc.contains("Returns the builder object of") && setterJavaDoc.contains(" */\n"));
}
......@@ -167,7 +167,7 @@ public final class JavaDocGenTest {
@Test
public void typeDefSetterGenerationTest() {
String typeDefSetter = JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER, "testGeneration1", false);
String typeDefSetter = JavaDocGen.getJavaDoc(JavaDocType.TYPE_DEF_SETTER_METHOD, "testGeneration1", false);
assertTrue(typeDefSetter.contains("Sets the value of") && typeDefSetter.contains(" */\n"));
}
}
\ No newline at end of file
......