Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 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 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 19 | import java.util.List; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.HasResolutionInfo; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 23 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 24 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 25 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangResolutionInfo; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 28 | import org.onosproject.yangutils.utils.YangConstructType; |
| 29 | |
| 30 | /** |
| 31 | * Utilities for data model tree. |
| 32 | */ |
| 33 | public final class DataModelUtils { |
| 34 | |
| 35 | /** |
| 36 | * Creates a new data model tree utility. |
| 37 | */ |
| 38 | private DataModelUtils() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Detects the colliding identifier name in a given YANG node and its child. |
| 43 | * |
| 44 | * @param identifierName name for which collision detection is to be |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 45 | * checked |
| 46 | * @param dataType type of YANG node asking for detecting collision |
| 47 | * @param node instance of calling node |
| 48 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 49 | */ |
| 50 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 51 | throws DataModelException { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 52 | if (dataType == YangConstructType.LEAF_DATA) { |
| 53 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
| 54 | if (leavesHolder.getListOfLeaf() != null) { |
| 55 | detectCollidingLeaf(leavesHolder, identifierName); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 56 | } |
| 57 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 58 | if (dataType == YangConstructType.LEAF_LIST_DATA) { |
| 59 | if (((YangLeavesHolder) node).getListOfLeafList() != null) { |
| 60 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
| 61 | detectCollidingLeafList(leavesHolder, identifierName); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | node = node.getChild(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 65 | while (node != null) { |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 66 | if (node instanceof CollisionDetector) { |
| 67 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 68 | } |
| 69 | node = node.getNextSibling(); |
| 70 | } |
| 71 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Detects the colliding identifier name in a given leaf node. |
| 75 | * |
| 76 | * @param leavesHolder leaves node against which collision to be checked |
| 77 | * @param identifierName name for which collision detection is to be |
| 78 | * checked |
| 79 | * @throws DataModelException a violation of data model rules |
| 80 | */ |
| 81 | private static void detectCollidingLeaf(YangLeavesHolder leavesHolder, String identifierName) throws |
| 82 | DataModelException { |
| 83 | for (YangLeaf leaf : leavesHolder.getListOfLeaf()) { |
| 84 | if (leaf.getLeafName().equals(identifierName)) { |
| 85 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \"" |
| 86 | + leaf.getLeafName() + "\""); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Detects the colliding identifier name in a given leaf-list node. |
| 93 | * |
| 94 | * @param leavesHolder leaves node against which collision to be checked |
| 95 | * @param identifierName name for which collision detection is to be |
| 96 | * checked |
| 97 | * @throws DataModelException a violation of data model rules |
| 98 | */ |
| 99 | private static void detectCollidingLeafList(YangLeavesHolder leavesHolder, String identifierName) throws |
| 100 | DataModelException { |
| 101 | for (YangLeafList leafList : leavesHolder.getListOfLeafList()) { |
| 102 | if (leafList.getLeafName().equals(identifierName)) { |
| 103 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " + |
| 104 | "list \"" + leafList.getLeafName() + "\""); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Add a resolution information. |
| 111 | * |
| 112 | * @param resolutionInfo information about the YANG construct which has to |
| 113 | * be resolved |
| 114 | * @throws DataModelException a violation of data model rules |
| 115 | */ |
| 116 | public static void addResolutionInfo(YangResolutionInfo resolutionInfo) throws DataModelException { |
| 117 | /* get the module node to add maintain the list of nested reference */ |
| 118 | YangNode curNode = resolutionInfo.getHolderOfEntityToResolve(); |
| 119 | while (!(curNode instanceof HasResolutionInfo)) { |
| 120 | curNode = curNode.getParent(); |
| 121 | if (curNode == null) { |
| 122 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 123 | } |
| 124 | } |
| 125 | HasResolutionInfo resolutionNode = (HasResolutionInfo) curNode; |
| 126 | resolutionNode.addToResolutionList(resolutionInfo); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Resolve linking for a resolution list. |
| 131 | * |
| 132 | * @param resolutionList resolution list for which linking to be done |
| 133 | * @param resolutionInfoNode module/sub-module node |
| 134 | * @throws DataModelException a violation of data model rules |
| 135 | */ |
| 136 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
| 137 | HasResolutionInfo resolutionInfoNode) |
| 138 | throws DataModelException { |
| 139 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
| 140 | if (resolutionInfo.getPrefix() == null || |
| 141 | resolutionInfo.getPrefix().equals(resolutionInfoNode.getPrefix())) { |
| 142 | resolutionInfo.resolveLinkingForResolutionInfo(resolutionInfoNode.getPrefix()); |
| 143 | } |
| 144 | } |
| 145 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 146 | } |