1 /* 2 * Copyright (C) 2018 NetDEF, Inc. 3 * Renato Westphal 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License as published by the Free 7 * Software Foundation; either version 2 of the License, or (at your option) 8 * any later version. 9 * 10 * This program is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 * more details. 14 * 15 * You should have received a copy of the GNU General Public License along 16 * with this program; see the file COPYING; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 20 #ifndef _FRR_YANG_H_ 21 #define _FRR_YANG_H_ 22 23 #include "memory.h" 24 25 #include <libyang/libyang.h> 26 #ifdef HAVE_SYSREPO 27 #include <sysrepo.h> 28 #endif 29 30 #include "yang_wrappers.h" 31 32 #ifdef __cplusplus 33 extern "C" { 34 #endif 35 36 /* Maximum XPath length. */ 37 #define XPATH_MAXLEN 1024 38 39 /* Maximum list key length. */ 40 #define LIST_MAXKEYS 8 41 42 /* Maximum list key length. */ 43 #define LIST_MAXKEYLEN 128 44 45 /* Maximum string length of an YANG value. */ 46 #define YANG_VALUE_MAXLEN 1024 47 48 struct yang_module_embed { 49 struct yang_module_embed *next; 50 const char *mod_name, *mod_rev; 51 const char *sub_mod_name; 52 const char *sub_mod_rev; 53 const char *data; 54 LYS_INFORMAT format; 55 }; 56 57 struct yang_module { 58 RB_ENTRY(yang_module) entry; 59 const char *name; 60 const struct lys_module *info; 61 #ifdef HAVE_CONFD 62 int confd_hash; 63 #endif 64 #ifdef HAVE_SYSREPO 65 sr_subscription_ctx_t *sr_subscription; 66 struct thread *sr_thread; 67 #endif 68 }; 69 RB_HEAD(yang_modules, yang_module); 70 RB_PROTOTYPE(yang_modules, yang_module, entry, yang_module_compare); 71 72 struct yang_data { 73 /* XPath identifier of the data element. */ 74 char xpath[XPATH_MAXLEN]; 75 76 /* Value encoded as a raw string. */ 77 char *value; 78 }; 79 80 struct yang_list_keys { 81 /* Number os keys (max: LIST_MAXKEYS). */ 82 uint8_t num; 83 84 /* Value encoded as a raw string. */ 85 char key[LIST_MAXKEYS][LIST_MAXKEYLEN]; 86 }; 87 88 enum yang_path_type { 89 YANG_PATH_SCHEMA = 0, 90 YANG_PATH_DATA, 91 }; 92 93 enum yang_iter_flags { 94 /* Filter non-presence containers. */ 95 YANG_ITER_FILTER_NPCONTAINERS = (1<<0), 96 97 /* Filter list keys (leafs). */ 98 YANG_ITER_FILTER_LIST_KEYS = (1<<1), 99 100 /* Filter RPC input/output nodes. */ 101 YANG_ITER_FILTER_INPUT_OUTPUT = (1<<2), 102 103 /* Filter implicitely created nodes. */ 104 YANG_ITER_FILTER_IMPLICIT = (1<<3), 105 106 /* Allow iteration over augmentations. */ 107 YANG_ITER_ALLOW_AUGMENTATIONS = (1<<4), 108 }; 109 110 /* Callback used by the yang_snodes_iterate_*() family of functions. */ 111 typedef int (*yang_iterate_cb)(const struct lys_node *snode, void *arg); 112 113 /* Callback used by the yang_dnode_iterate() function. */ 114 typedef int (*yang_dnode_iter_cb)(const struct lyd_node *dnode, void *arg); 115 116 /* Return values of the 'yang_iterate_cb' callback. */ 117 #define YANG_ITER_CONTINUE 0 118 #define YANG_ITER_STOP -1 119 120 /* Global libyang context for native FRR models. */ 121 extern struct ly_ctx *ly_native_ctx; 122 123 /* Tree of all loaded YANG modules. */ 124 extern struct yang_modules yang_modules; 125 126 /* 127 * Create a new YANG module and load it using libyang. If the YANG module is not 128 * found in the YANG_MODELS_PATH directory, the program will exit with an error. 129 * Once loaded, a YANG module can't be unloaded anymore. 130 * 131 * module_name 132 * Name of the YANG module. 133 * 134 * Returns: 135 * Pointer to newly created YANG module. 136 */ 137 extern struct yang_module *yang_module_load(const char *module_name); 138 139 /* 140 * Load all FRR native YANG models. 141 */ 142 extern void yang_module_load_all(void); 143 144 /* 145 * Find a YANG module by its name. 146 * 147 * module_name 148 * Name of the YANG module. 149 * 150 * Returns: 151 * Pointer to YANG module if found, NULL otherwise. 152 */ 153 extern struct yang_module *yang_module_find(const char *module_name); 154 155 /* 156 * Register a YANG module embedded in the binary file. Should be called 157 * from a constructor function. 158 * 159 * embed 160 * YANG module embedding structure to register. (static global provided 161 * by caller.) 162 */ 163 extern void yang_module_embed(struct yang_module_embed *embed); 164 165 /* 166 * Iterate recursively over all children of a schema node. 167 * 168 * snode 169 * YANG schema node to operate on. 170 * 171 * cb 172 * Function to call with each schema node. 173 * 174 * flags 175 * YANG_ITER_* flags to control how the iteration is performed. 176 * 177 * arg 178 * Arbitrary argument passed as the second parameter in each call to 'cb'. 179 * 180 * Returns: 181 * The return value of the last called callback. 182 */ 183 extern int yang_snodes_iterate_subtree(const struct lys_node *snode, 184 yang_iterate_cb cb, uint16_t flags, 185 void *arg); 186 187 /* 188 * Iterate over all libyang schema nodes from the given YANG module. 189 * 190 * module 191 * YANG module to operate on. 192 * 193 * cb 194 * Function to call with each schema node. 195 * 196 * flags 197 * YANG_ITER_* flags to control how the iteration is performed. 198 * 199 * arg 200 * Arbitrary argument passed as the second parameter in each call to 'cb'. 201 * 202 * Returns: 203 * The return value of the last called callback. 204 */ 205 extern int yang_snodes_iterate_module(const struct lys_module *module, 206 yang_iterate_cb cb, uint16_t flags, 207 void *arg); 208 209 /* 210 * Iterate over all libyang schema nodes from all loaded YANG modules. 211 * 212 * cb 213 * Function to call with each schema node. 214 * 215 * flags 216 * YANG_ITER_* flags to control how the iteration is performed. 217 * 218 * arg 219 * Arbitrary argument passed as the second parameter in each call to 'cb'. 220 * 221 * Returns: 222 * The return value of the last called callback. 223 */ 224 extern int yang_snodes_iterate_all(yang_iterate_cb cb, uint16_t flags, 225 void *arg); 226 227 /* 228 * Build schema path or data path of the schema node. 229 * 230 * snode 231 * libyang schema node to be processed. 232 * 233 * type 234 * Specify whether a schema path or a data path should be built. 235 * 236 * xpath 237 * Pointer to previously allocated buffer. 238 * 239 * xpath_len 240 * Size of the xpath buffer. 241 */ 242 extern void yang_snode_get_path(const struct lys_node *snode, 243 enum yang_path_type type, char *xpath, 244 size_t xpath_len); 245 246 /* 247 * Find first parent schema node which is a presence-container or a list 248 * (non-presence containers are ignored). 249 * 250 * snode 251 * libyang schema node to operate on. 252 * 253 * Returns: 254 * The parent libyang schema node if found, or NULL if not found. 255 */ 256 extern struct lys_node *yang_snode_real_parent(const struct lys_node *snode); 257 258 /* 259 * Find first parent schema node which is a list. 260 * 261 * snode 262 * libyang schema node to operate on. 263 * 264 * Returns: 265 * The parent libyang schema node (list) if found, or NULL if not found. 266 */ 267 extern struct lys_node *yang_snode_parent_list(const struct lys_node *snode); 268 269 /* 270 * Check if the libyang schema node represents typeless data (e.g. containers, 271 * leafs of type empty, etc). 272 * 273 * snode 274 * libyang schema node to operate on. 275 * 276 * Returns: 277 * true if the schema node represents typeless data, false otherwise. 278 */ 279 extern bool yang_snode_is_typeless_data(const struct lys_node *snode); 280 281 /* 282 * Get the default value associated to a YANG leaf or leaf-list. 283 * 284 * snode 285 * libyang schema node to operate on. 286 * 287 * Returns: 288 * The default value if it exists, NULL otherwise. 289 */ 290 extern const char *yang_snode_get_default(const struct lys_node *snode); 291 292 /* 293 * Get the type structure of a leaf of leaf-list. If the type is a leafref, the 294 * final (if there is a chain of leafrefs) target's type is found. 295 * 296 * snode 297 * libyang schema node to operate on. 298 * 299 * Returns: 300 * The found type if the schema node represents a leaf or a leaf-list, NULL 301 * otherwise. 302 */ 303 extern const struct lys_type *yang_snode_get_type(const struct lys_node *snode); 304 305 /* 306 * Build data path of the data node. 307 * 308 * dnode 309 * libyang data node to be processed. 310 * 311 * xpath 312 * Pointer to previously allocated buffer. 313 * 314 * xpath_len 315 * Size of the xpath buffer. 316 */ 317 extern void yang_dnode_get_path(const struct lyd_node *dnode, char *xpath, 318 size_t xpath_len); 319 320 /* 321 * Return the schema name of the given libyang data node. 322 * 323 * dnode 324 * libyang data node. 325 * 326 * xpath_fmt 327 * Optional XPath expression (absolute or relative) to specify a different 328 * data node to operate on in the same data tree. 329 * 330 * Returns: 331 * Schema name of the libyang data node. 332 */ 333 extern const char *yang_dnode_get_schema_name(const struct lyd_node *dnode, 334 const char *xpath_fmt, ...); 335 336 /* 337 * Find a libyang data node by its YANG data path. 338 * 339 * dnode 340 * Base libyang data node to operate on. 341 * 342 * xpath_fmt 343 * XPath expression (absolute or relative). 344 * 345 * Returns: 346 * The libyang data node if found, or NULL if not found. 347 */ 348 extern struct lyd_node *yang_dnode_get(const struct lyd_node *dnode, 349 const char *xpath_fmt, ...); 350 351 /* 352 * Check if a libyang data node exists. 353 * 354 * dnode 355 * Base libyang data node to operate on. 356 * 357 * xpath_fmt 358 * XPath expression (absolute or relative). 359 * 360 * Returns: 361 * true if the libyang data node was found, false otherwise. 362 */ 363 extern bool yang_dnode_exists(const struct lyd_node *dnode, 364 const char *xpath_fmt, ...); 365 366 /* 367 * Iterate over all libyang data nodes that satisfy an XPath query. 368 * 369 * cb 370 * Function to call with each data node. 371 * 372 * arg 373 * Arbitrary argument passed as the second parameter in each call to 'cb'. 374 * 375 * dnode 376 * Base libyang data node to operate on. 377 * 378 * xpath_fmt 379 * XPath expression (absolute or relative). 380 */ 381 void yang_dnode_iterate(yang_dnode_iter_cb cb, void *arg, 382 const struct lyd_node *dnode, const char *xpath_fmt, 383 ...); 384 385 /* 386 * Check if the libyang data node contains a default value. Non-presence 387 * containers are assumed to always contain a default value. 388 * 389 * dnode 390 * Base libyang data node to operate on. 391 * 392 * xpath_fmt 393 * Optional XPath expression (absolute or relative) to specify a different 394 * data node to operate on in the same data tree. 395 * 396 * Returns: 397 * true if the data node contains the default value, false otherwise. 398 */ 399 extern bool yang_dnode_is_default(const struct lyd_node *dnode, 400 const char *xpath_fmt, ...); 401 402 /* 403 * Check if the libyang data node and all of its children contain default 404 * values. Non-presence containers are assumed to always contain a default 405 * value. 406 * 407 * dnode 408 * libyang data node to operate on. 409 * 410 * Returns: 411 * true if the data node and all of its children contain default values, 412 * false otherwise. 413 */ 414 extern bool yang_dnode_is_default_recursive(const struct lyd_node *dnode); 415 416 /* 417 * Change the value of a libyang leaf node. 418 * 419 * dnode 420 * libyang data node to operate on. 421 * 422 * value 423 * String representing the new value. 424 */ 425 extern void yang_dnode_change_leaf(struct lyd_node *dnode, const char *value); 426 427 /* 428 * Create a new libyang data node. 429 * 430 * ly_ctx 431 * libyang context to operate on. 432 * 433 * config 434 * Specify whether the data node will contain only configuration data (true) 435 * or both configuration data and state data (false). 436 * 437 * Returns: 438 * Pointer to newly created libyang data node. 439 */ 440 extern struct lyd_node *yang_dnode_new(struct ly_ctx *ly_ctx, bool config_only); 441 442 /* 443 * Duplicate a libyang data node. 444 * 445 * dnode 446 * libyang data node to duplicate. 447 * 448 * Returns: 449 * Pointer to duplicated libyang data node. 450 */ 451 extern struct lyd_node *yang_dnode_dup(const struct lyd_node *dnode); 452 453 /* 454 * Delete a libyang data node. 455 * 456 * dnode 457 * Pointer to the libyang data node that is going to be deleted. 458 */ 459 extern void yang_dnode_free(struct lyd_node *dnode); 460 461 /* 462 * Create a new yang_data structure. 463 * 464 * xpath 465 * Data path of the YANG data. 466 * 467 * value 468 * String representing the value of the YANG data. 469 * 470 * Returns: 471 * Pointer to newly created yang_data structure. 472 */ 473 extern struct yang_data *yang_data_new(const char *xpath, const char *value); 474 475 /* 476 * Delete a yang_data structure. 477 * 478 * data 479 * yang_data to delete. 480 */ 481 extern void yang_data_free(struct yang_data *data); 482 483 /* 484 * Create a new linked list of yang_data structures. The list 'del' callback is 485 * initialized appropriately so that the entire list can be deleted safely with 486 * list_delete_and_null(). 487 * 488 * Returns: 489 * Pointer to newly created linked list. 490 */ 491 extern struct list *yang_data_list_new(void); 492 493 /* 494 * Find the yang_data structure corresponding to an XPath in a list. 495 * 496 * list 497 * list of yang_data structures to operate on. 498 * 499 * xpath_fmt 500 * XPath to search for (format string). 501 * 502 * Returns: 503 * Pointer to yang_data if found, NULL otherwise. 504 */ 505 extern struct yang_data *yang_data_list_find(const struct list *list, 506 const char *xpath_fmt, ...); 507 508 /* 509 * Create and set up a libyang context (for use by the translator) 510 * 511 * embedded_modules 512 * Specify whether libyang should attempt to look for embedded YANG modules. 513 */ 514 extern struct ly_ctx *yang_ctx_new_setup(bool embedded_modules); 515 516 /* 517 * Enable or disable libyang verbose debugging. 518 * 519 * enable 520 * When set to true, enable libyang verbose debugging, otherwise disable it. 521 */ 522 extern void yang_debugging_set(bool enable); 523 524 /* 525 * Print libyang error messages into the provided buffer. 526 * 527 * ly_ctx 528 * libyang context to operate on. 529 * 530 * buf 531 * Buffer to store the libyang error messages. 532 * 533 * buf_len 534 * Size of buf. 535 * 536 * Returns: 537 * The provided buffer. 538 */ 539 extern const char *yang_print_errors(struct ly_ctx *ly_ctx, char *buf, 540 size_t buf_len); 541 542 /* 543 * Initialize the YANG subsystem. Should be called only once during the 544 * daemon initialization process. 545 * 546 * embedded_modules 547 * Specify whether libyang should attempt to look for embedded YANG modules. 548 */ 549 extern void yang_init(bool embedded_modules); 550 551 /* 552 * Finish the YANG subsystem gracefully. Should be called only when the daemon 553 * is exiting. 554 */ 555 extern void yang_terminate(void); 556 557 /* 558 * API to return the parent dnode having a given schema-node name 559 * Use case: One has to access the parent dnode's private pointer 560 * for a given child node. 561 * For that there is a need to find parent dnode first. 562 * 563 * dnode The starting node to work on 564 * 565 * name The name of container/list schema-node 566 * 567 * Returns The dnode matched with the given name 568 */ 569 extern const struct lyd_node * 570 yang_dnode_get_parent(const struct lyd_node *dnode, const char *name); 571 572 573 /* 574 * In some cases there is a need to auto delete the parent nodes 575 * if the given node is last in the list. 576 * It tries to delete all the parents in a given tree in a given module. 577 * The use case is with static routes and route maps 578 * example : ip route 1.1.1.1/32 ens33 579 * ip route 1.1.1.1/32 ens34 580 * After this no ip route 1.1.1.1/32 ens34 came, now staticd 581 * has to find out upto which level it has to delete the dnodes. 582 * For this case it has to send delete nexthop 583 * After this no ip route 1.1.1.1/32 ens33 came, now staticd has to 584 * clear nexthop, path and route nodes. 585 * The same scheme is required for routemaps also 586 * dnode The starting node to work on 587 * 588 * Returns The final parent node selected for deletion 589 */ 590 extern const struct lyd_node * 591 yang_get_subtree_with_no_sibling(const struct lyd_node *dnode); 592 593 /* To get the relative position of a node in list */ 594 extern uint32_t yang_get_list_pos(const struct lyd_node *node); 595 596 /* To get the number of elements in a list 597 * 598 * dnode : The head of list 599 * Returns : The number of dnodes present in the list 600 */ 601 extern uint32_t yang_get_list_elements_count(const struct lyd_node *node); 602 603 604 /* To get the immediate child of a dnode */ 605 const struct lyd_node *yang_dnode_get_child(const struct lyd_node *dnode); 606 607 608 #ifdef __cplusplus 609 } 610 #endif 611 612 #endif /* _FRR_YANG_H_ */ 613