Bharat saraswal
Committed by Gerrit Code Review

[ONOS-4073] YANG enum translator implementation.

Change-Id: I129eaec0c8f81c674a994a579650f18499340052
Showing 24 changed files with 658 additions and 50 deletions
......@@ -35,6 +35,7 @@ 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.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 @@ public final class YangDataModelFactory {
}
}
}
/**
* 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.");
}
}
}
}
......
......@@ -55,6 +55,8 @@ import org.onosproject.yangutils.parser.exceptions.ParserException;
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 @@ public final class EnumerationListener {
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.
......
......@@ -57,6 +57,11 @@ public final class GeneratedJavaFileType {
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() {
......
......@@ -82,6 +82,11 @@ public final class GeneratedTempFileType {
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() {
......
......@@ -124,6 +124,29 @@ public final class JavaAttributeInfo {
}
/**
* 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
......
......@@ -20,7 +20,11 @@ import java.io.File;
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.YangNode;
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.GeneratedJavaFileType.
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.
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.FOUR_SPACE_INDENTATI
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 @@ public class TempJavaCodeFragmentFiles {
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 @@ public class TempJavaCodeFragmentFiles {
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 @@ public class TempJavaCodeFragmentFiles {
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 @@ public class TempJavaCodeFragmentFiles {
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 @@ public class TempJavaCodeFragmentFiles {
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 @@ public class TempJavaCodeFragmentFiles {
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 @@ public class TempJavaCodeFragmentFiles {
}
/**
* 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 @@ public class TempJavaCodeFragmentFiles {
}
/**
* 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 @@ public class TempJavaCodeFragmentFiles {
}
/**
* 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 @@ public class TempJavaCodeFragmentFiles {
}
/**
* 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 @@ public class TempJavaCodeFragmentFiles {
* @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 @@ public class TempJavaCodeFragmentFiles {
}
/**
* 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 @@ public class TempJavaCodeFragmentFiles {
addToStringMethod(newAttrInfo);
}
if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
addAttributesForEnumClass(newAttrInfo);
}
if ((generatedTempFiles & OF_STRING_IMPL_MASK) != 0) {
addOfStringMethod(newAttrInfo);
}
......@@ -1395,7 +1532,12 @@ public class TempJavaCodeFragmentFiles {
* 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 @@ public class TempJavaCodeFragmentFiles {
}
/**
* 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 @@ public class TempJavaCodeFragmentFiles {
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 @@ public class TempJavaCodeFragmentFiles {
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);
}
......
/*
* 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);
}
}
......@@ -18,6 +18,7 @@ package org.onosproject.yangutils.translator.tojava.utils;
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.datamodel.YangUnion;
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 @@ public final class AttributesJavaDataType {
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 @@ public final class AttributesJavaDataType {
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 @@ public final class AttributesJavaDataType {
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 @@ public final class AttributesJavaDataType {
case DECIMAL64:
//TODO: DECIMAL64.
case ENUMERATION:
//TODO: ENUMERATION.
return getEnumsPackage(yangType);
case BITS:
//TODO:BITS
case BINARY:
......@@ -287,7 +291,7 @@ public final class AttributesJavaDataType {
case DERIVED:
return getTypDefsPackage(yangType);
default:
return null;
throw new TranslatorException("given data type is not supported.");
}
} else {
switch (type) {
......@@ -298,7 +302,7 @@ public final class AttributesJavaDataType {
case STRING:
return JAVA_LANG;
case ENUMERATION:
//TODO: ENUMERATION.
return getEnumsPackage(yangType);
case BITS:
//TODO:BITS
case BINARY:
......@@ -364,6 +368,24 @@ public final class AttributesJavaDataType {
}
/**
* 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
......
......@@ -18,6 +18,7 @@ package org.onosproject.yangutils.translator.tojava.utils;
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.translator.tojava.utils.JavaFileGenerato
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 @@ public final class ClassDefinitionGenerator {
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
......
......@@ -20,11 +20,13 @@ import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
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.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
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 final class JavaCodeSnippetGen {
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;
}
}
......
......@@ -27,6 +27,7 @@ import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
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.GeneratedJavaFileType.
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.
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.MethodsGenerator
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 @@ public final class JavaFileGenerator {
}
/**
* 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 @@ public final class JavaFileGenerator {
}
/**
* 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 @@ public final class JavaFileGenerator {
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;
}
}
......
......@@ -28,6 +28,7 @@ import org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType;
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.GeneratedJavaFileType.
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.
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 @@ public final class JavaFileGeneratorUtils {
} 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 @@ public final class JavaFileGeneratorUtils {
} 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 @@ public final class JavaFileGeneratorUtils {
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;
}
}
......
......@@ -381,6 +381,25 @@ public final class JavaIdentifierSyntax {
}
/**
* 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
......
......@@ -87,6 +87,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.TRY;
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.OF_
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 @@ public final class MethodsGenerator {
* @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 @@ public final class MethodsGenerator {
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;
}
}
......
......@@ -31,6 +31,8 @@ import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
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 @@ public final class TempJavaCodeFragmentFilesUtils {
} else {
imports.remove(curImport);
}
java.util.Collections.sort(imports);
sort(imports);
return imports;
}
......
......@@ -30,6 +30,7 @@ import org.onosproject.yangutils.datamodel.YangNotification;
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 @@ public final class YangJavaModelUtils {
} else if (javaCodeGeneratorInfo instanceof HasType) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.addTypeInfoToTempFiles((HasType) javaCodeGeneratorInfo);
} else if (javaCodeGeneratorInfo instanceof YangJavaEnumeration) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
} else {
//TODO throw exception
}
......
......@@ -53,9 +53,24 @@ public final class UtilConstants {
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 final class UtilConstants {
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";
......
......@@ -23,6 +23,7 @@ import java.io.FileReader;
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 @@ public final class CopyrightHeader {
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 @@ public final class CopyrightHeader {
OutputStream out = new FileOutputStream(temp);
int index;
out.write(COPYRIGHTS_FIRST_LINE.getBytes());
while ((index = stream.read()) != EOF) {
out.write(index);
}
......
......@@ -22,6 +22,8 @@ import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
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 @@ public final class JavaDocGen {
/**
* For build.
*/
BUILD_METHOD
BUILD_METHOD,
/**
* For enum.
*/
ENUM_CLASS,
/**
* For enum's attributes.
*/
ENUM_ATTRIBUTE
}
/**
......@@ -176,6 +188,10 @@ public final class JavaDocGen {
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 @@ public final class JavaDocGen {
}
/**
* 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,30 +299,28 @@ public final class JavaDocGen {
}
/**
* Generates javaDocs for typedef constructor.
* Generates javaDocs for the impl class.
*
* @param attribute attribute
* @param className class name
* @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);
private static String generateForImplClass(String className) {
return NEW_LINE + JAVA_DOC_FIRST_LINE + IMPL_CLASS_JAVA_DOC + className + PERIOD + NEW_LINE + JAVA_DOC_END_LINE;
}
/**
* Generate javaDocs for the impl class.
* Generates javaDocs for enum.
*
* @param className class name
* @param className enum class name
* @return javaDocs
*/
private static String generateForImplClass(String className) {
return NEW_LINE + JAVA_DOC_FIRST_LINE + IMPL_CLASS_JAVA_DOC + className + PERIOD + NEW_LINE + JAVA_DOC_END_LINE;
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;
}
/**
* Generate javaDocs for the builder class.
* Generates javaDocs for the builder class.
*
* @param className class name
* @return javaDocs
......@@ -306,7 +331,7 @@ public final class JavaDocGen {
}
/**
* Generate javaDoc for the interface.
* Generates javaDoc for the interface.
*
* @param interfaceName interface name
* @return javaDocs
......@@ -317,7 +342,7 @@ public final class JavaDocGen {
}
/**
* Generate javaDoc for the builder interface.
* Generates javaDoc for the builder interface.
*
* @param builderforName builder for name
* @return javaDocs
......@@ -328,7 +353,7 @@ public final class JavaDocGen {
}
/**
* Generate javaDocs for package-info.
* Generates javaDocs for package-info.
*
* @param packageName package name
* @return javaDocs
......@@ -338,7 +363,7 @@ public final class JavaDocGen {
}
/**
* Generate javaDocs for default constructor.
* Generates javaDocs for default constructor.
*
* @param className class name
* @return javaDocs
......@@ -349,7 +374,7 @@ public final class JavaDocGen {
}
/**
* Generate javaDocs for constructor with parameters.
* Generates javaDocs for constructor with parameters.
*
* @param className class name
* @return javaDocs
......@@ -363,7 +388,7 @@ public final class JavaDocGen {
}
/**
* Generate javaDocs for build.
* Generates javaDocs for build.
*
* @param buildName builder name
* @return javaDocs
......
......@@ -44,6 +44,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTA
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 @@ public final class YangIoUtils {
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();
......
/*
* 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.
......
......@@ -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 @@ import static org.slf4j.LoggerFactory.getLogger;
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 @@ public final class CopyrightHeaderTest {
@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
......
......@@ -159,7 +159,7 @@ public final class JavaDocGenTest {
@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)));
}
/**
......
/*
* 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.
......