summaryrefslogtreecommitdiffstats
path: root/src/gnmi/sysrepoapi.cpp
blob: dcf766886595634b64a22e99ceac2a15a1c9210a (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (c) 2019 PANTHEON.tech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "sysrepoapi.h"
#include "log.h"

#include <exception>
#include <iostream>

void event_notif_cb(const sr_ev_notif_type_t notif_type,
                    const char *xpath, const sr_val_t *values,
                    const size_t value_cnt, time_t timestamp,
                    void *private_ct)
{
    if (NULL == xpath || NULL == values || NULL == private_ct) {
        ERROR("Error, NULL detect.");
        return;
    }

    SysrepoAPI *sapi = (SysrepoAPI*) private_ct;

    for (size_t i = 0; i < value_cnt; i++ ) {
        sapi->setOutVal(*(values + i));
    }

    sapi->sendEvent(sapi);
}

SysrepoAPI::SysrepoAPI()
{
    sr_log_stderr(SR_LL_DBG);
}

SysrepoAPI::~SysrepoAPI()
{
    if (nullptr != subscription) {
        sr_unsubscribe(sess, subscription);
    }

    if (nullptr != sess) {
        sr_session_stop(sess);
    }

    if (nullptr != conn) {
        sr_disconnect(conn);
    }
}

void SysrepoAPI::setSysrepoName(const std::string& sysrepo_name)
{
    this->sysrepo_name = sysrepo_name;
}

void SysrepoAPI::connect()
{
    int rc = sr_connect(sysrepo_name.c_str(), SR_CONN_DEFAULT, &conn);
    if (SR_ERR_OK != rc) {
        ERROR("Failed connect to sysrepo.");
        throw std::runtime_error("Failed connect to sysrepo.");
    }
}

void SysrepoAPI::createSession(sr_datastore_e sesType)
{
    int rc = SR_ERR_OK;

    if (nullptr == conn) {
        ERROR("Error, nullptr detect.");
        throw std::runtime_error("Error, nullptr detect.");
    }

    closeSession();

    rc = sr_session_start(conn, sesType, SR_SESS_DEFAULT, &sess);
    if (SR_ERR_OK != rc) {
        ERROR("Failed connect to sysrepo session.");
        throw std::runtime_error("Failed connect to sysrepo session.");
    }
}

void SysrepoAPI::closeSession()
{
    if (nullptr != sess) {
        sr_session_stop(sess);
        sess = nullptr;
    }
}

void SysrepoAPI::commit()
{
    int rc = SR_ERR_OK;

    if (nullptr == sess) {
        ERROR("Error, nullptr detect.");
        throw std::runtime_error("Error, nullptr detect.");
    }

    rc = sr_commit(sess);
    if (SR_ERR_OK != rc) {
        ERROR("Failed commit change to sysrepo.");
        throw std::runtime_error("Failed commit change to sysrepo.");
    }
}

void SysrepoAPI::addData(const gNMIData& data)
{
    inputData.push_back(data);
}

void SysrepoAPI::getItemMessage()
{
    if (nullptr == sess) {
        ERROR("Error, nullptr detect.");
        throw std::runtime_error("Error, nullptr detect.");
    }

    for (const auto &data : inputData) {
        sr_val_t *value = nullptr;
        sr_val_iter_t *iter = nullptr;
//         size_t count;
        DEBUG("Path: %s", data.getXPath(gNMIData::xPathType::sysrepoPath).c_str());

        int rc = sr_get_items_iter(sess, data.getXPath(gNMIData::xPathType::sysrepoPath).c_str(),
                             &iter);
//         int rc = sr_get_item(sess, data.getXPath(gNMIData::xPathType::sysrepoPath).c_str(),
//                              &value);
        if (SR_ERR_OK != rc) {
            ERROR("Failed get item from sysrepo.");
            throw std::runtime_error("Failed get item from sysrepo.");
        }

        while (SR_ERR_OK == sr_get_item_next(sess, iter, &value)) {
            sr_print_val(value);

//         sr_print_val(value);

        gNMIData oData;

        oData.setXPath(value->xpath, gNMIData::xPathType::sysrepoPath);
        oData.setValue(sr_val_to_str(value));

        outpuData.push_back(oData);

        sr_free_val(value);
        }
    }
}

void SysrepoAPI::setItemMessage()
{
    int rc = SR_ERR_OK;

//     sr_xpath_ctx_t state = {0};

    if (nullptr == sess) {
        ERROR("Error, nullptr detect.");
        throw std::runtime_error("Error, nullptr detect.");
    }

    for (const auto &data : inputData) {
        DEBUG("Set path: %s", data.getXPath(gNMIData::xPathType::sysrepoPath).c_str());
        DEBUG("Val: %s", data.getStr().c_str());

//         if (gNMIData::ValueType::dStringVal == data.dataType()) {
            rc = sr_set_item_str(sess,
                                 data.getXPath(gNMIData::xPathType::sysrepoPath).c_str(),
                                 data.getStr().c_str(), SR_EDIT_DEFAULT);
//         } else {
//             sr_val_t value = { 0 };
//             setSrVal(value, data);
// 
//             rc = sr_set_item(sess, data.getXPath(gNMIData::xPathType::sysrepoPath).c_str(),
//                              &value, SR_EDIT_DEFAULT);
//         }
        if (SR_ERR_OK != rc) {
            ERROR("Failed set item to sysrepo.");
            throw std::runtime_error("Failed set item to sysrepo.");
        }

        //TODO: Hmm, this is duplication copy, I`m not sure with this solution.
        outpuData.push_back(data);
    }
}

void SysrepoAPI::rpcSend(const std::string& xpath, sr_val_t &input)
{
    int rc = SR_ERR_OK;
    sr_val_t *output = nullptr;
    size_t output_cnt = 0;

    if (nullptr == sess) {
        ERROR("Error, nullptr detect.");
        throw std::runtime_error("Error, nullptr detect.");
    }

    rc = sr_rpc_send(sess, xpath.c_str(), &input, 1, &output, &output_cnt);
    if (SR_ERR_OK != rc) {
        ERROR("RPC message failed: %s.", sr_strerror(rc));
        throw std::runtime_error("RPC message failed.");
    }
}

void SysrepoAPI::eventSubscribeMessage()
{
    int rc = SR_ERR_OK;

    if (nullptr == sess) {
        ERROR("Error, nullptr detect.");
        throw std::runtime_error("Error, nullptr detect.");
    }

    for (const auto &data : inputData) {
        DEBUG("Register subscribe path: %s",
              data.getXPath(gNMIData::xPathType::sysrepoPath).c_str());
        rc = sr_event_notif_subscribe(sess,
                                      data.getXPath(gNMIData::xPathType::sysrepoPath).c_str(),
                                      event_notif_cb, this, SR_SUBSCR_DEFAULT,
                                      &subscription);
        if (SR_ERR_OK != rc) {
            ERROR("Event notification subscribe failed: %s.", sr_strerror(rc));
            throw std::runtime_error("Event notification subscribe failed.");
        }
    }
}

const std::list<SysrepSchema> &SysrepoAPI::getSchemas()
{
    int rc = SR_ERR_OK;
    sr_schema_t *schemas = nullptr;
    size_t schema_cnt = 0;

    if (nullptr == sess) {
        ERROR("Error, nullptr detect.");
        throw std::runtime_error("Error, nullptr detect.");
    }

    rc = sr_list_schemas(sess, &schemas, &schema_cnt);
    if (SR_ERR_OK != rc) {
        ERROR("List schemas failed: %s.", sr_strerror(rc));
        throw std::runtime_error("List schemas failed failed.");
    }

    for (size_t i = 0; i < schema_cnt; i++) {
//         DEBUG("Module: %s, xpath: %s, prefix: %s, r yang: %s, revison: %s",
//               schemas[i].module_name, schemas[i].ns, schemas[i].prefix,
//               schemas[i].revision.file_path_yang, schemas[i].revision.revision);
        SysrepSchema schema;

        if (NULL != schemas[i].module_name) {
            schema.moduleName = std::string(schemas[i].module_name);
        }

        if (NULL != schemas[i].revision.revision) {
            schema.revision = std::string(schemas[i].revision.revision);
        }
        schemaList.push_back(schema);
    }

    sr_free_schemas(schemas, schema_cnt);

    return schemaList;
}

std::list<gNMIData> SysrepoAPI::getOutputData() const
{
    return outpuData;
}

void SysrepoAPI::cleanData()
{
    inputData.clear();
    outpuData.clear();
    schemaList.clear();
}

enum sr_type_e SysrepoAPI::convergNMITypeToSysrepo(gNMIData::ValueType type)
{
    switch (type) {
        case gNMIData::ValueType::dStringVal:
            return SR_STRING_T;

        case gNMIData::ValueType::dIntVal:
            return SR_INT32_T;

        case gNMIData::ValueType::UnknownVal:
        default:
            return SR_UNKNOWN_T;
    }

    return SR_UNKNOWN_T;
}

gNMIData::ValueType SysrepoAPI::convergSysrepoTypeTogNMI(sr_type_e type)
{
    switch (type) {
        case SR_STRING_T:
            return gNMIData::ValueType::dStringVal;

        case SR_INT8_T:
        case SR_INT16_T:
        case SR_INT32_T:
        case SR_INT64_T:
        case SR_UINT8_T:
        case SR_UINT16_T:
        case SR_UINT32_T:
        case SR_UINT64_T:
            return gNMIData::ValueType::dIntVal;

        case SR_ANYDATA_T:
        case SR_ANYXML_T:
        case SR_BINARY_T:
        case SR_BITS_T:
        case SR_BOOL_T:
        case SR_CONTAINER_PRESENCE_T:
        case SR_CONTAINER_T:
        case SR_DECIMAL64_T:
        case SR_ENUM_T:
        case SR_IDENTITYREF_T:
        case SR_INSTANCEID_T:
        case SR_LEAF_EMPTY_T:
        case SR_LIST_T:
        case SR_TREE_ITERATOR_T:
        case SR_UNKNOWN_T:
            return gNMIData::ValueType::UnknownVal;

        default:
            return gNMIData::ValueType::UnknownVal;
    }

    return gNMIData::ValueType::UnknownVal;
}

void SysrepoAPI::setSrVal(sr_val_t& value, const gNMIData& gData)
{
    value.type = convergNMITypeToSysrepo(gData.dataType());

    switch (gData.dataType()) {
        case gNMIData::ValueType::dStringVal:
            value.data.string_val = (char *) gData.getStr().c_str();
            DEBUG("Val: %s", value.data.string_val);
            break;

        case gNMIData::ValueType::dIntVal:
            value.data.int32_val = gData.getInt();
            DEBUG("Val: %d", value.data.int32_val);
            break;

        case gNMIData::ValueType::UnknownVal:
        default:
            DEBUG("Unknown value.");
            //TODO:
            break;
    }
}

void SysrepoAPI::setOutVal(const sr_val_t& value)
{
//     auto key = convergSysrepoTypeTogNMI(value.type);
    std::string str;

    //TODO: Need rewrite, only for test
    switch (value.type) {
        case SR_STRING_T:
            str = std::string(value.data.string_val);
            break;

        case SR_INT8_T:
            str = std::to_string(value.data.int8_val);
            break;

        case SR_INT16_T:
            str = std::to_string(value.data.int16_val);
            break;

        case SR_INT32_T:
            str = std::to_string(value.data.int32_val);
            break;

        case SR_INT64_T:
            str = std::to_string(value.data.int64_val);
            break;

        case SR_UINT8_T:
            str = std::to_string(value.data.uint8_val);
            break;
        case SR_UINT16_T:
            str = std::to_string(value.data.uint16_val);
            break;
        case SR_UINT32_T:
            str = std::to_string(value.data.uint32_val);
            break;
        case SR_UINT64_T:
            str = std::to_string(value.data.uint64_val);
            break;

        default:
            //TODO: Need implement.
            break;
    }

    gNMIData sData;
    sData.setXPath(value.xpath, gNMIData::xPathType::sysrepoPath);
    sData.setValue(str);

    outpuData.push_back(sData);
}