Bharat saraswal
Committed by Thomas Vachuska

[ONOS-4411,ONOS-4413,ONOS-4526] Yang Enumeration Translator and Defect fixes.

Change-Id: Ie5b6b5d6d6df283a57ae06b1979f0d03022f3baf
Showing 33 changed files with 796 additions and 300 deletions
......@@ -78,6 +78,8 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorTyp
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorType.MISSING_HOLDER;
import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
import static org.onosproject.yangutils.utils.YangConstructType.ENUM_DATA;
import static org.onosproject.yangutils.utils.UtilConstants.QUOTES;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
/**
* Represents listener based call back function corresponding to the "enum" rule
......@@ -104,10 +106,18 @@ public final class EnumListener {
checkStackIsNotEmpty(listener, MISSING_HOLDER, ENUM_DATA, ctx.string().getText(), ENTRY);
YangEnum enumNode = new YangEnum();
enumNode.setNamedValue(ctx.string().getText());
enumNode.setNamedValue(getValidNamedValue(ctx.string().getText()));
listener.getParsedDataStack().push(enumNode);
}
/* Removes quotes from the enum name if present.*/
private static String getValidNamedValue(String name) {
if (name.contains(QUOTES)) {
name = name.replace(QUOTES, EMPTY_STRING);
}
return name;
}
/**
* It is called when parser exits from grammar rule (enum), it perform
* validations and update the data model tree.
......@@ -163,7 +173,8 @@ public final class EnumListener {
}
} else {
throw new ParserException(
constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUM_DATA, ctx.string().getText(), EXIT));
constructListenerErrorMessage(MISSING_CURRENT_HOLDER, ENUM_DATA, ctx.string().getText(),
EXIT));
}
}
}
......
......@@ -158,6 +158,7 @@ public class YangUtilManager extends AbstractMojo {
logInfo = logInfo + NEW_LINE + e.getMessage();
}
getLog().info(logInfo);
throw e;
}
}
......
......@@ -147,7 +147,7 @@ public class JavaImportData {
}
}
if (isListToImport) {
if (getIfListImported()) {
imports.add(getImportForList());
}
......
......@@ -166,6 +166,7 @@ public class JavaQualifiedTypeInfo
* @return return the import info for this attribute
*/
public static JavaQualifiedTypeInfo getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo) {
/*
* Get the java qualified type information for the wrapper classes and
* set it in new java attribute information.
......@@ -178,7 +179,6 @@ public class JavaQualifiedTypeInfo
return qualifiedInfoOfFromString;
}
@Override
public int hashCode() {
return Objects.hash(pkgInfo, classInfo);
......
......@@ -51,7 +51,6 @@ public class TempJavaBeanFragmentFiles
super(javaFileInfo);
/*
* Initialize getterImpl, attributes, constructor, hash code, equals and
* to strings when generation file type matches to impl class mask.
......@@ -98,6 +97,7 @@ public class TempJavaBeanFragmentFiles
* files
* @throws IOException IO operation fail
*/
@Override
void addJavaSnippetInfoToApplicableTempFiles(JavaAttributeInfo newAttrInfo)
throws IOException {
super.addJavaSnippetInfoToApplicableTempFiles(newAttrInfo);
......@@ -124,5 +124,4 @@ public class TempJavaBeanFragmentFiles
super.freeTemporaryResources(isErrorOccurred);
}
}
......
......@@ -67,7 +67,6 @@ public class TempJavaCodeFragmentFiles {
*/
private TempJavaEventListenerFragmentFiles eventListenerTempFiles;
/**
* Creates an instance of temporary java code fragment.
*
......@@ -126,7 +125,6 @@ public class TempJavaCodeFragmentFiles {
this.beanTempFiles = beanTempFiles;
}
/**
* Retrieves the temp file handle for data type file generation.
*
......@@ -136,7 +134,6 @@ public class TempJavaCodeFragmentFiles {
return typeTempFiles;
}
/**
* Sets temp file handle for data type file generation.
*
......@@ -234,23 +231,29 @@ public class TempJavaCodeFragmentFiles {
getBeanTempFiles().generateJavaFile(fileType, curNode);
}
/**
/*
* Creates user defined data type class file.
*/
if ((fileType & GENERATE_TYPE_CLASS) != 0) {
getTypeTempFiles().generateJavaFile(fileType, curNode);
}
/*
* Creats service and manager class file.
*/
if (fileType == GENERATE_SERVICE_AND_MANAGER) {
getServiceTempFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, curNode);
}
/*
* Creats enumeration class file.
*/
if (fileType == GENERATE_ENUM_CLASS) {
getEnumerationTempFiles().generateJavaFile(GENERATE_ENUM_CLASS, curNode);
}
if ((fileType & GENERATE_EVENT_CLASS) != 0) {
/**
/*
* Creates event class file.
*/
if (getEventTempFiles() != null) {
......@@ -350,7 +353,6 @@ public class TempJavaCodeFragmentFiles {
throw new TranslatorException("default constructor should not be added");
}
/**
* Adds build method's implementation for class.
*
......@@ -397,22 +399,4 @@ 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 (getEnumerationTempFiles() != null) {
getEnumerationTempFiles().addEnumAttributeToTempFiles(curNode);
return;
}
throw new TranslatorException("build should not be added");
}
}
......
......@@ -15,14 +15,68 @@
*/
package org.onosproject.yangutils.translator.tojava;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangEnum;
import org.onosproject.yangutils.datamodel.YangEnumeration;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaType;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.ENUM_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
import static org.onosproject.yangutils.translator.tojava.utils.JavaCodeSnippetGen.generateEnumAttributeString;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateEnumClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
/**
* Represents implementation of java code fragments temporary implementations.
* Maintains the temp files required specific for enumeration java snippet generation.
*/
public class TempJavaEnumerationFragmentFiles
extends TempJavaFragmentFiles {
public class TempJavaEnumerationFragmentFiles extends TempJavaFragmentFiles {
/**
* File name for temporary enum class.
*/
private static final String ENUM_CLASS_TEMP_FILE_NAME = "EnumClass";
/**
* File name for enum class file name suffix.
*/
private static final String ENUM_CLASS_FILE_NAME_SUFFIX = EMPTY_STRING;
/**
* Current enum's value.
*/
private int enumValue;
/**
* Contains data of enumSet.
*/
private Map<String, Integer> enumStringMap = new HashMap<>();
/**
* Contains data of enumSet.
*/
private List<String> enumStringList;
/**
* Temporary file handle for enum class file.
*/
private File enumClassTempFileHandle;
/**
* Java file handle for enum class.
*/
private File enumClassJavaFileHandle;
/**
* Creates an instance of temporary java code fragment.
......@@ -32,6 +86,212 @@ public class TempJavaEnumerationFragmentFiles
*/
public TempJavaEnumerationFragmentFiles(JavaFileInfo javaFileInfo)
throws IOException {
super(javaFileInfo);
setEnumSetJavaMap(new HashMap<>());
setEnumStringList(new ArrayList<>());
/*
* Initialize enum when generation file type matches to enum class mask.
*/
addGeneratedTempFile(ENUM_IMPL_MASK);
setEnumClassTempFileHandle(getTemporaryFileHandle(ENUM_CLASS_TEMP_FILE_NAME));
}
/**
* Returns enum class java file handle.
*
* @return enum class java file handle
*/
public 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 enum's value.
*
* @return enum's value
*/
private int getEnumValue() {
return enumValue;
}
/**
* Sets enum's value.
*
* @param enumValue enum's value
*/
private void setEnumValue(int enumValue) {
this.enumValue = enumValue;
}
/**
* Returns enum set java map.
*
* @return the enum set java map
*/
public Map<String, Integer> getEnumSetJavaMap() {
return enumStringMap;
}
/**
* Sets enum set java map.
*
* @param map the enum set java map to set
*/
private void setEnumSetJavaMap(Map<String, Integer> map) {
this.enumStringMap = map;
}
/**
* 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;
}
/**
* Adds enum class attributes to temporary file.
*
* @param curEnumInfo current YANG enum
* @throws IOException when fails to do IO operations.
*/
private void addAttributesForEnumClass(String curEnumName) throws IOException {
appendToFile(getEnumClassTempFileHandle(), generateEnumAttributeString(curEnumName, getEnumValue()));
}
/**
* 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 {
super.addJavaSnippetInfoToApplicableTempFiles(getJavaAttributeForEnum());
if (curNode instanceof YangEnumeration) {
YangEnumeration enumeration = (YangEnumeration) curNode;
for (YangEnum curEnum : enumeration.getEnumSet()) {
setEnumValue(curEnum.getValue());
addToEnumStringList(curEnum.getNamedValue());
addToEnumSetJavaMap(curEnum.getNamedValue(), curEnum.getValue());
addJavaSnippetInfoToApplicableTempFiles(curEnum.getNamedValue());
}
} else {
throw new TranslatorException("current node should be of enumeration type.");
}
}
/**
* Returns java attribute for enum class.
*
* @return java attribute
*/
public JavaAttributeInfo getJavaAttributeForEnum() {
YangJavaType<?> javaType = new YangJavaType<>();
javaType.setDataType(YangDataTypes.INT32);
javaType.setDataTypeName("int");
javaType.updateJavaQualifiedInfo();
return getAttributeInfoForTheData(
javaType.getJavaQualifiedInfo(),
javaType.getDataTypeName(), javaType,
getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
false);
}
/**
* Adds current enum name to java list.
*
* @param curEnumName current enum name
*/
private void addToEnumSetJavaMap(String curEnumName, int value) {
getEnumSetJavaMap().put(curEnumName.toUpperCase(), value);
}
/**
* Adds the new attribute info to the target generated temporary files.
*
* @param curEnumName the attribute name that needs to be added to temporary
* files
* @throws IOException IO operation fail
*/
void addJavaSnippetInfoToApplicableTempFiles(String curEnumName) throws IOException {
addAttributesForEnumClass(curEnumName);
}
/**
* Constructs java code exit.
*
* @param fileType generated file type
* @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
@Override
public void generateJavaFile(int fileType, YangNode curNode) throws IOException {
createPackage(curNode);
setEnumClassJavaFileHandle(getJavaFileHandle(getJavaClassName(ENUM_CLASS_FILE_NAME_SUFFIX)));
setEnumClassJavaFileHandle(generateEnumClassFile(getEnumClassJavaFileHandle(), curNode));
freeTemporaryResources(false);
}
/**
* Removes all temporary file handles.
*
* @param isErrorOccurred when translator fails to generate java files we
* need to close all open file handles include temporary files
* and java files.
* @throws IOException when failed to delete the temporary files
*/
@Override
public void freeTemporaryResources(boolean isErrorOccurred) throws IOException {
closeFile(getEnumClassJavaFileHandle(), isErrorOccurred);
closeFile(getEnumClassTempFileHandle(), true);
super.freeTemporaryResources(isErrorOccurred);
}
/**
* Adds to enum string list.
*
* @param curEnumValue current enum value
*/
private void addToEnumStringList(String curEnumValue) {
getEnumStringList().add(curEnumValue.toUpperCase());
}
/**
* Returns enum string list.
*
* @return the enumStringList
*/
public List<String> getEnumStringList() {
return enumStringList;
}
/**
* Sets enum string list.
*
* @param enumStringList the enumStringList to set
*/
public void setEnumStringList(List<String> enumStringList) {
this.enumStringList = enumStringList;
}
}
......
......@@ -56,7 +56,7 @@ public class TempJavaEventFragmentFiles
setExtendsList(new ArrayList<>());
setJavaImportData(new JavaImportData());
setJavaFileInfo(javaFileInfo);
clearGeneratedTempFileMask();
setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath()));
......@@ -80,7 +80,6 @@ public class TempJavaEventFragmentFiles
this.eventJavaFileHandle = eventJavaFileHandle;
}
/**
* Constructs java code exit.
*
......@@ -88,6 +87,7 @@ public class TempJavaEventFragmentFiles
* @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
@Override
public void generateJavaFile(int fileType, YangNode curNode)
throws IOException {
......@@ -113,6 +113,7 @@ public class TempJavaEventFragmentFiles
* and java files.
* @throws IOException when failed to delete the temporary files
*/
@Override
public void freeTemporaryResources(boolean isErrorOccurred)
throws IOException {
boolean isError = isErrorOccurred;
......@@ -122,5 +123,6 @@ public class TempJavaEventFragmentFiles
closeFile(getEventJavaFileHandle(), isError);
super.freeTemporaryResources(isErrorOccurred);
}
}
......
......@@ -56,7 +56,6 @@ public class TempJavaEventListenerFragmentFiles
setExtendsList(new ArrayList<>());
setJavaImportData(new JavaImportData());
setJavaFileInfo(javaFileInfo);
clearGeneratedTempFileMask();
setAbsoluteDirPath(getAbsolutePackagePath(getJavaFileInfo().getBaseCodeGenPath(),
getJavaFileInfo().getPackageFilePath()));
}
......@@ -79,7 +78,6 @@ public class TempJavaEventListenerFragmentFiles
this.eventListenerJavaFileHandle = eventListenerJavaFileHandle;
}
/**
* Constructs java code exit.
*
......@@ -87,6 +85,7 @@ public class TempJavaEventListenerFragmentFiles
* @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
@Override
public void generateJavaFile(int fileType, YangNode curNode)
throws IOException {
......@@ -112,6 +111,7 @@ public class TempJavaEventListenerFragmentFiles
* and java files.
* @throws IOException when failed to delete the temporary files
*/
@Override
public void freeTemporaryResources(boolean isErrorOccurred)
throws IOException {
boolean isError = isErrorOccurred;
......
......@@ -33,9 +33,11 @@ import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addArrayListImport;
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.addAugmentedInfoImport;
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils
.isHasAugmentationExtended;
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.isHasAugmentationExtended;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.RPC_INPUT_VAR_NAME;
import static org.onosproject.yangutils.utils.UtilConstants.VOID;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.generateJavaDocForRpc;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
......@@ -180,12 +182,13 @@ public class TempJavaServiceFragmentFiles
}
/**
* Constructs java code exit.
* Generate java code.
*
* @param fileType generated file type
* @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
@Override
public void generateJavaFile(int fileType, YangNode curNode)
throws IOException {
List<String> imports = new ArrayList<>();
......@@ -199,9 +202,6 @@ public class TempJavaServiceFragmentFiles
setServiceInterfaceJavaFileHandle(getJavaFileHandle(getJavaClassName(SERVICE_FILE_NAME_SUFFIX)));
generateServiceInterfaceFile(getServiceInterfaceJavaFileHandle(), curNode, imports, isAttributePresent());
if (isAttributePresent()) {
addImportsToStringAndHasCodeMethods(curNode, imports);
}
if (isHasAugmentationExtended(getExtendsList())) {
addAugmentedInfoImport(curNode, imports, true);
addArrayListImport(curNode, imports, true);
......@@ -229,21 +229,20 @@ public class TempJavaServiceFragmentFiles
* @param rpcName name of the rpc function
* @throws IOException IO operation fail
*/
private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput, JavaAttributeInfo javaAttributeInfoOfOutput,
String rpcName)
throws IOException {
String rpcInput = "";
String rpcOutput = "void";
private void addRpcString(JavaAttributeInfo javaAttributeInfoOfInput,
JavaAttributeInfo javaAttributeInfoOfOutput,
String rpcName) throws IOException {
String rpcInput = EMPTY_STRING;
String rpcOutput = VOID;
if (javaAttributeInfoOfInput != null) {
rpcInput = javaAttributeInfoOfInput.getAttributeName();
}
if (javaAttributeInfoOfOutput != null) {
rpcOutput = javaAttributeInfoOfOutput.getAttributeName();
}
appendToFile(getRpcInterfaceTempFileHandle(), generateJavaDocForRpc(rpcName, rpcInput, rpcOutput) +
getRpcServiceMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE);
appendToFile(getRpcImplTempFileHandle(),
getRpcManagerMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE);
appendToFile(getRpcInterfaceTempFileHandle(), generateJavaDocForRpc(rpcName, RPC_INPUT_VAR_NAME, rpcOutput)
+ getRpcServiceMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE);
appendToFile(getRpcImplTempFileHandle(), getRpcManagerMethod(rpcName, rpcInput, rpcOutput) + NEW_LINE);
}
/**
......@@ -269,12 +268,17 @@ public class TempJavaServiceFragmentFiles
* and java files.
* @throws IOException when failed to delete the temporary files
*/
@Override
public void freeTemporaryResources(boolean isErrorOccurred)
throws IOException {
boolean isError = isErrorOccurred;
closeFile(getServiceInterfaceJavaFileHandle(), isError);
closeFile(getRpcInterfaceTempFileHandle(), true);
closeFile(getRpcImplTempFileHandle(), true);
closeFile(getGetterInterfaceTempFileHandle(), true);
closeFile(getSetterInterfaceTempFileHandle(), true);
closeFile(getSetterImplTempFileHandle(), true);
super.freeTemporaryResources(isErrorOccurred);
......
......@@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangDataTypes;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangTypeHolder;
......@@ -33,10 +34,9 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.FROM_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.OF_STRING_IMPL_MASK;
import static org.onosproject.yangutils.translator.tojava.JavaAttributeInfo.getAttributeInfoForTheData;
import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo.getQualifiedInfoOfFromString;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateTypeDefClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGenerator.generateUnionClassFile;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getFromStringMethod;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getOfMethodStringAndJavaDoc;
import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator.getTypeConstructorStringAndJavaDoc;
import static org.onosproject.yangutils.translator.tojava.utils.TempJavaCodeFragmentFilesUtils.closeFile;
......@@ -60,10 +60,6 @@ public class TempJavaTypeFragmentFiles
* File name for construction for special type like union, typedef.
*/
private static final String CONSTRUCTOR_FOR_TYPE_FILE_NAME = "ConstructorForType";
/**
* File name for from string method.
*/
private static final String FROM_STRING_METHOD_FILE_NAME = "FromString";
/**
* File name for typedef class file name suffix.
......@@ -86,11 +82,6 @@ public class TempJavaTypeFragmentFiles
private File constructorForTypeTempFileHandle;
/**
* Temporary file handle for from string method of class.
*/
private File fromStringImplTempFileHandle;
/**
* Java file handle for typedef class file.
*/
private File typedefClassJavaFileHandle;
......@@ -118,10 +109,8 @@ public class TempJavaTypeFragmentFiles
addGeneratedTempFile(CONSTRUCTOR_FOR_TYPE_MASK);
addGeneratedTempFile(FROM_STRING_IMPL_MASK);
setOfStringImplTempFileHandle(getTemporaryFileHandle(OF_STRING_METHOD_FILE_NAME));
setConstructorForTypeTempFileHandle(getTemporaryFileHandle(CONSTRUCTOR_FOR_TYPE_FILE_NAME));
setFromStringImplTempFileHandle(getTemporaryFileHandle(FROM_STRING_METHOD_FILE_NAME));
}
......@@ -146,25 +135,6 @@ public class TempJavaTypeFragmentFiles
}
/**
* Returns from string method's temporary file handle.
*
* @return from string method's temporary file handle
*/
public File getFromStringImplTempFileHandle() {
return fromStringImplTempFileHandle;
}
/**
* Sets from string method's temporary file handle.
*
* @param fromStringImplTempFileHandle from string method's temporary file
* handle
*/
private void setFromStringImplTempFileHandle(File fromStringImplTempFileHandle) {
this.fromStringImplTempFileHandle = fromStringImplTempFileHandle;
}
/**
* Returns java file handle for typedef class file.
*
* @return java file handle for typedef class file
......@@ -239,9 +209,14 @@ public class TempJavaTypeFragmentFiles
}
YangJavaType<?> javaType = (YangJavaType<?>) yangType;
javaType.updateJavaQualifiedInfo();
String typeName = javaType.getDataTypeName();
if (javaType.getDataType().equals(YangDataTypes.DERIVED)) {
typeName = getCamelCase(typeName, null);
}
JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
javaType.getJavaQualifiedInfo(),
javaType.getDataTypeName(), javaType,
typeName, javaType,
getIsQualifiedAccessOrAddToImportList(javaType.getJavaQualifiedInfo()),
false);
addJavaSnippetInfoToApplicableTempFiles((YangNode) yangTypeHolder, javaAttributeInfo);
......@@ -269,34 +244,6 @@ public class TempJavaTypeFragmentFiles
if ((getGeneratedTempFiles() & CONSTRUCTOR_FOR_TYPE_MASK) != 0) {
addTypeConstructor(javaAttributeInfo);
}
JavaQualifiedTypeInfo qualifiedInfoOfFromString = getQualifiedInfoOfFromString(javaAttributeInfo);
/*
* Create a new java attribute info with qualified information of
* wrapper classes.
*/
JavaAttributeInfo fromStringAttributeInfo = getAttributeInfoForTheData(qualifiedInfoOfFromString,
javaAttributeInfo.getAttributeName(),
javaAttributeInfo.getAttributeType(),
getIsQualifiedAccessOrAddToImportList(qualifiedInfoOfFromString), false);
if ((getGeneratedTempFiles() & FROM_STRING_IMPL_MASK) != 0) {
addFromStringMethod(javaAttributeInfo, fromStringAttributeInfo);
}
}
/**
* Adds from string method for union class.
*
* @param javaAttributeInfo type attribute info
* @param fromStringAttributeInfo from string attribute info
* @throws IOException when fails to append to temporary file
*/
private void addFromStringMethod(JavaAttributeInfo javaAttributeInfo,
JavaAttributeInfo fromStringAttributeInfo)
throws IOException {
appendToFile(getFromStringImplTempFileHandle(), getFromStringMethod(javaAttributeInfo,
fromStringAttributeInfo) + NEW_LINE);
}
/**
......@@ -324,7 +271,6 @@ public class TempJavaTypeFragmentFiles
+ NEW_LINE);
}
/**
* Removes all temporary file handles.
*
......@@ -333,6 +279,7 @@ public class TempJavaTypeFragmentFiles
* and java files.
* @throws IOException when failed to delete the temporary files
*/
@Override
public void freeTemporaryResources(boolean isErrorOccurred)
throws IOException {
boolean isError = isErrorOccurred;
......@@ -356,6 +303,7 @@ public class TempJavaTypeFragmentFiles
}
super.freeTemporaryResources(isErrorOccurred);
}
/**
......@@ -365,6 +313,7 @@ public class TempJavaTypeFragmentFiles
* @param curNode current YANG node
* @throws IOException when fails to generate java files
*/
@Override
public void generateJavaFile(int fileType, YangNode curNode)
throws IOException {
List<String> imports = new ArrayList<>();
......
......@@ -76,7 +76,6 @@ public class YangJavaEnumeration
*/
@Override
public void setJavaFileInfo(JavaFileInfo javaInfo) {
javaFileInfo = javaInfo;
}
......@@ -87,7 +86,6 @@ public class YangJavaEnumeration
*/
@Override
public TempJavaCodeFragmentFiles getTempJavaCodeFragmentFiles() {
return tempFileHandle;
}
......@@ -98,7 +96,6 @@ public class YangJavaEnumeration
*/
@Override
public void setTempJavaCodeFragmentFiles(TempJavaCodeFragmentFiles fileHandle) {
tempFileHandle = fileHandle;
}
......
......@@ -27,6 +27,7 @@ import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
/**
* Represents module information extended to support java code generation.
......@@ -118,5 +119,7 @@ public class YangJavaModule
public void generateCodeExit()
throws IOException {
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
getJavaFileInfo().getPackageFilePath());
}
}
......
......@@ -28,6 +28,7 @@ import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_SERVICE_AND_MANAGER;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getRootPackage;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.searchAndDeleteTempDir;
/**
* Represents sub module information extended to support java code generation.
......@@ -133,5 +134,7 @@ public class YangJavaSubModule
public void generateCodeExit()
throws IOException {
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_SERVICE_AND_MANAGER, this);
searchAndDeleteTempDir(getJavaFileInfo().getBaseCodeGenPath() +
getJavaFileInfo().getPackageFilePath());
}
}
......
......@@ -68,6 +68,7 @@ public class YangJavaType<T>
}
importInfo.setClassInfo(dataTypeName);
}
setJavaQualifiedInfo(importInfo);
}
@Override
......
......@@ -38,6 +38,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.BYTE;
import static org.onosproject.yangutils.utils.UtilConstants.BYTE_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.FROM_STRING_METHOD_NAME;
import static org.onosproject.yangutils.utils.UtilConstants.INT;
import static org.onosproject.yangutils.utils.UtilConstants.INTEGER_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.JAVA_LANG;
......@@ -45,7 +46,6 @@ import static org.onosproject.yangutils.utils.UtilConstants.JAVA_MATH;
import static org.onosproject.yangutils.utils.UtilConstants.LONG;
import static org.onosproject.yangutils.utils.UtilConstants.LONG_WRAPPER;
import static org.onosproject.yangutils.utils.UtilConstants.NEW;
import static org.onosproject.yangutils.utils.UtilConstants.OF;
import static org.onosproject.yangutils.utils.UtilConstants.PARSE_BYTE;
import static org.onosproject.yangutils.utils.UtilConstants.PARSE_INT;
import static org.onosproject.yangutils.utils.UtilConstants.PARSE_LONG;
......@@ -146,7 +146,7 @@ public final class AttributesJavaDataType {
case BINARY:
//TODO:BINARY
case DERIVED:
return targetDataType + PERIOD + OF;
return targetDataType + PERIOD + FROM_STRING_METHOD_NAME;
default:
throw new TranslatorException("given data type is not supported.");
}
......@@ -316,8 +316,7 @@ public final class AttributesJavaDataType {
} else {
switch (type) {
case UINT64:
//TODO: BIGINTEGER.
break;
return JAVA_MATH;
case DECIMAL64:
//TODO: DECIMAL64
break;
......
......@@ -74,7 +74,7 @@ public final class ClassDefinitionGenerator {
if ((genFileTypes & INTERFACE_MASK) != 0) {
return getInterfaceDefinition(yangName);
} else if ((genFileTypes & BUILDER_CLASS_MASK) != 0) {
return getBuilderClassDefinition(yangName, genFileTypes);
return getBuilderClassDefinition(yangName);
} else if ((genFileTypes & IMPL_CLASS_MASK) != 0) {
return getImplClassDefinition(yangName);
} else if ((genFileTypes & BUILDER_INTERFACE_MASK) != 0) {
......@@ -142,14 +142,9 @@ public final class ClassDefinitionGenerator {
* @param genFileTypes
* @return definition
*/
private static String getBuilderClassDefinition(String yangName, int genFileTypes) {
if ((genFileTypes & GENERATE_SERVICE_AND_MANAGER) != 0) {
return PUBLIC + SPACE + CLASS + SPACE + yangName + MANAGER + SPACE + IMPLEMENTS + SPACE + yangName +
SERVICE + PERIOD + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
} else {
return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
+ yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
private static String getBuilderClassDefinition(String yangName) {
return PUBLIC + SPACE + CLASS + SPACE + yangName + BUILDER + SPACE + IMPLEMENTS + SPACE + yangName + PERIOD
+ yangName + BUILDER + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -180,7 +175,11 @@ public final class ClassDefinitionGenerator {
* @return definition
*/
private static String getRpcInterfaceDefinition(String yangName) {
return INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
if (yangName.contains(SERVICE)) {
return PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
return PUBLIC + SPACE + CLASS + SPACE + yangName + MANAGER + SPACE + IMPLEMENTS + SPACE + yangName + SERVICE
+ SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/**
......@@ -195,7 +194,7 @@ public final class ClassDefinitionGenerator {
if (classDef.length() < 5) {
throw new RuntimeException("Event class name is error");
}
classDef = classDef.substring(0, (classDef.length() - 5));
classDef = classDef.substring(0, classDef.length() - 5);
classDef = classDef + ">" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
return classDef;
......@@ -213,7 +212,7 @@ public final class ClassDefinitionGenerator {
if (intfDef.length() < 8) {
throw new RuntimeException("Event listener interface name is error");
}
intfDef = intfDef.substring(0, (intfDef.length() - 8));
intfDef = intfDef.substring(0, intfDef.length() - 8);
intfDef = intfDef + "Event>" + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
return intfDef;
......
......@@ -19,7 +19,6 @@ package org.onosproject.yangutils.translator.tojava.utils;
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;
......@@ -87,11 +86,6 @@ public final class JavaCodeSnippetGen {
* @return corresponding textual java code information
*/
public static String getJavaClassDefStart(int genFileTypes, String yangName) {
/*
* get the camel case name for java class / interface.
*/
yangName = getCamelCase(yangName, null);
return generateClassDefinition(genFileTypes, yangName);
}
......@@ -168,8 +162,9 @@ public final class JavaCodeSnippetGen {
* @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;
return getJavaDoc(ENUM_ATTRIBUTE, name, false) + FOUR_SPACE_INDENTATION
+ getEnumJavaAttribute(name).toUpperCase() + OPEN_PARENTHESIS
+ value + CLOSE_PARENTHESIS + COMMA + NEW_LINE;
}
}
......
......@@ -25,6 +25,7 @@ import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFilesContainer;
import org.onosproject.yangutils.translator.tojava.TempJavaEnumerationFragmentFiles;
import org.onosproject.yangutils.translator.tojava.javamodel.JavaCodeGeneratorInfo;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.BUILDER_CLASS_MASK;
......@@ -62,6 +63,7 @@ import static org.onosproject.yangutils.translator.tojava.utils.MethodsGenerator
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.getEnumsOfMethod;
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;
......@@ -84,7 +86,6 @@ import static org.onosproject.yangutils.utils.UtilConstants.EVENT_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.MANAGER;
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;
......@@ -164,7 +165,8 @@ public final class JavaFileGenerator {
* @return interface file
* @throws IOException when fails to write in file
*/
public static File generateInterfaceFile(File file, List<String> imports, YangNode curNode, boolean isAttrPresent)
public static File generateInterfaceFile(File file, List<String> imports, YangNode curNode,
boolean isAttrPresent)
throws IOException {
JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
......@@ -311,7 +313,8 @@ public final class JavaFileGenerator {
/**
* Add default constructor and build method impl.
*/
methods.add(((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles().addBuildMethodImpl());
methods.add(((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
.addBuildMethodImpl());
methods.add(((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
.addDefaultConstructor(PUBLIC, BUILDER));
......@@ -340,42 +343,45 @@ public final class JavaFileGenerator {
JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
String className = getCaptialCase(javaFileInfo.getJavaName()) + MANAGER;
String className = getCaptialCase(javaFileInfo.getJavaName());
String path = javaFileInfo.getBaseCodeGenPath() + javaFileInfo.getPackageFilePath();
initiateJavaFileGeneration(file, className, GENERATE_SERVICE_AND_MANAGER, imports, path);
List<String> methods = new ArrayList<>();
if (isAttrPresent) {
try {
try {
if (isAttrPresent) {
/**
* Getter methods.
*/
methods.add(getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK,
((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
.getServiceTempFiles()));
methods.add(
getDataFromTempFileHandle(GETTER_FOR_CLASS_MASK,
((TempJavaCodeFragmentFilesContainer) curNode)
.getTempJavaCodeFragmentFiles().getServiceTempFiles()));
/**
* Setter methods.
*/
methods.add(getDataFromTempFileHandle(SETTER_FOR_CLASS_MASK,
((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
.getServiceTempFiles()) +
NEW_LINE);
methods.add(
getDataFromTempFileHandle(SETTER_FOR_CLASS_MASK,
((TempJavaCodeFragmentFilesContainer) curNode)
.getTempJavaCodeFragmentFiles().getServiceTempFiles())
+ NEW_LINE);
}
if (((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getServiceTempFiles() != null) {
JavaCodeGeneratorInfo javaGeninfo = (JavaCodeGeneratorInfo) curNode;
/**
* Rpc methods
*/
methods.add(getDataFromTempFileHandle(RPC_IMPL_MASK, javaGeninfo.getTempJavaCodeFragmentFiles()
.getServiceTempFiles()));
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while manager class file generation");
methods.add(getDataFromTempFileHandle(RPC_IMPL_MASK,
javaGeninfo.getTempJavaCodeFragmentFiles().getServiceTempFiles()));
}
} else {
insertDataIntoJavaFile(file, NEW_LINE);
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while manager class file generation");
}
/**
......@@ -746,16 +752,34 @@ public final class JavaFileGenerator {
/**
* Add a constructor for enum.
*/
insertDataIntoJavaFile(file,
getJavaDoc(TYPE_CONSTRUCTOR, getSmallCase(className), false) + getEnumsConstrcutor(className)
+ NEW_LINE);
insertDataIntoJavaFile(file, getJavaDoc(TYPE_CONSTRUCTOR, getSmallCase(className), false)
+ getEnumsConstrcutor(className) + NEW_LINE);
TempJavaEnumerationFragmentFiles enumFragFiles =
((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
.getEnumerationTempFiles();
insertDataIntoJavaFile(file, getEnumsOfMethod(className,
enumFragFiles.getJavaAttributeForEnum(),
enumFragFiles.getEnumSetJavaMap(),
enumFragFiles.getEnumStringList())
+ NEW_LINE);
/**
* Add a getter method for enum.
*/
insertDataIntoJavaFile(file,
getJavaDoc(GETTER_METHOD, getSmallCase(className), false)
+ getGetter(INT, getSmallCase(className), GENERATE_SERVICE_AND_MANAGER) + NEW_LINE);
insertDataIntoJavaFile(file, getJavaDoc(GETTER_METHOD, getSmallCase(className), false)
+ getGetter(INT, getSmallCase(className), GENERATE_ENUM_CLASS) + NEW_LINE);
try {
insertDataIntoJavaFile(file, getFromStringMethodSignature(className)
+ getDataFromTempFileHandle(FROM_STRING_IMPL_MASK,
((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
.getEnumerationTempFiles())
+ getFromStringMethodClose());
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while enum class file generation");
}
insertDataIntoJavaFile(file, CLOSE_CURLY_BRACKET + NEW_LINE);
......@@ -785,33 +809,30 @@ public final class JavaFileGenerator {
List<String> methods = new ArrayList<>();
try {
if (isAttributePresent) {
/**
* Getter methods.
*/
methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK,
methods.add(getDataFromTempFileHandle(GETTER_FOR_INTERFACE_MASK,
((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
.getServiceTempFiles()));
/**
* Setter methods.
*/
methods.add(NEW_LINE);
methods.add(FOUR_SPACE_INDENTATION + getDataFromTempFileHandle(SETTER_FOR_INTERFACE_MASK,
methods.add(getDataFromTempFileHandle(SETTER_FOR_INTERFACE_MASK,
((TempJavaCodeFragmentFilesContainer) curNode).getTempJavaCodeFragmentFiles()
.getServiceTempFiles()));
}
JavaCodeGeneratorInfo javaGeninfo = (JavaCodeGeneratorInfo) curNode;
/**
* Rpc methods
*/
methods.add(getDataFromTempFileHandle(RPC_INTERFACE_MASK, javaGeninfo.getTempJavaCodeFragmentFiles()
.getServiceTempFiles()));
if (((JavaCodeGeneratorInfo) curNode).getTempJavaCodeFragmentFiles().getServiceTempFiles() != null) {
JavaCodeGeneratorInfo javaGeninfo = (JavaCodeGeneratorInfo) curNode;
/**
* Rpc methods
*/
methods.add(getDataFromTempFileHandle(RPC_INTERFACE_MASK,
javaGeninfo.getTempJavaCodeFragmentFiles().getServiceTempFiles()));
}
} catch (IOException e) {
throw new IOException("No data found in temporary java code fragment files for " + className
+ " while rpc class file generation");
......
......@@ -23,6 +23,7 @@ import java.util.List;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.TempJavaBeanFragmentFiles;
import org.onosproject.yangutils.translator.tojava.TempJavaEnumerationFragmentFiles;
import org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles;
import org.onosproject.yangutils.translator.tojava.TempJavaServiceFragmentFiles;
import org.onosproject.yangutils.translator.tojava.TempJavaTypeFragmentFiles;
......@@ -39,6 +40,7 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.IMPL_CLASS_MASK;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.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;
......@@ -58,13 +60,16 @@ import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSy
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.MANAGER;
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.SERVICE;
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;
......@@ -73,7 +78,7 @@ import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.EVE
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.JavaDocType.RPC_INTERFACE;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.RPC_MANAGER;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.insertDataIntoJavaFile;
/**
......@@ -105,7 +110,6 @@ public final class JavaFileGeneratorUtils {
* Returns data stored in temporary files.
*
* @param generatedTempFiles temporary file types
* @param generatedTempFiles temporary file types
* @param tempJavaFragmentFiles temp java fragment files
* @return data stored in temporary files
* @throws IOException when failed to get the data from temporary file handle
......@@ -130,9 +134,10 @@ public final class JavaFileGeneratorUtils {
if (tempJavaFragmentFiles instanceof TempJavaServiceFragmentFiles) {
serviceFragmentFiles = (TempJavaServiceFragmentFiles) tempJavaFragmentFiles;
}
if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
if ((generatedTempFiles & ATTRIBUTES_MASK) != 0) {
return tempJavaFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getAttributesTempFileHandle());
} else if ((generatedTempFiles & GETTER_FOR_INTERFACE_MASK) != 0) {
return tempJavaFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getGetterInterfaceTempFileHandle());
} else if ((generatedTempFiles & SETTER_FOR_INTERFACE_MASK) != 0) {
......@@ -172,14 +177,16 @@ public final class JavaFileGeneratorUtils {
return typeFragmentFiles
.getTemporaryDataFromFileHandle(typeFragmentFiles.getConstructorForTypeTempFileHandle());
} else if ((generatedTempFiles & FROM_STRING_IMPL_MASK) != 0) {
if (typeFragmentFiles == null) {
throw new TranslatorException("Required from string info is missing.");
}
return typeFragmentFiles
.getTemporaryDataFromFileHandle(typeFragmentFiles.getFromStringImplTempFileHandle());
} else if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
return tempJavaFragmentFiles
.getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getEnumClassTempFileHandle());
.getTemporaryDataFromFileHandle(tempJavaFragmentFiles.getFromStringImplTempFileHandle());
} else if ((generatedTempFiles & ENUM_IMPL_MASK) != 0) {
if (!(tempJavaFragmentFiles instanceof TempJavaEnumerationFragmentFiles)) {
throw new TranslatorException("Required enum info is missing.");
}
TempJavaEnumerationFragmentFiles enumFragmentFiles =
(TempJavaEnumerationFragmentFiles) tempJavaFragmentFiles;
return enumFragmentFiles
.getTemporaryDataFromFileHandle(enumFragmentFiles.getEnumClassTempFileHandle());
} else if ((generatedTempFiles & RPC_INTERFACE_MASK) != 0) {
if (serviceFragmentFiles == null) {
throw new TranslatorException("Required rpc interface info is missing.");
......@@ -328,7 +335,15 @@ public final class JavaFileGeneratorUtils {
*/
private static void write(File file, String fileName, int genType, JavaDocType javaDocType)
throws IOException {
insertDataIntoJavaFile(file, getJavaDoc(javaDocType, fileName, false));
if ((genType & GENERATE_SERVICE_AND_MANAGER) != 0) {
if (!fileName.contains(SERVICE)) {
insertDataIntoJavaFile(file, getJavaDoc(RPC_MANAGER, fileName + MANAGER, false));
} else {
insertDataIntoJavaFile(file, getJavaDoc(javaDocType, fileName, false));
}
} else {
insertDataIntoJavaFile(file, getJavaDoc(javaDocType, fileName, false));
}
insertDataIntoJavaFile(file, getJavaClassDefStart(genType, fileName));
}
......
......@@ -16,6 +16,9 @@
package org.onosproject.yangutils.translator.tojava.utils;
import java.util.List;
import java.util.Map;
import org.onosproject.yangutils.translator.tojava.JavaAttributeInfo;
import org.onosproject.yangutils.utils.io.impl.JavaDocGen;
......@@ -31,12 +34,15 @@ import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
import static org.onosproject.yangutils.utils.UtilConstants.BOOLEAN_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.BUILD;
import static org.onosproject.yangutils.utils.UtilConstants.BUILDER;
import static org.onosproject.yangutils.utils.UtilConstants.CASE;
import static org.onosproject.yangutils.utils.UtilConstants.CATCH;
import static org.onosproject.yangutils.utils.UtilConstants.CHECK_NOT_NULL_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.CLEAR;
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.COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
import static org.onosproject.yangutils.utils.UtilConstants.DEFAULT;
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.EIGHT_SPACE_INDENTATION;
......@@ -82,6 +88,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.STATIC;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.SUFFIX_S;
import static org.onosproject.yangutils.utils.UtilConstants.SWITCH;
import static org.onosproject.yangutils.utils.UtilConstants.THIS;
import static org.onosproject.yangutils.utils.UtilConstants.TMP_VAL;
import static org.onosproject.yangutils.utils.UtilConstants.TO;
......@@ -494,10 +501,11 @@ public final class MethodsGenerator {
rpcName = getSmallCase(getCamelCase(rpcName, null));
inputName = getCaptialCase(inputName);
outputName = getCaptialCase(outputName);
return FOUR_SPACE_INDENTATION + PUBLIC + SPACE + outputName + SPACE + rpcName + OPEN_PARENTHESIS
+ inputName + SPACE + RPC_INPUT_VAR_NAME + CLOSE_PARENTHESIS + SEMI_COLAN;
if (!outputName.equals(VOID)) {
outputName = getCaptialCase(outputName);
}
return FOUR_SPACE_INDENTATION + outputName + SPACE + rpcName + OPEN_PARENTHESIS + inputName + SPACE
+ RPC_INPUT_VAR_NAME + CLOSE_PARENTHESIS + SEMI_COLAN;
}
/**
......@@ -512,12 +520,14 @@ public final class MethodsGenerator {
rpcName = getSmallCase(getCamelCase(rpcName, null));
inputName = getCaptialCase(inputName);
outputName = getCaptialCase(outputName);
if (!outputName.equals(VOID)) {
outputName = getCaptialCase(outputName);
}
String method = getOverRideString() +
FOUR_SPACE_INDENTATION + PUBLIC + SPACE + outputName + SPACE + rpcName + OPEN_PARENTHESIS
+ inputName + SPACE + RPC_INPUT_VAR_NAME + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET
+ NEW_LINE + EIGHT_SPACE_INDENTATION + YANG_UTILS_TODO + NEW_LINE;
String method =
getOverRideString() + FOUR_SPACE_INDENTATION + PUBLIC + SPACE + outputName + SPACE + rpcName
+ OPEN_PARENTHESIS + inputName + SPACE + RPC_INPUT_VAR_NAME + CLOSE_PARENTHESIS + SPACE
+ OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION + YANG_UTILS_TODO + NEW_LINE;
if (!outputName.contentEquals(VOID)) {
method += EIGHT_SPACE_INDENTATION + RETURN + SPACE + NULL + SEMI_COLAN + NEW_LINE;
}
......@@ -601,7 +611,7 @@ public final class MethodsGenerator {
* @return from string method's open string
*/
public static String getFromStringMethodSignature(String className) {
return getJavaDoc(FROM_METHOD, className, false) + FOUR_SPACE_INDENTATION + PUBLIC + SPACE
return getJavaDoc(FROM_METHOD, className, false) + FOUR_SPACE_INDENTATION + PUBLIC + SPACE + STATIC + SPACE
+ className + SPACE + FROM_STRING_METHOD_NAME + OPEN_PARENTHESIS + STRING_DATA_TYPE + SPACE
+ FROM_STRING_PARAM_NAME + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
......@@ -677,7 +687,6 @@ public final class MethodsGenerator {
+ OPEN_PARENTHESIS + FROM_STRING_PARAM_NAME + CLOSE_PARENTHESIS;
}
/**
* Returns hash code method open strings.
*
......@@ -916,4 +925,38 @@ public final class MethodsGenerator {
+ OPEN_CURLY_BRACKET + NEW_LINE + EIGHT_SPACE_INDENTATION + getSmallCase(className) + SPACE + EQUAL
+ SPACE + VALUE + SEMI_COLAN + NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
}
/**
* Returns of method for enum class.
*
* @param className class name
* @param attr java attribute
* @param enumMap enum's sets map
* @param enumList enum's sets list
* @return of method
*/
public static String getEnumsOfMethod(String className, JavaAttributeInfo attr,
Map<String, Integer> enumMap, List<String> enumList) {
String attrType = getReturnType(attr);
String attrName = getSmallCase(attr.getAttributeName());
String method = FOUR_SPACE_INDENTATION + PUBLIC + SPACE + STATIC + SPACE + className + SPACE + OF
+ OPEN_PARENTHESIS
+ attrType + SPACE + VALUE + CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE
+ EIGHT_SPACE_INDENTATION + SWITCH + SPACE + OPEN_PARENTHESIS + VALUE
+ CLOSE_PARENTHESIS + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
int value = 0;
for (String str : enumList) {
value = enumMap.get(str);
method = method + TWELVE_SPACE_INDENTATION + CASE + SPACE + value + COLAN + NEW_LINE
+ SIXTEEN_SPACE_INDENTATION + RETURN + SPACE + className + PERIOD
+ str + SEMI_COLAN + NEW_LINE;
}
method = method + TWELVE_SPACE_INDENTATION + DEFAULT + SPACE + COLAN + NEW_LINE + SIXTEEN_SPACE_INDENTATION
+ RETURN + SPACE + NULL + SEMI_COLAN + NEW_LINE + EIGHT_SPACE_INDENTATION + CLOSE_CURLY_BRACKET
+ NEW_LINE + FOUR_SPACE_INDENTATION + CLOSE_CURLY_BRACKET;
return getJavaDoc(OF_METHOD, className + " for type " + attrName, false) + method;
}
}
......
......@@ -21,7 +21,11 @@ import java.io.IOException;
import java.util.List;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
import org.onosproject.yangutils.translator.tojava.JavaImportDataContainer;
import org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfo;
import static java.util.Collections.sort;
import static org.onosproject.yangutils.translator.tojava.JavaImportData.getAugmentedInfoImport;
import static org.onosproject.yangutils.translator.tojava.JavaImportData.getHasAugmentationImport;
......@@ -31,8 +35,6 @@ 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.
*/
......@@ -52,7 +54,8 @@ public final class TempJavaCodeFragmentFilesUtils {
* @param operation add or delete import
* @return import for HasAugmentation class
*/
public static List<String> addHasAugmentationImport(YangNode curNode, List<String> imports, boolean operation) {
public static List<String> addHasAugmentationImport(YangNode curNode, List<String> imports,
boolean operation) {
if (curNode instanceof JavaImportDataContainer) {
String thisImport = getHasAugmentationImport();
performOperationOnImports(imports, thisImport, operation);
......@@ -109,13 +112,14 @@ public final class TempJavaCodeFragmentFilesUtils {
* @param operation add or remove
* @return import list
*/
private static List<String> performOperationOnImports(List<String> imports, String curImport, boolean operation) {
private static List<String> performOperationOnImports(List<String> imports, String curImport,
boolean operation) {
if (operation) {
imports.add(curImport);
} else {
imports.remove(curImport);
}
sort(imports);
sortImports(imports);
return imports;
}
......@@ -142,7 +146,7 @@ public final class TempJavaCodeFragmentFilesUtils {
* @return true or false
*/
public static boolean isHasAugmentationExtended(List<String> extendsList) {
return (extendsList != null && extendsList.contains(HAS_AUGMENTATION));
return extendsList != null && extendsList.contains(HAS_AUGMENTATION);
}
/**
......@@ -152,7 +156,7 @@ public final class TempJavaCodeFragmentFilesUtils {
* @return true or false
*/
public static boolean isAugmentedInfoExtended(List<String> extendsList) {
return (extendsList != null && extendsList.contains(AUGMENTED_INFO));
return extendsList != null && extendsList.contains(AUGMENTED_INFO);
}
/**
......@@ -172,4 +176,37 @@ public final class TempJavaCodeFragmentFilesUtils {
}
}
}
/**
* Detects collision between parent and child node which have same name.
* When parent and child node both have the same name in that case child node should be used with
* qualified name.
*
* @param curNode current YANG node
* @param qualifiedTypeInfo current node's qualified info
* @return true if collision is detected
*/
public static boolean detectCollisionBwParentAndChildForImport(YangNode curNode,
JavaQualifiedTypeInfo qualifiedTypeInfo) {
YangNode parent = curNode.getParent();
String parentsClassInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName();
String childsClassInfo = qualifiedTypeInfo.getClassInfo();
if (childsClassInfo.equals(parentsClassInfo)) {
return true;
}
return false;
}
/**
* Returns sorted import list.
*
* @param imports import list
* @return sorted import list
*/
public static List<String> sortImports(List<String> imports) {
sort(imports);
return imports;
}
}
......
......@@ -149,7 +149,7 @@ public final class YangJavaModelUtils {
/**
* Enumeration
*/
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().getEnumerationTempFiles()
.addEnumAttributeToTempFiles((YangNode) javaCodeGeneratorInfo);
} else if (javaCodeGeneratorInfo instanceof YangChoice) {
/*Do nothing, only the interface needs to be generated*/
......
......@@ -41,7 +41,6 @@ public final class UtilConstants {
public static final String INTERFACE_JAVA_DOC = " * Abstraction of an entity which represents the"
+ " functionality of ";
/**
* JavaDocs for event.
*/
......@@ -52,7 +51,6 @@ public final class UtilConstants {
*/
public static final String EVENT_LISTENER_JAVA_DOC = " * Abstraction for event listener of ";
/**
* JavaDocs for builder interface class.
*/
......@@ -169,6 +167,11 @@ public final class UtilConstants {
public static final String NEW_LINE = "\n";
/**
* Static attribute for default.
*/
public static final String DEFAULT = "default";
/**
* Static attribute for multiple new line.
*/
public static final String MULTIPLE_NEW_LINE = "\n\n";
......@@ -434,6 +437,16 @@ public final class UtilConstants {
public static final String CLEAR = "clear";
/**
* Static attribute for switch syntax.
*/
public static final String SWITCH = "switch";
/**
* Static attribute for case syntax.
*/
public static final String CASE = "case";
/**
* Static attribute for temp val syntax.
*/
public static final String TMP_VAL = "tmpVal";
......@@ -703,7 +716,6 @@ public final class UtilConstants {
*/
public static final String SERVICE = "Service";
/**
* Static attribute for interface syntax.
*/
......@@ -875,7 +887,7 @@ public final class UtilConstants {
public static final String ARRAY_LIST = "ArrayList";
/**
* comment to be added for autogenerated impl methods.
* Comment to be added for autogenerated impl methods.
*/
public static final String YANG_UTILS_TODO = "//TODO: YANG utils generated code";
......
......@@ -24,8 +24,9 @@ import java.io.IOException;
import java.io.PrintWriter;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
......@@ -35,7 +36,6 @@ 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.MULTIPLE_NEW_LINE;
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.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
......@@ -62,7 +62,7 @@ public final class FileSystemUtil {
public static boolean doesPackageExist(String pkg) {
File pkgDir = new File(getPackageDirPathFromJavaJPackage(pkg));
File pkgWithFile = new File(pkgDir + SLASH + "package-info.java");
return (pkgDir.exists() && pkgWithFile.isFile());
return pkgDir.exists() && pkgWithFile.isFile();
}
/**
......@@ -71,26 +71,24 @@ public final class FileSystemUtil {
* @param yangNode YANG node for which code is being generated
* @throws IOException any IO exception
*/
public static void createPackage(YangNode yangNode)
throws IOException {
YangNode parent = getParentNodeInGenCode(yangNode);
JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) yangNode).getJavaFileInfo();
String absolutePath = getAbsolutePackagePath(javaFileInfo.getBaseCodeGenPath(),
javaFileInfo.getPackageFilePath());
String pkgInfo;
if (parent != null) {
pkgInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName()
+ PACKAGE_INFO_JAVADOC_OF_CHILD;
} else {
pkgInfo = javaFileInfo.getJavaName();
public static void createPackage(YangNode yangNode) throws IOException {
if (!(yangNode instanceof JavaFileInfoContainer)) {
throw new TranslatorException("current node must have java file info");
}
if (!doesPackageExist(absolutePath)) {
String pkgInfo;
JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) yangNode).getJavaFileInfo();
String pkg = getAbsolutePackagePath(javaFileInfo.getBaseCodeGenPath(), javaFileInfo.getPackageFilePath());
if (!doesPackageExist(pkg)) {
try {
File pack = createDirectories(absolutePath);
addPackageInfo(pack, pkgInfo, getJavaPackageFromPackagePath(absolutePath));
File pack = createDirectories(pkg);
YangNode parent = getParentNodeInGenCode(yangNode);
if (parent != null) {
pkgInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName();
addPackageInfo(pack, pkgInfo, getJavaPackageFromPackagePath(pkg), true);
} else {
pkgInfo = ((JavaFileInfoContainer) yangNode).getJavaFileInfo().getJavaName();
addPackageInfo(pack, pkgInfo, getJavaPackageFromPackagePath(pkg), false);
}
} catch (IOException e) {
throw new IOException("failed to create package-info file");
}
......
......@@ -19,7 +19,6 @@ package org.onosproject.yangutils.utils.io.impl;
import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
import static org.onosproject.yangutils.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;
......@@ -54,12 +53,14 @@ import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE_ASTERISK;
import static org.onosproject.yangutils.utils.UtilConstants.OBJECT;
import static org.onosproject.yangutils.utils.UtilConstants.OF;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE_INFO_JAVADOC;
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.RPC_INPUT_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.RPC_OUTPUT_STRING;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.STRING_DATA_TYPE;
import static org.onosproject.yangutils.utils.UtilConstants.VALUE;
import static org.onosproject.yangutils.utils.UtilConstants.VOID;
/**
* Represents javadoc for the generated classes.
......@@ -108,11 +109,16 @@ public final class JavaDocGen {
GETTER_METHOD,
/**
* For rpc.
* For rpc service.
*/
RPC_INTERFACE,
/**
* For rpc manager.
*/
RPC_MANAGER,
/**
* For event.
*/
EVENT,
......@@ -203,7 +209,7 @@ public final class JavaDocGen {
return generateForBuilderInterface(name);
}
case PACKAGE_INFO: {
return generateForPackage(name);
return generateForPackage(name, isList);
}
case GETTER_METHOD: {
return generateForGetters(name, isList);
......@@ -239,7 +245,10 @@ public final class JavaDocGen {
return generateForEnumAttr(name);
}
case RPC_INTERFACE: {
return generateForRpcInterface(name);
return generateForRpcService(name);
}
case RPC_MANAGER: {
return generateForImplClass(name);
}
case EVENT: {
return generateForEvent(name);
......@@ -274,13 +283,14 @@ public final class JavaDocGen {
*/
public static String generateJavaDocForRpc(String rpcName, String inputName, String outputName) {
rpcName = getCamelCase(rpcName, null);
inputName = getCaptialCase(inputName);
outputName = getCaptialCase(outputName);
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_RPC
String javadoc = NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_RPC
+ rpcName + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK
+ getInputString(inputName, rpcName) + getOutputString(outputName, rpcName) + FOUR_SPACE_INDENTATION
+ JAVA_DOC_END_LINE;
+ getInputString(inputName, rpcName);
if (!outputName.equals(VOID)) {
javadoc = javadoc + getOutputString(outputName, rpcName);
}
return javadoc + FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
/**
......@@ -315,7 +325,7 @@ public final class JavaDocGen {
* @param interfaceName interface name
* @return javaDocs
*/
private static String generateForRpcInterface(String interfaceName) {
private static String generateForRpcService(String interfaceName) {
return NEW_LINE + JAVA_DOC_FIRST_LINE + INTERFACE_JAVA_DOC + interfaceName + PERIOD + NEW_LINE
+ JAVA_DOC_END_LINE;
}
......@@ -436,7 +446,7 @@ public final class JavaDocGen {
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_OF
+ attribute + SPACE + FROM_STRING_METHOD_NAME + SPACE + INPUT + SPACE + STRING_DATA_TYPE + PERIOD
+ NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK + FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM
+ FROM_STRING_PARAM_NAME + SPACE + INPUT + SPACE + STRING_DATA_TYPE + PERIOD + NEW_LINE
+ FROM_STRING_PARAM_NAME + SPACE + INPUT + SPACE + STRING_DATA_TYPE + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_RETURN + OBJECT + SPACE + OF + SPACE + attribute + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
......@@ -512,10 +522,15 @@ public final class JavaDocGen {
* Generates javaDocs for package-info.
*
* @param packageName package name
* @param isChildNode is it child node
* @return javaDocs
*/
private static String generateForPackage(String packageName) {
return JAVA_DOC_FIRST_LINE + PACKAGE_INFO_JAVADOC + packageName + PERIOD + NEW_LINE + JAVA_DOC_END_LINE;
private static String generateForPackage(String packageName, boolean isChildNode) {
String javaDoc = JAVA_DOC_FIRST_LINE + PACKAGE_INFO_JAVADOC + packageName;
if (isChildNode) {
javaDoc = javaDoc + PACKAGE_INFO_JAVADOC_OF_CHILD;
}
return javaDoc + PERIOD + NEW_LINE + JAVA_DOC_END_LINE;
}
/**
......@@ -536,11 +551,10 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForConstructors(String className) {
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_CONSTRUCTOR + className + IMPL + PERIOD + NEW_LINE
+ FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK + FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM
+ BUILDER.toLowerCase() + OBJECT + SPACE + BUILDER_OBJECT + className + NEW_LINE
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_CONSTRUCTOR
+ className + IMPL + PERIOD + NEW_LINE + FOUR_SPACE_INDENTATION + NEW_LINE_ASTERISK
+ FOUR_SPACE_INDENTATION + JAVA_DOC_PARAM + BUILDER.toLowerCase() + OBJECT + SPACE + BUILDER_OBJECT
+ className + NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
/**
......
......@@ -23,7 +23,9 @@ import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import org.apache.commons.io.FileUtils;
import org.apache.maven.model.Resource;
......@@ -80,9 +82,11 @@ public final class YangIoUtils {
* @param path directory path
* @param classInfo class info for the package
* @param pack package of the directory
* @param isChildNode is it a child node
* @throws IOException when fails to create package info file
*/
public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode)
throws IOException {
if (pack.contains(ORG)) {
String[] strArray = pack.split(ORG);
......@@ -97,7 +101,7 @@ public final class YangIoUtils {
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, false));
bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode));
bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
bufferedWriter.close();
......@@ -119,8 +123,42 @@ public final class YangIoUtils {
try {
FileUtils.deleteDirectory(generatedDirectory);
} catch (IOException e) {
throw new IOException("Failed to delete the generated files in " + generatedDirectory + " directory");
throw new IOException(
"Failed to delete the generated files in " + generatedDirectory + " directory");
}
}
}
/**
* Searches and deletes generated temporary directories.
*
* @param root root directory
* @throws IOException when fails to do IO operations.
*/
public static void searchAndDeleteTempDir(String root) throws IOException {
List<File> store = new LinkedList<>();
Stack<String> stack = new Stack<>();
stack.push(root);
while (!stack.empty()) {
root = stack.pop();
File file = new File(root);
File[] filelist = file.listFiles();
if (filelist == null || filelist.length == 0) {
continue;
}
for (File current : filelist) {
if (current.isDirectory()) {
stack.push(current.toString());
if (current.getName().endsWith("-Temp")) {
store.add(current);
}
}
}
}
for (File dir : store) {
dir.delete();
}
}
......
/*
* 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.utils;
import java.io.IOException;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.YangUtilsParserManager;
import static org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorUtil.generateJavaCode;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.deleteDirectory;
/**
* Unit test case for enum translator.
*/
public final class EnumTranslatorTest {
private final YangUtilsParserManager manager = new YangUtilsParserManager();
/**
* Checks enum translation should not result in any exception.
*/
@Test
public void processEnumTranslator()
throws IOException, ParserException {
String userDir = System.getProperty("user.dir");
YangNode node = manager.getDataModel("src/test/resources/EnumTranslator.yang");
YangPluginConfig yangPluginConfig = new YangPluginConfig();
yangPluginConfig.setCodeGenDir(userDir + "/target/EnumTestGenFile/");
generateJavaCode(node, yangPluginConfig);
deleteDirectory(userDir + "/target/EnumTestGenFile/");
}
// TODO enhance the test cases, after having a framework of translator test.
}
......@@ -22,6 +22,7 @@ import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.Test;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModule;
......@@ -41,20 +42,23 @@ import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileH
public final class FileSystemUtilTest {
private static final String BASE_DIR_PKG = "target.UnitTestCase.";
private static final String PKG_INFO_CONTENT = "testGeneration6";
private static final String BASE_PKG = "target/UnitTestCase";
private static final String TEST_DATA_1 = "This is to append a text to the file first1\n";
private static final String TEST_DATA_2 = "This is next second line\n";
private static final String TEST_DATA_3 = "This is next third line in the file";
private static final String TEST_FILE = "testFile";
private static final String SOURCE_TEST_FILE = "sourceTestFile";
private static final String DIR_PATH = "exist1.exist2.exist3";
private static final String PKG_INFO = "package-info.java";
/**
* A private constructor is tested.
*
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws SecurityException if any security violation is observed
* @throws NoSuchMethodException if when the method is not found
* @throws IllegalArgumentException if there is illegal argument found
* @throws InstantiationException if instantiation is provoked for the private constructor
* @throws IllegalAccessException if instance is provoked or a method is provoked
* @throws InvocationTargetException when an exception occurs by the method or constructor
*/
@Test
......@@ -62,7 +66,7 @@ public final class FileSystemUtilTest {
throws SecurityException, NoSuchMethodException, IllegalArgumentException,
InstantiationException, IllegalAccessException, InvocationTargetException {
Class<?>[] classesToConstruct = {FileSystemUtil.class};
Class<?>[] classesToConstruct = {FileSystemUtil.class };
for (Class<?> clazz : classesToConstruct) {
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.setAccessible(true);
......@@ -76,14 +80,13 @@ public final class FileSystemUtilTest {
* @throws IOException when fails to create a test file
*/
@Test
public void updateFileHandleTest()
throws IOException {
public void updateFileHandleTest() throws IOException {
File dir = new File(BASE_PKG + SLASH + "File1");
File dir = new File(BASE_PKG + SLASH + TEST_FILE);
dir.mkdirs();
File createFile = new File(dir + "testFile");
File createFile = new File(dir + TEST_FILE);
createFile.createNewFile();
File createSourceFile = new File(dir + "sourceTestFile");
File createSourceFile = new File(dir + SOURCE_TEST_FILE);
createSourceFile.createNewFile();
updateFileHandle(createFile, TEST_DATA_1, false);
updateFileHandle(createFile, TEST_DATA_2, false);
......@@ -98,23 +101,31 @@ public final class FileSystemUtilTest {
* @throws IOException when failed to create a test file
*/
@Test
public void packageExistTest()
throws IOException {
public void packageExistTest() throws IOException {
String dirPath = "exist1.exist2.exist3";
String strPath = BASE_DIR_PKG + dirPath;
String strPath = BASE_DIR_PKG + DIR_PATH;
File createDir = new File(strPath.replace(PERIOD, SLASH));
createDir.mkdirs();
File createFile = new File(createDir + SLASH + "package-info.java");
File createFile = new File(createDir + SLASH + PKG_INFO);
createFile.createNewFile();
assertThat(true, is(doesPackageExist(strPath)));
JavaFileInfo javaFileInfo = new JavaFileInfo();
javaFileInfo.setBaseCodeGenPath(BASE_DIR_PKG);
javaFileInfo.setPackageFilePath(dirPath);
YangJavaModule moduleNode = new YangJavaModule();
moduleNode.setJavaFileInfo(javaFileInfo);
createPackage(moduleNode);
createPackage(getStubNode());
createDir.delete();
}
/**
* Returns stub YANG node.
*
* @return stub node
*/
private YangNode getStubNode() {
YangJavaModule module = new YangJavaModule();
module.setName(TEST_DATA_1);
JavaFileInfo javafileInfo = new JavaFileInfo();
javafileInfo.setJavaName(TEST_DATA_1);
javafileInfo.setBaseCodeGenPath("");
javafileInfo.setPackageFilePath(BASE_PKG);
module.setJavaFileInfo(javafileInfo);
return module;
}
}
......
......@@ -51,7 +51,7 @@ public final class JavaDocGenTest {
public ExpectedException thrown = ExpectedException.none();
/**
* This test case checks the content recieved for the builder class java doc.
* This test case checks the content received for the builder class java doc.
*/
@Test
public void builderClassGenerationTest() {
......@@ -61,7 +61,7 @@ public final class JavaDocGenTest {
}
/**
* This test case checks the content recieved for the builder interface ge java doc.
* This test case checks the content received for the builder interface ge java doc.
*/
@Test
public void builderInterfaceGenerationTest() {
......@@ -71,7 +71,7 @@ public final class JavaDocGenTest {
}
/**
* This test case checks the content recieved for the build java doc.
* This test case checks the content received for the build java doc.
*/
@Test
public void buildGenerationTest() {
......@@ -102,7 +102,7 @@ public final class JavaDocGenTest {
}
/**
* This test case checks the content recieved for the constructor java doc.
* This test case checks the content received for the constructor java doc.
*/
@Test
public void constructorGenerationTest() {
......@@ -113,7 +113,7 @@ public final class JavaDocGenTest {
}
/**
* This test case checks the content recieved for the default constructor java doc.
* This test case checks the content received for the default constructor java doc.
*/
@Test
public void defaultConstructorGenerationTest() {
......@@ -123,7 +123,7 @@ public final class JavaDocGenTest {
}
/**
* This test case checks the content recieved for the getter java doc.
* This test case checks the content received for the getter java doc.
*/
@Test
public void getterGenerationTest() {
......@@ -132,7 +132,7 @@ public final class JavaDocGenTest {
}
/**
* This test case checks the content recieved for the impl class java doc.
* This test case checks the content received for the impl class java doc.
*/
@Test
public void implClassGenerationTest() {
......@@ -143,7 +143,7 @@ public final class JavaDocGenTest {
}
/**
* This test case checks the content recieved for the interface java doc.
* This test case checks the content received for the interface java doc.
*/
@Test
public void interfaceGenerationTest() {
......@@ -154,7 +154,7 @@ public final class JavaDocGenTest {
}
/**
* This test case checks the content recieved for the package info java doc.
* This test case checks the content received for the package info java doc.
*/
@Test
public void packageInfoGenerationTest() {
......@@ -163,7 +163,17 @@ public final class JavaDocGenTest {
}
/**
* This test case checks the content recieved for the setter java doc.
* This test case checks the content received for the package info java doc.
*/
@Test
public void packageInfoGenerationForChildNodeTest() {
String packageInfo = getJavaDoc(PACKAGE_INFO, TEST_NAME, true);
assertThat(true, is(packageInfo.contains("Implementation of YANG node testName's children nodes")
&& packageInfo.contains(END_STRING)));
}
/**
* This test case checks the content received for the setter java doc.
*/
@Test
public void setterGenerationTest() {
......
......@@ -47,6 +47,10 @@ public final class YangIoUtilsTest {
private static final String CREATE_PATH = BASE_DIR + File.separator + "dir1/dir2/dir3/dir4/";
private static final String CHECK_STRING = "one, two, three, four, five, six";
private static final String TRIM_STRING = "one, two, three, four, five, ";
private static final String CHECK1 = "check1";
private static final String PKG_INFO = "package-info.java";
private static final String PATH = "src/main/yangmodel/";
private static final String MSG = "Exception occured while creating package info file.";
/**
* Expected exceptions.
......@@ -64,8 +68,8 @@ public final class YangIoUtilsTest {
File dirPath = new File(CREATE_PATH);
dirPath.mkdirs();
addPackageInfo(dirPath, "check1", CREATE_PATH);
File filePath = new File(dirPath + File.separator + "package-info.java");
addPackageInfo(dirPath, CHECK1, CREATE_PATH, false);
File filePath = new File(dirPath + File.separator + PKG_INFO);
assertThat(filePath.isFile(), is(true));
}
......@@ -79,8 +83,23 @@ public final class YangIoUtilsTest {
File dirPath = new File(CREATE_PATH);
dirPath.mkdirs();
addPackageInfo(dirPath, "check1", "src/main/yangmodel/" + CREATE_PATH);
File filePath = new File(dirPath + File.separator + "package-info.java");
addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, false);
File filePath = new File(dirPath + File.separator + PKG_INFO);
assertThat(filePath.isFile(), is(true));
}
/**
* This test case checks with a child node.
*
* @throws IOException when fails to do IO operations for test case
*/
@Test
public void addPackageInfoWithChildNode() throws IOException {
File dirPath = new File(CREATE_PATH);
dirPath.mkdirs();
addPackageInfo(dirPath, CHECK1, PATH + CREATE_PATH, true);
File filePath = new File(dirPath + File.separator + PKG_INFO);
assertThat(filePath.isFile(), is(true));
}
......@@ -94,9 +113,9 @@ public final class YangIoUtilsTest {
File dirPath = new File("invalid/check");
thrown.expect(IOException.class);
thrown.expectMessage("Exception occured while creating package info file.");
addPackageInfo(dirPath, "check1", CREATE_PATH);
File filePath1 = new File(dirPath + File.separator + "package-info.java");
thrown.expectMessage(MSG);
addPackageInfo(dirPath, CHECK1, CREATE_PATH, false);
File filePath1 = new File(dirPath + File.separator + PKG_INFO);
assertThat(filePath1.isFile(), is(false));
}
......
module Sfc {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf test{
type string;
}
leaf myenum {
type enumeration {
enum zero;
enum one;
enum seven {
value 7;
}
}
}
}