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 | |
| 33 | node_::node_() = default; |
| 34 | |
| 35 | node_::node_(decltype(m_suffix) node) |
| 36 | : m_suffix(node) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | node_::node_(module_ module, decltype(m_suffix) node) |
| 41 | : m_prefix(module) |
| 42 | , m_suffix(node) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | |
| 47 | bool node_::operator==(const node_& b) const |
| 48 | { |
| 49 | return this->m_suffix == b.m_suffix && this->m_prefix == b.m_prefix; |
| 50 | } |
| 51 | |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 52 | bool leaf_::operator==(const leaf_& b) const |
| 53 | { |
| 54 | return this->m_name == b.m_name; |
| 55 | } |
| 56 | |
| 57 | listElement_::listElement_(const std::string& listName, const std::map<std::string, std::string>& keys) |
| 58 | : m_name(listName) |
| 59 | , m_keys(keys) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | bool listElement_::operator==(const listElement_& b) const |
| 64 | { |
| 65 | return (this->m_name == b.m_name && this->m_keys == b.m_keys); |
| 66 | } |
| 67 | |
| 68 | bool path_::operator==(const path_& b) const |
| 69 | { |
| 70 | if (this->m_nodes.size() != b.m_nodes.size()) |
| 71 | return false; |
| 72 | return this->m_nodes == b.m_nodes; |
| 73 | } |
| 74 | |
| 75 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 76 | struct nodeToSchemaStringVisitor : public boost::static_visitor<std::string> { |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 77 | std::string operator()(const nodeup_&) const |
| 78 | { |
| 79 | return ".."; |
| 80 | } |
| 81 | template <class T> |
| 82 | std::string operator()(const T& node) const |
| 83 | { |
| 84 | return node.m_name; |
| 85 | } |
| 86 | }; |
Jan Kundrát | 2a8f433 | 2018-09-14 17:05:31 +0200 | [diff] [blame] | 87 | |
| 88 | std::string escapeListKeyString(const std::string& what) |
| 89 | { |
| 90 | // If we have both single and double quote, then we're screwed, but that "shouldn't happen" |
| 91 | // in <= YANG 1.1 due to limitations in XPath 1.0. |
| 92 | if (what.find('\'') != std::string::npos) { |
| 93 | return '\"' + what + '\"'; |
| 94 | } else { |
| 95 | return '\'' + what + '\''; |
| 96 | } |
| 97 | } |
| 98 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 99 | struct nodeToDataStringVisitor : public boost::static_visitor<std::string> { |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 100 | std::string operator()(const listElement_& node) const |
| 101 | { |
| 102 | std::ostringstream res; |
| 103 | res << node.m_name + "["; |
| 104 | std::transform(node.m_keys.begin(), node.m_keys.end(), |
| 105 | std::experimental::make_ostream_joiner(res, ' '), |
Jan Kundrát | 2a8f433 | 2018-09-14 17:05:31 +0200 | [diff] [blame] | 106 | [] (const auto& it) { return it.first + "=" + escapeListKeyString(it.second); }); |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 107 | res << "]"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 108 | return res.str(); |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 109 | } |
| 110 | std::string operator()(const nodeup_&) const |
| 111 | { |
| 112 | return ".."; |
| 113 | } |
| 114 | template <class T> |
| 115 | std::string operator()(const T& node) const |
| 116 | { |
| 117 | return node.m_name; |
| 118 | } |
| 119 | }; |
| 120 | |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 121 | std::string nodeToSchemaString(decltype(path_::m_nodes)::value_type node) |
| 122 | { |
| 123 | return boost::apply_visitor(nodeToSchemaStringVisitor(), node.m_suffix); |
| 124 | } |
| 125 | |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 126 | std::string pathToDataString(const path_& path) |
| 127 | { |
| 128 | std::string res; |
| 129 | for (const auto it : path.m_nodes) |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 130 | if (it.m_prefix) |
| 131 | res = joinPaths(res, it.m_prefix.value().m_name + ":" + boost::apply_visitor(nodeToDataStringVisitor(), it.m_suffix)); |
| 132 | else |
| 133 | res = joinPaths(res, boost::apply_visitor(nodeToDataStringVisitor(), it.m_suffix)); |
| 134 | |
| 135 | return res; |
| 136 | } |
| 137 | |
| 138 | std::string pathToAbsoluteSchemaString(const path_& path) |
| 139 | { |
| 140 | std::string res; |
| 141 | if (path.m_nodes.empty()) { |
| 142 | return ""; |
| 143 | } |
| 144 | |
| 145 | auto topLevelModule = path.m_nodes.at(0).m_prefix.value(); |
| 146 | for (const auto it : path.m_nodes) { |
| 147 | if (it.m_prefix) |
| 148 | res = joinPaths(res, it.m_prefix.value().m_name + ":" + boost::apply_visitor(nodeToSchemaStringVisitor(), it.m_suffix)); |
| 149 | else |
| 150 | res = joinPaths(res, topLevelModule.m_name + ":" + boost::apply_visitor(nodeToSchemaStringVisitor(), it.m_suffix)); |
| 151 | } |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 152 | return res; |
| 153 | } |
| 154 | |
| 155 | std::string pathToSchemaString(const path_& path) |
| 156 | { |
| 157 | std::string res; |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 158 | for (const auto it : path.m_nodes) { |
| 159 | if (it.m_prefix) |
| 160 | res = joinPaths(res, it.m_prefix.value().m_name + ":" + boost::apply_visitor(nodeToSchemaStringVisitor(), it.m_suffix)); |
| 161 | else |
| 162 | res = joinPaths(res, boost::apply_visitor(nodeToSchemaStringVisitor(), it.m_suffix)); |
| 163 | } |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 164 | return res; |
| 165 | } |