blob: 89988f7fd63ea6a7cd5204ea9c11238a5b8e6336 [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
29/**
Sithara Punnassery44e2a702017-03-06 15:38:10 -080030 * Utilities to work on the ResourceId.
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080031 */
Sithara Punnasserybda82502017-03-22 19:08:19 -070032//FIXME add javadocs
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080033public final class ResourceIdParser {
34
Sithara Punnassery44e2a702017-03-06 15:38:10 -080035 public static final String ROOT = "root";
36 public static final String NM_SEP = "#";
37 public static final String VAL_SEP = "@";
38 public static final String KEY_SEP = "$";
Sithara Punnasserybda82502017-03-22 19:08:19 -070039 public static final String EL_SEP = "|";
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -070040 public static final String VAL_CHK = "\\@";
41 public static final String KEY_CHK = "\\$";
Sithara Punnasserybda82502017-03-22 19:08:19 -070042 public static final String NM_CHK = "\\#";
Sithara Punnassery0b51d432017-03-28 01:09:28 -070043 public static final String EL_CHK = "\\|";
Sithara Punnassery44e2a702017-03-06 15:38:10 -080044
45
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080046 private ResourceIdParser() {
47
48 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -080049
Sithara Punnassery44e2a702017-03-06 15:38:10 -080050 public static NodeKey getInstanceKey(ResourceId path) {
51 int last = path.nodeKeys().size();
52 NodeKey ret = path.nodeKeys().get(last - 1);
53 if (ret instanceof NodeKey) {
54 return ret;
55 } else {
56 return null;
57 }
58 }
59
60 public static NodeKey getMultiInstanceKey(ResourceId path) {
61 int last = path.nodeKeys().size();
62 NodeKey ret = path.nodeKeys().get(last - 1);
63 if (ret instanceof ListKey) {
64 return ret;
65 } else {
66 return null;
67 }
68 }
69
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -070070 public static String getNamespace(String nmspc) {
71 String ret = null;
72 if (nmspc.contains(ResourceIdParser.KEY_SEP)) {
73 ret = nmspc.split(KEY_CHK)[0];
74 } else if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
75 ret = nmspc.split(VAL_CHK)[0];
76 } else {
77 ret = nmspc;
78 }
79 return ret;
80 }
81
82 public static String getKeyVal(String nmspc) {
83 String ret = null;
84 if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
85 ret = nmspc.split(VAL_CHK)[1];
86 }
87 return ret;
88 }
89
Sithara Punnassery44e2a702017-03-06 15:38:10 -080090 public static String appendMultiInstKey(String path, String leaf) {
91 return (path + leaf.substring(leaf.indexOf(KEY_SEP)));
92 }
93
94 public static String appendKeyLeaf(String path, String key) {
95 return (path + EL_SEP + key);
96 }
97
Sithara Punnassery44e2a702017-03-06 15:38:10 -080098 public static String appendKeyLeaf(String path, KeyLeaf key) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080099 StringBuilder bldr = new StringBuilder();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800100 bldr.append(key.leafSchema().name());
101 bldr.append(NM_SEP);
102 bldr.append(key.leafSchema().namespace());
103 bldr.append(NM_SEP);
104 bldr.append(key.leafValue().toString());
105 return (path + EL_SEP + bldr.toString());
106 }
107
108 public static String appendNodeKey(String path, NodeKey key) {
109 return (path + EL_SEP + key.schemaId().name() + NM_SEP + key.schemaId().namespace());
110 }
111
112 public static String appendNodeKey(String path, String name, String nmSpc) {
113 return (path + EL_SEP + name + NM_SEP + nmSpc);
114 }
115
116 public static String appendLeafList(String path, LeafListKey key) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700117 return (path + VAL_SEP + key.asString());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800118 }
119
120 public static String appendLeafList(String path, String val) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700121 return (path + VAL_SEP + val);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800122 }
123
124 public static String appendKeyList(String path, ListKey key) {
125 StringBuilder bldr = new StringBuilder();
126 for (KeyLeaf keyLeaf : key.keyLeafs()) {
127 bldr.append(KEY_SEP);
128 bldr.append(keyLeaf.leafSchema().name());
129 bldr.append(NM_SEP);
130 bldr.append(keyLeaf.leafSchema().namespace());
131 bldr.append(NM_SEP);
132 bldr.append(keyLeaf.leafValue().toString());
133 }
134 return (path + bldr.toString());
135 }
136
137 public static String parseNodeKey(NodeKey key) {
138 if (key == null) {
139 return null;
140 }
141 StringBuilder bldr = new StringBuilder();
142 if (key instanceof LeafListKey) {
143 parseLeafList((LeafListKey) key, bldr);
144 } else if (key instanceof ListKey) {
145 parseKeyList((ListKey) key, bldr);
146 } else {
147 parseNodeKey(key, bldr);
148 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800149 return bldr.toString();
150 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800151
152 public static String parseResId(ResourceId path) {
153 StringBuilder bldr = new StringBuilder();
154 bldr.append(ROOT);
155 if (path == null) {
156 return bldr.toString();
157 }
Sithara Punnasserybc9edb12017-07-20 14:32:33 -0700158 List<NodeKey> nodeKeyList = new LinkedList<>();
159 Iterator<NodeKey> itr = path.nodeKeys().iterator();
160 while (itr.hasNext()) {
161 nodeKeyList.add(itr.next());
162 }
163 if (nodeKeyList.get(0).schemaId().name().compareTo("/") == 0) {
164 nodeKeyList.remove(0);
165 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800166 for (NodeKey key : nodeKeyList) {
167 bldr.append(EL_SEP);
168 if (key instanceof LeafListKey) {
169 parseLeafList((LeafListKey) key, bldr);
170 } else if (key instanceof ListKey) {
171 parseKeyList((ListKey) key, bldr);
172 } else {
173 parseNodeKey(key, bldr);
174 }
175 }
176 return bldr.toString();
177 }
178
Sithara Punnassery9d464a32017-07-23 16:14:02 -0700179 public static String[] getService(ResourceId path) {
180 String[] res = null;
181 if (path == null) {
182 return res;
183 }
184 int last = path.nodeKeys().size() - 1;
185 res[0] = path.nodeKeys().get(last - 1).schemaId().name();
186 res[1] = path.nodeKeys().get(last).schemaId().name();
187 return res;
188 }
189
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800190 private static void parseLeafList(LeafListKey key, StringBuilder bldr) {
191 bldr.append(key.schemaId().name());
192 bldr.append(NM_SEP);
193 bldr.append(key.schemaId().namespace());
194 bldr.append(NM_SEP);
195 bldr.append(key.asString());
196 }
197
198 private static void parseKeyList(ListKey key, StringBuilder bldr) {
199 bldr.append(key.schemaId().name());
200 bldr.append(NM_SEP);
201 bldr.append(key.schemaId().namespace());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800202 Iterator<KeyLeaf> iter = key.keyLeafs().iterator();
203 KeyLeaf next;
204 while (iter.hasNext()) {
205 next = iter.next();
206 bldr.append(KEY_SEP);
207 bldr.append(next.leafSchema().name());
208 bldr.append(NM_SEP);
209 bldr.append(next.leafSchema().namespace());
210 bldr.append(NM_SEP);
211 bldr.append(next.leafValue().toString());
212 }
213 }
214
215 private static void parseNodeKey(NodeKey key, StringBuilder bldr) {
216 bldr.append(key.schemaId().name());
217 bldr.append(NM_SEP);
218 bldr.append(key.schemaId().namespace());
219 }
220
221 public static ResourceId getResId(List<String> dpath) {
222 ResourceId.Builder resBldr = new ResourceId.Builder();
223 Iterator<String> itr = dpath.iterator();
224 itr.next();
225 while (itr.hasNext()) {
226 String name = itr.next();
227 if (name.contains(VAL_SEP)) {
228 resBldr.addLeafListBranchPoint(name.substring(0, name.indexOf(NM_SEP)),
229 name.substring(name.indexOf(NM_SEP) + 1, name.indexOf(VAL_SEP)),
230 name.substring(name.indexOf(VAL_SEP) + 1));
231 } else if (name.contains(KEY_SEP)) {
Sithara Punnasserybda82502017-03-22 19:08:19 -0700232 String[] keys = name.split(KEY_CHK);
233 String[] nm = keys[0].split(NM_CHK);
234 resBldr.addBranchPointSchema(nm[0], nm[1]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800235 for (int i = 1; i < keys.length; i++) {
236 String key = keys[i];
Sithara Punnasserybda82502017-03-22 19:08:19 -0700237 String[] el = keys[i].split(NM_CHK);
238 if (el.length != 3) {
239 throw new FailedException("Malformed event subject, cannot parse");
240 }
241 resBldr.addKeyLeaf(el[0], el[1], el[2]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800242 }
243 } else {
244 resBldr.addBranchPointSchema(name.substring(0, name.indexOf(NM_SEP)),
245 name.substring(name.indexOf(NM_SEP) + 1));
246 }
247 }
248 return resBldr.build();
249 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800250}