sysrepo简单使用

摘自:https://blog.csdn.net/qq_27923047/article/details/108069409

sysrepo是一种基于YANG模型的数据库管理系统,基于NETCONF的netopeer2就是使用sysrepo进行数据库管理的。当前有两种方式对sysrepo数据库进行访问:独立进程方式(直接方式)和插件方式(间接方式)。独立进程方式其实就是相关文件中包含main函数,编译形成独立可执行程序,插件方式其实就是编译生成动态链接库.so,将其放入plugin目录下,启动sysrepo-plugind程序后,该插件就会生效。

直接方式使用sysrepo

以sysrepo项目给出的examples例子为例:
examples.yang在sysrepo/examples目录下,定义为

module examples {
    namespace "urn:examples";
    prefix e;

    container cont {
        leaf l {
            type string;
        }
    }

    container stats {
        config false;
        leaf counter {
            type uint64;
        }

        leaf counter2 {
            type uint64;
        }
    }

    rpc oper {
        input {
            leaf arg {
                type string;
            }
        }

        output {
            leaf ret {
                type int64;
            }
        }
    }

    notification notif {
        leaf val {
            type decimal64 {
                fraction-digits 2;
            }
        }
    }
}
 
  1. 安装yang文件,sysrepo/examples目录下,执行
# ./sysrepoctl -i examples.yang
  1. 配置数据

监听examples模型运行态配置变化:

# ./application_changes_example examples

另启一个终端,设置examples模型中cont容器中的l节点值为value:

# ./sr_set_item_example /examples:cont/l value
  1. 操作状态数据
# ./oper_data_example examples /examples:stats

获取examples的operational数据

# ./sr_get_items_example /examples:*//. operational
  1. RPCs

注册/examples:oper的RPC监听进程

# ./rpc_subscribe_example /examples:oper

进行RPC操作

# ./rpc_send_example /examples:oper
  1. Notifications

注册examples的事件监听进程

# ./notif_subscribe_example examples

进行notifications相关操作

# ./notif_send_example /examples:notif val 25.22

可以用sysrepocfg命令查看module信息

  1. 输出正在运行的module为examples的数据库数据
# sysrepocfg -d running -m examples -X

可以使用netopeer2对module进行操作
启动netopeer2-server

[root@localhost examples]# netopeer2-server -d -v3
[INF]: LY: Plugin "/usr/local/lib64/libyang1/extensions/nacm.so" successfully loaded.
[INF]: LY: Plugin "/usr/local/lib64/libyang1/extensions/metadata.so" successfully loaded.
[INF]: LY: Plugin "/usr/local/lib64/libyang1/extensions/yangdata.so" successfully loaded.
[INF]: LY: Plugin "/usr/local/lib64/libyang1/extensions/libyang_ext_test.so" successfully loaded.
[INF]: LY: Plugin "/usr/local/lib64/libyang1/user_types/user_yang_types.so" successfully loaded.
[INF]: LY: Plugin "/usr/local/lib64/libyang1/user_types/user_inet_types.so" successfully loaded.
......
[INF]: LY: Searching for "examples" in /home/renzg/netopeer2/libnetconf2/sysrepo-master/build/repository/yang.
[INF]: LY: Loading schema from "/home/renzg/netopeer2/libnetconf2/sysrepo-master/build/repository/yang/examples.yang" file.
[INF]: LY: Module "examples" successfully parsed as implemented.
[INF]: SR: Session 2 (user "root") created.
......
[INF]: LN: Schema "examples" was requested.
[INF]: LY: Resolving unresolved data nodes and their constraints...
[INF]: LY: All data nodes and constraints resolved.
[INF]: NP: Session 3: thread 0 event new RPC.

启动netopeer2-cli,并查看examples的running配置信息

[root@localhost examples]# netopeer2-cli 
> connect localhost
Interactive SSH Authentication
Type your password:
Password: 
> get-config --source running --filter-xpath /examples:*
DATA
<cont xmlns="urn:examples">
  <l>vlaue</l>
</cont>

使用user-rpc命令:

> user-rpc

编辑内容:

<oper xmlns="urn:examples">
    <arg>rpcs</arg>
</oper>

保存退出:

> user-rpc 
DATA
<ret xmlns="urn:examples">-123456</ret>
netopeer2-server端输出信息为:
[INF]: SR: Published event "rpc" "/examples:oper" with ID 3 priority 0 for 1 subscribers.
[INF]: SR: Event "rpc" with ID 3 priority 0 succeeded.
[INF]: LY: Resolving unresolved data nodes and their constraints...
[INF]: LY: All data nodes and constraints resolved.
[INF]: NP: Session 3: thread 3 event new RPC.

间接方式使用sysrepo

以sysrepo项目给出的oven例子为例:
oven.yang在sysrepo/examples/plugin目录下,定义为

module oven {
    namespace "urn:sysrepo:oven";
    prefix ov;

    revision 2018-01-19 {
        description "Initial revision.";
    }

    typedef oven-temperature {
        description "Temperature range that is accepted by the oven.";
        type uint8 {
            range "0..250";
        }
    }

    container oven {
        description "Configuration container of the oven.";

        leaf turned-on {
            description "Main switch determining whether the oven is on or off.";
            type boolean;
            default false;
        }

        leaf temperature {
            description "Slider for configuring the desired temperature.";
            type oven-temperature;
            default 0;
        }
    }

    container oven-state {
        description "State data container of the oven.";
        config false;

        leaf temperature {
            description "Actual temperature inside the oven.";
            type oven-temperature;
        }

        leaf food-inside {
            description "Informs whether the food is inside the oven or not.";
            type boolean;
        }
    }

    rpc insert-food {
        description "Operation to order the oven to put the prepared food inside.";

        input {
            leaf time {
                description "Parameter determining when to perform the operation.";
                type enumeration {
                    enum now {
                        description "Put the food in the oven immediately.";
                    }
                    enum on-oven-ready {
                        description
                            "Put the food in once the temperature inside
                             the oven is at least the configured one. If it
                             is already, the behaviour is similar to 'now'.";
                    }
                }
            }
        }
    }

    rpc remove-food {
        description "Operation to order the oven to take the food out.";
    }

    notification oven-ready {
        description
            "Event of the configured temperature matching the actual
             temperature inside the oven. If the configured temperature
             is lower than the actual one, no notification is generated
             when the oven cools down to the configured temperature.";
    }
}
 

sysrepo编译完成后,会在examples目录下生成oven.so共享库,将其放入plugins路径下

# cp sesrepo/examples/oven.so /usr/local/bin

安装oven.yang

# sysrepoctl -i sysrepo/examples/plugin/oven.yang

开启sysrepo-plugind

# sysrepo-plugind -d -v3

使用sysrepo/build/examples目录下的notif_subscribe_example程序注册oven的notifications

# ./notif_subscribe_example oven

使用rpc_subscribe_example监听oven的RPC事件

# ./rpc_subscribe_example /oven:insert-food

使用sysrepocfg命令操作RPC

# sysrepocfg --rpc=vim

编辑内容

<insert-food xmlns="urn:sysrepo:oven">
    <time>on-oven-ready</time>
</insert-food>
保存退出,监听端输出内容
[root@localhost examples]# ./rpc_subscribe_example /oven:insert-food
Application will subscribe to "/oven:insert-food" RPC.

 ========== LISTENING FOR RPC ==========

 ========== RPC "/oven:insert-food" RECEIVED: =======================

/oven:insert-food/time = on-oven-ready
 

使用netopeer对module进行操作

开启netopeer2-server和netopeer2-cli,在netopeer2-cli界面进行操作:

[root@localhost examples]# netopeer2-cli 
> connect localhost
Interactive SSH Authentication
Type your password:
Password: 
> user-rpc 
OK

输入user-rpc后,输入内容:

<insert-food xmlns="urn:sysrepo:oven">
    <time>on-oven-ready</time>
</insert-food>

保存退出,显示OK,

监听端输出相同内容:

[root@localhost examples]# ./rpc_subscribe_example /oven:insert-food
Application will subscribe to "/oven:insert-food" RPC.

 ========== LISTENING FOR RPC ==========

 ========== RPC "/oven:insert-food" RECEIVED: =======================

/oven:insert-food/time = on-oven-ready

完毕。

原文地址:https://www.cnblogs.com/LiuYanYGZ/p/14271288.html