blob: 97c7327ba02074d81c163771de4d077cce9f03c5 [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;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053022import org.onosproject.yangutils.datamodel.ResolvableType;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053023import org.onosproject.yangutils.datamodel.YangLeaf;
24import org.onosproject.yangutils.datamodel.YangLeafList;
25import org.onosproject.yangutils.datamodel.YangLeavesHolder;
26import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal95b416c2016-06-07 14:00:26 +053027import org.onosproject.yangutils.datamodel.YangReferenceResolver;
28import org.onosproject.yangutils.datamodel.YangResolutionInfo;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053029import org.onosproject.yangutils.datamodel.YangRpc;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053030import org.onosproject.yangutils.datamodel.YangType;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053031import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
janani b4e53f9b2016-04-26 18:49:20 +053032import org.onosproject.yangutils.parser.Parsable;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053033import org.onosproject.yangutils.utils.YangConstructType;
34
35/**
Bharat saraswald9822e92016-04-05 15:13:44 +053036 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053037 */
38public final class DataModelUtils {
39
40 /**
41 * Creates a new data model tree utility.
42 */
43 private DataModelUtils() {
44 }
45
46 /**
47 * Detects the colliding identifier name in a given YANG node and its child.
48 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053049 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053050 * @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 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053080 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053081 * @param dataType type of YANG node asking for detecting collision
82 * @param node node instance of calling node
janani b4e53f9b2016-04-26 18:49:20 +053083 * @throws DataModelException a violation of data model rules
84 */
85 public static void detectCollidingForUsesGrouping(String identifierName, YangConstructType dataType, YangNode node)
86 throws DataModelException {
87
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053088 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053089 while (node != null) {
janani b4e53f9b2016-04-26 18:49:20 +053090 Parsable parsable = (Parsable) node;
91 if (node instanceof CollisionDetector
Bharat saraswal33dfa012016-05-17 19:59:16 +053092 && parsable.getYangConstructType() == dataType) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053093 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
94 }
95 node = node.getNextSibling();
96 }
97 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053098
99 /**
100 * Detects the colliding identifier name in a given leaf node.
101 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530102 * @param listOfLeaf List of leaves to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530103 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530104 * @throws DataModelException a violation of data model rules
105 */
janani b4e53f9b2016-04-26 18:49:20 +0530106 private static void detectCollidingLeaf(List<YangLeaf> listOfLeaf, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530107 throws DataModelException {
108
janani b4e53f9b2016-04-26 18:49:20 +0530109 if (listOfLeaf == null) {
110 return;
111 }
112 for (YangLeaf leaf : listOfLeaf) {
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530113 if (leaf.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530114 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530115 + leaf.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530116 }
117 }
118 }
119
120 /**
121 * Detects the colliding identifier name in a given leaf-list node.
122 *
janani b4e53f9b2016-04-26 18:49:20 +0530123 * @param listOfLeafList list of leaf-lists to detect collision
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530124 * @param identifierName name for which collision detection is to be checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530125 * @throws DataModelException a violation of data model rules
126 */
janani b4e53f9b2016-04-26 18:49:20 +0530127 private static void detectCollidingLeafList(List<YangLeafList> listOfLeafList, String identifierName)
Bharat saraswald9822e92016-04-05 15:13:44 +0530128 throws DataModelException {
129
janani b4e53f9b2016-04-26 18:49:20 +0530130 if (listOfLeafList == null) {
131 return;
132 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530133 for (YangLeafList leafList : listOfLeafList) {
134 if (leafList.getName().equals(identifierName)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530135 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530136 "list \"" + leafList.getName() + "\"");
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530137 }
138 }
139 }
140
141 /**
142 * Add a resolution information.
143 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530144 * @param resolutionInfo information about the YANG construct which has to be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530145 * @throws DataModelException a violation of data model rules
146 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530147 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
148 throws DataModelException {
149
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530150 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530151 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
152 .getHolderOfEntityToResolve();
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530153 while (!(curNode instanceof YangReferenceResolver)) {
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530154 curNode = curNode.getParent();
155 if (curNode == null) {
156 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
157 }
158 }
Vinod Kumar Se4b9b0c2016-04-30 21:09:15 +0530159 YangReferenceResolver resolutionNode = (YangReferenceResolver) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530160
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530161 if (resolutionInfo.getEntityToResolveInfo()
162 .getEntityToResolve() instanceof YangType) {
163 resolutionNode.addToResolutionList(resolutionInfo,
164 ResolvableType.YANG_DERIVED_DATA_TYPE);
165 } else {
166 resolutionNode.addToResolutionList(resolutionInfo,
167 ResolvableType.YANG_USES);
168 }
169
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530170 }
171
janani b4e53f9b2016-04-26 18:49:20 +0530172 /**
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530173 * Resolve linking for a resolution list.
174 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530175 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530176 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530177 * @throws DataModelException a violation of data model rules
178 */
179 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530180 YangReferenceResolver dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530181 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530182
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530183 for (YangResolutionInfo resolutionInfo : resolutionList) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530184 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode);
185 }
186 }
187
188 /**
189 * Links type/uses referring to typedef/uses of inter YANG file.
190 *
191 * @param resolutionList resolution list for which linking to be done
192 * @param dataModelRootNode module/sub-module node
193 * @throws DataModelException a violation of data model rules
194 */
195 public static void linkInterFileReferences(List<YangResolutionInfo> resolutionList,
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530196 YangReferenceResolver dataModelRootNode)
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530197 throws DataModelException {
198 /*
199 * Run through the resolution list, find type/uses referring to
200 * inter file typedef/grouping, ask for linking.
201 */
202 for (YangResolutionInfo resolutionInfo : resolutionList) {
203 resolutionInfo.linkInterFile(dataModelRootNode);
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530204 }
205 }
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530206
207 /**
208 * Checks if there is any rpc defined in the module or sub-module.
209 *
210 * @param rootNode root node of the data model
211 * @return status of rpc's existence
212 */
213 public static boolean isRpcChildNodePresent(YangNode rootNode) {
214 YangNode childNode = rootNode.getChild();
215 while (childNode != null) {
216 if (childNode instanceof YangRpc) {
217 return true;
218 }
219 childNode = childNode.getNextSibling();
220 }
221 return false;
222 }
Vidyashree Rama1db15562016-05-17 16:16:15 +0530223
224 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530225 * Returns referred node in a given set.
Vidyashree Rama1db15562016-05-17 16:16:15 +0530226 *
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530227 * @param yangNodeSet YANG node set
228 * @param refNodeName name of the node which is referred
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530229 * @return referred node's reference
Vidyashree Rama1db15562016-05-17 16:16:15 +0530230 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530231 public static YangNode findReferredNode(Set<YangNode> yangNodeSet, String refNodeName) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530232 /*
233 * Run through the YANG files to see which YANG file matches the
234 * referred node name.
235 */
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530236 for (YangNode yangNode : yangNodeSet) {
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530237 if (yangNode.getName().equals(refNodeName)) {
Gaurav Agrawal95b416c2016-06-07 14:00:26 +0530238 return yangNode;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530239 }
240 }
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530241 return null;
Vidyashree Rama1db15562016-05-17 16:16:15 +0530242 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530243}