blob: 4b8a3a1928ff19e09ba3e4156da553e662c98467 [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;
27import org.onosproject.yangutils.datamodel.YangLeavesHolder;
28import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053029import org.onosproject.yangutils.datamodel.YangReferenceResolver;
30import org.onosproject.yangutils.datamodel.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053031import org.onosproject.yangutils.datamodel.YangRpc;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053032import org.onosproject.yangutils.datamodel.YangType;
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053033import org.onosproject.yangutils.datamodel.YangUses;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053034import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035
36/**
Bharat saraswald9822e92016-04-05 15:13:44 +053037 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053038 */
39public final class DataModelUtils {
Vidyashree Ramadeac28b2016-06-20 15:12:43 +053040
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053041 /**
42 * Creates a new data model tree utility.
43 */
44 private DataModelUtils() {
45 }
46
47 /**
48 * Detects the colliding identifier name in a given YANG node and its child.
49 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053050 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053051 * @param dataType type of YANG node asking for detecting collision
52 * @param node instance of calling node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053053 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053054 */
55 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
56 throws DataModelException {
janani b4e53f9b2016-04-26 18:49:20 +053057 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
58 detectCollidingForUsesGrouping(identifierName, dataType, node);
59 } else {
60 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053061 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053062 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
63 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
64 }
65 node = node.getChild();
66 while (node != null) {
67 Parsable parsable = (Parsable) node;
68 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053069 && parsable.getYangConstructType() != YangConstructType.USES_DATA
70 && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) {
janani b4e53f9b2016-04-26 18:49:20 +053071 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
72 }
73 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053074 }
75 }
janani b4e53f9b2016-04-26 18:49:20 +053076 }
77
78 /**
79 * Detects colliding of uses and grouping only with uses and grouping respectively.
80 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053081 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053082 * @param dataType type of YANG node asking for detecting collision
83 * @param node node instance of calling node
janani b4e53f9b2016-04-26 18:49:20 +053084 * @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 Agrawal8e8770a2016-02-27 03:57:50 +053089 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053090 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +053091 Parsable parsable = (Parsable) node;
92 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053093 && parsable.getYangConstructType() == dataType) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053094 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
95 }
96 node = node.getNextSibling();
97 }
98 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053099
100 /**
101 * Detects the colliding identifier name in a given leaf node.
102 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530103 * @param listOfLeaf List of leaves to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530104 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530105 * @throws DataModelException a violation of data model rules
106 */
janani b4e53f9b2016-04-26 18:49:20 +0530107 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530108 throws DataModelException {
109
janani b4e53f9b2016-04-26 18:49:20 +0530110 if (listOfLeaf == null) {
111 return;
112 }
113 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530114 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530115 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530116 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530117 }
118 }
119 }
120
121 /**
122 * Detects the colliding identifier name in a given leaf-list node.
123 *
janani b4e53f9b2016-04-26 18:49:20 +0530124 * @param listOfLeafList list of leaf-lists to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530125 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530126 * @throws DataModelException a violation of data model rules
127 */
janani b4e53f9b2016-04-26 18:49:20 +0530128 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530129 throws DataModelException {
130
janani b4e53f9b2016-04-26 18:49:20 +0530131 if (listOfLeafList == null) {
132 return;
133 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530134 for (YangLeafList leafList : listOfLeafList) {
135 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530136 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530137 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530138 }
139 }
140 }
141
142 /**
143 * Add a resolution information.
144 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530145 * @param resolutionInfo information about the YANG construct which has to be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530146 * @throws DataModelException a violation of data model rules
147 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530148 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
149 throws DataModelException {
150
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530151 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530152 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
153 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530154 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530155 curNode = curNode.getParent();
156 if (curNode == null) {
157 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
158 }
159 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530160 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530161
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530162 if (resolutionInfo.getEntityToResolveInfo()
163 .getEntityToResolve() instanceof YangType) {
164 resolutionNode.addToResolutionList(resolutionInfo,
165 ResolvableType.YANG_DERIVED_DATA_TYPE);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530166 } else if (resolutionInfo.getEntityToResolveInfo()
167 .getEntityToResolve() instanceof YangUses) {
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530168 resolutionNode.addToResolutionList(resolutionInfo,
169 ResolvableType.YANG_USES);
Vidyashree Ramadeac28b2016-06-20 15:12:43 +0530170 } else if (resolutionInfo.getEntityToResolveInfo()
171 .getEntityToResolve() instanceof YangIfFeature) {
172 resolutionNode.addToResolutionList(resolutionInfo,
173 ResolvableType.YANG_IF_FEATURE);
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530174 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530175 }
176
janani b4e53f9b2016-04-26 18:49:20 +0530177 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530178 * Resolve linking for a resolution list.
179 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530180 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530181 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530182 * @throws DataModelException a violation of data model rules
183 */
184 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530185 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530186 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530187
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530188 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530189 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
190 }
191 }
192
193 /**
194 * Links type/uses referring to typedef/uses of inter YANG file.
195 *
196 * @param resolutionList resolution list for which linking to be done
197 * @param dataModelRootNode module/sub-module node
198 * @throws DataModelException a violation of data model rules
199 */
200 public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
Bharat saraswal96dfef02016-06-16 00:29:12 +0530201 YangReferenceResolver dataModelRootNode)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530202 throws DataModelException {
203 /*
Bharat saraswal96dfef02016-06-16 00:29:12 +0530204 * Run through the resolution list, find type/uses referring to inter
205 * file typedef/grouping, ask for linking.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530206 */
207 for (YangResolutionInfo resolutionInfo : resolutionList) {
208 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530209 }
210 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530211
212 /**
213 * Checks if there is any rpc defined in the module or sub-module.
214 *
215 * @param rootNode root node of the data model
216 * @return status of rpc's existence
217 */
218 public static boolean isRpcChildNodePresent(YangNode rootNode) {
219 YangNode childNode = rootNode.getChild();
220 while (childNode != null) {
221 if (childNode instanceof YangRpc) {
222 return true;
223 }
224 childNode = childNode.getNextSibling();
225 }
226 return false;
227 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530228
229 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530230 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530231 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530232 * @param yangNodeSet YANG node set
233 * @param refNodeName name of the node which is referred
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530234 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530235 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530236 public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530237 /*
238 * Run through the YANG files to see which YANG file matches the
239 * referred node name.
240 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530241 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530242 if (yangNode.getName().equals(refNodeName)) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530243 return yangNode;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530244 }
245 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530246 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530247 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530248
249 /**
250 * Returns the contained data model parent node.
251 *
252 * @param currentNode current node which parent contained node is required
253 * @return parent node in which the current node is an attribute
254 */
255 public static YangNode getParentNodeInGenCode(YangNode currentNode) {
256
257 /*
258 * TODO: recursive parent lookup to support choice/augment/uses. TODO:
259 * need to check if this needs to be updated for
260 * choice/case/augment/grouping
261 */
262 return currentNode.getParent();
263 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530264}