Vidyashree Rama
Committed by Thomas Vachuska

yang choice case translator + whitebox fix

Change-Id: I96f1855bc71a002335946150b51e911ae6ac2fb2
Showing 39 changed files with 285 additions and 148 deletions
......@@ -28,5 +28,5 @@ public enum ResolutionType {
/**
* Identifies that resolution is for grouping.
*/
GROUPING_RESOLUTION;
GROUPING_RESOLUTION
}
......
......@@ -286,7 +286,7 @@ public class YangCase extends YangNode
*/
@Override
public YangConstructType getYangConstructType() {
return YangConstructType.CASE_DATA;
return CASE_DATA;
}
/**
......@@ -311,7 +311,7 @@ public class YangCase extends YangNode
@Override
public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
if (getParent() == null || !(getParent() instanceof YangChoice)) {
if (!(getParent() instanceof YangChoice)) {
throw new DataModelException("Internal Data Model Tree Error: Invalid/Missing holder in case " +
getName());
}
......
......@@ -277,11 +277,11 @@ public class YangChoice extends YangNode implements YangCommonInfo, Parsable, Co
/**
* Returns the type of the data.
*
* @return returns CHOICE_DATA
* @return choice data
*/
@Override
public YangConstructType getYangConstructType() {
return YangConstructType.CHOICE_DATA;
return CHOICE_DATA;
}
/**
......
......@@ -72,7 +72,7 @@ public class YangEntityToResolveInfo<T> {
}
String prefix;
T entityToResolve = (T) getEntityToResolve();
T entityToResolve = getEntityToResolve();
if (entityToResolve instanceof YangType) {
prefix = ((YangType<?>) entityToResolve).getPrefix();
} else if (entityToResolve instanceof YangUses) {
......
......@@ -22,10 +22,10 @@ package org.onosproject.yangutils.datamodel;
public class YangNodeIdentifier {
// Name of the node.
String name;
private String name;
// Prefix of the node.
String prefix;
private String prefix;
/**
* Creates an instance of YANG node identifier.
......
......@@ -118,7 +118,7 @@ public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
if (getAscendingRangeIntervals() == null) {
throw new DataModelException("No range restriction info");
}
if (getAscendingRangeIntervals().size() == 0) {
if (getAscendingRangeIntervals().isEmpty()) {
throw new DataModelException("No range interval info");
}
return getAscendingRangeIntervals().get(0).getStartValue();
......@@ -134,7 +134,7 @@ public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
if (getAscendingRangeIntervals() == null) {
throw new DataModelException("No range restriction info");
}
if (getAscendingRangeIntervals().size() == 0) {
if (getAscendingRangeIntervals().isEmpty()) {
throw new DataModelException("No range interval info");
}
return getAscendingRangeIntervals()
......@@ -184,7 +184,7 @@ public class YangRangeRestriction<T extends YangBuiltInDataTypeInfo<T>>
public boolean isValidValueString(String valueInString) throws DataModelException {
if (getAscendingRangeIntervals() == null
|| getAscendingRangeIntervals().size() == 0) {
|| getAscendingRangeIntervals().isEmpty()) {
// Throw exception, At least one default range needs to be set in constructor or in linker.
throw new DataModelException("Range interval missing in range restriction.");
......
......@@ -450,8 +450,6 @@ public class YangResolutionInfo<T> {
}
curNode = curNode.getNextSibling();
}
return;
}
/**
......
......@@ -21,6 +21,7 @@ import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.INTRA_FILE_RESOLVED;
import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
/*
* Reference:RFC 6020.
......@@ -294,20 +295,18 @@ public class YangType<T>
}
@Override
public void resolve() {
public void resolve() throws DataModelException {
/*
Inherit the Restriction from the referred typedef definition.
*/
if (getDataType() != YangDataTypes.DERIVED) {
throw new RuntimeException("Resolve should only be called for derrived data types");
if (getDataType() != DERIVED) {
throw new DataModelException("Resolve should only be called for derived data types");
}
YangDerivedInfo<?> derrivedInfo = (YangDerivedInfo<?>) getDataTypeExtendedInfo();
YangType<?> baseType = derrivedInfo.getReferredTypeDef().getTypeDefBaseType();
if (YangDataTypes.DERIVED == baseType.getDataType()) {
if (baseType.getResolvableStatus() == INTRA_FILE_RESOLVED) {
if (DERIVED == baseType.getDataType() && baseType.getResolvableStatus() == INTRA_FILE_RESOLVED) {
setResolvableStatus(INTRA_FILE_RESOLVED);
}
}
//TODO:
}
......
......@@ -224,6 +224,5 @@ public final class AugmentListener {
*/
private static void validateForChildNodes() {
//TODO: implement with linker.
return;
}
}
......
......@@ -80,8 +80,8 @@ public final class BaseFileListener {
checkStackIsNotEmpty(listener, MISSING_HOLDER, YANGBASE_DATA, "", EXIT);
// Data Model tree root node is set.
if ((listener.getParsedDataStack().peek() instanceof YangModule)
|| (listener.getParsedDataStack().peek() instanceof YangSubModule)) {
if (listener.getParsedDataStack().peek() instanceof YangModule
|| listener.getParsedDataStack().peek() instanceof YangSubModule) {
listener.setRootNode((YangNode) listener.getParsedDataStack().pop());
} else {
throw new ParserException(constructListenerErrorMessage(INVALID_CHILD, YANGBASE_DATA, "", EXIT));
......
......@@ -137,7 +137,7 @@ public final class BitListener {
switch (tmpNode.getYangConstructType()) {
case BITS_DATA: {
YangBits yangBits = (YangBits) tmpNode;
if ((ctx.bitBodyStatement() == null) || (ctx.bitBodyStatement().positionStatement() == null)) {
if (ctx.bitBodyStatement() == null || ctx.bitBodyStatement().positionStatement() == null) {
int maxPosition = 0;
boolean isPositionPresent = false;
......
......@@ -131,7 +131,7 @@ public final class EnumListener {
switch (tmpNode.getYangConstructType()) {
case ENUMERATION_DATA: {
YangEnumeration yangEnumeration = (YangEnumeration) tmpNode;
if ((ctx.enumStatementBody() == null) || (ctx.enumStatementBody().valueStatement() == null)) {
if (ctx.enumStatementBody() == null || ctx.enumStatementBody().valueStatement() == null) {
int maxValue = 0;
boolean isValuePresent = false;
......
......@@ -73,6 +73,8 @@ public final class LengthRestrictionListener {
private static final String PIPE = "|";
private static final String LENGTH_INTERVAL = "..";
private static final int MAX_RANGE_BOUNDARY = 2;
private static final int MIN_RANGE_BOUNDARY = 1;
/**
* Creates a new length restriction listener.
......@@ -114,7 +116,8 @@ public final class LengthRestrictionListener {
GeneratedYangParser.LengthStatementContext ctx) {
YangStringRestriction stringRestriction;
YangBuiltInDataTypeInfo<?> startValue, endValue;
YangBuiltInDataTypeInfo<?> startValue;
YangBuiltInDataTypeInfo<?> endValue;
YangRangeRestriction lengthRestriction = new YangRangeRestriction<>();
if (type.getDataType() != YangDataTypes.STRING && type.getDataType() != YangDataTypes.DERIVED) {
......@@ -148,11 +151,12 @@ public final class LengthRestrictionListener {
String[] rangeArguments = rangeArgument.trim().split(Pattern.quote(PIPE));
for (String rangePart : rangeArguments) {
String startInterval, endInterval;
String startInterval;
String endInterval;
YangRangeInterval rangeInterval = new YangRangeInterval<>();
String[] rangeBoundary = rangePart.trim().split(Pattern.quote(LENGTH_INTERVAL));
if (rangeBoundary.length > 2) {
if (rangeBoundary.length > MAX_RANGE_BOUNDARY) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(LENGTH_DATA) + " " + rangeArgument +
" is not valid.");
......@@ -161,7 +165,7 @@ public final class LengthRestrictionListener {
throw parserException;
}
if (rangeBoundary.length == 1) {
if (rangeBoundary.length == MIN_RANGE_BOUNDARY) {
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[0];
} else {
......
......@@ -70,6 +70,8 @@ public final class RangeRestrictionListener {
private static final String PIPE = "|";
private static final String RANGE_INTERVAL = "..";
private static final int MAX_RANGE_BOUNDARY = 2;
private static final int MIN_RANGE_BOUNDARY = 1;
/**
* Creates a new range restriction listener.
......@@ -110,18 +112,20 @@ public final class RangeRestrictionListener {
private static void setRangeRestriction(YangType type,
GeneratedYangParser.RangeStatementContext ctx) {
YangBuiltInDataTypeInfo<?> startValue, endValue;
YangBuiltInDataTypeInfo<?> startValue;
YangBuiltInDataTypeInfo<?> endValue;
YangRangeRestriction rangeRestriction = new YangRangeRestriction();
String rangeArgument = removeQuotesAndHandleConcat(ctx.range().getText());
String[] rangeArguments = rangeArgument.trim().split(Pattern.quote(PIPE));
for (String rangePart : rangeArguments) {
String startInterval, endInterval;
String startInterval;
String endInterval;
YangRangeInterval rangeInterval = new YangRangeInterval();
String[] rangeBoundary = rangePart.trim().split(Pattern.quote(RANGE_INTERVAL));
if (rangeBoundary.length > 2) {
if (rangeBoundary.length > MAX_RANGE_BOUNDARY) {
ParserException parserException = new ParserException("YANG file error : " +
YangConstructType.getYangConstructType(RANGE_DATA) + " " + rangeArgument +
" is not valid.");
......@@ -130,7 +134,7 @@ public final class RangeRestrictionListener {
throw parserException;
}
if (rangeBoundary.length == 1) {
if (rangeBoundary.length == MIN_RANGE_BOUNDARY) {
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[0];
} else {
......
......@@ -31,7 +31,6 @@ import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.ResolvableStatus.UNRESOLVED;
import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.addResolutionInfo;
......@@ -89,7 +88,7 @@ public final class TypeListener {
checkStackIsNotEmpty(listener, MISSING_HOLDER, TYPE_DATA, ctx.string().getText(), ENTRY);
// Validate node identifier.
YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), YangConstructType.TYPE_DATA,
YangNodeIdentifier nodeIdentifier = getValidNodeIdentifier(ctx.string().getText(), TYPE_DATA,
ctx);
// Obtain the YANG data type.
......@@ -107,7 +106,7 @@ public final class TypeListener {
switch (tmpData.getYangConstructType()) {
case LEAF_DATA:
YangLeaf leaf = (YangLeaf) tmpData;
leaf.setDataType((YangType<?>) type);
leaf.setDataType(type);
/*
* If data type is derived, resolution information to be added
......@@ -139,7 +138,7 @@ public final class TypeListener {
break;
case LEAF_LIST_DATA:
YangLeafList leafList = (YangLeafList) tmpData;
leafList.setDataType((YangType<?>) type);
leafList.setDataType(type);
/*
* If data type is derived, resolution information to be added
......@@ -157,9 +156,6 @@ public final class TypeListener {
ctx.string().getText(), EXIT));
}
// Get the prefix information
String prefix = ((YangType<?>) type).getPrefix();
// Create empty derived info and attach it to type extended info.
YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
......@@ -174,7 +170,7 @@ public final class TypeListener {
case UNION_DATA:
YangUnion unionNode = (YangUnion) tmpData;
try {
unionNode.addType((YangType<?>) type);
unionNode.addType(type);
} catch (DataModelException e) {
ParserException parserException = new ParserException(e.getMessage());
parserException.setLine(ctx.getStart().getLine());
......@@ -188,16 +184,13 @@ public final class TypeListener {
*/
if (yangDataTypes == YangDataTypes.DERIVED) {
// Get the prefix information
String prefix = ((YangType<?>) type).getPrefix();
// Create empty derived info and attach it to type extended info.
YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
// Add resolution information to the list
YangResolutionInfo resolutionInfo =
new YangResolutionInfo<YangType>(type, (YangNode) unionNode, errorLine, errorPosition);
new YangResolutionInfo<YangType>(type, unionNode, errorLine, errorPosition);
addToResolutionList(resolutionInfo, ctx);
}
......@@ -205,24 +198,20 @@ public final class TypeListener {
case TYPEDEF_DATA:
/* Prepare the base type info and set in derived type */
YangTypeDef typeDef = (YangTypeDef) tmpData;
typeDef.setDataType((YangType<?>) type);
typeDef.setDataType(type);
/*
* If data type is derived, resolution information to be added
* in resolution list.
*/
if (yangDataTypes == YangDataTypes.DERIVED) {
// Get the prefix information
String prefix = ((YangType<?>) type).getPrefix();
// Create empty derived info and attach it to type extended info.
YangDerivedInfo<?> yangDerivedInfo = new YangDerivedInfo<>();
((YangType<YangDerivedInfo>) type).setDataTypeExtendedInfo(yangDerivedInfo);
// Add resolution information to the list
YangResolutionInfo resolutionInfo =
new YangResolutionInfo<YangType>(type, (YangNode) typeDef, errorLine, errorPosition);
new YangResolutionInfo<YangType>(type, typeDef, errorLine, errorPosition);
addToResolutionList(resolutionInfo, ctx);
}
break;
......
......@@ -51,7 +51,6 @@ import org.onosproject.yangutils.parser.Parsable;
import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
import org.onosproject.yangutils.parser.exceptions.ParserException;
import org.onosproject.yangutils.parser.impl.TreeWalkListener;
import org.onosproject.yangutils.utils.YangConstructType;
import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangUnionNode;
......@@ -214,7 +213,7 @@ public final class UnionListener {
curNode.addChild(unionNode);
} catch (DataModelException e) {
throw new ParserException(constructExtendedListenerErrorMessage(UNHANDLED_PARSED_DATA,
YangConstructType.UNION_DATA, "", ENTRY, e.getMessage()));
UNION_DATA, "", ENTRY, e.getMessage()));
}
}
}
......
......@@ -117,8 +117,6 @@ public class YangUtilManager extends AbstractMojo {
private static final String DEFAULT_PKG = SLASH + getPackageDirPathFromJavaJPackage(DEFAULT_BASE_PKG);
private YangUtilsParser yangUtilsParser = new YangUtilsParserManager();
private String searchDir;
private String codeGenDir;
private YangNode rootNode;
@Override
......@@ -132,12 +130,12 @@ public class YangUtilManager extends AbstractMojo {
clean(getDirectory(baseDir, genFilesDir) + DEFAULT_PKG);
clean(getDirectory(baseDir, outputDirectory));
searchDir = getDirectory(baseDir, yangFilesDir);
codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
String searchDir = getDirectory(baseDir, yangFilesDir);
String codeGenDir = getDirectory(baseDir, genFilesDir) + SLASH;
YangToJavaNamingConflictUtil conflictResolver = new YangToJavaNamingConflictUtil();
conflictResolver.setReplacementForPeriod(replacementForPeriod);
conflictResolver.setReplacementForHyphen(replacementForHyphen);
conflictResolver.setReplacementForUnderscore(replacementForHyphen);
conflictResolver.setReplacementForUnderscore(replacementForUnderscore);
List<String> yangFiles = YangFileScanner.getYangFiles(searchDir);
YangPluginConfig yangPlugin = new YangPluginConfig();
yangPlugin.setCodeGenDir(codeGenDir);
......
......@@ -132,16 +132,14 @@ public final class JavaCodeGeneratorUtil {
/**
* Free other YANG nodes of data-model tree when error occurs while file generation of current node.
*
* @throws DataModelException when fails to do datamodel operations
*/
public static void freeRestResources() throws DataModelException {
public static void freeRestResources() {
YangNode curNode = getCurNode();
YangNode tempNode = curNode;
TraversalType curTraversal = ROOT;
while (!(curNode == tempNode.getParent())) {
while (curNode != tempNode.getParent()) {
if (curTraversal != PARENT && curNode.getChild() != null) {
curTraversal = CHILD;
......
......@@ -162,6 +162,29 @@ public class JavaImportData {
}
/**
* Returns import for class.
*
* @return imports for class
*/
public List<String> getImports() {
String importString;
List<String> imports = new ArrayList<>();
for (JavaQualifiedTypeInfo importInfo : getImportSet()) {
if (!importInfo.getPkgInfo().equals(EMPTY_STRING) && importInfo.getClassInfo() != null
&& !importInfo.getPkgInfo().equals(JAVA_LANG)) {
importString = IMPORT + importInfo.getPkgInfo() + PERIOD + importInfo.getClassInfo() + SEMI_COLAN
+ NEW_LINE;
imports.add(importString);
}
}
sort(imports);
return imports;
}
/**
* Returns import for hash and equals method.
*
* @return import for hash and equals method
......
......@@ -110,14 +110,14 @@ public class JavaQualifiedTypeInfo implements Comparable<JavaQualifiedTypeInfo>
* Current leaves holder is adding a leaf info as a attribute to the
* current class.
*/
String className = AttributesJavaDataType.getJavaImportClass(attrType, isListAttr);
String className = getJavaImportClass(attrType, isListAttr);
if (className != null) {
/*
* Corresponding to the attribute type a class needs to be imported,
* since it can be a derived type or a usage of wrapper classes.
*/
importInfo.setClassInfo(className);
String classPkg = AttributesJavaDataType.getJavaImportPackage(attrType, isListAttr, className);
String classPkg = getJavaImportPackage(attrType, isListAttr, className);
if (classPkg == null) {
throw new TranslatorException("import package cannot be null when the class is used");
}
......
......@@ -29,6 +29,7 @@ import org.onosproject.yangutils.datamodel.YangLeafList;
import org.onosproject.yangutils.datamodel.YangLeavesHolder;
import org.onosproject.yangutils.datamodel.YangNode;
import org.onosproject.yangutils.datamodel.YangType;
import org.onosproject.yangutils.datamodel.YangCase;
import org.onosproject.yangutils.translator.exception.TranslatorException;
import static org.onosproject.yangutils.datamodel.YangNodeType.MODULE_NODE;
......@@ -110,6 +111,8 @@ 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.UtilConstants.IMPORT;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
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.JavaDocType.GETTER_METHOD;
......@@ -393,6 +396,11 @@ public class TempJavaCodeFragmentFiles {
private YangNode curYangNode;
/**
* Import info for case.
*/
private JavaQualifiedTypeInfo caseImportInfo;
/**
* Current enum's value.
*/
private int enumValue;
......@@ -1130,9 +1138,8 @@ public class TempJavaCodeFragmentFiles {
* Adds build method for interface.
*
* @return build method for interface
* @throws IOException when fails to append to temporary file
*/
public String addBuildMethodForInterface() throws IOException {
public String addBuildMethodForInterface() {
return parseBuilderInterfaceBuildMethodString(generatedJavaClassName);
}
......@@ -1140,9 +1147,8 @@ public class TempJavaCodeFragmentFiles {
* Adds build method's implementation for class.
*
* @return build method implementation for class
* @throws IOException when fails to append to temporary file
*/
public String addBuildMethodImpl() throws IOException {
public String addBuildMethodImpl() {
return getBuildString(generatedJavaClassName) + NEW_LINE;
}
......@@ -1162,9 +1168,8 @@ public class TempJavaCodeFragmentFiles {
* @param modifier modifier for constructor.
* @param toAppend string which need to be appended with the class name
* @return default constructor for class
* @throws IOException when fails to append to file
*/
public String addDefaultConstructor(String modifier, String toAppend) throws IOException {
public String addDefaultConstructor(String modifier, String toAppend) {
return NEW_LINE + getDefaultConstructorString(generatedJavaClassName + toAppend, modifier);
}
......@@ -1172,9 +1177,8 @@ public class TempJavaCodeFragmentFiles {
* Adds default constructor for class.
*
* @return default constructor for class
* @throws IOException when fails to append to file
*/
public String addOfMethod() throws IOException {
public String addOfMethod() {
return getJavaDoc(OF_METHOD, generatedJavaClassName, false)
+ getOfMethod(generatedJavaClassName, newAttrInfo);
}
......@@ -1380,6 +1384,28 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Adds parent's info to current node import list.
*
* @param curNode current node for which import list needs to be updated
*/
public void addParentInfoInCurNodeTempFile(YangNode curNode) {
caseImportInfo = new JavaQualifiedTypeInfo();
YangNode parent = getParentNodeInGenCode(curNode);
if (!(parent instanceof JavaCodeGenerator)) {
throw new TranslatorException("missing parent node to contain current node info in generated file");
}
if (!(curNode instanceof HasJavaFileInfo)) {
throw new TranslatorException("missing java file information to get the package details "
+ "of attribute corresponding to child node");
}
caseImportInfo.setClassInfo(getCaptialCase(getCamelCase(parent.getName(), null)));
caseImportInfo.setPkgInfo(((HasJavaFileInfo) parent).getJavaFileInfo().getPackage());
((HasJavaImportData) curNode).getJavaImportData().addImportInfo(curNode, caseImportInfo);
}
/**
* Adds leaf attributes in generated files.
*
* @param listOfLeaves list of YANG leaf
......@@ -1415,7 +1441,7 @@ public class TempJavaCodeFragmentFiles {
* Check if the attribute is of type list, then the java.lang.list
* needs to be imported.
*/
if (listOfLeafList.size() != 0) {
if (!listOfLeafList.isEmpty()) {
if (!(curNode instanceof HasJavaImportData)) {
throw new TranslatorException("missing import info in current data model node");
......@@ -1651,6 +1677,18 @@ public class TempJavaCodeFragmentFiles {
if ((fileType & INTERFACE_MASK) != 0 | (fileType & BUILDER_INTERFACE_MASK) != 0) {
/**
* Adds import for case.
*/
if (curNode instanceof YangCase) {
List<String> importData = ((HasJavaImportData) curNode).getJavaImportData().getImports();
for (String importInfo : importData) {
if (!imports.contains(importInfo)) {
imports.add(importInfo);
}
}
}
/**
* Adds import for HasAugmentation class.
*/
if (isHasAugmentationExtended(getExtendsList())) {
......@@ -1689,6 +1727,10 @@ public class TempJavaCodeFragmentFiles {
if (isAugmentedInfoExtended(getExtendsList())) {
addAugmentedInfoImport(curNode, imports, false);
}
if (curNode instanceof YangCase) {
removeCaseImport(imports);
}
}
if ((fileType & BUILDER_CLASS_MASK) != 0 | (fileType & IMPL_CLASS_MASK) != 0) {
......@@ -1763,6 +1805,21 @@ public class TempJavaCodeFragmentFiles {
}
/**
* Removes case import info from import list.
*
* @param imports list of imports
* @return import for class
*/
private List<String> removeCaseImport(List<String> imports) {
if (imports != null && caseImportInfo != null) {
String caseImport = IMPORT + caseImportInfo.getPkgInfo() + PERIOD + caseImportInfo.getClassInfo() +
SEMI_COLAN + NEW_LINE;
imports.remove(caseImport);
}
return imports;
}
/**
* Removes all temporary file handles.
*
* @param isErrorOccurred when translator fails to generate java files we need to close
......
......@@ -139,8 +139,7 @@ public class YangJavaCase extends YangCase implements JavaCodeGeneratorInfo, Jav
* Creates a java file using the YANG case info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().generateJavaFile(GENERATE_INTERFACE_WITH_BUILDER, this);
}
}
......
......@@ -24,7 +24,7 @@ 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_INTERFACE_WITH_BUILDER;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.INTERFACE_MASK;
import static org.onosproject.yangutils.translator.tojava.utils.YangJavaModelUtils.generateCodeOfNode;
/**
......@@ -56,7 +56,7 @@ public class YangJavaChoice extends YangChoice implements JavaCodeGeneratorInfo,
super();
setJavaFileInfo(new JavaFileInfo());
setJavaImportData(new JavaImportData());
getJavaFileInfo().setGeneratedFileTypes(GENERATE_INTERFACE_WITH_BUILDER);
getJavaFileInfo().setGeneratedFileTypes(INTERFACE_MASK);
}
/**
......@@ -133,15 +133,13 @@ public class YangJavaChoice extends YangChoice implements JavaCodeGeneratorInfo,
@Override
public void generateCodeEntry(YangPluginConfig yangPlugin) throws IOException {
generateCodeOfNode(this, yangPlugin, false);
// TODO:getTempJavaCodeFragmentFiles().addCurNodeLeavesInfoToTempFiles(this);
}
/**
* Creates a java file using the YANG choice info.
*/
@Override
public void generateCodeExit() {
// TODO Auto-generated method stub
public void generateCodeExit() throws IOException {
getTempJavaCodeFragmentFiles().generateJavaFile(INTERFACE_MASK, this);
}
}
......
......@@ -109,7 +109,7 @@ public final class ClassDefinitionGenerator {
}
String def = PUBLIC + SPACE + INTERFACE + SPACE + yangName + SPACE + EXTEND + SPACE;
for (String extend : getExtendsList()) {
def = def + extend + COMMA;
def = def + extend + COMMA + SPACE;
}
def = trimAtLast(def, COMMA);
......
......@@ -373,7 +373,7 @@ public final class MethodsGenerator {
private static String getReturnType(JavaAttributeInfo attr) {
String returnType = EMPTY_STRING;
if (attr.isQualifiedName() && (attr.getImportInfo().getPkgInfo() != null)) {
if (attr.isQualifiedName() && attr.getImportInfo().getPkgInfo() != null) {
returnType = attr.getImportInfo().getPkgInfo() + PERIOD;
}
returnType = returnType + attr.getImportInfo().getClassInfo();
......
......@@ -141,7 +141,7 @@ public final class TempJavaCodeFragmentFilesUtils {
*/
public static void prepareJavaFileGeneratorForExtendsList(List<String> extendsList) {
if (!extendsList.isEmpty() && !extendsList.equals(null)) {
if (extendsList != null && !extendsList.isEmpty()) {
JavaFileGenerator.setExtendsList(extendsList);
JavaFileGenerator.setIsExtendsList(true);
} else {
......@@ -157,10 +157,7 @@ public final class TempJavaCodeFragmentFilesUtils {
* @return true or false
*/
public static boolean isHasAugmentationExtended(List<String> extendsList) {
if (extendsList != null && extendsList.contains(HAS_AUGMENTATION)) {
return true;
}
return false;
return (extendsList != null && extendsList.contains(HAS_AUGMENTATION));
}
/**
......@@ -170,10 +167,7 @@ public final class TempJavaCodeFragmentFilesUtils {
* @return true or false
*/
public static boolean isAugmentedInfoExtended(List<String> extendsList) {
if (extendsList != null && extendsList.contains(AUGMENTED_INFO)) {
return true;
}
return false;
return (extendsList != null && extendsList.contains(AUGMENTED_INFO));
}
/**
......
......@@ -39,6 +39,7 @@ import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSy
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
import static org.onosproject.yangutils.utils.UtilConstants.AUGMENTED_INFO;
import static org.onosproject.yangutils.utils.UtilConstants.HAS_AUGMENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
/**
......@@ -159,8 +160,10 @@ public final class YangJavaModelUtils {
updatePackageInfo((HasJavaFileInfo) javaCodeGeneratorInfo, yangPlugin);
generateTempFiles(javaCodeGeneratorInfo, yangPlugin.getCodeGenDir());
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
if (!(javaCodeGeneratorInfo instanceof YangCase)) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
.addCurNodeInfoInParentTempFile((YangNode) javaCodeGeneratorInfo, isMultiInstance);
}
/**
* For augmentation of nodes.
......@@ -176,6 +179,21 @@ public final class YangJavaModelUtils {
} else if (javaCodeGeneratorInfo instanceof YangAugment) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(AUGMENTED_INFO);
}
if (javaCodeGeneratorInfo instanceof YangCase) {
YangNode parent = ((YangCase) javaCodeGeneratorInfo).getParent();
String curNodeName = ((YangCase) javaCodeGeneratorInfo).getName();
if (!parent.getName().equals(curNodeName)) {
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(getCaptialCase(getCamelCase(
parent.getName(), null)));
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addParentInfoInCurNodeTempFile((YangNode)
javaCodeGeneratorInfo);
} else {
String parentPackage = ((HasJavaFileInfo) parent).getJavaFileInfo().getPackage();
String caseExtendInfo = parentPackage + PERIOD + parent.getName();
javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles().addToExtendsList(caseExtendInfo);
}
}
}
/**
......
......@@ -22,12 +22,6 @@ package org.onosproject.yangutils.translator.tojava.utils;
public final class YangPluginConfig {
/**
* Creates an object for YANG plugin config.
*/
public YangPluginConfig() {
}
/**
* Contains the code generation directory.
*/
private String codeGenDir;
......@@ -38,6 +32,12 @@ public final class YangPluginConfig {
private YangToJavaNamingConflictUtil conflictResolver;
/**
* Creates an object for YANG plugin config.
*/
public YangPluginConfig() {
}
/**
* Sets the path of the java code where it has to be generated.
*
* @param codeGenDir path of the directory
......
......@@ -26,12 +26,6 @@ import java.util.List;
public final class UtilConstants {
/**
* Creates an instance of util constants.
*/
private UtilConstants() {
}
/**
* JavaDocs for impl class.
*/
public static final String IMPL_CLASS_JAVA_DOC = " * Represents the implementation of ";
......@@ -841,4 +835,10 @@ public final class UtilConstants {
* Static attribute for array list.
*/
public static final String ARRAY_LIST = "ArrayList";
/**
* Creates an instance of util constants.
*/
private UtilConstants() {
}
}
......
......@@ -55,10 +55,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");
if (pkgDir.exists() && pkgWithFile.isFile()) {
return true;
}
return false;
return (pkgDir.exists() && pkgWithFile.isFile());
}
/**
......
......@@ -376,10 +376,10 @@ public final class JavaDocGen {
* @return javaDocs
*/
private static String generateForTypeDefSetter(String attribute) {
return (NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION
return NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION
+ JAVA_DOC_SETTERS_COMMON + 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);
+ NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
/**
......@@ -491,10 +491,9 @@ public final class JavaDocGen {
* @return javaDocs for type constructor
*/
private static String generateForTypeConstructor(String attribute) {
return (NEW_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_FIRST_LINE + FOUR_SPACE_INDENTATION + JAVA_DOC_CONSTRUCTOR
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);
+ FOUR_SPACE_INDENTATION + JAVA_DOC_END_LINE;
}
}
......
......@@ -223,7 +223,7 @@ public final class YangIoUtils {
for (File file : files) {
Files.copy(file.toPath(),
(new File(path + file.getName())).toPath(),
new File(path + file.getName()).toPath(),
StandardCopyOption.REPLACE_EXISTING);
}
Resource rsc = new Resource();
......
......@@ -53,7 +53,7 @@ public class IntraFileTypeLinkingTest {
YangNode node = manager.getDataModel("src/test/resources/SelfResolutionWhenTypeAndTypedefAtRootLevel.yang");
// Check whether the data model tree returned is of type module.
assertThat((node instanceof YangModule), is(true));
assertThat(node instanceof YangModule, is(true));
// Check whether the node type is set properly to module.
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
......@@ -72,7 +72,7 @@ public class IntraFileTypeLinkingTest {
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) node.getChild()));
assertThat((leafInfo.getDataType().getResolvableStatus()),
assertThat(leafInfo.getDataType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
}
......@@ -111,7 +111,7 @@ public class IntraFileTypeLinkingTest {
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) node.getChild()));
assertThat((leafInfo.getDataType().getResolvableStatus()),
assertThat(leafInfo.getDataType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
}
......@@ -151,7 +151,7 @@ public class IntraFileTypeLinkingTest {
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) node.getChild().getNextSibling()));
assertThat((leafInfo.getDataType().getResolvableStatus()),
assertThat(leafInfo.getDataType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
}
......@@ -191,7 +191,7 @@ public class IntraFileTypeLinkingTest {
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) yangContainer.getChild().getNextSibling()));
assertThat((leafInfo.getDataType().getResolvableStatus()),
assertThat(leafInfo.getDataType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
}
......@@ -228,21 +228,21 @@ public class IntraFileTypeLinkingTest {
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) yangList.getChild()));
assertThat((leafInfo.getDataType().getResolvableStatus()),
assertThat(leafInfo.getDataType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
YangTypeDef typeDef1 = (YangTypeDef) yangList.getChild();
assertThat(((YangDerivedInfo<?>) typeDef1.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) yangContainer.getChild().getNextSibling()));
assertThat((typeDef1.getTypeDefBaseType().getResolvableStatus()),
assertThat(typeDef1.getTypeDefBaseType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
YangTypeDef typeDef2 = (YangTypeDef) yangContainer.getChild().getNextSibling();
assertThat(((YangDerivedInfo<?>) typeDef2.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) node.getChild()));
assertThat((typeDef2.getTypeDefBaseType().getResolvableStatus()),
assertThat(typeDef2.getTypeDefBaseType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
}
......@@ -280,21 +280,21 @@ public class IntraFileTypeLinkingTest {
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) yangList.getChild()));
assertThat((leafInfo.getDataType().getResolvableStatus()),
assertThat(leafInfo.getDataType().getResolvableStatus(),
is(ResolvableStatus.INTRA_FILE_RESOLVED));
YangTypeDef typeDef1 = (YangTypeDef) yangList.getChild();
assertThat(((YangDerivedInfo<?>) typeDef1.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) yangContainer.getChild().getNextSibling()));
assertThat((typeDef1.getTypeDefBaseType().getResolvableStatus()),
assertThat(typeDef1.getTypeDefBaseType().getResolvableStatus(),
is(ResolvableStatus.INTRA_FILE_RESOLVED));
YangTypeDef typeDef2 = (YangTypeDef) yangContainer.getChild().getNextSibling();
assertThat(((YangDerivedInfo<?>) typeDef2.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) node.getChild()));
assertThat((typeDef2.getTypeDefBaseType().getResolvableStatus()),
assertThat(typeDef2.getTypeDefBaseType().getResolvableStatus(),
is(ResolvableStatus.INTRA_FILE_RESOLVED));
}
......@@ -331,21 +331,21 @@ public class IntraFileTypeLinkingTest {
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) yangList.getChild()));
assertThat((leafInfo.getDataType().getResolvableStatus()),
assertThat(leafInfo.getDataType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
YangTypeDef typeDef1 = (YangTypeDef) yangList.getChild();
assertThat(((YangDerivedInfo<?>) typeDef1.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) yangContainer.getChild().getNextSibling()));
assertThat((typeDef1.getTypeDefBaseType().getResolvableStatus()),
assertThat(typeDef1.getTypeDefBaseType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
YangTypeDef typeDef2 = (YangTypeDef) yangContainer.getChild().getNextSibling();
assertThat(((YangDerivedInfo<?>) typeDef2.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) node.getChild()));
assertThat((typeDef2.getTypeDefBaseType().getResolvableStatus()),
assertThat(typeDef2.getTypeDefBaseType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
}
......@@ -383,7 +383,7 @@ public class IntraFileTypeLinkingTest {
assertThat(((YangDerivedInfo<?>) leafInfo.getDataType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) yangList.getChild()));
assertThat((leafInfo.getDataType().getResolvableStatus()),
assertThat(leafInfo.getDataType().getResolvableStatus(),
is(ResolvableStatus.INTRA_FILE_RESOLVED));
YangTypeDef typeDef1 = (YangTypeDef) yangList.getChild();
......@@ -392,7 +392,7 @@ public class IntraFileTypeLinkingTest {
assertThat(((YangDerivedInfo<?>) typeDef2.getTypeDefBaseType().getDataTypeExtendedInfo()).getReferredTypeDef(),
is((YangTypeDef) node.getChild()));
assertThat((typeDef2.getTypeDefBaseType().getResolvableStatus()),
assertThat(typeDef2.getTypeDefBaseType().getResolvableStatus(),
is(ResolvableStatus.RESOLVED));
}
......
......@@ -27,22 +27,22 @@ import org.onosproject.yangutils.parser.exceptions.ParserException;
*/
public final class CustomExceptionMatcher extends TypeSafeMatcher<ParserException> {
private int actualLine;
private final int expectedLine;
private int actualCharPosition;
private final int expectedCharPosition;
/**
* Customized exception matcher to match error location.
*
* @param line error line
* @param charPosition error character position
* @return
* @return customized exception matcher to match error location
*/
public static CustomExceptionMatcher errorLocation(int line, int charPosition) {
return new CustomExceptionMatcher(line, charPosition);
}
private int actualLine;
private final int expectedLine;
private int actualCharPosition;
private final int expectedCharPosition;
private CustomExceptionMatcher(int expectedLine, int expectedCharPosition) {
this.expectedLine = expectedLine;
this.expectedCharPosition = expectedCharPosition;
......
......@@ -54,9 +54,6 @@ public class AugmentListenerTest {
assertThat(yangNode.getName(), is("Test"));
YangAugment yangAugment = (YangAugment) yangNode.getChild();
if (yangAugment.getTargetNode().isEmpty()) {
System.out.println("list is empty");
}
ListIterator<YangNodeIdentifier> nodeIdentifierIterator = yangAugment.getTargetNode().listIterator();
YangNodeIdentifier yangNodeIdentifier = nodeIdentifierIterator.next();
assertThat(yangNodeIdentifier.getPrefix(), is("if"));
......
/*
* 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.clean;
/**
* Unit tests for choice-case translator.
*/
public final class ChoiceCaseTranslatorTest {
private final YangUtilsParserManager manager = new YangUtilsParserManager();
/**
* Checks choice-case translation should not result in any exception.
*/
@Test
public void processChoiceCaseTranslator() throws IOException, ParserException {
String userDir = System.getProperty("user.dir");
YangNode node = manager.getDataModel("src/test/resources/ChoiceCaseTranslator.yang");
YangPluginConfig yangPluginConfig = new YangPluginConfig();
yangPluginConfig.setCodeGenDir(userDir + "/target/ChoiceCaseTestGenFile/");
generateJavaCode(node, yangPluginConfig);
clean(userDir + "/target/ChoiceCaseTestGenFile/");
}
// TODO enhance the test cases, after having a framework of translator test.
}
\ No newline at end of file
......@@ -28,21 +28,18 @@ import java.util.Calendar;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;
import static org.apache.commons.io.FileUtils.contentEquals;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
import static org.onosproject.yangutils.utils.io.impl.CopyrightHeader.getCopyrightHeader;
import static org.slf4j.LoggerFactory.getLogger;
/**
* Unit Tests for the CopyrightHeader contents.
*/
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
......
......@@ -26,14 +26,12 @@ import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertThat;
import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getJavaFiles;
import static org.onosproject.yangutils.utils.io.impl.YangFileScanner.getYangFiles;
import static org.slf4j.LoggerFactory.getLogger;
import static java.io.File.separator;
......@@ -42,8 +40,6 @@ import static java.io.File.separator;
*/
public final class YangFileScannerTest {
private final Logger log = getLogger(getClass());
@Rule
public ExpectedException thrown = ExpectedException.none();
......
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container food {
choice snack {
case sports-arena {
leaf pretzel {
type string;
}
leaf beer {
type string;
}
}
case late-night {
leaf chocolate {
type string;
}
}
}
}
}