blob: 3007ffadeb75e7fd90ec9fde431ebb3702ad3162 [file] [log] [blame]
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +05303 *
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
17package org.onosproject.yangutils.datamodel.utils;
18
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053019import java.util.List;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053020import java.util.Set;
Bharat saraswal96dfef02016-06-16 00:29:12 +053021
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053022import org.onosproject.yangutils.datamodel.CollisionDetector;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053023import org.onosproject.yangutils.datamodel.ResolvableType;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053024import org.onosproject.yangutils.datamodel.YangIfFeature;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053025import org.onosproject.yangutils.datamodel.YangLeaf;
26import org.onosproject.yangutils.datamodel.YangLeafList;
janani be18b5342016-07-13 21:06:41 +053027import org.onosproject.yangutils.datamodel.YangLeafRef;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053028import org.onosproject.yangutils.datamodel.YangLeavesHolder;
29import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053030import org.onosproject.yangutils.datamodel.YangReferenceResolver;
31import org.onosproject.yangutils.datamodel.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053032import org.onosproject.yangutils.datamodel.YangRpc;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053033import org.onosproject.yangutils.datamodel.YangType;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053034import org.onosproject.yangutils.datamodel.YangUses;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053036
37/**
Bharat saraswald9822e92016-04-05 15:13:44 +053038 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053039 */
40public final class DataModelUtils {
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053041
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053042 /**
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-Huawei2ee9e7e2016-06-01 14:30:22 +053051 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053052 * @param dataType type of YANG node asking for detecting collision
53 * @param node instance of calling node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053054 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053055 */
56 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
57 throws DataModelException {
janani b4e53f9b2016-04-26 18:49:20 +053058 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
59 detectCollidingForUsesGrouping(identifierName, dataType, node);
60 } else {
61 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053062 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053063 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 saraswal33dfa012016-05-17 19:59:16 +053070 && parsable.getYangConstructType() != YangConstructType.USES_DATA
71 && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) {
janani b4e53f9b2016-04-26 18:49:20 +053072 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
73 }
74 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053075 }
76 }
janani b4e53f9b2016-04-26 18:49:20 +053077 }
78
79 /**
80 * Detects colliding of uses and grouping only with uses and grouping respectively.
81 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053082 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053083 * @param dataType type of YANG node asking for detecting collision
84 * @param node node instance of calling node
janani b4e53f9b2016-04-26 18:49:20 +053085 * @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 Agrawal8e8770a2016-02-27 03:57:50 +053090 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053091 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +053092 Parsable parsable = (Parsable) node;
93 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053094 && parsable.getYangConstructType() == dataType) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053095 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
96 }
97 node = node.getNextSibling();
98 }
99 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530100
101 /**
102 * Detects the colliding identifier name in a given leaf node.
103 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530104 * @param listOfLeaf List of leaves to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530105 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530106 * @throws DataModelException a violation of data model rules
107 */
janani b4e53f9b2016-04-26 18:49:20 +0530108 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530109 throws DataModelException {
110
janani b4e53f9b2016-04-26 18:49:20 +0530111 if (listOfLeaf == null) {
112 return;
113 }
114 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530115 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530116 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530117 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530118 }
119 }
120 }
121
122 /**
123 * Detects the colliding identifier name in a given leaf-list node.
124 *
janani b4e53f9b2016-04-26 18:49:20 +0530125 * @param listOfLeafList list of leaf-lists to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530126 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530127 * @throws DataModelException a violation of data model rules
128 */
janani b4e53f9b2016-04-26 18:49:20 +0530129 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530130 throws DataModelException {
131
janani b4e53f9b2016-04-26 18:49:20 +0530132 if (listOfLeafList == null) {
133 return;
134 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530135 for (YangLeafList leafList : listOfLeafList) {
136 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530137 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530138 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530139 }
140 }
141 }
142
143 /**
144 * Add a resolution information.
145 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530146 * @param resolutionInfo information about the YANG construct which has to be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530147 * @throws DataModelException a violation of data model rules
148 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530149 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
150 throws DataModelException {
151
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530152 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530153 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
154 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530155 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530156 curNode = curNode.getParent();
157 if (curNode == null) {
158 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
159 }
160 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530161 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530162
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530163 if (resolutionInfo.getEntityToResolveInfo()
164 .getEntityToResolve() instanceof YangType) {
165 resolutionNode.addToResolutionList(resolutionInfo,
166 ResolvableType.YANG_DERIVED_DATA_TYPE);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530167 } else if (resolutionInfo.getEntityToResolveInfo()
168 .getEntityToResolve() instanceof YangUses) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530169 resolutionNode.addToResolutionList(resolutionInfo,
170 ResolvableType.YANG_USES);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530171 } else if (resolutionInfo.getEntityToResolveInfo()
172 .getEntityToResolve() instanceof YangIfFeature) {
173 resolutionNode.addToResolutionList(resolutionInfo,
174 ResolvableType.YANG_IF_FEATURE);
janani be18b5342016-07-13 21:06:41 +0530175 } else if (resolutionInfo.getEntityToResolveInfo()
176 .getEntityToResolve() instanceof YangLeafRef) {
177 resolutionNode.addToResolutionList(resolutionInfo,
178 ResolvableType.YANG_LEAFREF);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530179 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530180 }
181
janani b4e53f9b2016-04-26 18:49:20 +0530182 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530183 * Resolve linking for a resolution list.
184 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530185 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530186 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530187 * @throws DataModelException a violation of data model rules
188 */
189 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530190 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530191 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530192
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530193 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530194 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
195 }
196 }
197
198 /**
199 * Links type/uses referring to typedef/uses of inter YANG file.
200 *
201 * @param resolutionList resolution list for which linking to be done
202 * @param dataModelRootNode module/sub-module node
203 * @throws DataModelException a violation of data model rules
204 */
205 public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530206 YangReferenceResolver dataModelRootNode)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530207 throws DataModelException {
208 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530209 * Run through the resolution list, find type/uses referring to inter
210 * file typedef/grouping, ask for linking.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530211 */
212 for (YangResolutionInfo resolutionInfo : resolutionList) {
213 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530214 }
215 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530216
217 /**
218 * Checks if there is any rpc defined in the module or sub-module.
219 *
220 * @param rootNode root node of the data model
221 * @return status of rpc's existence
222 */
223 public static boolean isRpcChildNodePresent(YangNode rootNode) {
224 YangNode childNode = rootNode.getChild();
225 while (childNode != null) {
226 if (childNode instanceof YangRpc) {
227 return true;
228 }
229 childNode = childNode.getNextSibling();
230 }
231 return false;
232 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530233
234 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530235 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530236 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530237 * @param yangNodeSet YANG node set
238 * @param refNodeName name of the node which is referred
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530239 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530240 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530241 public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530242 /*
243 * Run through the YANG files to see which YANG file matches the
244 * referred node name.
245 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530246 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530247 if (yangNode.getName().equals(refNodeName)) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530248 return yangNode;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530249 }
250 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530251 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530252 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530253
254 /**
255 * Returns the contained data model parent node.
256 *
257 * @param currentNode current node which parent contained node is required
258 * @return parent node in which the current node is an attribute
259 */
260 public static YangNode getParentNodeInGenCode(YangNode currentNode) {
261
262 /*
263 * TODO: recursive parent lookup to support choice/augment/uses. TODO:
264 * need to check if this needs to be updated for
265 * choice/case/augment/grouping
266 */
267 return currentNode.getParent();
268 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530269}