blob: a279d40c1ab9457cb77e6ee9ebfa7bb4c62390bc [file] [log] [blame]
Sithara Punnassery9306e6b2017-02-06 15:38:19 -08001/*
2 * Copyright 2017-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;
17
18import java.util.Iterator;
19import java.util.LinkedList;
20import java.util.List;
21
22import static com.google.common.base.MoreObjects.toStringHelper;
23import static com.google.common.base.Preconditions.checkNotNull;
24import static java.util.Objects.hash;
25import static org.onosproject.config.model.ModelConstants.*;
26
27/**
28 * Representation of an entity which identifies a resource in the logical tree
29 * data store. It is a list of node keys to identify the branch point
30 * hierarchy to reach a resource in the instance tree.
31 */
32
33public final class ResourceId {
34
35 //private final Logger log = LoggerFactory.getLogger(getClass());
36 /**
37 * List of node keys.
38 */
39 private List<NodeKey> nodeKeyList;
40
41 /**
42 * Create object from builder.
43 *
44 * @param builder initialized builder
45 */
46 private ResourceId(Builder builder) {
47 nodeKeyList = builder.nodeKeyList;
48 }
49
50 /**
51 * Retrieves a new resource builder.
52 *
53 * @return resource builder
54 */
55 public static Builder builder() {
56 return new Builder();
57 }
58
59 /**
60 * Returns the list of node key used to uniquely identify the branch in the
61 * logical tree starting from root.
62 *
63 * @return node key uniquely identifying the branch
64 */
65 public List<NodeKey> nodeKeys() {
66 return nodeKeyList;
67 }
68
69 /**
70 * Returns resource identifier builder for a given resource identifier.
71 * It contains all the attributes from the resource identifier. It is to
72 * provide mutability of resource identifier using builder pattern.
73 *
74 * @return data node builder
75 * @throws CloneNotSupportedException if clone fails
76 */
77 public Builder copyBuilder() throws CloneNotSupportedException {
78 return new Builder(this);
79 }
80
81 @Override
82 public int hashCode() {
83 return hash(nodeKeyList);
84 }
85
86 @Override
87 public boolean equals(Object obj) {
88 if (obj == null) {
89 return false;
90 }
91 ResourceId that = (ResourceId) obj;
92 List<NodeKey> thatList = that.nodeKeyList;
93 return nodeKeyList.size() == thatList.size() &&
94 nodeKeyList.containsAll(thatList);
95 }
96
97 @Override
98 public String toString() {
99 return toStringHelper(getClass())
100 .add("nodeKeyList", nodeKeyList)
101 .toString();
102 }
103
104 /**
105 * Builder to construct resource identifier.
106 */
107 public static class Builder {
108
109 /**
110 * Application related information, this enables application to use
111 * this builder as there work bench.
112 */
113 protected Object appInfo;
114
115 private List<NodeKey> nodeKeyList;
116 private NodeKey.NodeKeyBuilder curKeyBuilder = null;
117
118 /**
119 * Creates an instance of resource identifier builder.
120 */
121 public Builder() {
122 nodeKeyList = new LinkedList<>();
123 }
124
125 /**
126 * Creates an instance of resource identifier builder. This is used
127 * in scenario when builder is required from a given resource
128 * identifier.
129 *
130 * @param id old resource identifier
131 * @throws CloneNotSupportedException if clone fails
132 */
133 public Builder(ResourceId id) throws CloneNotSupportedException {
134 nodeKeyList = new LinkedList<>();
135 for (NodeKey key : id.nodeKeyList) {
136 nodeKeyList.add(key.clone());
137 }
138 }
139
140 /**
141 * Appends a given resource id to current builder.
142 *
143 * @param id resource identifier to be appended
144 * @return builder
145 * @throws CloneNotSupportedException if clone fails
146 */
147 public Builder append(ResourceId id) throws CloneNotSupportedException {
148 processCurKey();
149 curKeyBuilder = null;
150 Builder ob = id.copyBuilder();
151 nodeKeyList.addAll(ob.nodeKeyList);
152 return this;
153 }
154
155 /**
156 * Validates, build and add current key.
157 */
158 private void processCurKey() {
159 if (curKeyBuilder != null) {
160 if (curKeyBuilder instanceof LeafListKey.LeafListKeyBuilder) {
161 throw new ModelException(LEAF_IS_TERMINAL);
162 }
163 nodeKeyList.add(curKeyBuilder.build());
164 }
165 }
166
167 /**
168 * Adds the descendent node's schema identity.
169 *
170 * @param name name of descendent node
171 * @param nameSpace name space pf descendent node
172 * @return updated builder pointing to the specified schema location
173 */
174 public Builder addBranchPointSchema(String name, String nameSpace) {
175 processCurKey();
176 curKeyBuilder = new NodeKey.NodeKeyBuilder();
177 curKeyBuilder.schemaId(name, nameSpace);
178 return this;
179 }
180
181 /**
182 * Adds a multi instance attribute's node identity.
183 *
184 * @param name name of the leaf list
185 * @param nameSpace name space of leaf list
186 * @param val value of attribute to identify the instance
187 * @return updated builder pointing to the specific attribute
188 * value instance
189 */
190 public Builder addLeafListBranchPoint(String name, String nameSpace,
191 Object val) {
192 LeafListKey.LeafListKeyBuilder leafListKeyBuilder;
193 if (curKeyBuilder instanceof LeafListKey.LeafListKeyBuilder) {
194 throw new ModelException(NON_KEY_LEAF);
195 }
196 leafListKeyBuilder = new LeafListKey.LeafListKeyBuilder()
197 .schemaId(name, nameSpace).value(val);
198
199 curKeyBuilder = leafListKeyBuilder;
200 return this;
201 }
202
203 /**
204 * Adds a multi instance nodes key attribute value to identify
205 * the branch point of instance tree.
206 *
207 * @param name name of the key attribute
208 * @param nameSpace name space of key attribute
209 * @param val value of the key leaf, to match in the list entry
210 * @return updated builder with list branching information
211 */
212 public Builder addKeyLeaf(String name, String nameSpace, Object val) {
213 ListKey.ListKeyBuilder listKeyBuilder;
214 if (!(curKeyBuilder instanceof ListKey.ListKeyBuilder)) {
215 if (curKeyBuilder instanceof LeafListKey.LeafListKeyBuilder) {
216 throw new ModelException(LEAF_IS_TERMINAL);
217 }
218
219 listKeyBuilder = new ListKey.ListKeyBuilder(curKeyBuilder);
220 } else {
221 listKeyBuilder = (ListKey.ListKeyBuilder) curKeyBuilder;
222 }
223
224 listKeyBuilder.addKeyLeaf(name, nameSpace, val);
225 curKeyBuilder = listKeyBuilder;
226 return this;
227 }
228
229 /**
230 * Builds a resource identifier to based on set path information of
231 * the resource.
232 *
233 * @return built resource identifier
234 */
235 public ResourceId build() {
236 checkNotNull(curKeyBuilder, NO_KEY_SET);
237 nodeKeyList.add(curKeyBuilder.build());
238 return new ResourceId(this);
239 }
240
241 /**
242 * Returns application information. This enables application to use
243 * this builder as there work bench.
244 *
245 * @return application information
246 */
247 public Object appInfo() {
248 return appInfo;
249 }
250
251 /**
252 * Sets application information. This enables application to use
253 * this builder as there work bench.
254 *
255 * @param appInfo application related information
256 */
257 public void appInfo(Object appInfo) {
258 appInfo = appInfo;
259 }
260 }
261
262 public String asString() {
263 StringBuilder bldr = new StringBuilder();
264 bldr.append("root.");
265 Iterator<NodeKey> iter = nodeKeyList.iterator();
266 NodeKey key;
267 while (iter.hasNext()) {
268 key = iter.next();
269 //log.info("Iter: key {}", key.toString());
270 bldr.append(key.schemaId().name());
271 bldr.append("#");
272 bldr.append(key.schemaId().namespace());
273 if (iter.hasNext()) {
274 bldr.append(".");
275 }
276 }
277 return bldr.toString();
278 }
279
280 public String lastNm() {
281 int sz = nodeKeyList.size();
282 return nodeKeyList.get(sz - 1).schemaId().name();
283 }
284
285 public String lastNmspc() {
286 int sz = nodeKeyList.size();
287 return nodeKeyList.get(sz - 1).schemaId().namespace();
288 }
289}