blob: 643eda5939e100c99accb759bc4d2d75c0d4243b [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;
Bharat saraswald9822e92016-04-05 15:13:44 +053020
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053021import org.onosproject.yangutils.datamodel.CollisionDetector;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053022import org.onosproject.yangutils.datamodel.HasResolutionInfo;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053023import org.onosproject.yangutils.datamodel.YangImport;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053024import org.onosproject.yangutils.datamodel.YangLeaf;
25import org.onosproject.yangutils.datamodel.YangLeafList;
26import org.onosproject.yangutils.datamodel.YangLeavesHolder;
27import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053028import org.onosproject.yangutils.datamodel.YangResolutionInfo;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053029import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
30import org.onosproject.yangutils.utils.YangConstructType;
31
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053032
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053033/**
Bharat saraswald9822e92016-04-05 15:13:44 +053034 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053035 */
36public final class DataModelUtils {
37
38 /**
39 * Creates a new data model tree utility.
40 */
41 private DataModelUtils() {
42 }
43
44 /**
45 * Detects the colliding identifier name in a given YANG node and its child.
46 *
47 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053048 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053049 * @param dataType type of YANG node asking for detecting collision
50 * @param node instance of calling node
51 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053052 */
53 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
54 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +053055
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053056 if (dataType == YangConstructType.LEAF_DATA) {
57 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
58 if (leavesHolder.getListOfLeaf() != null) {
59 detectCollidingLeaf(leavesHolder, identifierName);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053060 }
61 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053062 if (dataType == YangConstructType.LEAF_LIST_DATA) {
63 if (((YangLeavesHolder) node).getListOfLeafList() != null) {
64 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
65 detectCollidingLeafList(leavesHolder, identifierName);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053066 }
67 }
68 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053069 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053070 if (node instanceof CollisionDetector) {
71 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
72 }
73 node = node.getNextSibling();
74 }
75 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053076
77 /**
78 * Detects the colliding identifier name in a given leaf node.
79 *
80 * @param leavesHolder leaves node against which collision to be checked
81 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +053082 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053083 * @throws DataModelException a violation of data model rules
84 */
Bharat saraswald9822e92016-04-05 15:13:44 +053085 private static void detectCollidingLeaf(YangLeavesHolder leavesHolder, String identifierName)
86 throws DataModelException {
87
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053088 for (YangLeaf leaf : leavesHolder.getListOfLeaf()) {
89 if (leaf.getLeafName().equals(identifierName)) {
90 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
91 + leaf.getLeafName() + "\"");
92 }
93 }
94 }
95
96 /**
97 * Detects the colliding identifier name in a given leaf-list node.
98 *
99 * @param leavesHolder leaves node against which collision to be checked
100 * @param identifierName name for which collision detection is to be
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530101 * checked
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530102 * @throws DataModelException a violation of data model rules
103 */
Bharat saraswald9822e92016-04-05 15:13:44 +0530104 private static void detectCollidingLeafList(YangLeavesHolder leavesHolder, String identifierName)
105 throws DataModelException {
106
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530107 for (YangLeafList leafList : leavesHolder.getListOfLeafList()) {
108 if (leafList.getLeafName().equals(identifierName)) {
109 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
110 "list \"" + leafList.getLeafName() + "\"");
111 }
112 }
113 }
114
115 /**
116 * Add a resolution information.
117 *
118 * @param resolutionInfo information about the YANG construct which has to
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530119 * be resolved
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530120 * @throws DataModelException a violation of data model rules
121 */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530122 public static void addResolutionInfo(YangResolutionInfo resolutionInfo)
123 throws DataModelException {
124
125
Bharat saraswald9822e92016-04-05 15:13:44 +0530126
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530127 /* get the module node to add maintain the list of nested reference */
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530128 YangNode curNode = resolutionInfo.getEntityToResolveInfo()
129 .getHolderOfEntityToResolve();
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530130 while (!(curNode instanceof HasResolutionInfo)) {
131 curNode = curNode.getParent();
132 if (curNode == null) {
133 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
134 }
135 }
136 HasResolutionInfo resolutionNode = (HasResolutionInfo) curNode;
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530137
138 if (!isPrefixValid(resolutionInfo.getEntityToResolveInfo().getEntityPrefix(),
139 resolutionNode)) {
140 throw new DataModelException("The prefix used is not valid");
141 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530142 resolutionNode.addToResolutionList(resolutionInfo);
143 }
144
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530145 private static boolean isPrefixValid(String entityPrefix, HasResolutionInfo resolutionNode) {
146 if (entityPrefix == null) {
147 return true;
148 }
149
150 if (resolutionNode.getPrefix().contentEquals(entityPrefix)) {
151 return true;
152 }
153
154 if (resolutionNode.getImportList() != null) {
155 for (YangImport importedInfo : resolutionNode.getImportList()) {
156 if (importedInfo.getPrefixId().contentEquals(entityPrefix)) {
157 return true;
158 }
159 }
160 }
161
162 if (resolutionNode.getIncludeList() != null) {
163 /**
164 * TODO: check if the prefix matches with the imported data
165
166 for (YangInclude includedInfo : resolutionNode.getIncludeList()) {
167 if (includedInfo.contentEquals(prefix)) {
168 return true;
169 }
170 }*/
171 }
172
173 return false;
174 }
175
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530176 /**
177 * Resolve linking for a resolution list.
178 *
179 * @param resolutionList resolution list for which linking to be done
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530180 * @param dataModelRootNode module/sub-module node
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530181 * @throws DataModelException a violation of data model rules
182 */
183 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530184 HasResolutionInfo dataModelRootNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530185 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530186
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530187 for (YangResolutionInfo resolutionInfo : resolutionList) {
Vinod Kumar Sd4deb062016-04-15 18:08:57 +0530188 resolutionInfo.resolveLinkingForResolutionInfo(dataModelRootNode.getPrefix());
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530189 }
190 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530191}