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; |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 20 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.HasResolutionInfo; |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangImport; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 25 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 26 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 27 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangResolutionInfo; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 30 | import org.onosproject.yangutils.parser.Parsable; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.utils.YangConstructType; |
| 32 | |
| 33 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 34 | * Represents utilities for data model tree. |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 35 | */ |
| 36 | public final class DataModelUtils { |
| 37 | |
| 38 | /** |
| 39 | * Creates a new data model tree utility. |
| 40 | */ |
| 41 | private DataModelUtils() { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Detects the colliding identifier name in a given YANG node and its child. |
| 46 | * |
| 47 | * @param identifierName name for which collision detection is to be |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 48 | * checked |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 49 | * @param dataType type of YANG node asking for detecting collision |
| 50 | * @param node instance of calling node |
| 51 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 52 | */ |
| 53 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 54 | throws DataModelException { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 55 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 56 | if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) { |
| 57 | detectCollidingForUsesGrouping(identifierName, dataType, node); |
| 58 | } else { |
| 59 | if (node instanceof YangLeavesHolder) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 60 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 61 | detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName); |
| 62 | detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName); |
| 63 | } |
| 64 | node = node.getChild(); |
| 65 | while (node != null) { |
| 66 | Parsable parsable = (Parsable) node; |
| 67 | if (node instanceof CollisionDetector |
| 68 | && (parsable.getYangConstructType() != YangConstructType.USES_DATA) |
| 69 | && (parsable.getYangConstructType() != YangConstructType.GROUPING_DATA)) { |
| 70 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 71 | } |
| 72 | node = node.getNextSibling(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 73 | } |
| 74 | } |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Detects colliding of uses and grouping only with uses and grouping respectively. |
| 79 | * |
| 80 | * @param identifierName name for which collision detection is to be |
| 81 | * checked |
| 82 | * @param dataType type of YANG node asking for detecting collision |
| 83 | * @param node node instance of calling node |
| 84 | * @throws DataModelException a violation of data model rules |
| 85 | */ |
| 86 | public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node) |
| 87 | throws DataModelException { |
| 88 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 89 | node = node.getChild(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 90 | while (node != null) { |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 91 | Parsable parsable = (Parsable) node; |
| 92 | if (node instanceof CollisionDetector |
| 93 | && (parsable.getYangConstructType() == dataType)) { |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 94 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 95 | } |
| 96 | node = node.getNextSibling(); |
| 97 | } |
| 98 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 99 | |
| 100 | /** |
| 101 | * Detects the colliding identifier name in a given leaf node. |
| 102 | * |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 103 | * @param listOfLeaf List of leaves to detect collision |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 104 | * @param identifierName name for which collision detection is to be |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 105 | * checked |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 106 | * @throws DataModelException a violation of data model rules |
| 107 | */ |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 108 | private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName) |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 109 | throws DataModelException { |
| 110 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 111 | if (listOfLeaf == null) { |
| 112 | return; |
| 113 | } |
| 114 | for (YangLeaf leaf : listOfLeaf) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 115 | if (leaf.getLeafName().equals(identifierName)) { |
| 116 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \"" |
| 117 | + leaf.getLeafName() + "\""); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Detects the colliding identifier name in a given leaf-list node. |
| 124 | * |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 125 | * @param listOfLeafList list of leaf-lists to detect collision |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 126 | * @param identifierName name for which collision detection is to be |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 127 | * checked |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 128 | * @throws DataModelException a violation of data model rules |
| 129 | */ |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 130 | private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName) |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 131 | throws DataModelException { |
| 132 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 133 | if (listOfLeafList == null) { |
| 134 | return; |
| 135 | } |
| 136 | for (YangLeafList leafList : listOfLeafList) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 137 | if (leafList.getLeafName().equals(identifierName)) { |
| 138 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " + |
| 139 | "list \"" + leafList.getLeafName() + "\""); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Add a resolution information. |
| 146 | * |
| 147 | * @param resolutionInfo information about the YANG construct which has to |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 148 | * be resolved |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 149 | * @throws DataModelException a violation of data model rules |
| 150 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 151 | public static void addResolutionInfo(YangResolutionInfo resolutionInfo) |
| 152 | throws DataModelException { |
| 153 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 154 | /* get the module node to add maintain the list of nested reference */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 155 | YangNode curNode = resolutionInfo.getEntityToResolveInfo() |
| 156 | .getHolderOfEntityToResolve(); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 157 | while (!(curNode instanceof HasResolutionInfo)) { |
| 158 | curNode = curNode.getParent(); |
| 159 | if (curNode == null) { |
| 160 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 161 | } |
| 162 | } |
| 163 | HasResolutionInfo resolutionNode = (HasResolutionInfo) curNode; |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 164 | |
| 165 | if (!isPrefixValid(resolutionInfo.getEntityToResolveInfo().getEntityPrefix(), |
| 166 | resolutionNode)) { |
| 167 | throw new DataModelException("The prefix used is not valid"); |
| 168 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 169 | resolutionNode.addToResolutionList(resolutionInfo); |
| 170 | } |
| 171 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame^] | 172 | /** |
| 173 | * Evaluates whether the prefix in uses/type is valid. |
| 174 | * |
| 175 | * @param entityPrefix prefix in the current module/sub-module |
| 176 | * @param resolutionNode uses/type node which has the prefix with it |
| 177 | * @return whether prefix is valid or not |
| 178 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 179 | private static boolean isPrefixValid(String entityPrefix, HasResolutionInfo resolutionNode) { |
| 180 | if (entityPrefix == null) { |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | if (resolutionNode.getPrefix().contentEquals(entityPrefix)) { |
| 185 | return true; |
| 186 | } |
| 187 | |
| 188 | if (resolutionNode.getImportList() != null) { |
| 189 | for (YangImport importedInfo : resolutionNode.getImportList()) { |
| 190 | if (importedInfo.getPrefixId().contentEquals(entityPrefix)) { |
| 191 | return true; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | if (resolutionNode.getIncludeList() != null) { |
| 197 | /** |
| 198 | * TODO: check if the prefix matches with the imported data |
| 199 | |
| 200 | for (YangInclude includedInfo : resolutionNode.getIncludeList()) { |
| 201 | if (includedInfo.contentEquals(prefix)) { |
| 202 | return true; |
| 203 | } |
| 204 | }*/ |
| 205 | } |
| 206 | |
| 207 | return false; |
| 208 | } |
| 209 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 210 | /** |
| 211 | * Resolve linking for a resolution list. |
| 212 | * |
| 213 | * @param resolutionList resolution list for which linking to be done |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 214 | * @param dataModelRootNode module/sub-module node |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 215 | * @throws DataModelException a violation of data model rules |
| 216 | */ |
| 217 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 218 | HasResolutionInfo dataModelRootNode) |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 219 | throws DataModelException { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 220 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 221 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 222 | resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode.getPrefix()); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 223 | } |
| 224 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 225 | } |