Committed by
Patrick Liu
[ONOS-4839] update file priority for input files
Change-Id: I4aa0cbbfb0f168efd7c1895c9bb0c6589088eefd
Showing
8 changed files
with
181 additions
and
3 deletions
... | @@ -29,7 +29,7 @@ import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.updateClo | ... | @@ -29,7 +29,7 @@ import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.updateClo |
29 | * Represents base class of a node in data model tree. | 29 | * Represents base class of a node in data model tree. |
30 | */ | 30 | */ |
31 | public abstract class YangNode | 31 | public abstract class YangNode |
32 | - implements Cloneable, Serializable, YangDataNode { | 32 | + implements Cloneable, Serializable, YangDataNode, Comparable<YangNode> { |
33 | 33 | ||
34 | private static final long serialVersionUID = 806201601L; | 34 | private static final long serialVersionUID = 806201601L; |
35 | 35 | ||
... | @@ -59,6 +59,29 @@ public abstract class YangNode | ... | @@ -59,6 +59,29 @@ public abstract class YangNode |
59 | private YangNode previousSibling; | 59 | private YangNode previousSibling; |
60 | 60 | ||
61 | /** | 61 | /** |
62 | + * Priority of the node. | ||
63 | + */ | ||
64 | + private int priority; | ||
65 | + | ||
66 | + /** | ||
67 | + * Returns the priority of the node. | ||
68 | + * | ||
69 | + * @return priority of the node | ||
70 | + */ | ||
71 | + public int getPriority() { | ||
72 | + return priority; | ||
73 | + } | ||
74 | + | ||
75 | + /** | ||
76 | + * Sets the priority of the node. | ||
77 | + * | ||
78 | + * @param priority of the node | ||
79 | + */ | ||
80 | + public void setPriority(int priority) { | ||
81 | + this.priority = priority; | ||
82 | + } | ||
83 | + | ||
84 | + /** | ||
62 | * Returns the nodes name. | 85 | * Returns the nodes name. |
63 | * | 86 | * |
64 | * @return nodes name | 87 | * @return nodes name |
... | @@ -234,6 +257,14 @@ public abstract class YangNode | ... | @@ -234,6 +257,14 @@ public abstract class YangNode |
234 | } | 257 | } |
235 | } | 258 | } |
236 | 259 | ||
260 | + @Override | ||
261 | + public int compareTo(YangNode otherNode) { | ||
262 | + if (priority == otherNode.getPriority()) { | ||
263 | + return 1; | ||
264 | + } | ||
265 | + return ((Integer) otherNode.getPriority()).compareTo(priority); | ||
266 | + } | ||
267 | + | ||
237 | /** | 268 | /** |
238 | * Clones the current node contents and create a new node. | 269 | * Clones the current node contents and create a new node. |
239 | * | 270 | * | ... | ... |
... | @@ -16,9 +16,15 @@ | ... | @@ -16,9 +16,15 @@ |
16 | 16 | ||
17 | package org.onosproject.yangutils.linker.impl; | 17 | package org.onosproject.yangutils.linker.impl; |
18 | 18 | ||
19 | +import java.util.Collections; | ||
19 | import java.util.HashSet; | 20 | import java.util.HashSet; |
21 | +import java.util.Iterator; | ||
22 | +import java.util.LinkedList; | ||
23 | +import java.util.List; | ||
20 | import java.util.Set; | 24 | import java.util.Set; |
21 | import org.onosproject.yangutils.datamodel.ResolvableType; | 25 | import org.onosproject.yangutils.datamodel.ResolvableType; |
26 | +import org.onosproject.yangutils.datamodel.YangImport; | ||
27 | +import org.onosproject.yangutils.datamodel.YangInclude; | ||
22 | import org.onosproject.yangutils.datamodel.YangNode; | 28 | import org.onosproject.yangutils.datamodel.YangNode; |
23 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; | 29 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
24 | import org.onosproject.yangutils.datamodel.YangSubModule; | 30 | import org.onosproject.yangutils.datamodel.YangSubModule; |
... | @@ -73,6 +79,9 @@ public class YangLinkerManager | ... | @@ -73,6 +79,9 @@ public class YangLinkerManager |
73 | // Add reference to include list. | 79 | // Add reference to include list. |
74 | addRefToYangFilesIncludeList(yangNodeSet); | 80 | addRefToYangFilesIncludeList(yangNodeSet); |
75 | 81 | ||
82 | + // Update the priority for all the files. | ||
83 | + updateFilePriority(yangNodeSet); | ||
84 | + | ||
76 | // TODO check for circular import/include. | 85 | // TODO check for circular import/include. |
77 | 86 | ||
78 | // Carry out inter-file linking. | 87 | // Carry out inter-file linking. |
... | @@ -154,7 +163,10 @@ public class YangLinkerManager | ... | @@ -154,7 +163,10 @@ public class YangLinkerManager |
154 | */ | 163 | */ |
155 | public void processInterFileLinking(Set<YangNode> yangNodeSet) | 164 | public void processInterFileLinking(Set<YangNode> yangNodeSet) |
156 | throws LinkerException { | 165 | throws LinkerException { |
157 | - for (YangNode yangNode : yangNodeSet) { | 166 | + List<YangNode> yangNodeSortedList = new LinkedList<>(); |
167 | + yangNodeSortedList.addAll(yangNodeSet); | ||
168 | + Collections.sort(yangNodeSortedList); | ||
169 | + for (YangNode yangNode : yangNodeSortedList) { | ||
158 | try { | 170 | try { |
159 | ((YangReferenceResolver) yangNode) | 171 | ((YangReferenceResolver) yangNode) |
160 | .resolveInterFileLinking(ResolvableType.YANG_IF_FEATURE); | 172 | .resolveInterFileLinking(ResolvableType.YANG_IF_FEATURE); |
... | @@ -178,4 +190,53 @@ public class YangLinkerManager | ... | @@ -178,4 +190,53 @@ public class YangLinkerManager |
178 | } | 190 | } |
179 | } | 191 | } |
180 | } | 192 | } |
193 | + | ||
194 | + /** | ||
195 | + * Updates the priority for all the input files. | ||
196 | + * | ||
197 | + * @param yangNodeSet set of YANG files info | ||
198 | + */ | ||
199 | + public void updateFilePriority(Set<YangNode> yangNodeSet) { | ||
200 | + for (YangNode yangNode : yangNodeSet) { | ||
201 | + updateFilePriorityOfNode(yangNode); | ||
202 | + } | ||
203 | + } | ||
204 | + | ||
205 | + /** | ||
206 | + * Updates priority of the node. | ||
207 | + * | ||
208 | + * @param yangNode YANG node information | ||
209 | + */ | ||
210 | + public void updateFilePriorityOfNode(YangNode yangNode) { | ||
211 | + int curNodePriority = yangNode.getPriority(); | ||
212 | + if (yangNode instanceof YangReferenceResolver) { | ||
213 | + List<YangImport> yangImportList = ((YangReferenceResolver) yangNode).getImportList(); | ||
214 | + if (yangImportList != null && !yangImportList.isEmpty()) { | ||
215 | + Iterator<YangImport> importInfoIterator = yangImportList.iterator(); | ||
216 | + // Run through the imported list to update priority. | ||
217 | + while (importInfoIterator.hasNext()) { | ||
218 | + YangImport yangImport = importInfoIterator.next(); | ||
219 | + YangNode importedNode = yangImport.getImportedNode(); | ||
220 | + if (curNodePriority >= importedNode.getPriority()) { | ||
221 | + importedNode.setPriority(curNodePriority + 1); | ||
222 | + updateFilePriorityOfNode(importedNode); | ||
223 | + } | ||
224 | + } | ||
225 | + } | ||
226 | + | ||
227 | + List<YangInclude> yangIncludeList = ((YangReferenceResolver) yangNode).getIncludeList(); | ||
228 | + if (yangIncludeList != null && !yangIncludeList.isEmpty()) { | ||
229 | + Iterator<YangInclude> includeInfoIterator = yangIncludeList.iterator(); | ||
230 | + // Run through the imported list to update priority. | ||
231 | + while (includeInfoIterator.hasNext()) { | ||
232 | + YangInclude yangInclude = includeInfoIterator.next(); | ||
233 | + YangNode includedNode = yangInclude.getIncludedNode(); | ||
234 | + if (curNodePriority >= includedNode.getPriority()) { | ||
235 | + includedNode.setPriority(curNodePriority + 1); | ||
236 | + updateFilePriorityOfNode(includedNode); | ||
237 | + } | ||
238 | + } | ||
239 | + } | ||
240 | + } | ||
241 | + } | ||
181 | } | 242 | } | ... | ... |
... | @@ -59,7 +59,7 @@ public final class YangPluginConfig { | ... | @@ -59,7 +59,7 @@ public final class YangPluginConfig { |
59 | /** | 59 | /** |
60 | * Sets the string sbi or nbi for code generation. | 60 | * Sets the string sbi or nbi for code generation. |
61 | * | 61 | * |
62 | - * @par code generation is for sbi | 62 | + * @param codeGenerateForsbi generation is for sbi |
63 | */ | 63 | */ |
64 | public void setCodeGenerateForsbi(String codeGenerateForsbi) { | 64 | public void setCodeGenerateForsbi(String codeGenerateForsbi) { |
65 | this.codeGenerateForsbi = codeGenerateForsbi; | 65 | this.codeGenerateForsbi = codeGenerateForsbi; | ... | ... |
... | @@ -984,4 +984,52 @@ public class InterFileLinkingTest { | ... | @@ -984,4 +984,52 @@ public class InterFileLinkingTest { |
984 | assertThat(leafref.getEffectiveDataType().getDataType(), | 984 | assertThat(leafref.getEffectiveDataType().getDataType(), |
985 | is(YangDataTypes.STRING)); | 985 | is(YangDataTypes.STRING)); |
986 | } | 986 | } |
987 | + | ||
988 | + /** | ||
989 | + * Checks priority of the file. | ||
990 | + */ | ||
991 | + @Test | ||
992 | + public void interFilePriority() | ||
993 | + throws IOException, ParserException, MojoExecutionException { | ||
994 | + | ||
995 | + String searchDir = "src/test/resources/interfilepriority"; | ||
996 | + utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(searchDir)); | ||
997 | + utilManager.parseYangFileInfoSet(); | ||
998 | + utilManager.resolveDependenciesUsingLinker(); | ||
999 | + | ||
1000 | + YangNode selfNode = null; | ||
1001 | + YangNode refNode1 = null; | ||
1002 | + YangNode refNode2 = null; | ||
1003 | + | ||
1004 | + for (YangNode rootNode : utilManager.getYangNodeSet()) { | ||
1005 | + if (rootNode.getName().equals("module1")) { | ||
1006 | + selfNode = rootNode; | ||
1007 | + } else if (rootNode.getName().equals("module2")) { | ||
1008 | + refNode1 = rootNode; | ||
1009 | + } else { | ||
1010 | + refNode2 = rootNode; | ||
1011 | + } | ||
1012 | + } | ||
1013 | + | ||
1014 | + // Check whether the data model tree returned is of type module. | ||
1015 | + assertThat(selfNode instanceof YangModule, is(true)); | ||
1016 | + | ||
1017 | + // Check whether the node type is set properly to module. | ||
1018 | + assertThat(selfNode.getNodeType(), is(MODULE_NODE)); | ||
1019 | + | ||
1020 | + // Check whether the module name is set correctly. | ||
1021 | + YangModule yangNode = (YangModule) selfNode; | ||
1022 | + assertThat(yangNode.getName(), is("module1")); | ||
1023 | + assertThat(yangNode.getPriority(), is(2)); | ||
1024 | + | ||
1025 | + // Check whether the data model tree returned is of type module. | ||
1026 | + assertThat(refNode1 instanceof YangModule, is(true)); | ||
1027 | + | ||
1028 | + // Check whether the node type is set properly to module. | ||
1029 | + assertThat(refNode1.getNodeType(), is(MODULE_NODE)); | ||
1030 | + | ||
1031 | + YangModule referredNode1 = (YangModule) refNode1; | ||
1032 | + assertThat(referredNode1.getName(), is("module2")); | ||
1033 | + assertThat(referredNode1.getPriority(), is(3)); | ||
1034 | + } | ||
987 | } | 1035 | } | ... | ... |
-
Please register or login to post a comment