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