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