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 | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 42 | |
| 43 | |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 44 | private ResourceIdParser() { |
| 45 | |
| 46 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 47 | |
| 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 74 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 95 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 103 | public static String appendKeyLeaf(String path, KeyLeaf key) { |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 104 | StringBuilder bldr = new StringBuilder(); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 105 | 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 Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 122 | return (path + VAL_SEP + key.asString()); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | public static String appendLeafList(String path, String val) { |
Sithara Punnassery | 4cd2ced | 2017-03-17 17:36:43 -0700 | [diff] [blame] | 126 | return (path + VAL_SEP + val); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 127 | } |
| 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 Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 154 | return bldr.toString(); |
| 155 | } |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 156 | |
| 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 189 | 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 Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 219 | String[] keys = name.split(KEY_CHK); |
| 220 | String[] nm = keys[0].split(NM_CHK); |
| 221 | resBldr.addBranchPointSchema(nm[0], nm[1]); |
Sithara Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 222 | for (int i = 1; i < keys.length; i++) { |
| 223 | String key = keys[i]; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 224 | 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 Punnassery | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 229 | } |
| 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 Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 237 | } |