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; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 21 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
| 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.YangRpc; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangType; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 30 | import org.onosproject.yangutils.linker.ResolvableType; |
| 31 | import org.onosproject.yangutils.linker.YangReferenceResolver; |
| 32 | import org.onosproject.yangutils.linker.impl.YangResolutionInfo; |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.parser.Parsable; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.plugin.manager.YangFileInfo; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.utils.YangConstructType; |
| 36 | |
| 37 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 38 | * Represents utilities for data model tree. |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 39 | */ |
| 40 | public final class DataModelUtils { |
| 41 | |
| 42 | /** |
| 43 | * Creates a new data model tree utility. |
| 44 | */ |
| 45 | private DataModelUtils() { |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Detects the colliding identifier name in a given YANG node and its child. |
| 50 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 51 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 52 | * @param dataType type of YANG node asking for detecting collision |
| 53 | * @param node instance of calling node |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 54 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 55 | */ |
| 56 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 57 | throws DataModelException { |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 58 | if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) { |
| 59 | detectCollidingForUsesGrouping(identifierName, dataType, node); |
| 60 | } else { |
| 61 | if (node instanceof YangLeavesHolder) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 62 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 63 | detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName); |
| 64 | detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName); |
| 65 | } |
| 66 | node = node.getChild(); |
| 67 | while (node != null) { |
| 68 | Parsable parsable = (Parsable) node; |
| 69 | if (node instanceof CollisionDetector |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 70 | && parsable.getYangConstructType() != YangConstructType.USES_DATA |
| 71 | && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) { |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 72 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 73 | } |
| 74 | node = node.getNextSibling(); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 75 | } |
| 76 | } |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Detects colliding of uses and grouping only with uses and grouping respectively. |
| 81 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 82 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 83 | * @param dataType type of YANG node asking for detecting collision |
| 84 | * @param node node instance of calling node |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 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 |
Bharat saraswal | 715d3fc | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 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 | * |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 104 | * @param listOfLeaf List of leaves to detect collision |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 105 | * @param identifierName name for which collision detection is to be 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 |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 126 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 127 | * @throws DataModelException a violation of data model rules |
| 128 | */ |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 129 | private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName) |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 130 | throws DataModelException { |
| 131 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 132 | if (listOfLeafList == null) { |
| 133 | return; |
| 134 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 135 | for (YangLeafList leafList : listOfLeafList) { |
| 136 | if (leafList.getName().equals(identifierName)) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 137 | 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] | 138 | "list \"" + leafList.getName() + "\""); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Add a resolution information. |
| 145 | * |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 146 | * @param resolutionInfo information about the YANG construct which has to be resolved |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 147 | * @throws DataModelException a violation of data model rules |
| 148 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 149 | public static void addResolutionInfo(YangResolutionInfo resolutionInfo) |
| 150 | throws DataModelException { |
| 151 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 152 | /* 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] | 153 | YangNode curNode = resolutionInfo.getEntityToResolveInfo() |
| 154 | .getHolderOfEntityToResolve(); |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 155 | while (!(curNode instanceof YangReferenceResolver)) { |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 156 | curNode = curNode.getParent(); |
| 157 | if (curNode == null) { |
| 158 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 159 | } |
| 160 | } |
Vinod Kumar S | 79a374b | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 161 | YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode; |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 162 | |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 163 | if (resolutionInfo.getEntityToResolveInfo() |
| 164 | .getEntityToResolve() instanceof YangType) { |
| 165 | resolutionNode.addToResolutionList(resolutionInfo, |
| 166 | ResolvableType.YANG_DERIVED_DATA_TYPE); |
| 167 | } else { |
| 168 | resolutionNode.addToResolutionList(resolutionInfo, |
| 169 | ResolvableType.YANG_USES); |
| 170 | } |
| 171 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 172 | } |
| 173 | |
janani b | 06eca9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 174 | /** |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 175 | * Resolve linking for a resolution list. |
| 176 | * |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 177 | * @param resolutionList resolution list for which linking to be done |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 178 | * @param dataModelRootNode module/sub-module node |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 179 | * @throws DataModelException a violation of data model rules |
| 180 | */ |
| 181 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 182 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 183 | throws DataModelException { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 184 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 185 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 186 | resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Links type/uses referring to typedef/uses of inter YANG file. |
| 192 | * |
| 193 | * @param resolutionList resolution list for which linking to be done |
| 194 | * @param dataModelRootNode module/sub-module node |
| 195 | * @throws DataModelException a violation of data model rules |
| 196 | */ |
| 197 | public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList, |
VinodKumarS-Huawei | d81eccb | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 198 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 199 | throws DataModelException { |
| 200 | /* |
| 201 | * Run through the resolution list, find type/uses referring to |
| 202 | * inter file typedef/grouping, ask for linking. |
| 203 | */ |
| 204 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
| 205 | resolutionInfo.linkInterFile(dataModelRootNode); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 206 | } |
| 207 | } |
VinodKumarS-Huawei | 6266db3 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * Checks if there is any rpc defined in the module or sub-module. |
| 211 | * |
| 212 | * @param rootNode root node of the data model |
| 213 | * @return status of rpc's existence |
| 214 | */ |
| 215 | public static boolean isRpcChildNodePresent(YangNode rootNode) { |
| 216 | YangNode childNode = rootNode.getChild(); |
| 217 | while (childNode != null) { |
| 218 | if (childNode instanceof YangRpc) { |
| 219 | return true; |
| 220 | } |
| 221 | childNode = childNode.getNextSibling(); |
| 222 | } |
| 223 | return false; |
| 224 | } |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 225 | |
| 226 | /** |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 227 | * Returns referred node in a given set. |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 228 | * |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 229 | * @param yangFileInfoSet YANG file info set |
| 230 | * @param refNodeName name of the node which is referred |
| 231 | * @return referred node's reference |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 232 | */ |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 233 | public static YangNode findReferredNode(Set<YangFileInfo> yangFileInfoSet, String refNodeName) { |
| 234 | /* |
| 235 | * Run through the YANG files to see which YANG file matches the |
| 236 | * referred node name. |
| 237 | */ |
| 238 | for (YangFileInfo yangFileInfo : yangFileInfoSet) { |
| 239 | YangNode yangNode = yangFileInfo.getRootNode(); |
| 240 | if (yangNode.getName().equals(refNodeName)) { |
| 241 | return yangFileInfo.getRootNode(); |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 242 | } |
| 243 | } |
Gaurav Agrawal | ab7c4bd | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 244 | return null; |
Vidyashree Rama | b624817 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 245 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 246 | } |