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; |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 20 | import java.util.Set; |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 21 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.ResolvableType; |
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; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
| 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; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangType; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 33 | |
Gaurav Agrawal | 8a5af14 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 34 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 35 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 36 | * Represents utilities for data model tree. |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 37 | */ |
| 38 | public final class DataModelUtils { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 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 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 48 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 49 | * @param dataType type of YANG node asking for detecting collision |
| 50 | * @param node instance of calling node |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 51 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 52 | */ |
| 53 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 54 | throws DataModelException { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 55 | if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) { |
| 56 | detectCollidingForUsesGrouping(identifierName, dataType, node); |
| 57 | } else { |
| 58 | if (node instanceof YangLeavesHolder) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 59 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 60 | detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName); |
| 61 | detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName); |
| 62 | } |
| 63 | node = node.getChild(); |
| 64 | while (node != null) { |
| 65 | Parsable parsable = (Parsable) node; |
| 66 | if (node instanceof CollisionDetector |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 67 | && parsable.getYangConstructType() != YangConstructType.USES_DATA |
| 68 | && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 69 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 70 | } |
| 71 | node = node.getNextSibling(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 72 | } |
| 73 | } |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Detects colliding of uses and grouping only with uses and grouping respectively. |
| 78 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 79 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 80 | * @param dataType type of YANG node asking for detecting collision |
| 81 | * @param node node instance of calling node |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 82 | * @throws DataModelException a violation of data model rules |
| 83 | */ |
| 84 | public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node) |
| 85 | throws DataModelException { |
| 86 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 87 | node = node.getChild(); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 88 | while (node != null) { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 89 | Parsable parsable = (Parsable) node; |
| 90 | if (node instanceof CollisionDetector |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 91 | && parsable.getYangConstructType() == dataType) { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 92 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 93 | } |
| 94 | node = node.getNextSibling(); |
| 95 | } |
| 96 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Detects the colliding identifier name in a given leaf node. |
| 100 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 101 | * @param listOfLeaf List of leaves to detect collision |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 102 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 103 | * @throws DataModelException a violation of data model rules |
| 104 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 105 | private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 106 | throws DataModelException { |
| 107 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 108 | if (listOfLeaf == null) { |
| 109 | return; |
| 110 | } |
| 111 | for (YangLeaf leaf : listOfLeaf) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 112 | if (leaf.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 113 | 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] | 114 | + leaf.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Detects the colliding identifier name in a given leaf-list node. |
| 121 | * |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 122 | * @param listOfLeafList list of leaf-lists to detect collision |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 123 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 124 | * @throws DataModelException a violation of data model rules |
| 125 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 126 | private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 127 | throws DataModelException { |
| 128 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 129 | if (listOfLeafList == null) { |
| 130 | return; |
| 131 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 132 | for (YangLeafList leafList : listOfLeafList) { |
| 133 | if (leafList.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 134 | 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] | 135 | "list \"" + leafList.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Add a resolution information. |
| 142 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 143 | * @param resolutionInfo information about the YANG construct which has to be resolved |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 144 | * @throws DataModelException a violation of data model rules |
| 145 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 146 | public static void addResolutionInfo(YangResolutionInfo resolutionInfo) |
| 147 | throws DataModelException { |
| 148 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 149 | /* 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] | 150 | YangNode curNode = resolutionInfo.getEntityToResolveInfo() |
| 151 | .getHolderOfEntityToResolve(); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 152 | while (!(curNode instanceof YangReferenceResolver)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 153 | curNode = curNode.getParent(); |
| 154 | if (curNode == null) { |
| 155 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 156 | } |
| 157 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 158 | YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode; |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 159 | |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 160 | if (resolutionInfo.getEntityToResolveInfo() |
| 161 | .getEntityToResolve() instanceof YangType) { |
| 162 | resolutionNode.addToResolutionList(resolutionInfo, |
| 163 | ResolvableType.YANG_DERIVED_DATA_TYPE); |
| 164 | } else { |
| 165 | resolutionNode.addToResolutionList(resolutionInfo, |
| 166 | ResolvableType.YANG_USES); |
| 167 | } |
| 168 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 169 | } |
| 170 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 171 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 172 | * Resolve linking for a resolution list. |
| 173 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 174 | * @param resolutionList resolution list for which linking to be done |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 175 | * @param dataModelRootNode module/sub-module node |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 176 | * @throws DataModelException a violation of data model rules |
| 177 | */ |
| 178 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 179 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 180 | throws DataModelException { |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 181 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 182 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 183 | resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Links type/uses referring to typedef/uses of inter YANG file. |
| 189 | * |
| 190 | * @param resolutionList resolution list for which linking to be done |
| 191 | * @param dataModelRootNode module/sub-module node |
| 192 | * @throws DataModelException a violation of data model rules |
| 193 | */ |
| 194 | public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList, |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 195 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 196 | throws DataModelException { |
| 197 | /* |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 198 | * Run through the resolution list, find type/uses referring to inter |
| 199 | * file typedef/grouping, ask for linking. |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 200 | */ |
| 201 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
| 202 | resolutionInfo.linkInterFile(dataModelRootNode); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 203 | } |
| 204 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Checks if there is any rpc defined in the module or sub-module. |
| 208 | * |
| 209 | * @param rootNode root node of the data model |
| 210 | * @return status of rpc's existence |
| 211 | */ |
| 212 | public static boolean isRpcChildNodePresent(YangNode rootNode) { |
| 213 | YangNode childNode = rootNode.getChild(); |
| 214 | while (childNode != null) { |
| 215 | if (childNode instanceof YangRpc) { |
| 216 | return true; |
| 217 | } |
| 218 | childNode = childNode.getNextSibling(); |
| 219 | } |
| 220 | return false; |
| 221 | } |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 222 | |
| 223 | /** |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 224 | * Returns referred node in a given set. |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 225 | * |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 226 | * @param yangNodeSet YANG node set |
| 227 | * @param refNodeName name of the node which is referred |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 228 | * @return referred node's reference |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 229 | */ |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 230 | public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 231 | /* |
| 232 | * Run through the YANG files to see which YANG file matches the |
| 233 | * referred node name. |
| 234 | */ |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 235 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 236 | if (yangNode.getName().equals(refNodeName)) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 237 | return yangNode; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 238 | } |
| 239 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 240 | return null; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 241 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * Returns the contained data model parent node. |
| 245 | * |
| 246 | * @param currentNode current node which parent contained node is required |
| 247 | * @return parent node in which the current node is an attribute |
| 248 | */ |
| 249 | public static YangNode getParentNodeInGenCode(YangNode currentNode) { |
| 250 | |
| 251 | /* |
| 252 | * TODO: recursive parent lookup to support choice/augment/uses. TODO: |
| 253 | * need to check if this needs to be updated for |
| 254 | * choice/case/augment/grouping |
| 255 | */ |
| 256 | return currentNode.getParent(); |
| 257 | } |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 258 | } |