blob: 1a26a1c4cd6ad4df63171adf052eb6385b7f4eb0 [file] [log] [blame]
Václav Kubernát24df80e2018-06-06 15:18:03 +02001/*
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át509ce652019-05-29 19:46:44 +020010#include <sstream>
Václav Kubernát24df80e2018-06-06 15:18:03 +020011#include "ast_path.hpp"
12#include "utils.hpp"
13
14container_::container_(const std::string& name)
15 : m_name(name)
16{
17}
18
19bool container_::operator==(const container_& b) const
20{
21 return this->m_name == b.m_name;
22}
23
24leaf_::leaf_(const std::string& name)
25 : m_name(name)
26{
27}
28
Václav Kubernát5b8a8f32020-05-20 00:57:22 +020029bool leafListElement_::operator==(const leafListElement_& b) const
30{
31 return this->m_name == b.m_name && this->m_value == b.m_value;
32}
33
34leafList_::leafList_() = default;
35
36leafList_::leafList_(const std::string& name)
37 : m_name(name)
38{
39}
40
41bool leafList_::operator==(const leafList_& b) const
42{
43 return this->m_name == b.m_name;
44}
45
Václav Kubernát744f57f2018-06-29 22:46:26 +020046bool module_::operator==(const module_& b) const
47{
48 return this->m_name == b.m_name;
49}
50
Václav Kubernát2eaceb82018-10-08 19:56:30 +020051dataNode_::dataNode_() = default;
Václav Kubernát744f57f2018-06-29 22:46:26 +020052
Václav Kubernát2eaceb82018-10-08 19:56:30 +020053dataNode_::dataNode_(decltype(m_suffix) node)
Václav Kubernát744f57f2018-06-29 22:46:26 +020054 : m_suffix(node)
55{
56}
57
Václav Kubernát2eaceb82018-10-08 19:56:30 +020058dataNode_::dataNode_(module_ module, decltype(m_suffix) node)
Václav Kubernát744f57f2018-06-29 22:46:26 +020059 : m_prefix(module)
60 , m_suffix(node)
61{
62}
63
Václav Kubernát5c75b252018-10-10 18:33:47 +020064schemaNode_::schemaNode_(decltype(m_suffix) node)
65 : m_suffix(node)
66{
67}
68
Václav Kubernát2eaceb82018-10-08 19:56:30 +020069schemaNode_::schemaNode_(module_ module, decltype(m_suffix) node)
70 : m_prefix(module)
71 , m_suffix(node)
72{
73}
Václav Kubernát744f57f2018-06-29 22:46:26 +020074
Václav Kubernát2eaceb82018-10-08 19:56:30 +020075schemaNode_::schemaNode_() = default;
76
77bool schemaNode_::operator==(const schemaNode_& b) const
78{
79 return this->m_suffix == b.m_suffix && this->m_prefix == b.m_prefix;
80}
81
82bool dataNode_::operator==(const dataNode_& b) const
Václav Kubernát744f57f2018-06-29 22:46:26 +020083{
84 return this->m_suffix == b.m_suffix && this->m_prefix == b.m_prefix;
85}
86
Václav Kubernát24df80e2018-06-06 15:18:03 +020087bool leaf_::operator==(const leaf_& b) const
88{
89 return this->m_name == b.m_name;
90}
91
Václav Kubernát7707cae2020-01-16 12:04:53 +010092listElement_::listElement_(const std::string& listName, const std::map<std::string, leaf_data_>& keys)
Václav Kubernát24df80e2018-06-06 15:18:03 +020093 : m_name(listName)
94 , m_keys(keys)
95{
96}
97
98bool 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át2eaceb82018-10-08 19:56:30 +0200103bool list_::operator==(const list_& b) const
104{
105 return (this->m_name == b.m_name);
106}
107
108list_::list_(const std::string& listName)
109 : m_name(listName)
110{
111}
112
113bool 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
120bool dataPath_::operator==(const dataPath_& b) const
Václav Kubernát24df80e2018-06-06 15:18:03 +0200121{
122 if (this->m_nodes.size() != b.m_nodes.size())
123 return false;
124 return this->m_nodes == b.m_nodes;
125}
126
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200127struct nodeToSchemaStringVisitor {
Václav Kubernát24df80e2018-06-06 15:18:03 +0200128 std::string operator()(const nodeup_&) const
129 {
130 return "..";
131 }
132 template <class T>
133 std::string operator()(const T& node) const
134 {
135 return node.m_name;
136 }
137};
Jan Kundrát2a8f4332018-09-14 17:05:31 +0200138
139std::string escapeListKeyString(const std::string& what)
140{
141 // If we have both single and double quote, then we're screwed, but that "shouldn't happen"
142 // in <= YANG 1.1 due to limitations in XPath 1.0.
143 if (what.find('\'') != std::string::npos) {
144 return '\"' + what + '\"';
145 } else {
146 return '\'' + what + '\'';
147 }
148}
149
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200150struct nodeToDataStringVisitor {
Václav Kubernát24df80e2018-06-06 15:18:03 +0200151 std::string operator()(const listElement_& node) const
152 {
153 std::ostringstream res;
154 res << node.m_name + "[";
155 std::transform(node.m_keys.begin(), node.m_keys.end(),
Václav Kubernát5395e712019-12-03 18:24:33 +0100156 std::experimental::make_ostream_joiner(res, "]["),
Václav Kubernát7707cae2020-01-16 12:04:53 +0100157 [] (const auto& it) { return it.first + "=" + escapeListKeyString(leafDataToString(it.second)); });
Václav Kubernát24df80e2018-06-06 15:18:03 +0200158 res << "]";
Václav Kubernátebca2552018-06-08 19:06:02 +0200159 return res.str();
Václav Kubernát24df80e2018-06-06 15:18:03 +0200160 }
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200161 std::string operator()(const leafListElement_& node) const
162 {
163 return node.m_name + "[.=" + escapeListKeyString(leafDataToString(node.m_value)) + "]";
164 }
Václav Kubernát24df80e2018-06-06 15:18:03 +0200165 std::string operator()(const nodeup_&) const
166 {
167 return "..";
168 }
169 template <class T>
170 std::string operator()(const T& node) const
171 {
172 return node.m_name;
173 }
174};
175
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200176std::string nodeToSchemaString(decltype(dataPath_::m_nodes)::value_type node)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200177{
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200178 return std::visit(nodeToSchemaStringVisitor(), node.m_suffix);
Václav Kubernát744f57f2018-06-29 22:46:26 +0200179}
180
Václav Kubernátefcac932020-01-10 15:26:32 +0100181std::string pathToDataString(const dataPath_& path, Prefixes prefixes)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200182{
183 std::string res;
Václav Kubernát5395e712019-12-03 18:24:33 +0100184 if (path.m_scope == Scope::Absolute) {
185 res = "/";
186 }
Václav Kubernátefcac932020-01-10 15:26:32 +0100187
Václav Kubernátef085742020-04-21 09:28:44 +0200188 for (const auto& it : path.m_nodes) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200189 if (it.m_prefix)
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200190 res = joinPaths(res, it.m_prefix.value().m_name + ":" + std::visit(nodeToDataStringVisitor(), it.m_suffix));
Václav Kubernát744f57f2018-06-29 22:46:26 +0200191 else
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200192 res = joinPaths(res, (prefixes == Prefixes::Always ? path.m_nodes.at(0).m_prefix.value().m_name + ":" : "") + std::visit(nodeToDataStringVisitor(), it.m_suffix));
Václav Kubernátefcac932020-01-10 15:26:32 +0100193 }
Václav Kubernát744f57f2018-06-29 22:46:26 +0200194
195 return res;
196}
197
Václav Kubernátefcac932020-01-10 15:26:32 +0100198std::string pathToSchemaString(const schemaPath_& path, Prefixes prefixes)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200199{
200 std::string res;
Václav Kubernátefcac932020-01-10 15:26:32 +0100201 if (path.m_scope == Scope::Absolute) {
202 res = "/";
Václav Kubernát744f57f2018-06-29 22:46:26 +0200203 }
204
Václav Kubernátef085742020-04-21 09:28:44 +0200205 for (const auto& it : path.m_nodes) {
Václav Kubernát744f57f2018-06-29 22:46:26 +0200206 if (it.m_prefix)
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200207 res = joinPaths(res, it.m_prefix.value().m_name + ":" + std::visit(nodeToSchemaStringVisitor(), it.m_suffix));
Václav Kubernát744f57f2018-06-29 22:46:26 +0200208 else
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200209 res = joinPaths(res, (prefixes == Prefixes::Always ? path.m_nodes.at(0).m_prefix.value().m_name + ":" : "") + std::visit(nodeToSchemaStringVisitor(), it.m_suffix));
Václav Kubernát744f57f2018-06-29 22:46:26 +0200210 }
Václav Kubernát24df80e2018-06-06 15:18:03 +0200211 return res;
212}
213
Václav Kubernátefcac932020-01-10 15:26:32 +0100214std::string pathToSchemaString(const dataPath_& path, Prefixes prefixes)
Václav Kubernát24df80e2018-06-06 15:18:03 +0200215{
Václav Kubernátefcac932020-01-10 15:26:32 +0100216 return pathToSchemaString(dataPathToSchemaPath(path), prefixes);
Václav Kubernát5c75b252018-10-10 18:33:47 +0200217}
218
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200219struct dataSuffixToSchemaSuffix {
220 using ReturnType = decltype(schemaNode_::m_suffix);
221 ReturnType operator()(const listElement_& listElement) const
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200222 {
223 return list_{listElement.m_name};
224 }
225
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200226 ReturnType operator()(const leafListElement_& leafListElement) const
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200227 {
228 return leafList_{leafListElement.m_name};
229 }
230
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200231 template <typename T>
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200232 ReturnType operator()(const T& suffix) const
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200233 {
234 return suffix;
235 }
236};
237
238schemaNode_ dataNodeToSchemaNode(const dataNode_& node)
239{
240 schemaNode_ res;
241 res.m_prefix = node.m_prefix;
Václav Kubernátb5ca1542020-05-27 01:03:54 +0200242 res.m_suffix = std::visit(dataSuffixToSchemaSuffix(), node.m_suffix);
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200243 return res;
244}
245
246schemaPath_ dataPathToSchemaPath(const dataPath_& path)
247{
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100248 schemaPath_ res{path.m_scope, {}};
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200249
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100250 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át2eaceb82018-10-08 19:56:30 +0200253
Václav Kubernátbf083ec2019-02-19 13:58:09 +0100254 return res;
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200255}