summaryrefslogtreecommitdiffstats
path: root/src/lib/yang/translator.cc
blob: 9e38cfa3d7dfe3fa66d2180913cfaaa38492b495 (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (C) 2018-2019,2021 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/translator.h>
#include <util/encode/base64.h>
#include <cstring>

using namespace std;
using namespace isc::data;
using namespace isc::util::encode;
using namespace sysrepo;

namespace {

string encode64(const string& input) {
    vector<uint8_t> binary;
    binary.resize(input.size());
    memmove(&binary[0], input.c_str(), binary.size());
    return (encodeBase64(binary));
}

string decode64(const string& input) {
    vector<uint8_t> binary;
    decodeBase64(input, binary);
    string result;
    result.resize(binary.size());
    memmove(&result[0], &binary[0], result.size());
    return (result);
}

} // end of anonymous namespace

namespace isc {
namespace yang {

TranslatorBasic::TranslatorBasic(S_Session session, const string& model)
    : session_(session), model_(model) {
}

TranslatorBasic::~TranslatorBasic() {
}

ElementPtr
TranslatorBasic::value(sysrepo::S_Val s_val) {
    if (!s_val) {
        isc_throw(BadValue, "value called with null");
    }
    switch (s_val->type()) {
    case SR_CONTAINER_T:
    case SR_CONTAINER_PRESENCE_T:
        // Internal node.
        return (ElementPtr());

    case SR_LIST_T:
        return (Element::createList());

    case SR_STRING_T:
        return (Element::create(string(s_val->data()->get_string())));

    case SR_BOOL_T:
        return (Element::create(s_val->data()->get_bool() ? true : false));

    case SR_UINT8_T:
        return (Element::create(static_cast<long long>(s_val->data()->get_uint8())));

    case SR_UINT16_T:
        return (Element::create(static_cast<long long>(s_val->data()->get_uint16())));

    case SR_UINT32_T:
        return (Element::create(static_cast<long long>(s_val->data()->get_uint32())));

    case SR_INT8_T:
        return (Element::create(static_cast<long long>(s_val->data()->get_int8())));

    case SR_INT16_T:
        return (Element::create(static_cast<long long>(s_val->data()->get_int16())));

    case SR_INT32_T:
        return (Element::create(static_cast<long long>(s_val->data()->get_int32())));

    case SR_DECIMAL64_T:
        return (Element::create(s_val->data()->get_decimal64()));

    case SR_IDENTITYREF_T:
        return (Element::create(string(s_val->data()->get_identityref())));

    case SR_ENUM_T:
        return (Element::create(string(s_val->data()->get_enum())));

    case SR_BINARY_T:
        return (Element::create(decode64(s_val->data()->get_binary())));

    default:
        isc_throw(NotImplemented,
                  "value called with unsupported type: " << s_val->type());
    }
}

ElementPtr
TranslatorBasic::getItem(const string& xpath) {
    S_Val s_val;
    try {
        s_val = session_->get_item(xpath.c_str());
    } catch (const sysrepo_exception& ex) {
        if (std::string(ex.what()).find("Item not found") != string::npos) {
            // The YANG configuration node was not there.
            return nullptr;
        }
        isc_throw(SysrepoError, "sysrepo error getting item at '" << xpath
                  << "': " << ex.what());
    }
    if (!s_val) {
        return (ElementPtr());
    }
    return (value(s_val));
}

ElementPtr
TranslatorBasic::getItems(const string& xpath) {
    S_Vals s_vals(getValuesFromItems(xpath));
    if (!s_vals) {
        return (ElementPtr());
    }
    ElementPtr result(Element::createList());
    try {
        for (size_t i = 0; i < s_vals->val_cnt(); ++i) {
            S_Val s_val = s_vals->val(i);
            result->add(value(s_val));
        }
    } catch (const sysrepo_exception& ex) {
        isc_throw(SysrepoError, "sysrepo error getting item at '"
                                    << xpath << "': " << ex.what());
    }
    return (result);
}

S_Val
TranslatorBasic::value(ConstElementPtr elem, sr_type_t type) {
    if (!elem) {
        isc_throw(BadValue, "value called with null");
    }
    S_Val s_val;
    switch (type) {
    case SR_CONTAINER_T:
    case SR_CONTAINER_PRESENCE_T:
        isc_throw(NotImplemented, "value called for a container");

    case SR_LIST_T:
        if (elem->getType() != Element::list) {
            isc_throw(BadValue, "value for a list called with not a list: "
                      << elem->str());
        }
        // Return null.
        break;

    case SR_STRING_T:
    case SR_IDENTITYREF_T:
    case SR_ENUM_T:
        if (elem->getType() != Element::string) {
            isc_throw(BadValue,
                      "value for a string called with not a string: "
                      << elem->str());
        }
        s_val.reset(new Val(elem->stringValue().c_str(), type));
        break;

    case SR_BOOL_T:
        if (elem->getType() != Element::boolean) {
            isc_throw(BadValue,
                      "value for a boolean called with not a boolean: "
                      << elem->str());
        }
        s_val.reset(new Val(elem->boolValue(), type));
        break;

    case SR_UINT8_T:
        if (elem->getType() != Element::integer) {
            isc_throw(BadValue,
                      "value for an integer called with not an integer: "
                      << elem->str());
        }
        s_val.reset(new Val(elem->intValue(), type));
        break;

    case SR_UINT16_T:
        if (elem->getType() != Element::integer) {
            isc_throw(BadValue,
                      "value for an integer called with not an integer: "
                      << elem->str());
        }
        s_val.reset(new Val(elem->intValue(), type));
        break;

    case SR_UINT32_T:
        if (elem->getType() != Element::integer) {
            isc_throw(BadValue,
                      "value for an integer called with not an integer: "
                      << elem->str());
        }
        s_val.reset(new Val(elem->intValue(), type));
        break;

    case SR_INT8_T:
        if (elem->getType() != Element::integer) {
            isc_throw(BadValue,
                      "value for an integer called with not an integer: "
                      << elem->str());
        }
        s_val.reset(new Val(elem->intValue(), type));
        break;

    case SR_INT16_T:
        if (elem->getType() != Element::integer) {
            isc_throw(BadValue,
                      "value for an integer called with not an integer: "
                      << elem->str());
        }
        s_val.reset(new Val(elem->intValue(), type));
        break;

    case SR_INT32_T:
        if (elem->getType() != Element::integer) {
            isc_throw(BadValue,
                      "value for an integer called with not an integer: "
                      << elem->str());
        }
        s_val.reset(new Val(elem->intValue(), type));
        break;

    case SR_DECIMAL64_T:
        if (elem->getType() != Element::real) {
            isc_throw(BadValue, "value for a real called with not a real");
        }
        s_val.reset(new Val(elem->doubleValue()));
        break;

    case SR_BINARY_T:
        if (elem->getType() != Element::string) {
            isc_throw(BadValue,
                      "value for a base64 called with not a string: "
                      << elem->str());
        }
        s_val.reset(new Val(encode64(elem->stringValue()).c_str(), type));
        break;

    default:
        isc_throw(NotImplemented,
                  "value called with unsupported type: " << type);
    }

    return (s_val);
}

void
TranslatorBasic::setItem(const string& xpath, ConstElementPtr elem,
                         sr_type_t type) {
    S_Val s_val = value(elem, type);
    try {
        session_->set_item(xpath.c_str(), s_val);
    } catch (const sysrepo_exception& ex) {
        isc_throw(SysrepoError,
                  "sysrepo error setting item '" << elem->str()
                  << "' at '" << xpath << "': " << ex.what());
    }
    session_->apply_changes();
}

void
TranslatorBasic::checkAndGetLeaf(ElementPtr& storage,
                                 const std::string& xpath,
                                 const std::string& name) {
    ConstElementPtr x = getItem(xpath + "/" + name);
    if (x) {
        storage->set(name, x);
    }
}

void TranslatorBasic::checkAndSetLeaf(ConstElementPtr const& from,
                                      string const& xpath,
                                      string const& name,
                                      sr_type_t const type) {
    ConstElementPtr const& x(from->get(name));
    if (x) {
        setItem(xpath + "/" + name, x, type);
    }
}

void
TranslatorBasic::delItem(const std::string& xpath) {
    try {
        session_->delete_item(xpath.c_str());
    } catch (const sysrepo_exception& ex) {
        if (std::string(ex.what()).find("Invalid argument") != string::npos) {
            // The YANG configuration node was not there.
            return;
        }
        isc_throw(SysrepoError,
                  "sysrepo error deleting item at '"
                  << xpath << "': " << ex.what());
    }
    session_->apply_changes();
}

S_Vals TranslatorBasic::getValuesFromItems(std::string const& xpath) {
    try {
        return session_->get_items(xpath.c_str());
    } catch (sysrepo::sysrepo_exception const& exception) {
        isc_throw(SysrepoError, "sysrepo error getting items: " << exception.what());
    }
}

}  // namespace yang
}  // namespace isc