Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include <experimental/iterator> |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 10 | #include <sstream> |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 11 | #include "ast_path.hpp" |
| 12 | #include "utils.hpp" |
| 13 | |
| 14 | container_::container_(const std::string& name) |
| 15 | : m_name(name) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | bool container_::operator==(const container_& b) const |
| 20 | { |
| 21 | return this->m_name == b.m_name; |
| 22 | } |
| 23 | |
| 24 | leaf_::leaf_(const std::string& name) |
| 25 | : m_name(name) |
| 26 | { |
| 27 | } |
| 28 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 29 | bool leafListElement_::operator==(const leafListElement_& b) const |
| 30 | { |
| 31 | return this->m_name == b.m_name && this->m_value == b.m_value; |
| 32 | } |
| 33 | |
| 34 | leafList_::leafList_() = default; |
| 35 | |
| 36 | leafList_::leafList_(const std::string& name) |
| 37 | : m_name(name) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | bool leafList_::operator==(const leafList_& b) const |
| 42 | { |
| 43 | return this->m_name == b.m_name; |
| 44 | } |
| 45 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 46 | bool module_::operator==(const module_& b) const |
| 47 | { |
| 48 | return this->m_name == b.m_name; |
| 49 | } |
| 50 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 51 | dataNode_::dataNode_() = default; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 52 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 53 | dataNode_::dataNode_(decltype(m_suffix) node) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 54 | : m_suffix(node) |
| 55 | { |
| 56 | } |
| 57 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 58 | dataNode_::dataNode_(module_ module, decltype(m_suffix) node) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 59 | : m_prefix(module) |
| 60 | , m_suffix(node) |
| 61 | { |
| 62 | } |
| 63 | |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 64 | schemaNode_::schemaNode_(decltype(m_suffix) node) |
| 65 | : m_suffix(node) |
| 66 | { |
| 67 | } |
| 68 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 69 | schemaNode_::schemaNode_(module_ module, decltype(m_suffix) node) |
| 70 | : m_prefix(module) |
| 71 | , m_suffix(node) |
| 72 | { |
| 73 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 74 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 75 | schemaNode_::schemaNode_() = default; |
| 76 | |
| 77 | bool schemaNode_::operator==(const schemaNode_& b) const |
| 78 | { |
| 79 | return this->m_suffix == b.m_suffix && this->m_prefix == b.m_prefix; |
| 80 | } |
| 81 | |
| 82 | bool dataNode_::operator==(const dataNode_& b) const |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 83 | { |
| 84 | return this->m_suffix == b.m_suffix && this->m_prefix == b.m_prefix; |
| 85 | } |
| 86 | |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 87 | bool leaf_::operator==(const leaf_& b) const |
| 88 | { |
| 89 | return this->m_name == b.m_name; |
| 90 | } |
| 91 | |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 92 | listElement_::listElement_(const std::string& listName, const std::map<std::string, leaf_data_>& keys) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 93 | : m_name(listName) |
| 94 | , m_keys(keys) |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | bool listElement_::operator==(const listElement_& b) const |
| 99 | { |
| 100 | return (this->m_name == b.m_name && this->m_keys == b.m_keys); |
| 101 | } |
| 102 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 103 | bool list_::operator==(const list_& b) const |
| 104 | { |
| 105 | return (this->m_name == b.m_name); |
| 106 | } |
| 107 | |
| 108 | list_::list_(const std::string& listName) |
| 109 | : m_name(listName) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | bool schemaPath_::operator==(const schemaPath_& b) const |
| 114 | { |
| 115 | if (this->m_nodes.size() != b.m_nodes.size()) |
| 116 | return false; |
| 117 | return this->m_nodes == b.m_nodes; |
| 118 | } |
| 119 | |
| 120 | bool dataPath_::operator==(const dataPath_& b) const |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 121 | { |
| 122 | if (this->m_nodes.size() != b.m_nodes.size()) |
| 123 | return false; |
| 124 | return this->m_nodes == b.m_nodes; |
| 125 | } |
| 126 | |
| 127 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 128 | struct nodeToSchemaStringVisitor : public boost::static_visitor<std::string> { |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 129 | std::string operator()(const nodeup_&) const |
| 130 | { |
| 131 | return ".."; |
| 132 | } |
| 133 | template <class T> |
| 134 | std::string operator()(const T& node) const |
| 135 | { |
| 136 | return node.m_name; |
| 137 | } |
| 138 | }; |
Jan Kundrát | 2a8f433 | 2018-09-14 17:05:31 +0200 | [diff] [blame] | 139 | |
| 140 | std::string escapeListKeyString(const std::string& what) |
| 141 | { |
| 142 | // If we have both single and double quote, then we're screwed, but that "shouldn't happen" |
| 143 | // in <= YANG 1.1 due to limitations in XPath 1.0. |
| 144 | if (what.find('\'') != std::string::npos) { |
| 145 | return '\"' + what + '\"'; |
| 146 | } else { |
| 147 | return '\'' + what + '\''; |
| 148 | } |
| 149 | } |
| 150 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 151 | struct nodeToDataStringVisitor : public boost::static_visitor<std::string> { |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 152 | std::string operator()(const listElement_& node) const |
| 153 | { |
| 154 | std::ostringstream res; |
| 155 | res << node.m_name + "["; |
| 156 | std::transform(node.m_keys.begin(), node.m_keys.end(), |
Václav Kubernát | 5395e71 | 2019-12-03 18:24:33 +0100 | [diff] [blame] | 157 | std::experimental::make_ostream_joiner(res, "]["), |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 158 | [] (const auto& it) { return it.first + "=" + escapeListKeyString(leafDataToString(it.second)); }); |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 159 | res << "]"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 160 | return res.str(); |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 161 | } |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 162 | std::string operator()(const leafListElement_& node) const |
| 163 | { |
| 164 | return node.m_name + "[.=" + escapeListKeyString(leafDataToString(node.m_value)) + "]"; |
| 165 | } |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 166 | std::string operator()(const nodeup_&) const |
| 167 | { |
| 168 | return ".."; |
| 169 | } |
| 170 | template <class T> |
| 171 | std::string operator()(const T& node) const |
| 172 | { |
| 173 | return node.m_name; |
| 174 | } |
| 175 | }; |
| 176 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 177 | std::string nodeToSchemaString(decltype(dataPath_::m_nodes)::value_type node) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 178 | { |
| 179 | return boost::apply_visitor(nodeToSchemaStringVisitor(), node.m_suffix); |
| 180 | } |
| 181 | |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 182 | std::string pathToDataString(const dataPath_& path, Prefixes prefixes) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 183 | { |
| 184 | std::string res; |
Václav Kubernát | 5395e71 | 2019-12-03 18:24:33 +0100 | [diff] [blame] | 185 | if (path.m_scope == Scope::Absolute) { |
| 186 | res = "/"; |
| 187 | } |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 188 | |
Václav Kubernát | ef08574 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 189 | for (const auto& it : path.m_nodes) { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 190 | if (it.m_prefix) |
| 191 | res = joinPaths(res, it.m_prefix.value().m_name + ":" + boost::apply_visitor(nodeToDataStringVisitor(), it.m_suffix)); |
| 192 | else |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 193 | res = joinPaths(res, (prefixes == Prefixes::Always ? path.m_nodes.at(0).m_prefix.value().m_name + ":" : "") + boost::apply_visitor(nodeToDataStringVisitor(), it.m_suffix)); |
| 194 | } |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 195 | |
| 196 | return res; |
| 197 | } |
| 198 | |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 199 | std::string pathToSchemaString(const schemaPath_& path, Prefixes prefixes) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 200 | { |
| 201 | std::string res; |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 202 | if (path.m_scope == Scope::Absolute) { |
| 203 | res = "/"; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Václav Kubernát | ef08574 | 2020-04-21 09:28:44 +0200 | [diff] [blame] | 206 | for (const auto& it : path.m_nodes) { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 207 | if (it.m_prefix) |
| 208 | res = joinPaths(res, it.m_prefix.value().m_name + ":" + boost::apply_visitor(nodeToSchemaStringVisitor(), it.m_suffix)); |
| 209 | else |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 210 | res = joinPaths(res, (prefixes == Prefixes::Always ? path.m_nodes.at(0).m_prefix.value().m_name + ":" : "") + boost::apply_visitor(nodeToSchemaStringVisitor(), it.m_suffix)); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 211 | } |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 212 | return res; |
| 213 | } |
| 214 | |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 215 | std::string pathToSchemaString(const dataPath_& path, Prefixes prefixes) |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 216 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 217 | return pathToSchemaString(dataPathToSchemaPath(path), prefixes); |
Václav Kubernát | 5c75b25 | 2018-10-10 18:33:47 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 220 | struct dataSuffixToSchemaSuffix : boost::static_visitor<decltype(schemaNode_::m_suffix)> { |
| 221 | auto operator()(const listElement_& listElement) const |
| 222 | { |
| 223 | return list_{listElement.m_name}; |
| 224 | } |
| 225 | |
Václav Kubernát | 5b8a8f3 | 2020-05-20 00:57:22 +0200 | [diff] [blame] | 226 | auto operator()(const leafListElement_& leafListElement) const |
| 227 | { |
| 228 | return leafList_{leafListElement.m_name}; |
| 229 | } |
| 230 | |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 231 | template <typename T> |
| 232 | auto operator()(const T& suffix) const |
| 233 | { |
| 234 | return suffix; |
| 235 | } |
| 236 | }; |
| 237 | |
| 238 | schemaNode_ dataNodeToSchemaNode(const dataNode_& node) |
| 239 | { |
| 240 | schemaNode_ res; |
| 241 | res.m_prefix = node.m_prefix; |
| 242 | res.m_suffix = boost::apply_visitor(dataSuffixToSchemaSuffix(), node.m_suffix); |
| 243 | return res; |
| 244 | } |
| 245 | |
| 246 | schemaPath_ dataPathToSchemaPath(const dataPath_& path) |
| 247 | { |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 248 | schemaPath_ res{path.m_scope, {}}; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 249 | |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 250 | std::transform(path.m_nodes.begin(), path.m_nodes.end(), |
| 251 | std::back_inserter(res.m_nodes), |
| 252 | [](const dataNode_& node) { return dataNodeToSchemaNode(node); }); |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 253 | |
Václav Kubernát | bf083ec | 2019-02-19 13:58:09 +0100 | [diff] [blame] | 254 | return res; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 255 | } |