1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302 | // Copyright (C) 2018-2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <yang/adaptor.h>
#include <iostream><--- Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results.
using namespace std;
using namespace isc::data;
namespace isc {
namespace yang {
ConstElementPtr
Adaptor::getContext(ConstElementPtr parent) {
ConstElementPtr context = parent->get("user-context");
ConstElementPtr comment = parent->get("comment");
if (!comment) {
return (context);
}
ElementPtr result;
if (context) {
result = copy(context);
} else {
result = Element::createMap();
}
result->set("comment", comment);
return (result);
}
void
Adaptor::fromParent(const string& name, ConstElementPtr parent,
ConstElementPtr list) {
ConstElementPtr param = parent->get(name);
if (!param) {
return;
}
for (ElementPtr const& item : list->listValue()) {
// don't override. Skip this entry if it already has the parameter.
if (item->contains(name)) {
continue;
}
item->set(name, param);
}
}
void
Adaptor::toParent(const string& name, ElementPtr parent,
ConstElementPtr list) {
ConstElementPtr param;
bool first = true;
for (ElementPtr const& item : list->listValue()) {
if (first) {
first = false;
param = item->get(name);
} else if ((!param && item->contains(name)) ||
(param && !item->contains(name)) ||
(param && item->contains(name) &&
!param->equals(*item->get(name)))) {
isc_throw(BadValue,
"inconsistent value of " << name
<< " in " << list->str());
}
}
if (!first && param) {
for (ElementPtr const& item : list->listValue()) {
if (param) {<--- Condition 'param' is always true
item->remove(name);
}
}
parent->set(name, param);
}
}
namespace {
/// @brief Apply insert.
///
/// This function applies an insert action at the given scope:
/// - in a map it adds the value at the key when the key does not exist
/// - in a list it appends the value
/// - in other scope type it does nothing
/// @note a copy of the value is used to avoid unwanted sharing/side effects.
///
/// @param key The key of the modification.
/// @param value The value of the modification.
/// @param scope The place to apply the insert.
void applyInsert(ConstElementPtr key, ConstElementPtr value,
ElementPtr scope) {
if (scope->getType() == Element::map) {
if (!key || !value || (key->getType() != Element::string)) {
return;
}
string name = key->stringValue();
if (!name.empty() && !scope->contains(name)) {
scope->set(name, copy(value));
}
} else if (scope->getType() == Element::list) {
if (value) {
scope->add(copy(value));
}
}
}
/// @brief Apply replace.
///
/// This function works only on map scopes and acts as insert at the
/// exception the new value is set even if the key already exists.
///
/// @param key The key of the modification.
/// @param value The value of the modification.
/// @param scope The place to apply the replace.
void applyReplace(ConstElementPtr key, ConstElementPtr value,
ElementPtr scope) {
if ((scope->getType() != Element::map) ||
!key || !value || (key->getType() != Element::string)) {
return;
}
string name = key->stringValue();
if (!name.empty()) {
scope->set(name, copy(value));
}
}
/// @brief Apply delete.
///
/// This function deletes the value designed by the key from the given scope:
/// - in a map the key is the key of the value to remove
/// - in a list the key is:
/// * when it is an integer the index of the value to remove
/// * when it is a map the key / value pair which belongs to the value
/// to remove
/// - in other scope type it does nothing
///
/// @param key The key item of the path.
/// @param scope The place to apply the delete.
void applyDelete(ConstElementPtr key, ElementPtr scope) {
if (scope->getType() == Element::map) {
if (!key || (key->getType() != Element::string)) {
return;
}
string name = key->stringValue();
if (!name.empty()) {
scope->remove(name);
}
} else if (scope->getType() == Element::list) {
if (!key) {
return;
} else if (key->getType() == Element::integer) {
int index = key->intValue();
if ((index >= 0) && (index < scope->size())) {
scope->remove(index);
}
} else if (key->getType() == Element::map) {
ConstElementPtr entry = key->get("key");
ConstElementPtr value = key->get("value");
if (!entry || !value || (entry->getType() != Element::string)) {
return;
}
string name = entry->stringValue();
if (name.empty()) {
return;
}
for (int i = 0; i < scope->size(); ++i) {
ElementPtr item = scope->getNonConst(i);
if (!item || (item->getType() != Element::map)) {
continue;
}
ConstElementPtr compare = item->get(name);
if (compare && value->equals(*compare)) {
scope->remove(i);
return;
}
}
}
}
}
/// @brief Apply action.
///
/// This function applies one action (insert, replace or delete) using
/// applyInsert, applyReplace or applyDelete.
///
/// @param actions The action list.
/// @param scope The current scope.
/// @param next The index of the next action.
void applyAction(ConstElementPtr actions, ElementPtr scope, size_t next) {
if (next == actions->size()) {
return;
}
ConstElementPtr action = actions->get(next);
++next;
if (!action || (action->getType() != Element::map) ||
!action->contains("action")) {
applyAction(actions, scope, next);
return;
}
string name = action->get("action")->stringValue();
if (name == "insert") {
applyInsert(action->get("key"), action->get("value"), scope);
} else if (name == "replace") {
applyReplace(action->get("key"), action->get("value"), scope);
} else if (name == "delete") {
applyDelete(action->get("key"), scope);
}
applyAction(actions, scope, next);
}
/// @brief Apply recursively actions following the path and going down
/// in the to-be-modified structure.
///
/// The recursive call when the end of the path is not reached is done:
/// - in a map on the value at the key
/// - in a list the next path item is:
/// * if it is a positive integer on the value at the index
/// * if it is -1 on all elements of the list
/// * if a map on the element where the key / value pair belongs to
///
/// @param path The search list.
/// @param actions The action list.
/// @param scope The current scope.
/// @param next The index of the next item to use in the path.
void applyDown(ConstElementPtr path, ConstElementPtr actions, ElementPtr scope,
size_t next) {
if (!scope) {
return;
}
if (next == path->size()) {
applyAction(actions, scope, 0);
return;
}
ConstElementPtr step = path->get(next);
++next;
if (scope->getType() == Element::map) {
if (!step || (step->getType() != Element::string)) {
return;
}
string name = step->stringValue();
if (name.empty() || !scope->contains(name)) {
return;
}
ConstElementPtr down(scope->get(name));
if (down) {
ElementPtr mutable_down(copy(down, 0));
applyDown(path, actions, mutable_down, next);
scope->set(name, mutable_down);
}
} else if (scope->getType() == Element::list) {
if (!step) {
return;
}
auto downs = scope->listValue();
if (step->getType() == Element::map) {
ConstElementPtr key = step->get("key");
ConstElementPtr value = step->get("value");
if (!key || !value || (key->getType() != Element::string)) {
return;
}
string name = key->stringValue();
if (name.empty()) {
return;
}
for (ElementPtr& down : downs) {
if (!down || (down->getType() != Element::map)) {
continue;
}
ConstElementPtr compare = down->get(name);
if (compare && value->equals(*compare)) {
applyDown(path, actions, down, next);
return;
}
}
} else if (step->getType() != Element::integer) {
return;
}
int index = step->intValue();
if (index == -1) {
for (ElementPtr& down : downs) {<--- Variable 'down' can be declared as reference to const
applyDown(path, actions, down, next);
}
} else if ((index >= 0) && (index < scope->size())) {
applyDown(path, actions, scope->getNonConst(index), next);
}
}
}
} //anonymous namespace
/// Apply recursively starting at the beginning of the path.
void
Adaptor::modify(ConstElementPtr path, ConstElementPtr actions,
ElementPtr config) {
applyDown(path, actions, config, 0);
}
} // namespace yang
} // namespace isc
|