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; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 20 | import java.util.Set; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
| 22 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 23 | import org.onosproject.yangutils.datamodel.YangLeafList; |
| 24 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
| 25 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.linker.impl.YangReferenceResolver; |
| 27 | import org.onosproject.yangutils.linker.impl.YangResolutionInfo; |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangRpc; |
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; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.plugin.manager.YangFileInfo; |
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 |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 49 | * checked |
| 50 | * @param dataType type of YANG node asking for detecting collision |
| 51 | * @param node instance of calling node |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 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 { |
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 |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 68 | && parsable.getYangConstructType() != YangConstructType.USES_DATA |
| 69 | && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) { |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 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 |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 81 | * checked |
| 82 | * @param dataType type of YANG node asking for detecting collision |
| 83 | * @param node node instance of calling node |
janani b | 06eca9b | 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 | 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 |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 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 | * |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +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 |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +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) { |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 115 | if (leaf.getName().equals(identifierName)) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 116 | 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] | 117 | + leaf.getName() + "\""); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 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 |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +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 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 136 | for (YangLeafList leafList : listOfLeafList) { |
| 137 | if (leafList.getName().equals(identifierName)) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 138 | 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] | 139 | "list \"" + leafList.getName() + "\""); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Add a resolution information. |
| 146 | * |
| 147 | * @param resolutionInfo information about the YANG construct which has to |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +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(); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 157 | while (!(curNode instanceof YangReferenceResolver)) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 158 | curNode = curNode.getParent(); |
| 159 | if (curNode == null) { |
| 160 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 161 | } |
| 162 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 163 | YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode; |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 164 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 165 | resolutionNode.addToResolutionList(resolutionInfo); |
| 166 | } |
| 167 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 168 | /** |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 169 | * Resolve linking for a resolution list. |
| 170 | * |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 171 | * @param resolutionList resolution list for which linking to be done |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 172 | * @param dataModelRootNode module/sub-module node |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 173 | * @throws DataModelException a violation of data model rules |
| 174 | */ |
| 175 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 176 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 177 | throws DataModelException { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 178 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 179 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 180 | resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Links type/uses referring to typedef/uses of inter YANG file. |
| 186 | * |
| 187 | * @param resolutionList resolution list for which linking to be done |
| 188 | * @param dataModelRootNode module/sub-module node |
| 189 | * @throws DataModelException a violation of data model rules |
| 190 | */ |
| 191 | public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList, |
| 192 | YangReferenceResolver dataModelRootNode) |
| 193 | throws DataModelException { |
| 194 | /* |
| 195 | * Run through the resolution list, find type/uses referring to |
| 196 | * inter file typedef/grouping, ask for linking. |
| 197 | */ |
| 198 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
| 199 | resolutionInfo.linkInterFile(dataModelRootNode); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 200 | } |
| 201 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * Checks if there is any rpc defined in the module or sub-module. |
| 205 | * |
| 206 | * @param rootNode root node of the data model |
| 207 | * @return status of rpc's existence |
| 208 | */ |
| 209 | public static boolean isRpcChildNodePresent(YangNode rootNode) { |
| 210 | YangNode childNode = rootNode.getChild(); |
| 211 | while (childNode != null) { |
| 212 | if (childNode instanceof YangRpc) { |
| 213 | return true; |
| 214 | } |
| 215 | childNode = childNode.getNextSibling(); |
| 216 | } |
| 217 | return false; |
| 218 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 219 | |
| 220 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 221 | * Returns referred node in a given set. |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 222 | * |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 223 | * @param yangFileInfoSet YANG file info set |
| 224 | * @param refNodeName name of the node which is referred |
| 225 | * @return referred node's reference |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 226 | */ |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 227 | public static YangNode findReferredNode(Set<YangFileInfo> yangFileInfoSet, String refNodeName) { |
| 228 | /* |
| 229 | * Run through the YANG files to see which YANG file matches the |
| 230 | * referred node name. |
| 231 | */ |
| 232 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 233 | YangNode yangNode = yangFileInfo.getRootNode(); |
| 234 | if (yangNode.getName().equals(refNodeName)) { |
| 235 | return yangFileInfo.getRootNode(); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 236 | } |
| 237 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 238 | return null; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 239 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 240 | } |