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

完畢。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM