blob: 1691d2a306c137f74695e8a0c5ad2ccdd3f8d494 [file] [log] [blame]
sonu gupta1bb37b82016-11-11 16:51:18 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
sonu gupta1bb37b82016-11-11 16:51:18 +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.yms.app.ydt;
18
19import com.google.common.collect.ImmutableList;
20import org.onosproject.yangutils.datamodel.YangList;
sonu guptaeff184b2016-11-24 12:43:49 +053021import org.onosproject.yangutils.datamodel.YangSchemaNode;
sonu gupta1bb37b82016-11-11 16:51:18 +053022import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier;
sonu guptaeff184b2016-11-24 12:43:49 +053023import org.onosproject.yms.app.ydt.exceptions.YdtException;
sonu gupta1bb37b82016-11-11 16:51:18 +053024import org.onosproject.yms.ydt.YdtContext;
25
26import java.util.ArrayList;
27import java.util.Iterator;
28import java.util.List;
sonu guptaeff184b2016-11-24 12:43:49 +053029import java.util.Set;
sonu gupta1bb37b82016-11-11 16:51:18 +053030
31import static org.onosproject.yms.app.ydt.YdtConstants.errorMsg;
32import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_NODE;
33
34
35/**
36 * Represents a multi instance node in YANG data tree.
37 */
38public class YdtMultiInstanceNode extends YdtNode {
39
sonu guptaeff184b2016-11-24 12:43:49 +053040 // YDT formatted error string
sonu gupta1bb37b82016-11-11 16:51:18 +053041 private static final String FMT_MISSING_KEY =
42 "%s is missing some of the keys of %s.";
sonu guptaeff184b2016-11-24 12:43:49 +053043 private static final String FMT_UNI_KEY =
44 "Some of the key elements are not unique in %s.";
45 private static final String FMT_MANY_INS =
46 "Too many instances of %s. Expected maximum instances %d.";
47 private static final String FMT_FEW_INS =
48 "Too few instances of %s. Expected minimum instances %d.";
sonu gupta1bb37b82016-11-11 16:51:18 +053049
50 /*
51 * Reference for list of key element's ydtContext.
52 */
53 private List<YdtContext> keyNodeList = new ArrayList<>();
54
55 /*
56 * Reference for composite key string for multi Instance Node..
57 */
58 private String compositeKey;
59
60 /**
61 * Creates a YANG multi instance node object.
62 *
sonu guptaeff184b2016-11-24 12:43:49 +053063 * @param node schema of YDT multi instance node .
sonu gupta1bb37b82016-11-11 16:51:18 +053064 */
sonu guptaeff184b2016-11-24 12:43:49 +053065 YdtMultiInstanceNode(YangSchemaNode node) {
66 super(MULTI_INSTANCE_NODE, node);
sonu gupta1bb37b82016-11-11 16:51:18 +053067 }
68
69 /**
70 * Returns the composite key string for current multi instance node.
71 *
72 * @return composite key string
73 */
sonu guptaeff184b2016-11-24 12:43:49 +053074 private String getCompositeKey() {
sonu gupta1bb37b82016-11-11 16:51:18 +053075 return compositeKey;
76 }
77
78 /**
79 * Returns the list of key element's ydtContext.
80 *
81 * @return list of key element's ydtContext
82 */
83 public List<YdtContext> getKeyNodeList() {
84 return ImmutableList.copyOf(keyNodeList);
85 }
86
87 @Override
sonu guptaeff184b2016-11-24 12:43:49 +053088 public void createKeyNodeList() throws YdtException {
sonu gupta1bb37b82016-11-11 16:51:18 +053089 YangList yangListHolder = (YangList) getYangSchemaNode();
90 List<String> schemaKeyList = yangListHolder.getKeyList();
91
92 /*
93 * If key element not defined in schema or config is false then
94 * return no need to do create key list.
95 */
96 if (schemaKeyList == null || !yangListHolder.isConfig()) {
97 return;
98 }
99
100 StringBuilder ksb = new StringBuilder();
101
102 // Iterator for schema key name list.
103 Iterator<String> sklItr = schemaKeyList.iterator();
104
105 List<YdtContext> nodeList = new ArrayList<>();
106
107 YangSchemaNodeIdentifier id = new YangSchemaNodeIdentifier();
sonu guptaeff184b2016-11-24 12:43:49 +0530108 id.setNameSpace(new NameSpace(getNamespace()));
sonu gupta1bb37b82016-11-11 16:51:18 +0530109 // This loop should run while schema key list is not finished
110 while (sklItr.hasNext()) {
111 String name = sklItr.next();
112 id.setName(name);
sonu guptaeff184b2016-11-24 12:43:49 +0530113 YdtNode<YdtSingleInstanceLeafNode> collidingChild =
114 (YdtNode<YdtSingleInstanceLeafNode>) ydtNodeMap.get(id);
sonu gupta1bb37b82016-11-11 16:51:18 +0530115
116 if (collidingChild == null) {
sonu guptaeff184b2016-11-24 12:43:49 +0530117 throw new YdtException(
118 errorMsg(FMT_MISSING_KEY, yangListHolder.getParent()
119 .getName(), yangListHolder.getName()));
sonu gupta1bb37b82016-11-11 16:51:18 +0530120 }
121
sonu gupta1bb37b82016-11-11 16:51:18 +0530122 /*
123 * Preparing composite key string by concatenating values of
124 * all the key leaf.
125 */
sonu guptaeff184b2016-11-24 12:43:49 +0530126 ksb.append(collidingChild.getValue());
127 nodeList.add(collidingChild);
sonu gupta1bb37b82016-11-11 16:51:18 +0530128 }
129 //Setting te key object in List.
130 keyNodeList = nodeList;
131 compositeKey = ksb.toString();
132 }
sonu guptaeff184b2016-11-24 12:43:49 +0530133
134 /**
135 * Validates the given list of instances by verifying the allowed
136 * instance count and key element uniqueness.
137 *
138 * @param keyStringSet set to validate the key element uniqueness
139 * @param list list of instance's of same list
140 * @throws YdtException when user requested multi instance node instance's
141 * count doesn't fit into the allowed instance's limit
142 * or doesn't have unique key's
143 */
144 public void validateInstances(Set keyStringSet, List list)
145 throws YdtException {
146
147 // Clearing the set.
148 keyStringSet.clear();
149
150 /*
151 * Storing the number of multiInstance node for number
152 * if instance validation.
153 */
154 int instanceCount = list.size();
155
156 YangList listSchema = (YangList) ((YdtMultiInstanceNode) list.get(0))
157 .getYangSchemaNode();
158 validateInstanceCount(instanceCount, listSchema);
159 if (listSchema.isConfig() && instanceCount > 1) {
160
161 /*
162 * Iterating over values in ydtNodeList of
163 * multiInstanceNode and compare the key string.
164 */
165 for (YdtNode ydtNode : (List<YdtNode<YdtMultiInstanceNode>>) list) {
166 if (!keyStringSet.add(((YdtMultiInstanceNode) ydtNode)
167 .getCompositeKey())) {
168 throw new YdtException(
169 errorMsg(FMT_UNI_KEY, ydtNode.getName()));
170 }
171 }
172 }
173 }
174
175 /**
176 * Validates the instance count for given list entry.
177 *
178 * @param instanceCount actual count
179 * @param list list entry for which instance count need
180 * to be validated
181 * @throws YdtException when user requested multi instance node instance's
182 * count doesn't fit into the allowed instance's limit
183 */
184 private void validateInstanceCount(int instanceCount, YangList list)
185 throws YdtException {
186
187 if (list.getMinElements() != null) {
188 int minElement = list.getMinElements().getMinElement();
189 if (instanceCount < minElement) {
190 throw new YdtException(errorMsg(FMT_FEW_INS, list.getName(),
191 minElement));
192 }
193 }
194
195 if (list.getMaxElements() != null) {
196 int maxElement = list.getMaxElements().getMaxElement();
197 if (instanceCount > maxElement) {
198 throw new YdtException(errorMsg(FMT_MANY_INS, list.getName(),
199 maxElement));
200 }
201 }
202 }
sonu gupta1bb37b82016-11-11 16:51:18 +0530203}