Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [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 | package org.onosproject.config.model; |
| 17 | |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 18 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame^] | 19 | * Abstraction of an entity which represents data tree node. Information |
| 20 | * exchange between YANG runtime, protocol and store will be based on this |
| 21 | * node, agnostic of schema. |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 22 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame^] | 23 | public abstract class DataNode { |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 24 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame^] | 25 | /* |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 26 | * Represents type of node in data store. |
| 27 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame^] | 28 | public enum Type { |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Single instance node. |
| 32 | */ |
| 33 | SINGLE_INSTANCE_NODE, |
| 34 | |
| 35 | /** |
| 36 | * Multi instance node. |
| 37 | */ |
| 38 | MULTI_INSTANCE_NODE, |
| 39 | |
| 40 | /** |
| 41 | * Single instance leaf node. |
| 42 | */ |
| 43 | SINGLE_INSTANCE_LEAF_VALUE_NODE, |
| 44 | |
| 45 | /** |
| 46 | * Multi instance leaf node. |
| 47 | */ |
| 48 | MULTI_INSTANCE_LEAF_VALUE_NODE |
| 49 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame^] | 50 | |
| 51 | /** |
| 52 | * Type of node in data store. |
| 53 | */ |
| 54 | protected Type type; |
| 55 | |
| 56 | /** |
| 57 | * Identifies a node uniquely among its siblings. |
| 58 | */ |
| 59 | protected NodeKey key; |
| 60 | |
| 61 | /** |
| 62 | * Returns the type of node. |
| 63 | * |
| 64 | * @return node type |
| 65 | */ |
| 66 | public Type type() { |
| 67 | return type; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Returns the key to identify a branching node. |
| 72 | * |
| 73 | * @return key to identify a branching node |
| 74 | */ |
| 75 | public NodeKey key() { |
| 76 | return key; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Creates an instance of data node. |
| 81 | * |
| 82 | * @param builder data node builder |
| 83 | */ |
| 84 | protected DataNode(Builder builder) { |
| 85 | type = builder.type; |
| 86 | key = builder.key; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Returns data node builder for a given data node. |
| 91 | * It contains all the attributes from the data node. It is to provide |
| 92 | * mutability of data node using builder pattern. |
| 93 | * |
| 94 | * @return data node builder |
| 95 | */ |
| 96 | public abstract Builder copyBuilder(); |
| 97 | |
| 98 | /** |
| 99 | * Represents the implementation of data node builder class. |
| 100 | * |
| 101 | * @param <B> type of data node builder |
| 102 | */ |
| 103 | public abstract static class Builder<B extends Builder<B>> { |
| 104 | |
| 105 | /** |
| 106 | * Type of node in data store. |
| 107 | */ |
| 108 | protected Type type; |
| 109 | |
| 110 | /** |
| 111 | * Identifies a node uniquely among its siblings. |
| 112 | */ |
| 113 | protected NodeKey key; |
| 114 | |
| 115 | /** |
| 116 | * Node key builder. |
| 117 | */ |
| 118 | protected NodeKey.NodeKeyBuilder keyBuilder; |
| 119 | |
| 120 | /** |
| 121 | * Parent data node. |
| 122 | */ |
| 123 | protected InnerNode.Builder parent; |
| 124 | |
| 125 | /** |
| 126 | * Creates an instance of data node builder. |
| 127 | */ |
| 128 | protected Builder() { |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Creates an instance of data node builder using old data node. |
| 133 | * |
| 134 | * @param node data node which |
| 135 | */ |
| 136 | protected Builder(DataNode node) { |
| 137 | type = node.type; |
| 138 | key = node.key; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Sets node key in builder object. |
| 143 | * when serializers have an instance of key present with them they can |
| 144 | * directly set the key value using this method. |
| 145 | * |
| 146 | * @param key node key identifier |
| 147 | * @return data node builder object |
| 148 | */ |
| 149 | public B key(NodeKey key) { |
| 150 | this.key = key; |
| 151 | return (B) this; |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Sets parent node's builder. |
| 156 | * |
| 157 | * @param node parent node builder |
| 158 | * @return builder object |
| 159 | */ |
| 160 | protected B parent(InnerNode.Builder node) { |
| 161 | parent = node; |
| 162 | return (B) this; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Sets node type in builder object. |
| 167 | * |
| 168 | * @param type node type |
| 169 | * @return data node builder |
| 170 | */ |
| 171 | public B type(Type type) { |
| 172 | this.type = type; |
| 173 | return (B) this; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Creates a child builder of type inner node and set a back reference |
| 178 | * of parent node. it is used while creating a data tree. |
| 179 | * |
| 180 | * @param name name of inner node |
| 181 | * @param nameSpace namespace of inner node |
| 182 | * @return child node builder |
| 183 | */ |
| 184 | public abstract InnerNode.Builder createChildBuilder( |
| 185 | String name, String nameSpace); |
| 186 | |
| 187 | /** |
| 188 | * Creates a child build of type leaf node and set a back reference |
| 189 | * of parent node. it is used while creating a data tree. the value |
| 190 | * of leaf is set while creation. |
| 191 | * |
| 192 | * @param name name of leaf node |
| 193 | * @param nameSpace namespace of leaf node |
| 194 | * @param value value for leaf node |
| 195 | * @return child node builder |
| 196 | */ |
| 197 | public abstract LeafNode.Builder createChildBuilder( |
| 198 | String name, String nameSpace, Object value); |
| 199 | |
| 200 | /** |
| 201 | * Deletes child node for a given node key from parent node. |
| 202 | * <p> |
| 203 | * for deleting a node from data tree , caller should parse resource |
| 204 | * identifier to reach to the child node. while parsing the resource |
| 205 | * identifier caller need to create a new data node using copy |
| 206 | * builder. this copy builder can be used further to create child |
| 207 | * nodes copy builders. |
| 208 | * |
| 209 | * @param key node key for child node |
| 210 | * @return data node builder |
| 211 | */ |
| 212 | public abstract InnerNode.Builder deleteChild(NodeKey key); |
| 213 | |
| 214 | /** |
| 215 | * Returns a child node builder for a given node key. it contains all |
| 216 | * the attribute of child node. it is used to modify the data tree |
| 217 | * while delete or update operations. |
| 218 | * <p> |
| 219 | * this method provides copy builder of child node when a |
| 220 | * update/delete request comes. it sets a back reference of parent |
| 221 | * node as well in child node's copy builder. |
| 222 | * |
| 223 | * @param key data node key |
| 224 | * @return child node |
| 225 | */ |
| 226 | public abstract InnerNode.Builder getChildBuilder(NodeKey key); |
| 227 | |
| 228 | |
| 229 | /** |
| 230 | * Add key leaf for list node key. It can be used while handling a |
| 231 | * list node when in your yang file you have multiple key leaves. |
| 232 | * <p> |
| 233 | * this method is used for adding multiple key leaves in you list |
| 234 | * node. these keys will be added to key builder which is built while |
| 235 | * while node building. To use this method caller should know about |
| 236 | * schema of list and key leaves. |
| 237 | * |
| 238 | * @param name name of leaf node |
| 239 | * @param nameSpace namespace of leaf node |
| 240 | * @param val value of leaf |
| 241 | * @return data node builder |
| 242 | */ |
| 243 | public abstract InnerNode.Builder addKeyLeaf(String name, String nameSpace, |
| 244 | Object val); |
| 245 | |
| 246 | /** |
| 247 | * Add key value to leaf list key. this can be used while handling a |
| 248 | * leaf list where you need to add multiple values. |
| 249 | * |
| 250 | * @param val value |
| 251 | * @return data node builder |
| 252 | */ |
| 253 | public abstract LeafNode.Builder addLeafListValue(Object val); |
| 254 | |
| 255 | /** |
| 256 | * Builds data node. |
| 257 | * |
| 258 | * @return data node |
| 259 | */ |
| 260 | public abstract DataNode build(); |
| 261 | |
| 262 | /** |
| 263 | * Returns parent node builder after building and adding the child |
| 264 | * node to parent's child node map. |
| 265 | * <p> |
| 266 | * This method is used when caller has reached to the depth of the |
| 267 | * subtree and then he wants to go back to its parent node so he |
| 268 | * should build the node and then it should add it to parent node's |
| 269 | * map. this method provides both the functionalities of build and |
| 270 | * add to parent . Also it returns back the parent pointer so caller |
| 271 | * can do further operations on data tree. |
| 272 | * |
| 273 | * @return parent's builder object |
| 274 | */ |
| 275 | public InnerNode.Builder exitNode() { |
| 276 | if (parent != null) { |
| 277 | parent.addNode(build()); |
| 278 | } |
| 279 | return parent; |
| 280 | } |
| 281 | } |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 282 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame^] | 283 | |