blob: 802e6821b7b5a0984c6b52cc987387aaa6a9536b [file] [log] [blame]
Sithara Punnassery4b091dc2017-03-02 17:22:40 -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;
17
18import java.util.Iterator;
Sithara Punnassery44e2a702017-03-06 15:38:10 -080019
20import java.util.List;
21
22import org.onosproject.yang.model.KeyLeaf;
23import org.onosproject.yang.model.LeafListKey;
24import org.onosproject.yang.model.ListKey;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080025import org.onosproject.yang.model.NodeKey;
26import org.onosproject.yang.model.ResourceId;
27
28/**
Sithara Punnassery44e2a702017-03-06 15:38:10 -080029 * Utilities to work on the ResourceId.
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080030 */
Sithara Punnasserybda82502017-03-22 19:08:19 -070031//FIXME add javadocs
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080032public final class ResourceIdParser {
33
Sithara Punnassery44e2a702017-03-06 15:38:10 -080034 public static final String ROOT = "root";
35 public static final String NM_SEP = "#";
36 public static final String VAL_SEP = "@";
37 public static final String KEY_SEP = "$";
Sithara Punnasserybda82502017-03-22 19:08:19 -070038 public static final String EL_SEP = "|";
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -070039 public static final String VAL_CHK = "\\@";
40 public static final String KEY_CHK = "\\$";
Sithara Punnasserybda82502017-03-22 19:08:19 -070041 public static final String NM_CHK = "\\#";
Sithara Punnassery0b51d432017-03-28 01:09:28 -070042 public static final String EL_CHK = "\\|";
Sithara Punnassery44e2a702017-03-06 15:38:10 -080043
44
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080045 private ResourceIdParser() {
46
47 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -080048
49 public static ResourceId getParent(ResourceId path) {
50 int last = path.nodeKeys().size();
51 path.nodeKeys().remove(last - 1);
52 return path;
53 }
54
55 public static NodeKey getInstanceKey(ResourceId path) {
56 int last = path.nodeKeys().size();
57 NodeKey ret = path.nodeKeys().get(last - 1);
58 if (ret instanceof NodeKey) {
59 return ret;
60 } else {
61 return null;
62 }
63 }
64
65 public static NodeKey getMultiInstanceKey(ResourceId path) {
66 int last = path.nodeKeys().size();
67 NodeKey ret = path.nodeKeys().get(last - 1);
68 if (ret instanceof ListKey) {
69 return ret;
70 } else {
71 return null;
72 }
73 }
74
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -070075 public static String getNamespace(String nmspc) {
76 String ret = null;
77 if (nmspc.contains(ResourceIdParser.KEY_SEP)) {
78 ret = nmspc.split(KEY_CHK)[0];
79 } else if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
80 ret = nmspc.split(VAL_CHK)[0];
81 } else {
82 ret = nmspc;
83 }
84 return ret;
85 }
86
87 public static String getKeyVal(String nmspc) {
88 String ret = null;
89 if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
90 ret = nmspc.split(VAL_CHK)[1];
91 }
92 return ret;
93 }
94
95
Sithara Punnassery44e2a702017-03-06 15:38:10 -080096 public static String appendMultiInstKey(String path, String leaf) {
97 return (path + leaf.substring(leaf.indexOf(KEY_SEP)));
98 }
99
100 public static String appendKeyLeaf(String path, String key) {
101 return (path + EL_SEP + key);
102 }
103
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800104 public static String appendKeyLeaf(String path, KeyLeaf key) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800105 StringBuilder bldr = new StringBuilder();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800106 bldr.append(key.leafSchema().name());
107 bldr.append(NM_SEP);
108 bldr.append(key.leafSchema().namespace());
109 bldr.append(NM_SEP);
110 bldr.append(key.leafValue().toString());
111 return (path + EL_SEP + bldr.toString());
112 }
113
114 public static String appendNodeKey(String path, NodeKey key) {
115 return (path + EL_SEP + key.schemaId().name() + NM_SEP + key.schemaId().namespace());
116 }
117
118 public static String appendNodeKey(String path, String name, String nmSpc) {
119 return (path + EL_SEP + name + NM_SEP + nmSpc);
120 }
121
122 public static String appendLeafList(String path, LeafListKey key) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700123 return (path + VAL_SEP + key.asString());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800124 }
125
126 public static String appendLeafList(String path, String val) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700127 return (path + VAL_SEP + val);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800128 }
129
130 public static String appendKeyList(String path, ListKey key) {
131 StringBuilder bldr = new StringBuilder();
132 for (KeyLeaf keyLeaf : key.keyLeafs()) {
133 bldr.append(KEY_SEP);
134 bldr.append(keyLeaf.leafSchema().name());
135 bldr.append(NM_SEP);
136 bldr.append(keyLeaf.leafSchema().namespace());
137 bldr.append(NM_SEP);
138 bldr.append(keyLeaf.leafValue().toString());
139 }
140 return (path + bldr.toString());
141 }
142
143 public static String parseNodeKey(NodeKey key) {
144 if (key == null) {
145 return null;
146 }
147 StringBuilder bldr = new StringBuilder();
148 if (key instanceof LeafListKey) {
149 parseLeafList((LeafListKey) key, bldr);
150 } else if (key instanceof ListKey) {
151 parseKeyList((ListKey) key, bldr);
152 } else {
153 parseNodeKey(key, bldr);
154 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800155 return bldr.toString();
156 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800157
158 public static String parseResId(ResourceId path) {
159 StringBuilder bldr = new StringBuilder();
160 bldr.append(ROOT);
161 if (path == null) {
162 return bldr.toString();
163 }
164 List<NodeKey> nodeKeyList = path.nodeKeys();
165 for (NodeKey key : nodeKeyList) {
166 bldr.append(EL_SEP);
167 if (key instanceof LeafListKey) {
168 parseLeafList((LeafListKey) key, bldr);
169 } else if (key instanceof ListKey) {
170 parseKeyList((ListKey) key, bldr);
171 } else {
172 parseNodeKey(key, bldr);
173 }
174 }
175 return bldr.toString();
176 }
177
178 private static void parseLeafList(LeafListKey key, StringBuilder bldr) {
179 bldr.append(key.schemaId().name());
180 bldr.append(NM_SEP);
181 bldr.append(key.schemaId().namespace());
182 bldr.append(NM_SEP);
183 bldr.append(key.asString());
184 }
185
186 private static void parseKeyList(ListKey key, StringBuilder bldr) {
187 bldr.append(key.schemaId().name());
188 bldr.append(NM_SEP);
189 bldr.append(key.schemaId().namespace());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800190 Iterator<KeyLeaf> iter = key.keyLeafs().iterator();
191 KeyLeaf next;
192 while (iter.hasNext()) {
193 next = iter.next();
194 bldr.append(KEY_SEP);
195 bldr.append(next.leafSchema().name());
196 bldr.append(NM_SEP);
197 bldr.append(next.leafSchema().namespace());
198 bldr.append(NM_SEP);
199 bldr.append(next.leafValue().toString());
200 }
201 }
202
203 private static void parseNodeKey(NodeKey key, StringBuilder bldr) {
204 bldr.append(key.schemaId().name());
205 bldr.append(NM_SEP);
206 bldr.append(key.schemaId().namespace());
207 }
208
209 public static ResourceId getResId(List<String> dpath) {
210 ResourceId.Builder resBldr = new ResourceId.Builder();
211 Iterator<String> itr = dpath.iterator();
212 itr.next();
213 while (itr.hasNext()) {
214 String name = itr.next();
215 if (name.contains(VAL_SEP)) {
216 resBldr.addLeafListBranchPoint(name.substring(0, name.indexOf(NM_SEP)),
217 name.substring(name.indexOf(NM_SEP) + 1, name.indexOf(VAL_SEP)),
218 name.substring(name.indexOf(VAL_SEP) + 1));
219 } else if (name.contains(KEY_SEP)) {
Sithara Punnasserybda82502017-03-22 19:08:19 -0700220 String[] keys = name.split(KEY_CHK);
221 String[] nm = keys[0].split(NM_CHK);
222 resBldr.addBranchPointSchema(nm[0], nm[1]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800223 for (int i = 1; i < keys.length; i++) {
224 String key = keys[i];
Sithara Punnasserybda82502017-03-22 19:08:19 -0700225 String[] el = keys[i].split(NM_CHK);
226 if (el.length != 3) {
227 throw new FailedException("Malformed event subject, cannot parse");
228 }
229 resBldr.addKeyLeaf(el[0], el[1], el[2]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800230 }
231 } else {
232 resBldr.addBranchPointSchema(name.substring(0, name.indexOf(NM_SEP)),
233 name.substring(name.indexOf(NM_SEP) + 1));
234 }
235 }
236 return resBldr.build();
237 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800238}