Vinod Kumar S
Committed by Gerrit Code Review

YANG Translator optimization

Change-Id: Ie6a6b9d371a4fc5fd973cf56d6f3c7b44a3146ba
Showing 70 changed files with 3216 additions and 1019 deletions
......@@ -21,7 +21,6 @@ import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* Reference RFC 6020.
......@@ -77,7 +76,8 @@ import org.onosproject.yangutils.translator.CachedFileHandle;
/**
* Data model node to maintain information defined in YANG augment.
*/
public class YangAugment extends YangNode implements YangLeavesHolder, YangCommonInfo, Parsable {
public class YangAugment extends YangNode
implements YangLeavesHolder, YangCommonInfo, Parsable {
/**
* Augment target node.
......@@ -110,11 +110,6 @@ public class YangAugment extends YangNode implements YangLeavesHolder, YangCommo
private YangStatusType status;
/**
* Package of the generated java code.
*/
private String pkg;
/**
* Create a YANG augment node.
*/
public YangAugment() {
......@@ -316,57 +311,4 @@ public class YangAugment extends YangNode implements YangLeavesHolder, YangCommo
}
/**
* Get the mapped java package.
*
* @return the java package
*/
@Override
public String getPackage() {
return pkg;
}
/**
* Set the mapped java package.
*
* @param pakg the package to set
*/
@Override
public void setPackage(String pakg) {
pkg = pakg;
}
/**
* Prepare the information for java code generation corresponding to YANG
* grouping info.
*
* @param codeGenDir code generation directory
*/
@Override
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
/**
* Create a java file using the YANG grouping info.
*/
@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
}
}
......
......@@ -15,15 +15,15 @@
*/
package org.onosproject.yangutils.datamodel;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
import java.util.LinkedList;
import java.util.List;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
/*-
* Reference RFC 6020.
......@@ -127,11 +127,6 @@ public class YangCase extends YangNode
private YangStatusType status;
/**
* Package of the generated java code.
*/
private String pkg;
/**
* Create a choice node.
*/
public YangCase() {
......@@ -314,76 +309,23 @@ public class YangCase extends YangNode
// TODO auto-generated method stub, to be implemented by parser
}
/**
* Get the mapped java package.
*
* @return the java package
*/
@Override
public String getPackage() {
return pkg;
}
/**
* Set the mapped java package.
*
* @param pakg the package to set
*/
@Override
public void setPackage(String pakg) {
pkg = pakg;
}
/**
* Generate the code corresponding to YANG case info.
*
* @param codeGenDir code generation directory
*/
@Override
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
/**
* Free resource used for generating code and generate valid java files
* corresponding to YANG case info.
*/
@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
}
@Override
public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
if ((this.getParent() == null) || (!(this.getParent() instanceof YangChoice))) {
if (getParent() == null || !(getParent() instanceof YangChoice)) {
throw new DataModelException("Internal Data Model Tree Error: Invalid/Missing holder in case " +
this.getName());
getName());
}
// Traverse up in tree to ask parent choice start collision detection.
((CollisionDetector) this.getParent()).detectCollidingChild(identifierName, dataType);
((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
}
@Override
public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
if (dataType == CASE_DATA) {
if (this.getName().equals(identifierName)) {
if (getName().equals(identifierName)) {
throw new DataModelException("YANG File Error: Identifier collision detected in case \"" +
this.getName() + "\"");
getName() + "\"");
}
return;
}
......
......@@ -17,8 +17,8 @@ package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.utils.YangConstructType.CHOICE_DATA;
/*-
......@@ -140,11 +140,21 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable, Co
super(YangNodeType.CHOICE_NODE);
}
/**
* Get the choice name.
*
* @return choice name
*/
@Override
public String getName() {
return name;
}
/**
* Set the choice name.
*
* @param name choice name
*/
@Override
public void setName(String name) {
this.name = name;
......@@ -295,46 +305,13 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable, Co
}
@Override
public String getPackage() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setPackage(String pkg) {
// TODO Auto-generated method stub
}
@Override
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
@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
}
@Override
public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
YangNode node = this.getChild();
while ((node != null)) {
if (this.getParent() instanceof YangCase && dataType != YangConstructType.CASE_DATA) {
((CollisionDetector) getParent()).detectCollidingChild(identifierName, dataType);
}
YangNode node = getChild();
while (node != null) {
if (node instanceof CollisionDetector) {
((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
}
......@@ -346,15 +323,15 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable, Co
public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
if (dataType == CHOICE_DATA) {
if (this.getName().equals(identifierName)) {
if (getName().equals(identifierName)) {
throw new DataModelException("YANG file error: Identifier collision detected in choice \"" +
this.getName() + "\"");
getName() + "\"");
}
return;
}
YangNode node = this.getChild();
while ((node != null)) {
YangNode node = getChild();
while (node != null) {
if (node instanceof CollisionDetector) {
((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
}
......
......@@ -16,20 +16,15 @@
package org.onosproject.yangutils.datamodel;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
/*-
* Reference RFC 6020.
*
......@@ -136,16 +131,6 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
private YangStatusType status = YangStatusType.CURRENT;
/**
* Package of the generated java code.
*/
private String pkg;
/**
* Cached Java File Handle.
*/
private CachedFileHandle fileHandle;
/**
* Create a container node.
*/
public YangContainer() {
......@@ -335,26 +320,6 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
}
/**
* Get the cached file handle.
*
* @return the fileHandle
*/
@Override
public CachedFileHandle getFileHandle() {
return fileHandle;
}
/**
* Set the cached file handle.
*
* @param handle the fileHandle to set
*/
@Override
public void setFileHandle(CachedFileHandle handle) {
fileHandle = handle;
}
/**
* Returns the type of the data.
*
* @return returns CONTAINER_DATA
......@@ -435,7 +400,7 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
* If a node has "config" set to "false", no node underneath it can have
* "config" set to "true".
*/
if ((!isConfig) && (leaves != null)) {
if (!isConfig && leaves != null) {
for (YangLeaf leaf : leaves) {
if (leaf.isConfig()) {
throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
......@@ -444,7 +409,7 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
}
}
if ((!isConfig) && (leafLists != null)) {
if (!isConfig && leafLists != null) {
for (YangLeafList leafList : leafLists) {
if (leafList.isConfig()) {
throw new DataModelException("If a container has \"config\" set to \"false\", no node underneath " +
......@@ -454,98 +419,6 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
}
}
/**
* Get the mapped java package.
*
* @return the java package
*/
@Override
public String getPackage() {
return pkg;
}
/**
* Set the mapped java package.
*
* @param pcg the package to set
*/
@Override
public void setPackage(String pcg) {
pkg = pcg;
}
/**
* Generate the java code corresponding to YANG container.
*
* @param codeGenDir code generation directory
* @throws IOException when fails to generate the source files.
*/
@Override
public void generateJavaCodeEntry(String codeGenDir) throws IOException {
YangNode parent = getParent();
String contPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), parent.getName());
contPkg = JavaIdentifierSyntax.getCamelCase(contPkg).toLowerCase();
setPackage(contPkg);
CachedFileHandle handle = null;
try {
FileSystemUtil.createPackage(codeGenDir + getPackage(), parent.getName() + UtilConstants.CHILDREN);
handle = FileSystemUtil.createSourceFiles(getPackage(), getName(),
GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER);
handle.setRelativeFilePath(getPackage().replace(".", "/"));
handle.setCodeGenFilePath(codeGenDir);
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
addLeavesAttributes();
addLeafListAttributes();
addAttributeInParent();
}
/**
* Adds current node attribute to parent file.
*/
private void addAttributeInParent() {
if (getParent() != null) {
getParent().getFileHandle().addAttributeInfo(null, getName(), false);
}
}
@Override
public void generateJavaCodeExit() throws IOException {
getFileHandle().close();
return;
}
/**
* Adds leaf attributes in generated files.
*/
private void addLeavesAttributes() {
List<YangLeaf> leaves = getListOfLeaf();
if (leaves != null) {
for (YangLeaf leaf : leaves) {
getFileHandle().addAttributeInfo(leaf.getDataType(), leaf.getLeafName(), false);
}
}
}
/**
* Adds leaf list's attributes in generated files.
*/
private void addLeafListAttributes() {
List<YangLeafList> leavesList = getListOfLeafList();
if (leavesList != null) {
for (YangLeafList leafList : leavesList) {
getFileHandle().addAttributeInfo(leafList.getDataType(), leafList.getLeafName(), true);
}
}
return;
}
@Override
public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
// Asks helper to detect colliding child.
......@@ -554,9 +427,9 @@ public class YangContainer extends YangNode implements YangLeavesHolder, YangCom
@Override
public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
if (this.getName().equals(identifierName)) {
if (getName().equals(identifierName)) {
throw new DataModelException("YANG file error: Duplicate input identifier detected, same as container \""
+ this.getName() + "\"");
+ getName() + "\"");
}
}
}
......
......@@ -21,7 +21,6 @@ import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* Reference RFC 6020.
......@@ -110,11 +109,6 @@ public class YangGrouping extends YangNode
private YangStatusType status;
/**
* Package of the generated java code.
*/
private String pkg;
/**
* Creates the grouping node.
*/
public YangGrouping() {
......@@ -296,58 +290,4 @@ public class YangGrouping extends YangNode
public void validateDataOnExit() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
/**
* Generate the code for YANG grouping.
*
* @param codeGenDir code generated directory.
*/
@Override
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
/**
* Free the resources used to generate java files corresponding to YANG
* grouping info and generate valid java files.
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
}
/**
* Get the mapped java package.
*
* @return the java package
*/
@Override
public String getPackage() {
return pkg;
}
/**
* Set the mapped java package.
*
* @param pakg the package to set
*/
@Override
public void setPackage(String pakg) {
pkg = pakg;
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
}
......
......@@ -16,16 +16,16 @@
package org.onosproject.yangutils.datamodel;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.utils.YangConstructType;
import java.util.LinkedList;
import java.util.List;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
/*-
/*
* The "list" statement is used to define an interior data node in the
* schema tree. A list node may exist in multiple instances in the data
* tree. Each such instance is known as a list entry. The "list"
......@@ -166,11 +166,6 @@ public class YangList extends YangNode
private YangStatusType status = YangStatusType.CURRENT;
/**
* Package of the generated java code.
*/
private String pkg;
/**
* Constructor.
*/
public YangList() {
......@@ -619,61 +614,6 @@ public class YangList extends YangNode
}
}
/**
* Populate the cached handle with information about the list attributes to
* generate java code.
*
* @param codeGenDir code generated directory
*/
@Override
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
/**
* Free the resources used to generate the java file corresponding to YANG
* list info.
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
}
/**
* Get the mapped java package.
*
* @return the java package
*/
@Override
public String getPackage() {
return pkg;
}
/**
* Set the mapped java package.
*
* @param pakg the package to set
*/
@Override
public void setPackage(String pakg) {
pkg = pakg;
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
@Override
public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
// Asks helper to detect colliding child.
......@@ -682,9 +622,9 @@ public class YangList extends YangNode
@Override
public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
if (this.getName().equals(identifierName)) {
if (getName().equals(identifierName)) {
throw new DataModelException("YANG file error: Duplicate input identifier detected, same as list \"" +
this.getName() + "\"");
getName() + "\"");
}
}
}
......
......@@ -15,20 +15,14 @@
*/
package org.onosproject.yangutils.datamodel;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.CodeGenerator;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
/*-
* Reference:RFC 6020.
......@@ -74,7 +68,7 @@ import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
* Data model node to maintain information defined in YANG module.
*/
public class YangModule extends YangNode
implements YangLeavesHolder, YangDesc, YangReference, Parsable, CodeGenerator, CollisionDetector {
implements YangLeavesHolder, YangDesc, YangReference, Parsable, CollisionDetector {
/**
* Name of the module.
......@@ -157,11 +151,6 @@ public class YangModule extends YangNode
*/
private byte version;
/**
* Cached Java File Handle.
*/
private CachedFileHandle fileHandle;
/*-
* Reference RFC 6020.
*
......@@ -510,52 +499,6 @@ public class YangModule extends YangNode
}
/**
* Get the mapped java package.
*
* @return the java package
*/
@Override
public String getPackage() {
if (getFileHandle() != null) {
return getFileHandle().getRelativeFilePath().replace("/", ".");
}
return null;
}
/**
* Set the mapped java package.
*
* @param pcg the package to set
*/
@Override
public void setPackage(String pcg) {
if (getFileHandle() != null) {
pcg.replace(".", "/");
getFileHandle().setRelativeFilePath(pcg);
}
}
/**
* Get the cached file handle.
*
* @return the fileHandle
*/
@Override
public CachedFileHandle getFileHandle() {
return fileHandle;
}
/**
* Set the cached file handle.
*
* @param handle the fileHandle to set
*/
@Override
public void setFileHandle(CachedFileHandle handle) {
fileHandle = handle;
}
/**
* Get the list of nested reference's which required resolution.
*
* @return list of nested reference's which required resolution
......@@ -623,64 +566,6 @@ public class YangModule extends YangNode
}
/**
* Generates java code for module.
*
* @param codeGenDir code generation directory
* @throws IOException when fails to generate the source files
*/
@Override
public void generateJavaCodeEntry(String codeGenDir) throws IOException {
String modPkg = JavaIdentifierSyntax.getRootPackage(getVersion(), getNameSpace().getUri(),
getRevision().getRevDate());
modPkg = JavaIdentifierSyntax.getCamelCase(modPkg);
CachedFileHandle handle = null;
try {
FileSystemUtil.createPackage(codeGenDir + modPkg, getName());
handle = FileSystemUtil.createSourceFiles(modPkg, getName(),
GeneratedFileType.GENERATE_INTERFACE_WITH_BUILDER);
handle.setCodeGenFilePath(codeGenDir);
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
addLeavesAttributes();
addLeafListAttributes();
}
@Override
public void generateJavaCodeExit() throws IOException {
getFileHandle().close();
return;
}
/**
* Adds leaf attributes in generated files.
*/
private void addLeavesAttributes() {
List<YangLeaf> leaves = getListOfLeaf();
if (leaves != null) {
for (YangLeaf leaf : leaves) {
getFileHandle().addAttributeInfo(leaf.getDataType(), leaf.getLeafName(), false);
}
}
}
/**
* Adds leaf list's attributes in generated files.
*/
private void addLeafListAttributes() {
List<YangLeafList> leavesList = getListOfLeafList();
if (leavesList != null) {
for (YangLeafList leafList : leavesList) {
getFileHandle().addAttributeInfo(leafList.getDataType(), leafList.getLeafName(), true);
}
}
}
/**
* Add a type to resolve the nested references.
*
* @param node grouping or typedef node which needs to be resolved
......@@ -715,4 +600,5 @@ public class YangModule extends YangNode
public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
// Not required as module doesn't have any parent.
}
}
......
......@@ -16,13 +16,11 @@
package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.CodeGenerator;
/**
* Base class of a node in data model tree.
*/
public abstract class YangNode implements CodeGenerator {
public abstract class YangNode {
/**
* Type of node.
......@@ -50,6 +48,20 @@ public abstract class YangNode implements CodeGenerator {
private YangNode previousSibling;
/**
* Get the nodes name.
*
* @return nodes name
*/
public abstract String getName();
/**
* Set the nodes name.
*
* @param name nodes name
*/
public abstract void setName(String name);
/**
* Default constructor is made private to ensure node type is always set.
*/
@SuppressWarnings("unused")
......@@ -195,19 +207,25 @@ public abstract class YangNode implements CodeGenerator {
YangNode curNode;
curNode = getChild();
/* If the new node needs to be the first child */
/*-
* If the new node needs to be the first child
if (newChild.getNodeType().ordinal() < curNode.getNodeType().ordinal()) {
newChild.setNextSibling(curNode);
curNode.setPreviousSibling(newChild);
setChild(newChild);
return;
}
*/
/*
* Get the predecessor child of new child
*/
while (curNode.getNextSibling() != null
&& newChild.getNodeType().ordinal() >= curNode.getNextSibling().getNodeType().ordinal()) {
/*
* && newChild.getNodeType().ordinal() >=
* curNode.getNextSibling().getNodeType().ordinal()
*/) {
curNode = curNode.getNextSibling();
}
......@@ -218,54 +236,13 @@ public abstract class YangNode implements CodeGenerator {
return;
}
/* Insert the new node in child node list sorted by type */
/*-
* Insert the new node in child node list sorted by type
newChild.setNextSibling(curNode.getNextSibling());
newChild.setPreviousSibling(curNode);
curNode.getNextSibling().setPreviousSibling(newChild);
curNode.setNextSibling(newChild);
return;
}
/**
* Get the YANG name of the node.
*
* @return the name of node as defined in YANG file.
*/
public abstract String getName();
/**
* Set the YANG name of the node.
*
* @param name the name of node as defined in YANG file.
*/
public abstract void setName(String name);
/**
* Get the mapped java package.
*
* @return the java package
*/
public abstract String getPackage();
/**
* Set the mapped java package.
*
* @param pkg the package to set
*/
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);
}
}
......
......@@ -15,14 +15,14 @@
*/
package org.onosproject.yangutils.datamodel;
import java.util.LinkedList;
import java.util.List;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.utils.YangConstructType;
import java.util.LinkedList;
import java.util.List;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
/*
* Reference RFC 6020.
......@@ -144,11 +144,6 @@ public class YangSubModule extends YangNode
private byte version;
/**
* package of the generated java code.
*/
private String pkg;
/**
* Create a sub module node.
*/
public YangSubModule() {
......@@ -468,57 +463,6 @@ public class YangSubModule extends YangNode
// TODO auto-generated method stub, to be implemented by parser
}
/**
* Generates java code for sub-module.
*
* @param codeGenDir code generation directory.
*/
@Override
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
/**
* Free resources used to generate code.
*/
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
}
/**
* Get the mapped java package.
*
* @return the java package
*/
@Override
public String getPackage() {
return pkg;
}
/**
* Set the mapped java package.
*
* @param pakg the package to set
*/
@Override
public void setPackage(String pakg) {
pkg = pakg;
}
@Override
public CachedFileHandle getFileHandle() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setFileHandle(CachedFileHandle fileHandle) {
// TODO Auto-generated method stub
}
@Override
public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
// Asks helper to detect colliding child.
......
......@@ -15,16 +15,9 @@
*/
package org.onosproject.yangutils.datamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.translator.CachedFileHandle;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.utils.io.impl.FileSystemUtil;
/*-
* Reference RFC 6020.
......@@ -92,16 +85,6 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
private String units;
/**
* package of the generated java code.
*/
private String pkg;
/**
* Cached Java File Handle.
*/
private CachedFileHandle fileHandle;
/**
* Create a typedef node.
*/
public YangTypeDef() {
......@@ -307,103 +290,4 @@ public class YangTypeDef extends YangNode implements YangCommonInfo, Parsable {
getDerivedType().setDataType(YangDataTypes.DERIVED);
}
/**
* Generate java code snippet corresponding to YANG typedef.
*
* @param codeGenDir code generation directory
* @throws IOException when fails to generate files for typedef
*/
@Override
public void generateJavaCodeEntry(String codeGenDir) throws IOException {
YangNode parent = getParent();
String typeDefPkg = JavaIdentifierSyntax.getPackageFromParent(parent.getPackage(), parent.getName());
typeDefPkg = JavaIdentifierSyntax.getCamelCase(typeDefPkg).toLowerCase();
setPackage(typeDefPkg);
CachedFileHandle handle = null;
try {
FileSystemUtil.createPackage(codeGenDir + getPackage(), parent.getName() + UtilConstants.CHILDREN);
handle = FileSystemUtil.createSourceFiles(getPackage(), getName(),
GeneratedFileType.GENERATE_TYPEDEF_CLASS);
handle.setRelativeFilePath(getPackage().replace(".", "/"));
handle.setCodeGenFilePath(codeGenDir);
} catch (IOException e) {
throw new IOException("Failed to create the source files.");
}
setFileHandle(handle);
getDerivedType().getDataTypeExtendedInfo().getBaseType().setJavaPackage(getPackage());
addAttributeInfo();
addAttributeInParent();
}
/**
* Adds current node attribute to parent file.
*/
private void addAttributeInParent() {
if (getParent() != null) {
getParent().getFileHandle().addAttributeInfo(null, getName(), false);
}
}
/**
* Adds attribute to file handle.
*/
private void addAttributeInfo() {
getFileHandle().addAttributeInfo(getDerivedType().getDataTypeExtendedInfo().getBaseType(),
JavaIdentifierSyntax.getCamelCase(getName()), false);
}
/**
* Free resource used for code generation of YANG typedef.
*
* @throws IOException when fails to generate files
*/
@Override
public void generateJavaCodeExit() throws IOException {
getFileHandle().close();
return;
}
/**
* Get the mapped java package.
*
* @return the java package
*/
@Override
public String getPackage() {
return pkg;
}
/**
* Set the mapped java package.
*
* @param pakg mapped java package
*/
@Override
public void setPackage(String pakg) {
pkg = pakg;
}
/**
* Get the file handle of the cached file used during code generation.
*
* @return cached file handle
*/
@Override
public CachedFileHandle getFileHandle() {
return fileHandle;
}
/**
* Set the file handle to be used used for code generation.
*
* @param handle cached file handle
*/
@Override
public void setFileHandle(CachedFileHandle handle) {
fileHandle = handle;
}
}
......
......@@ -18,7 +18,6 @@ package org.onosproject.yangutils.datamodel;
import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
import org.onosproject.yangutils.translator.CachedFileHandle;
/*-
* Reference RFC 6020.
......@@ -213,51 +212,26 @@ public class YangUses extends YangNode implements YangCommonInfo, Parsable {
// TODO auto-generated method stub, to be implemented by parser
}
/**
* Get uses name.
*
* @return uses name
*/
@Override
public String getName() {
// TODO Auto-generated method stub
return null;
}
/**
* Set uses name.
*
* @param name uses name
*/
@Override
public void setName(String name) {
// TODO Auto-generated method stub
}
@Override
public void generateJavaCodeEntry(String codeGenDir) {
// TODO Auto-generated method stub
}
@Override
public void generateJavaCodeExit() {
// TODO Auto-generated method stub
}
@Override
public String getPackage() {
// TODO Auto-generated method stub
return null;
}
@Override
public void setPackage(String pkg) {
// 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
}
}
......
......@@ -63,7 +63,7 @@ public final class DataModelUtils {
}
}
node = node.getChild();
while ((node != null)) {
while (node != null) {
if (node instanceof CollisionDetector) {
((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
}
......
/*
* 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.datamodel.utils;
/**
* The target language in which the YANG information is modeled.
*/
public enum GeneratedLanguage {
/**
* Java.
*/
JAVA_GENERATION;
}
/*
* 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.datamodel.utils;
import org.onosproject.yangutils.datamodel.YangAugment;
import org.onosproject.yangutils.datamodel.YangCase;
import org.onosproject.yangutils.datamodel.YangChoice;
import org.onosproject.yangutils.datamodel.YangContainer;
import org.onosproject.yangutils.datamodel.YangGrouping;
import org.onosproject.yangutils.datamodel.YangList;
import org.onosproject.yangutils.datamodel.YangModule;
import org.onosproject.yangutils.datamodel.YangSubModule;
import org.onosproject.yangutils.datamodel.YangTypeDef;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaAugment;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaCase;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaChoice;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaContainer;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGrouping;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaList;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaSubModule;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUses;
/**
* Factory to create data model objects based on the target file type.
*/
public final class YangDataModelFactory {
/**
* Utility class, hence private to prevent creating objects.
*/
private YangDataModelFactory() {
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangModule getYangModuleNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaModule();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangAugment getYangAugmentNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaAugment();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangCase getYangCaseNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaCase();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangChoice getYangChoiceNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaChoice();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangContainer getYangContainerNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaContainer();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangGrouping getYangGroupingNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaGrouping();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangList getYangListNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaList();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangSubModule getYangSubModuleNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaSubModule();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangTypeDef getYangTypeDefNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaTypeDef();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
/**
* Based on the target language generate the inherited data model node.
*
* @param targetLanguage target language in which YANG mapping needs to be
* generated.
* @return the corresponding inherited node based on the target language.
*/
public static YangUses getYangUsesNode(GeneratedLanguage targetLanguage) {
switch (targetLanguage) {
case JAVA_GENERATION: {
return new YangJavaUses();
}
default: {
throw new RuntimeException("Only YANG to Java is supported.");
}
}
}
}
......@@ -24,11 +24,12 @@ import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidBooleanValue;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidBooleanValue;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
......
......@@ -27,7 +27,8 @@ import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangContainerNode;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
......@@ -37,6 +38,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
import static org.onosproject.yangutils.utils.YangConstructType.CONFIG_DATA;
......@@ -110,7 +112,7 @@ public final class ContainerListener {
int charPositionInLine = ctx.getStart().getCharPositionInLine();
detectCollidingChildUtil(listener, line, charPositionInLine, identifier, CONTAINER_DATA);
YangContainer container = new YangContainer();
YangContainer container = getYangContainerNode(JAVA_GENERATION);
container.setName(identifier);
/*
......@@ -123,8 +125,8 @@ public final class ContainerListener {
}
Parsable curData = listener.getParsedDataStack().peek();
if ((curData instanceof YangModule) || (curData instanceof YangContainer)
|| (curData instanceof YangList)) {
if (curData instanceof YangModule || curData instanceof YangContainer
|| curData instanceof YangList) {
YangNode curNode = (YangNode) curData;
try {
curNode.addChild(container);
......
......@@ -27,7 +27,8 @@ import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangListNode;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
......@@ -37,6 +38,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityNonZero;
......@@ -117,7 +119,7 @@ public final class ListListener {
int charPositionInLine = ctx.getStart().getCharPositionInLine();
detectCollidingChildUtil(listener, line, charPositionInLine, identifier, LIST_DATA);
YangList yangList = new YangList();
YangList yangList = getYangListNode(JAVA_GENERATION);
yangList.setName(identifier);
/*
......
......@@ -21,13 +21,15 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangModuleNode;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.utils.YangConstructType.MODULE_DATA;
......@@ -76,7 +78,7 @@ public final class ModuleListener {
String identifier = getValidIdentifier(ctx.identifier().getText(), MODULE_DATA, ctx);
YangModule yangModule = new YangModule();
YangModule yangModule = getYangModuleNode(JAVA_GENERATION);
yangModule.setName(identifier);
if (ctx.moduleBody().moduleHeaderStatement().yangVersionStatement() == null) {
......
......@@ -21,13 +21,15 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangSubModuleNode;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructListenerErrorMessage;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.INVALID_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.utils.YangConstructType.SUB_MODULE_DATA;
......@@ -79,7 +81,7 @@ public final class SubModuleListener {
String identifier = getValidIdentifier(ctx.identifier().getText(), SUB_MODULE_DATA, ctx);
YangSubModule yangSubModule = new YangSubModule();
YangSubModule yangSubModule = getYangSubModuleNode(JAVA_GENERATION);
yangSubModule.setName(identifier);
if (ctx.submoduleBody().submoduleHeaderStatement().yangVersionStatement() == null) {
......
......@@ -31,7 +31,8 @@ import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangTypeDefNode;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorMessageConstruction.constructExtendedListenerErrorMessage;
......@@ -41,6 +42,7 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_CURRENT_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.UNHANDLED_PARSED_DATA;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.getValidIdentifier;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityEqualsOne;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
......@@ -121,7 +123,7 @@ public final class TypeDefListener {
derivedType.setDataType(YangDataTypes.DERIVED);
derivedType.setDataTypeName(identifier);
YangTypeDef typeDefNode = new YangTypeDef();
YangTypeDef typeDefNode = getYangTypeDefNode(JAVA_GENERATION);
typeDefNode.setDerivedType(derivedType);
Parsable curData = listener.getParsedDataStack().peek();
......
......@@ -33,13 +33,16 @@ 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.translator.tojava.JavaCodeGenerator;
import org.onosproject.yangutils.utils.UtilConstants;
import org.onosproject.yangutils.utils.io.impl.CopyrightHeader;
import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
import org.onosproject.yangutils.utils.io.impl.YangIoUtils;
import org.sonatype.plexus.build.incremental.BuildContext;
import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
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.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.
......@@ -70,7 +73,7 @@ public class YangUtilManager extends AbstractMojo {
* Output directory.
*/
@Parameter(property = "project.build.outputDirectory", required = true, defaultValue = "target/classes")
private File outputDirectory;
private String outputDirectory;
/**
* Current maven project.
......@@ -84,6 +87,9 @@ 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 YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
private String searchDir;
private String codeGenDir;
......@@ -94,6 +100,7 @@ public class YangUtilManager extends AbstractMojo {
* @param curProject maven project
*/
public void setCurrentProject(final MavenProject curProject) {
project = curProject;
}
......@@ -102,15 +109,14 @@ public class YangUtilManager extends AbstractMojo {
try {
CopyrightHeader.parseCopyrightHeader();
/**
* For deleting the generated code in previous build.
*/
YangIoUtils.clean(baseDir);
clean(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
clean(getDirectory(baseDir, outputDirectory));
searchDir = baseDir + File.separator + yangFilesDir;
codeGenDir = baseDir + File.separator + genFilesDir + File.separator;
searchDir = getDirectory(baseDir, yangFilesDir);
codeGenDir = getDirectory(baseDir, genFilesDir) + File.separator;
List<String> yangFiles = YangFileScanner.getYangFiles(searchDir);
Iterator<String> yangFileIterator = yangFiles.iterator();
......@@ -118,7 +124,7 @@ public class YangUtilManager extends AbstractMojo {
String yangFile = yangFileIterator.next();
try {
YangNode yangNode = yangUtilsParser.getDataModel(yangFile);
JavaCodeGenerator.generateJavaCode(yangNode, codeGenDir);
generateJavaCode(yangNode, codeGenDir);
} catch (ParserException e) {
String logInfo = "Error in file: " + e.getFileName();
if (e.getLineNumber() != 0) {
......@@ -127,16 +133,17 @@ public class YangUtilManager extends AbstractMojo {
}
if (e.getMessage() != null) {
logInfo = logInfo + "\n" + e.getMessage();
logInfo = logInfo + UtilConstants.NEW_LINE + e.getMessage();
}
getLog().info(logInfo);
}
}
YangIoUtils.addToSource(baseDir + File.separator + UtilConstants.YANG_GEN_DIR, project, context);
addToSource(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG, project, context);
copyYangFilesToTarget(yangFiles, getDirectory(baseDir, outputDirectory), project);
} catch (Exception e) {
getLog().info(e);
//throw new MojoExecutionException("Exception occured due to " + e.getLocalizedMessage());
}
}
}
......
/*
* 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;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.utils.TempDataStoreTypes;
/**
* Cached java file handle, which supports the addition of member attributes and
* methods.
*/
public interface CachedFileHandle {
/**
* Add a new attribute to the file(s).
*
* @param attrType data type of the added attribute
* @param name name of the attribute
* @param isListAttr if the current added attribute needs to be maintained
* in a list
*/
void addAttributeInfo(YangType<?> attrType, String name, boolean isListAttr);
/**
* Flushes the cached contents to the target file, frees used resources.
*
* @throws IOException when failes to generated java files
*/
void close() throws IOException;
/**
* Sets directory package path for code generation.
*
* @param filePath directory package path for code generation
*/
void setRelativeFilePath(String filePath);
/**
* Gets directory package path for code generation.
*
* @return directory package path for code generation
*/
String getRelativeFilePath();
/**
* Gets base directory package path for code generation.
*
* @return directory package path for code generation
*/
String getCodeGenFilePath();
/**
* Sets base directory package path for code generation.
*
* @param path base directory path
*/
void setCodeGenFilePath(String path);
/**
* Writes specific info to a Temp file.
*
* @param data data to be stored
* @param type type of Temp data store
* @param className class name
* @param genDir generated directory
* @throws IOException when fails to create a Temp data file
*/
void setTempData(String data, TempDataStoreTypes type, String className, String genDir) throws IOException;
/**
* Get the Temp data.
*
* @param type type of Temp data store
* @param className name of the class
* @param genDir generated directory
* @return temp data
* @throws IOException when fails to read from the file
* @throws ClassNotFoundException when class is missing
* @throws FileNotFoundException when file is missing
*/
String getTempData(TempDataStoreTypes type, String className, String genDir)
throws IOException, FileNotFoundException, ClassNotFoundException;
}
......@@ -14,17 +14,17 @@
* limitations under the License.
*/
package org.onosproject.yangutils.translator;
package org.onosproject.yangutils.translator.tojava;
/**
* Type of files generated.
*/
public final class GeneratedFileType {
public final class GeneratedJavaFileType {
/**
* prevent creating attributes.
*/
private GeneratedFileType() {
private GeneratedJavaFileType() {
}
/**
......
/*
* 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;
/**
* Type of files generated.
*/
public final class GeneratedTempFileType {
/**
* prevent creating attributes.
*/
private GeneratedTempFileType() {
}
/**
* attributes definition temporary file.
*/
public static final int ATTRIBUTES_MASK = 1;
/**
* getter methods for interface.
*/
public static final int GETTER_FOR_INTERFACE_MASK = 2;
/**
* getter methods for class.
*/
public static final int GETTER_FOR_CLASS_MASK = 4;
/**
* setter methods for interface.
*/
public static final int SETTER_FOR_INTERFACE_MASK = 8;
/**
* setter methods for class.
*/
public static final int SETTER_FOR_CLASS_MASK = 16;
/**
* constructor method of class.
*/
public static final int CONSTRUCTOR_IMPL_MASK = 32;
/**
* hash code implementation of class.
*/
public static final int HASH_CODE_IMPL_MASK = 64;
/**
* equals implementation of class.
*/
public static final int EQUALS_IMPL_MASK = 128;
/**
* to string implementation of class.
*/
public static final int TO_STRING_IMPL_MASK = 256;
}
/*
* 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;
/**
* Data model nodes which are required to generate java classes, need to support
* java file info.
*/
public interface HasJavaFileInfo {
/**
* Get the generated java file information.
*
* @return generated java file information
*/
public JavaFileInfo getJavaFileInfo();
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
public void setJavaFileInfo(JavaFileInfo javaInfo);
}
/*
* 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;
/**
* Maintains the information of the java import data.
*/
public interface HasJavaImportData {
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
public JavaImportData getJavaImportData();
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
public void setJavaImportData(JavaImportData javaImportData);
}
......@@ -13,29 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.yangutils.translator;
import java.io.IOException;
package org.onosproject.yangutils.translator.tojava;
/**
* Abstraction of an entity which provides Code generator functionalities.
* Has temporary file handle.
*/
public interface CodeGenerator {
public interface HasTempJavaCodeFragmentFiles {
/**
* Traverse the schema of application and generate corresponding code.
* Get the temporary file handle.
*
* @param codeGenDir code generation directory
* @throws IOException when fails to translate the data model tree
* @return temporary file handle
*/
void generateJavaCodeEntry(String codeGenDir) throws IOException;
TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles();
/**
* Traverse the schema of application and generate corresponding code.
* Set temporary file handle.
*
* @throws IOException when fails to generate java code
* @param fileHandle temporary file handle
*/
void generateJavaCodeExit() throws IOException;
void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle);
}
......
......@@ -16,12 +16,18 @@
package org.onosproject.yangutils.translator.tojava;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getIsQualifiedAccessOrAddToImportList;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfCurNode;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedTypeInfoOfLeafAttribute;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
/**
* Maintains the attribute info corresponding to class/interface generated.
*/
public class AttributeInfo {
public final class JavaAttributeInfo {
/**
* The data type info of attribute.
......@@ -47,12 +53,12 @@ public class AttributeInfo {
* The class info will be used to set the attribute type and package info
* will be use for qualified name.
*/
private ImportInfo importInfo;
private JavaQualifiedTypeInfo importInfo;
/**
* Default constructor.
*/
public AttributeInfo() {
private JavaAttributeInfo() {
}
/**
......@@ -61,6 +67,10 @@ public class AttributeInfo {
* @return the data type info of attribute
*/
public YangType<?> getAttributeType() {
if (attrType == null) {
throw new RuntimeException("Expected java attribute type is null");
}
return attrType;
}
......@@ -80,6 +90,10 @@ public class AttributeInfo {
* @return name of the attribute
*/
public String getAttributeName() {
if (name == null) {
throw new RuntimeException("Expected java attribute name is null");
}
return name;
}
......@@ -89,6 +103,7 @@ public class AttributeInfo {
* @param attrName name of the attribute
*/
public void setAttributeName(String attrName) {
name = attrName;
}
......@@ -98,6 +113,7 @@ public class AttributeInfo {
* @return the if the added attribute is a list of info
*/
public boolean isListAttr() {
return isListAttr;
}
......@@ -107,6 +123,7 @@ public class AttributeInfo {
* @param isList if the added attribute is a list of info
*/
public void setListAttr(boolean isList) {
isListAttr = isList;
}
......@@ -118,6 +135,7 @@ public class AttributeInfo {
* qualified manner.
*/
public boolean isQualifiedName() {
return isQualifiedName;
}
......@@ -128,7 +146,8 @@ public class AttributeInfo {
* @param isQualified if the added attribute has to be accessed in a fully
* qualified manner
*/
public void setQualifiedName(boolean isQualified) {
public void setIsQualifiedAccess(boolean isQualified) {
isQualifiedName = isQualified;
}
......@@ -138,7 +157,8 @@ public class AttributeInfo {
*
* @return import info
*/
public ImportInfo getImportInfo() {
public JavaQualifiedTypeInfo getImportInfo() {
return importInfo;
}
......@@ -147,8 +167,83 @@ public class AttributeInfo {
*
* @param importInfo import info for the attribute type
*/
public void setImportInfo(ImportInfo importInfo) {
public void setImportInfo(JavaQualifiedTypeInfo importInfo) {
this.importInfo = importInfo;
}
/**
* Create an attribute info object corresponding to the passed leaf
* 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 getAttributeInfoOfLeaf(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);
newAttr.setImportInfo(importInfo);
newAttr.setIsQualifiedAccess(getIsQualifiedAccessOrAddToImportList(
curNode, importInfo));
newAttr.setAttributeName(getCamelCase(attributeName));
newAttr.setListAttr(isListAttribute);
newAttr.setImportInfo(importInfo);
newAttr.setAttributeType(attributeType);
return newAttr;
}
/**
* Create an attribute info object corresponding to a data model node and
* return it.
*
* @param curNode current data model node for which the java code generation
* is being handled
* @param parentNode parent node in which the current node is an attribute
* @param isListNode is the current added attribute needs to be a list
* @return AttributeInfo attribute details required to add in temporary
* files
*/
public static JavaAttributeInfo getCurNodeAsAttributeInParent(
YangNode curNode, YangNode parentNode, boolean isListNode) {
JavaAttributeInfo newAttr = new JavaAttributeInfo();
// if (curNode instanceof HasJavaFileInfo) {
// throw new RuntimeException("translator data model node does not have java info");
// }
String curNodeName = ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName();
/*
* Get the import info corresponding to the attribute for import in
* generated java files or qualified access
*/
JavaQualifiedTypeInfo qualifiedTypeInfo = getQualifiedTypeInfoOfCurNode(parentNode,
curNodeName, isListNode);
newAttr.setImportInfo(qualifiedTypeInfo);
newAttr.setIsQualifiedAccess(
getIsQualifiedAccessOrAddToImportList(parentNode,
qualifiedTypeInfo));
newAttr.setAttributeName(getCamelCase(curNodeName));
newAttr.setListAttr(isListNode);
newAttr.setImportInfo(qualifiedTypeInfo);
newAttr.setAttributeType(null);
return newAttr;
}
}
......
......@@ -18,46 +18,24 @@ package org.onosproject.yangutils.translator.tojava;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangNode;
/**
* Implementation of Java code generator based on application schema.
* Abstraction of an entity which provides Code generator functionalities.
*/
public final class JavaCodeGenerator {
public interface JavaCodeGenerator {
/**
* Default constructor.
* Traverse the schema of application and generate corresponding code.
*
* @param codeGenDir code generation directory
* @throws IOException when fails to translate the data model tree
*/
private JavaCodeGenerator() {
}
void generateCodeEntry(String codeGenDir) throws IOException;
/**
* Generate Java code files corresponding to the YANG schema.
* Traverse the schema of application and generate corresponding code.
*
* @param rootNode root node of the data model tree
* @param codeGenDir code generation directory
* @throws IOException when fails to generate java code file the current node
* @throws IOException when fails to generate java code
*/
public static void generateJavaCode(YangNode rootNode, String codeGenDir) throws IOException {
YangNode curNode = rootNode;
TraversalType curTraversal = TraversalType.ROOT;
void generateCodeExit() throws IOException;
while (!(curNode == null)) {
if (curTraversal != TraversalType.PARENT) {
curNode.generateJavaCodeEntry(codeGenDir);
}
if (curTraversal != TraversalType.PARENT && curNode.getChild() != null) {
curTraversal = TraversalType.CHILD;
curNode = curNode.getChild();
} else if (curNode.getNextSibling() != null) {
curNode.generateJavaCodeExit();
curTraversal = TraversalType.SIBILING;
curNode = curNode.getNextSibling();
} else {
curNode.generateJavaCodeExit();
curTraversal = TraversalType.PARENT;
curNode = curNode.getParent();
}
}
}
}
......
/*
* 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;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangNode;
/**
* Implementation of Java code generator based on application schema.
*/
public final class JavaCodeGeneratorUtil {
/**
* Default constructor.
*/
private JavaCodeGeneratorUtil() {
}
/**
* Generate Java code files corresponding to the YANG schema.
*
* @param rootNode root node of the data model tree
* @param codeGenDir code generation directory
* @throws IOException when fails to generate java code file the current
* node
*/
public static void generateJavaCode(YangNode rootNode, String codeGenDir) throws IOException {
YangNode curNode = rootNode;
TraversalType curTraversal = TraversalType.ROOT;
while (!(curNode == null)) {
if (curTraversal != TraversalType.PARENT) {
generateCodeEntry(curNode, codeGenDir);
}
if (curTraversal != TraversalType.PARENT && curNode.getChild() != null) {
curTraversal = TraversalType.CHILD;
curNode = curNode.getChild();
} else if (curNode.getNextSibling() != null) {
generateCodeExit(curNode);
curTraversal = TraversalType.SIBILING;
curNode = curNode.getNextSibling();
} else {
generateCodeExit(curNode);
curTraversal = TraversalType.PARENT;
curNode = curNode.getParent();
}
}
}
/**
* Generates the current nodes code snippet.
*
* @param curNode current data model node for which the code needs to be
* generated
* @param codeGenDir the base directory where the code needs to be generated
* @throws IOException IO operation exception
*/
private static void generateCodeEntry(YangNode curNode,
String codeGenDir) throws IOException {
if (curNode instanceof JavaCodeGenerator) {
((JavaCodeGenerator) curNode).generateCodeEntry(codeGenDir);
} else {
throw new RuntimeException(
"Gnenerated data model node cannot be translated to target language code");
}
}
/**
* Generates the current nodes code target code from the snippet.
*
* @param curNode current data model node for which the code needs to be
* generated
* @throws IOException IO operation exception
*/
private static void generateCodeExit(YangNode curNode) throws IOException {
if (curNode instanceof JavaCodeGenerator) {
((JavaCodeGenerator) curNode).generateCodeExit();
} else {
throw new RuntimeException(
"Gnenerated data model node cannot be translated to target language code");
}
}
}
/*
* 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;
/**
* Cached java file handle, which supports the addition of member attributes and
* methods.
*/
public class JavaFileInfo {
/**
* The type(s) of java source file(s) to be generated when the cached file
* handle is closed.
*/
private int genFileTypes;
/**
* Name of the module.
*/
private String javaName;
/**
* java Package of the mapped java class.
*/
private String pkg;
/**
* File generation directory path.
*/
private String relativeFilePath;
/**
* File generation base directory path.
*/
private String codeGenDirFilePath;
/**
* Get the types of files being generated corresponding to the YANG
* definition.
*
* @return the types of files being generated corresponding to the YANG
* definition
*/
public int getGeneratedFileTypes() {
return genFileTypes;
}
/**
* Set the types of files being generated corresponding to the YANG
* definition.
*
* @param fileTypes the types of files being generated corresponding to the
* YANG definition
*/
public void setGeneratedFileTypes(int fileTypes) {
genFileTypes = fileTypes;
}
/**
* Get the java name of the node.
*
* @return the java name of node
*/
public String getJavaName() {
return javaName;
}
/**
* Set the java name of the node.
*
* @param name the java name of node
*/
public void setJavaName(String name) {
javaName = name;
}
/**
* Get the mapped java package.
*
* @return the java package
*/
public String getPackage() {
if (pkg == null) {
throw new RuntimeException("Referencing package of a generated java file which is not set");
}
return pkg;
}
/**
* Set the node's package.
*
* @param nodePackage node's package
*/
public void setPackage(String nodePackage) {
pkg = nodePackage;
}
/**
* Sets directory package path for code generation.
*
* @param path directory package path for code generation
*/
public void setPackageFilePath(String path) {
relativeFilePath = path;
}
/**
* Gets directory package path for code generation.
*
* @return directory package path for code generation
*/
public String getPackageFilePath() {
return relativeFilePath;
}
/**
* Gets base directory package path for code generation.
*
* @return directory package path for code generation
*/
public String getBaseCodeGenPath() {
return codeGenDirFilePath;
}
/**
* Sets base directory package path for code generation.
*
* @param path base directory path
*/
public void setBaseCodeGenPath(String path) {
codeGenDirFilePath = path;
}
}
/*
* 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;
import java.util.SortedSet;
import java.util.TreeSet;
import org.onosproject.yangutils.datamodel.YangNode;
/**
* Generated Java file can contain imports.
*/
public class JavaImportData {
/**
* Flag to denote if any list in imported.
*/
private boolean isListToImport;
/**
* Sorted set of import info, to be used to maintain the set of classes to
* be imported in the generated class.
*/
private SortedSet<JavaQualifiedTypeInfo> importSet;
/**
* Default constructor.
*/
public JavaImportData() {
setImportSet(new TreeSet<JavaQualifiedTypeInfo>());
}
/**
* Get if the list needs to be imported.
*
* @return true if any of the attribute needs to be maintained as a list.
*/
public boolean getIfListImported() {
return isListToImport;
}
/**
* Set the status of importing list.
*
* @param isList status to mention list is bing imported.
*/
public void setIfListImported(boolean isList) {
isListToImport = isList;
}
/**
* Get the set containing the imported class/interface info.
*
* @return the set containing the imported class/interface info
*/
public SortedSet<JavaQualifiedTypeInfo> getImportSet() {
return importSet;
}
/**
* Assign the set containing the imported class/interface info.
*
* @param importSet the set containing the imported class/interface info
*/
private void setImportSet(SortedSet<JavaQualifiedTypeInfo> importSet) {
this.importSet = importSet;
}
/**
* Add an imported class/interface info if it is not already part of the
* collection.
*
* If already part of the collection, check if the packages are same, if so
* then return true, to denote it is already in the import collection, and
* it can be accessed without qualified access. If the packages do not
* match, then do not add to the import collection, and return false to
* denote, it is not added to import collection and needs to be accessed in
* a qualified manner.
*
* @param curNode current data model node
* @param newImportInfo class/interface info being imported
* @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");
}
for (JavaQualifiedTypeInfo curImportInfo : ((HasJavaImportData) curNode).getJavaImportData().getImportSet()) {
if (curImportInfo.getClassInfo()
.contentEquals(newImportInfo.getClassInfo())) {
return curImportInfo.getPkgInfo()
.contentEquals(newImportInfo.getPkgInfo());
}
}
((HasJavaImportData) curNode).getJavaImportData().getImportSet().add(newImportInfo);
return true;
}
}
......@@ -18,12 +18,16 @@ package org.onosproject.yangutils.translator.tojava;
import java.util.Objects;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.utils.AttributesJavaDataType;
import com.google.common.base.MoreObjects;
/**
* Maintains the information about individual imports in the generated file.
*/
public class ImportInfo implements Comparable {
public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo> {
/**
* Package location where the imported class/interface is defined.
......@@ -38,7 +42,7 @@ public class ImportInfo implements Comparable {
/**
* Default constructor.
*/
public ImportInfo() {
public JavaQualifiedTypeInfo() {
}
/**
......@@ -47,6 +51,7 @@ public class ImportInfo implements Comparable {
* @return the imported package info
*/
public String getPkgInfo() {
return pkgInfo;
}
......@@ -56,6 +61,7 @@ public class ImportInfo implements Comparable {
* @param pkgInfo the imported package info
*/
public void setPkgInfo(String pkgInfo) {
this.pkgInfo = pkgInfo;
}
......@@ -65,6 +71,7 @@ public class ImportInfo implements Comparable {
* @return the imported class/interface info
*/
public String getClassInfo() {
return classInfo;
}
......@@ -74,21 +81,187 @@ public class ImportInfo implements Comparable {
* @param classInfo the imported class/interface info
*/
public void setClassInfo(String classInfo) {
this.classInfo = classInfo;
}
/**
* Get the import info for an attribute, which needs to be used for code
* generation for import or for qualified access.
*
* @param curNode current data model node for which the java file is being
* generated.
* @param attrType type of attribute being added, it will be null, when the
* child class is added as an attribute
* @param attributeName name of the attribute being added, it will used in
* import info for child class.
* @param isListAttr is the added attribute going to be used as a list
* @return return the import info for this attribute
*/
public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfLeafAttribute(YangNode curNode,
YangType<?> attrType, String attributeName,
boolean isListAttr) {
JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
if (attrType == null) {
throw new RuntimeException("missing data type of leaf " + attributeName);
}
/*
* Current leaves holder is adding a leaf info as a attribute to the
* current class.
*/
String className = AttributesJavaDataType.getJavaImportClass(attrType, isListAttr);
if (className != null) {
/*
* Corresponding to the attribute type a class needs to be imported,
* since it can be a derived type or a usage of wrapper classes.
*/
importInfo.setClassInfo(className);
String classPkg = AttributesJavaDataType.getJavaImportPackage(attrType, isListAttr, className);
if (classPkg == null) {
throw new RuntimeException("import package cannot be null when the class is used");
}
importInfo.setPkgInfo(classPkg);
} else {
/*
* The attribute does not need a class to be imported, for example
* built in java types.
*/
String dataTypeName = AttributesJavaDataType.getJavaDataType(attrType);
if (dataTypeName == null) {
throw new RuntimeException("not supported data type");
}
importInfo.setClassInfo(dataTypeName);
}
return importInfo;
}
/**
* Get the import info for an attribute, which needs to be used for code
* generation for import or for qualified access.
*
* @param curNode current data model node for which the java file is being
* generated.
* @param attributeName name of the attribute being added, it will used in
* import info for child class.
* @param isListAttr is the added attribute going to be used as a list
* @return return the import info for this attribute
*/
public static JavaQualifiedTypeInfo getQualifiedTypeInfoOfCurNode(YangNode curNode,
String attributeName, boolean isListAttr) {
JavaQualifiedTypeInfo importInfo = new JavaQualifiedTypeInfo();
if (!(curNode instanceof HasJavaFileInfo)) {
throw new RuntimeException("missing java file information to get the package details "
+ "of attribute corresponding to child node");
}
/*
* The scenario when we need to add the child class as an attribute in
* the current class. The child class is in the package of the current
* classes package with current classes name.
*/
importInfo.setClassInfo(attributeName);
importInfo.setPkgInfo((((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage() + "."
+ ((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName()).toLowerCase());
return importInfo;
}
/**
* Get if the attribute needs to be accessed in a qualified manner or not,
* if it needs to be imported, then the same needs to be done.
*
* @param curNode current cache of the data model node for which java file
* is bing generated
* @param importInfo import info for the current attribute being added
* @return status of the qualified access to the attribute
*/
public static boolean getIsQualifiedAccessOrAddToImportList(YangNode curNode,
JavaQualifiedTypeInfo importInfo) {
boolean isImportPkgEqualCurNodePkg;
if (!(curNode instanceof HasJavaFileInfo)) {
throw new RuntimeException("missing java file info for getting the qualified access");
}
if (importInfo.getClassInfo().contentEquals(
((HasJavaFileInfo) curNode).getJavaFileInfo().getJavaName())) {
/*
* if the current class name is same as the attribute class name,
* then the attribute must be accessed in a qualified manner.
*/
return true;
} else if (importInfo.getPkgInfo() != null) {
/*
* If the attribute type is having the package info, it is contender
* for import list and also need to check if it needs to be a
* qualified access.
*/
isImportPkgEqualCurNodePkg = isImportPkgEqualCurNodePkg(curNode, importInfo);
if (!isImportPkgEqualCurNodePkg) {
/*
* If the package of the attribute added is not same as the
* current class package, then it must either be imported for
* access or it must be a qualified access.
*/
if (!(curNode instanceof HasJavaImportData)) {
/*
* If the current data model node is not supposed to import
* data, then this is a usage issue and needs to be fixed.
*/
throw new RuntimeException("Current node needs to support Imports");
}
boolean isImportAdded = ((HasJavaImportData) curNode).getJavaImportData()
.addImportInfo(curNode, importInfo);
if (!isImportAdded) {
/*
* If the attribute type info is not imported, then it must
* be a qualified access.
*/
return true;
}
}
}
return false;
}
/**
* Check if the import info is same as the package of the current generated
* java file.
*
* @param curNode Java identifier of the current data model node
* @param importInfo import info for an attribute
* @return true if the import info is same as the current nodes package
* false otherwise
*/
public static boolean isImportPkgEqualCurNodePkg(
YangNode curNode, JavaQualifiedTypeInfo importInfo) {
if (!(curNode instanceof HasJavaFileInfo)) {
throw new RuntimeException("missing java file info for the data model node");
}
return ((HasJavaFileInfo) curNode).getJavaFileInfo().getPackage()
.contentEquals(importInfo.getPkgInfo()
+ "." + importInfo.getClassInfo());
}
@Override
public int hashCode() {
return Objects.hash(pkgInfo, classInfo);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof ImportInfo) {
ImportInfo other = (ImportInfo) obj;
if (obj instanceof JavaQualifiedTypeInfo) {
JavaQualifiedTypeInfo other = (JavaQualifiedTypeInfo) obj;
return Objects.equals(pkgInfo, other.pkgInfo) &&
Objects.equals(classInfo, other.classInfo);
}
......@@ -101,7 +274,8 @@ public class ImportInfo implements Comparable {
* @param importInfo matched import
* @return if equal or not
*/
public boolean exactMatch(ImportInfo importInfo) {
public boolean exactMatch(JavaQualifiedTypeInfo importInfo) {
return equals(importInfo)
&& Objects.equals(pkgInfo, importInfo.getPkgInfo())
&& Objects.equals(classInfo, importInfo.getClassInfo());
......@@ -109,6 +283,7 @@ public class ImportInfo implements Comparable {
@Override
public String toString() {
return MoreObjects.toStringHelper(getClass())
.add("pkgInfo", pkgInfo)
.add("classInfo", classInfo).toString();
......@@ -117,16 +292,11 @@ public class ImportInfo implements Comparable {
/**
* Check that there is no 2 objects with the same class name.
*
* @param o compared import info.
* @param other compared import info.
*/
@Override
public int compareTo(Object o) {
ImportInfo other;
if (o instanceof ImportInfo) {
other = (ImportInfo) o;
} else {
return -1;
}
public int compareTo(JavaQualifiedTypeInfo other) {
return getClassInfo().compareTo(other.getClassInfo());
}
......
/*
* 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.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangAugment;
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.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;
/**
* Augment information extended to support java code generation.
*/
public class YangJavaAugment extends YangAugment
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Default constructor.
*/
public YangJavaAugment() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@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
* augment info.
*
* @param codeGenDir code generation directory
* @throws IOException IO operation fail
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false);
}
/**
* Create a java file using the YANG grouping info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
}
}
/*
* 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.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangCase;
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.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;
/**
* Case information extended to support java code generation.
*/
public class YangJavaCase extends YangCase
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Default constructor.
*/
public YangJavaCase() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@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
* case info.
*
* @param codeGenDir code generation directory
* @throws IOException IO operation fail
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false);
}
/**
* Create a java file using the YANG grouping info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
}
}
/*
* 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.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangChoice;
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.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;
/**
* Choice information extended to support java code generation.
*/
public class YangJavaChoice extends YangChoice
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Default constructor.
*/
public YangJavaChoice() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@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
* case info.
*
* @param codeGenDir code generation directory
* @throws IOException IO operation fail
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
// TODO:getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false);
}
/**
* Create a java file using the YANG grouping info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
}
}
/*
* 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.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangContainer;
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.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;
/**
* Container information extended to support java code generation.
*/
public class YangJavaContainer extends YangContainer
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Default constructor.
*/
public YangJavaContainer() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@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 handle 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 operation fail
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
createPackage(absloutePath, getName());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false);
}
/**
* Create a java file using the YANG grouping info.
*
* @throws IOException IO operation fail
*/
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
}
}
/*
* 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.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangGrouping;
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.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;
/**
* Grouping information extended to support java code generation.
*/
public class YangJavaGrouping extends YangGrouping
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Default constructor.
*/
public YangJavaGrouping() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@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 operation fail
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, false);
}
/**
* Create a java file using the YANG grouping info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
}
}
/*
* 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.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangList;
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.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;
/**
* YANG List information extended to support java code generation.
*/
public class YangJavaList extends YangList
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Default constructor.
*/
public YangJavaList() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@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 operation fail
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
createPackage(absloutePath, getName());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
getTempJavaCodeFragmentFiles().addCurNodeInfoInParentTempFile(this, true);
}
/**
* Create a java file using the YANG grouping info.
*
* @throws IOException IO operation fail
*/
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
}
}
/*
* 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.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangModule;
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.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;
/**
* Module information extended to support java code generation.
*/
public class YangJavaModule extends YangModule
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Create a YANG node of module type.
*/
public YangJavaModule() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@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;
}
/**
* Generates java code for module.
*
* @param baseCodeGenDir code generation directory
* @throws IOException when fails to generate the source files
*/
@Override
public void generateCodeEntry(String baseCodeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getRootPackage(getVersion(), getNameSpace().getUri(),
getRevision().getRevDate()));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(baseCodeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
createPackage(absloutePath, getName());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
}
@Override
public void generateCodeExit() throws IOException {
generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
getTempJavaCodeFragmentFiles().close();
return;
}
}
/*
* 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.javamodel;
import java.io.IOException;
import org.onosproject.yangutils.datamodel.YangBelongsTo;
import org.onosproject.yangutils.datamodel.YangSubModule;
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.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.YangIoUtils.getAbsolutePackagePath;
/**
* Sub module information extended to support java code generation.
*/
public class YangJavaSubModule extends YangSubModule
implements JavaCodeGenerator, HasJavaFileInfo,
HasJavaImportData, HasTempJavaCodeFragmentFiles {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* File handle to maintain temporary java code fragments as per the code
* snippet types.
*/
private TempJavaCodeFragmentFiles tempFileHandle;
/**
* Default constructor.
*/
public YangJavaSubModule() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@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;
}
/**
* Get the name space of the module to which the sub module belongs to.
*
* @param belongsToInfo Information of the module to which the sub module
* belongs
* @return the name space string of the module.
*/
private String getNameSpaceFromModule(YangBelongsTo belongsToInfo) {
// TODO Auto-generated method stub
return "";
}
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
*
* @param codeGenDir code generation directory
* @throws IOException IO operation fail
*/
@Override
public void generateCodeEntry(String codeGenDir) throws IOException {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getRootPackage(getVersion(),
getNameSpaceFromModule(getBelongsTo()),
getRevision().getRevDate()));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
String absloutePath = getAbsolutePackagePath(
getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath());
setTempJavaCodeFragmentFiles(new TempJavaCodeFragmentFiles(
getJavaFileInfo().getGeneratedFileTypes(), absloutePath,
getJavaFileInfo().getJavaName()));
getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
}
/**
* Create a java file using the YANG grouping info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
}
}
/*
* 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.javamodel;
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.JavaCodeGenerator;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaImportData;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
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;
/**
* Type define information extended to support java code generation.
*/
public class YangJavaTypeDef extends YangTypeDef
implements JavaCodeGenerator, HasJavaFileInfo, HasJavaImportData {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* Default constructor.
*/
public YangJavaTypeDef() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@Override
public void setJavaImportData(JavaImportData javaImportData) {
this.javaImportData = javaImportData;
}
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
*
* @param codeGenDir code generation directory
*/
@Override
public void generateCodeEntry(String codeGenDir) {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
// TODO: generate type define temporary files
}
/**
* Create a java file using the YANG grouping info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
}
}
/*
* 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.javamodel;
import org.onosproject.yangutils.datamodel.YangUses;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.HasJavaImportData;
import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaImportData;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_INTERFACE_WITH_BUILDER;
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;
/**
* Uses information extended to support java code generation.
*/
public class YangJavaUses extends YangUses implements JavaCodeGenerator, HasJavaFileInfo, HasJavaImportData {
/**
* Contains the information of the java file being generated.
*/
private JavaFileInfo javaFileInfo;
/**
* Contains information of the imports to be inserted in the java file
* generated.
*/
private JavaImportData javaImportData;
/**
* Default constructor.
*/
public YangJavaUses() {
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
}
/**
* Get the generated java file information.
*
* @return generated java file information
*/
@Override
public JavaFileInfo getJavaFileInfo() {
if (javaFileInfo == null) {
throw new RuntimeException("Missing java info in java datamodel node");
}
return javaFileInfo;
}
/**
* Set the java file info object.
*
* @param javaInfo java file info object
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
/**
* Get the data of java imports to be included in generated file.
*
* @return data of java imports to be included in generated file
*/
@Override
public JavaImportData getJavaImportData() {
return javaImportData;
}
/**
* Set the data of java imports to be included in generated file.
*
* @param javaImportData data of java imports to be included in generated
* file
*/
@Override
public void setJavaImportData(JavaImportData javaImportData) {
this.javaImportData = javaImportData;
}
/**
* Prepare the information for java code generation corresponding to YANG
* container info.
*
* @param codeGenDir code generation directory
*/
@Override
public void generateCodeEntry(String codeGenDir) {
getJavaFileInfo().setJavaName(getCaptialCase(getCamelCase(getName())));
getJavaFileInfo().setPackage(getCurNodePackage(this));
getJavaFileInfo().setPackageFilePath(
getPackageDirPathFromJavaJPackage(getJavaFileInfo().getPackage()));
getJavaFileInfo().setBaseCodeGenPath(codeGenDir);
//TODO:addCurNodeLeavesInfoToTempFiles(this);
//TODO:addCurNodeInfoInParentTempFile(this, false);
}
/**
* Create a java file using the YANG grouping info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
}
}
/*
* 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.
*/
/**
* Maintains application's schema mapped to java classes / interfaces.
*/
package org.onosproject.yangutils.translator.tojava.javamodel;
......@@ -16,8 +16,12 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.util.Set;
import java.util.TreeSet;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import org.onosproject.yangutils.utils.UtilConstants;
/**
......@@ -25,6 +29,8 @@ import org.onosproject.yangutils.utils.UtilConstants;
*/
public final class AttributesJavaDataType {
private static Set<JavaQualifiedTypeInfo> importInfo = new TreeSet<>();
/**
* Default constructor.
*/
......@@ -32,6 +38,24 @@ public final class AttributesJavaDataType {
}
/**
* Returns import info.
*
* @return import info
*/
public static Set<JavaQualifiedTypeInfo> getImportInfo() {
return importInfo;
}
/**
* Adds import info to the import info set.
*
* @param importData import info
*/
public static void addImportInfo(JavaQualifiedTypeInfo importData) {
getImportInfo().add(importData);
}
/**
* Returns java type.
*
* @param yangType YANG type
......@@ -134,7 +158,8 @@ public final class AttributesJavaDataType {
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
//TODO:DERIVED
return JavaIdentifierSyntax
.getCaptialCase(JavaIdentifierSyntax.getCamelCase(yangType.getDataTypeName()));
}
} else {
if (type.equals(YangDataTypes.UINT64)) {
......@@ -160,7 +185,8 @@ public final class AttributesJavaDataType {
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
//TODO:DERIVED
return JavaIdentifierSyntax
.getCaptialCase(JavaIdentifierSyntax.getCamelCase(yangType.getDataTypeName()));
}
}
return null;
......@@ -171,9 +197,10 @@ public final class AttributesJavaDataType {
*
* @param yangType YANG type
* @param isListAttr if the attribute is of list type
* @param classInfo java import class info
* @return java import package
*/
public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr) {
public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr, String classInfo) {
YangDataTypes type = yangType.getDataType();
if (isListAttr) {
......@@ -208,7 +235,11 @@ public final class AttributesJavaDataType {
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
//TODO:DERIVED
for (JavaQualifiedTypeInfo imports : getImportInfo()) {
if (imports.getClassInfo().equals(classInfo)) {
return imports.getPkgInfo();
}
}
}
} else {
......@@ -235,7 +266,11 @@ public final class AttributesJavaDataType {
} else if (type.equals(YangDataTypes.INSTANCE_IDENTIFIER)) {
//TODO:INSTANCE_IDENTIFIER
} else if (type.equals(YangDataTypes.DERIVED)) {
//TODO:DERIVED
for (JavaQualifiedTypeInfo imports : getImportInfo()) {
if (imports.getClassInfo().equals(classInfo)) {
return imports.getPkgInfo();
}
}
}
}
return null;
......
......@@ -16,7 +16,7 @@
package org.onosproject.yangutils.translator.tojava.utils;
import org.onosproject.yangutils.translator.GeneratedFileType;
import org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType;
import org.onosproject.yangutils.utils.UtilConstants;
/**
......@@ -44,19 +44,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 & GeneratedFileType.INTERFACE_MASK) != 0) {
if ((genFileTypes & GeneratedJavaFileType.INTERFACE_MASK) != 0) {
return getInterfaceDefinition(yangName);
} else if ((genFileTypes & GeneratedFileType.BUILDER_CLASS_MASK) != 0) {
} else if ((genFileTypes & GeneratedJavaFileType.BUILDER_CLASS_MASK) != 0) {
return getBuilderClassDefinition(yangName);
} else if ((genFileTypes & GeneratedFileType.IMPL_CLASS_MASK) != 0) {
} else if ((genFileTypes & GeneratedJavaFileType.IMPL_CLASS_MASK) != 0) {
return getImplClassDefinition(yangName);
} else if ((genFileTypes & GeneratedFileType.BUILDER_INTERFACE_MASK) != 0) {
} else if ((genFileTypes & GeneratedJavaFileType.BUILDER_INTERFACE_MASK) != 0) {
return getBuilderInterfaceDefinition(yangName);
} else if ((genFileTypes & GeneratedFileType.GENERATE_TYPEDEF_CLASS) != 0) {
} else if ((genFileTypes & GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS) != 0) {
return getTypeDefClassDefinition(yangName);
}
......@@ -112,7 +112,7 @@ public final class ClassDefinitionGenerator {
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 + UtilConstants.NEW_LINE;
+ UtilConstants.OPEN_CURLY_BRACKET + UtilConstants.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;
}
}