blob: 18715e28c59370fbe04b5b60bce174a9f47d6a3c [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
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070018import java.util.Arrays;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080019import java.util.Iterator;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080020
Sithara Punnasserybc9edb12017-07-20 14:32:33 -070021import java.util.LinkedList;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080022import java.util.List;
23
24import org.onosproject.yang.model.KeyLeaf;
25import org.onosproject.yang.model.LeafListKey;
26import org.onosproject.yang.model.ListKey;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080027import org.onosproject.yang.model.NodeKey;
28import org.onosproject.yang.model.ResourceId;
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070029import org.slf4j.Logger;
30import org.slf4j.LoggerFactory;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080031
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070032import com.google.common.annotations.Beta;
33
Yuta HIGUCHIde667842017-07-12 17:19:24 -070034// FIXME add non-trivial examples
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080035/**
Sithara Punnassery44e2a702017-03-06 15:38:10 -080036 * Utilities to work on the ResourceId.
Yuta HIGUCHIde667842017-07-12 17:19:24 -070037 * <p>
38 * Examples:
39 * <dl>
40 * <dt>root node</dt>
41 * <dd>root</dd>
42 * </dl>
43 *
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080044 */
Sithara Punnasserybda82502017-03-22 19:08:19 -070045//FIXME add javadocs
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -070046@Beta
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080047public final class ResourceIdParser {
48
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -070049 private static final Logger log = LoggerFactory.getLogger(ResourceIdParser.class);
50
Yuta HIGUCHIde667842017-07-12 17:19:24 -070051 /**
52 * root node name.
53 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -080054 public static final String ROOT = "root";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070055
56 /**
57 * Separator between SchemaId components and value part.
58 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -080059 public static final String NM_SEP = "#";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070060 // TODO not used in #parseResId(ResourceId)??
Sithara Punnassery44e2a702017-03-06 15:38:10 -080061 public static final String VAL_SEP = "@";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070062 /**
63 * Separator between ListKey schemaId and keyLeafs.
64 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -080065 public static final String KEY_SEP = "$";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070066 /**
67 * Separator between {@code NodeKey}s (~=tree nodes).
68 */
Sithara Punnasserybda82502017-03-22 19:08:19 -070069 public static final String EL_SEP = "|";
Yuta HIGUCHIde667842017-07-12 17:19:24 -070070
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -070071 public static final String VAL_CHK = "\\@";
72 public static final String KEY_CHK = "\\$";
Sithara Punnasserybda82502017-03-22 19:08:19 -070073 public static final String NM_CHK = "\\#";
Sithara Punnassery0b51d432017-03-28 01:09:28 -070074 public static final String EL_CHK = "\\|";
Sithara Punnassery44e2a702017-03-06 15:38:10 -080075
76
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080077 private ResourceIdParser() {
78
79 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -080080
Sithara Punnassery44e2a702017-03-06 15:38:10 -080081 public static NodeKey getInstanceKey(ResourceId path) {
82 int last = path.nodeKeys().size();
Ray Milkeyef310052018-02-06 08:58:51 -080083 return path.nodeKeys().get(last - 1);
Sithara Punnassery44e2a702017-03-06 15:38:10 -080084 }
85
86 public static NodeKey getMultiInstanceKey(ResourceId path) {
87 int last = path.nodeKeys().size();
88 NodeKey ret = path.nodeKeys().get(last - 1);
89 if (ret instanceof ListKey) {
90 return ret;
91 } else {
92 return null;
93 }
94 }
95
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -070096 public static String getNamespace(String nmspc) {
97 String ret = null;
98 if (nmspc.contains(ResourceIdParser.KEY_SEP)) {
99 ret = nmspc.split(KEY_CHK)[0];
100 } else if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
101 ret = nmspc.split(VAL_CHK)[0];
102 } else {
103 ret = nmspc;
104 }
105 return ret;
106 }
107
108 public static String getKeyVal(String nmspc) {
109 String ret = null;
110 if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
111 ret = nmspc.split(VAL_CHK)[1];
112 }
113 return ret;
114 }
115
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800116 public static String appendMultiInstKey(String path, String leaf) {
117 return (path + leaf.substring(leaf.indexOf(KEY_SEP)));
118 }
119
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800120 public static String appendNodeKey(String path, NodeKey key) {
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700121 // FIXME this is not handling root path exception
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800122 return (path + EL_SEP + key.schemaId().name() + NM_SEP + key.schemaId().namespace());
123 }
124
125 public static String appendNodeKey(String path, String name, String nmSpc) {
126 return (path + EL_SEP + name + NM_SEP + nmSpc);
127 }
128
129 public static String appendLeafList(String path, LeafListKey key) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700130 return (path + VAL_SEP + key.asString());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800131 }
132
133 public static String appendLeafList(String path, String val) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700134 return (path + VAL_SEP + val);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800135 }
136
137 public static String appendKeyList(String path, ListKey key) {
138 StringBuilder bldr = new StringBuilder();
139 for (KeyLeaf keyLeaf : key.keyLeafs()) {
140 bldr.append(KEY_SEP);
141 bldr.append(keyLeaf.leafSchema().name());
142 bldr.append(NM_SEP);
143 bldr.append(keyLeaf.leafSchema().namespace());
144 bldr.append(NM_SEP);
145 bldr.append(keyLeaf.leafValue().toString());
146 }
147 return (path + bldr.toString());
148 }
149
Yuta HIGUCHIde667842017-07-12 17:19:24 -0700150 /**
151 * Gets String representation of ResourceId.
Yuta HIGUCHIab350802018-05-07 14:56:32 -0700152 *
Yuta HIGUCHIde667842017-07-12 17:19:24 -0700153 * <pre>
154 * ResourceId := 'root' ('|' element)*
155 * element := LeafListKey | ListKey | NodeKey
156 * SchemaId := [string SchemaId#name] '#' [string SchemaId#namespace]
157 * LeafListKey := SchemaId '#' [string representation of LeafListKey#value]
158 * ListKey := SchemaId (KeyLeaf)*
159 * KeyLeaf := '$' SchemaId '#' [string representation of KeyLeaf#leafValue]
160 * NodeKey := SchemaId
161 * </pre>
162 *
163 * @param path to convert
164 * @return String representation
165 */
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800166 public static String parseResId(ResourceId path) {
167 StringBuilder bldr = new StringBuilder();
168 bldr.append(ROOT);
169 if (path == null) {
170 return bldr.toString();
171 }
Sithara Punnasserybc9edb12017-07-20 14:32:33 -0700172 List<NodeKey> nodeKeyList = new LinkedList<>();
173 Iterator<NodeKey> itr = path.nodeKeys().iterator();
174 while (itr.hasNext()) {
175 nodeKeyList.add(itr.next());
176 }
Yuta HIGUCHI3b5d64e2017-09-12 22:44:22 -0700177 // exception for dealing with root
178 if (nodeKeyList.get(0).schemaId().name().equals("/")) {
Sithara Punnasserybc9edb12017-07-20 14:32:33 -0700179 nodeKeyList.remove(0);
180 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800181 for (NodeKey key : nodeKeyList) {
182 bldr.append(EL_SEP);
183 if (key instanceof LeafListKey) {
184 parseLeafList((LeafListKey) key, bldr);
185 } else if (key instanceof ListKey) {
186 parseKeyList((ListKey) key, bldr);
187 } else {
188 parseNodeKey(key, bldr);
189 }
190 }
191 return bldr.toString();
192 }
193
194 private static void parseLeafList(LeafListKey key, StringBuilder bldr) {
195 bldr.append(key.schemaId().name());
196 bldr.append(NM_SEP);
197 bldr.append(key.schemaId().namespace());
198 bldr.append(NM_SEP);
199 bldr.append(key.asString());
200 }
201
202 private static void parseKeyList(ListKey key, StringBuilder bldr) {
203 bldr.append(key.schemaId().name());
204 bldr.append(NM_SEP);
205 bldr.append(key.schemaId().namespace());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800206 Iterator<KeyLeaf> iter = key.keyLeafs().iterator();
207 KeyLeaf next;
208 while (iter.hasNext()) {
209 next = iter.next();
210 bldr.append(KEY_SEP);
211 bldr.append(next.leafSchema().name());
212 bldr.append(NM_SEP);
213 bldr.append(next.leafSchema().namespace());
214 bldr.append(NM_SEP);
215 bldr.append(next.leafValue().toString());
216 }
217 }
218
219 private static void parseNodeKey(NodeKey key, StringBuilder bldr) {
220 bldr.append(key.schemaId().name());
221 bldr.append(NM_SEP);
222 bldr.append(key.schemaId().namespace());
223 }
224
225 public static ResourceId getResId(List<String> dpath) {
226 ResourceId.Builder resBldr = new ResourceId.Builder();
227 Iterator<String> itr = dpath.iterator();
228 itr.next();
229 while (itr.hasNext()) {
230 String name = itr.next();
231 if (name.contains(VAL_SEP)) {
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -0700232 // dead branch? VAL_SEP never used in parseResId
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800233 resBldr.addLeafListBranchPoint(name.substring(0, name.indexOf(NM_SEP)),
234 name.substring(name.indexOf(NM_SEP) + 1, name.indexOf(VAL_SEP)),
235 name.substring(name.indexOf(VAL_SEP) + 1));
236 } else if (name.contains(KEY_SEP)) {
Sithara Punnasserybda82502017-03-22 19:08:19 -0700237 String[] keys = name.split(KEY_CHK);
238 String[] nm = keys[0].split(NM_CHK);
239 resBldr.addBranchPointSchema(nm[0], nm[1]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800240 for (int i = 1; i < keys.length; i++) {
241 String key = keys[i];
Sithara Punnasserybda82502017-03-22 19:08:19 -0700242 String[] el = keys[i].split(NM_CHK);
243 if (el.length != 3) {
Yuta HIGUCHIdbc08c02018-02-12 16:40:05 -0800244 throw new FailedException("Malformed event subject, cannot parse " +
245 key + " in " + dpath);
Sithara Punnasserybda82502017-03-22 19:08:19 -0700246 }
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -0700247 try {
Sithara Punnasserybda82502017-03-22 19:08:19 -0700248 resBldr.addKeyLeaf(el[0], el[1], el[2]);
Yuta HIGUCHIe057dee2017-09-15 13:56:10 -0700249 } catch (Exception e) {
250 log.error("dpath={}", dpath);
251 log.error("name={}", name);
252 log.error("key={}", key);
253 log.error("el={}", Arrays.asList(el));
254 throw e;
255 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800256 }
257 } else {
258 resBldr.addBranchPointSchema(name.substring(0, name.indexOf(NM_SEP)),
259 name.substring(name.indexOf(NM_SEP) + 1));
260 }
261 }
262 return resBldr.build();
263 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800264}