blob: f99355018191a91d3b2b708c4445702948d00ed7 [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 Punnassery44e2a702017-03-06 15:38:10 -080042
43
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080044 private ResourceIdParser() {
45
46 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -080047
48 public static ResourceId getParent(ResourceId path) {
49 int last = path.nodeKeys().size();
50 path.nodeKeys().remove(last - 1);
51 return path;
52 }
53
54 public static NodeKey getInstanceKey(ResourceId path) {
55 int last = path.nodeKeys().size();
56 NodeKey ret = path.nodeKeys().get(last - 1);
57 if (ret instanceof NodeKey) {
58 return ret;
59 } else {
60 return null;
61 }
62 }
63
64 public static NodeKey getMultiInstanceKey(ResourceId path) {
65 int last = path.nodeKeys().size();
66 NodeKey ret = path.nodeKeys().get(last - 1);
67 if (ret instanceof ListKey) {
68 return ret;
69 } else {
70 return null;
71 }
72 }
73
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -070074 public static String getNamespace(String nmspc) {
75 String ret = null;
76 if (nmspc.contains(ResourceIdParser.KEY_SEP)) {
77 ret = nmspc.split(KEY_CHK)[0];
78 } else if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
79 ret = nmspc.split(VAL_CHK)[0];
80 } else {
81 ret = nmspc;
82 }
83 return ret;
84 }
85
86 public static String getKeyVal(String nmspc) {
87 String ret = null;
88 if (nmspc.contains(ResourceIdParser.VAL_SEP)) {
89 ret = nmspc.split(VAL_CHK)[1];
90 }
91 return ret;
92 }
93
94
Sithara Punnassery44e2a702017-03-06 15:38:10 -080095 public static String appendMultiInstKey(String path, String leaf) {
96 return (path + leaf.substring(leaf.indexOf(KEY_SEP)));
97 }
98
99 public static String appendKeyLeaf(String path, String key) {
100 return (path + EL_SEP + key);
101 }
102
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800103 public static String appendKeyLeaf(String path, KeyLeaf key) {
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800104 StringBuilder bldr = new StringBuilder();
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800105 bldr.append(key.leafSchema().name());
106 bldr.append(NM_SEP);
107 bldr.append(key.leafSchema().namespace());
108 bldr.append(NM_SEP);
109 bldr.append(key.leafValue().toString());
110 return (path + EL_SEP + bldr.toString());
111 }
112
113 public static String appendNodeKey(String path, NodeKey key) {
114 return (path + EL_SEP + key.schemaId().name() + NM_SEP + key.schemaId().namespace());
115 }
116
117 public static String appendNodeKey(String path, String name, String nmSpc) {
118 return (path + EL_SEP + name + NM_SEP + nmSpc);
119 }
120
121 public static String appendLeafList(String path, LeafListKey key) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700122 return (path + VAL_SEP + key.asString());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800123 }
124
125 public static String appendLeafList(String path, String val) {
Sithara Punnassery4cd2ced2017-03-17 17:36:43 -0700126 return (path + VAL_SEP + val);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800127 }
128
129 public static String appendKeyList(String path, ListKey key) {
130 StringBuilder bldr = new StringBuilder();
131 for (KeyLeaf keyLeaf : key.keyLeafs()) {
132 bldr.append(KEY_SEP);
133 bldr.append(keyLeaf.leafSchema().name());
134 bldr.append(NM_SEP);
135 bldr.append(keyLeaf.leafSchema().namespace());
136 bldr.append(NM_SEP);
137 bldr.append(keyLeaf.leafValue().toString());
138 }
139 return (path + bldr.toString());
140 }
141
142 public static String parseNodeKey(NodeKey key) {
143 if (key == null) {
144 return null;
145 }
146 StringBuilder bldr = new StringBuilder();
147 if (key instanceof LeafListKey) {
148 parseLeafList((LeafListKey) key, bldr);
149 } else if (key instanceof ListKey) {
150 parseKeyList((ListKey) key, bldr);
151 } else {
152 parseNodeKey(key, bldr);
153 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800154 return bldr.toString();
155 }
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800156
157 public static String parseResId(ResourceId path) {
158 StringBuilder bldr = new StringBuilder();
159 bldr.append(ROOT);
160 if (path == null) {
161 return bldr.toString();
162 }
163 List<NodeKey> nodeKeyList = path.nodeKeys();
164 for (NodeKey key : nodeKeyList) {
165 bldr.append(EL_SEP);
166 if (key instanceof LeafListKey) {
167 parseLeafList((LeafListKey) key, bldr);
168 } else if (key instanceof ListKey) {
169 parseKeyList((ListKey) key, bldr);
170 } else {
171 parseNodeKey(key, bldr);
172 }
173 }
174 return bldr.toString();
175 }
176
177 private static void parseLeafList(LeafListKey key, StringBuilder bldr) {
178 bldr.append(key.schemaId().name());
179 bldr.append(NM_SEP);
180 bldr.append(key.schemaId().namespace());
181 bldr.append(NM_SEP);
182 bldr.append(key.asString());
183 }
184
185 private static void parseKeyList(ListKey key, StringBuilder bldr) {
186 bldr.append(key.schemaId().name());
187 bldr.append(NM_SEP);
188 bldr.append(key.schemaId().namespace());
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800189 Iterator<KeyLeaf> iter = key.keyLeafs().iterator();
190 KeyLeaf next;
191 while (iter.hasNext()) {
192 next = iter.next();
193 bldr.append(KEY_SEP);
194 bldr.append(next.leafSchema().name());
195 bldr.append(NM_SEP);
196 bldr.append(next.leafSchema().namespace());
197 bldr.append(NM_SEP);
198 bldr.append(next.leafValue().toString());
199 }
200 }
201
202 private static void parseNodeKey(NodeKey key, StringBuilder bldr) {
203 bldr.append(key.schemaId().name());
204 bldr.append(NM_SEP);
205 bldr.append(key.schemaId().namespace());
206 }
207
208 public static ResourceId getResId(List<String> dpath) {
209 ResourceId.Builder resBldr = new ResourceId.Builder();
210 Iterator<String> itr = dpath.iterator();
211 itr.next();
212 while (itr.hasNext()) {
213 String name = itr.next();
214 if (name.contains(VAL_SEP)) {
215 resBldr.addLeafListBranchPoint(name.substring(0, name.indexOf(NM_SEP)),
216 name.substring(name.indexOf(NM_SEP) + 1, name.indexOf(VAL_SEP)),
217 name.substring(name.indexOf(VAL_SEP) + 1));
218 } else if (name.contains(KEY_SEP)) {
Sithara Punnasserybda82502017-03-22 19:08:19 -0700219 String[] keys = name.split(KEY_CHK);
220 String[] nm = keys[0].split(NM_CHK);
221 resBldr.addBranchPointSchema(nm[0], nm[1]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800222 for (int i = 1; i < keys.length; i++) {
223 String key = keys[i];
Sithara Punnasserybda82502017-03-22 19:08:19 -0700224 String[] el = keys[i].split(NM_CHK);
225 if (el.length != 3) {
226 throw new FailedException("Malformed event subject, cannot parse");
227 }
228 resBldr.addKeyLeaf(el[0], el[1], el[2]);
Sithara Punnassery44e2a702017-03-06 15:38:10 -0800229 }
230 } else {
231 resBldr.addBranchPointSchema(name.substring(0, name.indexOf(NM_SEP)),
232 name.substring(name.indexOf(NM_SEP) + 1));
233 }
234 }
235 return resBldr.build();
236 }
Sithara Punnassery4b091dc2017-03-02 17:22:40 -0800237}