Fix for code generation of enum name starts with numeric value
Change-Id: Ifef42c85a59d35a90fe1beb88f220517e1816c1b
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/JavaCodeSnippetGen.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/JavaCodeSnippetGen.java
index 396b522..c848110 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/JavaCodeSnippetGen.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/JavaCodeSnippetGen.java
@@ -28,12 +28,13 @@
import java.util.List;
import static java.util.Collections.sort;
-import static org.onosproject.yang.compiler.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
import static org.onosproject.yang.compiler.translator.tojava.utils.JavaFileGeneratorUtils.getYangDataStructure;
+import static org.onosproject.yang.compiler.translator.tojava.utils.JavaIdentifierSyntax.getEnumJavaAttribute;
import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.getDefaultDefinition;
import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.getImportString;
import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.getOpenCloseParaWithValue;
import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.signatureClose;
+import static org.onosproject.yang.compiler.translator.tojava.utils.TranslatorUtils.getEnumYangName;
import static org.onosproject.yang.compiler.utils.UtilConstants.BIT_SET;
import static org.onosproject.yang.compiler.utils.UtilConstants.COMMA;
import static org.onosproject.yang.compiler.utils.UtilConstants.DIAMOND_CLOSE_BRACKET;
@@ -239,7 +240,7 @@
public static String generateEnumAttributeStringWithSchemaName(
String name, int value) {
String enumName = getEnumJavaAttribute(name);
- String str = value + COMMA + SPACE + QUOTES + name + QUOTES;
+ String str = value + COMMA + SPACE + QUOTES + getEnumYangName(name) + QUOTES;
return getJavaDoc(ENUM_ATTRIBUTE, name, false, null) +
FOUR_SPACE_INDENTATION + enumName.toUpperCase() +
getOpenCloseParaWithValue(str) + COMMA + NEW_LINE;
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/MethodsGenerator.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/MethodsGenerator.java
index fbefdd7..c667056 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/MethodsGenerator.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/MethodsGenerator.java
@@ -96,6 +96,7 @@
import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.multiAttrMethodSignature;
import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.signatureClose;
import static org.onosproject.yang.compiler.translator.tojava.utils.StringGenerator.valueAssign;
+import static org.onosproject.yang.compiler.translator.tojava.utils.TranslatorUtils.getEnumYangName;
import static org.onosproject.yang.compiler.translator.tojava.utils.TranslatorUtils.getIdentityRefName;
import static org.onosproject.yang.compiler.utils.UtilConstants.ADD;
import static org.onosproject.yang.compiler.utils.UtilConstants.ADD_AUGMENTATION;
@@ -1208,7 +1209,7 @@
str = getEnumJavaAttribute(yangEnum.getNamedValue())
.toUpperCase();
builder.append(getEnumValueMethodCases(
- QUOTES + yangEnum.getNamedValue() + QUOTES, str,
+ QUOTES + getEnumYangName(yangEnum.getNamedValue()) + QUOTES, str,
name));
}
break;
diff --git a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/TranslatorUtils.java b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/TranslatorUtils.java
index 8e0a149..308128a 100644
--- a/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/TranslatorUtils.java
+++ b/compiler/base/translator/src/main/java/org/onosproject/yang/compiler/translator/tojava/utils/TranslatorUtils.java
@@ -32,6 +32,7 @@
import static org.onosproject.yang.compiler.utils.UtilConstants.AT;
import static org.onosproject.yang.compiler.utils.UtilConstants.IN;
+import static org.onosproject.yang.compiler.utils.UtilConstants.YANG_AUTO_PREFIX;
/**
* Represents common translator utilities.
@@ -164,4 +165,17 @@
}
return name;
}
+
+ /**
+ * Returns enum YANG attribute name for given javaName.
+ *
+ * @param name enum javaName
+ * @return YANG attribute name
+ */
+ public static String getEnumYangName(String name) {
+ if (name.startsWith(YANG_AUTO_PREFIX)) {
+ name = name.replaceFirst(YANG_AUTO_PREFIX, "");
+ }
+ return name;
+ }
}
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/DefaultDataTreeBuilderTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/DefaultDataTreeBuilderTest.java
index dbc768b..c5a2575 100644
--- a/runtime/src/test/java/org/onosproject/yang/runtime/impl/DefaultDataTreeBuilderTest.java
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/DefaultDataTreeBuilderTest.java
@@ -126,6 +126,8 @@
import static org.onosproject.yang.gen.v1.ytbdatatypes.rev20160826.YtbDataTypes.LeafIdentifier.LL8;
import static org.onosproject.yang.gen.v1.ytbdatatypes.rev20160826.YtbDataTypes.LeafIdentifier.LL9;
import static org.onosproject.yang.gen.v1.ytbdatatypes.rev20160826.ytbdatatypes.Cont1.LeafIdentifier.LEAF15;
+import static org.onosproject.yang.gen.v1.ytbdatatypes.rev20160826.ytbdatatypes.Cont1.LeafIdentifier.LEAF16;
+import static org.onosproject.yang.gen.v1.ytbdatatypes.rev20160826.ytbdatatypes.Cont1.LeafIdentifier.LL16;
import static org.onosproject.yang.gen.v1.ytbdatatypes.rev20160826.ytbdatatypes.def1.def1union.Def1UnionEnum1.of;
import static org.onosproject.yang.model.DataNode.Type.MULTI_INSTANCE_LEAF_VALUE_NODE;
import static org.onosproject.yang.model.DataNode.Type.MULTI_INSTANCE_NODE;
@@ -1361,6 +1363,8 @@
SINGLE_INSTANCE_LEAF_VALUE_NODE, true, "physical");
validateDataNode(it.next(), "leaf15", ns,
SINGLE_INSTANCE_LEAF_VALUE_NODE, true, "physical");
+ validateDataNode(it.next(), "leaf16", ns,
+ SINGLE_INSTANCE_LEAF_VALUE_NODE, true, "3.3ms");
validateDataNode(it.next(), "ll1", ns,
MULTI_INSTANCE_LEAF_VALUE_NODE, true, "leaf-list1");
validateDataNode(it.next(), "ll1", ns,
@@ -1413,7 +1417,8 @@
MULTI_INSTANCE_LEAF_VALUE_NODE, true, "virtual");
validateDataNode(it.next(), "ll15", ns,
MULTI_INSTANCE_LEAF_VALUE_NODE, true, "physical");
-
+ validateDataNode(it.next(), "ll16", ns,
+ MULTI_INSTANCE_LEAF_VALUE_NODE, true, "3.3ms");
return it;
}
@@ -1430,6 +1435,8 @@
Def1 def1 = new Def1(union);
Def1Union union2 = new Def1Union(of(10));
Def1Union union22 = new Def1Union(of(10000));
+ Def1Union union33 = new Def1Union(of(100000));
+ Def1 def13 = new Def1(union33);
Def1 def11 = new Def1(union22);
Def1 def12 = new Def1(union2);
byte[] arr = Base64.getDecoder().decode("MTEwMTE=");
@@ -1462,6 +1469,7 @@
cont1.leaf13(def11);
cont1.leaf14(def6);
cont1.leaf15(def71);
+ cont1.leaf16(def13);
cont1.addToLl1("leaf-list1");
cont1.addToLl1("leaf-list1-2");
cont1.addToLl2(def1);
@@ -1490,7 +1498,7 @@
cont1.addToLl14(def6);
cont1.addToLl14(def61);
cont1.addToLl15(def71);
-
+ cont1.addToLl16(def13);
cont1.cont2(cont2);
data.addModelObject((ModelObject) cont1);
return data;
@@ -1511,6 +1519,8 @@
Def1Union union22 = new Def1Union(of(10000));
Def1 def11 = new Def1(union22);
Def1 def12 = new Def1(union2);
+ Def1Union union33 = new Def1Union(of(100000));
+ Def1 def13 = new Def1(union33);
byte[] arr = Base64.getDecoder().decode("MTEwMTE=");
byte[] arr1 = Base64.getDecoder().decode("MTEwMTEx");
BitSet bits = new BitSet();
@@ -1540,6 +1550,7 @@
augC.leaf13(def11);
augC.leaf14(def6);
augC.leaf15(def71);
+ augC.leaf16(def13);
augC.addToLl1("leaf-list1");
augC.addToLl1("leaf-list1-2");
augC.addToLl2(def1);
@@ -1568,6 +1579,7 @@
augC.addToLl14(def6);
augC.addToLl14(def61);
augC.addToLl15(def71);
+ augC.addToLl16(def13);
cont2.addAugmentation((InnerModelObject) augC);
return cont2;
}
@@ -1587,6 +1599,8 @@
Def1 def1 = new Def1(union);
Def1Union union22 = new Def1Union(of("*"));
Def1 def11 = new Def1(union22);
+ Def1Union union33 = new Def1Union(of("3.3ms"));
+ Def1 def13 = new Def1(union33);
data = addLeafModelObject(LEAF2, def1, data);
byte[] arr = Base64.getDecoder().decode("MTEwMTE=");
@@ -1624,6 +1638,7 @@
Def7 def71 = new Def7(def7);
data = addLeafModelObject(LEAF15, def71, data);
+ data = addLeafModelObject(LEAF16, def13, data);
List<Object> objs = new LinkedList<>();
objs.add("leaf-list1");
objs.add("leaf-list1-2");
@@ -1703,6 +1718,10 @@
objs = new LinkedList<>();
objs.add(def71);
data = addLeafListModelObject(LL15, objs, data);
+
+ objs = new LinkedList<>();
+ objs.add(def13);
+ data = addLeafListModelObject(LL16, objs, data);
return data;
}
diff --git a/runtime/src/test/java/org/onosproject/yang/runtime/impl/YobSimpleDataTypeTest.java b/runtime/src/test/java/org/onosproject/yang/runtime/impl/YobSimpleDataTypeTest.java
index 39ee323..74e4253 100644
--- a/runtime/src/test/java/org/onosproject/yang/runtime/impl/YobSimpleDataTypeTest.java
+++ b/runtime/src/test/java/org/onosproject/yang/runtime/impl/YobSimpleDataTypeTest.java
@@ -147,7 +147,7 @@
dBlr = addDataNode(dBlr, "lfdecimal6", DATA_TYPE_NAME_SPACE, value, null);
dBlr = exitDataNode(dBlr);
- value = "enum1";
+ value = "3.3ms";
dBlr = addDataNode(dBlr, "lfenum", DATA_TYPE_NAME_SPACE, value, null);
dBlr = exitDataNode(dBlr);
@@ -367,7 +367,7 @@
dBlr = addDataNode(dBlr, "llref6", DATA_TYPE_NAME_SPACE, value, null);
dBlr = exitDataNode(dBlr);
- value = "enum1";
+ value = "3.3ms";
dBlr = addDataNode(dBlr, "llref7", DATA_TYPE_NAME_SPACE, value, null);
dBlr = exitDataNode(dBlr);
@@ -673,7 +673,7 @@
is("-922337203685477.5808"));
assertThat(cont.lfdecimal6().toString(),
is("-9223372036854.775808"));
- assertThat(cont.lfenum().toString(), is("enum1"));
+ assertThat(cont.lfenum().toString(), is("3.3ms"));
assertThat(cont.lfbits().toString(), is("{0}"));
String str = new String(cont.lfbinary());
assertThat(str, is("hey"));
@@ -750,7 +750,7 @@
assertThat(cont3.llref5().get(0).toString(), is("-922337203685470058.08"));
value = 121;
assertThat(cont3.llref6().get(0), is(value));
- assertThat(cont3.llref7().get(0).toString(), is("enum1"));
+ assertThat(cont3.llref7().get(0).toString(), is("3.3ms"));
assertThat(cont3.llref8().get(0).toString(), is("-9223372036000.775808"));
arr = Base64.getDecoder().decode("MTExMTExMTE=");
assertThat(cont3.llref9().get(0), is(arr));
diff --git a/runtime/src/test/resources/yobTestYangFiles/simple-data-types.yang b/runtime/src/test/resources/yobTestYangFiles/simple-data-types.yang
index cacff6c..c5abfb5 100644
--- a/runtime/src/test/resources/yobTestYangFiles/simple-data-types.yang
+++ b/runtime/src/test/resources/yobTestYangFiles/simple-data-types.yang
@@ -174,7 +174,7 @@
leaf lfenum {
type enumeration {
- enum enum1;
+ enum 3.3ms;
enum enum2;
enum enum3;
enum enum4;
diff --git a/runtime/src/test/resources/ytbTestYangFiles/YtbDataTypes.yang b/runtime/src/test/resources/ytbTestYangFiles/YtbDataTypes.yang
index 4e06efa..ea6bf6b 100644
--- a/runtime/src/test/resources/ytbTestYangFiles/YtbDataTypes.yang
+++ b/runtime/src/test/resources/ytbTestYangFiles/YtbDataTypes.yang
@@ -21,6 +21,9 @@
enum * {
value "10000";
}
+ enum 3.3ms {
+ value "100000";
+ }
}
}
}
@@ -146,6 +149,10 @@
type def7;
}
+ leaf leaf16 {
+ type def1;
+ }
+
container cont1 {
leaf leaf1 {
type string;
@@ -217,6 +224,10 @@
type def7;
}
+ leaf leaf16 {
+ type def1;
+ }
+
leaf-list ll1 {
type string;
}
@@ -286,6 +297,10 @@
leaf-list ll15 {
type def7;
}
+
+ leaf-list ll16 {
+ type def1;
+ }
container cont2 {
}
}
@@ -359,6 +374,10 @@
leaf-list ll15 {
type def7;
}
+
+ leaf-list ll16 {
+ type def1;
+ }
augment "/cont1/cont2" {
leaf leaf1 {
type string;
@@ -430,6 +449,10 @@
type def7;
}
+ leaf leaf16 {
+ type def1;
+ }
+
leaf-list ll1 {
type string;
}
@@ -499,5 +522,9 @@
leaf-list ll15 {
type def7;
}
+
+ leaf-list ll16 {
+ type def1;
+ }
}
}