blob: 76e8f36ce2456f5e73c7f0c998a6b99ffd26d982 [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;
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 Agrawald9d6cc82016-03-29 02:17:23 +053027import org.onosproject.yangutils.datamodel.YangResolutionInfo;
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053028import org.onosproject.yangutils.datamodel.exceptions.DataModelException;
29import org.onosproject.yangutils.utils.YangConstructType;
30
31/**
Bharat saraswald9822e92016-04-05 15:13:44 +053032 * Represents utilities for data model tree.
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053033 */
34public final class DataModelUtils {
35
36 /**
37 * Creates a new data model tree utility.
38 */
39 private DataModelUtils() {
40 }
41
42 /**
43 * Detects the colliding identifier name in a given YANG node and its child.
44 *
45 * @param identifierName name for which collision detection is to be
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053046 * checked
47 * @param dataType type of YANG node asking for detecting collision
48 * @param node instance of calling node
49 * @throws DataModelException a violation of data model rules
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053050 */
51 public static void detectCollidingChildUtil(String identifierName, YangConstructType dataType, YangNode node)
52 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +053053
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053054 if (dataType == YangConstructType.LEAF_DATA) {
55 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
56 if (leavesHolder.getListOfLeaf() != null) {
57 detectCollidingLeaf(leavesHolder, identifierName);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053058 }
59 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053060 if (dataType == YangConstructType.LEAF_LIST_DATA) {
61 if (((YangLeavesHolder) node).getListOfLeafList() != null) {
62 YangLeavesHolder leavesHolder = (YangLeavesHolder) node;
63 detectCollidingLeafList(leavesHolder, identifierName);
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053064 }
65 }
66 node = node.getChild();
Vinod Kumar S38046502016-03-23 15:30:27 +053067 while (node != null) {
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +053068 if (node instanceof CollisionDetector) {
69 ((CollisionDetector) node).detectSelfCollision(identifierName, dataType);
70 }
71 node = node.getNextSibling();
72 }
73 }
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053074
75 /**
76 * Detects the colliding identifier name in a given leaf node.
77 *
78 * @param leavesHolder leaves node against which collision to be checked
79 * @param identifierName name for which collision detection is to be
80 * checked
81 * @throws DataModelException a violation of data model rules
82 */
Bharat saraswald9822e92016-04-05 15:13:44 +053083 private static void detectCollidingLeaf(YangLeavesHolder leavesHolder, String identifierName)
84 throws DataModelException {
85
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +053086 for (YangLeaf leaf : leavesHolder.getListOfLeaf()) {
87 if (leaf.getLeafName().equals(identifierName)) {
88 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf \""
89 + leaf.getLeafName() + "\"");
90 }
91 }
92 }
93
94 /**
95 * Detects the colliding identifier name in a given leaf-list node.
96 *
97 * @param leavesHolder leaves node against which collision to be checked
98 * @param identifierName name for which collision detection is to be
99 * checked
100 * @throws DataModelException a violation of data model rules
101 */
Bharat saraswald9822e92016-04-05 15:13:44 +0530102 private static void detectCollidingLeafList(YangLeavesHolder leavesHolder, String identifierName)
103 throws DataModelException {
104
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530105 for (YangLeafList leafList : leavesHolder.getListOfLeafList()) {
106 if (leafList.getLeafName().equals(identifierName)) {
107 throw new DataModelException("YANG file error: Duplicate input identifier detected, same as leaf " +
108 "list \"" + leafList.getLeafName() + "\"");
109 }
110 }
111 }
112
113 /**
114 * Add a resolution information.
115 *
116 * @param resolutionInfo information about the YANG construct which has to
117 * be resolved
118 * @throws DataModelException a violation of data model rules
119 */
120 public static void addResolutionInfo(YangResolutionInfo resolutionInfo) throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530121
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530122 /* get the module node to add maintain the list of nested reference */
123 YangNode curNode = resolutionInfo.getHolderOfEntityToResolve();
124 while (!(curNode instanceof HasResolutionInfo)) {
125 curNode = curNode.getParent();
126 if (curNode == null) {
127 throw new DataModelException("Internal datamodel error: Datamodel tree is not correct");
128 }
129 }
130 HasResolutionInfo resolutionNode = (HasResolutionInfo) curNode;
131 resolutionNode.addToResolutionList(resolutionInfo);
132 }
133
134 /**
135 * Resolve linking for a resolution list.
136 *
137 * @param resolutionList resolution list for which linking to be done
138 * @param resolutionInfoNode module/sub-module node
139 * @throws DataModelException a violation of data model rules
140 */
141 public static void resolveLinkingForResolutionList(List<YangResolutionInfo> resolutionList,
Bharat saraswald9822e92016-04-05 15:13:44 +0530142 HasResolutionInfo resolutionInfoNode)
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530143 throws DataModelException {
Bharat saraswald9822e92016-04-05 15:13:44 +0530144
Gaurav Agrawald9d6cc82016-03-29 02:17:23 +0530145 for (YangResolutionInfo resolutionInfo : resolutionList) {
146 if (resolutionInfo.getPrefix() == null ||
147 resolutionInfo.getPrefix().equals(resolutionInfoNode.getPrefix())) {
148 resolutionInfo.resolveLinkingForResolutionInfo(resolutionInfoNode.getPrefix());
149 }
150 }
151 }
Gaurav Agrawal8e8770a2016-02-27 03:57:50 +0530152}