blob: 2a0dfeec48fc585f9099f336017d262be0828348 [file] [log] [blame]
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08001/*
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 */
16package org.onosproject.config.model;
17import java.util.LinkedHashMap;
18import java.util.Map;
19
20import static org.onosproject.config.model.ModelConstants.LEAF_IS_TERMINAL;
21
22/**
23 * Abstraction of an entity which represents an inner node in data store.
24 */
25public final class InnerNode extends DataNode {
26
27 /**
28 * Map containing info of all child data nodes with respect to their node
29 * keys.
30 */
31 private Map<NodeKey, DataNode> childNodes = new LinkedHashMap<>();
32
33 /**
34 * Returns the children nodes to the current node.
35 * Children nodes are identified based on the node key.
36 *
37 * @return read only linked map of children nodes
38 */
39 public Map<NodeKey, DataNode> childNodes() {
40 return childNodes;
41 }
42
43 /**
44 * Creates an instance of inner node.
45 *
46 * @param builder inner node builder
47 */
48 public InnerNode(Builder builder) {
49 super(builder);
50 childNodes = builder.childNodes;
51 }
52
53 /**
54 * Returns inner node builder instance.
55 *
56 * @param name name of node
57 * @param nameSpace namespace of node
58 * @return inner node builder instance
59 */
60 public static Builder builder(String name, String nameSpace) {
61 return new Builder(name, nameSpace);
62 }
63
64 /**
65 * Returns inner node copy builder.
66 *
67 * @return inner node copy builder
68 */
69 @Override
70 public Builder copyBuilder() {
71 return new Builder(this);
72 }
73
74 /**
75 * Builder with get and set functions to build inner node,
76 * builder will be used both to create inner node from scratch or from a
77 * given inner node.
78 */
79 public static class Builder extends DataNode.Builder<Builder> {
80
81 /**
82 * Map containing info of all child data nodes with respect to their
83 * node keys.
84 */
85 private Map<NodeKey, DataNode> childNodes = new LinkedHashMap<>();
86
87 public Builder() {
88 }
89 /**
90 * Creates an instance of data node builder.
91 *
92 * @param name name of node
93 * @param namespace namespace of node
94 */
95 public Builder(String name, String namespace) {
96 keyBuilder = NodeKey.builder().schemaId(name, namespace);
97 }
98
99 /**
100 * Creates an instance of inner node builder.
101 *
102 * @param node old inner node
103 */
104 public Builder(InnerNode node) {
105 super(node);
106 childNodes = node.childNodes;
107 }
108
109 /**
110 * Adds node to the builder.
111 *
112 * @param node node to be added
113 * @return inner node builder
114 */
115 public Builder addNode(DataNode node) {
116 childNodes.put(node.key(), node);
117 return this;
118 }
119
120 /**
121 * Builds a inner node object.
122 *
123 * @return inner node
124 */
125 @Override
126 public InnerNode build() {
127 if (type == null) {
128 throw new IllegalStateException("node should have a type.");
129 }
130 if (key == null) {
131 key = keyBuilder.build();
132 }
133 return new InnerNode(this);
134 }
135
136 @Override
137 public InnerNode.Builder createChildBuilder(String name, String nameSpace) {
138 return InnerNode.builder(name, nameSpace)
139 .parent(this);
140 }
141
142 @Override
143 public LeafNode.Builder createChildBuilder(String name, String nameSpace,
144 Object value) {
145 return LeafNode.builder(name, nameSpace)
146 .parent(this)
147 .value(value);
148 }
149
150 @Override
151 public InnerNode.Builder deleteChild(NodeKey key) {
152 childNodes.remove(key);
153 return this;
154 }
155
156 @Override
157 public Builder getChildBuilder(NodeKey nodeKey) {
158 DataNode node = childNodes.get(nodeKey);
159 if (node == null) {
160 throw new IllegalArgumentException(
161 "Invalid key: no child nodes found for given key: " +
162 nodeKey);
163 }
164 return (Builder) node.copyBuilder().parent(this);
165 }
166
167 @Override
168 public Builder addKeyLeaf(String name, String nameSpace, Object val) {
169 ListKey.ListKeyBuilder listKeyBuilder;
170 if (!(keyBuilder instanceof ListKey.ListKeyBuilder)) {
171 if (keyBuilder instanceof LeafListKey.LeafListKeyBuilder) {
172 throw new ModelException(LEAF_IS_TERMINAL);
173 }
174
175 listKeyBuilder = new ListKey.ListKeyBuilder(keyBuilder);
176 } else {
177 listKeyBuilder = (ListKey.ListKeyBuilder) keyBuilder;
178 }
179
180 listKeyBuilder.addKeyLeaf(name, nameSpace, val);
181 keyBuilder = listKeyBuilder;
182 return this;
183 }
184
185 @Override
186 public LeafNode.Builder addLeafListValue(Object val) {
187 throw new IllegalStateException("node is not of leaf list type.");
188 }
189 }
190}