sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 3 | * |
| 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 | |
| 17 | package org.onosproject.yms.app.ydt; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableList; |
| 20 | import org.onosproject.yangutils.datamodel.YangList; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 21 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 22 | import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 23 | import org.onosproject.yms.app.ydt.exceptions.YdtException; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 24 | import org.onosproject.yms.ydt.YdtContext; |
| 25 | |
| 26 | import java.util.ArrayList; |
| 27 | import java.util.Iterator; |
| 28 | import java.util.List; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 29 | import java.util.Set; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 30 | |
| 31 | import static org.onosproject.yms.app.ydt.YdtConstants.errorMsg; |
| 32 | import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_NODE; |
| 33 | |
| 34 | |
| 35 | /** |
| 36 | * Represents a multi instance node in YANG data tree. |
| 37 | */ |
| 38 | public class YdtMultiInstanceNode extends YdtNode { |
| 39 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 40 | // YDT formatted error string |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 41 | private static final String FMT_MISSING_KEY = |
| 42 | "%s is missing some of the keys of %s."; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 43 | 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 gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 49 | |
| 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 63 | * @param node schema of YDT multi instance node . |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 64 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 65 | YdtMultiInstanceNode(YangSchemaNode node) { |
| 66 | super(MULTI_INSTANCE_NODE, node); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns the composite key string for current multi instance node. |
| 71 | * |
| 72 | * @return composite key string |
| 73 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 74 | private String getCompositeKey() { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 75 | 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 88 | public void createKeyNodeList() throws YdtException { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 89 | 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 108 | id.setNameSpace(new NameSpace(getNamespace())); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 109 | // 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 gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 113 | YdtNode<YdtSingleInstanceLeafNode> collidingChild = |
| 114 | (YdtNode<YdtSingleInstanceLeafNode>) ydtNodeMap.get(id); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 115 | |
| 116 | if (collidingChild == null) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 117 | throw new YdtException( |
| 118 | errorMsg(FMT_MISSING_KEY, yangListHolder.getParent() |
| 119 | .getName(), yangListHolder.getName())); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 120 | } |
| 121 | |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 122 | /* |
| 123 | * Preparing composite key string by concatenating values of |
| 124 | * all the key leaf. |
| 125 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 126 | ksb.append(collidingChild.getValue()); |
| 127 | nodeList.add(collidingChild); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 128 | } |
| 129 | //Setting te key object in List. |
| 130 | keyNodeList = nodeList; |
| 131 | compositeKey = ksb.toString(); |
| 132 | } |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 133 | |
| 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 gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 203 | } |