blob: 0236657e4c5b7fbf30f8fba5fc6450e609b5dbdb [file] [log] [blame]
Sithara Punnassery4b091dc2017-03-02 17:22:40 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Sithara Punnassery4b091dc2017-03-02 17:22:40 -08003 *
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;
17
18import java.util.Iterator;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080019
Sithara Punnasserybc9edb12017-07-20 14:32:33 -070020import java.util.LinkedList;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080021import java.util.List;
22
23import org.onosproject.yang.model.KeyLeaf;
24import org.onosproject.yang.model.LeafListKey;
25import org.onosproject.yang.model.ListKey;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080026import org.onosproject.yang.model.NodeKey;
27import org.onosproject.yang.model.ResourceId;
28
Yuta HIGUCHIde667842017-07-12 17:19:24 -070029// FIXME add non-trivial examples
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080030/**
Sithara Punnassery44e2a702017-03-06 15:38:10 -080031 * Utilities to work on the ResourceId.
Yuta HIGUCHIde667842017-07-12 17:19:24 -070032 * <p>
33 * Examples:
34 * <dl>
35 * <dt>root node</dt>
36 * <dd>root</dd>
37 * </dl>
38 *
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080039 */
Sithara Punnasserybda82502017-03-22 19:08:19 -070040//FIXME add javadocs
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080041public final class ResourceIdParser {
42
Yuta HIGUCHIde667842017-07-12 17:19:24 -070043 /**
44 * root node name.
45 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -080046 public static final String ROOT = "root";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070047
48 /**
49 * Separator between SchemaId components and value part.
50 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -080051 public static final String NM_SEP = "#";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070052 // TODO not used in #parseResId(ResourceId)??
Sithara Punnassery44e2a702017-03-06 15:38:10 -080053 public static final String VAL_SEP = "@";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070054 /**
55 * Separator between ListKey schemaId and keyLeafs.
56 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -080057 public static final String KEY_SEP = "$";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070058 /**
59 * Separator between {@code NodeKey}s (~=tree nodes).
60 */
Sithara Punnasserybda82502017-03-22 19:08:19 -070061 public static final String EL_SEP = "|";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070062
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -070063 public static final String VAL_CHK = "\\@";
64 public static final String KEY_CHK = "\\$";
Sithara Punnasserybda82502017-03-22 19:08:19 -070065 public static final String NM_CHK = "\\#";
Sithara Punnassery0b51d432017-03-28 01:09:28 -070066 public static final String EL_CHK = "\\|";
Sithara Punnassery44e2a702017-03-06 15:38:10 -080067
68
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080069 private ResourceIdParser() {
70
71 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -080072
Sithara Punnassery44e2a702017-03-06 15:38:10 -080073 public static NodeKey getInstanceKey(ResourceId path) {
74 int last = path.nodeKeys().size();
75 NodeKey ret = path.nodeKeys().get(last - 1);
76 if (ret instanceof NodeKey) {
77 return ret;
78 } else {
79 return null;
80 }
81 }
82
83 public static NodeKey getMultiInstanceKey(ResourceId path) {
84 int last = path.nodeKeys().size();
85 NodeKey ret = path.nodeKeys().get(last - 1);
86 if (ret instanceof ListKey) {
87 return ret;
88 } else {
89 return null;
90 }
91 }
92
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -070093 public static String getNamespace(String nmspc) {
94 String ret = null;
95 if (nmspc.contains(ResourceIdParser.KEY_SEP)) {
96 ret = nmspc.split(KEY_CHK)[0];
97 } else if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
98 ret = nmspc.split(VAL_CHK)[0];
99 } else {
100 ret = nmspc;
101 }
102 return ret;
103 }
104
105 public static String getKeyVal(String nmspc) {
106 String ret = null;
107 if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
108 ret = nmspc.split(VAL_CHK)[1];
109 }
110 return ret;
111 }
112
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800113 public static String appendMultiInstKey(String path, String leaf) {
114 return (path + leaf.substring(leaf.indexOf(KEY_SEP)));
115 }
116
117 public static String appendKeyLeaf(String path, String key) {
118 return (path + EL_SEP + key);
119 }
120
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800121 public static String appendKeyLeaf(String path, KeyLeaf key) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800122 StringBuilder bldr = new StringBuilder();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800123 bldr.append(key.leafSchema().name());
124 bldr.append(NM_SEP);
125 bldr.append(key.leafSchema().namespace());
126 bldr.append(NM_SEP);
127 bldr.append(key.leafValue().toString());
128 return (path + EL_SEP + bldr.toString());
129 }
130
131 public static String appendNodeKey(String path, NodeKey key) {
132 return (path + EL_SEP + key.schemaId().name() + NM_SEP + key.schemaId().namespace());
133 }
134
135 public static String appendNodeKey(String path, String name, String nmSpc) {
136 return (path + EL_SEP + name + NM_SEP + nmSpc);
137 }
138
139 public static String appendLeafList(String path, LeafListKey key) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700140 return (path + VAL_SEP + key.asString());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800141 }
142
143 public static String appendLeafList(String path, String val) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700144 return (path + VAL_SEP + val);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800145 }
146
147 public static String appendKeyList(String path, ListKey key) {
148 StringBuilder bldr = new StringBuilder();
149 for (KeyLeaf keyLeaf : key.keyLeafs()) {
150 bldr.append(KEY_SEP);
151 bldr.append(keyLeaf.leafSchema().name());
152 bldr.append(NM_SEP);
153 bldr.append(keyLeaf.leafSchema().namespace());
154 bldr.append(NM_SEP);
155 bldr.append(keyLeaf.leafValue().toString());
156 }
157 return (path + bldr.toString());
158 }
159
160 public static String parseNodeKey(NodeKey key) {
161 if (key == null) {
162 return null;
163 }
164 StringBuilder bldr = new StringBuilder();
165 if (key instanceof LeafListKey) {
166 parseLeafList((LeafListKey) key, bldr);
167 } else if (key instanceof ListKey) {
168 parseKeyList((ListKey) key, bldr);
169 } else {
170 parseNodeKey(key, bldr);
171 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800172 return bldr.toString();
173 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800174
Yuta HIGUCHIde667842017-07-12 17:19:24 -0700175 /**
176 * Gets String representation of ResourceId.
177 * <p>
178 * <pre>
179 * ResourceId := 'root' ('|' element)*
180 * element := LeafListKey | ListKey | NodeKey
181 * SchemaId := [string SchemaId#name] '#' [string SchemaId#namespace]
182 * LeafListKey := SchemaId '#' [string representation of LeafListKey#value]
183 * ListKey := SchemaId (KeyLeaf)*
184 * KeyLeaf := '$' SchemaId '#' [string representation of KeyLeaf#leafValue]
185 * NodeKey := SchemaId
186 * </pre>
187 *
188 * @param path to convert
189 * @return String representation
190 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800191 public static String parseResId(ResourceId path) {
192 StringBuilder bldr = new StringBuilder();
193 bldr.append(ROOT);
194 if (path == null) {
195 return bldr.toString();
196 }
Sithara Punnasserybc9edb12017-07-20 14:32:33 -0700197 List<NodeKey> nodeKeyList = new LinkedList<>();
198 Iterator<NodeKey> itr = path.nodeKeys().iterator();
199 while (itr.hasNext()) {
200 nodeKeyList.add(itr.next());
201 }
202 if (nodeKeyList.get(0).schemaId().name().compareTo("/") == 0) {
203 nodeKeyList.remove(0);
204 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800205 for (NodeKey key : nodeKeyList) {
206 bldr.append(EL_SEP);
207 if (key instanceof LeafListKey) {
208 parseLeafList((LeafListKey) key, bldr);
209 } else if (key instanceof ListKey) {
210 parseKeyList((ListKey) key, bldr);
211 } else {
212 parseNodeKey(key, bldr);
213 }
214 }
215 return bldr.toString();
216 }
217
Sithara Punnassery9d464a32017-07-23 16:14:02 -0700218 public static String[] getService(ResourceId path) {
219 String[] res = null;
220 if (path == null) {
221 return res;
222 }
223 int last = path.nodeKeys().size() - 1;
224 res[0] = path.nodeKeys().get(last - 1).schemaId().name();
225 res[1] = path.nodeKeys().get(last).schemaId().name();
226 return res;
227 }
228
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800229 private static void parseLeafList(LeafListKey key, StringBuilder bldr) {
230 bldr.append(key.schemaId().name());
231 bldr.append(NM_SEP);
232 bldr.append(key.schemaId().namespace());
233 bldr.append(NM_SEP);
234 bldr.append(key.asString());
235 }
236
237 private static void parseKeyList(ListKey key, StringBuilder bldr) {
238 bldr.append(key.schemaId().name());
239 bldr.append(NM_SEP);
240 bldr.append(key.schemaId().namespace());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800241 Iterator<KeyLeaf> iter = key.keyLeafs().iterator();
242 KeyLeaf next;
243 while (iter.hasNext()) {
244 next = iter.next();
245 bldr.append(KEY_SEP);
246 bldr.append(next.leafSchema().name());
247 bldr.append(NM_SEP);
248 bldr.append(next.leafSchema().namespace());
249 bldr.append(NM_SEP);
250 bldr.append(next.leafValue().toString());
251 }
252 }
253
254 private static void parseNodeKey(NodeKey key, StringBuilder bldr) {
255 bldr.append(key.schemaId().name());
256 bldr.append(NM_SEP);
257 bldr.append(key.schemaId().namespace());
258 }
259
260 public static ResourceId getResId(List<String> dpath) {
261 ResourceId.Builder resBldr = new ResourceId.Builder();
262 Iterator<String> itr = dpath.iterator();
263 itr.next();
264 while (itr.hasNext()) {
265 String name = itr.next();
266 if (name.contains(VAL_SEP)) {
267 resBldr.addLeafListBranchPoint(name.substring(0, name.indexOf(NM_SEP)),
268 name.substring(name.indexOf(NM_SEP) + 1, name.indexOf(VAL_SEP)),
269 name.substring(name.indexOf(VAL_SEP) + 1));
270 } else if (name.contains(KEY_SEP)) {
Sithara Punnasserybda82502017-03-22 19:08:19 -0700271 String[] keys = name.split(KEY_CHK);
272 String[] nm = keys[0].split(NM_CHK);
273 resBldr.addBranchPointSchema(nm[0], nm[1]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800274 for (int i = 1; i < keys.length; i++) {
275 String key = keys[i];
Sithara Punnasserybda82502017-03-22 19:08:19 -0700276 String[] el = keys[i].split(NM_CHK);
277 if (el.length != 3) {
278 throw new FailedException("Malformed event subject, cannot parse");
279 }
280 resBldr.addKeyLeaf(el[0], el[1], el[2]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800281 }
282 } else {
283 resBldr.addBranchPointSchema(name.substring(0, name.indexOf(NM_SEP)),
284 name.substring(name.indexOf(NM_SEP) + 1));
285 }
286 }
287 return resBldr.build();
288 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800289}