janani b
Committed by Ray Milkey

[ONOS-4547, ONOS-4566, ONOS-4575, ONOS-4582, ONOS-4581, ONOS-4600,

ONOS-4598, ONOS-4607, ONOS-4610, ONOS-4611] Prefix addition from config
 and defect fixes.

Change-Id: Ieaab5d3e0fe9a1bfa24a2527eeec5435cf0a1b85
Showing 33 changed files with 396 additions and 103 deletions
......@@ -41,4 +41,6 @@
<suppress checks="JavadocPackage"
files=".*/thirdparty/.*.java"/>
<!-- Suppressions for yangutils generated code -->
<suppress files="org.onosproject.yang.gen.v1.*" checks="JavadocStyle" />
</suppressions>
......
......@@ -31,7 +31,7 @@ import org.onosproject.yangutils.utils.YangConstructType;
/**
* Represents the enumeration data type information.
*/
public class YangEnumeration extends YangNode implements Parsable {
public class YangEnumeration extends YangNode implements Parsable, CollisionDetector {
// Enumeration info set.
private Set<YangEnum> enumSet;
......@@ -126,4 +126,18 @@ public class YangEnumeration extends YangNode implements Parsable {
public void validateDataOnExit() throws DataModelException {
// TODO auto-generated method stub, to be implemented by parser
}
@Override
public void detectCollidingChild(String identifierName, YangConstructType dataType) throws DataModelException {
/*
Do nothing.The implementation for this is not required.
*/
}
@Override
public void detectSelfCollision(String identifierName, YangConstructType dataType) throws DataModelException {
/*
Do nothing.The implementation for this is not required.
*/
}
}
......
......@@ -444,8 +444,8 @@ public class YangList extends YangNode
validateConfig(leaves, leafLists);
/* A list must have atleast one key leaf if config is true */
if (isConfig
&& (keys == null || leaves == null && leafLists == null && !isUsesPresentInList())) {
if (isConfig && (keys == null || leaves == null && leafLists == null) && !isUsesPresentInList()
&& !isListPresentInGrouping()) {
throw new DataModelException("A list must have atleast one key leaf if config is true;");
} else if (keys != null) {
validateKey(leaves, leafLists, keys);
......@@ -565,7 +565,7 @@ public class YangList extends YangNode
}
}
if (!leafFound && !isUsesPresentInList()) {
if (!leafFound && !isUsesPresentInList() && !isListPresentInGrouping()) {
throw new DataModelException("An identifier, in key, must refer to a child leaf of the list");
}
leafFound = false;
......@@ -617,6 +617,18 @@ public class YangList extends YangNode
node = node.getNextSibling();
}
return false;
// TODO When grouping linking is done this method has to be modified.
}
private boolean isListPresentInGrouping() {
YangNode node = this.getParent();
while (node != null) {
if (node instanceof YangGrouping) {
return true;
}
node = node.getParent();
}
return false;
// TODO When grouping linking is done this method has to be modified.
}
}
......
......@@ -122,6 +122,12 @@ public class YangUtilManager extends AbstractMojo {
private String replacementForHyphen;
/**
* Prefix which is required for adding with the identifier.
*/
@Parameter(property = "prefixForIdentifier")
private String prefixForIdentifier;
/**
* Build context.
*/
@Component
......@@ -146,6 +152,7 @@ public class YangUtilManager extends AbstractMojo {
conflictResolver.setReplacementForPeriod(replacementForPeriod);
conflictResolver.setReplacementForHyphen(replacementForHyphen);
conflictResolver.setReplacementForUnderscore(replacementForUnderscore);
conflictResolver.setPrefixForIdentifier(prefixForIdentifier);
YangPluginConfig yangPlugin = new YangPluginConfig();
yangPlugin.setCodeGenDir(codeGenDir);
yangPlugin.setConflictResolver(conflictResolver);
......@@ -174,7 +181,7 @@ public class YangUtilManager extends AbstractMojo {
addToSource(getDirectory(baseDir, genFilesDir), project, context);
copyYangFilesToTarget(getYangFileInfoSet(), getDirectory(baseDir, outputDirectory), project);
} catch (Exception e) {
} catch (IOException | ParserException e) {
String fileName = "";
if (getCurYangFileInfo() != null) {
fileName = getCurYangFileInfo().getYangFileName();
......
......@@ -103,8 +103,7 @@ public class JavaQualifiedTypeInfo
* Current leaves holder is adding a leaf info as a attribute to the
* current class.
*/
String className =
AttributesJavaDataType.getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
String className = AttributesJavaDataType.getJavaImportClass(leaf.getDataType(), leaf.isLeafList(),
leaf.getConflictResolveConfig());
if (className != null) {
/*
......@@ -113,7 +112,7 @@ public class JavaQualifiedTypeInfo
*/
importInfo.setClassInfo(className);
String classPkg = AttributesJavaDataType.getJavaImportPackage(leaf.getDataType(),
leaf.isLeafList(), className);
leaf.isLeafList(), className, leaf.getConflictResolveConfig());
if (classPkg == null) {
throw new TranslatorException("import package cannot be null when the class is used");
}
......@@ -166,11 +165,11 @@ public class JavaQualifiedTypeInfo
* Returns the java qualified type information for the wrapper classes.
*
* @param referredTypesAttrInfo attribute of referred type
* @param confilictResolver plugin configurations
* @param conflictResolver plugin configurations
* @return return the import info for this attribute
*/
public static JavaQualifiedTypeInfo getQualifiedInfoOfFromString(JavaAttributeInfo referredTypesAttrInfo,
YangToJavaNamingConflictUtil confilictResolver) {
YangToJavaNamingConflictUtil conflictResolver) {
/*
* Get the java qualified type information for the wrapper classes and
......@@ -179,9 +178,9 @@ public class JavaQualifiedTypeInfo
JavaQualifiedTypeInfo qualifiedInfoOfFromString = new JavaQualifiedTypeInfo();
qualifiedInfoOfFromString.setClassInfo(
getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true, confilictResolver));
getJavaImportClass(referredTypesAttrInfo.getAttributeType(), true, conflictResolver));
qualifiedInfoOfFromString.setPkgInfo(
getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, null));
getJavaImportPackage(referredTypesAttrInfo.getAttributeType(), true, null, conflictResolver));
return qualifiedInfoOfFromString;
}
......
......@@ -34,8 +34,12 @@ import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.
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.JavaIdentifierSyntax.getEnumJavaAttribute;
import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPrefixForIdentifier;
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.UtilConstants.REGEX_FOR_FIRST_DIGIT;
import static org.onosproject.yangutils.utils.UtilConstants.YANG_AUTO_PREFIX;
import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.createPackage;
/**
......@@ -173,7 +177,7 @@ public class TempJavaEnumerationFragmentFiles extends TempJavaFragmentFiles {
/**
* Adds enum class attributes to temporary file.
*
* @param curEnumInfo current YANG enum
* @param curEnumName current YANG enum
* @throws IOException when fails to do IO operations.
*/
private void addAttributesForEnumClass(String curEnumName, YangPluginConfig pluginConfig) throws IOException {
......@@ -194,6 +198,16 @@ public class TempJavaEnumerationFragmentFiles extends TempJavaFragmentFiles {
if (curNode instanceof YangEnumeration) {
YangEnumeration enumeration = (YangEnumeration) curNode;
for (YangEnum curEnum : enumeration.getEnumSet()) {
String enumName = curEnum.getNamedValue();
String prefixForIdentifier = null;
if (enumName.matches(REGEX_FOR_FIRST_DIGIT)) {
prefixForIdentifier = getPrefixForIdentifier(pluginConfig.getConflictResolver());
if (prefixForIdentifier != null) {
curEnum.setNamedValue(prefixForIdentifier + enumName);
} else {
curEnum.setNamedValue(YANG_AUTO_PREFIX + enumName);
}
}
setEnumValue(curEnum.getValue());
addToEnumStringList(curEnum.getNamedValue());
addToEnumSetJavaMap(curEnum.getNamedValue(), curEnum.getValue());
......@@ -228,7 +242,7 @@ public class TempJavaEnumerationFragmentFiles extends TempJavaFragmentFiles {
* @param curEnumName current enum name
*/
private void addToEnumSetJavaMap(String curEnumName, int value) {
getEnumSetJavaMap().put(curEnumName.toUpperCase(), value);
getEnumSetJavaMap().put(getEnumJavaAttribute(curEnumName).toUpperCase(), value);
}
/**
......@@ -240,7 +254,7 @@ public class TempJavaEnumerationFragmentFiles extends TempJavaFragmentFiles {
*/
void addJavaSnippetInfoToApplicableTempFiles(String curEnumName, YangPluginConfig pluginConfig)
throws IOException {
addAttributesForEnumClass(curEnumName, pluginConfig);
addAttributesForEnumClass(getEnumJavaAttribute(curEnumName), pluginConfig);
}
/**
......@@ -279,7 +293,7 @@ public class TempJavaEnumerationFragmentFiles extends TempJavaFragmentFiles {
* @param curEnumValue current enum value
*/
private void addToEnumStringList(String curEnumValue) {
getEnumStringList().add(curEnumValue.toUpperCase());
getEnumStringList().add(getEnumJavaAttribute(curEnumValue).toUpperCase());
}
/**
......
......@@ -28,7 +28,6 @@ import org.onosproject.yangutils.translator.exception.TranslatorException;
import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaType;
import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
import static org.onosproject.yangutils.datamodel.YangDataTypes.DERIVED;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_TYPEDEF_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_UNION_CLASS;
import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.CONSTRUCTOR_FOR_TYPE_MASK;
......@@ -212,10 +211,7 @@ public class TempJavaTypeFragmentFiles
YangJavaType<?> javaType = (YangJavaType<?>) yangType;
javaType.updateJavaQualifiedInfo(pluginConfig.getConflictResolver());
String typeName = javaType.getDataTypeName();
if (javaType.getDataType().equals(DERIVED)) {
typeName = getCamelCase(typeName, pluginConfig.getConflictResolver());
}
JavaAttributeInfo javaAttributeInfo = getAttributeInfoForTheData(
javaType.getJavaQualifiedInfo(),
typeName, javaType,
......
......@@ -126,7 +126,8 @@ public class YangJavaModule
*/
@Override
public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate());
String modulePkg = getRootPackage(getVersion(), getNameSpace().getUri(), getRevision().getRevDate(),
yangPlugin.getConflictResolver());
try {
generateCodeOfRootNode(this, yangPlugin, modulePkg);
} catch (IOException e) {
......
......@@ -139,7 +139,7 @@ public class YangJavaSubModule
@Override
public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
String subModulePkg = getRootPackage(getVersion(), getNameSpaceFromModule(getBelongsTo()),
getRevision().getRevDate());
getRevision().getRevDate(), yangPlugin.getConflictResolver());
try {
generateCodeOfRootNode(this, yangPlugin, subModulePkg);
} catch (IOException e) {
......
......@@ -41,13 +41,13 @@ public class YangJavaType<T>
}
@Override
public void updateJavaQualifiedInfo(YangToJavaNamingConflictUtil confilictResolver) {
public void updateJavaQualifiedInfo(YangToJavaNamingConflictUtil conflictResolver) {
JavaQualifiedTypeInfo importInfo = getJavaQualifiedInfo();
/*
* Type is added as an attribute in the class.
*/
String className = AttributesJavaDataType.getJavaImportClass(this, false, confilictResolver);
String className = AttributesJavaDataType.getJavaImportClass(this, false, conflictResolver);
if (className != null) {
/*
* Corresponding to the attribute type a class needs to be imported,
......@@ -55,7 +55,7 @@ public class YangJavaType<T>
*/
importInfo.setClassInfo(className);
String classPkg = AttributesJavaDataType.getJavaImportPackage(this,
false, className);
false, className, conflictResolver);
if (classPkg == null) {
throw new TranslatorException("import package cannot be null when the class is used");
}
......
......@@ -271,9 +271,11 @@ public final class AttributesJavaDataType {
* @param yangType YANG type
* @param isListAttr if the attribute is of list type
* @param classInfo java import class info
* @param conflictResolver object of YANG to java naming conflict util
* @return java import package
*/
public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr, String classInfo) {
public static String getJavaImportPackage(YangType<?> yangType, boolean isListAttr, String classInfo,
YangToJavaNamingConflictUtil conflictResolver) {
YangDataTypes type = yangType.getDataType();
......@@ -293,7 +295,7 @@ public final class AttributesJavaDataType {
case UINT64:
return JAVA_MATH;
case ENUMERATION:
return getEnumsPackage(yangType);
return getEnumsPackage(yangType, conflictResolver);
case DECIMAL64:
case BITS:
case BINARY:
......@@ -305,12 +307,12 @@ public final class AttributesJavaDataType {
//TODO:IDENTITYREF
break;
case UNION:
return getUnionPackage(yangType);
return getUnionPackage(yangType, conflictResolver);
case INSTANCE_IDENTIFIER:
//TODO:INSTANCE_IDENTIFIER
break;
case DERIVED:
return getTypDefsPackage(yangType);
return getTypDefsPackage(yangType, conflictResolver);
default:
throw new TranslatorException("given data type is not supported.");
}
......@@ -321,7 +323,7 @@ public final class AttributesJavaDataType {
case STRING:
return JAVA_LANG;
case ENUMERATION:
return getEnumsPackage(yangType);
return getEnumsPackage(yangType, conflictResolver);
case DECIMAL64:
case BITS:
case BINARY:
......@@ -335,12 +337,12 @@ public final class AttributesJavaDataType {
case EMPTY:
return JAVA_LANG;
case UNION:
return getUnionPackage(yangType);
return getUnionPackage(yangType, conflictResolver);
case INSTANCE_IDENTIFIER:
//TODO:INSTANCE_IDENTIFIER
break;
case DERIVED:
return getTypDefsPackage(yangType);
return getTypDefsPackage(yangType, conflictResolver);
default:
return null;
}
......@@ -352,9 +354,10 @@ public final class AttributesJavaDataType {
* Returns java package for typedef node.
*
* @param type YANG type
* @param conflictResolver object of YANG to java naming conflict util
* @return java package for typedef node
*/
private static String getTypDefsPackage(YangType<?> type) {
private static String getTypDefsPackage(YangType<?> type, YangToJavaNamingConflictUtil conflictResolver) {
Object var = type.getDataTypeExtendedInfo();
if (!(var instanceof YangDerivedInfo)) {
throw new TranslatorException("type should have been derived.");
......@@ -366,7 +369,7 @@ public final class AttributesJavaDataType {
YangJavaTypeDef typedef = (YangJavaTypeDef) ((YangDerivedInfo<?>) var).getReferredTypeDef();
if (typedef.getJavaFileInfo().getPackage() == null) {
return getPackageFromParent(typedef.getParent());
return getPackageFromParent(typedef.getParent(), conflictResolver);
}
return typedef.getJavaFileInfo().getPackage();
}
......@@ -375,9 +378,10 @@ public final class AttributesJavaDataType {
* Returns java package for union node.
*
* @param type YANG type
* @param conflictResolver object of YANG to java naming conflict util
* @return java package for union node
*/
private static String getUnionPackage(YangType<?> type) {
private static String getUnionPackage(YangType<?> type, YangToJavaNamingConflictUtil conflictResolver) {
if (!(type.getDataTypeExtendedInfo() instanceof YangUnion)) {
throw new TranslatorException("type should have been union.");
......@@ -385,7 +389,7 @@ public final class AttributesJavaDataType {
YangJavaUnion union = (YangJavaUnion) type.getDataTypeExtendedInfo();
if (union.getJavaFileInfo().getPackage() == null) {
return getPackageFromParent(union.getParent());
return getPackageFromParent(union.getParent(), conflictResolver);
}
return union.getJavaFileInfo().getPackage();
}
......@@ -394,16 +398,17 @@ public final class AttributesJavaDataType {
* Returns YANG enumeration's java package.
*
* @param type YANG type
* @param conflictResolver object of YANG to java naming conflict util
* @return YANG enumeration's java package
*/
private static String getEnumsPackage(YangType<?> type) {
private static String getEnumsPackage(YangType<?> type, YangToJavaNamingConflictUtil conflictResolver) {
if (!(type.getDataTypeExtendedInfo() instanceof YangEnumeration)) {
throw new TranslatorException("type should have been enumeration.");
}
YangJavaEnumeration enumeration = (YangJavaEnumeration) type.getDataTypeExtendedInfo();
if (enumeration.getJavaFileInfo().getPackage() == null) {
return getPackageFromParent(enumeration.getParent());
return getPackageFromParent(enumeration.getParent(), conflictResolver);
}
return enumeration.getJavaFileInfo().getPackage();
}
......@@ -412,9 +417,10 @@ public final class AttributesJavaDataType {
* Returns package from parent node.
*
* @param parent parent YANG node
* @param conflictResolver object of YANG to java naming conflict util
* @return java package from parent node
*/
private static String getPackageFromParent(YangNode parent) {
private static String getPackageFromParent(YangNode parent, YangToJavaNamingConflictUtil conflictResolver) {
if (!(parent instanceof JavaFileInfoContainer)) {
throw new TranslatorException("invalid child node is being processed.");
}
......@@ -423,13 +429,13 @@ public final class AttributesJavaDataType {
if (parent instanceof YangJavaModule) {
YangJavaModule module = (YangJavaModule) parent;
String modulePkg = getRootPackage(module.getVersion(), module.getNameSpace().getUri(), module
.getRevision().getRevDate());
.getRevision().getRevDate(), conflictResolver);
return modulePkg + PERIOD + getCamelCase(module.getName(), null).toLowerCase();
} else if (parent instanceof YangJavaSubModule) {
YangJavaSubModule submodule = (YangJavaSubModule) parent;
String subModulePkg = getRootPackage(submodule.getVersion(),
submodule.getNameSpaceFromModule(submodule.getBelongsTo()),
submodule.getRevision().getRevDate());
submodule.getRevision().getRevDate(), conflictResolver);
return subModulePkg + PERIOD + getCamelCase(submodule.getName(), null).toLowerCase();
}
}
......
......@@ -53,6 +53,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
import static org.onosproject.yangutils.utils.UtilConstants.OPEN_CURLY_BRACKET;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PUBLIC;
import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE;
import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
import static org.onosproject.yangutils.utils.UtilConstants.SUBJECT;
......@@ -241,24 +242,25 @@ public final class ClassDefinitionGenerator {
curNode = curNode.getNextSibling();
}
}
if (yangName.contains(SERVICE)) {
if (yangName.matches(REGEX_FOR_ANY_STRING_ENDING_WITH_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;
return PUBLIC + SPACE + CLASS + SPACE + yangName + SPACE + IMPLEMENTS + SPACE
+ yangName.substring(0, yangName.length() - 7) + SERVICE + SPACE + OPEN_CURLY_BRACKET + NEW_LINE;
}
/* Provides class definition when RPC interface needs to extends any event.*/
private static String getRpcInterfaceDefinitionWhenItExtends(String yangName,
JavaExtendsListHolder holder) {
if (yangName.contains(SERVICE)) {
if (yangName.matches(REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE)) {
String[] strArray = yangName.split(SERVICE);
return PUBLIC + SPACE + INTERFACE + SPACE + yangName + NEW_LINE + EIGHT_SPACE_INDENTATION
+ EXTEND + SPACE + LISTENER_SERVICE + DIAMOND_OPEN_BRACKET + strArray[0] + EVENT_STRING + COMMA
+ SPACE + strArray[0] + EVENT_LISTENER_STRING + DIAMOND_CLOSE_BRACKET + SPACE
+ OPEN_CURLY_BRACKET + NEW_LINE;
}
yangName = yangName.substring(0, yangName.length() - 7);
return PUBLIC + SPACE + CLASS + SPACE + yangName + MANAGER + NEW_LINE + EIGHT_SPACE_INDENTATION
+ EXTEND + SPACE + LISTENER_REG + DIAMOND_OPEN_BRACKET + yangName + EVENT_STRING + COMMA + SPACE
+ yangName + EVENT_LISTENER_STRING + DIAMOND_CLOSE_BRACKET + NEW_LINE
......
......@@ -97,6 +97,7 @@ import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATI
import static org.onosproject.yangutils.utils.UtilConstants.IMPL;
import static org.onosproject.yangutils.utils.UtilConstants.INT;
import static org.onosproject.yangutils.utils.UtilConstants.LOGGER_STATEMENT;
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;
......@@ -306,7 +307,7 @@ public final class JavaFileGenerator {
JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
String className = getCapitalCase(javaFileInfo.getJavaName());
String className = getCapitalCase(javaFileInfo.getJavaName()) + MANAGER;
initiateJavaFileGeneration(file, GENERATE_SERVICE_AND_MANAGER, imports, curNode, className);
......
......@@ -72,14 +72,13 @@ import static org.onosproject.yangutils.utils.UtilConstants.EQUAL;
import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
import static org.onosproject.yangutils.utils.UtilConstants.IMMEDIATE;
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.OPEN_PARENTHESIS;
import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
import static org.onosproject.yangutils.utils.UtilConstants.PRIVATE;
import static org.onosproject.yangutils.utils.UtilConstants.REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE;
import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
import static org.onosproject.yangutils.utils.UtilConstants.SERVICE;
import static org.onosproject.yangutils.utils.UtilConstants.SERVICE_ANNOTATION;
import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
......@@ -317,7 +316,7 @@ public final class JavaFileGeneratorUtils {
if (type == GENERATE_EVENT_CLASS
|| type == GENERATE_EVENT_LISTENER_INTERFACE
|| type == GENERATE_EVENT_SUBJECT_CLASS) {
pkgString = parsePackageString(path + PERIOD + name, importsList);
pkgString = parsePackageString((path + PERIOD + name).toLowerCase(), importsList);
} else {
pkgString = parsePackageString(path, importsList);
}
......@@ -454,8 +453,8 @@ public final class JavaFileGeneratorUtils {
YangPluginConfig pluginConfig = ((JavaFileInfoContainer) curNode).getJavaFileInfo().getPluginConfig();
if ((genType & GENERATE_SERVICE_AND_MANAGER) != 0) {
if (!fileName.contains(SERVICE)) {
insertDataIntoJavaFile(file, getJavaDoc(RPC_MANAGER, fileName + MANAGER, false, pluginConfig));
if (!fileName.matches(REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE)) {
insertDataIntoJavaFile(file, getJavaDoc(RPC_MANAGER, fileName, false, pluginConfig));
insertDataIntoJavaFile(file, addComponentString());
} else {
insertDataIntoJavaFile(file, getJavaDoc(javaDocType, fileName, false, pluginConfig));
......
......@@ -75,16 +75,18 @@ public final class JavaIdentifierSyntax {
* @param version YANG version
* @param nameSpace name space of the module
* @param revision revision of the module defined
* @return returns the root package string
* @param conflictResolver object of YANG to java naming conflict util
* @return the root package string
*/
public static String getRootPackage(byte version, String nameSpace, String revision) {
public static String getRootPackage(byte version, String nameSpace, String revision,
YangToJavaNamingConflictUtil conflictResolver) {
String pkg;
pkg = DEFAULT_BASE_PKG;
pkg = pkg + PERIOD;
pkg = pkg + getYangVersion(version);
pkg = pkg + PERIOD;
pkg = pkg + getPkgFromNameSpace(nameSpace);
pkg = pkg + getPkgFromNameSpace(nameSpace, conflictResolver);
pkg = pkg + PERIOD;
pkg = pkg + getYangRevisionStr(revision);
......@@ -144,9 +146,10 @@ public final class JavaIdentifierSyntax {
* Returns package name from name space.
*
* @param nameSpace name space of YANG module
* @param conflictResolver object of YANG to java naming conflict util
* @return java package name as per java rules
*/
private static String getPkgFromNameSpace(String nameSpace) {
private static String getPkgFromNameSpace(String nameSpace, YangToJavaNamingConflictUtil conflictResolver) {
ArrayList<String> pkgArr = new ArrayList<String>();
nameSpace = nameSpace.replace(QUOTES, EMPTY_STRING);
......@@ -156,7 +159,7 @@ public final class JavaIdentifierSyntax {
for (String nameSpaceString : nameSpaceArr) {
pkgArr.add(nameSpaceString);
}
return getPkgFrmArr(pkgArr);
return getPkgFrmArr(pkgArr, conflictResolver);
}
/**
......@@ -194,17 +197,19 @@ public final class JavaIdentifierSyntax {
* Returns the package string.
*
* @param pkgArr package array
* @param conflictResolver object of YANG to java naming conflict util
* @return package string
*/
private static String getPkgFrmArr(ArrayList<String> pkgArr) {
private static String getPkgFrmArr(ArrayList<String> pkgArr, YangToJavaNamingConflictUtil conflictResolver) {
String pkg = EMPTY_STRING;
int size = pkgArr.size();
int i = 0;
for (String member : pkgArr) {
boolean presenceOfKeyword = JAVA_KEY_WORDS.contains(member);
boolean presenceOfKeyword = JAVA_KEY_WORDS.contains(member.toLowerCase());
if (presenceOfKeyword || member.matches(REGEX_FOR_FIRST_DIGIT)) {
member = YANG_AUTO_PREFIX + member;
String prefix = getPrefixForIdentifier(conflictResolver);
member = prefix + member;
}
pkg = pkg + member;
if (i != size - 1) {
......@@ -216,20 +221,40 @@ public final class JavaIdentifierSyntax {
}
/**
* Returns package sub name from YANG identifier name.
* Prefix for adding with identifier and namespace, when it is a java keyword or starting with digits.
*
* @param name YANG identifier name
* @return java package sub name as per java rules
* @param conflictResolver object of YANG to java naming conflict util
* @return prefix which needs to be added
*/
public static String getSubPkgFromName(String name) {
ArrayList<String> pkgArr = new ArrayList<String>();
String[] nameArr = name.split(COLAN);
public static String getPrefixForIdentifier(YangToJavaNamingConflictUtil conflictResolver) {
for (String nameString : nameArr) {
pkgArr.add(nameString);
String prefixForIdentifier = null;
if (conflictResolver != null) {
prefixForIdentifier = conflictResolver.getPrefixForIdentifier();
}
if (prefixForIdentifier != null) {
prefixForIdentifier = prefixForIdentifier.replaceAll(REGEX_WITH_ALL_SPECIAL_CHAR, COLAN);
String[] strArray = prefixForIdentifier.split(COLAN);
try {
if (strArray[0].isEmpty()) {
List<String> stringArrangement = new ArrayList<String>();
for (int i = 1; i < strArray.length; i++) {
stringArrangement.add(strArray[i]);
}
strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
}
prefixForIdentifier = strArray[0];
for (int j = 1; j < strArray.length; j++) {
prefixForIdentifier = prefixForIdentifier + strArray[j].substring(0, 1).toUpperCase() +
strArray[j].substring(1);
}
return getPkgFrmArr(pkgArr);
} catch (ArrayIndexOutOfBoundsException outOfBoundsException) {
throw new TranslatorException("The given prefix in pom.xml is invalid.");
}
} else {
prefixForIdentifier = YANG_AUTO_PREFIX;
}
return prefixForIdentifier;
}
/**
......@@ -267,16 +292,18 @@ public final class JavaIdentifierSyntax {
}
strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
}
return upperCaseConflictResolver(strArray);
return upperCaseConflictResolver(strArray, conflictResolver);
}
/**
* Resolves the conflict when input has uppercase.
* Resolves the conflict when input has upper case.
*
* @param stringArray containing strings for uppercase conflict resolver
* @param stringArray containing strings for upper case conflict resolver
* @param conflictResolver object of YANG to java naming conflict util
* @return camel cased string
*/
private static String upperCaseConflictResolver(String[] stringArray) {
private static String upperCaseConflictResolver(String[] stringArray,
YangToJavaNamingConflictUtil conflictResolver) {
for (int l = 0; l < stringArray.length; l++) {
String[] upperCaseSplitArray = stringArray[l].split(REGEX_WITH_UPPERCASE);
......@@ -317,7 +344,7 @@ public final class JavaIdentifierSyntax {
}
}
stringArray = result.toArray(new String[result.size()]);
return applyCamelCaseRule(stringArray);
return applyCamelCaseRule(stringArray, conflictResolver);
}
/**
......@@ -325,9 +352,10 @@ public final class JavaIdentifierSyntax {
* the letter next to a number in an array.
*
* @param stringArray containing strings for camel case separation
* @param conflictResolver object of YANG to java naming conflict util
* @return camel case rule checked string
*/
private static String applyCamelCaseRule(String[] stringArray) {
private static String applyCamelCaseRule(String[] stringArray, YangToJavaNamingConflictUtil conflictResolver) {
String ruleChecker = stringArray[0].toLowerCase();
int i;
......@@ -359,23 +387,25 @@ public final class JavaIdentifierSyntax {
ruleChecker = ruleChecker + stringArray[i].substring(0, 1).toUpperCase() + stringArray[i].substring(1);
}
}
String ruleCheckerWithPrefix = addPrefix(ruleChecker);
String ruleCheckerWithPrefix = addPrefix(ruleChecker, conflictResolver);
return restrictConsecutiveCapitalCase(ruleCheckerWithPrefix);
}
/**
* Adds prefix YANG auto prefix if the string begins with digit or is a java key word.
* Adds prefix, if the string begins with digit or is a java key word.
*
* @param camelCasePrefix string for adding prefix
* @param conflictResolver object of YANG to java naming conflict util
* @return prefixed camel case string
*/
private static String addPrefix(String camelCasePrefix) {
private static String addPrefix(String camelCasePrefix, YangToJavaNamingConflictUtil conflictResolver) {
String prefix = getPrefixForIdentifier(conflictResolver);
if (camelCasePrefix.matches(REGEX_FOR_FIRST_DIGIT)) {
camelCasePrefix = YANG_AUTO_PREFIX + camelCasePrefix;
camelCasePrefix = prefix + camelCasePrefix;
}
if (JAVA_KEY_WORDS.contains(camelCasePrefix.toLowerCase())) {
camelCasePrefix = YANG_AUTO_PREFIX + camelCasePrefix.substring(0, 1).toUpperCase()
if (JAVA_KEY_WORDS.contains(camelCasePrefix)) {
camelCasePrefix = prefix + camelCasePrefix.substring(0, 1).toUpperCase()
+ camelCasePrefix.substring(1);
}
return camelCasePrefix;
......@@ -444,13 +474,21 @@ public final class JavaIdentifierSyntax {
*/
public static String getEnumJavaAttribute(String name) {
String[] strArray = name.split(HYPHEN);
name = name.replaceAll(REGEX_WITH_ALL_SPECIAL_CHAR, COLAN);
String[] strArray = name.split(COLAN);
String output = EMPTY_STRING;
if (strArray[0].isEmpty()) {
List<String> stringArrangement = new ArrayList<String>();
for (int i = 1; i < strArray.length; i++) {
stringArrangement.add(strArray[i]);
}
strArray = stringArrangement.toArray(new String[stringArrangement.size()]);
}
for (int i = 0; i < strArray.length; i++) {
output = output + strArray[i];
if (i > 0 && i < strArray.length - 1) {
if (i > 0 && i < strArray.length) {
output = output + UNDER_SCORE;
}
output = output + strArray[i];
}
return output;
}
......
......@@ -37,6 +37,11 @@ public final class YangToJavaNamingConflictUtil {
private static String replacementForHyphenInIdentifier;
/**
* Contains the prefix value for adding with the identifier.
*/
private static String prefixForIdentifier;
/**
* Creates an object for YANG to java naming conflict util.
*/
public YangToJavaNamingConflictUtil() {
......@@ -95,4 +100,22 @@ public final class YangToJavaNamingConflictUtil {
public String getReplacementForUnderscore() {
return replacementForUnderscoreInIdentifier;
}
/**
* Sets the prefix value for adding with the identifier.
*
* @param prefix prefix for identifier
*/
public void setPrefixForIdentifier(String prefix) {
prefixForIdentifier = prefix;
}
/**
* Returns the prefix for identifier.
*
* @return prefix for identifier
*/
public String getPrefixForIdentifier() {
return prefixForIdentifier;
}
}
......
......@@ -99,11 +99,11 @@ public final class RestrictionResolver {
}
if (rangeBoundary.length == MIN_RANGE_BOUNDARY) {
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[0];
startInterval = rangeBoundary[0].trim();
endInterval = rangeBoundary[0].trim();
} else {
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[1];
startInterval = rangeBoundary[0].trim();
endInterval = rangeBoundary[1].trim();
}
try {
......@@ -186,11 +186,11 @@ public final class RestrictionResolver {
}
if (rangeBoundary.length == MIN_RANGE_BOUNDARY) {
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[0];
startInterval = rangeBoundary[0].trim();
endInterval = rangeBoundary[0].trim();
} else {
startInterval = rangeBoundary[0];
endInterval = rangeBoundary[1];
startInterval = rangeBoundary[0].trim();
endInterval = rangeBoundary[1].trim();
}
try {
......
......@@ -767,6 +767,11 @@ public final class UtilConstants {
public static final String REGEX_WITH_SINGLE_CAPITAL_CASE_AND_DIGITS_SMALL_CASES = "[A-Z][0-9a-z]+";
/**
* Static attribute for regex for any string ending with service.
*/
public static final String REGEX_FOR_ANY_STRING_ENDING_WITH_SERVICE = ".+Service";
/**
* Static attribute for class syntax.
*/
public static final String CLASS = "class";
......
......@@ -25,8 +25,8 @@ import YangLexer;
package org.onosproject.yangutils.parser.antlrgencode;
}
yangfile : moduleStatement
| subModuleStatement;
yangfile : moduleStatement EOF
| subModuleStatement EOF;
/**
* module-stmt = optsep module-keyword sep identifier-arg-str
......
......@@ -52,4 +52,34 @@ public class TreeWalkListenerTest {
thrown.expectMessage("YANG file error : \"anyxml\" is not supported.");
manager.getDataModel("src/test/resources/AnyxmlStatement.yang");
}
/**
* Checks whether exception is thrown when extra brace is added in the EOF.
*/
@Test
public void processFileWithExtraBrace() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input '}' expecting <EOF>");
manager.getDataModel("src/test/resources/ProcessFileWithExtraBrace.yang");
}
/**
* Checks whether exception is thrown when leaf is given after module ends.
*/
@Test
public void processFileWithExtraLeaf() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input 'leaf' expecting <EOF>");
manager.getDataModel("src/test/resources/ProcessFileWithExtraLeaf.yang");
}
/**
* Checks whether exception is thrown when extra brace is added in between the EOF.
*/
@Test
public void processFileWithExtraBraceInBetween() throws IOException, ParserException {
thrown.expect(ParserException.class);
thrown.expectMessage("mismatched input 'container' expecting <EOF>");
manager.getDataModel("src/test/resources/ProcessFileWithExtraBraceInBetween.yang");
}
}
......
......@@ -267,4 +267,36 @@ public class LengthRestrictionListenerTest {
assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
}
/**
* Checks whether space can be allowed when length statement is present.
*/
@Test
public void processLengthStatementWithSpace() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/LengthStatementWithSpace.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("invalid-interval"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("string"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.STRING));
YangStringRestriction stringRestriction = (YangStringRestriction) leafInfo
.getDataType().getDataTypeExtendedInfo();
YangRangeRestriction lengthRestriction = stringRestriction.getLengthRestriction();
ListIterator<YangRangeInterval> lengthListIterator = lengthRestriction.getAscendingRangeIntervals()
.listIterator();
YangRangeInterval rangeInterval = lengthListIterator.next();
assertThat(((YangUint64) rangeInterval.getStartValue()).getValue(), is(BigInteger.valueOf(0)));
assertThat(((YangUint64) rangeInterval.getEndValue()).getValue(), is(BigInteger.valueOf(100)));
}
}
......
......@@ -207,4 +207,33 @@ public class RangeRestrictionListenerTest {
assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
}
/**
* Checks whether space can be allowed when range statement is present.
*/
@Test
public void processRangeStatementWithSpace() throws IOException, ParserException {
YangNode node = manager.getDataModel("src/test/resources/RangeStatementWithSpace.yang");
assertThat((node instanceof YangModule), is(true));
assertThat(node.getNodeType(), is(YangNodeType.MODULE_NODE));
YangModule yangNode = (YangModule) node;
assertThat(yangNode.getName(), is("Test"));
ListIterator<YangLeaf> leafIterator = yangNode.getListOfLeaf().listIterator();
YangLeaf leafInfo = leafIterator.next();
assertThat(leafInfo.getName(), is("invalid-interval"));
assertThat(leafInfo.getDataType().getDataTypeName(), is("int32"));
assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.INT32));
YangRangeRestriction rangeRestriction = (YangRangeRestriction) leafInfo
.getDataType().getDataTypeExtendedInfo();
ListIterator<YangRangeInterval> rangeListIterator = rangeRestriction.getAscendingRangeIntervals()
.listIterator();
YangRangeInterval rangeInterval = rangeListIterator.next();
assertThat(((YangInt32) rangeInterval.getStartValue()).getValue(), is(1));
assertThat(((YangInt32) rangeInterval.getEndValue()).getValue(), is(4));
}
}
......
......@@ -59,6 +59,7 @@ public class AttributesJavaDataTypeTest {
private static final String CLASS_INFO5 = "Integer";
private static final String TYPE_DEF_PKG = "target.test";
private static String test = "";
private static YangToJavaNamingConflictUtil pluginConfig = null;
/**
* Unit test for private constructor.
......@@ -88,7 +89,6 @@ public class AttributesJavaDataTypeTest {
*/
@Test
public void testgetJavaClassInfo() {
YangToJavaNamingConflictUtil pluginConfig = null;
test = getJavaImportClass(getStubYangType(TYPE1), false, pluginConfig);
assertThat(true, is(test.equals(CLASS_INFO1)));
......@@ -125,16 +125,16 @@ public class AttributesJavaDataTypeTest {
*/
@Test
public void testgetJavaPkgInfo() {
test = getJavaImportPackage(getStubYangType(TYPE1), false, CLASS_INFO1);
test = getJavaImportPackage(getStubYangType(TYPE1), false, CLASS_INFO1, pluginConfig);
assertThat(true, is(test.equals(JAVA_LANG)));
test = getJavaImportPackage(getStubYangType(TYPE2), true, CLASS_INFO5);
test = getJavaImportPackage(getStubYangType(TYPE2), true, CLASS_INFO5, pluginConfig);
assertThat(true, is(test.equals(JAVA_LANG)));
test = getJavaImportPackage(getStubYangType(TYPE3), false, CLASS_INFO3);
test = getJavaImportPackage(getStubYangType(TYPE3), false, CLASS_INFO3, pluginConfig);
assertThat(null, is(test));
test = getJavaImportPackage(getStubYangType(TYPE4), false, CLASS_INFO4);
test = getJavaImportPackage(getStubYangType(TYPE4), false, CLASS_INFO4, pluginConfig);
assertThat(null, is(test));
}
......@@ -145,7 +145,7 @@ public class AttributesJavaDataTypeTest {
*/
@Test
public void testForTypeDef() throws DataModelException {
test = getJavaImportPackage(getStubExtendedInfo(getStubYangType(TYPE_DEF)), false, TYPE_DEF_PKG);
test = getJavaImportPackage(getStubExtendedInfo(getStubYangType(TYPE_DEF)), false, TYPE_DEF_PKG, pluginConfig);
assertThat(true, is(test.equals(TYPE_DEF_PKG)));
}
......
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf invalid-interval {
type string {
length " 0 .. 100 ";
}
}
}
......@@ -11,5 +11,5 @@ module Test {
bit Ten-Mb-only;
}
}
}
}
......
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container food {
choice snack {
list sports-arena {
}
}
}
}
}
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
import ietf-yang-types {
prefix "P";
}
grouping Percentage {
leaf hello{
type string;
}
leaf invalid1{
type string;
}
}
leaf invalid2{
type string;
}
}
container ospf {
list valid {
key "invalid";
leaf invalid{
type string;
}
uses Ant:FirstClass;
grouping FirstClass {
uses P:PassingClass;
}
}
grouping PassingClass {
uses Ant:Percentage;
}
}
}
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
container food {
choice snack {
list sports-arena {
}
}
}
}
leaf invalid {
module Test {
yang-version 1;
namespace http://huawei.com;
prefix Ant;
leaf invalid-interval {
type int32 {
range " 1 .. 4 | 10 .. 20 ";
}
}
}