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; |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.HasResolutionInfo; |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangImport; |
Gaurav Agrawal | db828bd | 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 | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 28 | import org.onosproject.yangutils.datamodel.YangResolutionInfo; |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 30 | import org.onosproject.yangutils.utils.YangConstructType; |
| 31 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 32 | |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 33 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 34 | * Represents utilities for data model tree. |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 35 | */ |
| 36 | public final class DataModelUtils { |
| 37 | |
| 38 | /** |
| 39 | * Creates a new data model tree utility. |
| 40 | */ |
| 41 | private DataModelUtils() { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Detects the colliding identifier name in a given YANG node and its child. |
| 46 | * |
| 47 | * @param identifierName name for which collision detection is to be |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 48 | * checked |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 49 | * @param dataType type of YANG node asking for detecting collision |
| 50 | * @param node instance of calling node |
| 51 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 52 | */ |
| 53 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 54 | throws DataModelException { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 55 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 56 | if (dataType == YangConstructType.LEAF_DATA) { |
| 57 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
| 58 | if (leavesHolder.getListOfLeaf() != null) { |
| 59 | detectCollidingLeaf(leavesHolder, identifierName); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 60 | } |
| 61 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 62 | if (dataType == YangConstructType.LEAF_LIST_DATA) { |
| 63 | if (((YangLeavesHolder) node).getListOfLeafList() != null) { |
| 64 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
| 65 | detectCollidingLeafList(leavesHolder, identifierName); |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | node = node.getChild(); |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 69 | while (node != null) { |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 70 | if (node instanceof CollisionDetector) { |
| 71 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 72 | } |
| 73 | node = node.getNextSibling(); |
| 74 | } |
| 75 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 76 | |
| 77 | /** |
| 78 | * Detects the colliding identifier name in a given leaf node. |
| 79 | * |
| 80 | * @param leavesHolder leaves node against which collision to be checked |
| 81 | * @param identifierName name for which collision detection is to be |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 82 | * checked |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 83 | * @throws DataModelException a violation of data model rules |
| 84 | */ |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 85 | private static void detectCollidingLeaf(YangLeavesHolder leavesHolder, String identifierName) |
| 86 | throws DataModelException { |
| 87 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 88 | for (YangLeaf leaf : leavesHolder.getListOfLeaf()) { |
| 89 | if (leaf.getLeafName().equals(identifierName)) { |
| 90 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \"" |
| 91 | + leaf.getLeafName() + "\""); |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Detects the colliding identifier name in a given leaf-list node. |
| 98 | * |
| 99 | * @param leavesHolder leaves node against which collision to be checked |
| 100 | * @param identifierName name for which collision detection is to be |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 101 | * checked |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 102 | * @throws DataModelException a violation of data model rules |
| 103 | */ |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 104 | private static void detectCollidingLeafList(YangLeavesHolder leavesHolder, String identifierName) |
| 105 | throws DataModelException { |
| 106 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 107 | for (YangLeafList leafList : leavesHolder.getListOfLeafList()) { |
| 108 | if (leafList.getLeafName().equals(identifierName)) { |
| 109 | throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " + |
| 110 | "list \"" + leafList.getLeafName() + "\""); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Add a resolution information. |
| 117 | * |
| 118 | * @param resolutionInfo information about the YANG construct which has to |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 119 | * be resolved |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 120 | * @throws DataModelException a violation of data model rules |
| 121 | */ |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 122 | public static void addResolutionInfo(YangResolutionInfo resolutionInfo) |
| 123 | throws DataModelException { |
| 124 | |
| 125 | |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 126 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 127 | /* 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] | 128 | YangNode curNode = resolutionInfo.getEntityToResolveInfo() |
| 129 | .getHolderOfEntityToResolve(); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 130 | while (!(curNode instanceof HasResolutionInfo)) { |
| 131 | curNode = curNode.getParent(); |
| 132 | if (curNode == null) { |
| 133 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 134 | } |
| 135 | } |
| 136 | HasResolutionInfo resolutionNode = (HasResolutionInfo) curNode; |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 137 | |
| 138 | if (!isPrefixValid(resolutionInfo.getEntityToResolveInfo().getEntityPrefix(), |
| 139 | resolutionNode)) { |
| 140 | throw new DataModelException("The prefix used is not valid"); |
| 141 | } |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 142 | resolutionNode.addToResolutionList(resolutionInfo); |
| 143 | } |
| 144 | |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 145 | private static boolean isPrefixValid(String entityPrefix, HasResolutionInfo resolutionNode) { |
| 146 | if (entityPrefix == null) { |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | if (resolutionNode.getPrefix().contentEquals(entityPrefix)) { |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | if (resolutionNode.getImportList() != null) { |
| 155 | for (YangImport importedInfo : resolutionNode.getImportList()) { |
| 156 | if (importedInfo.getPrefixId().contentEquals(entityPrefix)) { |
| 157 | return true; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if (resolutionNode.getIncludeList() != null) { |
| 163 | /** |
| 164 | * TODO: check if the prefix matches with the imported data |
| 165 | |
| 166 | for (YangInclude includedInfo : resolutionNode.getIncludeList()) { |
| 167 | if (includedInfo.contentEquals(prefix)) { |
| 168 | return true; |
| 169 | } |
| 170 | }*/ |
| 171 | } |
| 172 | |
| 173 | return false; |
| 174 | } |
| 175 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 176 | /** |
| 177 | * Resolve linking for a resolution list. |
| 178 | * |
| 179 | * @param resolutionList resolution list for which linking to be done |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 180 | * @param dataModelRootNode module/sub-module node |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 181 | * @throws DataModelException a violation of data model rules |
| 182 | */ |
| 183 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 184 | HasResolutionInfo dataModelRootNode) |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 185 | throws DataModelException { |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 186 | |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 187 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
Vinod Kumar S | f677daf | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 188 | resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode.getPrefix()); |
Gaurav Agrawal | 1fbfae1 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 189 | } |
| 190 | } |
Gaurav Agrawal | db828bd | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 191 | } |