blob: fd2e2974e93071cc9a6bf2034c85d73201619b6e [file] [log] [blame]
Václav Kubernát94938b72018-05-04 15:12:24 +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*/
Václav Kubernát2984f442020-02-20 17:43:35 +01008#include <experimental/iterator>
Václav Kubernát509ce652019-05-29 19:46:44 +02009#include <sstream>
Václav Kubernátcb3af402020-02-12 16:49:17 +010010#include "completion.hpp"
Václav Kubernát94938b72018-05-04 15:12:24 +020011#include "utils.hpp"
12
13std::string joinPaths(const std::string& prefix, const std::string& suffix)
14{
Václav Kubernáta44bdf22020-01-24 12:15:31 +010015 // These two if statements are essential for the algorithm:
16 // The first one solves joining nothing and a relative path - the algorithm
17 // down below adds a leading slash, turning it into an absolute path.
18 // The second one would always add a trailing slash to the path.
19 if (prefix.empty()) {
20 return suffix;
21 }
22
23 if (suffix.empty()) {
24 return prefix;
25 }
26
27 // Otherwise, strip slashes where the join is going to happen. This will
28 // also change "/" to "", but the return statement takes care of that and
29 // inserts the slash again.
30 auto prefixWithoutTrailingSlash = !prefix.empty() && prefix.back() == '/' ? prefix.substr(0, prefix.length() - 1) : prefix;
31 auto suffixWithoutLeadingSlash = !suffix.empty() && suffix.front() == '/' ? suffix.substr(1) : suffix;
32
33 // And join the result with a slash.
34 return prefixWithoutTrailingSlash + '/' + suffixWithoutLeadingSlash;
Václav Kubernát94938b72018-05-04 15:12:24 +020035}
Václav Kubernát60d6f292018-05-25 09:45:32 +020036
37std::string stripLastNodeFromPath(const std::string& path)
38{
39 std::string res = path;
40 auto pos = res.find_last_of('/');
Václav Kubernátefcac932020-01-10 15:26:32 +010041 if (pos == res.npos) { // path has no backslash - it's either empty, or is a relative path with one fragment
Václav Kubernát60d6f292018-05-25 09:45:32 +020042 res.clear();
Václav Kubernátefcac932020-01-10 15:26:32 +010043 } else if (pos == 0) { // path has one backslash at the start - it's either "/" or "/one-path-fragment"
44 return "/";
45 } else {
Václav Kubernát60d6f292018-05-25 09:45:32 +020046 res.erase(pos);
Václav Kubernátefcac932020-01-10 15:26:32 +010047 }
Václav Kubernát60d6f292018-05-25 09:45:32 +020048 return res;
49}
Václav Kubernátebca2552018-06-08 19:06:02 +020050
Václav Kubernát2eaceb82018-10-08 19:56:30 +020051schemaPath_ pathWithoutLastNode(const schemaPath_& path)
Václav Kubernátebca2552018-06-08 19:06:02 +020052{
Václav Kubernát2eaceb82018-10-08 19:56:30 +020053 return schemaPath_{path.m_scope, decltype(schemaPath_::m_nodes)(path.m_nodes.begin(), path.m_nodes.end() - 1)};
Václav Kubernátebca2552018-06-08 19:06:02 +020054}
Václav Kubernát0b0272f2018-06-13 14:13:08 +020055
Jan Kundrát6ebcc6c2020-05-07 01:58:51 +020056ModuleNodePair splitModuleNode(const std::string& input)
57{
58 auto colonLocation = input.find_first_of(':');
59 if (colonLocation != std::string::npos) {
60 return ModuleNodePair{input.substr(0, colonLocation), input.substr(colonLocation + 1)};
61 }
62 throw std::logic_error("Internal error: got module-unqualified node name");
63}
64
Václav Kubernát3a99f002020-03-31 02:27:41 +020065struct impl_leafDataTypeToString {
66 std::string operator()(const yang::String)
67 {
Václav Kubernát0b0272f2018-06-13 14:13:08 +020068 return "a string";
Václav Kubernát0b0272f2018-06-13 14:13:08 +020069 }
Václav Kubernát3a99f002020-03-31 02:27:41 +020070 std::string operator()(const yang::Decimal)
71 {
72 return "a decimal";
73 }
74 std::string operator()(const yang::Bool)
75 {
76 return "a boolean";
77 }
78 std::string operator()(const yang::Int8)
79 {
80 return "an 8-bit integer";
81 }
82 std::string operator()(const yang::Uint8)
83 {
84 return "an 8-bit unsigned integer";
85 }
86 std::string operator()(const yang::Int16)
87 {
88 return "a 16-bit integer";
89 }
90 std::string operator()(const yang::Uint16)
91 {
92 return "a 16-bit unsigned integer";
93 }
94 std::string operator()(const yang::Int32)
95 {
96 return "a 32-bit integer";
97 }
98 std::string operator()(const yang::Uint32)
99 {
100 return "a 32-bit unsigned integer";
101 }
102 std::string operator()(const yang::Int64)
103 {
104 return "a 64-bit integer";
105 }
106 std::string operator()(const yang::Uint64)
107 {
108 return "a 64-bit unsigned integer";
109 }
110 std::string operator()(const yang::Binary)
111 {
112 return "a base64-encoded binary value";
113 }
114 std::string operator()(const yang::Enum&)
115 {
116 return "an enum";
117 }
118 std::string operator()(const yang::IdentityRef&)
119 {
120 return "an identity";
121 }
122 std::string operator()(const yang::LeafRef&)
123 {
124 return "a leafref";
125 }
Jan Kundrát379bb572020-05-07 03:23:13 +0200126 std::string operator()(const yang::Empty&)
127 {
128 return "an empty leaf";
129 }
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200130 std::string operator()(const yang::InstanceIdentifier&)
131 {
132 return "an instance identifier";
133 }
Václav Kubernát2984f442020-02-20 17:43:35 +0100134 std::string operator()(const yang::Union& type)
135 {
136 std::ostringstream ss;
137 std::transform(type.m_unionTypes.begin(), type.m_unionTypes.end(), std::experimental::make_ostream_joiner(ss, ", "), [this](const auto& unionType) {
Václav Kubernát13b23d72020-04-16 21:49:51 +0200138 return std::visit(*this, unionType.m_type);
Václav Kubernát2984f442020-02-20 17:43:35 +0100139 });
140 return ss.str();
141 }
Václav Kubernátdab73ca2020-10-26 23:44:43 +0100142 std::string operator()(const yang::Bits& type)
143 {
144 std::ostringstream ss;
145 ss << "bits {";
146 std::copy(type.m_allowedValues.begin(), type.m_allowedValues.end(), std::experimental::make_ostream_joiner(ss, ", "));
147 ss << "}";
148 return ss.str();
149 }
Václav Kubernát3a99f002020-03-31 02:27:41 +0200150};
151
152std::string leafDataTypeToString(const yang::LeafDataType& type)
153{
154 return std::visit(impl_leafDataTypeToString{}, type);
Václav Kubernát0b0272f2018-06-13 14:13:08 +0200155}
Václav Kubernát744f57f2018-06-29 22:46:26 +0200156
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200157std::string fullNodeName(const schemaPath_& location, const ModuleNodePair& pair)
Václav Kubernát744f57f2018-06-29 22:46:26 +0200158{
159 if (!pair.first) {
160 return location.m_nodes.at(0).m_prefix.value().m_name + ":" + pair.second;
161 } else {
162 return pair.first.value() + ":" + pair.second;
163 }
164}
Václav Kubernát2eaceb82018-10-08 19:56:30 +0200165
166std::string fullNodeName(const dataPath_& location, const ModuleNodePair& pair)
167{
168 return fullNodeName(dataPathToSchemaPath(location), pair);
169}
Václav Kubernát4108e0d2018-10-29 13:32:22 +0100170
Václav Kubernát9b725992019-05-29 16:39:47 +0200171struct leafDataToStringVisitor : boost::static_visitor<std::string> {
172 std::string operator()(const enum_& data) const
173 {
174 return data.m_value;
175 }
176
177 std::string operator()(const binary_& data) const
178 {
179 return data.m_value;
180 }
181
Jan Kundrát379bb572020-05-07 03:23:13 +0200182 std::string operator()(const empty_) const
183 {
184 return "[empty]";
185 }
186
Václav Kubernát9b725992019-05-29 16:39:47 +0200187 std::string operator()(const identityRef_& data) const
188 {
Jan Kundrát0d8abd12020-05-07 02:00:14 +0200189 return data.m_prefix ? (data.m_prefix.value().m_name + ":" + data.m_value) : data.m_value;
Václav Kubernát9b725992019-05-29 16:39:47 +0200190 }
191
Václav Kubernát144729d2020-01-08 15:20:35 +0100192 std::string operator()(const special_& data) const
193 {
194 return specialValueToString(data);
195 }
196
Jan Kundrátc43acf72019-07-02 19:28:22 +0200197 std::string operator()(const std::string& data) const
198 {
199 return data;
200 }
201
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200202 std::string operator()(const bool& data) const
203 {
Václav Kubernát3a433232020-07-08 17:52:50 +0200204 if (data) {
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200205 return "true";
Václav Kubernát3a433232020-07-08 17:52:50 +0200206 } else {
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200207 return "false";
Václav Kubernát3a433232020-07-08 17:52:50 +0200208 }
Václav Kubernát8e121ff2019-10-15 15:47:45 +0200209 }
210
Václav Kubernát19097f32020-10-05 10:08:29 +0200211 std::string operator()(const bits_& data) const
212 {
213 std::stringstream ss;
214 std::copy(data.m_bits.begin(), data.m_bits.end(), std::experimental::make_ostream_joiner(ss, " "));
215 return ss.str();
216 }
217
Jan Kundrátbb7aa852023-08-30 11:51:43 +0200218 std::string operator()(const instanceIdentifier_& data) const
219 {
220 return data.m_xpath;
221 }
222
Václav Kubernát9b725992019-05-29 16:39:47 +0200223 template <typename T>
224 std::string operator()(const T& data) const
225 {
Jan Kundrátc43acf72019-07-02 19:28:22 +0200226 return std::to_string(data);
Václav Kubernát9b725992019-05-29 16:39:47 +0200227 }
228};
229
230std::string leafDataToString(const leaf_data_ value)
231{
232 return boost::apply_visitor(leafDataToStringVisitor(), value);
233}
Václav Kubernát3a823f42020-04-29 23:40:21 +0200234
235struct getSchemaPathVisitor : boost::static_visitor<schemaPath_> {
236 schemaPath_ operator()(const dataPath_& path) const
237 {
238 return dataPathToSchemaPath(path);
239 }
240
241 schemaPath_ operator()(const schemaPath_& path) const
242 {
243 return path;
244 }
245
246 [[noreturn]] schemaPath_ operator()([[maybe_unused]] const module_& path) const
247 {
248 throw std::logic_error("getSchemaPathVisitor: Tried getting a schema path from a module");
249 }
250};
251
252schemaPath_ anyPathToSchemaPath(const boost::variant<dataPath_, schemaPath_, module_>& path)
253{
254 return boost::apply_visitor(getSchemaPathVisitor(), path);
255}
Václav Kubernát5b8a8f32020-05-20 00:57:22 +0200256
257std::string stripLeafListValueFromPath(const std::string& path)
258{
259 auto res = path;
260 res.erase(res.find_last_of('['));
261 return res;
262}
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200263
264std::string stripLastListInstanceFromPath(const std::string& path)
265{
266 auto res = path;
267 res.erase(res.find_first_of('[', res.find_last_of('/')));
268 return res;
269}
270
Václav Kubernát2c4778b2020-06-18 11:55:20 +0200271std::string instanceToString(const ListInstance& instance, const std::optional<std::string>& modName)
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200272{
273 std::string instanceStr;
Václav Kubernát2c4778b2020-06-18 11:55:20 +0200274 auto modulePrefix = modName ? *modName + ":" : "";
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200275 for (const auto& [key, value] : instance) {
276 using namespace std::string_literals;
Václav Kubernát2c4778b2020-06-18 11:55:20 +0200277 instanceStr += "[" + modulePrefix + key + "=" + escapeListKeyString(leafDataToString(value)) + "]";
Václav Kubernátbf65dd72020-05-28 02:32:31 +0200278 }
279 return instanceStr;
280}