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