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 | |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.onosproject.yang.model.KeyLeaf; |
| 23 | import org.onosproject.yang.model.LeafListKey; |
| 24 | import org.onosproject.yang.model.ListKey; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 25 | import org.onosproject.yang.model.NodeKey; |
| 26 | import org.onosproject.yang.model.ResourceId; |
| 27 | |
| 28 | /** |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 29 | * Utilities to work on the ResourceId. |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 30 | */ |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 31 | //FIXME add javadocs |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 32 | public final class ResourceIdParser { |
| 33 | |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 34 | 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 Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 38 | public static final String EL_SEP = "|"; |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 39 | public static final String VAL_CHK = "\\@"; |
| 40 | public static final String KEY_CHK = "\\$"; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 41 | public static final String NM_CHK = "\\#"; |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 42 | public static final String EL_CHK = "\\|"; |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 43 | |
| 44 | |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 45 | private ResourceIdParser() { |
| 46 | |
| 47 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 48 | |
| 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 75 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 96 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 104 | public static String appendKeyLeaf(String path, KeyLeaf key) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 105 | StringBuilder bldr = new StringBuilder(); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 106 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 123 | return (path + VAL_SEP + key.asString()); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | public static String appendLeafList(String path, String val) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 127 | return (path + VAL_SEP + val); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 128 | } |
| 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 Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 155 | return bldr.toString(); |
| 156 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 157 | |
| 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 190 | 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 Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 220 | String[] keys = name.split(KEY_CHK); |
| 221 | String[] nm = keys[0].split(NM_CHK); |
| 222 | resBldr.addBranchPointSchema(nm[0], nm[1]); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 223 | for (int i = 1; i < keys.length; i++) { |
| 224 | String key = keys[i]; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 225 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 230 | } |
| 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 Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 238 | } |