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 | */ |
| 31 | |
| 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 = "$"; |
| 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 | 44e2a70 | 2017-03-06 15:38:10 -0800 | [diff] [blame] | 41 | |
| 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 | |
| 103 | //DONE |
| 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()); |
| 190 | bldr.append(NM_SEP); |
| 191 | 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)) { |
| 221 | resBldr.addBranchPointSchema(name.substring(0, name.indexOf(NM_SEP)), |
| 222 | name.substring(name.indexOf(NM_SEP) + 1, name.indexOf(KEY_SEP))); |
| 223 | String[] keys = name.split(KEY_SEP); |
| 224 | for (int i = 1; i < keys.length; i++) { |
| 225 | String key = keys[i]; |
| 226 | resBldr.addKeyLeaf(key.substring(0, key.indexOf(NM_SEP)), |
| 227 | key.substring(key.indexOf(NM_SEP) + 1, key.lastIndexOf(NM_SEP)), |
| 228 | key.substring(name.lastIndexOf(NM_SEP) + 1)); |
| 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 | } |