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 | |
| 19 | import org.onosproject.yangutils.datamodel.CollisionDetector; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.ResolvableType; |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangAtomicPath; |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangAugment; |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 23 | import org.onosproject.yangutils.datamodel.YangBase; |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 24 | import org.onosproject.yangutils.datamodel.YangEntityToResolveInfoImpl; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 25 | import org.onosproject.yangutils.datamodel.YangEnumeration; |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 26 | import org.onosproject.yangutils.datamodel.YangIdentityRef; |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 27 | import org.onosproject.yangutils.datamodel.YangIfFeature; |
| 28 | import org.onosproject.yangutils.datamodel.YangImport; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 29 | import org.onosproject.yangutils.datamodel.YangLeaf; |
| 30 | import org.onosproject.yangutils.datamodel.YangLeafList; |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 31 | import org.onosproject.yangutils.datamodel.YangLeafRef; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 32 | import org.onosproject.yangutils.datamodel.YangLeavesHolder; |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 33 | import org.onosproject.yangutils.datamodel.YangModule; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 34 | import org.onosproject.yangutils.datamodel.YangNode; |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 35 | import org.onosproject.yangutils.datamodel.YangReferenceResolver; |
| 36 | import org.onosproject.yangutils.datamodel.YangResolutionInfo; |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 37 | import org.onosproject.yangutils.datamodel.YangRpc; |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 38 | import org.onosproject.yangutils.datamodel.YangType; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 39 | import org.onosproject.yangutils.datamodel.YangUnion; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 40 | import org.onosproject.yangutils.datamodel.YangUses; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 41 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 42 | import org.onosproject.yangutils.datamodel.utils.builtindatatype.YangDataTypes; |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 43 | |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 44 | import java.io.FileInputStream; |
| 45 | import java.io.IOException; |
| 46 | import java.io.ObjectInputStream; |
| 47 | import java.util.ArrayList; |
| 48 | import java.util.Iterator; |
| 49 | import java.util.LinkedList; |
| 50 | import java.util.List; |
| 51 | import java.util.Map; |
| 52 | import java.util.Set; |
| 53 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 54 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 55 | * Represents utilities for data model tree. |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 56 | */ |
| 57 | public final class DataModelUtils { |
Mahesh Poojary S | bbd4849 | 2016-07-19 10:58:07 +0530 | [diff] [blame^] | 58 | public static final String TRUE = "true"; |
| 59 | public static final String FALSE = "false"; |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 60 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 61 | /** |
| 62 | * Creates a new data model tree utility. |
| 63 | */ |
| 64 | private DataModelUtils() { |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Detects the colliding identifier name in a given YANG node and its child. |
| 69 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 70 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 71 | * @param dataType type of YANG node asking for detecting collision |
| 72 | * @param node instance of calling node |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 73 | * @throws DataModelException a violation of data model rules |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 74 | */ |
| 75 | public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node) |
| 76 | throws DataModelException { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 77 | if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) { |
| 78 | detectCollidingForUsesGrouping(identifierName, dataType, node); |
| 79 | } else { |
| 80 | if (node instanceof YangLeavesHolder) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 81 | YangLeavesHolder leavesHolder = (YangLeavesHolder) node; |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 82 | detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName); |
| 83 | detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName); |
| 84 | } |
| 85 | node = node.getChild(); |
| 86 | while (node != null) { |
| 87 | Parsable parsable = (Parsable) node; |
| 88 | if (node instanceof CollisionDetector |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 89 | && parsable.getYangConstructType() != YangConstructType.USES_DATA |
| 90 | && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 91 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 92 | } |
| 93 | node = node.getNextSibling(); |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 94 | } |
| 95 | } |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Detects colliding of uses and grouping only with uses and grouping respectively. |
| 100 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 101 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 102 | * @param dataType type of YANG node asking for detecting collision |
| 103 | * @param node node instance of calling node |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 104 | * @throws DataModelException a violation of data model rules |
| 105 | */ |
| 106 | public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node) |
| 107 | throws DataModelException { |
| 108 | |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 109 | node = node.getChild(); |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 110 | while (node != null) { |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 111 | Parsable parsable = (Parsable) node; |
| 112 | if (node instanceof CollisionDetector |
Bharat saraswal | 33dfa01 | 2016-05-17 19:59:16 +0530 | [diff] [blame] | 113 | && parsable.getYangConstructType() == dataType) { |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 114 | ((CollisionDetector) node).detectSelfCollision(identifierName, dataType); |
| 115 | } |
| 116 | node = node.getNextSibling(); |
| 117 | } |
| 118 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Detects the colliding identifier name in a given leaf node. |
| 122 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 123 | * @param listOfLeaf List of leaves to detect collision |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 124 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 125 | * @throws DataModelException a violation of data model rules |
| 126 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 127 | private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 128 | throws DataModelException { |
| 129 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 130 | if (listOfLeaf == null) { |
| 131 | return; |
| 132 | } |
| 133 | for (YangLeaf leaf : listOfLeaf) { |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 134 | if (leaf.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 135 | 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] | 136 | + leaf.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Detects the colliding identifier name in a given leaf-list node. |
| 143 | * |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 144 | * @param listOfLeafList list of leaf-lists to detect collision |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 145 | * @param identifierName name for which collision detection is to be checked |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 146 | * @throws DataModelException a violation of data model rules |
| 147 | */ |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 148 | private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName) |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 149 | throws DataModelException { |
| 150 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 151 | if (listOfLeafList == null) { |
| 152 | return; |
| 153 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 154 | for (YangLeafList leafList : listOfLeafList) { |
| 155 | if (leafList.getName().equals(identifierName)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 156 | 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] | 157 | "list \"" + leafList.getName() + "\""); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Add a resolution information. |
| 164 | * |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 165 | * @param resolutionInfo information about the YANG construct which has to be resolved |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 166 | * @throws DataModelException a violation of data model rules |
| 167 | */ |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 168 | public static void addResolutionInfo(YangResolutionInfo resolutionInfo) |
| 169 | throws DataModelException { |
| 170 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 171 | /* 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] | 172 | YangNode curNode = resolutionInfo.getEntityToResolveInfo() |
| 173 | .getHolderOfEntityToResolve(); |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 174 | while (!(curNode instanceof YangReferenceResolver)) { |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 175 | curNode = curNode.getParent(); |
| 176 | if (curNode == null) { |
| 177 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 178 | } |
| 179 | } |
Vinod Kumar S | e4b9b0c | 2016-04-30 21:09:15 +0530 | [diff] [blame] | 180 | YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode; |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 181 | |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 182 | if (resolutionInfo.getEntityToResolveInfo() |
| 183 | .getEntityToResolve() instanceof YangType) { |
| 184 | resolutionNode.addToResolutionList(resolutionInfo, |
| 185 | ResolvableType.YANG_DERIVED_DATA_TYPE); |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 186 | } else if (resolutionInfo.getEntityToResolveInfo() |
| 187 | .getEntityToResolve() instanceof YangUses) { |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 188 | resolutionNode.addToResolutionList(resolutionInfo, |
| 189 | ResolvableType.YANG_USES); |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 190 | } else if (resolutionInfo.getEntityToResolveInfo() |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 191 | .getEntityToResolve() instanceof YangAugment) { |
| 192 | resolutionNode.addToResolutionList(resolutionInfo, |
| 193 | ResolvableType.YANG_AUGMENT); |
| 194 | } else if (resolutionInfo.getEntityToResolveInfo() |
Vidyashree Rama | deac28b | 2016-06-20 15:12:43 +0530 | [diff] [blame] | 195 | .getEntityToResolve() instanceof YangIfFeature) { |
| 196 | resolutionNode.addToResolutionList(resolutionInfo, |
| 197 | ResolvableType.YANG_IF_FEATURE); |
janani b | e18b534 | 2016-07-13 21:06:41 +0530 | [diff] [blame] | 198 | } else if (resolutionInfo.getEntityToResolveInfo() |
| 199 | .getEntityToResolve() instanceof YangLeafRef) { |
| 200 | resolutionNode.addToResolutionList(resolutionInfo, |
| 201 | ResolvableType.YANG_LEAFREF); |
Shankara-Huawei | df7b9ca | 2016-07-14 11:35:34 +0530 | [diff] [blame] | 202 | } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangBase) { |
| 203 | resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_BASE); |
| 204 | } else if (resolutionInfo.getEntityToResolveInfo().getEntityToResolve() instanceof YangIdentityRef) { |
| 205 | resolutionNode.addToResolutionList(resolutionInfo, ResolvableType.YANG_IDENTITYREF); |
VinodKumarS-Huawei | 2ee9e7e | 2016-06-01 14:30:22 +0530 | [diff] [blame] | 206 | } |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 207 | } |
| 208 | |
janani b | 4e53f9b | 2016-04-26 18:49:20 +0530 | [diff] [blame] | 209 | /** |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 210 | * Resolve linking for a resolution list. |
| 211 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 212 | * @param resolutionList resolution list for which linking to be done |
Vinod Kumar S | d4deb06 | 2016-04-15 18:08:57 +0530 | [diff] [blame] | 213 | * @param dataModelRootNode module/sub-module node |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 214 | * @throws DataModelException a violation of data model rules |
| 215 | */ |
| 216 | public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList, |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 217 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 218 | throws DataModelException { |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 219 | |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 220 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 221 | resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Links type/uses referring to typedef/uses of inter YANG file. |
| 227 | * |
| 228 | * @param resolutionList resolution list for which linking to be done |
| 229 | * @param dataModelRootNode module/sub-module node |
| 230 | * @throws DataModelException a violation of data model rules |
| 231 | */ |
| 232 | public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList, |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 233 | YangReferenceResolver dataModelRootNode) |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 234 | throws DataModelException { |
| 235 | /* |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 236 | * Run through the resolution list, find type/uses referring to inter |
| 237 | * file typedef/grouping, ask for linking. |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 238 | */ |
| 239 | for (YangResolutionInfo resolutionInfo : resolutionList) { |
| 240 | resolutionInfo.linkInterFile(dataModelRootNode); |
Gaurav Agrawal | d9d6cc8 | 2016-03-29 02:17:23 +0530 | [diff] [blame] | 241 | } |
| 242 | } |
VinodKumarS-Huawei | cb3a1f5 | 2016-05-10 17:58:57 +0530 | [diff] [blame] | 243 | |
| 244 | /** |
| 245 | * Checks if there is any rpc defined in the module or sub-module. |
| 246 | * |
| 247 | * @param rootNode root node of the data model |
| 248 | * @return status of rpc's existence |
| 249 | */ |
| 250 | public static boolean isRpcChildNodePresent(YangNode rootNode) { |
| 251 | YangNode childNode = rootNode.getChild(); |
| 252 | while (childNode != null) { |
| 253 | if (childNode instanceof YangRpc) { |
| 254 | return true; |
| 255 | } |
| 256 | childNode = childNode.getNextSibling(); |
| 257 | } |
| 258 | return false; |
| 259 | } |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 260 | |
| 261 | /** |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 262 | * Returns referred node in a given set. |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 263 | * |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 264 | * @param yangNodeSet YANG node set |
| 265 | * @param refNodeName name of the node which is referred |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 266 | * @return referred node's reference |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 267 | */ |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 268 | public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 269 | /* |
| 270 | * Run through the YANG files to see which YANG file matches the |
| 271 | * referred node name. |
| 272 | */ |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 273 | for (YangNode yangNode : yangNodeSet) { |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 274 | if (yangNode.getName().equals(refNodeName)) { |
Gaurav Agrawal | 95b416c | 2016-06-07 14:00:26 +0530 | [diff] [blame] | 275 | return yangNode; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 276 | } |
| 277 | } |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 278 | return null; |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 279 | } |
Bharat saraswal | 96dfef0 | 2016-06-16 00:29:12 +0530 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * Returns the contained data model parent node. |
| 283 | * |
| 284 | * @param currentNode current node which parent contained node is required |
| 285 | * @return parent node in which the current node is an attribute |
| 286 | */ |
| 287 | public static YangNode getParentNodeInGenCode(YangNode currentNode) { |
| 288 | |
| 289 | /* |
| 290 | * TODO: recursive parent lookup to support choice/augment/uses. TODO: |
| 291 | * need to check if this needs to be updated for |
| 292 | * choice/case/augment/grouping |
| 293 | */ |
| 294 | return currentNode.getParent(); |
| 295 | } |
Bharat saraswal | b1170bd | 2016-07-14 13:26:18 +0530 | [diff] [blame] | 296 | |
| 297 | /** |
| 298 | * Returns de-serializes YANG data-model nodes. |
| 299 | * |
| 300 | * @param serializableInfoSet YANG file info set |
| 301 | * @return de-serializes YANG data-model nodes |
| 302 | * @throws IOException when fails do IO operations |
| 303 | */ |
| 304 | public static List<YangNode> deSerializeDataModel(List<String> serializableInfoSet) throws IOException { |
| 305 | |
| 306 | List<YangNode> nodes = new ArrayList<>(); |
| 307 | for (String fileInfo : serializableInfoSet) { |
| 308 | YangNode node = null; |
| 309 | try { |
| 310 | FileInputStream fileInputStream = new FileInputStream(fileInfo); |
| 311 | ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); |
| 312 | node = (YangNode) objectInputStream.readObject(); |
| 313 | nodes.add(node); |
| 314 | objectInputStream.close(); |
| 315 | fileInputStream.close(); |
| 316 | } catch (IOException | ClassNotFoundException e) { |
| 317 | throw new IOException(fileInfo + " not found."); |
| 318 | } |
| 319 | } |
| 320 | return nodes; |
| 321 | } |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 322 | |
| 323 | /** |
| 324 | * Clones the list of leaves and list of leaf list in the leaves holder. |
| 325 | * |
| 326 | * @param leavesHolder YANG node potentially containing leaves or leaf lists |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 327 | * @param yangUses instance of YANG uses |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 328 | * @throws CloneNotSupportedException clone is not supported |
| 329 | * @throws DataModelException data model error |
| 330 | */ |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 331 | public static void cloneLeaves(YangLeavesHolder leavesHolder, YangUses yangUses) |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 332 | throws CloneNotSupportedException, DataModelException { |
| 333 | List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf(); |
| 334 | if (currentListOfLeaves != null) { |
| 335 | List<YangLeaf> clonedLeavesList = new LinkedList<YangLeaf>(); |
| 336 | for (YangLeaf leaf : currentListOfLeaves) { |
| 337 | YangLeaf clonedLeaf = leaf.clone(); |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 338 | if (yangUses.getCurrentGroupingDepth() == 0) { |
| 339 | YangEntityToResolveInfoImpl resolveInfo = |
| 340 | resolveLeafrefUnderGroupingForLeaf(clonedLeaf, leavesHolder, yangUses); |
| 341 | if (resolveInfo != null) { |
| 342 | yangUses.addEntityToResolve(resolveInfo); |
| 343 | } |
| 344 | } |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 345 | clonedLeaf.setContainedIn(leavesHolder); |
| 346 | clonedLeavesList.add(clonedLeaf); |
| 347 | } |
| 348 | leavesHolder.setListOfLeaf(clonedLeavesList); |
| 349 | } |
| 350 | |
| 351 | List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList(); |
| 352 | if (currentListOfLeafList != null) { |
| 353 | List<YangLeafList> clonedListOfLeafList = new LinkedList<YangLeafList>(); |
| 354 | for (YangLeafList leafList : currentListOfLeafList) { |
| 355 | YangLeafList clonedLeafList = leafList.clone(); |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 356 | if (yangUses.getCurrentGroupingDepth() == 0) { |
| 357 | YangEntityToResolveInfoImpl resolveInfo = |
| 358 | resolveLeafrefUnderGroupingForLeafList(clonedLeafList, leavesHolder); |
| 359 | if (resolveInfo != null) { |
| 360 | yangUses.addEntityToResolve(resolveInfo); |
| 361 | } |
| 362 | } |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 363 | clonedLeafList.setContainedIn(leavesHolder); |
| 364 | clonedListOfLeafList.add(clonedLeafList); |
| 365 | } |
| 366 | leavesHolder.setListOfLeafList(clonedListOfLeafList); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /** |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 371 | * Resolves leafref in leaf, which are under grouping by adding it to the resolution list. |
| 372 | * |
| 373 | * @param clonedLeaf cloned leaf in uses from grouping |
| 374 | * @param leafParentHolder holder of the leaf from uses |
Bharat saraswal | d50c638 | 2016-07-14 21:57:13 +0530 | [diff] [blame] | 375 | * @param yangUses YANG uses |
janani b | 23ccc31 | 2016-07-14 19:35:22 +0530 | [diff] [blame] | 376 | * @return entity of leafref which has to be resolved |
| 377 | * @throws DataModelException data model error |
| 378 | */ |
| 379 | public static YangEntityToResolveInfoImpl resolveLeafrefUnderGroupingForLeaf(YangLeaf clonedLeaf, |
| 380 | YangLeavesHolder leafParentHolder, |
| 381 | YangUses yangUses) throws |
| 382 | DataModelException { |
| 383 | if (clonedLeaf.getDataType().getDataTypeExtendedInfo() instanceof YangLeafRef) { |
| 384 | YangLeafRef leafrefForCloning = (YangLeafRef) clonedLeaf.getDataType().getDataTypeExtendedInfo(); |
| 385 | // Conversion of prefixes in absolute path while cloning them. |
| 386 | convertThePrefixesDuringChange(leafrefForCloning, yangUses); |
| 387 | leafrefForCloning.setParentNodeOfLeafref((YangNode) leafParentHolder); |
| 388 | YangEntityToResolveInfoImpl yangEntityToResolveInfo = new YangEntityToResolveInfoImpl(); |
| 389 | yangEntityToResolveInfo.setEntityToResolve(leafrefForCloning); |
| 390 | yangEntityToResolveInfo.setHolderOfEntityToResolve((YangNode) leafParentHolder); |
| 391 | yangEntityToResolveInfo.setLineNumber(leafrefForCloning.getLineNumber()); |
| 392 | yangEntityToResolveInfo.setCharPosition(leafrefForCloning.getCharPosition()); |
| 393 | return yangEntityToResolveInfo; |
| 394 | } |
| 395 | return null; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Converts the prefixes in all the nodes of the leafref with respect to the uses node. |
| 400 | * |
| 401 | * @param leafrefForCloning leafref that is to be cloned |
| 402 | * @param yangUses instance of YANG uses where cloning is done |
| 403 | * @throws DataModelException data model error |
| 404 | */ |
| 405 | private static void convertThePrefixesDuringChange(YangLeafRef leafrefForCloning, YangUses yangUses) throws |
| 406 | DataModelException { |
| 407 | List<YangAtomicPath> atomicPathList = leafrefForCloning.getAtomicPath(); |
| 408 | if (atomicPathList != null && !atomicPathList.isEmpty()) { |
| 409 | Iterator<YangAtomicPath> atomicPathIterator = atomicPathList.listIterator(); |
| 410 | while (atomicPathIterator.hasNext()) { |
| 411 | YangAtomicPath atomicPath = atomicPathIterator.next(); |
| 412 | Map<String, String> prefixesAndItsImportNameNode = leafrefForCloning.getPrefixAndItsImportedModule(); |
| 413 | if (!prefixesAndItsImportNameNode.isEmpty() || prefixesAndItsImportNameNode != null) { |
| 414 | String prefixInPath = atomicPath.getNodeIdentifier().getPrefix(); |
| 415 | String importedNodeName = prefixesAndItsImportNameNode.get(prefixInPath); |
| 416 | assignCurrentLeafrefWithNewPrefixes(importedNodeName, atomicPath, yangUses); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * Assigns leafref with new prefixes while cloning. |
| 424 | * |
| 425 | * @param importedNodeName imported node name from grouping |
| 426 | * @param atomicPath atomic path in leafref |
| 427 | * @param node instance of YANG uses where cloning is done |
| 428 | * @throws DataModelException data model error |
| 429 | */ |
| 430 | private static void assignCurrentLeafrefWithNewPrefixes(String importedNodeName, YangAtomicPath atomicPath, |
| 431 | YangNode node) throws DataModelException { |
| 432 | while (!(node instanceof YangReferenceResolver)) { |
| 433 | node = node.getParent(); |
| 434 | if (node == null) { |
| 435 | throw new DataModelException("Internal datamodel error: Datamodel tree is not correct"); |
| 436 | } |
| 437 | } |
| 438 | if (node instanceof YangModule) { |
| 439 | List<YangImport> importInUsesList = ((YangModule) node).getImportList(); |
| 440 | if (importInUsesList != null && !importInUsesList.isEmpty()) { |
| 441 | Iterator<YangImport> importInUsesListIterator = importInUsesList.listIterator(); |
| 442 | while (importInUsesListIterator.hasNext()) { |
| 443 | YangImport importInUsesNode = importInUsesListIterator.next(); |
| 444 | if (importInUsesNode.getModuleName().equals(importedNodeName)) { |
| 445 | atomicPath.getNodeIdentifier().setPrefix(importInUsesNode.getPrefixId()); |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Resolves leafref in leaf-list, which are under grouping by adding it to the resolution list. |
| 454 | * |
| 455 | * @param clonedLeafList cloned leaf-list in uses from grouping |
| 456 | * @param leafListParentHolder holder of the leaf-list from uses |
| 457 | * @return entity of leafref which has to be resolved |
| 458 | * @throws DataModelException data model error |
| 459 | */ |
| 460 | public static YangEntityToResolveInfoImpl resolveLeafrefUnderGroupingForLeafList(YangLeafList clonedLeafList, |
| 461 | YangLeavesHolder |
| 462 | leafListParentHolder) |
| 463 | throws DataModelException { |
| 464 | if (clonedLeafList.getDataType().getDataTypeExtendedInfo() instanceof YangLeafRef) { |
| 465 | YangLeafRef leafrefForCloning = (YangLeafRef) clonedLeafList.getDataType().getDataTypeExtendedInfo(); |
| 466 | leafrefForCloning.setParentNodeOfLeafref((YangNode) leafListParentHolder); |
| 467 | YangEntityToResolveInfoImpl yangEntityToResolveInfo = new YangEntityToResolveInfoImpl(); |
| 468 | yangEntityToResolveInfo.setEntityToResolve(leafrefForCloning); |
| 469 | yangEntityToResolveInfo.setHolderOfEntityToResolve((YangNode) leafListParentHolder); |
| 470 | yangEntityToResolveInfo.setLineNumber(leafrefForCloning.getLineNumber()); |
| 471 | yangEntityToResolveInfo.setCharPosition(leafrefForCloning.getCharPosition()); |
| 472 | return yangEntityToResolveInfo; |
| 473 | } |
| 474 | return null; |
| 475 | } |
| 476 | |
| 477 | /** |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 478 | * Clones the union or enum leaves. If there is any cloned leaves whose type is union/enum then the corresponding |
| 479 | * type info needs to be updated to the cloned new type node. |
| 480 | * |
| 481 | * @param leavesHolder cloned leaves holder, for whom the leaves reference needs to be updated |
Bharat saraswal | b551aae | 2016-07-14 15:18:20 +0530 | [diff] [blame] | 482 | * @throws DataModelException when fails to do data model operations |
Vidyashree Rama | 405d2e6 | 2016-07-08 20:45:41 +0530 | [diff] [blame] | 483 | */ |
| 484 | public static void updateClonedLeavesUnionEnumRef(YangLeavesHolder leavesHolder) throws DataModelException { |
| 485 | List<YangLeaf> currentListOfLeaves = leavesHolder.getListOfLeaf(); |
| 486 | if (currentListOfLeaves != null) { |
| 487 | for (YangLeaf leaf : currentListOfLeaves) { |
| 488 | if (leaf.getDataType().getDataType() == YangDataTypes.ENUMERATION |
| 489 | || leaf.getDataType().getDataType() == YangDataTypes.UNION) { |
| 490 | try { |
| 491 | updateClonedTypeRef(leaf.getDataType(), leavesHolder); |
| 492 | } catch (DataModelException e) { |
| 493 | throw e; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | } |
| 499 | |
| 500 | List<YangLeafList> currentListOfLeafList = leavesHolder.getListOfLeafList(); |
| 501 | if (currentListOfLeafList != null) { |
| 502 | for (YangLeafList leafList : currentListOfLeafList) { |
| 503 | if (leafList.getDataType().getDataType() == YangDataTypes.ENUMERATION |
| 504 | || leafList.getDataType().getDataType() == YangDataTypes.UNION) { |
| 505 | try { |
| 506 | updateClonedTypeRef(leafList.getDataType(), leavesHolder); |
| 507 | } catch (DataModelException e) { |
| 508 | throw e; |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Updates the types extended info pointer to point to the cloned type node. |
| 517 | * |
| 518 | * @param dataType data type, whose extended info needs to be pointed to the cloned type |
| 519 | * @param leavesHolder the leaves holder having the cloned type |
| 520 | */ |
| 521 | private static void updateClonedTypeRef(YangType dataType, YangLeavesHolder leavesHolder) |
| 522 | throws DataModelException { |
| 523 | if (!(leavesHolder instanceof YangNode)) { |
| 524 | throw new DataModelException("Data model error: cloned leaves holder is not a node"); |
| 525 | } |
| 526 | YangNode potentialTypeNode = ((YangNode) leavesHolder).getChild(); |
| 527 | while (potentialTypeNode != null) { |
| 528 | String dataTypeName = null; |
| 529 | if (dataType.getDataType() == YangDataTypes.ENUMERATION) { |
| 530 | YangEnumeration enumNode = (YangEnumeration) dataType.getDataTypeExtendedInfo(); |
| 531 | dataTypeName = enumNode.getName(); |
| 532 | } else if (dataType.getDataType() == YangDataTypes.UNION) { |
| 533 | YangUnion unionNode = (YangUnion) dataType.getDataTypeExtendedInfo(); |
| 534 | dataTypeName = unionNode.getName(); |
| 535 | } |
| 536 | if (potentialTypeNode.getName().contentEquals(dataTypeName)) { |
| 537 | dataType.setDataTypeExtendedInfo((Object) potentialTypeNode); |
| 538 | return; |
| 539 | } |
| 540 | potentialTypeNode = potentialTypeNode.getNextSibling(); |
| 541 | } |
| 542 | |
| 543 | throw new DataModelException("Data model error: cloned leaves type is not found"); |
| 544 | } |
Gaurav Agrawal | 8e8770a | 2016-02-27 03:57:50 +0530 | [diff] [blame] | 545 | } |