Bharat saraswal
Committed by Gerrit Code Review

[ONOS-3908] Implemetation of YANG container translator.

Change-Id: I9ffcfc4b370edb801dfc90c5394cef787c77641d
Showing 23 changed files with 431 additions and 147 deletions
......@@ -21,6 +21,7 @@ import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* Reference RFC 6020.
......@@ -342,4 +343,16 @@ public class YangAugment extends YangNode implements YangLeavesHolder, YangCommo
// TODO Auto-generated method stub
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -21,6 +21,7 @@ import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* Reference RFC 6020.
......@@ -336,4 +337,16 @@ public class YangCase extends YangNode implements YangLeavesHolder, YangCommonIn
// TODO Auto-generated method stub
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -21,6 +21,7 @@ import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* Reference RFC 6020.
......@@ -252,6 +253,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
*
* @return the description.
*/
@Override
public String getDescription() {
return description;
}
......@@ -261,6 +263,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
*
* @param description set the description.
*/
@Override
public void setDescription(String description) {
this.description = description;
}
......@@ -270,6 +273,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
*
* @return the reference.
*/
@Override
public String getReference() {
return reference;
}
......@@ -279,6 +283,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
*
* @param reference the reference to set.
*/
@Override
public void setReference(String reference) {
this.reference = reference;
}
......@@ -288,6 +293,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
*
* @return the status.
*/
@Override
public YangStatusType getStatus() {
return status;
}
......@@ -297,6 +303,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
*
* @param status the status to set.
*/
@Override
public void setStatus(YangStatusType status) {
this.status = status;
}
......@@ -306,6 +313,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
*
* @return returns CHOICE_DATA
*/
@Override
public ParsableDataType getParsableDataType() {
return ParsableDataType.CHOICE_DATA;
}
......@@ -315,6 +323,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
*
* @throws DataModelException a violation of data model rules.
*/
@Override
public void validateDataOnEntry() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
......@@ -324,6 +333,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
*
* @throws DataModelException a violation of data model rules.
*/
@Override
public void validateDataOnExit() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
......@@ -349,6 +359,7 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeEntry()
*/
@Override
public void generateJavaCodeEntry() {
// TODO Auto-generated method stub
......@@ -357,8 +368,21 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable {
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeExit()
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -133,7 +133,7 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
private YangStatusType status;
/**
* package of the generated java code.
* Package of the generated java code.
*/
private String pkg;
......@@ -332,6 +332,7 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
*
* @return the fileHandle
*/
@Override
public CachedFileHandle getFileHandle() {
return fileHandle;
}
......@@ -341,6 +342,7 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
*
* @param handle the fileHandle to set
*/
@Override
public void setFileHandle(CachedFileHandle handle) {
fileHandle = handle;
}
......@@ -395,11 +397,6 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
pkg = pcg;
}
/**
* Generate the java code corresponding to YANG container.
*
* @throws IOException when fails to generate the source files.
*/
@Override
public void generateJavaCodeEntry() throws IOException {
YangNode parent = getParent();
......@@ -414,14 +411,33 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
addLavesAttributes();
addAttributeInParent();
}
/**
* Adds current node attribute to parent file.
*
* @param pkg java file package path
*/
private void addAttributeInParent() {
if (getParent() != null) {
getParent().getFileHandle().setChildsPackage(getPackage());
getParent().getFileHandle().addAttributeInfo(null, getName(), false);
}
}
@Override
public void generateJavaCodeExit() throws IOException {
addLeavesAttributes();
addLeafListAttributes();
getFileHandle().close();
return;
}
/**
* Adds leaf attributes in generated files.
*/
private void addLavesAttributes() {
private void addLeavesAttributes() {
List<YangLeaf<?>> leaves = getListOfLeaf();
if (leaves != null) {
......@@ -442,17 +458,5 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
}
}
return;
}
/**
* Free resources used to generate code.
*
* @throws IOException when fails to generate source files.
*/
@Override
public void generateJavaCodeExit() throws IOException {
getFileHandle().close();
return;
}
}
......
......@@ -22,6 +22,7 @@ import java.util.Set;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*
* The enumeration built-in type represents values from a set of
......@@ -79,6 +80,7 @@ public class YangEnumeration extends YangNode implements Parsable {
*
* @return returns ENUMERATION_DATA
*/
@Override
public ParsableDataType getParsableDataType() {
return ParsableDataType.ENUMERATION_DATA;
}
......@@ -88,6 +90,7 @@ public class YangEnumeration extends YangNode implements Parsable {
*
* @throws DataModelException a violation of data model rules.
*/
@Override
public void validateDataOnEntry() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
......@@ -97,6 +100,7 @@ public class YangEnumeration extends YangNode implements Parsable {
*
* @throws DataModelException a violation of data model rules.
*/
@Override
public void validateDataOnExit() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
......@@ -140,6 +144,7 @@ public class YangEnumeration extends YangNode implements Parsable {
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeEntry()
*/
@Override
public void generateJavaCodeEntry() {
// TODO Auto-generated method stub
......@@ -148,8 +153,21 @@ public class YangEnumeration extends YangNode implements Parsable {
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeExit()
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -21,6 +21,7 @@ import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* Reference RFC 6020.
......@@ -321,4 +322,16 @@ public class YangGrouping extends YangNode implements YangLeavesHolder, YangComm
// TODO Auto-generated method stub
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -22,6 +22,7 @@ import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* The "list" statement is used to define an interior data node in the
......@@ -459,4 +460,16 @@ public class YangList extends YangNode implements YangLeavesHolder, YangCommonIn
// TODO Auto-generated method stub
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -155,7 +155,7 @@ public class YangModule extends YangNode implements YangLeavesHolder, YangDesc,
private byte version;
/**
* package of the generated java code.
* Package of the generated java code.
*/
private String pkg;
......@@ -493,6 +493,7 @@ public class YangModule extends YangNode implements YangLeavesHolder, YangDesc,
*
* @return the fileHandle
*/
@Override
public CachedFileHandle getFileHandle() {
return fileHandle;
}
......@@ -502,6 +503,7 @@ public class YangModule extends YangNode implements YangLeavesHolder, YangDesc,
*
* @param handle the fileHandle to set
*/
@Override
public void setFileHandle(CachedFileHandle handle) {
fileHandle = handle;
}
......@@ -555,14 +557,20 @@ public class YangModule extends YangNode implements YangLeavesHolder, YangDesc,
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
addLavesAttributes();
}
@Override
public void generateJavaCodeExit() throws IOException {
addLeavesAttributes();
addLeafListAttributes();
getFileHandle().close();
return;
}
/**
* Adds leaf attributes in generated files.
*/
private void addLavesAttributes() {
private void addLeavesAttributes() {
List<YangLeaf<?>> leaves = getListOfLeaf();
if (leaves != null) {
......@@ -584,15 +592,4 @@ public class YangModule extends YangNode implements YangLeavesHolder, YangDesc,
}
}
/**
* Free resources used to generate code.
*
* @throws IOException when fails to generate source files.
*/
@Override
public void generateJavaCodeExit() throws IOException {
getFileHandle().close();
return;
}
}
......
......@@ -16,6 +16,7 @@
package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.CodeGenerator;
/**
......@@ -253,4 +254,18 @@ public abstract class YangNode implements CodeGenerator {
*/
public abstract void setPackage(String pkg);
/**
* Get the mapped java file handler.
*
* @return the file handle.
*/
public abstract CachedFileHandle getFileHandle();
/**
* Set the mapped java file handle.
*
* @param fileHandle the file handle to set of current node.
*/
public abstract void setFileHandle(CachedFileHandle fileHandle);
}
......
......@@ -21,6 +21,7 @@ import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*
* Reference RFC 6020.
......@@ -491,4 +492,16 @@ public class YangSubModule extends YangNode implements YangLeavesHolder, YangDes
// TODO Auto-generated method stub
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -18,6 +18,7 @@ package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* Reference RFC 6020.
......@@ -138,6 +139,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*
* @return the description.
*/
@Override
public String getDescription() {
return description;
}
......@@ -147,6 +149,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*
* @param description set the description.
*/
@Override
public void setDescription(String description) {
this.description = description;
}
......@@ -156,6 +159,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*
* @return the reference.
*/
@Override
public String getReference() {
return reference;
}
......@@ -165,6 +169,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*
* @param reference the reference to set.
*/
@Override
public void setReference(String reference) {
this.reference = reference;
}
......@@ -174,6 +179,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*
* @return the status.
*/
@Override
public YangStatusType getStatus() {
return status;
}
......@@ -183,6 +189,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*
* @param status the status to set.
*/
@Override
public void setStatus(YangStatusType status) {
this.status = status;
}
......@@ -230,6 +237,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*
* @return returns TYPEDEF_DATA
*/
@Override
public ParsableDataType getParsableDataType() {
return ParsableDataType.TYPEDEF_DATA;
}
......@@ -239,6 +247,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*
* @throws DataModelException a violation of data model rules.
*/
@Override
public void validateDataOnEntry() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
......@@ -248,6 +257,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
*
* @throws DataModelException a violation of data model rules.
*/
@Override
public void validateDataOnExit() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
......@@ -273,6 +283,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeEntry()
*/
@Override
public void generateJavaCodeEntry() {
// TODO Auto-generated method stub
......@@ -281,6 +292,7 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeExit()
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
......@@ -303,4 +315,16 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
// TODO Auto-generated method stub
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -18,6 +18,7 @@ package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.ParsableDataType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* Reference RFC 6020.
......@@ -127,6 +128,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
*
* @return the description.
*/
@Override
public String getDescription() {
return description;
}
......@@ -136,6 +138,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
*
* @param description set the description.
*/
@Override
public void setDescription(String description) {
this.description = description;
}
......@@ -145,6 +148,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
*
* @return the reference.
*/
@Override
public String getReference() {
return reference;
}
......@@ -154,6 +158,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
*
* @param reference the reference to set.
*/
@Override
public void setReference(String reference) {
this.reference = reference;
}
......@@ -163,6 +168,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
*
* @return the status.
*/
@Override
public YangStatusType getStatus() {
return status;
}
......@@ -172,6 +178,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
*
* @param status the status to set.
*/
@Override
public void setStatus(YangStatusType status) {
this.status = status;
}
......@@ -181,6 +188,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
*
* @return returns USES_DATA.
*/
@Override
public ParsableDataType getParsableDataType() {
return ParsableDataType.USES_DATA;
}
......@@ -190,6 +198,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
*
* @throws DataModelException a violation of data model rules.
*/
@Override
public void validateDataOnEntry() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
......@@ -199,6 +208,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
*
* @throws DataModelException a violation of data model rules.
*/
@Override
public void validateDataOnExit() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
......@@ -224,6 +234,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeEntry()
*/
@Override
public void generateJavaCodeEntry() {
// TODO Auto-generated method stub
......@@ -232,6 +243,7 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
/* (non-Javadoc)
* @see org.onosproject.yangutils.translator.CodeGenerator#generateJavaCodeExit()
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
......@@ -254,4 +266,16 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
// TODO Auto-generated method stub
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -42,4 +42,11 @@ public interface CachedFileHandle {
* @throws IOException when failes to generated java files.
*/
void close() throws IOException;
/**
* Sets child package path for import.
*
* @param pkg child's package path
*/
void setChildsPackage(String pkg);
}
......
......@@ -18,6 +18,7 @@ package org.onosproject.yangutils.translator.tojava;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.SortedSet;
......@@ -68,6 +69,11 @@ public class CachedJavaFileHandle implements CachedFileHandle {
private String pkg;
/**
* Java package in which the child class/interface needs to be generated.
*/
private String childsPkg;
/**
* Name of the object in YANG file.
*/
private String yangName;
......@@ -76,7 +82,7 @@ public class CachedJavaFileHandle implements CachedFileHandle {
* Sorted set of import info, to be used to maintain the set of classes to
* be imported in the generated class.
*/
private SortedSet<ImportInfo> importSet;
private SortedSet<String> importSet;
/**
* Cached list of attribute info.
......@@ -171,11 +177,30 @@ public class CachedJavaFileHandle implements CachedFileHandle {
}
/**
* Get the java package.
*
* @return the java package.
*/
public String getChildsPackage() {
return childsPkg;
}
/**
* Set the java package.
*
* @param pcg the package to set
*/
@Override
public void setChildsPackage(String pcg) {
childsPkg = pcg;
}
/**
* Get the set containing the imported class/interface info.
*
* @return the set containing the imported class/interface info.
*/
public SortedSet<ImportInfo> getImportSet() {
public SortedSet<String> getImportSet() {
return importSet;
}
......@@ -184,7 +209,7 @@ public class CachedJavaFileHandle implements CachedFileHandle {
*
* @param importSet the set containing the imported class/interface info.
*/
private void setImportSet(SortedSet<ImportInfo> importSet) {
private void setImportSet(SortedSet<String> importSet) {
this.importSet = importSet;
}
......@@ -203,9 +228,9 @@ public class CachedJavaFileHandle implements CachedFileHandle {
* be used in the generated class.
*/
if (getImportSet() == null) {
setImportSet(new TreeSet<ImportInfo>());
setImportSet(new TreeSet<String>());
}
return getImportSet().add(importInfo);
return getImportSet().add(JavaCodeSnippetGen.getImportText(importInfo));
}
/**
......@@ -253,23 +278,42 @@ public class CachedJavaFileHandle implements CachedFileHandle {
public void addAttributeInfo(YangType<?> attrType, String name, boolean isListAttr) {
AttributeInfo newAttr = new AttributeInfo();
if (attrType != null) {
attrType.setDataTypeName(attrType.getDataTypeName().replace("\"", ""));
if (attrType.getDataTypeName().equals("string")) {
attrType.setDataTypeName(
attrType.getDataTypeName().substring(0, 1).toUpperCase() + attrType.getDataTypeName().substring(1));
attrType.setDataTypeName(JavaIdentifierSyntax.getCaptialCase(attrType.getDataTypeName()));
}
newAttr.setAttributeType(attrType);
} else {
ImportInfo importInfo = new ImportInfo();
importInfo.setPkgInfo(getChildsPackage());
importInfo.setClassInfo(JavaIdentifierSyntax.getCaptialCase(name));
if (getImportSet() != null) {
getImportSet().add(JavaCodeSnippetGen.getImportText(importInfo));
} else {
SortedSet<String> newImportInfo = new TreeSet<>();
newImportInfo.add(JavaCodeSnippetGen.getImportText(importInfo));
setImportSet(newImportInfo);
}
newAttr.setQualifiedName(getQualifiedFlag(JavaCodeSnippetGen.getImportText(importInfo)));
}
newAttr.setAttributeName(name);
newAttr.setListAttr(isListAttr);
/*
* TODO: get the prefix and name of data type from attrType and
* initialize in importInfo.
*/
if (isListAttr) {
String listImport = UtilConstants.COLLECTION_IMPORTS + UtilConstants.LIST + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.NEW_LINE;
if (getImportSet() != null) {
getImportSet().add(listImport);
} else {
SortedSet<String> newImportInfo = new TreeSet<>();
newImportInfo.add(listImport);
setImportSet(newImportInfo);
}
/**
* TODO: Handle QualifiedFlag for imports.
*/
newAttr.setQualifiedName(getQualifiedFlag(listImport));
}
if (getCachedAttributeList() != null) {
if (getCachedAttributeList().size() == MAX_CACHABLE_ATTR) {
......@@ -281,7 +325,32 @@ public class CachedJavaFileHandle implements CachedFileHandle {
newAttributeInfo.add(newAttr);
setCachedAttributeList(newAttributeInfo);
}
name = JavaIdentifierSyntax.getCamelCase(name);
}
/**
* Check if the import set does not have a class info same as the new class
* info, if so the new class info be added to the import set. Otherwise
* check if the corresponding package info is same as the new package info,
* if so no need to qualified access, otherwise, it needs qualified access.
*
* @param newImportInfo new import info to be check for qualified access or
* not and updated in the import set accordingly.
* @return if the new attribute needs to be accessed in a qualified manner.
*/
private boolean getQualifiedFlag(String newImportInfo) {
for (String curImportInfo : getImportSet()) {
if (curImportInfo.equals(newImportInfo)) {
/*
* If import is already existing import with same package, we
* don't need qualified access, otherwise it needs to be
* qualified access.
*/
return !curImportInfo.equals(newImportInfo);
}
}
getImportSet().add(newImportInfo);
return false;
}
/**
......@@ -291,7 +360,7 @@ public class CachedJavaFileHandle implements CachedFileHandle {
public void close() throws IOException {
String className = getYangName();
className = (className.substring(0, 1).toUpperCase() + className.substring(1));
className = JavaIdentifierSyntax.getCaptialCase(className);
String packagePath = getPackage();
String filePath = UtilConstants.YANG_GEN_DIR + packagePath.replace(".", "/");
GeneratedFileType fileType = getGeneratedFileTypes();
......@@ -322,60 +391,18 @@ public class CachedJavaFileHandle implements CachedFileHandle {
String implFileName = className + UtilConstants.IMPL + TEMP_FILE_EXTENSION;
File implTempFile = new File(filePath + File.separator + implFileName);
if (fileType.equals(GeneratedFileType.INTERFACE) || fileType.equals(GeneratedFileType.ALL)) {
try {
interfaceFile.createNewFile();
appendContents(interfaceFile, className, GeneratedFileType.INTERFACE);
} catch (IOException e) {
throw new IOException("Failed to create interface file.");
}
}
if (fileType.equals(GeneratedFileType.BUILDER_CLASS) || fileType.equals(GeneratedFileType.ALL)) {
try {
builderFile.createNewFile();
appendContents(builderFile, className, GeneratedFileType.BUILDER_CLASS);
} catch (IOException e) {
throw new IOException("Failed to create builder class file.");
}
}
if (fileType.equals(GeneratedFileType.IMPL) || fileType.equals(GeneratedFileType.ALL)) {
try {
implTempFile.createNewFile();
appendContents(implTempFile, className, GeneratedFileType.IMPL);
} catch (IOException e) {
throw new IOException("Failed to create impl class file.");
}
}
if (fileType.equals(GeneratedFileType.BUILDER_INTERFACE) || fileType.equals(GeneratedFileType.ALL)) {
try {
builderInterfaceFile.createNewFile();
appendContents(builderInterfaceFile, className, GeneratedFileType.BUILDER_INTERFACE);
} catch (IOException e) {
throw new IOException("Failed to create builder interface class file.");
}
}
/*
* TODO: add the file header using
* JavaCodeSnippetGen.getFileHeaderComment
*/
/*
* TODO: get the import list using getImportText and add to the
* generated java file using JavaCodeSnippetGen.getImportText
*/
List<String> attributes = new LinkedList<>();
List<String> interfaceMethods = new LinkedList<>();
List<String> builderInterfaceMethods = new LinkedList<>();
List<String> builderClassMethods = new LinkedList<>();
List<String> implClassMethods = new LinkedList<>();
//TODO: Handle imports for the attributes.
List<String> imports = new LinkedList<>();
try {
attributes = SerializedDataStore.getSerializeData(SerializedDataStore.SerializedDataStoreType.ATTRIBUTE);
......@@ -391,7 +418,7 @@ public class CachedJavaFileHandle implements CachedFileHandle {
implClassMethods = SerializedDataStore
.getSerializeData(SerializedDataStore.SerializedDataStoreType.IMPL_METHODS);
//TODO:imports = SerializedDataStore.getSerializeData(SerializedDataStore.SerializedDataStoreType.IMPORT);
imports = SerializedDataStore.getSerializeData(SerializedDataStore.SerializedDataStoreType.IMPORT);
} catch (ClassNotFoundException | IOException e) {
log.info("There is no attribute info of " + className + " YANG file in the serialized files.");
}
......@@ -399,6 +426,14 @@ public class CachedJavaFileHandle implements CachedFileHandle {
if (getCachedAttributeList() != null) {
MethodsGenerator.setAttrInfo(getCachedAttributeList());
for (AttributeInfo attr : getCachedAttributeList()) {
if (attr.isListAttr()) {
String listString = JavaCodeSnippetGen.getListAttribute(attr.getAttributeType().getDataTypeName());
@SuppressWarnings("rawtypes")
YangType<?> type = new YangType();
type.setDataTypeName(listString);
attr.setAttributeType(type);
}
attributes.add(getAttributeString(attr));
interfaceMethods.add(MethodsGenerator.getMethodString(attr, GeneratedFileType.INTERFACE));
......@@ -409,6 +444,10 @@ public class CachedJavaFileHandle implements CachedFileHandle {
.add(MethodsGenerator.getMethodString(attr, GeneratedFileType.BUILDER_INTERFACE));
implClassMethods.add(MethodsGenerator.getMethodString(attr, GeneratedFileType.IMPL));
if (getImportSet() != null) {
imports = new ArrayList<>(getImportSet());
}
}
}
......@@ -422,6 +461,25 @@ public class CachedJavaFileHandle implements CachedFileHandle {
implClassMethods.add(MethodsGenerator.getConstructorString(className));
/**
* Start generation of files.
*/
if (fileType.equals(GeneratedFileType.INTERFACE) || fileType.equals(GeneratedFileType.ALL)) {
initiateFile(interfaceFile, className, GeneratedFileType.INTERFACE, imports);
}
if (fileType.equals(GeneratedFileType.BUILDER_CLASS) || fileType.equals(GeneratedFileType.ALL)) {
initiateFile(builderFile, className, GeneratedFileType.BUILDER_CLASS, imports);
}
if (fileType.equals(GeneratedFileType.IMPL) || fileType.equals(GeneratedFileType.ALL)) {
initiateFile(implTempFile, className, GeneratedFileType.IMPL, imports);
}
if (fileType.equals(GeneratedFileType.BUILDER_INTERFACE) || fileType.equals(GeneratedFileType.ALL)) {
initiateFile(builderInterfaceFile, className, GeneratedFileType.BUILDER_INTERFACE, imports);
}
/**
* Add attributes to the file.
*/
for (String attribute : attributes) {
......@@ -483,6 +541,25 @@ public class CachedJavaFileHandle implements CachedFileHandle {
}
/**
* Initiate generation of file based on generated file type.
*
* @param file generated file
* @param className generated file class name
* @param type generated file type
* @param imports imports for the file
* @throws IOException when fails to generate a file
*/
private void initiateFile(File file, String className, GeneratedFileType type, List<String> imports)
throws IOException {
try {
file.createNewFile();
appendContents(file, className, type, imports);
} catch (IOException e) {
throw new IOException("Failed to create " + file.getName() + " class file.");
}
}
/**
* Appends the temp files to main files.
*
* @param appendFile temp file
......@@ -584,7 +661,8 @@ public class CachedJavaFileHandle implements CachedFileHandle {
* @param fileName generated file name
* @param type generated file type
*/
private void appendContents(File file, String fileName, GeneratedFileType type) throws IOException {
private void appendContents(File file, String fileName, GeneratedFileType type, List<String> importsList)
throws IOException {
if (type.equals(GeneratedFileType.IMPL)) {
......@@ -599,12 +677,20 @@ public class CachedJavaFileHandle implements CachedFileHandle {
if (type.equals(GeneratedFileType.INTERFACE)) {
insert(file, CopyrightHeader.getCopyrightHeader());
insert(file, "package" + UtilConstants.SPACE + getPackage() + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE);
+ UtilConstants.NEW_LINE + UtilConstants.NEW_LINE);
for (String imports : importsList) {
insert(file, imports);
}
insert(file, UtilConstants.NEW_LINE);
write(file, fileName, type, JavaDocType.INTERFACE);
} else if (type.equals(GeneratedFileType.BUILDER_CLASS)) {
insert(file, CopyrightHeader.getCopyrightHeader());
insert(file, "package" + UtilConstants.SPACE + getPackage() + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE);
+ UtilConstants.NEW_LINE + UtilConstants.NEW_LINE);
for (String imports : importsList) {
insert(file, imports);
}
insert(file, UtilConstants.NEW_LINE);
write(file, fileName, type, JavaDocType.BUILDER_CLASS);
}
}
......
......@@ -42,18 +42,19 @@ public final class JavaCodeGenerator {
TraversalType curTraversal = TraversalType.ROOT;
while (!(curNode == null)) {
if (curTraversal != TraversalType.PARENT) {
if (curTraversal != TraversalType.PARENT || curTraversal == TraversalType.SIBILING) {
curNode.generateJavaCodeEntry();
}
if (curTraversal != TraversalType.PARENT && !(curNode.getChild() == null)) {
curTraversal = TraversalType.CHILD;
curNode = curNode.getChild();
} else if (!(curNode.getNextSibling() == null)) {
} else if (curTraversal == TraversalType.PARENT && !(curNode.getNextSibling() == null)) {
curNode.generateJavaCodeExit();
curTraversal = TraversalType.SIBILING;
curNode = curNode.getNextSibling();
} else {
curTraversal = TraversalType.PARENT;
curNode.generateJavaCodeExit();
curTraversal = TraversalType.PARENT;
curNode = curNode.getParent();
}
}
......
......@@ -16,9 +16,6 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.util.List;
import java.util.SortedSet;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.GeneratedMethodTypes;
......@@ -50,29 +47,15 @@ public final class JavaCodeSnippetGen {
}
/**
* reorder the import list based on the ONOS import rules.
*
* @param importInfo the set of classes/interfaces to be imported.
* @return string of import info.
*/
public List<ImportInfo> sortImportOrder(SortedSet<ImportInfo> importInfo) {
/* TODO: reorder the import list based on the ONOS import rules. */
return null;
}
/**
* Get the textual java code information corresponding to the import list.
*
* @param importInfo sorted list of import info.
* @param importInfo import info.
* @return the textual java code information corresponding to the import
* list.
*/
public static String getImportText(List<ImportInfo> importInfo) {
/*
* TODO: get the textual java code information corresponding to the
* import list
*/
return null;
public static String getImportText(ImportInfo importInfo) {
return UtilConstants.IMPORT + importInfo.getPkgInfo() + UtilConstants.PERIOD + importInfo.getClassInfo()
+ UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE;
}
/**
......@@ -101,9 +84,23 @@ public final class JavaCodeSnippetGen {
*/
public static String getJavaAttributeInfo(GeneratedFileType genFileTypes, String yangName, YangType<?> type) {
yangName = JavaIdentifierSyntax.getCamelCase(yangName);
if (type != null) {
return UtilConstants.PRIVATE + UtilConstants.SPACE + type.getDataTypeName() + UtilConstants.SPACE + yangName
+ UtilConstants.SEMI_COLAN;
}
return UtilConstants.PRIVATE + UtilConstants.SPACE + JavaIdentifierSyntax.getCaptialCase(yangName)
+ UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN;
}
/**
* Returns list attribute string.
*
* @param type attribute type
* @return list attribute string
*/
public static String getListAttribute(String type) {
return UtilConstants.LIST + UtilConstants.DIAMOND_OPEN_BRACKET + type + UtilConstants.DIAMOND_CLOSE_BRACKET;
}
/**
* Based on the file type and method type(s) and the YANG name of the
......
......@@ -163,4 +163,14 @@ public final class JavaIdentifierSyntax {
}
return camelCase;
}
/**
* Translate the YANG identifier name to java identifier with first letter in caps.
*
* @param yangIdentifier identifier in YANG file.
* @return corresponding java identifier
*/
public static String getCaptialCase(String yangIdentifier) {
return yangIdentifier.substring(0, 1).toUpperCase() + yangIdentifier.substring(1);
}
}
......
......@@ -112,6 +112,13 @@ public final class MethodsGenerator {
public static String constructMethodInfo(GeneratedFileType genFileTypes, String yangName,
GeneratedMethodTypes methodTypes, YangType<?> returnType) {
if (returnType == null) {
@SuppressWarnings("rawtypes")
YangType<?> type = new YangType();
type.setDataTypeName(yangName);
returnType = type;
}
if (genFileTypes.equals(GeneratedFileType.INTERFACE)) {
/**
......@@ -177,12 +184,10 @@ public final class MethodsGenerator {
String overrideString = UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.OVERRIDE
+ UtilConstants.NEW_LINE;
String getterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
attr.getAttributeName(), GeneratedMethodTypes.GETTER, attr.getAttributeType());
String setterString = JavaCodeSnippetGen.getJavaMethodInfo(GeneratedFileType.BUILDER_CLASS,
attr.getAttributeName(), GeneratedMethodTypes.SETTER, attr.getAttributeType());
return overrideString + getterString + UtilConstants.NEW_LINE + overrideString + setterString
+ UtilConstants.NEW_LINE;
}
......@@ -299,7 +304,8 @@ public final class MethodsGenerator {
private static String getGetterForClass(String yangName, YangType<?> returnType) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE
+ returnType.getDataTypeName() + UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX + yangName
+ JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName()) + UtilConstants.SPACE
+ UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
+ UtilConstants.RETURN + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN
......@@ -316,13 +322,14 @@ public final class MethodsGenerator {
private static String getSetterForClass(String yangName, YangType<?> returnType) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.PUBLIC + UtilConstants.SPACE + getBuilderClassName()
+ UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX + yangName + UtilConstants.OPEN_PARENTHESIS
+ returnType.getDataTypeName() + UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SPACE + UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.THIS + UtilConstants.PERIOD + yangName
+ UtilConstants.SPACE + UtilConstants.EQUAL + UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN
+ UtilConstants.SPACE + UtilConstants.THIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.SPACE + UtilConstants.SET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
+ UtilConstants.OPEN_PARENTHESIS + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
+ UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SPACE
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.NEW_LINE + UtilConstants.EIGHT_SPACE_INDENTATION
+ UtilConstants.THIS + UtilConstants.PERIOD + yangName + UtilConstants.SPACE + UtilConstants.EQUAL
+ UtilConstants.SPACE + yangName + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.EIGHT_SPACE_INDENTATION + UtilConstants.RETURN + UtilConstants.SPACE
+ UtilConstants.THIS + UtilConstants.SEMI_COLAN + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.CLOSE_CURLY_BRACKET;
}
......@@ -335,9 +342,9 @@ public final class MethodsGenerator {
*/
private static String getGetterForInterface(String yangName, YangType<?> returnType) {
returnType.setDataTypeName(returnType.getDataTypeName().replace("\"", ""));
return UtilConstants.FOUR_SPACE_INDENTATION + returnType.getDataTypeName() + UtilConstants.SPACE
+ UtilConstants.GET_METHOD_PREFIX + yangName + UtilConstants.OPEN_PARENTHESIS
+ UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
return UtilConstants.FOUR_SPACE_INDENTATION + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
+ UtilConstants.SPACE + UtilConstants.GET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
}
/**
......@@ -349,9 +356,9 @@ public final class MethodsGenerator {
*/
private static String getSetterForInterface(String yangName, YangType<?> returnType) {
return UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.BUILDER + UtilConstants.SPACE
+ UtilConstants.SET_METHOD_PREFIX + yangName + UtilConstants.OPEN_PARENTHESIS
+ returnType.getDataTypeName() + UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS
+ UtilConstants.SEMI_COLAN;
+ UtilConstants.SET_METHOD_PREFIX + JavaIdentifierSyntax.getCaptialCase(yangName)
+ UtilConstants.OPEN_PARENTHESIS + JavaIdentifierSyntax.getCaptialCase(returnType.getDataTypeName())
+ UtilConstants.SPACE + yangName + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN;
}
/**
......@@ -387,7 +394,8 @@ public final class MethodsGenerator {
constructor = constructor + UtilConstants.TWELVE_SPACE_INDENTATION + UtilConstants.THIS
+ UtilConstants.PERIOD + attribute.getAttributeName() + UtilConstants.SPACE
+ UtilConstants.EQUAL + UtilConstants.SPACE + builderAttribute + UtilConstants.OBJECT
+ UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX + attribute.getAttributeName()
+ UtilConstants.PERIOD + UtilConstants.GET_METHOD_PREFIX
+ JavaIdentifierSyntax.getCaptialCase(attribute.getAttributeName())
+ UtilConstants.OPEN_PARENTHESIS + UtilConstants.CLOSE_PARENTHESIS + UtilConstants.SEMI_COLAN
+ UtilConstants.NEW_LINE;
}
......
......@@ -129,7 +129,7 @@ public final class UtilConstants {
public static final String STATIC = "static";
public static final String FINAL = "final";
public static final String PACKAGE = "package";
public static final String IMPORT = "import";
public static final String IMPORT = "import ";
public static final String NULL = "null";
public static final String RETURN = "return";
public static final String NEW = "new";
......@@ -144,6 +144,7 @@ public final class UtilConstants {
/**
* For collections.
*/
public static final String COLLECTION_IMPORTS = "import java.util.";
public static final String ABSTRACT_COLLECTION = "AbstractCollection";
public static final String LIST = "List";
......
......@@ -62,8 +62,7 @@ public final class FileSystemUtil {
public static void createPackage(String pkg, String pkgInfo) throws IOException {
if (!doesPackageExist(new File(pkg))) {
try {
File pack = YangIoUtils
.createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
File pack = YangIoUtils.createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
YangIoUtils.addPackageInfo(pack, pkgInfo, pkg);
} catch (IOException e) {
throw new IOException("failed to create package-info file");
......
......@@ -226,8 +226,8 @@ public final class JavaDocGen {
*/
private static String generateForConstructors(String className) {
return (UtilConstants.JAVA_DOC_FIRST_LINE + UtilConstants.FOUR_SPACE_INDENTATION
+ UtilConstants.JAVA_DOC_SETTERS + className + UtilConstants.PERIOD + UtilConstants.NEW_LINE
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC
+ UtilConstants.JAVA_DOC_CONSTRUCTOR + className + UtilConstants.IMPL + UtilConstants.PERIOD
+ UtilConstants.NEW_LINE + UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.NEW_LINE_ESTRIC
+ UtilConstants.FOUR_SPACE_INDENTATION + UtilConstants.JAVA_DOC_PARAM
+ (className.substring(0, 1).toLowerCase() + className.substring(1)) + UtilConstants.OBJECT
+ UtilConstants.SPACE + UtilConstants.BUILDER_OBJECT + UtilConstants.SPACE + className
......
......@@ -27,6 +27,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
/**
......@@ -165,7 +166,6 @@ public final class SerializedDataStore {
* @throws IOException when fails to read from the file.
* @throws ClassNotFoundException when file is missing.
*/
@SuppressWarnings("unchecked")
public static List<String> getSerializeData(SerializedDataStoreType type)
throws IOException, ClassNotFoundException {
......@@ -191,10 +191,13 @@ public final class SerializedDataStore {
InputStream buffer = new BufferedInputStream(file);
ObjectInput input = new ObjectInputStream(buffer);
try {
List<String> recoveredData = (List<String>) input.readObject();
String data = (String) input.readObject();
List<String> recoveredData = new ArrayList<>();
recoveredData.add(data);
return recoveredData;
} finally {
input.close();
file.close();
}
} catch (ClassNotFoundException ex) {
throw new ClassNotFoundException("failed to fetch the serialized data file.");
......
......@@ -27,6 +27,7 @@ import org.apache.maven.project.MavenProject;
import org.apache.maven.model.Resource;
import org.onosproject.yangutils.utils.UtilConstants;
import org.apache.commons.io.FileUtils;
import static org.slf4j.LoggerFactory.getLogger;
import org.slf4j.Logger;
......@@ -102,10 +103,10 @@ public final class YangIoUtils {
File currentFile = new File(file);
currentFile.delete();
}
FileUtils.deleteDirectory(generatedDirectory);
} catch (IOException e) {
log.info("Failed to delete the generated files in " + generatedDirectory + " directory");
}
generatedDirectory.delete();
}
}
......