blob: 6bcdeccef713f0fe1b795f9316341aedb04de313 [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
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
179 private static void parseLeafList(LeafListKey key, StringBuilder bldr) {
180 bldr.append(key.schemaId().name());
181 bldr.append(NM_SEP);
182 bldr.append(key.schemaId().namespace());
183 bldr.append(NM_SEP);
184 bldr.append(key.asString());
185 }
186
187 private static void parseKeyList(ListKey key, StringBuilder bldr) {
188 bldr.append(key.schemaId().name());
189 bldr.append(NM_SEP);
190 bldr.append(key.schemaId().namespace());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800191 Iterator<KeyLeaf> iter = key.keyLeafs().iterator();
192 KeyLeaf next;
193 while (iter.hasNext()) {
194 next = iter.next();
195 bldr.append(KEY_SEP);
196 bldr.append(next.leafSchema().name());
197 bldr.append(NM_SEP);
198 bldr.append(next.leafSchema().namespace());
199 bldr.append(NM_SEP);
200 bldr.append(next.leafValue().toString());
201 }
202 }
203
204 private static void parseNodeKey(NodeKey key, StringBuilder bldr) {
205 bldr.append(key.schemaId().name());
206 bldr.append(NM_SEP);
207 bldr.append(key.schemaId().namespace());
208 }
209
210 public static ResourceId getResId(List<String> dpath) {
211 ResourceId.Builder resBldr = new ResourceId.Builder();
212 Iterator<String> itr = dpath.iterator();
213 itr.next();
214 while (itr.hasNext()) {
215 String name = itr.next();
216 if (name.contains(VAL_SEP)) {
217 resBldr.addLeafListBranchPoint(name.substring(0, name.indexOf(NM_SEP)),
218 name.substring(name.indexOf(NM_SEP) + 1, name.indexOf(VAL_SEP)),
219 name.substring(name.indexOf(VAL_SEP) + 1));
220 } else if (name.contains(KEY_SEP)) {
Sithara Punnasserybda82502017-03-22 19:08:19 -0700221 String[] keys = name.split(KEY_CHK);
222 String[] nm = keys[0].split(NM_CHK);
223 resBldr.addBranchPointSchema(nm[0], nm[1]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800224 for (int i = 1; i < keys.length; i++) {
225 String key = keys[i];
Sithara Punnasserybda82502017-03-22 19:08:19 -0700226 String[] el = keys[i].split(NM_CHK);
227 if (el.length != 3) {
228 throw new FailedException("Malformed event subject, cannot parse");
229 }
230 resBldr.addKeyLeaf(el[0], el[1], el[2]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800231 }
232 } else {
233 resBldr.addBranchPointSchema(name.substring(0, name.indexOf(NM_SEP)),
234 name.substring(name.indexOf(NM_SEP) + 1));
235 }
236 }
237 return resBldr.build();
238 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800239}