Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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.utils; |
| 18 | |
| 19 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
| 20 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 21 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 22 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 23 | import org.onosproject.yangutils.datamodel.YangNode; |
| 24 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 25 | import org.onosproject.yangutils.utils.YangConstructType; |
| 26 | |
| 27 | /** |
| 28 | * Utilities for data model tree. |
| 29 | */ |
| 30 | public final class DataModelUtils { |
| 31 | |
| 32 | /** |
| 33 | * Creates a new data model tree utility. |
| 34 | */ |
| 35 | private DataModelUtils() { |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Detects the colliding identifier name in a given YANG node and its child. |
| 40 | * |
| 41 | * @param identifierName name for which collision detection is to be |
| 42 | * checked. |
| 43 | * @param dataType type of YANG node asking for detecting collision. |
| 44 | * @param node instance of calling node. |
| 45 | * @throws DataModelException a violation of data model rules. |
| 46 | */ |
| 47 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 48 | throws DataModelException { |
| 49 | if (((YangLeavesHolder) node).getListOfLeaf() != null) { |
| 50 | for (YangLeaf leaf : ((YangLeavesHolder) node).getListOfLeaf()) { |
| 51 | if (leaf.getLeafName().equals(identifierName)) { |
| 52 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \"" |
| 53 | + leaf.getLeafName() + "\""); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | if (((YangLeavesHolder) node).getListOfLeafList() != null) { |
| 58 | for (YangLeafList leafList : ((YangLeavesHolder) node).getListOfLeafList()) { |
| 59 | if (leafList.getLeafName().equals(identifierName)) { |
| 60 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " + |
| 61 | "list \"" + leafList.getLeafName() + "\""); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | node = node.getChild(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 66 | while (node != null) { |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 67 | if (node instanceof CollisionDetector) { |
| 68 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 69 | } |
| 70 | node = node.getNextSibling(); |
| 71 | } |
| 72 | } |
| 73 | } |