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 | |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 19 | import java.util.Iterator; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 20 | import java.util.List; |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 21 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangImport; |
Gaurav Agrawal | 8e8770a | 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; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangResolutionInfo; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.datamodel.YangRpc; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.parser.Parsable; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 33 | import org.onosproject.yangutils.plugin.manager.YangFileInfo; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.utils.YangConstructType; |
| 35 | |
| 36 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 37 | * Represents utilities for data model tree. |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 38 | */ |
| 39 | public final class DataModelUtils { |
| 40 | |
| 41 | /** |
| 42 | * Creates a new data model tree utility. |
| 43 | */ |
| 44 | private DataModelUtils() { |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Detects the colliding identifier name in a given YANG node and its child. |
| 49 | * |
| 50 | * @param identifierName name for which collision detection is to be |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 51 | * checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 52 | * @param dataType type of YANG node asking for detecting collision |
| 53 | * @param node instance of calling node |
| 54 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 55 | */ |
| 56 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 57 | throws DataModelException { |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 58 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 59 | if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) { |
| 60 | detectCollidingForUsesGrouping(identifierName, dataType, node); |
| 61 | } else { |
| 62 | if (node instanceof YangLeavesHolder) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 63 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 64 | detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName); |
| 65 | detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName); |
| 66 | } |
| 67 | node = node.getChild(); |
| 68 | while (node != null) { |
| 69 | Parsable parsable = (Parsable) node; |
| 70 | if (node instanceof CollisionDetector |
| 71 | && (parsable.getYangConstructType() != YangConstructType.USES_DATA) |
| 72 | && (parsable.getYangConstructType() != YangConstructType.GROUPING_DATA)) { |
| 73 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 74 | } |
| 75 | node = node.getNextSibling(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 76 | } |
| 77 | } |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Detects colliding of uses and grouping only with uses and grouping respectively. |
| 82 | * |
| 83 | * @param identifierName name for which collision detection is to be |
| 84 | * checked |
| 85 | * @param dataType type of YANG node asking for detecting collision |
| 86 | * @param node node instance of calling node |
| 87 | * @throws DataModelException a violation of data model rules |
| 88 | */ |
| 89 | public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node) |
| 90 | throws DataModelException { |
| 91 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 92 | node = node.getChild(); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 93 | while (node != null) { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 94 | Parsable parsable = (Parsable) node; |
| 95 | if (node instanceof CollisionDetector |
| 96 | && (parsable.getYangConstructType() == dataType)) { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 97 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 98 | } |
| 99 | node = node.getNextSibling(); |
| 100 | } |
| 101 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 102 | |
| 103 | /** |
| 104 | * Detects the colliding identifier name in a given leaf node. |
| 105 | * |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 106 | * @param listOfLeaf List of leaves to detect collision |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 107 | * @param identifierName name for which collision detection is to be |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 108 | * checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 109 | * @throws DataModelException a violation of data model rules |
| 110 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 111 | private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 112 | throws DataModelException { |
| 113 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 114 | if (listOfLeaf == null) { |
| 115 | return; |
| 116 | } |
| 117 | for (YangLeaf leaf : listOfLeaf) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 118 | if (leaf.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 119 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \"" |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 120 | + leaf.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Detects the colliding identifier name in a given leaf-list node. |
| 127 | * |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 128 | * @param listOfLeafList list of leaf-lists to detect collision |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 129 | * @param identifierName name for which collision detection is to be |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 130 | * checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 131 | * @throws DataModelException a violation of data model rules |
| 132 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 133 | private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 134 | throws DataModelException { |
| 135 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 136 | if (listOfLeafList == null) { |
| 137 | return; |
| 138 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 139 | for (YangLeafList leafList : listOfLeafList) { |
| 140 | if (leafList.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 141 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " + |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 142 | "list \"" + leafList.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Add a resolution information. |
| 149 | * |
| 150 | * @param resolutionInfo information about the YANG construct which has to |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 151 | * be resolved |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 152 | * @throws DataModelException a violation of data model rules |
| 153 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 154 | public static void addResolutionInfo(YangResolutionInfo resolutionInfo) |
| 155 | throws DataModelException { |
| 156 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 157 | /* get the module node to add maintain the list of nested reference */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 158 | YangNode curNode = resolutionInfo.getEntityToResolveInfo() |
| 159 | .getHolderOfEntityToResolve(); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 160 | while (!(curNode instanceof YangReferenceResolver)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 161 | curNode = curNode.getParent(); |
| 162 | if (curNode == null) { |
| 163 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 164 | } |
| 165 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 166 | YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode; |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 167 | |
| 168 | if (!isPrefixValid(resolutionInfo.getEntityToResolveInfo().getEntityPrefix(), |
| 169 | resolutionNode)) { |
| 170 | throw new DataModelException("The prefix used is not valid"); |
| 171 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 172 | resolutionNode.addToResolutionList(resolutionInfo); |
| 173 | } |
| 174 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 175 | /** |
| 176 | * Evaluates whether the prefix in uses/type is valid. |
| 177 | * |
| 178 | * @param entityPrefix prefix in the current module/sub-module |
| 179 | * @param resolutionNode uses/type node which has the prefix with it |
| 180 | * @return whether prefix is valid or not |
| 181 | */ |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 182 | private static boolean isPrefixValid(String entityPrefix, YangReferenceResolver resolutionNode) { |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 183 | if (entityPrefix == null) { |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | if (resolutionNode.getPrefix().contentEquals(entityPrefix)) { |
| 188 | return true; |
| 189 | } |
| 190 | |
| 191 | if (resolutionNode.getImportList() != null) { |
| 192 | for (YangImport importedInfo : resolutionNode.getImportList()) { |
| 193 | if (importedInfo.getPrefixId().contentEquals(entityPrefix)) { |
| 194 | return true; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (resolutionNode.getIncludeList() != null) { |
| 200 | /** |
| 201 | * TODO: check if the prefix matches with the imported data |
| 202 | |
| 203 | for (YangInclude includedInfo : resolutionNode.getIncludeList()) { |
| 204 | if (includedInfo.contentEquals(prefix)) { |
| 205 | return true; |
| 206 | } |
| 207 | }*/ |
| 208 | } |
| 209 | |
| 210 | return false; |
| 211 | } |
| 212 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 213 | /** |
| 214 | * Resolve linking for a resolution list. |
| 215 | * |
| 216 | * @param resolutionList resolution list for which linking to be done |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 217 | * @param dataModelRootNode module/sub-module node |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 218 | * @throws DataModelException a violation of data model rules |
| 219 | */ |
| 220 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 221 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 222 | throws DataModelException { |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 223 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 224 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 225 | resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode.getPrefix()); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 226 | } |
| 227 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * Checks if there is any rpc defined in the module or sub-module. |
| 231 | * |
| 232 | * @param rootNode root node of the data model |
| 233 | * @return status of rpc's existence |
| 234 | */ |
| 235 | public static boolean isRpcChildNodePresent(YangNode rootNode) { |
| 236 | YangNode childNode = rootNode.getChild(); |
| 237 | while (childNode != null) { |
| 238 | if (childNode instanceof YangRpc) { |
| 239 | return true; |
| 240 | } |
| 241 | childNode = childNode.getNextSibling(); |
| 242 | } |
| 243 | return false; |
| 244 | } |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame^] | 245 | |
| 246 | /** |
| 247 | * Returns module's data model node to which sub-module belongs to. |
| 248 | * |
| 249 | * @param yangFileInfo YANG file information |
| 250 | * @param belongsToModuleName name of the module to which sub-module belongs to |
| 251 | * @return module node to which sub-module belongs to |
| 252 | * @throws DataModelException when belongs to module node is not found |
| 253 | */ |
| 254 | public static YangNode findBelongsToModuleNode(List<YangFileInfo> yangFileInfo, |
| 255 | String belongsToModuleName) throws DataModelException { |
| 256 | Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator(); |
| 257 | while (yangFileIterator.hasNext()) { |
| 258 | YangFileInfo yangFile = yangFileIterator.next(); |
| 259 | YangNode yangNode = yangFile.getRootNode(); |
| 260 | if (yangNode.getName().equals(belongsToModuleName)) { |
| 261 | return yangNode; |
| 262 | } |
| 263 | } |
| 264 | throw new DataModelException("YANG file error : Module " + belongsToModuleName + " to which sub-module " + |
| 265 | "belongs to is not found."); |
| 266 | } |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 267 | } |