Bharat saraswal
Committed by Gerrit Code Review

YANG augment's generated file name resolver implementation and UT fixes.

Change-Id: Ib960a15398a3b9f529f9ad28402d5bac539fb525
...@@ -18,6 +18,7 @@ package org.onosproject.yangutils.parser.impl.listeners; ...@@ -18,6 +18,7 @@ package org.onosproject.yangutils.parser.impl.listeners;
18 18
19 import java.util.List; 19 import java.util.List;
20 20
21 +import org.onosproject.yangutils.datamodel.CollisionDetector;
21 import org.onosproject.yangutils.datamodel.YangAugment; 22 import org.onosproject.yangutils.datamodel.YangAugment;
22 import org.onosproject.yangutils.datamodel.YangModule; 23 import org.onosproject.yangutils.datamodel.YangModule;
23 import org.onosproject.yangutils.datamodel.YangNode; 24 import org.onosproject.yangutils.datamodel.YangNode;
...@@ -32,6 +33,9 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener; ...@@ -32,6 +33,9 @@ import org.onosproject.yangutils.parser.impl.TreeWalkListener;
32 33
33 import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION; 34 import static org.onosproject.yangutils.datamodel.utils.GeneratedLanguage.JAVA_GENERATION;
34 import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangAugmentNode; 35 import static org.onosproject.yangutils.datamodel.utils.YangDataModelFactory.getYangAugmentNode;
36 +import static org.onosproject.yangutils.parser.impl.parserutils.AugmentJavaFileNameGenUtil.clearOccurrenceCount;
37 +import static org.onosproject.yangutils.parser.impl.parserutils.AugmentJavaFileNameGenUtil.createValidNameForAugment;
38 +import static org.onosproject.yangutils.parser.impl.parserutils.AugmentJavaFileNameGenUtil.updateNameWhenHasMultipleOuccrrence;
35 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil; 39 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerCollisionDetector.detectCollidingChildUtil;
36 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY; 40 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.ENTRY;
37 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT; 41 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerErrorLocation.EXIT;
...@@ -45,7 +49,6 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.get ...@@ -45,7 +49,6 @@ import static org.onosproject.yangutils.parser.impl.parserutils.ListenerUtil.get
45 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty; 49 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.checkStackIsNotEmpty;
46 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne; 50 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateCardinalityMaxOne;
47 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds; 51 import static org.onosproject.yangutils.parser.impl.parserutils.ListenerValidation.validateMutuallyExclusiveChilds;
48 -import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
49 import static org.onosproject.yangutils.utils.YangConstructType.AUGMENT_DATA; 52 import static org.onosproject.yangutils.utils.YangConstructType.AUGMENT_DATA;
50 import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA; 53 import static org.onosproject.yangutils.utils.YangConstructType.CASE_DATA;
51 import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA; 54 import static org.onosproject.yangutils.utils.YangConstructType.DATA_DEF_DATA;
...@@ -81,8 +84,6 @@ import static org.onosproject.yangutils.utils.YangConstructType.WHEN_DATA; ...@@ -81,8 +84,6 @@ import static org.onosproject.yangutils.utils.YangConstructType.WHEN_DATA;
81 */ 84 */
82 public final class AugmentListener { 85 public final class AugmentListener {
83 86
84 - private static final String AUGMENTED = "Augmented";
85 -
86 /** 87 /**
87 * Creates a new augment listener. 88 * Creates a new augment listener.
88 */ 89 */
...@@ -116,11 +117,11 @@ public final class AugmentListener { ...@@ -116,11 +117,11 @@ public final class AugmentListener {
116 117
117 Parsable curData = listener.getParsedDataStack().peek(); 118 Parsable curData = listener.getParsedDataStack().peek();
118 if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangUses) { 119 if (curData instanceof YangModule || curData instanceof YangSubModule || curData instanceof YangUses) {
119 -
120 YangNode curNode = (YangNode) curData; 120 YangNode curNode = (YangNode) curData;
121 YangAugment yangAugment = getYangAugmentNode(JAVA_GENERATION); 121 YangAugment yangAugment = getYangAugmentNode(JAVA_GENERATION);
122 yangAugment.setTargetNode(targetNodes); 122 yangAugment.setTargetNode(targetNodes);
123 - yangAugment.setName(getValidNameForAugment(targetNodes)); 123 + yangAugment.setName(detectCollisionForTargetNode(curData, targetNodes, line, charPositionInLine, listener));
124 +
124 try { 125 try {
125 curNode.addChild(yangAugment); 126 curNode.addChild(yangAugment);
126 } catch (DataModelException e) { 127 } catch (DataModelException e) {
...@@ -161,7 +162,6 @@ public final class AugmentListener { ...@@ -161,7 +162,6 @@ public final class AugmentListener {
161 * @param ctx context object of the grammar rule 162 * @param ctx context object of the grammar rule
162 */ 163 */
163 private static void validateSubStatementsCardinality(GeneratedYangParser.AugmentStatementContext ctx) { 164 private static void validateSubStatementsCardinality(GeneratedYangParser.AugmentStatementContext ctx) {
164 -
165 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, AUGMENT_DATA, ctx.augment().getText()); 165 validateCardinalityMaxOne(ctx.statusStatement(), STATUS_DATA, AUGMENT_DATA, ctx.augment().getText());
166 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, AUGMENT_DATA, ctx.augment().getText()); 166 validateCardinalityMaxOne(ctx.descriptionStatement(), DESCRIPTION_DATA, AUGMENT_DATA, ctx.augment().getText());
167 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, AUGMENT_DATA, ctx.augment().getText()); 167 validateCardinalityMaxOne(ctx.referenceStatement(), REFERENCE_DATA, AUGMENT_DATA, ctx.augment().getText());
...@@ -171,21 +171,52 @@ public final class AugmentListener { ...@@ -171,21 +171,52 @@ public final class AugmentListener {
171 } 171 }
172 172
173 /** 173 /**
174 - * Returns a name identifier for augment. 174 + * Detects collision for java file generation of augment node when
175 + * it is updating the same target node in same parent multiple times.
176 + * Returns name for generated java file of augment node
175 * 177 *
176 - * @param targetNode list of target nodes 178 + * @param curData parsable data
177 - * @return name identifier 179 + * @param targetNodes list of target nodes
180 + * @param line line in YANG file
181 + * @param charPositionInLine char position in YANG file
182 + * @param listener tree walk listener
183 + * @return name for generated java file for augment node
178 */ 184 */
179 - private static String getValidNameForAugment(List<YangNodeIdentifier> targetNodes) { 185 + private static String detectCollisionForTargetNode(Parsable curData, List<YangNodeIdentifier> targetNodes, int line,
180 - String name = ""; 186 + int charPositionInLine, TreeWalkListener listener) {
187 +
188 + String curPrefix = null;
189 + if (curData instanceof YangModule) {
190 + curPrefix = ((YangModule) curData).getPrefix();
191 + } else if (curData instanceof YangSubModule) {
192 + curPrefix = ((YangSubModule) curData).getPrefix();
193 + }
181 YangNodeIdentifier nodeId = targetNodes.get(targetNodes.size() - 1); 194 YangNodeIdentifier nodeId = targetNodes.get(targetNodes.size() - 1);
195 + boolean isPrefix = isPrefixPresent(nodeId, curPrefix);
196 + String xpath = createValidNameForAugment(nodeId, isPrefix);
182 197
183 - if (nodeId.getPrefix() != null) { 198 + if (listener.getParsedDataStack().peek() instanceof CollisionDetector) {
184 - name = AUGMENTED + getCaptialCase(nodeId.getPrefix()) + getCaptialCase(nodeId.getName()); 199 + try {
185 - } else { 200 + ((CollisionDetector) listener.getParsedDataStack().peek()).detectCollidingChild(xpath,
186 - //TODO: name = name + ((HasAugmentation)getParentNode()).getAugmentPrefix(nodeId); 201 + AUGMENT_DATA);
202 + } catch (DataModelException e) {
203 + return updateNameWhenHasMultipleOuccrrence(nodeId, isPrefix);
204 + }
187 } 205 }
188 - return name; 206 +
207 + clearOccurrenceCount();
208 + return xpath;
209 + }
210 +
211 + /**
212 + * Returns true if a prefix is present and it is not equals to parents prefix.
213 + *
214 + * @param nodeId YANG node identifier
215 + * @param parentsPrefix parent's prefix
216 + * @return true if a prefix is present and it is not equals to parents prefix
217 + */
218 + private static boolean isPrefixPresent(YangNodeIdentifier nodeId, String parentsPrefix) {
219 + return nodeId.getPrefix() != null && nodeId.getPrefix() != parentsPrefix;
189 } 220 }
190 221
191 /** 222 /**
......
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.yangutils.parser.impl.parserutils;
18 +
19 +import java.util.ArrayList;
20 +import java.util.List;
21 +
22 +import org.onosproject.yangutils.datamodel.YangNodeIdentifier;
23 +
24 +import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase;
25 +
26 +/**
27 + * Represents a utility which provides valid name for generated java file for augment node.
28 + */
29 +public final class AugmentJavaFileNameGenUtil {
30 +
31 + /**
32 + * Prefix to be added to generated java file for augment node.
33 + */
34 + private static final String AUGMENTED = "Augmented";
35 +
36 + /**
37 + * The number of time augment has updated the same target node in same module/submodule.
38 + */
39 + private static int occurrenceCount = 1;
40 +
41 + /**
42 + * List of names for generated augment java file.
43 + */
44 + private static List<String> augmentJavaFileNameList = new ArrayList<>();
45 +
46 + private static final int ONE = 1;
47 + private static final int TWO = 2;
48 + private static final int ZERO = 0;
49 +
50 + /**
51 + * Creates an instance of augment java file name generator utility.
52 + */
53 + private AugmentJavaFileNameGenUtil() {
54 + }
55 +
56 + /**
57 + * Sets the augment java file name list.
58 + *
59 + * @param nameList name list
60 + */
61 + private static void setAugmentJavaFileNameList(List<String> nameList) {
62 + augmentJavaFileNameList = nameList;
63 + }
64 +
65 + /**
66 + * Returns augment java file name list.
67 + *
68 + * @return augment java file name list
69 + */
70 + public static List<String> getAugmentJavaFileNameList() {
71 + return augmentJavaFileNameList;
72 + }
73 +
74 + /**
75 + * Sets occurrence count.
76 + *
77 + * @param occurrence occurrence count
78 + */
79 + private static void setOccurrenceCount(int occurrence) {
80 + occurrenceCount = occurrence;
81 + }
82 +
83 + /**
84 + * Returns occurrence count.
85 + *
86 + * @return occurrence count
87 + */
88 + private static int getOccurrenceCount() {
89 + return occurrenceCount;
90 + }
91 +
92 + /**
93 + * Creates a name identifier for augment.
94 + *
95 + * @param nodeId node identifier
96 + * @param isPrefix if prefix is present or it is not equals to parent's prefix
97 + * @return valid name for augment
98 + */
99 + public static String createValidNameForAugment(YangNodeIdentifier nodeId, boolean isPrefix) {
100 + getAugmentJavaFileNameList().add(createName(nodeId, isPrefix));
101 + setAugmentJavaFileNameList(getAugmentJavaFileNameList());
102 + return getAugmentJavaFileNameList().get(getAugmentJavaFileNameList().size() - 1);
103 + }
104 +
105 + /**
106 + * Creates name for the current augment file.
107 + *
108 + * @param nodeId node identifier
109 + * @param isPrefix if prefix is present or it is not equals to parent's prefix
110 + */
111 + private static String createName(YangNodeIdentifier nodeId, boolean isPrefix) {
112 + if (isPrefix) {
113 + return AUGMENTED + getCaptialCase(nodeId.getPrefix()) + getCaptialCase(nodeId.getName());
114 + } else {
115 + return AUGMENTED + getCaptialCase(nodeId.getName());
116 + }
117 + }
118 +
119 + /**
120 + * Updates occurrence count of augment.
121 + */
122 + public static void updateOccurenceCount() {
123 + int count = getOccurrenceCount();
124 + count++;
125 + setOccurrenceCount(count);
126 + }
127 +
128 + /**
129 + * Updates the list of name when augment has occurred multiple times to update the same target node
130 + * and returns a valid name for augment node's generated java file.
131 + *
132 + * @param nodeId YANG node identifier
133 + * @param isPrefix true if a prefix is present and it is not equals to parents prefix
134 + * @return valid name for augment node
135 + */
136 + public static String updateNameWhenHasMultipleOuccrrence(YangNodeIdentifier nodeId, boolean isPrefix) {
137 + String name = "";
138 + updateOccurenceCount();
139 +
140 + if (getOccurrenceCount() == TWO) {
141 + String previousAugmentsName = getAugmentJavaFileNameList().get(getAugmentJavaFileNameList().size() - ONE);
142 + getAugmentJavaFileNameList().remove(ZERO);
143 + getAugmentJavaFileNameList().add(previousAugmentsName + ONE);
144 + //TODO: update when already contains the name.
145 + name = createName(nodeId, isPrefix) + TWO;
146 + } else {
147 + name = createName(nodeId, isPrefix) + getOccurrenceCount();
148 + }
149 + getAugmentJavaFileNameList().add(name);
150 + return name;
151 + }
152 +
153 + /**
154 + * Resets occurrence count to one.
155 + */
156 + public static void clearOccurrenceCount() {
157 + setOccurrenceCount(ONE);
158 + }
159 +
160 +}
...@@ -52,7 +52,9 @@ import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory; ...@@ -52,7 +52,9 @@ import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getDirectory;
52 52
53 /** 53 /**
54 * Represents ONOS YANG utility maven plugin. 54 * Represents ONOS YANG utility maven plugin.
55 - * Goal of plugin is yang2java Execution phase in generate-sources requiresDependencyResolution at compile time. 55 + * Goal of plugin is yang2java.
56 + * Execution phase is generate-sources.
57 + * requiresDependencyResolution at compile time.
56 */ 58 */
57 @Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE, 59 @Mojo(name = "yang2java", defaultPhase = GENERATE_SOURCES, requiresDependencyResolution = COMPILE,
58 requiresProject = true) 60 requiresProject = true)
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.yangutils.parser.parseutils; 17 +package org.onosproject.yangutils.parser.impl.parseutils;
18 18
19 import org.junit.Test; 19 import org.junit.Test;
20 20
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.yangutils.parser.parseutils; 17 +package org.onosproject.yangutils.parser.impl.parseutils;
18 18
19 import org.junit.Rule; 19 import org.junit.Rule;
20 import org.junit.Test; 20 import org.junit.Test;
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17 -package org.onosproject.yangutils.parser.parseutils; 17 +package org.onosproject.yangutils.parser.impl.parseutils;
18 18
19 import org.antlr.v4.runtime.ANTLRFileStream; 19 import org.antlr.v4.runtime.ANTLRFileStream;
20 import org.antlr.v4.runtime.ANTLRInputStream; 20 import org.antlr.v4.runtime.ANTLRInputStream;
......