janani b
Committed by Gerrit Code Review

[ONOS-4991][ONOS-4994] Leafref and translator issue fixes

Change-Id: I583ff24e987a680706025f43979469edeb523b50
...@@ -19,6 +19,9 @@ package org.onosproject.yangutils.translator.tojava; ...@@ -19,6 +19,9 @@ package org.onosproject.yangutils.translator.tojava;
19 import org.onosproject.yangutils.datamodel.YangType; 19 import org.onosproject.yangutils.datamodel.YangType;
20 import org.onosproject.yangutils.translator.exception.TranslatorException; 20 import org.onosproject.yangutils.translator.exception.TranslatorException;
21 21
22 +import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeLeafref;
23 +import static org.onosproject.yangutils.translator.tojava.utils.JavaFileGeneratorUtils.isTypeNameLeafref;
24 +
22 /** 25 /**
23 * Represents the attribute info corresponding to class/interface generated. 26 * Represents the attribute info corresponding to class/interface generated.
24 */ 27 */
...@@ -235,6 +238,10 @@ public final class JavaAttributeInfo { ...@@ -235,6 +238,10 @@ public final class JavaAttributeInfo {
235 YangType<?> attributeType, boolean isQualifiedAccess, 238 YangType<?> attributeType, boolean isQualifiedAccess,
236 boolean isListAttribute) { 239 boolean isListAttribute) {
237 240
241 + if (attributeType != null) {
242 + attributeType = isTypeLeafref(attributeType);
243 + }
244 + attributeName = isTypeNameLeafref(attributeName, attributeType);
238 JavaAttributeInfo newAttr = new JavaAttributeInfo(); 245 JavaAttributeInfo newAttr = new JavaAttributeInfo();
239 newAttr.setImportInfo(importInfo); 246 newAttr.setImportInfo(importInfo);
240 newAttr.setAttributeName(attributeName); 247 newAttr.setAttributeName(attributeName);
......
...@@ -17,16 +17,16 @@ ...@@ -17,16 +17,16 @@
17 package org.onosproject.yangutils.translator.tojava; 17 package org.onosproject.yangutils.translator.tojava;
18 18
19 import java.io.IOException; 19 import java.io.IOException;
20 -
21 import org.onosproject.yangutils.datamodel.TraversalType; 20 import org.onosproject.yangutils.datamodel.TraversalType;
22 import org.onosproject.yangutils.datamodel.YangInput; 21 import org.onosproject.yangutils.datamodel.YangInput;
23 import org.onosproject.yangutils.datamodel.YangNode; 22 import org.onosproject.yangutils.datamodel.YangNode;
24 import org.onosproject.yangutils.datamodel.YangNodeType; 23 import org.onosproject.yangutils.datamodel.YangNodeType;
25 import org.onosproject.yangutils.datamodel.YangOutput; 24 import org.onosproject.yangutils.datamodel.YangOutput;
25 +import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
26 +import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
26 import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException; 27 import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
27 import org.onosproject.yangutils.translator.exception.TranslatorException; 28 import org.onosproject.yangutils.translator.exception.TranslatorException;
28 -import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig; 29 +
29 -import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
30 import static org.onosproject.yangutils.datamodel.TraversalType.CHILD; 30 import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
31 import static org.onosproject.yangutils.datamodel.TraversalType.PARENT; 31 import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
32 import static org.onosproject.yangutils.datamodel.TraversalType.ROOT; 32 import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
...@@ -279,6 +279,7 @@ public final class JavaCodeGeneratorUtil { ...@@ -279,6 +279,7 @@ public final class JavaCodeGeneratorUtil {
279 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true); 279 ((TempJavaCodeFragmentFilesContainer) node).getTempJavaCodeFragmentFiles().freeTemporaryResources(true);
280 } else { 280 } else {
281 281
282 + if (getRootNode() != null) {
282 JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo(); 283 JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) getRootNode()).getJavaFileInfo();
283 if (javaFileInfo != null) { 284 if (javaFileInfo != null) {
284 searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() + 285 searchAndDeleteTempDir(javaFileInfo.getBaseCodeGenPath() +
...@@ -286,7 +287,7 @@ public final class JavaCodeGeneratorUtil { ...@@ -286,7 +287,7 @@ public final class JavaCodeGeneratorUtil {
286 } else { 287 } else {
287 searchAndDeleteTempDir(yangPlugin.getManagerCodeGenDir()); 288 searchAndDeleteTempDir(yangPlugin.getManagerCodeGenDir());
288 } 289 }
289 - 290 + }
290 } 291 }
291 } 292 }
292 293
...@@ -320,7 +321,7 @@ public final class JavaCodeGeneratorUtil { ...@@ -320,7 +321,7 @@ public final class JavaCodeGeneratorUtil {
320 YangNode child = parentNode.getChild(); 321 YangNode child = parentNode.getChild();
321 TraversalType curTraversal = ROOT; 322 TraversalType curTraversal = ROOT;
322 if (child == null) { 323 if (child == null) {
323 - throw new IllegalArgumentException("given parent node does not contain any child nodes"); 324 + throw new IllegalArgumentException("Given parent node does not contain any child nodes");
324 } 325 }
325 326
326 while (child != null) { 327 while (child != null) {
......
...@@ -20,12 +20,10 @@ import java.io.File; ...@@ -20,12 +20,10 @@ import java.io.File;
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.util.ArrayList; 21 import java.util.ArrayList;
22 import java.util.List; 22 import java.util.List;
23 -
24 import org.onosproject.yangutils.datamodel.YangNode; 23 import org.onosproject.yangutils.datamodel.YangNode;
25 import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo; 24 import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
26 -import org.onosproject.yangutils.translator.tojava.javamodel.YangJavaGroupingTranslator;
27 -import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
28 import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig; 25 import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
26 +import org.onosproject.yangutils.translator.tojava.utils.JavaExtendsListHolder;
29 27
30 import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS; 28 import static org.onosproject.yangutils.translator.tojava.GeneratedJavaFileType.GENERATE_EVENT_SUBJECT_CLASS;
31 import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_ENUM_MASK; 29 import static org.onosproject.yangutils.translator.tojava.GeneratedTempFileType.EVENT_ENUM_MASK;
...@@ -490,15 +488,8 @@ public class TempJavaEventFragmentFiles ...@@ -490,15 +488,8 @@ public class TempJavaEventFragmentFiles
490 throws IOException { 488 throws IOException {
491 489
492 JavaFileInfo parentInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo(); 490 JavaFileInfo parentInfo = ((JavaFileInfoContainer) curNode).getJavaFileInfo();
493 - YangNode childNode = curNode.getChild();
494 -
495 - // Skip grouping, as it wont have the package name.
496 - while (childNode instanceof YangJavaGroupingTranslator) {
497 - childNode = childNode.getNextSibling();
498 - }
499 - JavaFileInfo childInfo = ((JavaFileInfoContainer) childNode).getJavaFileInfo();
500 return getFileObject(getDirPath(parentInfo), name, JAVA_FILE_EXTENSION, 491 return getFileObject(getDirPath(parentInfo), name, JAVA_FILE_EXTENSION,
501 - childInfo.getBaseCodeGenPath()); 492 + parentInfo.getPluginConfig().getCodeGenDir());
502 } 493 }
503 494
504 /** 495 /**
......
...@@ -16,10 +16,13 @@ ...@@ -16,10 +16,13 @@
16 package org.onosproject.yangutils.translator.tojava.javamodel; 16 package org.onosproject.yangutils.translator.tojava.javamodel;
17 17
18 import java.io.IOException; 18 import java.io.IOException;
19 - 19 +import org.onosproject.yangutils.datamodel.YangDerivedInfo;
20 +import org.onosproject.yangutils.datamodel.YangType;
20 import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo; 21 import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
21 import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaTypeDef; 22 import org.onosproject.yangutils.datamodel.javadatamodel.YangJavaTypeDef;
22 import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig; 23 import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
24 +import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
25 +import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
23 import org.onosproject.yangutils.translator.exception.TranslatorException; 26 import org.onosproject.yangutils.translator.exception.TranslatorException;
24 import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; 27 import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
25 import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; 28 import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
...@@ -105,6 +108,15 @@ public class YangJavaTypeDefTranslator ...@@ -105,6 +108,15 @@ public class YangJavaTypeDefTranslator
105 */ 108 */
106 @Override 109 @Override
107 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException { 110 public void generateCodeEntry(YangPluginConfig yangPlugin) throws TranslatorException {
111 + YangType typeInTypeDef = this.getTypeDefBaseType();
112 + if (typeInTypeDef.getDataType() == YangDataTypes.DERIVED) {
113 + YangDerivedInfo derivedInfo = (YangDerivedInfo) typeInTypeDef.getDataTypeExtendedInfo();
114 + if (derivedInfo.getEffectiveBuiltInType() == YangDataTypes.LEAFREF) {
115 + throw new InvalidNodeForTranslatorException();
116 + }
117 + } else if (typeInTypeDef.getDataType() == YangDataTypes.LEAFREF) {
118 + throw new InvalidNodeForTranslatorException();
119 + }
108 try { 120 try {
109 generateCodeOfNode(this, yangPlugin); 121 generateCodeOfNode(this, yangPlugin);
110 } catch (IOException e) { 122 } catch (IOException e) {
......
...@@ -20,13 +20,15 @@ import java.io.File; ...@@ -20,13 +20,15 @@ import java.io.File;
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.util.ArrayList; 21 import java.util.ArrayList;
22 import java.util.List; 22 import java.util.List;
23 -
24 import org.onosproject.yangutils.datamodel.YangAtomicPath; 23 import org.onosproject.yangutils.datamodel.YangAtomicPath;
25 import org.onosproject.yangutils.datamodel.YangAugment; 24 import org.onosproject.yangutils.datamodel.YangAugment;
25 +import org.onosproject.yangutils.datamodel.YangLeafRef;
26 import org.onosproject.yangutils.datamodel.YangNode; 26 import org.onosproject.yangutils.datamodel.YangNode;
27 import org.onosproject.yangutils.datamodel.YangNodeIdentifier; 27 import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
28 +import org.onosproject.yangutils.datamodel.YangType;
28 import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo; 29 import org.onosproject.yangutils.datamodel.javadatamodel.JavaFileInfo;
29 import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig; 30 import org.onosproject.yangutils.datamodel.javadatamodel.YangPluginConfig;
31 +import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
30 import org.onosproject.yangutils.translator.exception.TranslatorException; 32 import org.onosproject.yangutils.translator.exception.TranslatorException;
31 import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; 33 import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
32 import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer; 34 import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
...@@ -80,6 +82,7 @@ import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoT ...@@ -80,6 +82,7 @@ import static org.onosproject.yangutils.translator.tojava.JavaQualifiedTypeInfoT
80 import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.getAugmentedNodesPackage; 82 import static org.onosproject.yangutils.translator.tojava.YangJavaModelUtils.getAugmentedNodesPackage;
81 import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition; 83 import static org.onosproject.yangutils.translator.tojava.utils.ClassDefinitionGenerator.generateClassDefinition;
82 import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET; 84 import static org.onosproject.yangutils.utils.UtilConstants.CLOSE_CURLY_BRACKET;
85 +import static org.onosproject.yangutils.utils.UtilConstants.LEAFREF;
83 import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; 86 import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
84 import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE; 87 import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
85 import static org.onosproject.yangutils.utils.UtilConstants.PERIOD; 88 import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
...@@ -705,4 +708,31 @@ public final class JavaFileGeneratorUtils { ...@@ -705,4 +708,31 @@ public final class JavaFileGeneratorUtils {
705 708
706 } 709 }
707 710
711 + /**
712 + * Checks if the type name is leafref and returns the effective type name.
713 + *
714 + * @param attributeName name of the current type
715 + * @param attributeType effective type
716 + * @return name of the effective type
717 + */
718 + public static String isTypeNameLeafref(String attributeName, YangType<?> attributeType) {
719 + if (attributeName.equalsIgnoreCase(LEAFREF)) {
720 + return attributeType.getDataTypeName();
721 + }
722 + return attributeName;
723 + }
724 +
725 + /**
726 + * Checks if the type is leafref and returns the effective type.
727 + *
728 + * @param attributeType current type
729 + * @return effective type
730 + */
731 + public static YangType isTypeLeafref(YangType<?> attributeType) {
732 + if (attributeType.getDataType() == YangDataTypes.LEAFREF) {
733 + YangLeafRef leafRef = (YangLeafRef) attributeType.getDataTypeExtendedInfo();
734 + return leafRef.getEffectiveDataType();
735 + }
736 + return attributeType;
737 + }
708 } 738 }
......
...@@ -1022,11 +1022,11 @@ public final class UtilConstants { ...@@ -1022,11 +1022,11 @@ public final class UtilConstants {
1022 * List of keywords in java, this is used for checking if the input does not contain these keywords. 1022 * List of keywords in java, this is used for checking if the input does not contain these keywords.
1023 */ 1023 */
1024 public static final List<String> JAVA_KEY_WORDS = Arrays.asList( 1024 public static final List<String> JAVA_KEY_WORDS = Arrays.asList(
1025 - "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", 1025 + "abstract", "continue", "for", "new", "switch", "assert", "default", "goto", "package", "synchronized",
1026 - "default", "do", "double", "else", "extends", "false", "final", "finally", "float", "for", "goto", "if", 1026 + "boolean", "do", "if", "private", "this", "break", "double", "implements", "protected", "throw", "byte",
1027 - "implements", "import", "instanceof", "int", "interface", "long", "native", "new", "null", "package", 1027 + "else", "import", "public", "throws", "case", "enum", "instanceof", "return", "transient", "catch",
1028 - "private", "protected", "public", "return", "short", "static", "strictfp", "super", "switch", 1028 + "extends", "int", "short", "try", "char", "final", "interface", "static", "void", "class", "finally",
1029 - "synchronized", "this", "throw", "throws", "transient", "true", "try", "void", "volatile", "while"); 1029 + "long", "strictfp", "volatile", "const", "float", "native", "super", "while");
1030 1030
1031 /** 1031 /**
1032 * Static attribute for regex for all the special characters. 1032 * Static attribute for regex for all the special characters.
......
...@@ -1918,4 +1918,69 @@ public class IntraFileLeafrefLinkingTest { ...@@ -1918,4 +1918,69 @@ public class IntraFileLeafrefLinkingTest {
1918 assertThat(leafref.getEffectiveDataType().getDataType(), 1918 assertThat(leafref.getEffectiveDataType().getDataType(),
1919 is(YangDataTypes.DERIVED)); 1919 is(YangDataTypes.DERIVED));
1920 } 1920 }
1921 +
1922 + /**
1923 + * Checks self resolution when leafref under typedef refers to the node where it is used.
1924 + */
1925 + @Test
1926 + public void processSelfResolutionWhenLeafrefInTypedefIsUsedInSameReferredNode()
1927 + throws IOException, ParserException {
1928 +
1929 + String searchDir = "src/test/resources/leafreflinker/intrafile/leafrefintypedefwithsamereferpath";
1930 + utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir));
1931 + utilManager.parseYangFileInfoSet();
1932 + utilManager.createYangNodeSet();
1933 + YangNode selfNode = null;
1934 +
1935 + // Create YANG node set
1936 + yangLinkerManager.createYangNodeSet(utilManager.getYangNodeSet());
1937 +
1938 + // Add references to import list.
1939 + yangLinkerManager.addRefToYangFilesImportList(utilManager.getYangNodeSet());
1940 +
1941 + updateFilePriority(utilManager.getYangNodeSet());
1942 +
1943 + // Carry out inter-file linking.
1944 + yangLinkerManager.processInterFileLinking(utilManager.getYangNodeSet());
1945 +
1946 + Iterator<YangNode> yangNodeIterator = utilManager.getYangNodeSet().iterator();
1947 +
1948 + YangNode rootNode = yangNodeIterator.next();
1949 +
1950 + if (rootNode.getName().equals("typedef")) {
1951 + selfNode = rootNode;
1952 + }
1953 +
1954 + // Check whether the data model tree returned is of type module.
1955 + assertThat((selfNode instanceof YangModule), is(true));
1956 +
1957 + // Check whether the node type is set properly to module.
1958 + assertThat(selfNode.getNodeType(), is(YangNodeType.MODULE_NODE));
1959 +
1960 + // Check whether the module name is set correctly.
1961 + YangModule yangNode = (YangModule) selfNode;
1962 + assertThat(yangNode.getName(), is("typedef"));
1963 +
1964 + ListIterator<YangLeaf> leafIterator;
1965 + YangLeaf leafInfo;
1966 +
1967 + YangContainer yangContainer = (YangContainer) yangNode.getChild().getNextSibling();
1968 +
1969 + leafIterator = yangContainer.getListOfLeaf().listIterator();
1970 + leafInfo = leafIterator.next();
1971 +
1972 + // Check whether the information in the leaf is correct.
1973 + assertThat(leafInfo.getName(), is("reference"));
1974 + assertThat(leafInfo.getDataType().getDataTypeName(), is("leafref"));
1975 + assertThat(leafInfo.getDataType().getDataType(), is(YangDataTypes.LEAFREF));
1976 + YangLeafRef leafref = (YangLeafRef) (leafInfo.getDataType().getDataTypeExtendedInfo());
1977 +
1978 + // Check whether leafref type got resolved.
1979 + assertThat(leafref.getResolvableStatus(),
1980 + is(ResolvableStatus.RESOLVED));
1981 +
1982 + // Check the effective type for the leaf.
1983 + assertThat(leafref.getEffectiveDataType().getDataType(),
1984 + is(YangDataTypes.UINT8));
1985 + }
1921 } 1986 }
......
1 +module typedef {
2 + yang-version "1";
3 + namespace "http://rob.sh/yang/test/list";
4 + prefix "foo";
5 + organization "BugReports Inc";
6 + contact "Bug reporter";
7 +
8 + description
9 + "A test module";
10 + revision 2014-01-01 {
11 + description "april-fools";
12 + reference "fooled-you";
13 + }
14 +
15 + typedef referenced-leaf {
16 + type leafref {
17 + path "/container/target";
18 + require-instance false;
19 + }
20 + }
21 +
22 + container container {
23 + description
24 + "A container";
25 + leaf-list target {
26 + type uint8;
27 + description
28 + "A target leaf for leafref checks";
29 + }
30 + leaf reference {
31 + type referenced-leaf;
32 + }
33 + }
34 +}