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