Vidyashree Rama
Committed by Gerrit Code Review

[ONOS-4636]grouping and uses

Change-Id: Ic410d03a838003ad23b2b0e8874b91503da84153
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.datamodel;
18 +
19 +/**
20 + * Represents data model tree traversal types.
21 + */
22 +public enum TraversalType {
23 +
24 + /**
25 + * Start of traversal at the tree root.
26 + */
27 + ROOT,
28 +
29 + /**
30 + * Child node traversal.
31 + */
32 + CHILD,
33 +
34 + /**
35 + * Sibling node traversal.
36 + */
37 + SIBILING,
38 +
39 + /**
40 + * Parent node traversal.
41 + */
42 + PARENT
43 +}
...@@ -17,6 +17,13 @@ package org.onosproject.yangutils.datamodel; ...@@ -17,6 +17,13 @@ package org.onosproject.yangutils.datamodel;
17 17
18 import java.io.Serializable; 18 import java.io.Serializable;
19 import org.onosproject.yangutils.datamodel.exceptions.DataModelException; 19 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
20 +import org.onosproject.yangutils.datamodel.utils.Parsable;
21 +
22 +import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
23 +import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
24 +import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
25 +import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.cloneLeaves;
26 +import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.updateClonedLeavesUnionEnumRef;
20 27
21 /** 28 /**
22 * Represents base class of a node in data model tree. 29 * Represents base class of a node in data model tree.
...@@ -226,4 +233,189 @@ public abstract class YangNode ...@@ -226,4 +233,189 @@ public abstract class YangNode
226 newChild.setPreviousSibling(curNode); 233 newChild.setPreviousSibling(curNode);
227 } 234 }
228 } 235 }
236 +
237 + /**
238 + * Clones the current node contents and create a new node.
239 + *
240 + * @return cloned node
241 + * @throws CloneNotSupportedException clone is not supported by the referred
242 + * node
243 + */
244 + public YangNode clone()
245 + throws CloneNotSupportedException {
246 + YangNode clonedNode = (YangNode) super.clone();
247 + if (clonedNode instanceof YangLeavesHolder) {
248 + try {
249 + cloneLeaves((YangLeavesHolder) clonedNode);
250 + } catch (DataModelException e) {
251 + throw new CloneNotSupportedException(e.getMessage());
252 + }
253 + }
254 +
255 + clonedNode.setParent(null);
256 + clonedNode.setChild(null);
257 + clonedNode.setNextSibling(null);
258 + clonedNode.setPreviousSibling(null);
259 + return clonedNode;
260 + }
261 +
262 + /**
263 + * Clones the subtree from the specified source node to the mentioned target
264 + * node. The source and target root node cloning is carried out by the
265 + * caller.
266 + *
267 + * @param srcRootNode source node for sub tree cloning
268 + * @param dstRootNode destination node where the sub tree needs to be cloned
269 + * @throws DataModelException data model error
270 + */
271 + public static void cloneSubTree(YangNode srcRootNode, YangNode dstRootNode)
272 + throws DataModelException {
273 +
274 + YangNode nextNodeToClone = srcRootNode;
275 + TraversalType curTraversal;
276 +
277 + YangNode clonedTreeCurNode = dstRootNode;
278 + YangNode newNode = null;
279 +
280 + nextNodeToClone = nextNodeToClone.getChild();
281 + if (nextNodeToClone == null) {
282 + return;
283 + } else {
284 + /**
285 + * Root level cloning is taken care in the caller.
286 + */
287 + curTraversal = CHILD;
288 + }
289 +
290 + /**
291 + * Caller ensures the cloning of the root nodes
292 + */
293 + try {
294 + while (nextNodeToClone != srcRootNode) {
295 + if (nextNodeToClone == null) {
296 + throw new DataModelException("Internal error: Cloning failed, source tree null pointer reached");
297 + }
298 + if (curTraversal != PARENT) {
299 + newNode = nextNodeToClone.clone();
300 + detectCollisionWhileCloning(clonedTreeCurNode, newNode, curTraversal);
301 + }
302 +
303 + if (curTraversal == CHILD) {
304 +
305 + /**
306 + * add the new node to the cloned tree.
307 + */
308 + clonedTreeCurNode.addChild(newNode);
309 +
310 + /**
311 + * update the cloned tree's traversal current node as the
312 + * new node.
313 + */
314 + clonedTreeCurNode = newNode;
315 + } else if (curTraversal == SIBILING) {
316 +
317 + clonedTreeCurNode.addNextSibling(newNode);
318 + clonedTreeCurNode = newNode;
319 + } else if (curTraversal == PARENT) {
320 + if (clonedTreeCurNode instanceof YangLeavesHolder) {
321 + updateClonedLeavesUnionEnumRef((YangLeavesHolder) clonedTreeCurNode);
322 + }
323 + clonedTreeCurNode = clonedTreeCurNode.getParent();
324 + }
325 +
326 + if (curTraversal != PARENT && nextNodeToClone.getChild() != null) {
327 + curTraversal = CHILD;
328 +
329 + /**
330 + * update the traversal's current node.
331 + */
332 + nextNodeToClone = nextNodeToClone.getChild();
333 +
334 + } else if (nextNodeToClone.getNextSibling() != null) {
335 +
336 + curTraversal = SIBILING;
337 +
338 + nextNodeToClone = nextNodeToClone.getNextSibling();
339 + } else {
340 + curTraversal = PARENT;
341 + nextNodeToClone = nextNodeToClone.getParent();
342 + }
343 + }
344 + } catch (CloneNotSupportedException e) {
345 + throw new DataModelException("Failed to clone the tree");
346 + }
347 +
348 + }
349 +
350 + /**
351 + * Detects collision when the grouping is deep copied to the uses's parent.
352 + *
353 + * @param currentNode parent/previous sibling node for the new node
354 + * @param newNode node which has to be added
355 + * @param addAs traversal type of the node
356 + * @throws DataModelException data model error
357 + */
358 + private static void detectCollisionWhileCloning(YangNode currentNode, YangNode newNode, TraversalType addAs)
359 + throws DataModelException {
360 + if (!(currentNode instanceof CollisionDetector)
361 + || !(newNode instanceof Parsable)) {
362 + throw new DataModelException("Node in data model tree does not support collision detection");
363 + }
364 +
365 + CollisionDetector collisionDetector = (CollisionDetector) currentNode;
366 + Parsable parsable = (Parsable) newNode;
367 + if (addAs == TraversalType.CHILD) {
368 + collisionDetector.detectCollidingChild(newNode.getName(), parsable.getYangConstructType());
369 + } else if (addAs == TraversalType.SIBILING) {
370 + currentNode = currentNode.getParent();
371 + if (!(currentNode instanceof CollisionDetector)) {
372 + throw new DataModelException("Node in data model tree does not support collision detection");
373 + }
374 + collisionDetector = (CollisionDetector) currentNode;
375 + collisionDetector.detectCollidingChild(newNode.getName(), parsable.getYangConstructType());
376 + } else {
377 + throw new DataModelException("Errored tree cloning");
378 + }
379 +
380 + }
381 +
382 + /**
383 + * Adds a new next sibling.
384 + *
385 + * @param newSibling new sibling to be added
386 + * @throws DataModelException data model error
387 + */
388 + private void addNextSibling(YangNode newSibling)
389 + throws DataModelException {
390 +
391 + if (newSibling.getNodeType() == null) {
392 + throw new DataModelException("Cloned abstract node cannot be inserted into a tree");
393 + }
394 +
395 + if (newSibling.getParent() == null) {
396 + /**
397 + * Since the siblings needs to have a common parent, set the parent
398 + * as the current node's parent
399 + */
400 + newSibling.setParent(getParent());
401 +
402 + } else {
403 + throw new DataModelException("Node is already part of a tree, and cannot be added as a sibling");
404 + }
405 +
406 + if (newSibling.getPreviousSibling() == null) {
407 + newSibling.setPreviousSibling(this);
408 + setNextSibling(newSibling);
409 + } else {
410 + throw new DataModelException("New sibling to be added is not atomic, it already has a previous sibling");
411 + }
412 +
413 + if (newSibling.getChild() != null) {
414 + throw new DataModelException("Sibling to be added is not atomic, it already has a child");
415 + }
416 +
417 + if (newSibling.getNextSibling() != null) {
418 + throw new DataModelException("Sibling to be added is not atomic, it already has a next sibling");
419 + }
420 + }
229 } 421 }
......
...@@ -25,6 +25,7 @@ import org.onosproject.yangutils.datamodel.utils.YangConstructType; ...@@ -25,6 +25,7 @@ import org.onosproject.yangutils.datamodel.utils.YangConstructType;
25 25
26 import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil; 26 import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.detectCollidingChildUtil;
27 import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode; 27 import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
28 +import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.updateClonedLeavesUnionEnumRef;
28 29
29 /*- 30 /*-
30 * Reference RFC 6020. 31 * Reference RFC 6020.
...@@ -329,42 +330,45 @@ public class YangUses ...@@ -329,42 +330,45 @@ public class YangUses
329 } 330 }
330 331
331 YangLeavesHolder usesParentLeavesHolder = (YangLeavesHolder) usesParentNode; 332 YangLeavesHolder usesParentLeavesHolder = (YangLeavesHolder) usesParentNode;
332 - if (referredGrouping.getListOfLeaf() != null 333 + if (referredGrouping.getListOfLeaf() != null) {
333 - && referredGrouping.getListOfLeaf().size() != 0) { 334 + for (YangLeaf leaf : referredGrouping.getListOfLeaf()) {
334 - addLeavesOfGrouping( 335 + YangLeaf clonedLeaf = null;
335 - cloneLeavesList(referredGrouping.getListOfLeaf(), 336 + try {
336 - usesParentLeavesHolder)); 337 + ((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leaf.getName(),
337 - } 338 + YangConstructType.LEAF_DATA);
338 - 339 + clonedLeaf = leaf.clone();
339 - if (referredGrouping.getListOfLeafList() != null 340 +
340 - && referredGrouping.getListOfLeafList().size() != 0) { 341 + } catch (CloneNotSupportedException | DataModelException e) {
341 - addListOfLeafListOfGrouping( 342 + throw new DataModelException(e.getMessage());
342 - cloneListOfLeafList(referredGrouping.getListOfLeafList(), 343 + }
343 - usesParentLeavesHolder)); 344 +
345 + clonedLeaf.setContainedIn(usesParentLeavesHolder);
346 + usesParentLeavesHolder.addLeaf(clonedLeaf);
347 + }
344 } 348 }
345 - 349 + if (referredGrouping.getListOfLeafList() != null) {
346 - YangNode childInGrouping = referredGrouping.getChild(); 350 + for (YangLeafList leafList : referredGrouping.getListOfLeafList()) {
347 - 351 + YangLeafList clonedLeafList = null;
348 - while (childInGrouping != null) { 352 + try {
349 - if (childInGrouping instanceof YangEnumeration 353 + ((CollisionDetector) usesParentLeavesHolder).detectCollidingChild(leafList.getName(),
350 - || childInGrouping instanceof YangUnion 354 + YangConstructType.LEAF_LIST_DATA);
351 - || childInGrouping instanceof YangTypeDef) { 355 + clonedLeafList = leafList.clone();
352 - 356 +
353 - /* 357 + } catch (CloneNotSupportedException | DataModelException e) {
354 - * No need to copy the leaves, union / enum class, as these will 358 + throw new DataModelException(e.getMessage());
355 - * be generated in the scope of grouping 359 + }
356 - */ 360 +
357 - childInGrouping = childInGrouping.getNextSibling(); 361 + clonedLeafList.setContainedIn(usesParentLeavesHolder);
358 - continue; 362 + usesParentLeavesHolder.addLeafList(clonedLeafList);
359 - } else if (childInGrouping instanceof YangUses) {
360 - addResolvedUsesInfoOfGrouping((YangUses) childInGrouping,
361 - usesParentLeavesHolder);
362 - } else {
363 - addNodeOfGrouping(childInGrouping);
364 } 363 }
364 + }
365 365
366 - childInGrouping = childInGrouping.getNextSibling(); 366 + try {
367 + YangNode.cloneSubTree(referredGrouping, usesParentNode);
368 + } catch (DataModelException e) {
369 + throw new DataModelException(e.getMessage());
367 } 370 }
371 + updateClonedLeavesUnionEnumRef(usesParentLeavesHolder);
368 } 372 }
369 373
370 /** 374 /**
......
...@@ -20,6 +20,7 @@ import java.io.FileInputStream; ...@@ -20,6 +20,7 @@ import java.io.FileInputStream;
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.io.ObjectInputStream; 21 import java.io.ObjectInputStream;
22 import java.util.ArrayList; 22 import java.util.ArrayList;
23 +import java.util.LinkedList;
23 import java.util.List; 24 import java.util.List;
24 import java.util.Set; 25 import java.util.Set;
25 26
...@@ -28,6 +29,7 @@ import org.onosproject.yangutils.datamodel.ResolvableType; ...@@ -28,6 +29,7 @@ import org.onosproject.yangutils.datamodel.ResolvableType;
28 import org.onosproject.yangutils.datamodel.YangIfFeature; 29 import org.onosproject.yangutils.datamodel.YangIfFeature;
29 import org.onosproject.yangutils.datamodel.YangAugment; 30 import org.onosproject.yangutils.datamodel.YangAugment;
30 import org.onosproject.yangutils.datamodel.YangBase; 31 import org.onosproject.yangutils.datamodel.YangBase;
32 +import org.onosproject.yangutils.datamodel.YangEnumeration;
31 import org.onosproject.yangutils.datamodel.YangIdentityRef; 33 import org.onosproject.yangutils.datamodel.YangIdentityRef;
32 import org.onosproject.yangutils.datamodel.YangLeaf; 34 import org.onosproject.yangutils.datamodel.YangLeaf;
33 import org.onosproject.yangutils.datamodel.YangLeafList; 35 import org.onosproject.yangutils.datamodel.YangLeafList;
...@@ -38,8 +40,10 @@ import org.onosproject.yangutils.datamodel.YangReferenceResolver; ...@@ -38,8 +40,10 @@ import org.onosproject.yangutils.datamodel.YangReferenceResolver;
38 import org.onosproject.yangutils.datamodel.YangResolutionInfo; 40 import org.onosproject.yangutils.datamodel.YangResolutionInfo;
39 import org.onosproject.yangutils.datamodel.YangRpc; 41 import org.onosproject.yangutils.datamodel.YangRpc;
40 import org.onosproject.yangutils.datamodel.YangType; 42 import org.onosproject.yangutils.datamodel.YangType;
43 +import org.onosproject.yangutils.datamodel.YangUnion;
41 import org.onosproject.yangutils.datamodel.YangUses; 44 import org.onosproject.yangutils.datamodel.YangUses;
42 import org.onosproject.yangutils.datamodel.exceptions.DataModelException; 45 import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
46 +import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
43 47
44 /** 48 /**
45 * Represents utilities for data model tree. 49 * Represents utilities for data model tree.
...@@ -307,4 +311,104 @@ public final class DataModelUtils { ...@@ -307,4 +311,104 @@ public final class DataModelUtils {
307 } 311 }
308 return nodes; 312 return nodes;
309 } 313 }
314 +
315 + /**
316 + * Clones the list of leaves and list of leaf list in the leaves holder.
317 + *
318 + * @param leavesHolder YANG node potentially containing leaves or leaf lists
319 + * @throws CloneNotSupportedException clone is not supported
320 + * @throws DataModelException data model error
321 + */
322 + public static void cloneLeaves(YangLeavesHolder leavesHolder)
323 + throws CloneNotSupportedException, DataModelException {
324 + List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf();
325 + if (currentListOfLeaves != null) {
326 + List<YangLeaf> clonedLeavesList = new LinkedList<YangLeaf>();
327 + for (YangLeaf leaf : currentListOfLeaves) {
328 + YangLeaf clonedLeaf = leaf.clone();
329 + clonedLeaf.setContainedIn(leavesHolder);
330 + clonedLeavesList.add(clonedLeaf);
331 + }
332 + leavesHolder.setListOfLeaf(clonedLeavesList);
333 + }
334 +
335 + List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList();
336 + if (currentListOfLeafList != null) {
337 + List<YangLeafList> clonedListOfLeafList = new LinkedList<YangLeafList>();
338 + for (YangLeafList leafList : currentListOfLeafList) {
339 + YangLeafList clonedLeafList = leafList.clone();
340 + clonedLeafList.setContainedIn(leavesHolder);
341 + clonedListOfLeafList.add(clonedLeafList);
342 + }
343 + leavesHolder.setListOfLeafList(clonedListOfLeafList);
344 + }
345 + }
346 +
347 + /**
348 + * Clones the union or enum leaves. If there is any cloned leaves whose type is union/enum then the corresponding
349 + * type info needs to be updated to the cloned new type node.
350 + *
351 + * @param leavesHolder cloned leaves holder, for whom the leaves reference needs to be updated
352 + */
353 + public static void updateClonedLeavesUnionEnumRef(YangLeavesHolder leavesHolder) throws DataModelException {
354 + List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf();
355 + if (currentListOfLeaves != null) {
356 + for (YangLeaf leaf : currentListOfLeaves) {
357 + if (leaf.getDataType().getDataType() == YangDataTypes.ENUMERATION
358 + || leaf.getDataType().getDataType() == YangDataTypes.UNION) {
359 + try {
360 + updateClonedTypeRef(leaf.getDataType(), leavesHolder);
361 + } catch (DataModelException e) {
362 + throw e;
363 + }
364 + }
365 + }
366 +
367 + }
368 +
369 + List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList();
370 + if (currentListOfLeafList != null) {
371 + for (YangLeafList leafList : currentListOfLeafList) {
372 + if (leafList.getDataType().getDataType() == YangDataTypes.ENUMERATION
373 + || leafList.getDataType().getDataType() == YangDataTypes.UNION) {
374 + try {
375 + updateClonedTypeRef(leafList.getDataType(), leavesHolder);
376 + } catch (DataModelException e) {
377 + throw e;
378 + }
379 + }
380 + }
381 + }
382 + }
383 +
384 + /**
385 + * Updates the types extended info pointer to point to the cloned type node.
386 + *
387 + * @param dataType data type, whose extended info needs to be pointed to the cloned type
388 + * @param leavesHolder the leaves holder having the cloned type
389 + */
390 + private static void updateClonedTypeRef(YangType dataType, YangLeavesHolder leavesHolder)
391 + throws DataModelException {
392 + if (!(leavesHolder instanceof YangNode)) {
393 + throw new DataModelException("Data model error: cloned leaves holder is not a node");
394 + }
395 + YangNode potentialTypeNode = ((YangNode) leavesHolder).getChild();
396 + while (potentialTypeNode != null) {
397 + String dataTypeName = null;
398 + if (dataType.getDataType() == YangDataTypes.ENUMERATION) {
399 + YangEnumeration enumNode = (YangEnumeration) dataType.getDataTypeExtendedInfo();
400 + dataTypeName = enumNode.getName();
401 + } else if (dataType.getDataType() == YangDataTypes.UNION) {
402 + YangUnion unionNode = (YangUnion) dataType.getDataTypeExtendedInfo();
403 + dataTypeName = unionNode.getName();
404 + }
405 + if (potentialTypeNode.getName().contentEquals(dataTypeName)) {
406 + dataType.setDataTypeExtendedInfo((Object) potentialTypeNode);
407 + return;
408 + }
409 + potentialTypeNode = potentialTypeNode.getNextSibling();
410 + }
411 +
412 + throw new DataModelException("Data model error: cloned leaves type is not found");
413 + }
310 } 414 }
......
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.translator.exception;
18 +
19 +/**
20 + * Represents custom translator exception for translator's operations.
21 + */
22 +public class InvalidNodeForTranslatorException extends RuntimeException {
23 +
24 + private static final long serialVersionUID = 20160311L;
25 + private String fileName;
26 +
27 + /**
28 + * Create a new exception.
29 + */
30 + public InvalidNodeForTranslatorException() {
31 + super();
32 + }
33 +
34 + /**
35 + * Creates a new exception with given message.
36 + *
37 + * @param message the detail of exception in string
38 + */
39 + public InvalidNodeForTranslatorException(String message) {
40 + super(message);
41 + }
42 +
43 + /**
44 + * Creates a new exception from given message and cause.
45 + *
46 + * @param message the detail of exception in string
47 + * @param cause underlying cause of the error
48 + */
49 + public InvalidNodeForTranslatorException(final String message, final Throwable cause) {
50 + super(message, cause);
51 + }
52 +
53 + /**
54 + * Creates a new exception from cause.
55 + *
56 + * @param cause underlying cause of the error
57 + */
58 + public InvalidNodeForTranslatorException(final Throwable cause) {
59 + super(cause);
60 + }
61 +
62 + /**
63 + * Returns generated file name for the exception.
64 + *
65 + * @return generated file name for the exception
66 + */
67 + public String getFileName() {
68 + return this.fileName;
69 + }
70 +
71 + /**
72 + * Sets file name in translator exception.
73 + *
74 + * @param fileName generated file name
75 + */
76 + public void setFileName(String fileName) {
77 + this.fileName = fileName;
78 + }
79 +}
...@@ -17,17 +17,16 @@ ...@@ -17,17 +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 - 20 +import org.onosproject.yangutils.datamodel.TraversalType;
21 import org.onosproject.yangutils.datamodel.YangNode; 21 import org.onosproject.yangutils.datamodel.YangNode;
22 -import org.onosproject.yangutils.datamodel.YangTypeDef; 22 +import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
23 -import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes;
24 import org.onosproject.yangutils.translator.exception.TranslatorException; 23 import org.onosproject.yangutils.translator.exception.TranslatorException;
25 import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; 24 import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
26 25
27 -import static org.onosproject.yangutils.translator.tojava.TraversalType.CHILD; 26 +import static org.onosproject.yangutils.datamodel.TraversalType.CHILD;
28 -import static org.onosproject.yangutils.translator.tojava.TraversalType.PARENT; 27 +import static org.onosproject.yangutils.datamodel.TraversalType.PARENT;
29 -import static org.onosproject.yangutils.translator.tojava.TraversalType.ROOT; 28 +import static org.onosproject.yangutils.datamodel.TraversalType.ROOT;
30 -import static org.onosproject.yangutils.translator.tojava.TraversalType.SIBILING; 29 +import static org.onosproject.yangutils.datamodel.TraversalType.SIBILING;
31 30
32 /** 31 /**
33 * Representation of java code generator based on application schema. 32 * Representation of java code generator based on application schema.
...@@ -82,23 +81,18 @@ public final class JavaCodeGeneratorUtil { ...@@ -82,23 +81,18 @@ public final class JavaCodeGeneratorUtil {
82 if (!(codeGenNode instanceof JavaCodeGenerator)) { 81 if (!(codeGenNode instanceof JavaCodeGenerator)) {
83 throw new TranslatorException("Unsupported node to generate code"); 82 throw new TranslatorException("Unsupported node to generate code");
84 } 83 }
85 - if (codeGenNode instanceof YangTypeDef) {
86 - YangTypeDef typeDef = (YangTypeDef) codeGenNode;
87 - if (typeDef.getTypeDefBaseType().getDataType() == YangDataTypes.LEAFREF
88 - || typeDef.getTypeDefBaseType().getDataType() == YangDataTypes.IDENTITYREF) {
89 - if (codeGenNode.getNextSibling() != null) {
90 - curTraversal = SIBILING;
91 - codeGenNode = codeGenNode.getNextSibling();
92 - } else {
93 - curTraversal = PARENT;
94 - codeGenNode = codeGenNode.getParent();
95 - }
96 - continue;
97 - }
98 - }
99 setCurNode(codeGenNode); 84 setCurNode(codeGenNode);
100 try { 85 try {
101 generateCodeEntry(codeGenNode, yangPlugin); 86 generateCodeEntry(codeGenNode, yangPlugin);
87 + } catch (InvalidNodeForTranslatorException e) {
88 + if (codeGenNode.getNextSibling() != null) {
89 + curTraversal = SIBILING;
90 + codeGenNode = codeGenNode.getNextSibling();
91 + } else {
92 + curTraversal = PARENT;
93 + codeGenNode = codeGenNode.getParent();
94 + }
95 + continue;
102 } catch (Exception e) { 96 } catch (Exception e) {
103 throw new TranslatorException(e.getMessage()); 97 throw new TranslatorException(e.getMessage());
104 } 98 }
......
...@@ -15,9 +15,8 @@ ...@@ -15,9 +15,8 @@
15 */ 15 */
16 package org.onosproject.yangutils.translator.tojava.javamodel; 16 package org.onosproject.yangutils.translator.tojava.javamodel;
17 17
18 -import java.io.IOException;
19 -
20 import org.onosproject.yangutils.datamodel.YangGrouping; 18 import org.onosproject.yangutils.datamodel.YangGrouping;
19 +import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
21 import org.onosproject.yangutils.translator.exception.TranslatorException; 20 import org.onosproject.yangutils.translator.exception.TranslatorException;
22 import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; 21 import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
23 import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; 22 import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
...@@ -25,8 +24,6 @@ import org.onosproject.yangutils.translator.tojava.JavaFileInfo; ...@@ -25,8 +24,6 @@ import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
25 import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; 24 import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
26 import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; 25 import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
27 26
28 -import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.updatePackageInfo;
29 -
30 /** 27 /**
31 * Represents grouping information extended to support java code generation. 28 * Represents grouping information extended to support java code generation.
32 */ 29 */
...@@ -102,11 +99,7 @@ public class YangJavaGrouping ...@@ -102,11 +99,7 @@ public class YangJavaGrouping
102 @Override 99 @Override
103 public void generateCodeEntry(YangPluginConfig yangPlugin) 100 public void generateCodeEntry(YangPluginConfig yangPlugin)
104 throws TranslatorException { 101 throws TranslatorException {
105 - try { 102 + throw new InvalidNodeForTranslatorException();
106 - updatePackageInfo(this, yangPlugin);
107 - } catch (IOException e) {
108 - throw new TranslatorException(e.getCause());
109 - }
110 } 103 }
111 104
112 @Override 105 @Override
......
...@@ -15,14 +15,8 @@ ...@@ -15,14 +15,8 @@
15 */ 15 */
16 package org.onosproject.yangutils.translator.tojava.javamodel; 16 package org.onosproject.yangutils.translator.tojava.javamodel;
17 17
18 -import java.io.IOException;
19 -import java.util.List;
20 -
21 -import org.onosproject.yangutils.datamodel.YangGrouping;
22 -import org.onosproject.yangutils.datamodel.YangLeaf;
23 -import org.onosproject.yangutils.datamodel.YangLeafList;
24 -import org.onosproject.yangutils.datamodel.YangNode;
25 import org.onosproject.yangutils.datamodel.YangUses; 18 import org.onosproject.yangutils.datamodel.YangUses;
19 +import org.onosproject.yangutils.translator.exception.InvalidNodeForTranslatorException;
26 import org.onosproject.yangutils.translator.exception.TranslatorException; 20 import org.onosproject.yangutils.translator.exception.TranslatorException;
27 import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator; 21 import org.onosproject.yangutils.translator.tojava.JavaCodeGenerator;
28 import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo; 22 import org.onosproject.yangutils.translator.tojava.JavaCodeGeneratorInfo;
...@@ -30,10 +24,6 @@ import org.onosproject.yangutils.translator.tojava.JavaFileInfo; ...@@ -30,10 +24,6 @@ import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
30 import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles; 24 import org.onosproject.yangutils.translator.tojava.TempJavaCodeFragmentFiles;
31 import org.onosproject.yangutils.utils.io.impl.YangPluginConfig; 25 import org.onosproject.yangutils.utils.io.impl.YangPluginConfig;
32 26
33 -import static org.onosproject.yangutils.datamodel.utils.DataModelUtils.getParentNodeInGenCode;
34 -import static org.onosproject.yangutils.translator.tojava.TempJavaFragmentFiles.addCurNodeAsAttributeInTargetTempFile;
35 -import static org.onosproject.yangutils.translator.tojava.javamodel.YangJavaModelUtils.updatePackageInfo;
36 -
37 /** 27 /**
38 * Represents uses information extended to support java code generation. 28 * Represents uses information extended to support java code generation.
39 */ 29 */
...@@ -108,42 +98,7 @@ public class YangJavaUses ...@@ -108,42 +98,7 @@ public class YangJavaUses
108 @Override 98 @Override
109 public void generateCodeEntry(YangPluginConfig yangPlugin) 99 public void generateCodeEntry(YangPluginConfig yangPlugin)
110 throws TranslatorException { 100 throws TranslatorException {
111 - try { 101 + throw new InvalidNodeForTranslatorException();
112 - updatePackageInfo(this, yangPlugin);
113 -
114 - if (!(getParentNodeInGenCode(this) instanceof JavaCodeGeneratorInfo)) {
115 - throw new TranslatorException("invalid container of uses");
116 - }
117 - JavaCodeGeneratorInfo javaCodeGeneratorInfo = (JavaCodeGeneratorInfo) getParentNodeInGenCode(this);
118 -
119 - if (javaCodeGeneratorInfo instanceof YangGrouping) {
120 - /*
121 - * Do nothing, since it will taken care in the groupings uses.
122 - */
123 - return;
124 - }
125 -
126 - for (List<YangLeaf> leavesList : getUsesResolvedLeavesList()) {
127 - // add the resolved leaves to the parent as an attribute
128 - javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
129 - .getBeanTempFiles().addLeavesInfoToTempFiles(leavesList, yangPlugin);
130 - }
131 -
132 - for (List<YangLeafList> listOfLeafLists : getUsesResolvedListOfLeafList()) {
133 - // add the resolved leaf-list to the parent as an attribute
134 - javaCodeGeneratorInfo.getTempJavaCodeFragmentFiles()
135 - .getBeanTempFiles().addLeafListInfoToTempFiles(listOfLeafLists, yangPlugin);
136 - }
137 -
138 - for (YangNode usesResolvedNode : getUsesResolvedNodeList()) {
139 - // add the resolved nodes to the parent as an attribute
140 - addCurNodeAsAttributeInTargetTempFile(usesResolvedNode, yangPlugin,
141 - getParentNodeInGenCode(this));
142 - }
143 -
144 - } catch (IOException e) {
145 - throw new TranslatorException(e.getCause());
146 - }
147 } 102 }
148 103
149 @Override 104 @Override
......
...@@ -26,6 +26,7 @@ import org.onosproject.yangutils.datamodel.YangAugment; ...@@ -26,6 +26,7 @@ import org.onosproject.yangutils.datamodel.YangAugment;
26 import org.onosproject.yangutils.datamodel.YangNode; 26 import org.onosproject.yangutils.datamodel.YangNode;
27 import org.onosproject.yangutils.datamodel.YangReferenceResolver; 27 import org.onosproject.yangutils.datamodel.YangReferenceResolver;
28 import org.onosproject.yangutils.datamodel.YangResolutionInfo; 28 import org.onosproject.yangutils.datamodel.YangResolutionInfo;
29 +import org.onosproject.yangutils.linker.exceptions.LinkerException;
29 import org.onosproject.yangutils.linker.impl.YangLinkerManager; 30 import org.onosproject.yangutils.linker.impl.YangLinkerManager;
30 import org.onosproject.yangutils.linker.impl.YangXpathLinker; 31 import org.onosproject.yangutils.linker.impl.YangXpathLinker;
31 import org.onosproject.yangutils.utils.io.impl.YangFileScanner; 32 import org.onosproject.yangutils.utils.io.impl.YangFileScanner;
...@@ -229,7 +230,7 @@ public class YangXpathLinkerTest { ...@@ -229,7 +230,7 @@ public class YangXpathLinkerTest {
229 * 230 *
230 * @throws IOException when fails to do IO operations 231 * @throws IOException when fails to do IO operations
231 */ 232 */
232 - @Test 233 + @Test(expected = LinkerException.class)
233 public void processIntraFileLinkingInUsesSingleLevel() throws IOException { 234 public void processIntraFileLinkingInUsesSingleLevel() throws IOException {
234 235
235 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(INTRA_FILE_PATH + "IntraSingleUses/")); 236 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(INTRA_FILE_PATH + "IntraSingleUses/"));
...@@ -259,7 +260,7 @@ public class YangXpathLinkerTest { ...@@ -259,7 +260,7 @@ public class YangXpathLinkerTest {
259 * 260 *
260 * @throws IOException when fails to do IO operations 261 * @throws IOException when fails to do IO operations
261 */ 262 */
262 - @Test 263 + @Test(expected = LinkerException.class)
263 public void processIntraFileLinkingInUsesMultiLevel() throws IOException { 264 public void processIntraFileLinkingInUsesMultiLevel() throws IOException {
264 265
265 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(INTRA_FILE_PATH + "IntraMultiUses/")); 266 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(INTRA_FILE_PATH + "IntraMultiUses/"));
...@@ -568,7 +569,7 @@ public class YangXpathLinkerTest { ...@@ -568,7 +569,7 @@ public class YangXpathLinkerTest {
568 * 569 *
569 * @throws IOException when fails to do IO operations 570 * @throws IOException when fails to do IO operations
570 */ 571 */
571 - @Test 572 + @Test(expected = LinkerException.class)
572 public void processInterFileLinkingInUsesMultiLevel() throws IOException { 573 public void processInterFileLinkingInUsesMultiLevel() throws IOException {
573 574
574 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(INTER_FILE_PATH + "InterMultiUses/")); 575 utilManager.createYangFileInfoSet(YangFileScanner.getYangFiles(INTER_FILE_PATH + "InterMultiUses/"));
......