sysrepo / sysrepo-python

Python bindings for sysrepo
BSD 3-Clause "New" or "Revised" License
27 stars 21 forks source link

Question about using sysrepocfg.py #25

Closed fourwilling closed 2 years ago

fourwilling commented 2 years ago

Could you kindly provide an example of testing RPC calls using sysrepocfg.py? I have successfully send the RPC /sysrepo-example:poweroff using a simple Python script below, but I still have no clue how to use sysrepocfg.py.

import sysrepo
with sysrepo.SysrepoConnection() as conn:
    with conn.start_session() as sess:
        out = sess.rpc_send("sysrepo-example:poweroff", {"input-param":42})

Thank you!

rjarry commented 2 years ago

Hello,

this should be similar to sysrepocfg: https://netopeer.liberouter.org/doc/sysrepo/master/html/sysrepocfg.html

~# examples/sysrepoctl.py -i examples/sysrepo-example.yang
~# examples/application.py -v &
[1] 3909172
~# echo '{"sysrepo-example:poweroff": {"behaviour": "success"}' | examples/sysrepocfg.py -f json -R

========================
RPC call: /sysrepo-example:poweroff
params: {'behaviour': 'success'}
returning {'message': 'bye bye'}
---------------

{
  "sysrepo-example:poweroff": {
    "message": "bye bye"
  }
}
fourwilling commented 2 years ago

Hi rjarry,

I entered the command you provided on the /examples directory: echo '{"sysrepo-example:poweroff": {"behaviour": "success"}' | python3 sysrepocfg.py -f json -R

But I saw this error:

Traceback (most recent call last):
  File "sysrepocfg.py", line 138, in <module>
    sys.exit(main())
  File "sysrepocfg.py", line 118, in main
    strict=not args.not_strict,
  File "/usr/local/lib/python3.6/dist-packages/libyang/context.py", line 246, in parse_data_mem
    raise self.error("failed to parse data tree")
libyang.util.LibyangError: failed to parse data tree: Invalid JSON data (missing top-level end-object).

Could you help me discover what the problem really is?

Below is the version of Ubuntu I am running:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=18.04
DISTRIB_CODENAME=bionic
DISTRIB_DESCRIPTION="Ubuntu 18.04.6 LTS"
NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.6 LTS"
VERSION_ID="18.04"
HOME_URL=https://www.ubuntu.com/
SUPPORT_URL=https://help.ubuntu.com/
BUG_REPORT_URL=https://bugs.launchpad.net/ubuntu/
PRIVACY_POLICY_URL=https://www.ubuntu.com/legal/terms-and-policies/privacy-policy
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

I use Python 3.6.9, and installed Python packages are:

asn1crypto (0.24.0)
cffi (1.11.5)
cryptography (2.1.4)
idna (2.6)
keyring (10.6.0)
keyrings.alt (3.0)
libyang (1.7.0)
pip (9.0.1)
ply (3.11)
pycparser (2.18)
pycrypto (2.6.1)
pygobject (3.26.1)
pyxdg (0.25)
SecretStorage (2.3.1)
setuptools (39.0.1)
six (1.11.0)
sysrepo (0.6.0)
wheel (0.30.0)

Thank you!

rjarry commented 2 years ago

Sorry I missed one closing brace. This is invalid JSON as the error message indicates.

fourwilling commented 2 years ago

Hi rjarry,

Thanks for your reply. Can I ask two more basic questions?

I tried this command: ~# python3 sysrepocfg.py -X '/sysrepo-example:*//.' It seemed that I only got "conf", but no "status".

Do you know how to get the same result as the script below using sysrepocfg.py?

with sysrepo.SysrepoConnection() as conn:
    with conn.start_session("operational") as sess:
        data = sess.get_data("/sysrepo-example:status")

Also, I want to do module config replacement with the script below:

with sysrepo.SysrepoConnection() as conn:
    with conn.start_session() as sess:
        sess.replace_config({"system": {"hostname": "foobar"}}, "example-sysrepo")

But it didn't work. What do I need to replace the string "system" with?

Thank you!

rjarry commented 2 years ago

By default, sysrepocfg.py uses the running datastore. You must access the operational datastore for operational status data.

python3 sysrepocfg.py -d operational -X '/sysrepo-example:*//.'

Please read the official sysrepo documentation: https://netopeer.liberouter.org/doc/sysrepo/master/html/

As for your other problem, I cannot reply unless I see your example-sysrepo.yang yang model.

fourwilling commented 2 years ago

Hi rjarry,

Sorry, my yang model should be sysrepo-example.yang. It is the same as provided in the Github page:

https://github.com/sysrepo/sysrepo-python/blob/master/examples/sysrepo-example.yang

rjarry commented 2 years ago

replace_config requires valid data. Please read the RFCs.

https://datatracker.ietf.org/doc/html/rfc7950 https://datatracker.ietf.org/doc/html/rfc6241

fourwilling commented 2 years ago

Hi rjarry,

I have read some chapters of the two documents, but still don't quite understand what you mean. Doesn't sysrepo-example.yang map to valid data?

Thank you.

rjarry commented 2 years ago

There is no root system container in the YANG model. Only conf and state. The data you are sending is invalid.

Please read the RFCs extensively to familiarize yourself with YANG models and YANG data. Also have a look at the multiple unit tests available in this repository and the original sysrepo project.

https://github.com/sysrepo/sysrepo-python/tree/master/tests https://github.com/sysrepo/sysrepo/tree/master/tests https://github.com/sysrepo/sysrepo/tree/master/examples

I'm sorry but I cannot learn all this for you :)

fourwilling commented 2 years ago

Thank you. Solved!