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.ImmutableSet; |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 20 | import org.onosproject.yangutils.datamodel.YangSchemaNode; |
| 21 | import org.onosproject.yms.app.ydt.exceptions.YdtException; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 22 | |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 23 | import java.util.LinkedHashSet; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | |
| 26 | import static org.onosproject.yms.app.ydt.YdtConstants.errorMsg; |
| 27 | import static org.onosproject.yms.ydt.YdtType.MULTI_INSTANCE_LEAF_VALUE_NODE; |
| 28 | |
| 29 | /** |
| 30 | * Represents YDT multi instance leaf node which can hold multiple values, it |
| 31 | * is atomic element and doesn't have any child. |
| 32 | */ |
| 33 | class YdtMultiInstanceLeafNode extends YdtNode { |
| 34 | |
| 35 | // ydt formatted error string |
| 36 | private static final String FMT_DUP_ENTRY = |
| 37 | "Duplicate entry found under %s leaf-list node."; |
| 38 | |
| 39 | /** |
| 40 | * Set of values. |
| 41 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 42 | private final Set<String> valueSet = new LinkedHashSet<>(); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Creates a YANG multi instance leaf node. |
| 46 | * |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 47 | * @param node schema of YDT multi instance node |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 48 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 49 | YdtMultiInstanceLeafNode(YangSchemaNode node) { |
| 50 | super(MULTI_INSTANCE_LEAF_VALUE_NODE, node); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Set<String> getValueSet() { |
| 55 | return ImmutableSet.copyOf(valueSet); |
| 56 | } |
| 57 | |
| 58 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 59 | public void addValue(String value) throws YdtException { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 60 | // check the value against corresponding data-type. |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 61 | //TODO validation need to be decided |
| 62 | // try { |
| 63 | // getYangSchemaNode().isValueValid(value); |
| 64 | // } catch (Exception e) { |
| 65 | // throw new YdtException(e.getLocalizedMessage()); |
| 66 | // } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 67 | addValueToValueSet(value); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Adds value in the current node valueSet, after successful validation of |
| 72 | * the value. |
| 73 | * |
| 74 | * @param value value to be added |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 75 | * @throws YdtException when the duplicate entry found in leaf-list node |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 76 | */ |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 77 | private void addValueToValueSet(String value) throws YdtException { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 78 | if (!valueSet.add(value)) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 79 | throw new YdtException(errorMsg(FMT_DUP_ENTRY, getName())); |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 84 | public void addValueSet(Set valueSet) throws YdtException { |
| 85 | String value; |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 86 | // Check the value against corresponding data-type. |
| 87 | for (Object aValueSet : valueSet) { |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 88 | value = String.valueOf(aValueSet); |
| 89 | //TODO validation need to be decided |
| 90 | // try { |
| 91 | // value = String.valueOf(aValueSet); |
| 92 | // getYangSchemaNode().isValueValid(value); |
| 93 | // } catch (DataModelException e) { |
| 94 | // throw new YdtException(e.getLocalizedMessage()); |
| 95 | // } |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 96 | addValueToValueSet(value); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | @Override |
sonu gupta | eff184b | 2016-11-24 12:43:49 +0530 | [diff] [blame] | 101 | public void addValueWithoutValidation(String value, boolean isKeyLeaf) { |
sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 102 | valueSet.add(value); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void addValueSetWithoutValidation(Set valueSet) { |
| 107 | this.valueSet.clear(); |
| 108 | this.valueSet.addAll(valueSet); |
| 109 | } |
| 110 | } |