blob: ea1789132e7018889030b4db9c1b80bbed13037e [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;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053021import org.onosproject.yangutils.datamodel.CollisionDetector;
22import org.onosproject.yangutils.datamodel.YangLeaf;
23import org.onosproject.yangutils.datamodel.YangLeafList;
24import org.onosproject.yangutils.datamodel.YangLeavesHolder;
25import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053026import org.onosproject.yangutils.linker.impl.YangReferenceResolver;
27import org.onosproject.yangutils.linker.impl.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028import org.onosproject.yangutils.datamodel.YangRpc;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053029import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
janani b4e53f9b2016-04-26 18:49:20 +053030import org.onosproject.yangutils.parser.Parsable;
Vidyashree Rama1db15562016-05-17 16:16:15 +053031import org.onosproject.yangutils.plugin.manager.YangFileInfo;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053032import org.onosproject.yangutils.utils.YangConstructType;
33
34/**
Bharat saraswald9822e92016-04-05 15:13:44 +053035 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053036 */
37public final class DataModelUtils {
38
39 /**
40 * Creates a new data model tree utility.
41 */
42 private DataModelUtils() {
43 }
44
45 /**
46 * Detects the colliding identifier name in a given YANG node and its child.
47 *
48 * @param identifierName name for which collision detection is to be
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053049 * checked
50 * @param dataType type of YANG node asking for detecting collision
51 * @param node instance of calling node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053052 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053053 */
54 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
55 throws DataModelException {
janani b4e53f9b2016-04-26 18:49:20 +053056 if (dataType == YangConstructType.USES_DATA || dataType == YangConstructType.GROUPING_DATA) {
57 detectCollidingForUsesGrouping(identifierName, dataType, node);
58 } else {
59 if (node instanceof YangLeavesHolder) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053060 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
janani b4e53f9b2016-04-26 18:49:20 +053061 detectCollidingLeaf(leavesHolder.getListOfLeaf(), identifierName);
62 detectCollidingLeafList(leavesHolder.getListOfLeafList(), identifierName);
63 }
64 node = node.getChild();
65 while (node != null) {
66 Parsable parsable = (Parsable) node;
67 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053068 && parsable.getYangConstructType() != YangConstructType.USES_DATA
69 && parsable.getYangConstructType() != YangConstructType.GROUPING_DATA) {
janani b4e53f9b2016-04-26 18:49:20 +053070 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
71 }
72 node = node.getNextSibling();
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053073 }
74 }
janani b4e53f9b2016-04-26 18:49:20 +053075 }
76
77 /**
78 * Detects colliding of uses and grouping only with uses and grouping respectively.
79 *
80 * @param identifierName name for which collision detection is to be
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053081 * checked
82 * @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
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530104 * @param identifierName name for which collision detection is to be
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530105 * 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
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530126 * @param identifierName name for which collision detection is to be
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530127 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530128 * @throws DataModelException a violation of data model rules
129 */
janani b4e53f9b2016-04-26 18:49:20 +0530130 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530131 throws DataModelException {
132
janani b4e53f9b2016-04-26 18:49:20 +0530133 if (listOfLeafList == null) {
134 return;
135 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 for (YangLeafList leafList : listOfLeafList) {
137 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530138 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530139 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530140 }
141 }
142 }
143
144 /**
145 * Add a resolution information.
146 *
147 * @param resolutionInfo information about the YANG construct which has to
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530148 * be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530149 * @throws DataModelException a violation of data model rules
150 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530151 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
152 throws DataModelException {
153
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530154 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530155 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
156 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530157 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530158 curNode = curNode.getParent();
159 if (curNode == null) {
160 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
161 }
162 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530163 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530164
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530165 resolutionNode.addToResolutionList(resolutionInfo);
166 }
167
janani b4e53f9b2016-04-26 18:49:20 +0530168 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530169 * Resolve linking for a resolution list.
170 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530171 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530172 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530173 * @throws DataModelException a violation of data model rules
174 */
175 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530176 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530177 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530178
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530179 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530180 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
181 }
182 }
183
184 /**
185 * Links type/uses referring to typedef/uses of inter YANG file.
186 *
187 * @param resolutionList resolution list for which linking to be done
188 * @param dataModelRootNode module/sub-module node
189 * @throws DataModelException a violation of data model rules
190 */
191 public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
192 YangReferenceResolver dataModelRootNode)
193 throws DataModelException {
194 /*
195 * Run through the resolution list, find type/uses referring to
196 * inter file typedef/grouping, ask for linking.
197 */
198 for (YangResolutionInfo resolutionInfo : resolutionList) {
199 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530200 }
201 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530202
203 /**
204 * Checks if there is any rpc defined in the module or sub-module.
205 *
206 * @param rootNode root node of the data model
207 * @return status of rpc's existence
208 */
209 public static boolean isRpcChildNodePresent(YangNode rootNode) {
210 YangNode childNode = rootNode.getChild();
211 while (childNode != null) {
212 if (childNode instanceof YangRpc) {
213 return true;
214 }
215 childNode = childNode.getNextSibling();
216 }
217 return false;
218 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530219
220 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530221 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530222 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530223 * @param yangFileInfoSet YANG file info set
224 * @param refNodeName name of the node which is referred
225 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530226 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530227 public static YangNode findReferredNode(Set<YangFileInfo> yangFileInfoSet, String refNodeName) {
228 /*
229 * Run through the YANG files to see which YANG file matches the
230 * referred node name.
231 */
232 for (YangFileInfo yangFileInfo : yangFileInfoSet) {
233 YangNode yangNode = yangFileInfo.getRootNode();
234 if (yangNode.getName().equals(refNodeName)) {
235 return yangFileInfo.getRootNode();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530236 }
237 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530238 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530239 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530240}