sonu gupta | 1bb37b8 | 2016-11-11 16:51:18 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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; |
| 20 | import org.onosproject.yangutils.datamodel.YangSchemaNodeIdentifier; |
| 21 | import org.onosproject.yangutils.datamodel.exceptions.DataModelException; |
| 22 | |
| 23 | import java.util.HashSet; |
| 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 | */ |
| 42 | private final Set<String> valueSet = new HashSet<>(); |
| 43 | |
| 44 | /** |
| 45 | * Creates a YANG multi instance leaf node. |
| 46 | * |
| 47 | * @param id node identifier of YDT multi instance node |
| 48 | */ |
| 49 | protected YdtMultiInstanceLeafNode(YangSchemaNodeIdentifier id) { |
| 50 | super(MULTI_INSTANCE_LEAF_VALUE_NODE, id); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Set<String> getValueSet() { |
| 55 | return ImmutableSet.copyOf(valueSet); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void addValue(String value) { |
| 60 | // check the value against corresponding data-type. |
| 61 | try { |
| 62 | getYangSchemaNode().isValueValid(value); |
| 63 | } catch (Exception e) { |
| 64 | errorHandler(e.getLocalizedMessage(), this); |
| 65 | } |
| 66 | addValueToValueSet(value); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Adds value in the current node valueSet, after successful validation of |
| 71 | * the value. |
| 72 | * |
| 73 | * @param value value to be added |
| 74 | */ |
| 75 | private void addValueToValueSet(String value) { |
| 76 | |
| 77 | if (!valueSet.add(value)) { |
| 78 | errorHandler(errorMsg(FMT_DUP_ENTRY, |
| 79 | getYdtNodeIdentifier().getName()), this); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void addValueSet(Set valueSet) { |
| 85 | String value = null; |
| 86 | // Check the value against corresponding data-type. |
| 87 | for (Object aValueSet : valueSet) { |
| 88 | |
| 89 | try { |
| 90 | value = String.valueOf(aValueSet); |
| 91 | getYangSchemaNode().isValueValid(value); |
| 92 | } catch (DataModelException e) { |
| 93 | errorHandler(e.getLocalizedMessage(), this); |
| 94 | } |
| 95 | addValueToValueSet(value); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void addValueWithoutValidation(String value) { |
| 101 | valueSet.add(value); |
| 102 | } |
| 103 | |
| 104 | @Override |
| 105 | public void addValueSetWithoutValidation(Set valueSet) { |
| 106 | this.valueSet.clear(); |
| 107 | this.valueSet.addAll(valueSet); |
| 108 | } |
| 109 | } |