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