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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
|
//
// Copyright (C) 2019 NetDEF, Inc.
// Renato Westphal
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; see the file COPYING; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
#include <zebra.h>
#include "log.h"
#include "libfrr.h"
#include "version.h"
#include "command.h"
#include "lib_errors.h"
#include "northbound.h"
#include "northbound_db.h"
#include <iostream>
#include <sstream>
#include <memory>
#include <string>
#include <grpcpp/grpcpp.h>
#include "grpc/frr-northbound.grpc.pb.h"
#define GRPC_DEFAULT_PORT 50051
/*
* NOTE: we can't use the FRR debugging infrastructure here since it uses
* atomics and C++ has a different atomics API. Enable gRPC debugging
* unconditionally until we figure out a way to solve this problem.
*/
static bool nb_dbg_client_grpc = 1;
static pthread_t grpc_pthread;
class NorthboundImpl final : public frr::Northbound::Service
{
public:
NorthboundImpl(void)
{
_nextCandidateId = 0;
}
~NorthboundImpl(void)
{
// Delete candidates.
for (auto it = _candidates.begin(); it != _candidates.end();
it++)
delete_candidate(&it->second);
}
grpc::Status
GetCapabilities(grpc::ServerContext *context,
frr::GetCapabilitiesRequest const *request,
frr::GetCapabilitiesResponse *response) override
{
if (nb_dbg_client_grpc)
zlog_debug("received RPC GetCapabilities()");
// Response: string frr_version = 1;
response->set_frr_version(FRR_VERSION);
// Response: bool rollback_support = 2;
#ifdef HAVE_CONFIG_ROLLBACKS
response->set_rollback_support(true);
#else
response->set_rollback_support(false);
#endif
// Response: repeated ModuleData supported_modules = 3;
struct yang_module *module;
RB_FOREACH (module, yang_modules, &yang_modules) {
auto m = response->add_supported_modules();
m->set_name(module->name);
if (module->info->rev_size)
m->set_revision(module->info->rev[0].date);
m->set_organization(module->info->org);
}
// Response: repeated Encoding supported_encodings = 4;
response->add_supported_encodings(frr::JSON);
response->add_supported_encodings(frr::XML);
return grpc::Status::OK;
}
grpc::Status Get(grpc::ServerContext *context,
frr::GetRequest const *request,
grpc::ServerWriter<frr::GetResponse> *writer) override
{
// Request: DataType type = 1;
int type = request->type();
// Request: Encoding encoding = 2;
frr::Encoding encoding = request->encoding();
// Request: bool with_defaults = 3;
bool with_defaults = request->with_defaults();
if (nb_dbg_client_grpc)
zlog_debug(
"received RPC Get(type: %u, encoding: %u, with_defaults: %u)",
type, encoding, with_defaults);
// Request: repeated string path = 4;
auto paths = request->path();
for (const std::string &path : paths) {
frr::GetResponse response;
grpc::Status status;
// Response: int64 timestamp = 1;
response.set_timestamp(time(NULL));
// Response: DataTree data = 2;
auto *data = response.mutable_data();
data->set_encoding(request->encoding());
status = get_path(data, path, type,
encoding2lyd_format(encoding),
with_defaults);
// Something went wrong...
if (!status.ok())
return status;
writer->Write(response);
}
if (nb_dbg_client_grpc)
zlog_debug("received RPC Get() end");
return grpc::Status::OK;
}
grpc::Status
CreateCandidate(grpc::ServerContext *context,
frr::CreateCandidateRequest const *request,
frr::CreateCandidateResponse *response) override
{
if (nb_dbg_client_grpc)
zlog_debug("received RPC CreateCandidate()");
struct candidate *candidate = create_candidate();
if (!candidate)
return grpc::Status(
grpc::StatusCode::RESOURCE_EXHAUSTED,
"Can't create candidate configuration");
// Response: uint32 candidate_id = 1;
response->set_candidate_id(candidate->id);
return grpc::Status::OK;
}
grpc::Status
DeleteCandidate(grpc::ServerContext *context,
frr::DeleteCandidateRequest const *request,
frr::DeleteCandidateResponse *response) override
{
// Request: uint32 candidate_id = 1;
uint32_t candidate_id = request->candidate_id();
if (nb_dbg_client_grpc)
zlog_debug(
"received RPC DeleteCandidate(candidate_id: %u)",
candidate_id);
struct candidate *candidate = get_candidate(candidate_id);
if (!candidate)
return grpc::Status(
grpc::StatusCode::NOT_FOUND,
"candidate configuration not found");
delete_candidate(candidate);
return grpc::Status::OK;
}
grpc::Status
UpdateCandidate(grpc::ServerContext *context,
frr::UpdateCandidateRequest const *request,
frr::UpdateCandidateResponse *response) override
{
// Request: uint32 candidate_id = 1;
uint32_t candidate_id = request->candidate_id();
if (nb_dbg_client_grpc)
zlog_debug(
"received RPC UpdateCandidate(candidate_id: %u)",
candidate_id);
struct candidate *candidate = get_candidate(candidate_id);
if (!candidate)
return grpc::Status(
grpc::StatusCode::NOT_FOUND,
"candidate configuration not found");
if (candidate->transaction)
return grpc::Status(
grpc::StatusCode::FAILED_PRECONDITION,
"candidate is in the middle of a transaction");
if (nb_candidate_update(candidate->config) != NB_OK)
return grpc::Status(
grpc::StatusCode::INTERNAL,
"failed to update candidate configuration");
return grpc::Status::OK;
}
grpc::Status
EditCandidate(grpc::ServerContext *context,
frr::EditCandidateRequest const *request,
frr::EditCandidateResponse *response) override
{
// Request: uint32 candidate_id = 1;
uint32_t candidate_id = request->candidate_id();
if (nb_dbg_client_grpc)
zlog_debug(
"received RPC EditCandidate(candidate_id: %u)",
candidate_id);
struct candidate *candidate = get_candidate(candidate_id);
if (!candidate)
return grpc::Status(
grpc::StatusCode::NOT_FOUND,
"candidate configuration not found");
// Create a copy of the candidate. For consistency, we need to
// ensure that either all changes are accepted or none are (in
// the event of an error).
struct nb_config *candidate_tmp =
nb_config_dup(candidate->config);
auto pvs = request->update();
for (const frr::PathValue &pv : pvs) {
if (yang_dnode_edit(candidate_tmp->dnode, pv.path(),
pv.value())
!= 0) {
nb_config_free(candidate_tmp);
return grpc::Status(
grpc::StatusCode::INVALID_ARGUMENT,
"Failed to update \"" + pv.path()
+ "\"");
}
}
pvs = request->delete_();
for (const frr::PathValue &pv : pvs) {
if (yang_dnode_delete(candidate_tmp->dnode, pv.path())
!= 0) {
nb_config_free(candidate_tmp);
return grpc::Status(
grpc::StatusCode::INVALID_ARGUMENT,
"Failed to remove \"" + pv.path()
+ "\"");
}
}
// No errors, accept all changes.
nb_config_replace(candidate->config, candidate_tmp, false);
return grpc::Status::OK;
}
grpc::Status
LoadToCandidate(grpc::ServerContext *context,
frr::LoadToCandidateRequest const *request,
frr::LoadToCandidateResponse *response) override
{
// Request: uint32 candidate_id = 1;
uint32_t candidate_id = request->candidate_id();
// Request: LoadType type = 2;
int load_type = request->type();
// Request: DataTree config = 3;
auto config = request->config();
if (nb_dbg_client_grpc)
zlog_debug(
"received RPC LoadToCandidate(candidate_id: %u)",
candidate_id);
struct candidate *candidate = get_candidate(candidate_id);
if (!candidate)
return grpc::Status(
grpc::StatusCode::NOT_FOUND,
"candidate configuration not found");
struct lyd_node *dnode = dnode_from_data_tree(&config, true);
if (!dnode)
return grpc::Status(
grpc::StatusCode::INTERNAL,
"Failed to parse the configuration");
struct nb_config *loaded_config = nb_config_new(dnode);
if (load_type == frr::LoadToCandidateRequest::REPLACE)
nb_config_replace(candidate->config, loaded_config,
false);
else if (nb_config_merge(candidate->config, loaded_config,
false)
!= NB_OK)
return grpc::Status(
grpc::StatusCode::INTERNAL,
"Failed to merge the loaded configuration");
return grpc::Status::OK;
}
grpc::Status Commit(grpc::ServerContext *context,
frr::CommitRequest const *request,
frr::CommitResponse *response) override
{
// Request: uint32 candidate_id = 1;
uint32_t candidate_id = request->candidate_id();
// Request: Phase phase = 2;
int phase = request->phase();
// Request: string comment = 3;
const std::string comment = request->comment();
if (nb_dbg_client_grpc)
zlog_debug("received RPC Commit(candidate_id: %u)",
candidate_id);
// Find candidate configuration.
struct candidate *candidate = get_candidate(candidate_id);
if (!candidate)
return grpc::Status(
grpc::StatusCode::NOT_FOUND,
"candidate configuration not found");
int ret = NB_OK;
uint32_t transaction_id = 0;
// Check for misuse of the two-phase commit protocol.
switch (phase) {
case frr::CommitRequest::PREPARE:
case frr::CommitRequest::ALL:
if (candidate->transaction)
return grpc::Status(
grpc::StatusCode::FAILED_PRECONDITION,
"pending transaction in progress");
break;
case frr::CommitRequest::ABORT:
case frr::CommitRequest::APPLY:
if (!candidate->transaction)
return grpc::Status(
grpc::StatusCode::FAILED_PRECONDITION,
"no transaction in progress");
break;
default:
break;
}
// Execute the user request.
switch (phase) {
case frr::CommitRequest::VALIDATE:
ret = nb_candidate_validate(candidate->config);
break;
case frr::CommitRequest::PREPARE:
ret = nb_candidate_commit_prepare(
candidate->config, NB_CLIENT_GRPC, NULL,
comment.c_str(), &candidate->transaction);
break;
case frr::CommitRequest::ABORT:
nb_candidate_commit_abort(candidate->transaction);
break;
case frr::CommitRequest::APPLY:
nb_candidate_commit_apply(candidate->transaction, true,
&transaction_id);
break;
case frr::CommitRequest::ALL:
ret = nb_candidate_commit(
candidate->config, NB_CLIENT_GRPC, NULL, true,
comment.c_str(), &transaction_id);
break;
}
// Map northbound error codes to gRPC error codes.
switch (ret) {
case NB_ERR_NO_CHANGES:
return grpc::Status(
grpc::StatusCode::ABORTED,
"No configuration changes detected");
case NB_ERR_LOCKED:
return grpc::Status(
grpc::StatusCode::UNAVAILABLE,
"There's already a transaction in progress");
case NB_ERR_VALIDATION:
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"Validation error");
case NB_ERR_RESOURCE:
return grpc::Status(
grpc::StatusCode::RESOURCE_EXHAUSTED,
"Failed do allocate resources");
case NB_ERR:
return grpc::Status(grpc::StatusCode::INTERNAL,
"Internal error");
default:
break;
}
// Response: uint32 transaction_id = 1;
if (transaction_id)
response->set_transaction_id(transaction_id);
return grpc::Status::OK;
}
grpc::Status
ListTransactions(grpc::ServerContext *context,
frr::ListTransactionsRequest const *request,
grpc::ServerWriter<frr::ListTransactionsResponse>
*writer) override
{
if (nb_dbg_client_grpc)
zlog_debug("received RPC ListTransactions()");
nb_db_transactions_iterate(list_transactions_cb, writer);
return grpc::Status::OK;
}
grpc::Status
GetTransaction(grpc::ServerContext *context,
frr::GetTransactionRequest const *request,
frr::GetTransactionResponse *response) override
{
struct nb_config *nb_config;
// Request: uint32 transaction_id = 1;
uint32_t transaction_id = request->transaction_id();
// Request: Encoding encoding = 2;
frr::Encoding encoding = request->encoding();
// Request: bool with_defaults = 3;
bool with_defaults = request->with_defaults();
if (nb_dbg_client_grpc)
zlog_debug(
"received RPC GetTransaction(transaction_id: %u, encoding: %u)",
transaction_id, encoding);
// Load configuration from the transactions database.
nb_config = nb_db_transaction_load(transaction_id);
if (!nb_config)
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"Transaction not found");
// Response: DataTree config = 1;
auto config = response->mutable_config();
config->set_encoding(encoding);
// Dump data using the requested format.
if (data_tree_from_dnode(config, nb_config->dnode,
encoding2lyd_format(encoding),
with_defaults)
!= 0) {
nb_config_free(nb_config);
return grpc::Status(grpc::StatusCode::INTERNAL,
"Failed to dump data");
}
nb_config_free(nb_config);
return grpc::Status::OK;
}
grpc::Status LockConfig(grpc::ServerContext *context,
frr::LockConfigRequest const *request,
frr::LockConfigResponse *response) override
{
if (nb_dbg_client_grpc)
zlog_debug("received RPC LockConfig()");
if (nb_running_lock(NB_CLIENT_GRPC, NULL))
return grpc::Status(
grpc::StatusCode::FAILED_PRECONDITION,
"running configuration is locked already");
return grpc::Status::OK;
}
grpc::Status UnlockConfig(grpc::ServerContext *context,
frr::UnlockConfigRequest const *request,
frr::UnlockConfigResponse *response) override
{
if (nb_dbg_client_grpc)
zlog_debug("received RPC UnlockConfig()");
if (nb_running_unlock(NB_CLIENT_GRPC, NULL))
return grpc::Status(
grpc::StatusCode::FAILED_PRECONDITION,
"failed to unlock the running configuration");
return grpc::Status::OK;
}
grpc::Status Execute(grpc::ServerContext *context,
frr::ExecuteRequest const *request,
frr::ExecuteResponse *response) override
{
struct nb_node *nb_node;
struct list *input_list;
struct list *output_list;
struct listnode *node;
struct yang_data *data;
const char *xpath;
// Request: string path = 1;
xpath = request->path().c_str();
if (nb_dbg_client_grpc)
zlog_debug("received RPC Execute(path: \"%s\")", xpath);
if (request->path().empty())
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"Data path is empty");
nb_node = nb_node_find(xpath);
if (!nb_node)
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT,
"Unknown data path");
input_list = yang_data_list_new();
output_list = yang_data_list_new();
// Read input parameters.
auto input = request->input();
for (const frr::PathValue &pv : input) {
// Request: repeated PathValue input = 2;
data = yang_data_new(pv.path().c_str(),
pv.value().c_str());
listnode_add(input_list, data);
}
// Execute callback registered for this XPath.
if (nb_node->cbs.rpc(xpath, input_list, output_list) != NB_OK) {
flog_warn(EC_LIB_NB_CB_RPC,
"%s: rpc callback failed: %s", __func__,
xpath);
list_delete(&input_list);
list_delete(&output_list);
return grpc::Status(grpc::StatusCode::INTERNAL,
"RPC failed");
}
// Process output parameters.
for (ALL_LIST_ELEMENTS_RO(output_list, node, data)) {
// Response: repeated PathValue output = 1;
frr::PathValue *pv = response->add_output();
pv->set_path(data->xpath);
pv->set_value(data->value);
}
// Release memory.
list_delete(&input_list);
list_delete(&output_list);
return grpc::Status::OK;
}
private:
struct candidate {
uint32_t id;
struct nb_config *config;
struct nb_transaction *transaction;
};
std::map<uint32_t, struct candidate> _candidates;
uint32_t _nextCandidateId;
static int yang_dnode_edit(struct lyd_node *dnode,
const std::string &path,
const std::string &value)
{
ly_errno = LY_SUCCESS;
dnode = lyd_new_path(dnode, ly_native_ctx, path.c_str(),
(void *)value.c_str(),
(LYD_ANYDATA_VALUETYPE)0,
LYD_PATH_OPT_UPDATE);
if (!dnode && ly_errno != LY_SUCCESS) {
flog_warn(EC_LIB_LIBYANG, "%s: lyd_new_path() failed",
__func__);
return -1;
}
return 0;
}
static int yang_dnode_delete(struct lyd_node *dnode,
const std::string &path)
{
dnode = yang_dnode_get(dnode, path.c_str());
if (!dnode)
return -1;
lyd_free(dnode);
return 0;
}
static LYD_FORMAT encoding2lyd_format(enum frr::Encoding encoding)
{
switch (encoding) {
case frr::JSON:
return LYD_JSON;
case frr::XML:
return LYD_XML;
}
}
static int get_oper_data_cb(const struct lys_node *snode,
struct yang_translator *translator,
struct yang_data *data, void *arg)
{
struct lyd_node *dnode = static_cast<struct lyd_node *>(arg);
int ret = yang_dnode_edit(dnode, data->xpath, data->value);
yang_data_free(data);
return (ret == 0) ? NB_OK : NB_ERR;
}
static void list_transactions_cb(void *arg, int transaction_id,
const char *client_name,
const char *date, const char *comment)
{
grpc::ServerWriter<frr::ListTransactionsResponse> *writer =
static_cast<grpc::ServerWriter<
frr::ListTransactionsResponse> *>(arg);
frr::ListTransactionsResponse response;
// Response: uint32 id = 1;
response.set_id(transaction_id);
// Response: string client = 2;
response.set_client(client_name);
// Response: string date = 3;
response.set_date(date);
// Response: string comment = 4;
response.set_comment(comment);
writer->Write(response);
}
static int data_tree_from_dnode(frr::DataTree *dt,
const struct lyd_node *dnode,
LYD_FORMAT lyd_format,
bool with_defaults)
{
char *strp;
int options = 0;
SET_FLAG(options, LYP_FORMAT | LYP_WITHSIBLINGS);
if (with_defaults)
SET_FLAG(options, LYP_WD_ALL);
else
SET_FLAG(options, LYP_WD_TRIM);
if (lyd_print_mem(&strp, dnode, lyd_format, options) == 0) {
if (strp) {
dt->set_data(strp);
free(strp);
}
return 0;
}
return -1;
}
static struct lyd_node *dnode_from_data_tree(const frr::DataTree *dt,
bool config_only)
{
struct lyd_node *dnode;
int options;
if (config_only)
options = LYD_OPT_CONFIG;
else
options = LYD_OPT_DATA | LYD_OPT_DATA_NO_YANGLIB;
dnode = lyd_parse_mem(ly_native_ctx, dt->data().c_str(),
encoding2lyd_format(dt->encoding()),
options);
return dnode;
}
static struct lyd_node *get_dnode_config(const std::string &path)
{
struct lyd_node *dnode;
pthread_rwlock_rdlock(&running_config->lock);
{
dnode = yang_dnode_get(running_config->dnode,
path.empty() ? NULL
: path.c_str());
if (dnode)
dnode = yang_dnode_dup(dnode);
}
pthread_rwlock_unlock(&running_config->lock);
return dnode;
}
static struct lyd_node *get_dnode_state(const std::string &path)
{
struct lyd_node *dnode;
dnode = yang_dnode_new(ly_native_ctx, false);
if (nb_oper_data_iterate(path.c_str(), NULL, 0,
get_oper_data_cb, dnode)
!= NB_OK) {
yang_dnode_free(dnode);
return NULL;
}
return dnode;
}
static grpc::Status get_path(frr::DataTree *dt, const std::string &path,
int type, LYD_FORMAT lyd_format,
bool with_defaults)
{
struct lyd_node *dnode_config = NULL;
struct lyd_node *dnode_state = NULL;
struct lyd_node *dnode_final;
// Configuration data.
if (type == frr::GetRequest_DataType_ALL
|| type == frr::GetRequest_DataType_CONFIG) {
dnode_config = get_dnode_config(path);
if (!dnode_config)
return grpc::Status(
grpc::StatusCode::INVALID_ARGUMENT,
"Data path not found");
}
// Operational data.
if (type == frr::GetRequest_DataType_ALL
|| type == frr::GetRequest_DataType_STATE) {
dnode_state = get_dnode_state(path);
if (!dnode_state) {
if (dnode_config)
yang_dnode_free(dnode_config);
return grpc::Status(
grpc::StatusCode::INVALID_ARGUMENT,
"Failed to fetch operational data");
}
}
switch (type) {
case frr::GetRequest_DataType_ALL:
//
// Combine configuration and state data into a single
// dnode.
//
if (lyd_merge(dnode_state, dnode_config,
LYD_OPT_EXPLICIT)
!= 0) {
yang_dnode_free(dnode_state);
yang_dnode_free(dnode_config);
return grpc::Status(
grpc::StatusCode::INTERNAL,
"Failed to merge configuration and state data");
}
dnode_final = dnode_state;
break;
case frr::GetRequest_DataType_CONFIG:
dnode_final = dnode_config;
break;
case frr::GetRequest_DataType_STATE:
dnode_final = dnode_state;
break;
}
// Validate data to create implicit default nodes if necessary.
int validate_opts = 0;
if (type == frr::GetRequest_DataType_CONFIG)
validate_opts = LYD_OPT_CONFIG;
else
validate_opts = LYD_OPT_DATA | LYD_OPT_DATA_NO_YANGLIB;
lyd_validate(&dnode_final, validate_opts, ly_native_ctx);
// Dump data using the requested format.
int ret = data_tree_from_dnode(dt, dnode_final, lyd_format,
with_defaults);
yang_dnode_free(dnode_final);
if (ret != 0)
return grpc::Status(grpc::StatusCode::INTERNAL,
"Failed to dump data");
return grpc::Status::OK;
}
struct candidate *create_candidate(void)
{
uint32_t candidate_id = ++_nextCandidateId;
// Check for overflow.
// TODO: implement an algorithm for unique reusable IDs.
if (candidate_id == 0)
return NULL;
struct candidate *candidate = &_candidates[candidate_id];
candidate->id = candidate_id;
pthread_rwlock_rdlock(&running_config->lock);
{
candidate->config = nb_config_dup(running_config);
}
pthread_rwlock_unlock(&running_config->lock);
candidate->transaction = NULL;
return candidate;
}
void delete_candidate(struct candidate *candidate)
{
_candidates.erase(candidate->id);
nb_config_free(candidate->config);
if (candidate->transaction)
nb_candidate_commit_abort(candidate->transaction);
}
struct candidate *get_candidate(uint32_t candidate_id)
{
struct candidate *candidate;
if (_candidates.count(candidate_id) == 0)
return NULL;
return &_candidates[candidate_id];
}
};
static void *grpc_pthread_start(void *arg)
{
unsigned long *port = static_cast<unsigned long *>(arg);
NorthboundImpl service;
std::stringstream server_address;
server_address << "0.0.0.0:" << *port;
grpc::ServerBuilder builder;
builder.AddListeningPort(server_address.str(),
grpc::InsecureServerCredentials());
builder.RegisterService(&service);
std::unique_ptr<grpc::Server> server(builder.BuildAndStart());
zlog_notice("gRPC server listening on %s",
server_address.str().c_str());
server->Wait();
return NULL;
}
static int frr_grpc_init(unsigned long *port)
{
/* Create a pthread for gRPC since it runs its own event loop. */
if (pthread_create(&grpc_pthread, NULL, grpc_pthread_start, port)) {
flog_err(EC_LIB_SYSTEM_CALL, "%s: error creating pthread: %s",
__func__, safe_strerror(errno));
return -1;
}
pthread_detach(grpc_pthread);
return 0;
}
static int frr_grpc_finish(void)
{
// TODO: cancel the gRPC pthreads gracefully.
return 0;
}
static int frr_grpc_module_late_init(struct thread_master *tm)
{
static unsigned long port = GRPC_DEFAULT_PORT;
const char *args = THIS_MODULE->load_args;
// Parse port number.
if (args) {
try {
port = std::stoul(args);
if (port < 1024)
throw std::invalid_argument(
"can't use privileged port");
if (port > UINT16_MAX)
throw std::invalid_argument(
"port number is too big");
} catch (std::exception &e) {
flog_err(EC_LIB_GRPC_INIT,
"%s: failed to parse port number: %s",
__func__, e.what());
goto error;
}
}
if (frr_grpc_init(&port) < 0)
goto error;
hook_register(frr_fini, frr_grpc_finish);
return 0;
error:
flog_err(EC_LIB_GRPC_INIT, "failed to initialize the gRPC module");
return -1;
}
static int frr_grpc_module_init(void)
{
hook_register(frr_late_init, frr_grpc_module_late_init);
return 0;
}
FRR_MODULE_SETUP(.name = "frr_grpc", .version = FRR_VERSION,
.description = "FRR gRPC northbound module",
.init = frr_grpc_module_init, )
|