blob: 39db56abd5aa4fc4578b582ad20082921fcb5b38 [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.ImmutableSet;
sonu guptaeff184b2016-11-24 12:43:49 +053020import org.onosproject.yangutils.datamodel.YangSchemaNode;
21import org.onosproject.yms.app.ydt.exceptions.YdtException;
sonu gupta1bb37b82016-11-11 16:51:18 +053022
sonu guptaeff184b2016-11-24 12:43:49 +053023import java.util.LinkedHashSet;
sonu gupta1bb37b82016-11-11 16:51:18 +053024import java.util.Set;
25
26import static org.onosproject.yms.app.ydt.YdtConstants.errorMsg;
27import 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 */
33class 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 guptaeff184b2016-11-24 12:43:49 +053042 private final Set<String> valueSet = new LinkedHashSet<>();
sonu gupta1bb37b82016-11-11 16:51:18 +053043
44 /**
45 * Creates a YANG multi instance leaf node.
46 *
sonu guptaeff184b2016-11-24 12:43:49 +053047 * @param node schema of YDT multi instance node
sonu gupta1bb37b82016-11-11 16:51:18 +053048 */
sonu guptaeff184b2016-11-24 12:43:49 +053049 YdtMultiInstanceLeafNode(YangSchemaNode node) {
50 super(MULTI_INSTANCE_LEAF_VALUE_NODE, node);
sonu gupta1bb37b82016-11-11 16:51:18 +053051 }
52
53 @Override
54 public Set<String> getValueSet() {
55 return ImmutableSet.copyOf(valueSet);
56 }
57
58 @Override
sonu guptaeff184b2016-11-24 12:43:49 +053059 public void addValue(String value) throws YdtException {
sonu gupta1bb37b82016-11-11 16:51:18 +053060 // check the value against corresponding data-type.
sonu guptaeff184b2016-11-24 12:43:49 +053061 //TODO validation need to be decided
62// try {
63// getYangSchemaNode().isValueValid(value);
64// } catch (Exception e) {
65// throw new YdtException(e.getLocalizedMessage());
66// }
sonu gupta1bb37b82016-11-11 16:51:18 +053067 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 guptaeff184b2016-11-24 12:43:49 +053075 * @throws YdtException when the duplicate entry found in leaf-list node
sonu gupta1bb37b82016-11-11 16:51:18 +053076 */
sonu guptaeff184b2016-11-24 12:43:49 +053077 private void addValueToValueSet(String value) throws YdtException {
sonu gupta1bb37b82016-11-11 16:51:18 +053078 if (!valueSet.add(value)) {
sonu guptaeff184b2016-11-24 12:43:49 +053079 throw new YdtException(errorMsg(FMT_DUP_ENTRY, getName()));
sonu gupta1bb37b82016-11-11 16:51:18 +053080 }
81 }
82
83 @Override
sonu guptaeff184b2016-11-24 12:43:49 +053084 public void addValueSet(Set valueSet) throws YdtException {
85 String value;
sonu gupta1bb37b82016-11-11 16:51:18 +053086 // Check the value against corresponding data-type.
87 for (Object aValueSet : valueSet) {
sonu guptaeff184b2016-11-24 12:43:49 +053088 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 gupta1bb37b82016-11-11 16:51:18 +053096 addValueToValueSet(value);
97 }
98 }
99
100 @Override
sonu guptaeff184b2016-11-24 12:43:49 +0530101 public void addValueWithoutValidation(String value, boolean isKeyLeaf) {
sonu gupta1bb37b82016-11-11 16:51:18 +0530102 valueSet.add(value);
103 }
104
105 @Override
106 public void addValueSetWithoutValidation(Set valueSet) {
107 this.valueSet.clear();
108 this.valueSet.addAll(valueSet);
109 }
110}