Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.config; |
| 17 | |
| 18 | import java.util.Iterator; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 19 | |
Sithara Punnassery | bc9edb1 | 2017-07-20 14:32:33 -0700 | [diff] [blame] | 20 | import java.util.LinkedList; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 21 | import java.util.List; |
| 22 | |
| 23 | import org.onosproject.yang.model.KeyLeaf; |
| 24 | import org.onosproject.yang.model.LeafListKey; |
| 25 | import org.onosproject.yang.model.ListKey; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 26 | import org.onosproject.yang.model.NodeKey; |
| 27 | import org.onosproject.yang.model.ResourceId; |
| 28 | |
| 29 | /** |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 30 | * Utilities to work on the ResourceId. |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 31 | */ |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 32 | //FIXME add javadocs |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 33 | public final class ResourceIdParser { |
| 34 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 35 | 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 Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 39 | public static final String EL_SEP = "|"; |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 40 | public static final String VAL_CHK = "\\@"; |
| 41 | public static final String KEY_CHK = "\\$"; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 42 | public static final String NM_CHK = "\\#"; |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 43 | public static final String EL_CHK = "\\|"; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 44 | |
| 45 | |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 46 | private ResourceIdParser() { |
| 47 | |
| 48 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 49 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 50 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 70 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 90 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 98 | public static String appendKeyLeaf(String path, KeyLeaf key) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 99 | StringBuilder bldr = new StringBuilder(); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 100 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 117 | return (path + VAL_SEP + key.asString()); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | public static String appendLeafList(String path, String val) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 121 | return (path + VAL_SEP + val); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 122 | } |
| 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 Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 149 | return bldr.toString(); |
| 150 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 151 | |
| 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 Punnassery | bc9edb1 | 2017-07-20 14:32:33 -0700 | [diff] [blame] | 158 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 166 | 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 Punnassery | 9d464a3 | 2017-07-23 16:14:02 -0700 | [diff] [blame] | 179 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 190 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 202 | 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 Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 232 | String[] keys = name.split(KEY_CHK); |
| 233 | String[] nm = keys[0].split(NM_CHK); |
| 234 | resBldr.addBranchPointSchema(nm[0], nm[1]); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 235 | for (int i = 1; i < keys.length; i++) { |
| 236 | String key = keys[i]; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 237 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 242 | } |
| 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 Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 250 | } |