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