[ONOS-4583] Union defect fix.
Change-Id: Ic31866b9a1b7bd5d8209d5d22f4292ab9c79a118
diff --git a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaTypeFragmentFiles.java b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaTypeFragmentFiles.java
index 1d31d20..8e05be5 100644
--- a/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaTypeFragmentFiles.java
+++ b/plugin/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaTypeFragmentFiles.java
@@ -24,29 +24,37 @@
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeHolder;
+import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaType;
import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
+import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT32;
+import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.INT64;
+import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT16;
+import static org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes.UINT32;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
+import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
-import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
-import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
+import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.INT_TYPE_CONFLICT;
+import static org.onosproject.yangutils.translator.tojava.utils.ValidatorTypeForUnionTypes.LONG_TYPE_CONFLICT;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
-import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.createPackage;
+import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.closeFile;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getCamelCase;
/**
- * Represents implementation of java data type code fragments temporary implementations.
- * Maintains the temp files required specific for user defined data type java snippet generation.
+ * Represents implementation of java data type code fragments temporary implementations. Maintains the temp files
+ * required specific for user defined data type java snippet generation.
*/
public class TempJavaTypeFragmentFiles
extends TempJavaFragmentFiles {
@@ -67,12 +75,31 @@
private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
/**
- * File name for generated class file for special type like union, typedef
- * suffix.
+ * File name for generated class file for special type like union, typedef suffix.
*/
private static final String UNION_TYPE_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
/**
+ * Integer index in type list.
+ */
+ private int intIndex = 0;
+
+ /**
+ * UInt index in type list.
+ */
+ private int uIntIndex = 0;
+
+ /**
+ * long index in type list.
+ */
+ private int longIndex = 0;
+
+ /**
+ * ULong index in type list.
+ */
+ private int uLongIndex = 0;
+
+ /**
* Temporary file handle for of string method of class.
*/
private File ofStringImplTempFileHandle;
@@ -93,12 +120,32 @@
private File typeClassJavaFileHandle;
/**
+ * Java attribute for int.
+ */
+ private JavaAttributeInfo intAttribute;
+
+ /**
+ * Java attribute for long.
+ */
+ private JavaAttributeInfo longAttribute;
+
+ /**
+ * Java attribute for uInt.
+ */
+ private JavaAttributeInfo uIntAttribute;
+
+ /**
+ * Java attribute for uLong.
+ */
+ private JavaAttributeInfo uLongAttribute;
+
+ /**
* Creates an instance of temporary java code fragment.
*
* @param javaFileInfo generated java file info
* @throws IOException when fails to create new file handle
*/
- public TempJavaTypeFragmentFiles(JavaFileInfo javaFileInfo)
+ TempJavaTypeFragmentFiles(JavaFileInfo javaFileInfo)
throws IOException {
super(javaFileInfo);
@@ -129,8 +176,7 @@
/**
* Sets type class constructor method's temporary file handle.
*
- * @param constructorForTypeTempFileHandle type class constructor method's
- * temporary file handle
+ * @param constructorForTypeTempFileHandle type class constructor method's temporary file handle
*/
private void setConstructorForTypeTempFileHandle(File constructorForTypeTempFileHandle) {
this.constructorForTypeTempFileHandle = constructorForTypeTempFileHandle;
@@ -141,7 +187,7 @@
*
* @return java file handle for typedef class file
*/
- File getTypedefClassJavaFileHandle() {
+ private File getTypedefClassJavaFileHandle() {
return typedefClassJavaFileHandle;
}
@@ -159,7 +205,7 @@
*
* @return java file handle for type class file
*/
- File getTypeClassJavaFileHandle() {
+ private File getTypeClassJavaFileHandle() {
return typeClassJavaFileHandle;
}
@@ -185,23 +231,20 @@
/**
* Set of string method's temporary file handle.
*
- * @param ofStringImplTempFileHandle of string method's temporary file
- * handle
+ * @param ofStringImplTempFileHandle of string method's temporary file handle
*/
private void setOfStringImplTempFileHandle(File ofStringImplTempFileHandle) {
this.ofStringImplTempFileHandle = ofStringImplTempFileHandle;
}
/**
- * Adds all the type in the current data model node as part of the generated
- * temporary file.
+ * Adds all the type in the current data model node as part of the generated temporary file.
*
- * @param yangTypeHolder YANG java data model node which has type info, eg union /
- * typedef
- * @param pluginConfig plugin configurations for naming conventions
+ * @param yangTypeHolder YANG java data model node which has type info, eg union / typedef
+ * @param pluginConfig plugin configurations for naming conventions
* @throws IOException IO operation fail
*/
- public void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
+ void addTypeInfoToTempFiles(YangTypeHolder yangTypeHolder, YangPluginConfig pluginConfig)
throws IOException {
List<YangType<?>> typeList = yangTypeHolder.getTypeList();
@@ -210,49 +253,165 @@
if (!(yangType instanceof YangJavaType)) {
throw new TranslatorException("Type does not have Java info");
}
- YangJavaType<?> javaType = (YangJavaType<?>) yangType;
- javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
- String typeName = javaType.getDataTypeName();
- typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
- JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
- javaType.getJavaQualifiedInfo(),
- typeName, javaType,
- getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
- false);
- addJavaSnippetInfoToApplicableTempFiles((YangNode) yangTypeHolder, javaAttributeInfo,
- pluginConfig);
+ JavaAttributeInfo javaAttributeInfo = getAttributeForType(yangType, pluginConfig);
+ addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo,
+ pluginConfig, typeList);
}
+ addTypeConstructor(pluginConfig);
+ addMethodsInConflictCase(pluginConfig);
}
}
/**
- * Adds the new attribute info to the target generated temporary files for
- * union class.
+ * Returns java attribute.
*
- * @param hasType the node for which the type is being added as an attribute
- * @param javaAttributeInfo the attribute info that needs to be added to
- * temporary files
+ * @param yangType YANG type
* @param pluginConfig plugin configurations
+ * @return java attribute
+ */
+ private JavaAttributeInfo getAttributeForType(YangType yangType, YangPluginConfig pluginConfig) {
+ YangJavaType<?> javaType = (YangJavaType<?>) yangType;
+ javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
+ String typeName = javaType.getDataTypeName();
+ typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
+ return getAttributeInfoForTheData(
+ javaType.getJavaQualifiedInfo(),
+ typeName, javaType,
+ getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
+ false);
+ }
+
+ /**
+ * Adds the new attribute info to the target generated temporary files for union class.
+ *
+ * @param javaAttributeInfo the attribute info that needs to be added to temporary files
+ * @param pluginConfig plugin configurations
+ * @param typeList type list
* @throws IOException IO operation fail
*/
- private void addJavaSnippetInfoToApplicableTempFiles(YangNode hasType, JavaAttributeInfo javaAttributeInfo,
- YangPluginConfig pluginConfig)
+ private void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo javaAttributeInfo,
+ YangPluginConfig pluginConfig, List<YangType<?>> typeList)
throws IOException {
+ YangDataTypes attrType = javaAttributeInfo.getAttributeType().getDataType();
+
+ if (attrType == INT32 || attrType == UINT16) {
+ boolean isIntConflict = validateForConflictingIntTypes(typeList);
+ javaAttributeInfo.setIntConflict(isIntConflict);
+ updateAttributeCondition(javaAttributeInfo);
+ if (!isIntConflict) {
+ addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
+ }
+ } else if (attrType == INT64 || attrType == UINT32) {
+ boolean isLongConflict = validateForConflictingLongTypes(typeList);
+ javaAttributeInfo.setLongConflict(isLongConflict);
+ updateAttributeCondition(javaAttributeInfo);
+ if (!isLongConflict) {
+ addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
+ }
+ } else {
+ addMethodsWhenNoConflictingTypes(javaAttributeInfo, pluginConfig);
+ }
super.addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo, pluginConfig);
+ }
+
+ /**
+ * Adds of method and constructor when there is no conflictiing types.
+ *
+ * @param javaAttributeInfo java attribute info
+ * @param pluginConfig plugin configurations
+ * @throws IOException when fails to do IO opearions
+ */
+ private void addMethodsWhenNoConflictingTypes(JavaAttributeInfo javaAttributeInfo,
+ YangPluginConfig pluginConfig) throws IOException {
if ((getGeneratedTempFiles() & OF_STRING_IMPL_MASK) != 0) {
addOfStringMethod(javaAttributeInfo, pluginConfig);
}
+
if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
addTypeConstructor(javaAttributeInfo, pluginConfig);
}
}
/**
+ * Adds of, getter and from string method in conflict cases.
+ *
+ * @param pluginConfig plugin configurations
+ * @throws IOException when fails to do IO operations
+ */
+ private void addMethodsInConflictCase(YangPluginConfig pluginConfig) throws IOException {
+ JavaAttributeInfo attr = getIntAttribute();
+ if (attr != null) {
+ attr = getUIntAttribute();
+ }
+ if (attr != null) {
+ if (attr.isIntConflict()) {
+ if (getIntIndex() < getUIntIndex()) {
+ appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getIntAttribute(),
+ getGeneratedJavaClassName(), pluginConfig)
+ + NEW_LINE);
+ addGetterImpl(getIntAttribute(), pluginConfig);
+ addFromStringMethod(getIntAttribute(), pluginConfig);
+ } else {
+ appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getUIntAttribute(),
+ getGeneratedJavaClassName(), pluginConfig)
+ + NEW_LINE);
+ addGetterImpl(getUIntAttribute(), pluginConfig);
+ addFromStringMethod(getUIntAttribute(), pluginConfig);
+ }
+ }
+ }
+ attr = getLongAttribute();
+ if (attr != null) {
+ attr = getULongAttribute();
+ }
+ if (attr != null) {
+ if (attr.isLongConflict()) {
+ if (getLongIndex() < getULongIndex()) {
+ appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getLongAttribute(),
+ getGeneratedJavaClassName(), pluginConfig)
+ + NEW_LINE);
+ addGetterImpl(getLongAttribute(), pluginConfig);
+ addFromStringMethod(getLongAttribute(), pluginConfig);
+ } else {
+ appendToFile(getOfStringImplTempFileHandle(), getOfMethodStringAndJavaDoc(getULongAttribute(),
+ getGeneratedJavaClassName(), pluginConfig)
+ + NEW_LINE);
+ addGetterImpl(getULongAttribute(), pluginConfig);
+ addFromStringMethod(getULongAttribute(), pluginConfig);
+ }
+ }
+ }
+ }
+
+ /**
+ * Adds from string method for conflict case.
+ *
+ * @param newAttrInfo new attribute
+ * @param pluginConfig plugin configurations
+ * @throws IOException when fails to do IO operations
+ */
+ private void addFromStringMethod(JavaAttributeInfo newAttrInfo, YangPluginConfig pluginConfig) throws IOException {
+
+ JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(newAttrInfo,
+ pluginConfig.getConflictResolver());
+ /*
+ * Create a new java attribute info with qualified information of
+ * wrapper classes.
+ */
+ JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
+ newAttrInfo.getAttributeName(),
+ newAttrInfo.getAttributeType(),
+ getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
+
+ addFromStringMethod(newAttrInfo, fromStringAttributeInfo);
+ }
+
+ /**
* Adds type constructor.
*
- * @param attr attribute info
+ * @param attr attribute info
* @param pluginConfig plugin configurations
* @throws IOException when fails to append to temporary file
*/
@@ -263,9 +422,45 @@
}
/**
+ * Adds type constructor.
+ *
+ * @param pluginConfig plugin configurations
+ * @throws IOException when fails to append to temporary file
+ */
+ private void addTypeConstructor(YangPluginConfig pluginConfig)
+ throws IOException {
+ JavaAttributeInfo attr = getIntAttribute();
+ if (attr != null) {
+ attr = getUIntAttribute();
+ }
+ if (attr != null) {
+ if (attr.isIntConflict()) {
+ appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
+ getIntAttribute(),
+ getUIntAttribute(), getGeneratedJavaClassName(), pluginConfig, INT_TYPE_CONFLICT,
+ getIntIndex()
+ < getUIntIndex()) + NEW_LINE);
+ }
+ }
+ attr = getLongAttribute();
+ if (attr != null) {
+ attr = getULongAttribute();
+ }
+ if (attr != null) {
+ if (attr.isLongConflict()) {
+ appendToFile(getConstructorForTypeTempFileHandle(), getTypeConstructorStringAndJavaDoc(
+ getLongAttribute(),
+ getULongAttribute(), getGeneratedJavaClassName(), pluginConfig, LONG_TYPE_CONFLICT,
+ getLongIndex()
+ < getULongIndex()) + NEW_LINE);
+ }
+ }
+ }
+
+ /**
* Adds of string for type.
*
- * @param attr attribute info
+ * @param attr attribute info
* @param pluginConfig plugin configurations
* @throws IOException when fails to append to temporary file
*/
@@ -279,22 +474,19 @@
/**
* Removes all temporary file handles.
*
- * @param isErrorOccurred when translator fails to generate java files we
- * need to close all open file handles include temporary files
- * and java files.
+ * @param isErrorOccurred flag to tell translator that error has occurred while file generation
* @throws IOException when failed to delete the temporary files
*/
@Override
public void freeTemporaryResources(boolean isErrorOccurred)
throws IOException {
- boolean isError = isErrorOccurred;
if ((getGeneratedJavaFiles() & GENERATE_TYPEDEF_CLASS) != 0) {
- closeFile(getTypedefClassJavaFileHandle(), isError);
+ closeFile(getTypedefClassJavaFileHandle(), isErrorOccurred);
}
if ((getGeneratedJavaFiles() & GENERATE_UNION_CLASS) != 0) {
- closeFile(getTypeClassJavaFileHandle(), isError);
+ closeFile(getTypeClassJavaFileHandle(), isErrorOccurred);
}
if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
@@ -315,7 +507,7 @@
* Constructs java code exit.
*
* @param fileType generated file type
- * @param curNode current YANG node
+ * @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
@Override
@@ -350,4 +542,219 @@
*/
freeTemporaryResources(false);
}
+
+ /**
+ * Returns int type index from type list.
+ *
+ * @return int type index from type list
+ */
+ public int getIntIndex() {
+ return intIndex;
+ }
+
+ /**
+ * Sets int type index from type list.
+ *
+ * @param intIndex int type index from type list.
+ */
+ private void setIntIndex(int intIndex) {
+ this.intIndex = intIndex;
+ }
+
+ /**
+ * Returns uInt type index from type list.
+ *
+ * @return uInt type index from type list
+ */
+ public int getUIntIndex() {
+ return uIntIndex;
+ }
+
+ /**
+ * Sets uInt type index from type list.
+ *
+ * @param uIntIndex uInt type index from type list.
+ */
+ private void setUIntIndex(int uIntIndex) {
+ this.uIntIndex = uIntIndex;
+ }
+
+ /**
+ * Returns long type index from type list.
+ *
+ * @return long type index from type list
+ */
+ public int getLongIndex() {
+ return longIndex;
+ }
+
+ /**
+ * Sets long type index from type list.
+ *
+ * @param longIndex long type index from type list.
+ */
+ private void setLongIndex(int longIndex) {
+ this.longIndex = longIndex;
+ }
+
+ /**
+ * Returns uLong type index from type list.
+ *
+ * @return uLong type index from type list
+ */
+ public int getULongIndex() {
+ return uLongIndex;
+ }
+
+ /**
+ * Sets uLong type index from type list.
+ *
+ * @param uLongIndex uLong type index from type list.
+ */
+ private void setULongIndex(int uLongIndex) {
+ this.uLongIndex = uLongIndex;
+ }
+
+ /**
+ * Validates conflict for int and uInt.
+ *
+ * @param typeList type list
+ * @return true if conflict is there
+ */
+ private boolean validateForConflictingIntTypes(List<YangType<?>> typeList) {
+ boolean isIntPresent = false;
+ boolean isUIntPresent = false;
+ for (YangType type : typeList) {
+ if (type.getDataType().equals(INT32)) {
+ setIntIndex(typeList.indexOf(type));
+ isIntPresent = true;
+ }
+ if (type.getDataType().equals(UINT16)) {
+ setUIntIndex(typeList.indexOf(type));
+ isUIntPresent = true;
+ }
+ }
+
+ return isIntPresent && isUIntPresent;
+ }
+
+ /**
+ * Validates conflict for long and uLong.
+ *
+ * @param typeList type list
+ * @return true if conflict is there
+ */
+ private boolean validateForConflictingLongTypes(List<YangType<?>> typeList) {
+ boolean isLongPresent = false;
+ boolean isULongPresent = false;
+ for (YangType type : typeList) {
+ if (type.getDataType().equals(INT64)) {
+ setLongIndex(typeList.indexOf(type));
+ isLongPresent = true;
+ }
+ if (type.getDataType().equals(UINT32)) {
+ setULongIndex(typeList.indexOf(type));
+ isULongPresent = true;
+ }
+ }
+
+ return isLongPresent && isULongPresent;
+ }
+
+ /**
+ * Updates attribute info in case of conflicts.
+ *
+ * @param javaAttributeInfo java attribute info
+ */
+ private void updateAttributeCondition(JavaAttributeInfo javaAttributeInfo) {
+
+ if (javaAttributeInfo.isIntConflict()) {
+ if (javaAttributeInfo.getAttributeType().getDataType() == UINT16) {
+ setUIntAttribute(javaAttributeInfo);
+ } else if (javaAttributeInfo.getAttributeType().getDataType() == INT32) {
+ setIntAttribute(javaAttributeInfo);
+ }
+
+ }
+ if (javaAttributeInfo.isLongConflict()) {
+ if (javaAttributeInfo.getAttributeType().getDataType() == UINT32) {
+ setULongAttribute(javaAttributeInfo);
+ } else if (javaAttributeInfo.getAttributeType().getDataType() == INT64) {
+ setLongAttribute(javaAttributeInfo);
+ }
+
+ }
+ }
+
+ /**
+ * Returns attribute for int.
+ *
+ * @return attribute for int
+ */
+ public JavaAttributeInfo getIntAttribute() {
+ return intAttribute;
+ }
+
+ /**
+ * Sets attribute for int.
+ *
+ * @param intAttribute attribute for int
+ */
+ private void setIntAttribute(JavaAttributeInfo intAttribute) {
+ this.intAttribute = intAttribute;
+ }
+
+ /**
+ * Returns attribute for long.
+ *
+ * @return attribute for long
+ */
+ public JavaAttributeInfo getLongAttribute() {
+ return longAttribute;
+ }
+
+ /**
+ * Sets attribute for long.
+ *
+ * @param longAttribute attribute for long
+ */
+ private void setLongAttribute(JavaAttributeInfo longAttribute) {
+ this.longAttribute = longAttribute;
+ }
+
+ /**
+ * Returns attribute for uInt.
+ *
+ * @return attribute for uInt
+ */
+ public JavaAttributeInfo getUIntAttribute() {
+ return uIntAttribute;
+ }
+
+ /**
+ * Sets attribute for uInt.
+ *
+ * @param uIntAttribute attribute for uInt
+ */
+ private void setUIntAttribute(JavaAttributeInfo uIntAttribute) {
+ this.uIntAttribute = uIntAttribute;
+ }
+
+ /**
+ * Returns attribute for uLong.
+ *
+ * @return attribute for uLong
+ */
+ public JavaAttributeInfo getULongAttribute() {
+ return uLongAttribute;
+ }
+
+ /**
+ * Sets attribute for uLong.
+ *
+ * @param uLongAttribute attribute for uLong
+ */
+ private void setULongAttribute(JavaAttributeInfo uLongAttribute) {
+ this.uLongAttribute = uLongAttribute;
+ }
}