[ONOS-4073] YANG enum translator implementation.
Change-Id: I129eaec0c8f81c674a994a579650f18499340052
diff --git a/src/main/java/org/onosproject/yangutils/datamodel/utils/YangDataModelFactory.java b/src/main/java/org/onosproject/yangutils/datamodel/utils/YangDataModelFactory.java
index 110bd11..686360e 100644
--- a/src/main/java/org/onosproject/yangutils/datamodel/utils/YangDataModelFactory.java
+++ b/src/main/java/org/onosproject/yangutils/datamodel/utils/YangDataModelFactory.java
@@ -35,6 +35,7 @@
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.YangJavaEnumeration;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGrouping;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaLeaf;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaList;
@@ -346,4 +347,22 @@
}
}
}
+
+ /**
+ * Returns 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 YangJavaEnumeration getYangEnumerationNode(GeneratedLanguage targetLanguage) {
+ switch (targetLanguage) {
+ case JAVA_GENERATION: {
+ return new YangJavaEnumeration();
+ }
+ default: {
+ throw new TranslatorException("Only YANG to Java is supported.");
+ }
+ }
+ }
}
diff --git a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumerationListener.java b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumerationListener.java
index c1703c8..e822c14 100644
--- a/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumerationListener.java
+++ b/src/main/java/org/onosproject/yangutils/parser/impl/listeners/EnumerationListener.java
@@ -55,6 +55,8 @@
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.utils.YangConstructType;
+import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
+import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangEnumerationNode;
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;
@@ -99,7 +101,7 @@
checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUMERATION_DATA, "", ENTRY);
if (listener.getParsedDataStack().peek() instanceof YangType) {
- YangEnumeration enumerationNode = new YangEnumeration();
+ YangEnumeration enumerationNode = getYangEnumerationNode(JAVA_GENERATION);
Parsable typeData = listener.getParsedDataStack().pop();
// Check for stack to be non empty.
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedJavaFileType.java b/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedJavaFileType.java
index 4be1a65..17b0d57 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedJavaFileType.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedJavaFileType.java
@@ -57,6 +57,11 @@
public static final int GENERATE_UNION_CLASS = 32;
/**
+ * Java class corresponding to YANG enumeration.
+ */
+ public static final int GENERATE_ENUM_CLASS = 64;
+
+ /**
* Creates an instance of generate java file type.
*/
private GeneratedJavaFileType() {
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedTempFileType.java b/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedTempFileType.java
index ae22f07..f64579b 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedTempFileType.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/GeneratedTempFileType.java
@@ -82,6 +82,11 @@
public static final int UNION_FROM_STRING_IMPL_MASK = 2048;
/**
+ * Enum implementation of class.
+ */
+ public static final int ENUM_IMPL_MASK = 4096;
+
+ /**
* Creates an instance of generated temp file type.
*/
private GeneratedTempFileType() {
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/JavaAttributeInfo.java b/src/main/java/org/onosproject/yangutils/translator/tojava/JavaAttributeInfo.java
index b02b4e2..44e8ab2 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/JavaAttributeInfo.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/JavaAttributeInfo.java
@@ -124,6 +124,29 @@
}
/**
+ * Creates an attribute info object corresponding to the passed enumeration attribute
+ * information and return it.
+ *
+ * @param curNode current data model node for which the java file is being
+ * generated
+ * @param attributeName attribute name
+ * @return AttributeInfo attribute details required to add in temporary
+ * files
+ */
+ public static JavaAttributeInfo getAttributeInfoOfEnumAttribute(YangNode curNode, String attributeName) {
+
+ 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(curNode,
+ curNodeName, false);
+
+ return getAttributeInfoForTheData(qualifiedTypeInfo, attributeName, null, curNode, false);
+ }
+ /**
* Returns the data type info of attribute.
*
* @return the data type info of attribute
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaCodeFragmentFiles.java b/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaCodeFragmentFiles.java
index b66e253..efc106e 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaCodeFragmentFiles.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/TempJavaCodeFragmentFiles.java
@@ -20,7 +20,11 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Set;
+
import org.onosproject.yangutils.datamodel.HasType;
+import org.onosproject.yangutils.datamodel.YangEnum;
+import org.onosproject.yangutils.datamodel.YangEnumeration;
import org.onosproject.yangutils.datamodel.YangLeaf;
import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangLeavesHolder;
@@ -28,8 +32,10 @@
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.translator.exception.TranslatorException;
+import static org.onosproject.yangutils.datamodel.YangNodeType.MODULE_NODE;
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_ENUM_CLASS;
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.GeneratedJavaFileType.IMPL_CLASS_MASK;
@@ -37,6 +43,7 @@
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
+import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
@@ -46,14 +53,17 @@
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.SETTER_FOR_INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.TO_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.UNION_FROM_STRING_IMPL_MASK;
+import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfEnumAttribute;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfLeaf;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoOfType;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getCurNodeAsAttributeInParent;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getFromStringAttributeInfo;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaAttributeDefination;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefClose;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateBuilderInterfaceFile;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile;
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.generateTypeDefClassFile;
@@ -94,13 +104,14 @@
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.INTERFACE;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
+import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE_INFO_JAVADOC_OF_CHILD;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.readAppendFile;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.OF_METHOD;
-import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.clean;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.mergeJavaFiles;
@@ -203,6 +214,11 @@
private static final String OF_STRING_METHOD_FILE_NAME = "OfString";
/**
+ * File name for temporary enum class.
+ */
+ private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass";
+
+ /**
* File name for construction for special type like union, typedef.
*/
private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
@@ -238,6 +254,11 @@
private static final String TYPEDEF_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
/**
+ * File name for enum class file name suffix.
+ */
+ private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
+
+ /**
* 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;
@@ -318,6 +339,11 @@
private File toStringImplTempFileHandle;
/**
+ * Temporary file handle for enum class file.
+ */
+ private File enumClassTempFileHandle;
+
+ /**
* Temporary file handle for of string method of class.
*/
private File ofStringImplTempFileHandle;
@@ -343,10 +369,20 @@
private YangNode curYangNode;
/**
+ * Current enum's value.
+ */
+ private int enumValue;
+
+ /**
* Is attribute added.
*/
private boolean isAttributePresent = false;
+ /*
+ * Java file handle for enum class.
+ */
+ private File enumClassJavaFileHandle;
+
/**
* Creates an instance of temporary java code fragment.
*
@@ -432,7 +468,16 @@
generatedTempFiles |= TO_STRING_IMPL_MASK;
generatedTempFiles |= UNION_FROM_STRING_IMPL_MASK;
}
+ /**
+ * Initialize enum when generation file type matches to enum class mask.
+ */
+ if ((genFileType & GENERATE_ENUM_CLASS) != 0) {
+ generatedTempFiles |= ENUM_IMPL_MASK;
+ }
+ /**
+ * Set temporary file handles.
+ */
if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
setAttributesTempFileHandle(getTemporaryFileHandle(ATTRIBUTE_FILE_NAME));
}
@@ -467,7 +512,9 @@
if ((generatedTempFiles & TO_STRING_IMPL_MASK) != 0) {
setToStringImplTempFileHandle(getTemporaryFileHandle(TO_STRING_METHOD_FILE_NAME));
}
-
+ if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
+ setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME));
+ }
if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
}
@@ -572,6 +619,25 @@
}
/**
+ * Returns enum class java file handle.
+ *
+ * @return enum class java file handle
+ */
+ private File getEnumClassJavaFileHandle() {
+ return enumClassJavaFileHandle;
+ }
+
+ /**
+ * Sets enum class java file handle.
+ *
+ * @param enumClassJavaFileHandle enum class java file handle
+ */
+
+ private void setEnumClassJavaFileHandle(File enumClassJavaFileHandle) {
+ this.enumClassJavaFileHandle = enumClassJavaFileHandle;
+ }
+
+ /**
* Returns java file handle for type class file.
*
* @return java file handle for type class file
@@ -752,6 +818,25 @@
}
/**
+ * Returns temporary file handle for enum class file.
+ *
+ * @return temporary file handle for enum class file
+ */
+ public File getEnumClassTempFileHandle() {
+ return enumClassTempFileHandle;
+ }
+
+ /**
+ * Sets temporary file handle for enum class file.
+ *
+ * @param enumClassTempFileHandle temporary file handle for enum class file
+ */
+
+ private void setEnumClassTempFileHandle(File enumClassTempFileHandle) {
+ this.enumClassTempFileHandle = enumClassTempFileHandle;
+ }
+
+ /**
* Returns of string method's temporary file handle.
*
* @return of string method's temporary file handle
@@ -846,6 +931,24 @@
}
/**
+ * Returns enum's value.
+ *
+ * @return enum's value
+ */
+ public int getEnumValue() {
+ return enumValue;
+ }
+
+ /**
+ * Sets enum's value.
+ *
+ * @param enumValue enum's value
+ */
+ public void setEnumValue(int enumValue) {
+ this.enumValue = enumValue;
+ }
+
+ /**
* Returns list of classes to be extended by generated files.
*
* @return list of classes to be extended by generated files
@@ -1038,6 +1141,17 @@
}
/**
+ * Adds enum class attributes to temporary file.
+ *
+ * @param curEnum current YANG enum
+ * @throws IOException when fails to do IO operations.
+ */
+ private void addAttributesForEnumClass(JavaAttributeInfo curEnumInfo) throws IOException {
+ appendToFile(getEnumClassTempFileHandle(),
+ generateEnumAttributeString(curEnumInfo.getAttributeName(), getEnumValue()));
+ }
+
+ /**
* Add from string method for union class.
*
* @param javaAttributeInfo type attribute info
@@ -1080,7 +1194,6 @@
* @throws IOException when fails to create new file handle
*/
private File getJavaFileHandle(String fileName) throws IOException {
- createPackage(absoluteDirPath, getJavaFileInfo().getJavaName());
return getFileObject(getDirPath(), fileName, JAVA_FILE_EXTENSION, getJavaFileInfo());
}
@@ -1271,6 +1384,26 @@
}
/**
+ * Adds enum attributes to temporary files.
+ *
+ * @param curNode current YANG node
+ * @throws IOException when fails to do IO operations
+ */
+ public void addEnumAttributeToTempFiles(YangNode curNode) throws IOException {
+
+ if (curNode instanceof YangEnumeration) {
+ Set<YangEnum> enumSet = ((YangEnumeration) curNode).getEnumSet();
+ for (YangEnum curEnum : enumSet) {
+ JavaAttributeInfo javaAttributeInfo = getAttributeInfoOfEnumAttribute(curNode, curEnum.getNamedValue());
+ setEnumValue(curEnum.getValue());
+ addJavaSnippetInfoToApplicableTempFiles(javaAttributeInfo);
+ }
+ } else {
+ throw new TranslatorException("curnode should be of type enum.");
+ }
+ }
+
+ /**
* Adds the new attribute info to the target generated temporary files for union class.
*
* @param hasType the node for which the type is being added as an attribute
@@ -1337,6 +1470,10 @@
addToStringMethod(newAttrInfo);
}
+ if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
+ addAttributesForEnumClass(newAttrInfo);
+ }
+
if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
addOfStringMethod(newAttrInfo);
}
@@ -1395,7 +1532,12 @@
* Prepares java file generator for extends list.
*/
prepareJavaFileGeneratorForExtendsList(getExtendsList());
-
+ if (curNode.getNodeType().equals(MODULE_NODE)) {
+ createPackage(absoluteDirPath, getJavaFileInfo().getJavaName());
+ } else {
+ createPackage(absoluteDirPath, ((HasJavaFileInfo) curNode.getParent()).getJavaFileInfo().getJavaName()
+ + PACKAGE_INFO_JAVADOC_OF_CHILD);
+ }
/**
* Generate java code.
*/
@@ -1492,6 +1634,14 @@
}
/**
+ * Creates type enum class file.
+ */
+ if ((fileType & GENERATE_ENUM_CLASS) != 0) {
+ setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX)));
+ setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode));
+ }
+
+ /**
* Close all the file handles.
*/
close(false);
@@ -1525,6 +1675,9 @@
if ((generatedJavaFiles & GENERATE_TYPEDEF_CLASS) != 0) {
closeFile(getTypedefClassJavaFileHandle(), isError);
}
+ if ((generatedJavaFiles & GENERATE_ENUM_CLASS) != 0) {
+ closeFile(getEnumClassJavaFileHandle(), isError);
+ }
if ((generatedJavaFiles & GENERATE_UNION_CLASS) != 0) {
closeFile(getTypeClassJavaFileHandle(), isError);
}
@@ -1559,6 +1712,9 @@
if ((generatedTempFiles & EQUALS_IMPL_MASK) != 0) {
closeFile(getEqualsImplTempFileHandle(), true);
}
+ if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
+ closeFile(getEnumClassTempFileHandle(), true);
+ }
if ((generatedTempFiles & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
closeFile(getConstructorForTypeTempFileHandle(), true);
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaEnumeration.java b/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaEnumeration.java
new file mode 100644
index 0000000..fe99500
--- /dev/null
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/javamodel/YangJavaEnumeration.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2016-present 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.YangEnumeration;
+import org.onosproject.yangutils.translator.exception.TranslatorException;
+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 org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
+
+import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_ENUM_CLASS;
+import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfType;
+
+/**
+ * Represents YANG java enumeration information extended to support java code generation.
+ */
+public class YangJavaEnumeration extends YangEnumeration implements JavaCodeGenerator, JavaCodeGeneratorInfo {
+
+ /**
+ * 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;
+
+ /**
+ * Creates YANG java enumeration object.
+ */
+ public YangJavaEnumeration() {
+ super();
+ setJavaFileInfo(new JavaFileInfo());
+ setJavaImportData(new JavaImportData());
+ getJavaFileInfo().setGeneratedFileTypes(GENERATE_ENUM_CLASS);
+ }
+
+ /**
+ * Returns the generated java file information.
+ *
+ * @return generated java file information
+ */
+ @Override
+ public JavaFileInfo getJavaFileInfo() {
+
+ if (javaFileInfo == null) {
+ throw new TranslatorException("Missing java info in java datamodel node");
+ }
+ return javaFileInfo;
+ }
+
+ /**
+ * Sets the java file info object.
+ *
+ * @param javaInfo java file info object
+ */
+ @Override
+ public void setJavaFileInfo(JavaFileInfo javaInfo) {
+
+ javaFileInfo = javaInfo;
+ }
+
+ /**
+ * Returns 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;
+ }
+
+ /**
+ * Sets 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;
+ }
+
+ /**
+ * Returns the temporary file handle.
+ *
+ * @return temporary file handle
+ */
+ @Override
+ public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
+
+ return tempFileHandle;
+ }
+
+ /**
+ * Sets 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
+ * enumeration info.
+ *
+ * @param yangPlugin YANG plugin config
+ * @throws IOException IO operations fails
+ */
+ @Override
+ public void generateCodeEntry(YangPluginConfig yangPlugin) throws IOException {
+ generateCodeOfType(this, yangPlugin, false);
+ }
+
+ /**
+ * Creates a java file using the YANG enumeration info.
+ *
+ * @throws IOException IO operation fail
+ */
+ @Override
+ public void generateCodeExit() throws IOException {
+ getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_ENUM_CLASS, this);
+ }
+
+}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java
index 64fdf04..e28fa4e 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/AttributesJavaDataType.java
@@ -18,6 +18,7 @@
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangDerivedInfo;
+import org.onosproject.yangutils.datamodel.YangEnumeration;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeDef;
@@ -25,6 +26,7 @@
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.HasJavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
+import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaTypeDef;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaUnion;
@@ -186,7 +188,8 @@
case BOOLEAN:
return BOOLEAN_WRAPPER;
case ENUMERATION:
- //TODO: ENUMERATION.
+ return getCaptialCase(
+ getCamelCase(((YangJavaEnumeration) yangType.getDataTypeExtendedInfo()).getName(), null));
case BITS:
//TODO:BITS
case BINARY:
@@ -205,7 +208,7 @@
case DERIVED:
return getCaptialCase(getCamelCase(yangType.getDataTypeName(), null));
default:
- return null;
+ throw new TranslatorException("given data type is not supported.");
}
} else {
switch (type) {
@@ -216,7 +219,8 @@
case STRING:
return STRING_DATA_TYPE;
case ENUMERATION:
- //TODO: ENUMERATION.
+ return getCaptialCase(
+ getCamelCase(((YangJavaEnumeration) yangType.getDataTypeExtendedInfo()).getName(), null));
case BITS:
//TODO:BITS
case BINARY:
@@ -269,7 +273,7 @@
case DECIMAL64:
//TODO: DECIMAL64.
case ENUMERATION:
- //TODO: ENUMERATION.
+ return getEnumsPackage(yangType);
case BITS:
//TODO:BITS
case BINARY:
@@ -287,7 +291,7 @@
case DERIVED:
return getTypDefsPackage(yangType);
default:
- return null;
+ throw new TranslatorException("given data type is not supported.");
}
} else {
switch (type) {
@@ -298,7 +302,7 @@
case STRING:
return JAVA_LANG;
case ENUMERATION:
- //TODO: ENUMERATION.
+ return getEnumsPackage(yangType);
case BITS:
//TODO:BITS
case BINARY:
@@ -364,6 +368,24 @@
}
/**
+ * Returns YANG enumeration's java package.
+ *
+ * @param type YANG type
+ * @return YANG enumeration's java package
+ */
+ private static String getEnumsPackage(YangType<?> type) {
+
+ if (!(type.getDataTypeExtendedInfo() instanceof YangEnumeration)) {
+ throw new TranslatorException("type should have been enumeration.");
+ }
+ YangJavaEnumeration enumeration = (YangJavaEnumeration) type.getDataTypeExtendedInfo();
+ if (enumeration.getJavaFileInfo().getPackage() == null) {
+ return getPackageFromParent(enumeration.getParent());
+ }
+ return enumeration.getJavaFileInfo().getPackage();
+ }
+
+ /**
* Returns package from parent node.
*
* @param parent parent YANG node
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
index 529a93d..0db41c9 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/ClassDefinitionGenerator.java
@@ -18,6 +18,7 @@
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_ENUM_CLASS;
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.GeneratedJavaFileType.IMPL_CLASS_MASK;
@@ -27,6 +28,7 @@
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CLASS;
import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
+import static org.onosproject.yangutils.utils.UtilConstants.ENUM;
import static org.onosproject.yangutils.utils.UtilConstants.EXTEND;
import static org.onosproject.yangutils.utils.UtilConstants.FINAL;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
@@ -76,11 +78,23 @@
return getTypeClassDefinition(yangName);
} else if ((genFileTypes & GENERATE_UNION_CLASS) != 0) {
return getTypeClassDefinition(yangName);
+ } else if ((genFileTypes & GENERATE_ENUM_CLASS) != 0) {
+ return getEnumClassDefinition(yangName);
}
return null;
}
/**
+ * Returns enum file class definition.
+ *
+ * @param yangName class name
+ * @return enum file class definiton
+ */
+ private static String getEnumClassDefinition(String yangName) {
+ return PUBLIC + SPACE + ENUM + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
+ }
+
+ /**
* Returns interface file class definition.
*
* @param yangName file name
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java
index 07da946..1942b01 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaCodeSnippetGen.java
@@ -20,11 +20,13 @@
import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
import static org.onosproject.yangutils.utils.UtilConstants.ARRAY_LIST;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_PARENTHESIS;
+import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.DIAMOND_OPEN_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
@@ -38,6 +40,8 @@
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_ATTRIBUTE;
/**
* Represents utility class to generate the java snippet.
@@ -155,4 +159,17 @@
public static String getJavaClassDefClose() {
return CLOSE_CURLY_BRACKET;
}
+
+ /**
+ * Returns string for enum's attribute.
+ *
+ * @param name name of attribute
+ * @param value value of the enum
+ * @return string for enum's attribute
+ */
+ public static String generateEnumAttributeString(String name, int value) {
+ return getJavaDoc(ENUM_ATTRIBUTE, name, false) + FOUR_SPACE_INDENTATION + getEnumJavaAttribute(name)
+ + OPEN_PARENTHESIS + value + CLOSE_PARENTHESIS + COMMA + NEW_LINE;
+ }
+
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
index 6fc37a5..aa208e5 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGenerator.java
@@ -27,6 +27,7 @@
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_ENUM_CLASS;
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.GeneratedJavaFileType.IMPL_CLASS_MASK;
@@ -34,6 +35,7 @@
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
+import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
@@ -45,15 +47,19 @@
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.UNION_FROM_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getAugmentedInfoAttribute;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getDataFromTempFileHandle;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.getEnumsValueAttribute;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.initiateJavaFileGeneration;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAddAugmentInfoMethodImpl;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getAugmentInfoListImpl;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getConstructorStart;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEnumsConstrcutor;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getEqualsMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethodSignature;
+import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getGetter;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodClose;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getHashCodeMethodOpen;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOmitNullValueString;
@@ -63,14 +69,21 @@
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
+import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
+import static org.onosproject.yangutils.utils.UtilConstants.INT;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
+import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.GETTER_METHOD;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.partString;
+import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
/**
* Representation of java file generator.
@@ -384,7 +397,7 @@
}
/**
- * Generate class file for type def.
+ * Generates class file for type def.
*
* @param file generated file
* @param curNode current YANG node
@@ -469,7 +482,7 @@
}
/**
- * Generate class file for union type.
+ * Generates class file for union type.
*
* @param file generated file
* @param curNode current YANG node
@@ -559,4 +572,56 @@
return file;
}
+
+ /**
+ * Generates class file for type enum.
+ *
+ * @param file generated file
+ * @param curNode current YANG node
+ * @return class file for type enum
+ * @throws IOException when fails to generate class file
+ */
+ public static File generateEnumClassFile(File file, YangNode curNode) throws IOException {
+
+ JavaFileInfo javaFileInfo = ((HasJavaFileInfo) curNode).getJavaFileInfo();
+
+ String className = getCaptialCase(javaFileInfo.getJavaName());
+ String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
+
+ initiateJavaFileGeneration(file, className, GENERATE_ENUM_CLASS, null, path);
+ /**
+ * Add attribute strings.
+ */
+ try {
+ insertDataIntoJavaFile(file,
+ trimAtLast(trimAtLast(getDataFromTempFileHandle(ENUM_IMPL_MASK, curNode), COMMA), NEW_LINE)
+ + SEMI_COLAN + NEW_LINE);
+ } catch (IOException e) {
+ throw new IOException("No data found in temporary java code fragment files for " + className
+ + " while enum class file generation");
+ }
+
+ /**
+ * Add an attribute to get the enum's values.
+ */
+ insertDataIntoJavaFile(file, getEnumsValueAttribute(className));
+
+ /**
+ * Add a constructor for enum.
+ */
+ insertDataIntoJavaFile(file,
+ getJavaDoc(TYPE_CONSTRUCTOR, getSmallCase(className), false) + getEnumsConstrcutor(className)
+ + NEW_LINE);
+
+ /**
+ * Add a getter method for enum.
+ */
+ insertDataIntoJavaFile(file,
+ getJavaDoc(GETTER_METHOD, getSmallCase(className), false) + getGetter(INT, getSmallCase(className))
+ + NEW_LINE);
+
+ insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE);
+
+ return file;
+ }
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
index 6aa3ead..5828e03 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaFileGeneratorUtils.java
@@ -28,6 +28,7 @@
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_ENUM_CLASS;
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.GeneratedJavaFileType.IMPL_CLASS_MASK;
@@ -35,6 +36,7 @@
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ATTRIBUTES_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_IMPL_MASK;
+import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EQUALS_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.GETTER_FOR_INTERFACE_MASK;
@@ -46,17 +48,22 @@
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.UNION_FROM_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.getJavaClassDefStart;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
+import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getSmallCase;
+import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
+import static org.onosproject.yangutils.utils.UtilConstants.INT;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.ORG;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
+import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_CLASS;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILDER_INTERFACE;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.ENUM_CLASS;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.IMPL_CLASS;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.INTERFACE;
-import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
/**
@@ -134,6 +141,9 @@
} else if ((generatedTempFiles & UNION_FROM_STRING_IMPL_MASK) != 0) {
return tempJavaCodeFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getUnionFromStringImplTempFileHandle());
+ } else if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
+ return tempJavaCodeFragmentFiles
+ .getTemporaryDataFromFileHandle(tempJavaCodeFragmentFiles.getEnumClassTempFileHandle());
}
return null;
}
@@ -190,6 +200,9 @@
} else if ((type & GENERATE_UNION_CLASS) != 0) {
appendHeaderContents(file, pkgString, importsList);
write(file, fileName, type, IMPL_CLASS);
+ } else if ((type & GENERATE_ENUM_CLASS) != 0) {
+ appendHeaderContents(file, pkgString, importsList);
+ write(file, fileName, type, ENUM_CLASS);
}
}
@@ -259,4 +272,15 @@
insertDataIntoJavaFile(file, getJavaClassDefStart(genType, fileName));
}
+ /**
+ * Returns integer attribute for enum's class to get the values.
+ *
+ * @param className enum's class name
+ * @return enum's attribute
+ */
+ public static String getEnumsValueAttribute(String className) {
+ return NEW_LINE + FOUR_SPACE_INDENTATION + PRIVATE + SPACE + INT + SPACE + getSmallCase(className) + SEMI_COLAN
+ + NEW_LINE;
+ }
+
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
index dd0b893..27b9e80 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/JavaIdentifierSyntax.java
@@ -381,6 +381,25 @@
}
/**
+ * Returns enum's java name.
+ *
+ * @param name enum's name
+ * @return enum's java name
+ */
+ public static String getEnumJavaAttribute(String name) {
+
+ String[] strArray = name.split(HYPHEN);
+ String output = EMPTY_STRING;
+ for (int i = 0; i < strArray.length; i++) {
+ output = output + strArray[i];
+ if (i > 0 && i < strArray.length - 1) {
+ output = output + UNDER_SCORE;
+ }
+ }
+ return output;
+ }
+
+ /**
* Returns the directory path corresponding to java package.
*
* @param packagePath package path
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
index ec1296f..309ed22 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/MethodsGenerator.java
@@ -87,6 +87,7 @@
import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
import static org.onosproject.yangutils.utils.UtilConstants.VOID;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.BUILD_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.DEFAULT_CONSTRUCTOR;
@@ -95,7 +96,6 @@
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.SETTER_METHOD;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.TYPE_CONSTRUCTOR;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.UNION_FROM_METHOD;
-import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.trimAtLast;
/**
@@ -218,7 +218,7 @@
* @param name attribute name
* @return getter for attribute
*/
- private static String getGetter(String type, String name) {
+ public static String getGetter(String type, String name) {
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + type + SPACE + GET_METHOD_PREFIX + getCaptialCase(name)
+ OPEN_PARENTHESIS + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION
+ RETURN + SPACE + name + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
@@ -803,4 +803,15 @@
return method;
}
+ /**
+ * Returns enum's constructor.
+ *
+ * @param className enum's class name
+ * @return enum's constructor
+ */
+ public static String getEnumsConstrcutor(String className) {
+ return FOUR_SPACE_INDENTATION + className + OPEN_PARENTHESIS + INT + SPACE + VALUE + CLOSE_PARENTHESIS + SPACE
+ + OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION + getSmallCase(className) + SPACE + EQUAL
+ + SPACE + VALUE + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
+ }
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/TempJavaCodeFragmentFilesUtils.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/TempJavaCodeFragmentFilesUtils.java
index adc90ef..ccb8370 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/TempJavaCodeFragmentFilesUtils.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/TempJavaCodeFragmentFilesUtils.java
@@ -31,6 +31,8 @@
import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
+import static java.util.Collections.sort;
+
/**
* Represents utilities for temporary java code fragments.
*/
@@ -128,7 +130,7 @@
} else {
imports.remove(curImport);
}
- java.util.Collections.sort(imports);
+ sort(imports);
return imports;
}
diff --git a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/YangJavaModelUtils.java b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/YangJavaModelUtils.java
index c706b7d..c28271b 100644
--- a/src/main/java/org/onosproject/yangutils/translator/tojava/utils/YangJavaModelUtils.java
+++ b/src/main/java/org/onosproject/yangutils/translator/tojava/utils/YangJavaModelUtils.java
@@ -30,6 +30,7 @@
import org.onosproject.yangutils.datamodel.YangOutput;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
+import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaEnumeration;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
@@ -116,6 +117,9 @@
} else if (javaCodeGeneratorInfo instanceof HasType) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo);
+ } else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
+ javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
+ .addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
} else {
//TODO throw exception
}
diff --git a/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java b/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
index db3ff8f..4b9d7b5 100644
--- a/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
+++ b/src/main/java/org/onosproject/yangutils/utils/UtilConstants.java
@@ -53,9 +53,24 @@
public static final String BUILDER_INTERFACE_JAVA_DOC = " * Builder for ";
/**
+ * JavaDocs for enum class.
+ */
+ public static final String ENUM_CLASS_JAVADOC = " * Represents ENUM data of ";
+
+ /**
+ * JavaDocs for enum attribute.
+ */
+ public static final String ENUM_ATTRIBUTE_JAVADOC = " * Represents ";
+
+ /**
* JavaDocs for package info class.
*/
- public static final String PACKAGE_INFO_JAVADOC = " * Implementation of YANG file ";
+ public static final String PACKAGE_INFO_JAVADOC = " * Implementation of YANG node ";
+
+ /**
+ * JavaDocs for package info class.
+ */
+ public static final String PACKAGE_INFO_JAVADOC_OF_CHILD = "'s children nodes";
/**
* JavaDocs's first line.
@@ -283,6 +298,11 @@
public static final String VALUE = "value";
/**
+ * Static attribute for enumValue syntax.
+ */
+ public static final String ENUM_VALUE = "enumValue";
+
+ /**
* Static attribute for suffix s.
*/
public static final String SUFFIX_S = "s";
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
index 0a385ac..5833de2 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeader.java
@@ -23,6 +23,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.Calendar;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
@@ -33,6 +34,8 @@
private static final int EOF = -1;
private static final String COPYRIGHT_HEADER_FILE = "CopyrightHeader.txt";
+ private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + Calendar.getInstance().get(Calendar.YEAR)
+ + "-present Open Networking Laboratory\n";
private static final String TEMP_FILE = "temp.txt";
private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader();
@@ -82,6 +85,7 @@
OutputStream out = new FileOutputStream(temp);
int index;
+ out.write(COPYRIGHTS_FIRST_LINE.getBytes());
while ((index = stream.read()) != EOF) {
out.write(index);
}
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
index a1cae8e..0ecaaa1 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/JavaDocGen.java
@@ -22,6 +22,8 @@
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_CLASS_JAVA_DOC;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_INTERFACE_JAVA_DOC;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER_OBJECT;
+import static org.onosproject.yangutils.utils.UtilConstants.ENUM_ATTRIBUTE_JAVADOC;
+import static org.onosproject.yangutils.utils.UtilConstants.ENUM_CLASS_JAVADOC;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_METHOD_NAME;
import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_PARAM_NAME;
@@ -135,7 +137,17 @@
/**
* For build.
*/
- BUILD_METHOD
+ BUILD_METHOD,
+
+ /**
+ * For enum.
+ */
+ ENUM_CLASS,
+
+ /**
+ * For enum's attributes.
+ */
+ ENUM_ATTRIBUTE
}
/**
@@ -176,6 +188,10 @@
javaDoc = generateForTypeConstructor(name);
} else if (type.equals(JavaDocType.UNION_FROM_METHOD)) {
javaDoc = generateForUnionFrom(name);
+ } else if (type.equals(JavaDocType.ENUM_CLASS)) {
+ javaDoc = generateForEnum(name);
+ } else if (type.equals(JavaDocType.ENUM_ATTRIBUTE)) {
+ javaDoc = generateForEnumAttr(name);
} else {
javaDoc = generateForConstructors(name);
}
@@ -183,7 +199,18 @@
}
/**
- * Generate javaDocs for getter method.
+ * Generates javaDocs for enum's attributes.
+ *
+ * @param name attribute name
+ * @return javaDocs
+ */
+ private static String generateForEnumAttr(String name) {
+ return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + ENUM_ATTRIBUTE_JAVADOC
+ + name + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
+ }
+
+ /**
+ * Generates javaDocs for getter method.
*
* @param attribute attribute
* @param isList is list attribute
@@ -272,20 +299,7 @@
}
/**
- * Generates javaDocs for typedef constructor.
- *
- * @param attribute attribute
- * @return javaDocs
- */
- private static String generateForTypeDefConstructor(String attribute) {
- return (NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_CONSTRUCTOR
- + attribute + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK + FOUR_SPACE_INDENTATION
- + JAVA_DOC_PARAM + VALUE + SPACE + VALUE + SPACE + OF + SPACE + attribute + NEW_LINE
- + FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE);
- }
-
- /**
- * Generate javaDocs for the impl class.
+ * Generates javaDocs for the impl class.
*
* @param className class name
* @return javaDocs
@@ -295,7 +309,18 @@
}
/**
- * Generate javaDocs for the builder class.
+ * Generates javaDocs for enum.
+ *
+ * @param className enum class name
+ * @return javaDocs
+ */
+ private static String generateForEnum(String className) {
+ return NEW_LINE + NEW_LINE + JAVA_DOC_FIRST_LINE + ENUM_CLASS_JAVADOC + className + PERIOD + NEW_LINE
+ + JAVA_DOC_END_LINE;
+ }
+
+ /**
+ * Generates javaDocs for the builder class.
*
* @param className class name
* @return javaDocs
@@ -306,7 +331,7 @@
}
/**
- * Generate javaDoc for the interface.
+ * Generates javaDoc for the interface.
*
* @param interfaceName interface name
* @return javaDocs
@@ -317,7 +342,7 @@
}
/**
- * Generate javaDoc for the builder interface.
+ * Generates javaDoc for the builder interface.
*
* @param builderforName builder for name
* @return javaDocs
@@ -328,7 +353,7 @@
}
/**
- * Generate javaDocs for package-info.
+ * Generates javaDocs for package-info.
*
* @param packageName package name
* @return javaDocs
@@ -338,7 +363,7 @@
}
/**
- * Generate javaDocs for default constructor.
+ * Generates javaDocs for default constructor.
*
* @param className class name
* @return javaDocs
@@ -349,7 +374,7 @@
}
/**
- * Generate javaDocs for constructor with parameters.
+ * Generates javaDocs for constructor with parameters.
*
* @param className class name
* @return javaDocs
@@ -363,7 +388,7 @@
}
/**
- * Generate javaDocs for build.
+ * Generates javaDocs for build.
*
* @param buildName builder name
* @return javaDocs
diff --git a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
index c15d580..baf6929 100644
--- a/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
+++ b/src/main/java/org/onosproject/yangutils/utils/io/impl/YangIoUtils.java
@@ -44,6 +44,7 @@
import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
+import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
import static org.slf4j.LoggerFactory.getLogger;
@@ -96,7 +97,7 @@
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
- bufferedWriter.write(JavaDocGen.getJavaDoc(PACKAGE_INFO, classInfo, false));
+ bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, false));
bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
bufferedWriter.close();
diff --git a/src/main/resources/CopyrightHeader.txt b/src/main/resources/CopyrightHeader.txt
index d6a4b16..2cbed45 100644
--- a/src/main/resources/CopyrightHeader.txt
+++ b/src/main/resources/CopyrightHeader.txt
@@ -1,5 +1,3 @@
-/*
- * Copyright 2016-present 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.
diff --git a/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java b/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
index aa2f1ea..97302d8 100644
--- a/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
+++ b/src/test/java/org/onosproject/yangutils/utils/io/impl/CopyrightHeaderTest.java
@@ -17,10 +17,13 @@
package org.onosproject.yangutils.utils.io.impl;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
+import java.util.Calendar;
import org.junit.Rule;
import org.junit.Test;
@@ -40,7 +43,8 @@
public final class CopyrightHeaderTest {
private final Logger log = getLogger(getClass());
-
+ private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + Calendar.getInstance().get(Calendar.YEAR)
+ + "-present Open Networking Laboratory\n";
@Rule
public ExpectedException thrown = ExpectedException.none();
@@ -74,16 +78,30 @@
@Test
public void testGetCopyrightHeader() throws IOException {
- String baseDir = System.getProperty("basedir");
- String path = "/src/test/resources/CopyrightHeader.txt";
+ String path = "src/test/resources/CopyrightHeader.txt";
+
+ File testRsc = new File(path);
+ FileInputStream in = new FileInputStream(testRsc);
+
+ File testFile = new File("target/TestHeader.txt");
+ FileOutputStream out = new FileOutputStream(testFile);
+
+ out.write(COPYRIGHTS_FIRST_LINE.getBytes());
+ int c = 0;
+ while ((c = in.read()) != -1) {
+ out.write(c);
+ }
String licenseHeader = getCopyrightHeader();
File test = new File("target/TestCopyrightHeader.txt");
- FileWriter out = new FileWriter(test);
- out.write(licenseHeader);
+ FileWriter writer = new FileWriter(test);
+ writer.write(licenseHeader);
+ writer.close();
out.close();
+ out.flush();
+ in.close();
- assertThat(true, is(contentEquals(test, new File(baseDir + path))));
+ assertThat(true, is(contentEquals(test, testFile)));
}
}
\ No newline at end of file
diff --git a/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java b/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java
index 92df183..23541e0 100644
--- a/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java
+++ b/src/test/java/org/onosproject/yangutils/utils/io/impl/JavaDocGenTest.java
@@ -159,7 +159,7 @@
@Test
public void packageInfoGenerationTest() {
String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, false);
- assertThat(true, is(packageInfo.contains("Implementation of YANG file") && packageInfo.contains(END_STRING)));
+ assertThat(true, is(packageInfo.contains("Implementation of YANG node") && packageInfo.contains(END_STRING)));
}
/**
diff --git a/src/test/resources/CopyrightHeader.txt b/src/test/resources/CopyrightHeader.txt
index d6a4b16..2cbed45 100644
--- a/src/test/resources/CopyrightHeader.txt
+++ b/src/test/resources/CopyrightHeader.txt
@@ -1,5 +1,3 @@
-/*
- * Copyright 2016-present 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.